void editor::_editor() { //create the window keepUserdat=new std::vector<std::pair<unsigned,int64_t>>; luaCallback=new std::vector<std::pair<std::string,int64_t>>; startLuaConf(); menu = new Fl_Menu_Bar(0,0,800,24);//Create menubar, items.. lua_getglobal(Lconf,"generateMenu"); runLuaFunc(Lconf,0,1); { unsigned menucnt; std::vector<Fl_Menu_Item> tmp; if(!(menucnt=lua_rawlen(Lconf,-1))) { fl_alert("generateMenu must return a table"); exit(1); } for(unsigned i=1; i<=menucnt; ++i) { lua_rawgeti(Lconf,-1,i); unsigned len=lua_rawlen(Lconf,-1); if(len) { Fl_Menu_Item mi{0}; lua_rawgeti(Lconf,-1,1); char*txt=(char*)luaL_optstring(Lconf,-1,0); if(txt) txt=strdup(txt); mi.text=txt; lua_pop(Lconf,1); if(len>=2) { lua_rawgeti(Lconf,-1,2); mi.shortcut_=luaL_optinteger(Lconf,-1,0); lua_pop(Lconf,1); } else mi.shortcut_=0; int64_t userdat; if(len>=4) { lua_rawgeti(Lconf,-1,4); userdat=luaL_optinteger(Lconf,-1,0); lua_pop(Lconf,1); } else userdat=0; if(len>=3) { lua_rawgeti(Lconf,-1,3); if(lua_type(Lconf,-1)==LUA_TSTRING) { luaCallback->emplace_back(std::make_pair(lua_tostring(Lconf,-1),userdat)); mi.callback_=callLuaCB; mi.user_data_=&(*luaCallback)[luaCallback->size()-1]; } else if(lua_type(Lconf,-1)==LUA_TNUMBER) { int64_t lcall=lua_tointeger(Lconf,-1); if(lcall>0) { keepUserdat->emplace_back(std::make_pair(lcall-1,userdat)); mi.callback_=callCFuncLua; mi.user_data_=&(*keepUserdat)[keepUserdat->size()-1]; } else mi.callback_=0; } else { fl_alert("Error invalid type for callback %s",lua_typename(Lconf,lua_type(Lconf,-1))); exit(1); } lua_pop(Lconf,1); } else { mi.callback_=0; mi.user_data_=0; } if(len>=5) { lua_rawgeti(Lconf,-1,5); mi.flags=luaL_optinteger(Lconf,-1,0); lua_pop(Lconf,1); } else mi.flags=0; tmp.emplace_back(mi); } else tmp.emplace_back(Fl_Menu_Item{0}); lua_pop(Lconf,1); } unsigned cidx=0,lidx=0; for(unsigned i=0; i<menucnt; ++i) { //While resizing the vector the addresses may have changed. lua_rawgeti(Lconf,-1,i+1); unsigned len=lua_rawlen(Lconf,-1); if(len>=3) { lua_rawgeti(Lconf,-1,3); if(lua_type(Lconf,-1)==LUA_TSTRING) { tmp[i].user_data_=&(*luaCallback)[lidx++]; } else if(lua_type(Lconf,-1)==LUA_TNUMBER) { int64_t lcall=lua_tointeger(Lconf,-1); if(lcall>0) tmp[i].user_data_=&(*keepUserdat)[cidx++]; } lua_pop(Lconf,1); } lua_pop(Lconf,1); } lua_pop(Lconf,1); menu->copy(tmp.data()); } tile_placer_tile_offset_y=default_tile_placer_tile_offset_y; true_color_box_x=default_true_color_box_x; true_color_box_y=default_true_color_box_y; tile_edit_truecolor_off_x=default_tile_edit_truecolor_off_x; tile_edit_truecolor_off_y=default_tile_edit_truecolor_off_y; std::fill(tabsHidden,&tabsHidden[shareAmtPj],false); { the_tabs = new Fl_Tabs(0, 24, 800, 576); the_tabs->callback(set_mode_tabs); int rx,ry,rw,rh; the_tabs->client_area(rx,ry,rw,rh); { tabsMain.emplace_back(new Fl_Group(rx, ry, rw, rh, "Palette editor")); //stuff related to this group should go here palBar.addTab(0,true); pal_size = new Fl_Hor_Value_Slider(128,384,320,24,"Palette box size"); pal_size->minimum(1); pal_size->maximum(42); pal_size->step(1); pal_size->value(32); pal_size->align(FL_ALIGN_LEFT); pal_size->callback(redrawOnlyCB); ditherPower = new Fl_Hor_Value_Slider(128,416,320,24,"Dither Power"); ditherPower->tooltip("A lower value results in more dithering artifacts a higher value results in less artifacts"); ditherPower->minimum(1); ditherPower->maximum(256); ditherPower->step(1); ditherPower->value(16); ditherPower->align(FL_ALIGN_LEFT); ditherPower->callback(setSubditherSetting); gameSysSel=new Fl_Choice(96, 312, 192, 24); gameSysSel->copy(gameSysMenuSel); ditherAlgSel=new Fl_Choice(64, 464, 144, 24); ditherAlgSel->copy(ditherChoices); subSysC=new Fl_Choice(208, 464, 128, 24); subSysC->copy(subSysGenesis); subSysC->value(0); { Fl_Group *o = new Fl_Group(306, 188, 88, 96); { palType[0] = new Fl_Round_Button(306, 188, 64, 32, "Free"); palType[0]->type(FL_RADIO_BUTTON); palType[0]->set(); palType[0]->callback((Fl_Callback*) setPalType,(void *)0); palType[0]->tooltip(freeDes); palType[1] = new Fl_Round_Button(306, 220, 72, 32, "Locked"); palType[1]->type(FL_RADIO_BUTTON); palType[1]->callback((Fl_Callback*) setPalType,(void *)1); palType[1]->tooltip(lockedDes); palType[2] = new Fl_Round_Button(306, 252, 88, 32, "Reserved"); palType[2]->type(FL_RADIO_BUTTON); palType[2]->callback((Fl_Callback*) setPalType,(void *)2); palType[2]->tooltip(reservedDes); o->end(); } // End of buttons }//end of group lua_getglobal(Lconf,"tabConfig"); lua_pushinteger(Lconf,pal_edit); runLuaFunc(Lconf,1,0); tabsMain[pal_edit]->end(); } // Fl_Group* o { tabsMain.emplace_back(new Fl_Group(rx, ry, rw, rh, "Tile editor")); //stuff related to this group should go here { Fl_Group* o = new Fl_Group(0, 0, 800, 567); palRTE[0] = new Fl_Round_Button(384, default_palette_bar_offset_y+40, 56, 32, "Row 0"); palRTE[0]->type(FL_RADIO_BUTTON); palRTE[0]->set(); palRTE[0]->callback((Fl_Callback*) set_tile_row,(void *)0); palRTE[1] = new Fl_Round_Button(448, default_palette_bar_offset_y+40, 56, 32, "Row 1"); palRTE[1]->type(FL_RADIO_BUTTON); palRTE[1]->callback((Fl_Callback*) set_tile_row,(void *)1); palRTE[2] = new Fl_Round_Button(512, default_palette_bar_offset_y+40, 56, 32, "Row 2"); palRTE[2]->type(FL_RADIO_BUTTON); palRTE[2]->callback((Fl_Callback*) set_tile_row,(void *)2); palRTE[3] = new Fl_Round_Button(576, default_palette_bar_offset_y+40, 56, 32, "Row 3"); palRTE[3]->type(FL_RADIO_BUTTON); palRTE[3]->callback((Fl_Callback*) set_tile_row,(void *)3); o->end(); } // Fl_Group* o { Fl_Button *o = new Fl_Button(538, default_palette_bar_offset_y, 104, 32, "Append tile");//these button should be inline with the palette bar o->tooltip("This will append a blank tile to the tile buffer in the ram."); o->callback(new_tile); } { Fl_Button *o = new Fl_Button(656, default_palette_bar_offset_y, 140, 32, "Delete selected tile"); o->tooltip("This button will delete the currently selected tile"); o->callback(delete_tile_at_location); o->labelsize(12); } { Fl_Button *o = new Fl_Button(656, default_palette_bar_offset_y+34,140, 32,"Insert after tile"); o->callback(insertTileCB); } palBar.addTab(1); rgb_red = new Fl_Hor_Value_Slider(64,default_palette_bar_offset_y+136,128,24,"RGB red"); rgb_red->minimum(0); rgb_red->maximum(255); rgb_red->step(1); rgb_red->value(0); rgb_red->align(FL_ALIGN_LEFT); rgb_red->callback(update_truecolor,(void *)0); rgb_green = new Fl_Hor_Value_Slider(240,default_palette_bar_offset_y+136,128,24,"Green"); rgb_green->minimum(0); rgb_green->maximum(255); rgb_green->step(1); rgb_green->value(0); rgb_green->align(FL_ALIGN_LEFT); rgb_green->callback(update_truecolor,(void *)1); rgb_blue = new Fl_Hor_Value_Slider(402,default_palette_bar_offset_y+136,128,24,"Blue"); rgb_blue->minimum(0); rgb_blue->maximum(255); rgb_blue->step(1); rgb_blue->value(0); rgb_blue->align(FL_ALIGN_LEFT); rgb_blue->callback(update_truecolor,(void *)2); rgb_alpha = new Fl_Hor_Value_Slider(576,default_palette_bar_offset_y+136,128,24,"Alpha"); rgb_alpha->minimum(0); rgb_alpha->maximum(255); rgb_alpha->step(1); rgb_alpha->value(0); rgb_alpha->align(FL_ALIGN_LEFT); rgb_alpha->callback(update_truecolor,(void *)3); { Fl_Group *o = new Fl_Group(304, 96, 88, 96); { palType[3] = new Fl_Round_Button(304, 96, 64, 32, "Free"); palType[3]->type(FL_RADIO_BUTTON); palType[3]->set(); palType[3]->callback((Fl_Callback*) setPalType,(void *)0); palType[3]->tooltip(freeDes); palType[4] = new Fl_Round_Button(304, 128, 72, 32, "Locked"); palType[4]->type(FL_RADIO_BUTTON); palType[4]->callback((Fl_Callback*) setPalType,(void *)1); palType[4]->tooltip(lockedDes); palType[5] = new Fl_Round_Button(304, 160, 88, 32, "Reserved"); palType[5]->type(FL_RADIO_BUTTON); palType[5]->callback((Fl_Callback*) setPalType,(void *)2); palType[5]->tooltip(reservedDes); o->end(); } // End of buttons }//end of group { Fl_Check_Button* o = new Fl_Check_Button(694,default_palette_bar_offset_y+68,100,32,"Show grid?"); o->callback(set_grid); o->tooltip("This button Toggles weather or not you which to see a grid while editing your tiles. A grid can help you see the spacing between each pixel."); } tile_edit_offset_x=default_tile_edit_offset_x; tile_edit_offset_y=default_tile_edit_offset_y; tile_size = new Fl_Hor_Value_Slider(448,default_palette_bar_offset_y+72,242,24,"Tile zoom"); tile_size->tooltip(TooltipZoom); tile_size->minimum(1); tile_size->maximum(64); tile_size->step(1); tile_size->value(46); tile_size->align(FL_ALIGN_LEFT); tile_size->callback(update_offset_tile_edit); //now for the tile select slider tile_select = new Fl_Hor_Value_Slider(480,default_palette_bar_offset_y+104,312,24,"Tile select"); tile_select->tooltip("This slider selects which tile that you are editing the first tile is zero"); tile_select->minimum(0); tile_select->maximum(0); tile_select->step(1); tile_select->align(FL_ALIGN_LEFT); tile_select->callback(set_tile_current); lua_getglobal(Lconf,"tabConfig"); lua_pushinteger(Lconf,tile_edit); runLuaFunc(Lconf,1,0); tabsMain[tile_edit]->end(); } { tabsMain.emplace_back(new Fl_Group(rx,ry,rw,rh,"Plane mapping/block editor")); { Fl_Group* o = new Fl_Group(tile_place_buttons_x_off, 192, 60, 128); palRTE[4] = new Fl_Round_Button(tile_place_buttons_x_off, 192, 60, 28, "Row 0"); palRTE[4]->type(FL_RADIO_BUTTON); palRTE[4]->set(); palRTE[4]->callback((Fl_Callback*) set_tile_row,(void *)0); palRTE[5] = new Fl_Round_Button(tile_place_buttons_x_off, 220, 60, 28, "Row 1"); palRTE[5]->type(FL_RADIO_BUTTON); palRTE[5]->callback((Fl_Callback*) set_tile_row,(void *)1); palRTE[6] = new Fl_Round_Button(tile_place_buttons_x_off, 248, 60, 28, "Row 2"); palRTE[6]->type(FL_RADIO_BUTTON); palRTE[6]->callback((Fl_Callback*) set_tile_row,(void *)2); palRTE[7] = new Fl_Round_Button(tile_place_buttons_x_off, 276, 60, 28, "Row 3"); palRTE[7]->type(FL_RADIO_BUTTON); palRTE[7]->callback((Fl_Callback*) set_tile_row,(void *)3); o->end(); } // Fl_Group* o planeSelect=new Fl_Choice(408,default_palette_bar_offset_y+56,112,24,"Plane selection"); planeSelect->align(FL_ALIGN_TOP); { Fl_Button *o = new Fl_Button(408, default_palette_bar_offset_y+80,112,24,"Add plane"); o->callback(addPlaneTilemap); } { Fl_Button *o = new Fl_Button(408, default_palette_bar_offset_y+104,112,24,"Remove plane"); o->callback(removeTilemapsPlane); } curPlaneName = new Fl_Input(tile_place_buttons_x_off+616,56,168,24,"Plane name"); curPlaneName->value("0"); curPlaneName->callback(updateNameTilemaps); map_w = new Fl_Int_Input(608,default_palette_bar_offset_y+72,184,24,MapWidthTxt); map_w->when(FL_WHEN_ENTER_KEY); map_w->value("2"); map_w->align(FL_ALIGN_LEFT); map_w->callback(callback_resize_map); map_h = new Fl_Int_Input(608,default_palette_bar_offset_y+104,184,24,MapHeightTxt); map_h->when(FL_WHEN_ENTER_KEY); map_h->value("2"); map_h->align(FL_ALIGN_LEFT); map_h->callback(callback_resize_map); map_amt = new Fl_Int_Input(480,default_palette_bar_offset_y+136,312,24,"Blocks"); map_amt->value("1"); map_amt->align(FL_ALIGN_LEFT); map_amt->callback(blocksAmtCB); map_amt->hide(); map_x_scroll = new Fl_Scrollbar(default_map_off_x-32, default_map_off_y-32, 800-8-default_map_off_x, 24); map_x_scroll->value(0,0,0,0); map_x_scroll->type(FL_HORIZONTAL); map_x_scroll->tooltip("Use this scroll bar to move around the tile map if you are zoomed in and there is not enough room to display the entire tilemap at once. This scroll bar will move the map left and right."); map_x_scroll->callback(update_map_scroll_x); map_x_scroll->hide(); map_x_scroll->linesize(1); map_y_scroll = new Fl_Scrollbar(default_map_off_x-32, default_map_off_y, 24, 600-8-default_map_off_y); map_y_scroll->value(0,0,0,0); //map_x_scroll->type(FL_HORIZONTAL); map_y_scroll->tooltip("Use this scroll bar to move around the tile map if you are zoomed in and there is not enough room to display the entire tilemap at once. This scroll bar will move the map up and down."); map_y_scroll->callback(update_map_scroll_y); map_y_scroll->hide(); map_y_scroll->linesize(1); //now for the tile select slider tile_select_2 = new Fl_Hor_Value_Slider(528,default_palette_bar_offset_y+40,264,24,"Tile select"); tile_select_2->tooltip("This slider allows you to choice which tile you would like to place on the map remember you can both horizontally and vertically flip the tile once placed on the map and select which row the tile uses"); tile_select_2->minimum(0); tile_select_2->maximum(0); tile_select_2->step(1); tile_select_2->align(FL_ALIGN_TOP_LEFT); tile_select_2->callback(set_tile_currentTP); totalTiles=new Fl_Box(600,default_palette_bar_offset_y,128,64); totalTiles->labelsize(12); palBar.addTab(2); //buttons for tile settings { Fl_Group *o = new Fl_Group(304, 96, 88, 96); { palType[6] = new Fl_Round_Button(304, 90, 64, 32, "Free"); palType[6]->type(FL_RADIO_BUTTON); palType[6]->set(); palType[6]->callback((Fl_Callback*) setPalType,(void *)0); palType[6]->tooltip(freeDes); palType[7] = new Fl_Round_Button(304, 122, 72, 32, "Locked"); palType[7]->type(FL_RADIO_BUTTON); palType[7]->callback((Fl_Callback*) setPalType,(void *)1); palType[7]->tooltip(lockedDes); palType[8] = new Fl_Round_Button(304, 154, 88, 32, "Reserved"); palType[8]->type(FL_RADIO_BUTTON); palType[8]->callback((Fl_Callback*) setPalType,(void *)2); palType[8]->tooltip(reservedDes); o->end(); } // End of buttons }//end of group hflipCB[0] = new Fl_Check_Button(tile_place_buttons_x_off,304,64,32,"Hflip"); hflipCB[0]->callback(set_hflipCB,(void*)0); hflipCB[0]->tooltip("This sets whether or not the tile is flipped horizontally"); vflipCB[0] = new Fl_Check_Button(tile_place_buttons_x_off,336,64,32,"Vflip"); vflipCB[0]->callback(set_vflipCB,(void*)0); vflipCB[0]->tooltip("This sets whether or not the tile is flipped vertically"); prioCB[0] = new Fl_Check_Button(tile_place_buttons_x_off,368,72,32,"Priority"); prioCB[0]->callback(set_prioCB,(void*)0); prioCB[0]->tooltip("If checked tile is high priority"); { Fl_Check_Button* o = new Fl_Check_Button(tile_place_buttons_x_off,400,96,32,"Show grid?"); o->callback(set_grid_placer); o->tooltip("This button toggles whether or not a grid is visible over the tile map this will allow you to easily see were each tile is"); } BlocksCBtn = new Fl_Check_Button(tile_place_buttons_x_off,432,96,32,"Blocks?"); BlocksCBtn->callback(toggleBlocksCB); BlocksCBtn->tooltip("Toggles if tile map is treated as blocks"); { Fl_Check_Button* o = new Fl_Check_Button(tile_place_buttons_x_off,464,192,32,"Show only selected row"); o->callback(toggleRowSolo); o->tooltip("When checked tiles that do not use the selected row will not be drawn"); } place_tile_size = new Fl_Hor_Value_Slider(tile_place_buttons_x_off,512,168,24,"Tile zoom factor:"); place_tile_size->minimum(1); place_tile_size->maximum(16); place_tile_size->step(1); place_tile_size->value(12); place_tile_size->align(FL_ALIGN_TOP); place_tile_size->callback(update_map_size); place_tile_size->tooltip(TooltipZoom); tmapOffset = new Fl_Int_Input(tile_place_buttons_x_off,552,168,24,"Tile offset"); tmapOffset->when(FL_WHEN_ENTER_KEY); tmapOffset->value("0"); tmapOffset->align(FL_ALIGN_TOP); tmapOffset->callback(setTmapOffsetCB); cordDisp[0]=new Fl_Box(tile_place_buttons_x_off,556,128,64); cordDisp[0]->labelsize(12); lua_getglobal(Lconf,"tabConfig"); lua_pushinteger(Lconf,tile_place); runLuaFunc(Lconf,1,0); tabsMain[tile_place]->end(); } { tabsMain.emplace_back(new Fl_Group(rx,ry,rw,rh,"Chunk editor")); useBlocksChunkCBtn=new Fl_Check_Button(8, 48, 152, 24, "Use blocks"); useBlocksChunkCBtn->callback(useBlocksCB); chunkX = new Fl_Scrollbar(DefaultChunkX-32, DefaultChunkY-32, 800-DefaultChunkX+24, 24); chunkX->value(0,0,0,0); chunkX->type(FL_HORIZONTAL); chunkX->callback(scrollChunkX); chunkX->hide(); chunkY = new Fl_Scrollbar(DefaultChunkX-32, DefaultChunkY, 24, 600-8-DefaultChunkY); chunkY->value(0,0,0,0); chunkY->callback(scrollChunkY); chunkY->hide(); chunk_select = new Fl_Hor_Value_Slider(tile_place_buttons_x_off,88,160,24,"Chunk select"); chunk_select->minimum(0); chunk_select->maximum(0); chunk_select->step(1); chunk_select->value(0); chunk_select->align(FL_ALIGN_TOP); chunk_select->callback(currentChunkCB); tile_select_3 = new Fl_Hor_Value_Slider(tile_place_buttons_x_off,136,160,24,"Tile select"); tile_select_3->minimum(0); tile_select_3->maximum(0); tile_select_3->step(1); tile_select_3->align(FL_ALIGN_TOP); tile_select_3->callback(selBlockCB); hflipCB[1] = new Fl_Check_Button(tile_place_buttons_x_off,160,64,32,"Hflip"); hflipCB[1]->callback(set_hflipCB,(void*)1); vflipCB[1] = new Fl_Check_Button(tile_place_buttons_x_off,192,64,32,"Vflip"); vflipCB[1]->callback(set_vflipCB,(void*)1); prioCB[1] = new Fl_Check_Button(tile_place_buttons_x_off,224,72,32,"Priority"); prioCB[1]->callback(set_prioCB,(void*)1); solidChunkMenu=new Fl_Choice(tile_place_buttons_x_off,256,128,24); solidChunkMenu->copy(SolidMenu); chunksize[0]=new Fl_Int_Input(tile_place_buttons_x_off,296,128,24,"Width (in tiles)"); chunksize[0]->when(FL_WHEN_ENTER_KEY); chunksize[0]->align(FL_ALIGN_TOP); chunksize[0]->callback(resizeChunkCB); chunksize[0]->value("16"); chunksize[1]=new Fl_Int_Input(tile_place_buttons_x_off,336,128,24,"Height (in tiles)"); chunksize[1]->when(FL_WHEN_ENTER_KEY); chunksize[1]->align(FL_ALIGN_TOP); chunksize[1]->callback(resizeChunkCB); chunksize[1]->value("16"); planeSelectChunk = new Fl_Hor_Value_Slider(tile_place_buttons_x_off,480,160,24,"Plane select"); planeSelectChunk->minimum(0); planeSelectChunk->maximum(0); planeSelectChunk->step(1); planeSelectChunk->align(FL_ALIGN_TOP); planeSelectChunk->callback(setCurPlaneChunkCB); chunk_tile_size = new Fl_Hor_Value_Slider(tile_place_buttons_x_off,520,160,24,"Tile zoom factor:"); chunk_tile_size->minimum(1); chunk_tile_size->maximum(16); chunk_tile_size->step(1); chunk_tile_size->value(2); chunk_tile_size->align(FL_ALIGN_TOP); chunk_tile_size->callback(scrollChunkCB); chunk_tile_size->tooltip(TooltipZoom); cordDisp[1]=new Fl_Box(tile_place_buttons_x_off,556,128,64); cordDisp[1]->labelsize(12); { Fl_Button *o = new Fl_Button(tile_place_buttons_x_off,364, 112, 32, "Append chunk"); o->callback(appendChunkCB); } { Fl_Button *o = new Fl_Button(tile_place_buttons_x_off,396, 128, 32, "Insert after chunk"); o->callback(insertChunkCB); } { Fl_Button *o = new Fl_Button(tile_place_buttons_x_off,428, 160, 32, "Delete selected chunk"); o->callback(delChunkAtCB); } updateChunkSize(); lua_getglobal(Lconf,"tabConfig"); lua_pushinteger(Lconf,chunkEditor); runLuaFunc(Lconf,1,0); tabsMain[chunkEditor]->end(); } { tabsMain.emplace_back(new Fl_Group(rx,ry,rw,rh,"Sprites")); palBar.addTab(3,false,true,true); { Fl_Group *o = new Fl_Group(tile_place_buttons_x_off+616, 44, 88, 96); { palType[9] = new Fl_Round_Button(tile_place_buttons_x_off+616, 44, 64, 24, "Free"); palType[9]->type(FL_RADIO_BUTTON); palType[9]->set(); palType[9]->callback((Fl_Callback*) setPalType,(void *)0); palType[9]->tooltip(freeDes); palType[10] = new Fl_Round_Button(tile_place_buttons_x_off+616, 68, 72, 24, "Locked"); palType[10]->type(FL_RADIO_BUTTON); palType[10]->callback((Fl_Callback*) setPalType,(void *)1); palType[10]->tooltip(lockedDes); palType[11] = new Fl_Round_Button(tile_place_buttons_x_off+616, 92, 88, 24, "Reserved"); palType[11]->type(FL_RADIO_BUTTON); palType[11]->callback((Fl_Callback*) setPalType,(void *)2); palType[11]->tooltip(reservedDes); o->end(); } } spritegrouptxt = new Fl_Input(tile_place_buttons_x_off+616,128,168,24,"Group name"); spritegrouptxt->align(FL_ALIGN_TOP); spritegrouptxt->value(spriteDefName); spritegrouptxt->callback(assignSpritegroupnameCB); spritemetatxt = new Fl_Input(tile_place_buttons_x_off+616,168,168,24,"Meta name"); spritemetatxt->value(spritesName); spritemetatxt->callback(assignSpritemetaNameCB); spritemetatxt->align(FL_ALIGN_TOP); spriteglobaltxt = new Fl_Input(tile_place_buttons_x_off+616,208,168,24,"All meta name"); spriteglobaltxt->value(defMDesc); spriteglobaltxt->callback(assignSpriteAllMetanameCB); spriteglobaltxt->align(FL_ALIGN_TOP); metaspritesel=new Fl_Hor_Value_Slider(tile_place_buttons_x_off+616,244,168,24,"Meta group select:"); metaspritesel->step(1); metaspritesel->maximum(0); metaspritesel->align(FL_ALIGN_TOP); metaspritesel->callback(selspriteMeta); metaspritesel->labelsize(12); { Fl_Button *o = new Fl_Button(tile_place_buttons_x_off+616, 274, 96, 28, "Append meta"); o->callback(appendSpriteCB,(void*)(intptr_t)2); } spriteselgroup=new Fl_Hor_Value_Slider(tile_place_buttons_x_off,184,168,22,"Sprite group select:"); spriteselgroup->step(1); spriteselgroup->maximum(0); spriteselgroup->align(FL_ALIGN_TOP); spriteselgroup->callback(selspriteGroup); spriteselgroup->labelsize(12); spritesel=new Fl_Hor_Value_Slider(tile_place_buttons_x_off,220,168,22,"Sprite select:"); spritesel->step(1); spritesel->maximum(0); spritesel->align(FL_ALIGN_TOP); spritesel->callback(selSpriteCB); spritesel->labelsize(12); spritest=new Fl_Hor_Value_Slider(tile_place_buttons_x_off,256,168,22,"Start tile:"); spritest->step(1); spritest->maximum(0); spritest->align(FL_ALIGN_TOP); spritest->callback(setvalueSpriteCB,0); spritest->labelsize(12); spriteslat=new Fl_Hor_Value_Slider(tile_place_buttons_x_off,292,168,22,"Mapping tile"); spriteslat->step(1); spriteslat->maximum(0); spriteslat->align(FL_ALIGN_TOP); spriteslat->callback(setvalueSpriteCB,(void*)4); spriteslat->labelsize(12); spritesize[0]=new Fl_Hor_Value_Slider(tile_place_buttons_x_off+40,316,128,22,"Width"); spritesize[0]->step(1); spritesize[0]->value(1); spritesize[0]->minimum(1); spritesize[0]->maximum(4); spritesize[0]->align(FL_ALIGN_LEFT); spritesize[0]->callback(setvalueSpriteCB,(void*)1); spritesize[0]->labelsize(12); spritesize[1]=new Fl_Hor_Value_Slider(tile_place_buttons_x_off+40,340,128,22,"Height"); spritesize[1]->step(1); spritesize[1]->value(1); spritesize[1]->minimum(1); spritesize[1]->maximum(4); spritesize[1]->align(FL_ALIGN_LEFT); spritesize[1]->callback(setvalueSpriteCB,(void*)2); spritesize[1]->labelsize(12); spritepalrow=new Fl_Hor_Value_Slider(tile_place_buttons_x_off,376,168,22,"Palette row:"); spritepalrow->step(1); spritepalrow->maximum(3); spritepalrow->align(FL_ALIGN_TOP); spritepalrow->callback(setvalueSpriteCB,(void*)3); spritepalrow->labelsize(12); spritezoom=new Fl_Hor_Value_Slider(tile_place_buttons_x_off+38,400,130,22,"Zoom"); spritezoom->step(1); spritezoom->minimum(1); spritezoom->value(16); spritezoom->maximum(16); spritezoom->align(FL_ALIGN_LEFT); spritezoom->callback(redrawOnlyCB); spritezoom->labelsize(12); spritesoff[0] = new Fl_Int_Input(tile_place_buttons_x_off+62,424,106,24,"Offset X:"); spritesoff[0]->when(FL_WHEN_ENTER_KEY); spritesoff[0]->value("0"); spritesoff[0]->align(FL_ALIGN_LEFT); spritesoff[0]->callback(setoffspriteCB,0); spritesoff[1] = new Fl_Int_Input(tile_place_buttons_x_off+62,448,106,24,"Offset Y:"); spritesoff[1]->when(FL_WHEN_ENTER_KEY); spritesoff[1]->value("0"); spritesoff[1]->align(FL_ALIGN_LEFT); spritesoff[1]->callback(setoffspriteCB,(void*)1); spritehflip = new Fl_Check_Button(tile_place_buttons_x_off,470,48,20,"Hflip"); spritehflip->callback(spriteHflipCB); spritevflip = new Fl_Check_Button(tile_place_buttons_x_off+52,470,48,20,"Vflip"); spritevflip->callback(spriteVflipCB); spriteprio = new Fl_Check_Button(tile_place_buttons_x_off+104,470,56,20,"Priority"); spriteprio->callback(spritePrioCB); { Fl_Button *o = new Fl_Button(tile_place_buttons_x_off, 492, 64, 28, "Append"); o->callback(appendSpriteCB,0); o->labelsize(12); } { Fl_Button *o = new Fl_Button(tile_place_buttons_x_off+72, 492, 96, 28, "Append group"); o->callback(appendSpriteCB,(void*)(intptr_t)1); o->labelsize(12); } { Fl_Button *o = new Fl_Button(tile_place_buttons_x_off, 522, 64, 28, "Delete"); o->callback(delSpriteCB,0); o->labelsize(12); } { Fl_Button *o = new Fl_Button(tile_place_buttons_x_off+72, 522, 96, 28, "Delete group"); o->callback(delSpriteCB,(void*)1); o->labelsize(12); } spritealign[0] = new Fl_Button(tile_place_buttons_x_off, 552, 32, 28, "Left"); spritealign[0]->labelsize(12); spritealign[0]->callback(alignSpriteCB,(void*)0); spritealign[1] = new Fl_Button(tile_place_buttons_x_off+34, 552, 40, 28, "Right"); spritealign[1]->labelsize(12); spritealign[1]->callback(alignSpriteCB,(void*)1); spritealign[2] = new Fl_Button(tile_place_buttons_x_off+78, 552, 28, 28, "Top"); spritealign[2]->labelsize(12); spritealign[2]->callback(alignSpriteCB,(void*)2); spritealign[3] = new Fl_Button(tile_place_buttons_x_off+112, 552, 48, 28, "Bottom"); spritealign[3]->labelsize(12); spritealign[3]->callback(alignSpriteCB,(void*)3); { Fl_Group *o = new Fl_Group(tile_place_buttons_x_off, 572, 800, 480); { Fl_Round_Button*m = new Fl_Round_Button(tile_place_buttons_x_off, 572, 96, 32, "Top corner"); m->type(FL_RADIO_BUTTON); m->callback(setDrawSpriteCB,(void *)0); m->set(); } // Fl_Round_Button* o { Fl_Round_Button*m = new Fl_Round_Button(tile_place_buttons_x_off+96, 572, 64, 32, "Center"); m->type(FL_RADIO_BUTTON); m->callback(setDrawSpriteCB,(void *)1); } // Fl_Round_Button* o o->end(); } // End of buttons lua_getglobal(Lconf,"tabConfig"); lua_pushinteger(Lconf,spriteEditor); runLuaFunc(Lconf,1,0); tabsMain[spriteEditor]->end(); } { tabsMain.emplace_back(new Fl_Group(rx,ry,rw,rh,"Level editor")); lua_getglobal(Lconf,"tabConfig"); lua_pushinteger(Lconf,levelEditor); runLuaFunc(Lconf,1,0); tabsMain[levelEditor]->end(); } { tabsMain.emplace_back(new Fl_Group(rx,ry,rw,rh,"Settings/projects")); projectSelect=new Fl_Hor_Value_Slider(112,56,128,24,"Current project"); projectSelect->minimum(0); projectSelect->maximum(0); projectSelect->step(1); projectSelect->value(0); projectSelect->align(FL_ALIGN_LEFT); projectSelect->callback(switchProjectCB); { Fl_Button *o = new Fl_Button(260, 52, 152, 32, "Append blank project"); o->callback(appendProjectCB); } { Fl_Button *o = new Fl_Button(428, 52, 168, 32, "Delete selected project"); o->callback(deleteProjectCB); } //IMPORTANT if adding a new tab remember to update these sharePrj[0]=new Fl_Check_Button(8,112,112,16,"Share palette"); sharePrj[0]->callback(shareProjectCB,(void*)pjHavePal); sharePrj[1]=new Fl_Check_Button(120,112,96,16,"Share tiles"); sharePrj[1]->callback(shareProjectCB,(void*)pjHaveTiles); sharePrj[2]=new Fl_Check_Button(216,112,120,16,"Share tile map"); sharePrj[2]->callback(shareProjectCB,(void*)pjHaveMap); sharePrj[3]=new Fl_Check_Button(336,112,120,16,"Share chunks"); sharePrj[3]->callback(shareProjectCB,(void*)pjHaveChunks); sharePrj[4]=new Fl_Check_Button(456,112,120,16,"Share sprites"); sharePrj[4]->callback(shareProjectCB,(void*)pjHaveSprites); sharePrj[5]=new Fl_Check_Button(576,112,120,16,"Share level"); sharePrj[5]->callback(shareProjectCB,(void*)pjHaveLevel); havePrj[0]=new Fl_Check_Button(8,88,112,16,"Have palette"); havePrj[0]->callback(haveCB,(void*)pjHavePal); havePrj[1]=new Fl_Check_Button(120,88,96,16,"Have tiles"); havePrj[1]->callback(haveCB,(void*)pjHaveTiles); havePrj[2]=new Fl_Check_Button(232,88,120,16,"Have tile map"); havePrj[2]->callback(haveCB,(void*)pjHaveMap); havePrj[3]=new Fl_Check_Button(344,88,120,16,"Have chunks"); havePrj[3]->callback(haveCB,(void*)pjHaveChunks); havePrj[4]=new Fl_Check_Button(456,88,120,16,"Have sprites"); havePrj[4]->callback(haveCB,(void*)pjHaveSprites); havePrj[5]=new Fl_Check_Button(568,88,120,16,"Have level"); havePrj[5]->callback(haveCB,(void*)pjHaveLevel); shareWith[0]=new Fl_Hor_Value_Slider(8,142,128,24,"Share palette with:"); shareWith[0]->callback(switchShareCB,(void*)pjHavePal); shareWith[1]=new Fl_Hor_Value_Slider(136,142,128,24,"Share tiles with:"); shareWith[1]->callback(switchShareCB,(void*)pjHaveTiles); shareWith[2]=new Fl_Hor_Value_Slider(264,142,128,24,"Share tile map with:"); shareWith[2]->callback(switchShareCB,(void*)pjHaveMap); shareWith[3]=new Fl_Hor_Value_Slider(400,142,128,24,"Share chunks with:"); shareWith[3]->callback(switchShareCB,(void*)pjHaveChunks); shareWith[4]=new Fl_Hor_Value_Slider(536,142,128,24,"Share sprites with:"); shareWith[4]->callback(switchShareCB,(void*)pjHaveSprites); shareWith[5]=new Fl_Hor_Value_Slider(672,142,128,24,"Share level with:"); shareWith[5]->callback(switchShareCB,(void*)pjHaveLevel); for(unsigned x=0; x<shareAmtPj; ++x) { havePrj[x]->value(1); shareWith[x]->minimum(0); shareWith[x]->maximum(0); shareWith[x]->step(1); shareWith[x]->value(0); shareWith[x]->align(FL_ALIGN_TOP); } TxtBufProject = new Fl_Text_Buffer; TxtEditProject = new Fl_Text_Editor(8, 184, 640, 370,"Description/Notes"); TxtEditProject->buffer(TxtBufProject); TxtEditProject->textfont(FL_TIMES); TxtBufProject->text(currentProject->Name.c_str()); lua_getglobal(Lconf,"tabConfig"); lua_pushinteger(Lconf,settingsTab); runLuaFunc(Lconf,1,0); tabsMain[settingsTab]->end(); } { tabsMain.emplace_back(new Fl_Group(rx,ry,rw,rh,"Lua scripting")); luaScriptSel=new Fl_Choice(tile_place_buttons_x_off,default_palette_bar_offset_y+8,112,24); luaScriptSel->label("Script selection"); luaScriptSel->align(FL_ALIGN_TOP); luaScriptSel->callback(switchCurLuaScript); { Fl_Button *o = new Fl_Button(tile_place_buttons_x_off+120, default_palette_bar_offset_y,112, 32, "Append script"); o->callback(appendLuaScript); } { Fl_Button *o = new Fl_Button(tile_place_buttons_x_off+240, default_palette_bar_offset_y,144,32, "Delete selected script"); o->callback(deleteLuaScript); } { Fl_Button *o = new Fl_Button(tile_place_buttons_x_off+392, default_palette_bar_offset_y,48,32, "Run"); o->callback(runCurLuaScript); } luaScriptName = new Fl_Input(tile_place_buttons_x_off+448+8,default_palette_bar_offset_y+8,328,24,"Script name"); luaScriptName->callback(setNameLuaScript); luaScriptName->align(FL_ALIGN_TOP); luaBufProject = new Fl_Text_Buffer; luaEditProject = new Fl_Text_Editor(tile_place_buttons_x_off,default_palette_bar_offset_y+48, 784, 456,"Currently selected Lua script"); luaEditProject->buffer(luaBufProject); luaEditProject->textfont(FL_COURIER); luaEditProject->hide(); lua_getglobal(Lconf,"tabConfig"); lua_pushinteger(Lconf,luaTab); runLuaFunc(Lconf,1,0); tabsMain[luaTab]->end(); } } }
void luaReplyToRedisReply(redisClient *c, lua_State *lua) { int t = lua_type(lua,-1); switch(t) { case LUA_TSTRING: addReplyBulkCBuffer(c,(char*)lua_tostring(lua,-1),lua_strlen(lua,-1)); break; case LUA_TBOOLEAN: addReply(c,lua_toboolean(lua,-1) ? shared.cone : shared.nullbulk); break; case LUA_TNUMBER: addReplyLongLong(c,(long long)lua_tonumber(lua,-1)); break; case LUA_TTABLE: /* We need to check if it is an array, an error, or a status reply. * Error are returned as a single element table with 'err' field. * Status replies are returned as single elment table with 'ok' field */ lua_pushstring(lua,"err"); lua_gettable(lua,-2); t = lua_type(lua,-1); if (t == LUA_TSTRING) { sds err = sdsnew(lua_tostring(lua,-1)); sdsmapchars(err,"\r\n"," ",2); addReplySds(c,sdscatprintf(sdsempty(),"-%s\r\n",err)); sdsfree(err); lua_pop(lua,2); return; } lua_pop(lua,1); lua_pushstring(lua,"ok"); lua_gettable(lua,-2); t = lua_type(lua,-1); if (t == LUA_TSTRING) { sds ok = sdsnew(lua_tostring(lua,-1)); sdsmapchars(ok,"\r\n"," ",2); addReplySds(c,sdscatprintf(sdsempty(),"+%s\r\n",ok)); sdsfree(ok); lua_pop(lua,1); } else { void *replylen = addDeferredMultiBulkLength(c); int j = 1, mbulklen = 0; lua_pop(lua,1); /* Discard the 'ok' field value we popped */ while(1) { lua_pushnumber(lua,j++); lua_gettable(lua,-2); t = lua_type(lua,-1); if (t == LUA_TNIL) { lua_pop(lua,1); break; } luaReplyToRedisReply(c, lua); mbulklen++; } setDeferredMultiBulkLength(c,replylen,mbulklen); } break; default: addReply(c,shared.nullbulk); } lua_pop(lua,1); }
static int luaB_type (lua_State *L) { luaL_checkany(L, 1); lua_pushstring(L, lua_typename(L, lua_type(L, 1))); return 1; }
const char* LuaInterface::typeName(int index) { assert(hasIndex(index)); int type = lua_type(L, index); return lua_typename(L, type); }
int LuaTableSave::Save() { ACCHECK(lua_istable(m_pLua, -1)); m_objOut << "{"; bool isEmpty = true; lua_pushnil(m_pLua); m_nLayer++; while(lua_next(m_pLua, -2) != 0) { isEmpty = false; PrintTab(); //存储KEY if(lua_type(m_pLua,-2) == LUA_TNUMBER) m_objOut << "[" << lua_tonumber(m_pLua, -2) << "]" << " = "; else if(lua_type(m_pLua, -2) == LUA_TSTRING) m_objOut << lua_tostring(m_pLua, -2) << " = "; else { std::cout << "Unknown key type" << std::endl; ACCHECK(false); } //存储VALUE if(lua_type(m_pLua, -1) == LUA_TNUMBER) m_objOut << lua_tonumber(m_pLua, -1) << ","; else if(lua_type(m_pLua, -1) == LUA_TSTRING) m_objOut << "\"" << lua_tostring(m_pLua, -1) << "\"" << ","; else if(lua_type(m_pLua, -1) == LUA_TBOOLEAN) { int bval = lua_toboolean(m_pLua, -1); std::string strVal = bval ? "true" : "false"; m_objOut << strVal << ","; } else if(lua_type(m_pLua, -1) == LUA_TTABLE) { Save(); } else { std::cout << "Unknown value type" << std::endl; ACCHECK(false); } lua_pop(m_pLua, 1); } m_nLayer--; //层数未达到最大且table不为空,table结束处换行 if((m_nLayer + 1 <= m_nMaxLayer) && (!isEmpty)) PrintTab(); m_objOut << "}"; //不是第一层 if(m_nLayer) m_objOut << ","; return 0; }
/* * Arguments: filename (string) | function_dump (string), * [arguments (string | number | boolean | lightuserdata) ...] * Returns: [thread_ludata] */ static int thread_runvm (lua_State *L) { const char *path = luaL_checkstring(L, 1); lua_State *NL = NULL; struct sys_vmthread *vmtd = (struct sys_vmthread *) sys_get_thread(); #ifndef _WIN32 pthread_attr_t attr; #else HANDLE hThr; #endif int res = 0; if (!vmtd) luaL_argerror(L, 0, "Threading not initialized"); NL = luaL_newstate(); if (!NL) goto err; thread_openlibs(NL); if (path[0] == LUA_SIGNATURE[0] ? luaL_loadbuffer(NL, path, lua_rawlen(L, 1), "thread") : luaL_loadfile(NL, path)) { lua_pushstring(L, lua_tostring(NL, -1)); /* error message */ lua_close(NL); lua_error(L); } /* Arguments */ lua_pushlightuserdata(NL, vmtd); /* master */ { int i, top = lua_gettop(L); for (i = 2; i <= top; ++i) { switch (lua_type(L, i)) { case LUA_TSTRING: lua_pushstring(NL, lua_tostring(L, i)); break; case LUA_TNUMBER: lua_pushnumber(NL, lua_tonumber(L, i)); break; case LUA_TBOOLEAN: lua_pushboolean(NL, lua_toboolean(L, i)); break; case LUA_TLIGHTUSERDATA: case LUA_TUSERDATA: lua_pushlightuserdata(NL, lua_touserdata(L, i)); break; default: luaL_argerror(L, i, "primitive type expected"); } } } if (vmthread_new(NL, &vmtd)) goto err_clean; #ifndef _WIN32 res = pthread_attr_init(&attr); if (res) goto err_clean; pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); res = pthread_create(&vmtd->td.tid, &attr, (thread_func_t) thread_startvm, vmtd); pthread_attr_destroy(&attr); if (!res) { #else hThr = (HANDLE) _beginthreadex(NULL, 0, (thread_func_t) thread_startvm, vmtd, 0, &vmtd->td.tid); if (hThr) { CloseHandle(hThr); #endif lua_pushlightuserdata(L, vmtd); return 1; } err_clean: lua_close(NL); err: return sys_seterror(L, res); } /* * Arguments: thread_ludata */ static int thread_interrupt (lua_State *L) { struct sys_thread *td = lua_touserdata(L, 1); td->interrupted = 1; #ifndef _WIN32 pthread_kill(td->tid, SYS_SIGINTR); #endif return 0; } static int thread_yield (lua_State *L) { (void) L; sys_vm_leave(); #ifndef _WIN32 sched_yield(); #else Sleep(0); #endif sys_vm_enter(); return 0; }
static const char *opt_mode(lua_State *L, int *pidx) { if (lua_type(L, *pidx) != LUA_TSTRING) return "u"; return lua_tostring(L, (*pidx)++); }
static int AddReg(lua_State * L) { if(lua_gettop(L) == 3) { if(lua_type(L, 1) != LUA_TSTRING || lua_type(L, 2) != LUA_TSTRING || lua_type(L, 3) != LUA_TNUMBER) { luaL_checktype(L, 1, LUA_TSTRING); luaL_checktype(L, 2, LUA_TSTRING); luaL_checktype(L, 3, LUA_TNUMBER); lua_settop(L, 0); lua_pushnil(L); return 1; } size_t szNickLen, szPassLen; char *sNick = (char *)lua_tolstring(L, 1, &szNickLen); char *sPass = (char *)lua_tolstring(L, 2, &szPassLen); #if LUA_VERSION_NUM < 503 uint16_t i16Profile = (uint16_t)lua_tonumber(L, 3); #else uint16_t i16Profile = (uint16_t)lua_tounsigned(L, 3); #endif if(i16Profile > clsProfileManager::mPtr->iProfileCount-1 || szNickLen == 0 || szNickLen > 64 || szPassLen == 0 || szPassLen > 64 || strpbrk(sNick, " $|") != NULL || strchr(sPass, '|') != NULL) { lua_settop(L, 0); lua_pushnil(L); return 1; } bool bAdded = clsRegManager::mPtr->AddNew(sNick, sPass, i16Profile); lua_settop(L, 0); if(bAdded == false) { lua_pushnil(L); return 1; } lua_pushboolean(L, 1); return 1; } else if(lua_gettop(L) == 2) { if(lua_type(L, 1) != LUA_TSTRING || lua_type(L, 2) != LUA_TNUMBER) { luaL_checktype(L, 1, LUA_TSTRING); luaL_checktype(L, 2, LUA_TNUMBER); lua_settop(L, 0); lua_pushnil(L); return 1; } size_t szNickLen; char *sNick = (char *)lua_tolstring(L, 1, &szNickLen); #if LUA_VERSION_NUM < 503 uint16_t ui16Profile = (uint16_t)lua_tonumber(L, 2); #else uint16_t ui16Profile = (uint16_t)lua_tounsigned(L, 2); #endif if(ui16Profile > clsProfileManager::mPtr->iProfileCount-1 || szNickLen == 0 || szNickLen > 64 || strpbrk(sNick, " $|") != NULL) { lua_settop(L, 0); lua_pushnil(L); return 1; } // check if user is registered if(clsRegManager::mPtr->Find(sNick, szNickLen) != NULL) { lua_settop(L, 0); lua_pushnil(L); return 1; } User * pUser = clsHashManager::mPtr->FindUser(sNick, szNickLen); if(pUser == NULL) { lua_settop(L, 0); lua_pushnil(L); return 1; } if(pUser->uLogInOut == NULL) { pUser->uLogInOut = new LoginLogout(); if(pUser->uLogInOut == NULL) { pUser->ui32BoolBits |= User::BIT_ERROR; pUser->Close(); AppendDebugLog("%s - [MEM] Cannot allocate new pUser->uLogInOut in RegMan.AddReg\n", 0); lua_settop(L, 0); lua_pushnil(L); return 1; } } pUser->SetBuffer(clsProfileManager::mPtr->ProfilesTable[ui16Profile]->sName); pUser->ui32BoolBits |= User::BIT_WAITING_FOR_PASS; int iMsgLen = sprintf(clsServerManager::sGlobalBuffer, "<%s> %s.|$GetPass|", clsSettingManager::mPtr->sPreTexts[clsSettingManager::SETPRETXT_HUB_SEC], clsLanguageManager::mPtr->sTexts[LAN_YOU_WERE_REGISTERED_PLEASE_ENTER_YOUR_PASSWORD]); if(CheckSprintf(iMsgLen, clsServerManager::szGlobalBufferSize, "RegMan.AddReg1") == true) { pUser->SendCharDelayed(clsServerManager::sGlobalBuffer, iMsgLen); } lua_settop(L, 0); lua_pushboolean(L, 1); return 1; } else { luaL_error(L, "bad argument count to 'RegMan.AddReg' (2 or 3 expected, got %d)", lua_gettop(L)); lua_settop(L, 0); lua_pushnil(L); return 1; } }
static int ChangeReg(lua_State * L) { if(lua_gettop(L) != 3) { luaL_error(L, "bad argument count to 'ChangeReg' (3 expected, got %d)", lua_gettop(L)); lua_settop(L, 0); lua_pushnil(L); return 1; } if(lua_type(L, 1) != LUA_TSTRING || lua_type(L, 3) != LUA_TNUMBER) { luaL_checktype(L, 1, LUA_TSTRING); luaL_checktype(L, 3, LUA_TNUMBER); lua_settop(L, 0); lua_pushnil(L); return 1; } size_t szNickLen, szPassLen = 0; char * sNick = (char *)lua_tolstring(L, 1, &szNickLen); char * sPass = NULL; if(lua_type(L, 2) == LUA_TSTRING) { char * sPass = (char *)lua_tolstring(L, 2, &szPassLen); if(szPassLen == 0 || szPassLen > 64 || strpbrk(sPass, "|") != NULL) { lua_settop(L, 0); lua_pushnil(L); return 1; } } else if(lua_type(L, 2) != LUA_TNIL) { luaL_checktype(L, 2, LUA_TSTRING); lua_settop(L, 0); lua_pushnil(L); return 1; } #if LUA_VERSION_NUM < 503 uint16_t i16Profile = (uint16_t)lua_tonumber(L, 3); #else uint16_t i16Profile = (uint16_t)lua_tounsigned(L, 3); #endif if(i16Profile > clsProfileManager::mPtr->iProfileCount-1 || szNickLen == 0 || szNickLen > 64 || strpbrk(sNick, " $|") != NULL) { lua_settop(L, 0); lua_pushnil(L); return 1; } RegUser *reg = clsRegManager::mPtr->Find(sNick, szNickLen); if(reg == NULL) { lua_settop(L, 0); lua_pushnil(L); return 1; } clsRegManager::mPtr->ChangeReg(reg, sPass, i16Profile); lua_settop(L, 0); lua_pushboolean(L, 1); return 1; }
/* Class index function * If the object is a userdata (ie, an object), it searches the field in * the alternative table stored in the corresponding "ubox" table. */ static int class_index_event (lua_State* L) { int t = lua_type(L,1); if (t == LUA_TUSERDATA) { /* Access alternative table */ #ifdef LUA_VERSION_NUM /* new macro on version 5.1 */ lua_getfenv(L,1); if (!lua_rawequal(L, -1, TOLUA_NOPEER)) { lua_pushvalue(L, 2); /* key */ lua_gettable(L, -2); /* on lua 5.1, we trade the "tolua_peers" lookup for a gettable call */ if (!lua_isnil(L, -1)) return 1; }; #else lua_pushstring(L,"tolua_peers"); lua_rawget(L,LUA_REGISTRYINDEX); /* stack: obj key ubox */ lua_pushvalue(L,1); lua_rawget(L,-2); /* stack: obj key ubox ubox[u] */ if (lua_istable(L,-1)) { lua_pushvalue(L,2); /* key */ lua_rawget(L,-2); /* stack: obj key ubox ubox[u] value */ if (!lua_isnil(L,-1)) return 1; } #endif lua_settop(L,2); /* stack: obj key */ /* Try metatables */ lua_pushvalue(L,1); /* stack: obj key obj */ while (lua_getmetatable(L,-1)) { /* stack: obj key obj mt */ lua_remove(L,-2); /* stack: obj key mt */ if (lua_isnumber(L,2)) /* check if key is a numeric value */ { /* try operator[] */ lua_pushstring(L,".geti"); lua_rawget(L,-2); /* stack: obj key mt func */ if (lua_isfunction(L,-1)) { lua_pushvalue(L,1); lua_pushvalue(L,2); lua_call(L,2,1); return 1; } } else { lua_pushvalue(L,2); /* stack: obj key mt key */ lua_rawget(L,-2); /* stack: obj key mt value */ if (!lua_isnil(L,-1)) return 1; else lua_pop(L,1); /* try C/C++ variable */ lua_pushstring(L,".get"); lua_rawget(L,-2); /* stack: obj key mt tget */ if (lua_istable(L,-1)) { lua_pushvalue(L,2); lua_rawget(L,-2); /* stack: obj key mt value */ if (lua_iscfunction(L,-1)) { lua_pushvalue(L,1); lua_pushvalue(L,2); lua_call(L,2,1); return 1; } else if (lua_istable(L,-1)) { /* deal with array: create table to be returned and cache it in ubox */ void* u = *((void**)lua_touserdata(L,1)); lua_newtable(L); /* stack: obj key mt value table */ lua_pushstring(L,".self"); lua_pushlightuserdata(L,u); lua_rawset(L,-3); /* store usertype in ".self" */ lua_insert(L,-2); /* stack: obj key mt table value */ lua_setmetatable(L,-2); /* set stored value as metatable */ lua_pushvalue(L,-1); /* stack: obj key met table table */ lua_pushvalue(L,2); /* stack: obj key mt table table key */ lua_insert(L,-2); /* stack: obj key mt table key table */ storeatubox(L,1); /* stack: obj key mt table */ return 1; } } } lua_settop(L,3); } lua_pushnil(L); return 1; } else if (t== LUA_TTABLE) { module_index_event(L); return 1; } lua_pushnil(L); return 1; }
/** * expect a MainWindow class * and some optional parameters * * dans l'idéal, on récupère des chunks, ou plutôt des objets, qui sont * des componsants * si le composant existe, on le vire et on le remplace par le code * s'il n'existe pas ?... * **/ int reload(lua_State *L) { int top = lua_gettop(L); LUA_COMMON::liveCoding(true); // check what we have if(!lua_isfunction(L,2)) { lua_pushnil(L); lua_pushfstring(L,"luceLiveReload: expected function, got '%s'", lua_typename(L, lua_type(L,2))); return 2; } // TODO: get and remove params // call function // push result to luceLiveReload // // meanwhile: //lua_pop(L, lua_gettop(L)-2); // defined in luce.h for Lua5.1/5.2 compatibility lua_pushglobaltable(L); lua_pushstring(L, "LUCE_LIVE_CODING"); lua_pushnumber(L, 1); lua_settable(L, -3); if(top>2) { lua_pushstring(L, "LPATH"); lua_pushstring(L, luaL_checkstring(L,3)); lua_settable(L, -3); lua_remove(L,3); } lua_pop(L,1); int nb_args = 0; if ( lua_pcall(L, nb_args, 1, 0) != 0 ) { lua_pushnil(L); lua_pushvalue(L, -2); return 2; } if(lua_isnoneornil(L,-1)) { lua_pushnil(L); lua_pushfstring(L, "Wrong or malformed MainWindow: expected Component, got nil"); return 2; } int res = mainClass->luceLiveReload(L); return res; }
/** * Get nginx internal variables content * * @retval Always return a string or nil on Lua stack. Return nil when failed * to get content, and actual content string when found the specified variable. * @seealso ngx_http_lua_var_set * */ static int ngx_http_lua_var_get(lua_State *L) { ngx_http_request_t *r; u_char *p, *lowcase; size_t len; ngx_uint_t hash; ngx_str_t name; ngx_http_variable_value_t *vv; #if (NGX_PCRE) u_char *val; ngx_uint_t n; LUA_NUMBER index; int *cap; #endif r = ngx_http_lua_get_req(L); if (r == NULL) { return luaL_error(L, "no request object found"); } ngx_http_lua_check_fake_request(L, r); #if (NGX_PCRE) if (lua_type(L, -1) == LUA_TNUMBER) { /* it is a regex capturing variable */ index = lua_tonumber(L, -1); if (index <= 0) { lua_pushnil(L); return 1; } n = (ngx_uint_t) index * 2; dd("n = %d, ncaptures = %d", (int) n, (int) r->ncaptures); if (r->captures == NULL || r->captures_data == NULL || n >= r->ncaptures) { lua_pushnil(L); return 1; } /* n >= 0 && n < r->ncaptures */ cap = r->captures; p = r->captures_data; val = &p[cap[n]]; lua_pushlstring(L, (const char *) val, (size_t) (cap[n + 1] - cap[n])); return 1; } #endif p = (u_char *) luaL_checklstring(L, -1, &len); lowcase = lua_newuserdata(L, len); hash = ngx_hash_strlow(lowcase, p, len); name.len = len; name.data = lowcase; vv = ngx_http_get_variable(r, &name, hash); if (vv == NULL || vv->not_found) { lua_pushnil(L); return 1; } lua_pushlstring(L, (const char *) vv->data, (size_t) vv->len); return 1; }
/** * Set nginx internal variable content * * @retval Always return a boolean on Lua stack. Return true when variable * content was modified successfully, false otherwise. * @seealso ngx_http_lua_var_get * */ static int ngx_http_lua_var_set(lua_State *L) { ngx_http_variable_t *v; ngx_http_variable_value_t *vv; ngx_http_core_main_conf_t *cmcf; u_char *p, *lowcase, *val; size_t len; ngx_str_t name; ngx_uint_t hash; ngx_http_request_t *r; int value_type; const char *msg; r = ngx_http_lua_get_req(L); if (r == NULL) { return luaL_error(L, "no request object found"); } ngx_http_lua_check_fake_request(L, r); /* we skip the first argument that is the table */ /* we read the variable name */ p = (u_char *) luaL_checklstring(L, 2, &len); lowcase = lua_newuserdata(L, len); hash = ngx_hash_strlow(lowcase, p, len); name.len = len; name.data = lowcase; /* we read the variable new value */ value_type = lua_type(L, 3); switch (value_type) { case LUA_TNUMBER: case LUA_TSTRING: p = (u_char *) luaL_checklstring(L, 3, &len); val = ngx_palloc(r->pool, len); if (val == NULL) { return luaL_error(L, "memory allocation erorr"); } ngx_memcpy(val, p, len); break; case LUA_TNIL: /* undef the variable */ val = NULL; len = 0; break; default: msg = lua_pushfstring(L, "string, number, or nil expected, " "but got %s", lua_typename(L, value_type)); return luaL_argerror(L, 1, msg); } /* we fetch the variable itself */ cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); v = ngx_hash_find(&cmcf->variables_hash, hash, name.data, name.len); if (v) { if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) { return luaL_error(L, "variable \"%s\" not changeable", lowcase); } if (v->set_handler) { dd("set variables with set_handler"); vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t)); if (vv == NULL) { return luaL_error(L, "out of memory"); } if (value_type == LUA_TNIL) { vv->valid = 0; vv->not_found = 1; vv->no_cacheable = 0; vv->data = NULL; vv->len = 0; } else { vv->valid = 1; vv->not_found = 0; vv->no_cacheable = 0; vv->data = val; vv->len = len; } v->set_handler(r, vv, v->data); return 0; } if (v->flags & NGX_HTTP_VAR_INDEXED) { vv = &r->variables[v->index]; dd("set indexed variable"); if (value_type == LUA_TNIL) { vv->valid = 0; vv->not_found = 1; vv->no_cacheable = 0; vv->data = NULL; vv->len = 0; } else { vv->valid = 1; vv->not_found = 0; vv->no_cacheable = 0; vv->data = val; vv->len = len; } return 0; } return luaL_error(L, "variable \"%s\" cannot be assigned a value", lowcase); } /* variable not found */ return luaL_error(L, "variable \"%s\" not found for writing; " "maybe it is a built-in variable that is not changeable " "or you forgot to use \"set $%s '';\" " "in the config file to define it first", lowcase, lowcase); }
static void value_to_stringstream( std::stringstream& output, int i, lua_State* L, std::string indent, const bool verbose_table = true) { const int t = lua_type(L, i); switch (t) { case LUA_TSTRING: output << "STRING; VALUE: " << lua_tostring(L, i); break; case LUA_TBOOLEAN: output << "BOOLEAN; VALUE: " << (lua_toboolean(L, i) ? "true" : "false"); break; case LUA_TNUMBER: output << "NUMBER; VALUE: " << lua_tonumber(L, i); break; case LUA_TNIL: output << "NIL; VALUE: nil"; break; case LUA_TTABLE: { output << "TABLE; VALUE: " << lua_topointer(L, i); if(verbose_table) { indent += "\t"; unsigned keyindex = lua_gettop(L) + 1; lua_pushnil(L); while(lua_next(L, i) != 0) { output << "\n" << indent << "KEY: "; const int keytype = lua_type(L, keyindex); switch(keytype) { case LUA_TSTRING: output << lua_tostring(L, keyindex); break; case LUA_TBOOLEAN: output << (lua_toboolean(L, keyindex) ? "true" : "false"); break; case LUA_TNUMBER: output << lua_tonumber(L, keyindex); break; default: output << lua_topointer(L, keyindex); break; } output << "; TYPE: "; value_to_stringstream(output, keyindex + 1, L, indent); lua_pop(L, 1); } } } break; case LUA_TUSERDATA: output << "USERDATA; VALUE: " << lua_topointer(L, i); break; case LUA_TFUNCTION: output << "FUNCTION; VALUE: " << lua_topointer(L, i); break; case LUA_TTHREAD: output << "THREAD; VALUE: " << lua_topointer(L, i); break; case LUA_TLIGHTUSERDATA: output << "LIGHTUSERDATA; VALUE: " << lua_topointer(L, i); break; default: //There are no other types! assert(false); break; } }
int cLuaState::CopyStackFrom(cLuaState & a_SrcLuaState, int a_SrcStart, int a_SrcEnd) { /* // DEBUG: LOGD("Copying stack values from %d to %d", a_SrcStart, a_SrcEnd); a_SrcLuaState.LogStack("Src stack before copying:"); LogStack("Dst stack before copying:"); */ for (int i = a_SrcStart; i <= a_SrcEnd; ++i) { int t = lua_type(a_SrcLuaState, i); switch (t) { case LUA_TNIL: { lua_pushnil(m_LuaState); break; } case LUA_TSTRING: { AString s; a_SrcLuaState.ToString(i, s); Push(s); break; } case LUA_TBOOLEAN: { bool b = (tolua_toboolean(a_SrcLuaState, i, false) != 0); Push(b); break; } case LUA_TNUMBER: { lua_Number d = tolua_tonumber(a_SrcLuaState, i, 0); Push(d); break; } case LUA_TUSERDATA: { // Get the class name: const char * type = nullptr; if (lua_getmetatable(a_SrcLuaState, i) == 0) { LOGWARNING("%s: Unknown class in pos %d, cannot copy.", __FUNCTION__, i); lua_pop(m_LuaState, i - a_SrcStart); return -1; } lua_rawget(a_SrcLuaState, LUA_REGISTRYINDEX); // Stack +1 type = lua_tostring(a_SrcLuaState, -1); lua_pop(a_SrcLuaState, 1); // Stack -1 // Copy the value: void * ud = tolua_touserdata(a_SrcLuaState, i, nullptr); tolua_pushusertype(m_LuaState, ud, type); break; } default: { LOGWARNING("%s: Unsupported value: '%s' at stack position %d. Can only copy numbers, strings, bools and classes!", __FUNCTION__, lua_typename(a_SrcLuaState, t), i ); a_SrcLuaState.LogStack("Stack where copying failed:"); lua_pop(m_LuaState, i - a_SrcStart); return -1; } } } return a_SrcEnd - a_SrcStart + 1; }
void luaH_add_paths(lua_State *L, const gchar *config_dir) { lua_getglobal(L, "package"); if(LUA_TTABLE != lua_type(L, 1)) { warn("package is not a table"); return; } lua_getfield(L, 1, "path"); if(LUA_TSTRING != lua_type(L, 2)) { warn("package.path is not a string"); lua_pop(L, 1); return; } /* compile list of package search paths */ GPtrArray *paths = g_ptr_array_new_with_free_func(g_free); #if DEVELOPMENT_PATHS /* allows for testing luakit in the project directory */ g_ptr_array_add(paths, g_strdup("./lib")); g_ptr_array_add(paths, g_strdup("./config")); #endif /* add luakit install path */ g_ptr_array_add(paths, g_build_filename(LUAKIT_INSTALL_PATH, "lib", NULL)); /* add users config dir (see: XDG_CONFIG_DIR) */ if (config_dir) g_ptr_array_add(paths, g_strdup(config_dir)); /* add system config dirs (see: XDG_CONFIG_DIRS) */ const gchar* const *config_dirs = g_get_system_config_dirs(); for (; *config_dirs; config_dirs++) g_ptr_array_add(paths, g_build_filename(*config_dirs, "luakit", NULL)); const gchar *path; for (guint i = 0; i < paths->len; i++) { path = paths->pdata[i]; /* Search for file */ lua_pushliteral(L, ";"); lua_pushstring(L, path); lua_pushliteral(L, "/?.lua"); lua_concat(L, 3); /* Search for lib */ lua_pushliteral(L, ";"); lua_pushstring(L, path); lua_pushliteral(L, "/?/init.lua"); lua_concat(L, 3); /* concat with package.path */ lua_concat(L, 3); } g_ptr_array_free(paths, TRUE); /* package.path = "concatenated string" */ lua_setfield(L, 1, "path"); /* remove package module from stack */ lua_pop(L, 1); }
static int lhiredis_unwrap_reply(lua_State * L) { int type = 0; luaL_checkany(L, 1); luaL_checkstack(L, 3, "not enough stack to push reply"); if (!lua_istable(L, 1)) { if (lua_isnil(L, 1) && !lua_isnoneornil(L, 2)) { lua_pushvalue(L, 1); lua_pushvalue(L, 2); return 2; } lua_pushvalue(L, 1); return 1; } if (!lua_getmetatable(L, 1)) { lua_pushvalue(L, 1); return 1; } luaL_getmetatable(L, LUAHIREDIS_CONST_MT); if (!lua_rawequal(L, -1, -2)) { lua_pop(L, 2); /* both metatables */ lua_pushvalue(L, 1); return 1; } lua_pop(L, 2); /* both metatables */ lua_getfield(L, 1, "type"); if (lua_type(L, -1) != LUA_TNUMBER) { lua_pop(L, 1); /* t.type */ lua_pushvalue(L, 1); return 1; } type = lua_tonumber(L, -1); lua_pop(L, 1); /* t.type */ if (type == REDIS_REPLY_STATUS) { lua_getfield(L, 1, "name"); if (!lua_isstring(L, -1)) { /* We promised to users that this wouldn't be nil */ return luaL_error(L, "lua-hiredis internal error: bad const-object"); } lua_pushinteger(L, REDIS_REPLY_STATUS); return 2; } else if (type == REDIS_REPLY_ERROR) { lua_pushnil(L); lua_getfield(L, 1, "name"); if (!lua_isstring(L, -1)) { /* We promised to users that this wouldn't be nil */ return luaL_error(L, "lua-hiredis internal error: bad const-object"); } return 2; } /* Note that NIL is not unwrapped */ lua_pushvalue(L, 1); return 1; }
/*! Prepares a Lua state for, and catches errors from, CorsixTH_lua_main(). By executing in Lua mode as soon as possible, errors can be nicely caught sooner, hence this function does as little as possible and leaves the rest for CorsixTH_lua_main(). */ int main(int argc, char** argv) { struct compile_time_lua_check { // Lua 5.1, not 5.0, is required int lua_5_point_1_required[LUA_VERSION_NUM >= 501 ? 1 : -1]; // Lua numbers must be doubles so that the mantissa has at least // 32 bits (floats only have 24 bits) int number_is_double[types_equal<lua_Number, double>::result]; }; bool bRun = true; while(bRun) { lua_State *L = NULL; L = luaL_newstate(); if(L == NULL) { fprintf(stderr, "Fatal error starting CorsixTH: " "Cannot open Lua state.\n"); return 0; } lua_atpanic(L, CorsixTH_lua_panic); luaL_openlibs(L); lua_settop(L, 0); lua_pushcfunction(L, CorsixTH_lua_stacktrace); lua_pushcfunction(L, CorsixTH_lua_main); // Move command line parameters onto the Lua stack lua_checkstack(L, argc); for(int i = 0; i < argc; ++i) { lua_pushstring(L, argv[i]); } if(lua_pcall(L, argc, 0, 1) != 0) { const char* err = lua_tostring(L, -1); if(err != NULL) { fprintf(stderr, "%s\n", err); } else { fprintf(stderr, "An error has occured in CorsixTH:\n" "Uncaught non-string Lua error\n"); } lua_pushcfunction(L, Bootstrap_lua_error_report); lua_insert(L, -2); if(lua_pcall(L, 1, 0, 0) != 0) { fprintf(stderr, "%s\n", lua_tostring(L, -1)); } } lua_getfield(L, LUA_REGISTRYINDEX, "_RESTART"); bRun = lua_toboolean(L, -1) != 0; // Get cleanup functions out of the Lua state (but don't run them yet) std::stack<void(*)(void)> stkCleanup; lua_getfield(L, LUA_REGISTRYINDEX, "_CLEANUP"); if(lua_type(L, -1) == LUA_TTABLE) { for(unsigned int i = 1; i <= lua_objlen(L, -1); ++i) { lua_rawgeti(L, -1, (int)i); stkCleanup.push((void(*)(void))lua_touserdata(L, -1)); lua_pop(L, 1); } } lua_close(L); // The cleanup functions are executed _after_ the Lua state is fully // closed, and in reserve order to that in which they were registered. while(!stkCleanup.empty()) { if(stkCleanup.top() != NULL) stkCleanup.top()(); stkCleanup.pop(); } if(bRun) { printf("Restarting...\n"); } } return 0; }
/* Top-level delegating persist function */ static void persist(PersistInfo *pi) { /* Grab the object's type. */ int type = lua_type(pi->L, -1); int simple = type == LUA_TNIL || type == LUA_TBOOLEAN || type == LUA_TNUMBER || type == LUA_TLIGHTUSERDATA; /* Increment the number of objects persisted. */ ++(pi->counter); /* If the type isn't simple, check to see if one is already in the reftable. */ if(!simple) { /* perms reftbl ... obj */ lua_checkstack(pi->L, 2); /* If the object has already been written, write a reference to it */ lua_pushvalue(pi->L, -1); /* perms reftbl ... obj obj */ lua_rawget(pi->L, 2); /* perms reftbl ... obj ref? */ if(!lua_isnil(pi->L, -1)) { /* perms reftbl ... obj ref */ int zero = 0; int ref = (intptr_t)lua_touserdata(pi->L, -1); pi->writer(pi->L, &zero, sizeof(int), pi->ud); pi->writer(pi->L, &ref, sizeof(int), pi->ud); lua_pop(pi->L, 1); /* perms reftbl ... obj ref */ #ifdef PLUTO_DEBUG printindent(pi->level); printf("0 %d\n", ref); #endif return; } /* perms reftbl ... obj nil */ lua_pop(pi->L, 1); } /* perms reftbl ... obj */ /* If the object is nil, write the pseudoreference 0 */ if(lua_isnil(pi->L, -1)) { int zero = 0; /* firsttime */ pi->writer(pi->L, &zero, sizeof(int), pi->ud); /* ref */ pi->writer(pi->L, &zero, sizeof(int), pi->ud); #ifdef PLUTO_DEBUG printindent(pi->level); printf("0 0\n"); #endif return; } { /* indicate that it's the first time */ int one = 1; pi->writer(pi->L, &one, sizeof(int), pi->ud); } /* put the value in the reftable if necessary. Simple types don't need to be put in the reftable. */ if(!simple) { lua_pushvalue(pi->L, -1); /* perms reftbl ... obj obj */ lua_pushlightuserdata(pi->L, (void*)((intptr_t) pi->counter)); /* perms reftbl ... obj obj ref */ lua_rawset(pi->L, 2); /* perms reftbl ... obj */ } pi->writer(pi->L, &pi->counter, sizeof(int), pi->ud); /* At this point, we'll give the permanents table a chance to play. */ { lua_pushvalue(pi->L, -1); /* perms reftbl ... obj obj */ lua_gettable(pi->L, 1); /* perms reftbl ... obj permkey? */ if(!lua_isnil(pi->L, -1)) { /* perms reftbl ... obj permkey */ int type = PLUTO_TPERMANENT; #ifdef PLUTO_DEBUG printindent(pi->level); printf("1 %d PERM\n", pi->counter); pi->level++; #endif pi->writer(pi->L, &type, sizeof(int), pi->ud); persist(pi); lua_pop(pi->L, 1); /* perms reftbl ... obj */ #ifdef PLUTO_DEBUG pi->level--; #endif return; } else { /* perms reftbl ... obj nil */ lua_pop(pi->L, 1); /* perms reftbl ... obj */ } /* perms reftbl ... obj */ } { int type = lua_type(pi->L, -1); pi->writer(pi->L, &type, sizeof(int), pi->ud); #ifdef PLUTO_DEBUG printindent(pi->level); printf("1 %d %d\n", pi->counter, type); pi->level++; #endif } switch(lua_type(pi->L, -1)) { case LUA_TBOOLEAN: persistboolean(pi); break; case LUA_TLIGHTUSERDATA: persistlightuserdata(pi); break; case LUA_TNUMBER: persistnumber(pi); break; case LUA_TSTRING: persiststring(pi); break; case LUA_TTABLE: persisttable(pi); break; case LUA_TFUNCTION: persistfunction(pi); break; case LUA_TTHREAD: persistthread(pi); break; case LUA_TPROTO: persistproto(pi); break; case LUA_TUPVAL: persistupval(pi); break; case LUA_TUSERDATA: persistuserdata(pi); break; default: lua_assert(0); } #ifdef PLUTO_DEBUG pi->level--; #endif }
int lua_RenderState_setParameterAutoBinding(lua_State* state) { // Get the number of parameters. int paramCount = lua_gettop(state); // Attempt to match the parameters to a valid binding. switch (paramCount) { case 3: { do { if ((lua_type(state, 1) == LUA_TUSERDATA) && (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) && lua_type(state, 3) == LUA_TNUMBER) { // Get parameter 1 off the stack. const char* param1 = gameplay::ScriptUtil::getString(2, false); // Get parameter 2 off the stack. RenderState::AutoBinding param2 = (RenderState::AutoBinding)luaL_checkint(state, 3); RenderState* instance = getInstance(state); instance->setParameterAutoBinding(param1, param2); return 0; } } while (0); do { if ((lua_type(state, 1) == LUA_TUSERDATA) && (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) && (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL)) { // Get parameter 1 off the stack. const char* param1 = gameplay::ScriptUtil::getString(2, false); // Get parameter 2 off the stack. const char* param2 = gameplay::ScriptUtil::getString(3, false); RenderState* instance = getInstance(state); instance->setParameterAutoBinding(param1, param2); return 0; } } while (0); lua_pushstring(state, "lua_RenderState_setParameterAutoBinding - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } default: { lua_pushstring(state, "Invalid number of parameters (expected 3)."); lua_error(state); break; } } return 0; }
/* args-opts -- proc/nil error */ static int ex_spawn(lua_State *L) { struct spawn_params *params; int have_options; switch (lua_type(L, 1)) { default: return luaL_typerror(L, 1, "string or table"); case LUA_TSTRING: switch (lua_type(L, 2)) { default: return luaL_typerror(L, 2, "table"); case LUA_TNONE: have_options = 0; break; case LUA_TTABLE: have_options = 1; break; } break; case LUA_TTABLE: have_options = 1; /* avoid issues with strict.lua */ lua_pushstring(L, "command"); /* opts ... cmd */ lua_rawget(L, 1); if (!lua_isnil(L, -1)) { /* convert {command=command,arg1,...} to command {arg1,...} */ lua_insert(L, 1); /* cmd opts ... */ } else { /* convert {arg0,arg1,...} to arg0 {arg1,...} */ size_t i, n = lua_objlen(L, 1); lua_rawgeti(L, 1, 1); /* opts ... nil cmd */ lua_insert(L, 1); /* cmd opts ... nil */ for (i = 2; i <= n; i++) { lua_rawgeti(L, 2, i); /* cmd opts ... nil argi */ lua_rawseti(L, 2, i - 1); /* cmd opts ... nil */ } lua_rawseti(L, 2, n); /* cmd opts ... */ } if (lua_type(L, 1) != LUA_TSTRING) return luaL_error(L, "bad command option (string expected, got %s)", luaL_typename(L, 1)); break; } params = spawn_param_init(L); /* get filename to execute */ spawn_param_filename(params); /* get arguments, environment, and redirections */ if (have_options) { lua_getfield(L, 2, "args"); /* cmd opts ... argtab */ switch (lua_type(L, -1)) { default: return luaL_error(L, "bad args option (table expected, got %s)", luaL_typename(L, -1)); case LUA_TNIL: lua_pop(L, 1); /* cmd opts ... */ lua_pushvalue(L, 2); /* cmd opts ... opts */ if (0) /*FALLTHRU*/ case LUA_TTABLE: if (lua_objlen(L, 2) > 0) return luaL_error(L, "cannot specify both the args option and array values"); spawn_param_args(params); /* cmd opts ... */ break; } lua_getfield(L, 2, "env"); /* cmd opts ... envtab */ switch (lua_type(L, -1)) { default: return luaL_error(L, "bad env option (table expected, got %s)", luaL_typename(L, -1)); case LUA_TNIL: break; case LUA_TTABLE: spawn_param_env(params); /* cmd opts ... */ break; } lua_getfield(L, 2, "show"); /* cmd opts ... envtab */ spawn_param_show(params, lua_type(L, -1) == LUA_TBOOLEAN ? lua_toboolean(L, -1) : 0); lua_getfield(L, 2, "shell"); /* cmd opts ... envtab */ spawn_param_useshell(params, lua_type(L, -1) == LUA_TBOOLEAN ? lua_toboolean(L, -1) : 1); lua_getfield(L, 2, "detach"); /* cmd opts ... envtab */ spawn_param_detach(params, lua_type(L, -1) == LUA_TBOOLEAN ? lua_toboolean(L, -1) : 0); get_redirect(L, 2, "stdin", params); /* cmd opts ... */ get_redirect(L, 2, "stdout", params); /* cmd opts ... */ get_redirect(L, 2, "stderr", params); /* cmd opts ... */ } return spawn_param_execute(params); /* proc/nil error */ }
static int ngx_http_lua_ngx_set_cache_data(lua_State *L) { ngx_http_request_t *r; ngx_http_cache_t *c, c_tmp; ngx_http_file_cache_node_t *fcn, fcn_tmp; int n; /* top of stack when we start. */ struct { uint valid_sec:1; uint last_modified:1; uint date:1; uint min_uses:1; uint valid_msec:1; uint fcn_uses:1; uint fcn_valid_msec:1; uint fcn_expire:1; uint fcn_valid_sec:1; } isset; n = lua_gettop(L); if (n != 1) { return luaL_error(L, "only one argument is expected, but got %d", n); } luaL_checktype(L, -1, LUA_TTABLE); r = ngx_http_lua_get_request(L); if (lua_type(L, -1) != LUA_TTABLE) { return luaL_error(L, "the argument is not a table, " "but a %s", lua_typename(L, lua_type(L, -1))); } c = r->cache; if (!c) { lua_pushboolean(L, 0); return 1; } /* setup dummy copies of structs, to write into */ fcn = c->node; memset(&c_tmp, 0, sizeof(c_tmp)); memset(&fcn_tmp, 0, sizeof(fcn_tmp)); memset(&isset, 0, sizeof(isset)); ASSIGN_NUMBER_OR_RET("valid_sec", c_tmp.valid_sec, isset.valid_sec); ASSIGN_NUMBER_OR_RET("last_modified", c_tmp.last_modified, isset.last_modified); ASSIGN_NUMBER_OR_RET("date", c_tmp.date, isset.date); ASSIGN_NUMBER_OR_RET("min_uses", c_tmp.min_uses, isset.min_uses); ASSIGN_NUMBER_OR_RET("valid_msec", c_tmp.valid_msec, isset.valid_msec); /* pop all we pushed on stack */ lua_pop(L, lua_gettop(L)-n); /* file_cache_node */ if (fcn && lua_type(L, n+1) == LUA_TTABLE) { /* push the fcn subtable onto the stack */ lua_getfield(L, n, "fcn"); ASSIGN_NUMBER_OR_RET("uses", fcn_tmp.uses, isset.fcn_uses); ASSIGN_NUMBER_OR_RET("valid_msec", fcn_tmp.valid_msec, isset.fcn_valid_msec); ASSIGN_NUMBER_OR_RET("expire", fcn_tmp.expire, isset.fcn_expire); ASSIGN_NUMBER_OR_RET("valid_sec", fcn_tmp.valid_sec, isset.fcn_valid_sec); /* pop all the entries we pushed on the stack*/ lua_pop(L, lua_gettop(L)-n); } /* write out changes */ ngx_shmtx_lock(&c->file_cache->shpool->mutex); if (isset.valid_sec) { c->valid_sec = c_tmp.valid_sec; if (c->buf && c->buf->pos) { ngx_http_file_cache_header_t *h; h = (ngx_http_file_cache_header_t *) c->buf->pos; h->valid_sec = c->valid_sec; } } ASSIGN_IF_SET(c->last_modified, c_tmp.last_modified, isset.last_modified); ASSIGN_IF_SET(c->date, c_tmp.date, isset.date); ASSIGN_IF_SET(c->min_uses, c_tmp.min_uses, isset.min_uses); ASSIGN_IF_SET(c->valid_msec, c_tmp.valid_msec, isset.valid_msec); if (fcn) { ASSIGN_IF_SET(fcn->uses, fcn_tmp.uses, isset.fcn_uses); ASSIGN_IF_SET(fcn->valid_msec, fcn_tmp.valid_msec,isset.fcn_valid_msec); ASSIGN_IF_SET(fcn->expire, fcn_tmp.expire, isset.fcn_expire); ASSIGN_IF_SET(fcn->valid_sec, fcn_tmp.valid_sec, isset.fcn_valid_sec); } ngx_shmtx_unlock(&c->file_cache->shpool->mutex); /* pop the parameter off */ lua_pop(L, 1); /* push a true as a return */ lua_pushboolean(L, 1); return 1; }
int l_graphics_newFont(lua_State* state) { // TODO: alternative signatures for newFont char const * filename = NULL; int ptsize; const int type = lua_type(state, 1); if(type == LUA_TSTRING) { printf("a\n"); filename = lua_tostring(state, 1); if(lua_isnumber(state, 2)) { printf("b\n"); ptsize = lua_tonumber(state, 2); } else { printf("c\n"); ptsize = 12; lua_settop(state, 1); lua_pushnumber(state, ptsize); } lua_settop(state, 2); } else if(type == LUA_TNUMBER) { printf("d\n"); ptsize = l_tools_toNumberOrError(state, 1); lua_settop(state, 1); lua_pushstring(state, "(default)"); lua_insert(state, 1); } // Create string font:size // Stack: ... fontname lua_pushstring(state, ":"); lua_insert(state, -2); lua_concat(state, 3); printf("font: %s\n", lua_tostring(state, -1)); // Load font table to -2 // Stack: ... fonts fontname lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.loadedFontsRef); lua_insert(state, -2); // Save fontname for later // Stack: ... fonts fontname fontname lua_pushvalue(state, -1); // Load font // Stack: ... fonts fontname maybefont lua_gettable(state, -3); if(lua_isnoneornil(state, -1)) { // Stack: ... fonts fontname lua_pop(state, 1); // Stack: ... fonts fontname raw-font graphics_Font* font = lua_newuserdata(state, sizeof(graphics_Font)); if(graphics_Font_new(font, filename, ptsize)) { lua_pushstring(state, "Could not open font"); lua_error(state); } // Stack: ... fonts fontname raw-font metatable lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.fontMT); // Stack: ... fonts fontname constructed-font lua_setmetatable(state, -2); // Stack: ... fonts fontname constructed-font constructed-font lua_pushvalue(state, -1); // Stack: ... fonts constructed-font fontname constructed-font lua_insert(state, -3); // Stack: ... fonts constructed-font lua_settable(state, -4); } return 1; }
/** @name getMemoryUsage @text Get the current amount of memory used by MOAI and its subsystems. This will attempt to return reasonable estimates where exact values cannot be obtained. Some fields represent informational fields (i.e. are not double counted in the total, but present to assist debugging) and may be only available on certain platforms (e.g. Windows, etc). These fields begin with a '_' character. @out table usage The breakdown of each subsystem's memory usage, in bytes. There is also a "total" field that contains the summed value. */ int MOAISim::_getMemoryUsage ( lua_State* L ) { float divisor = 1.0f; if( lua_type(L, 1) == LUA_TSTRING ) { cc8* str = lua_tostring(L, 1); if( str[0] == 'k' || str[0] == 'K' ) divisor = 1024.0f; else if( str[0] == 'm' || str[0] == 'M' ) divisor = 1024.0f * 1024.0f; else if( str[0] == 'b' || str[0] == 'B' ) divisor = 1.0f; } size_t total = 0; lua_newtable(L); size_t count; count = MOAILuaRuntime::Get().GetMemoryUsage (); lua_pushnumber(L, count / divisor); lua_setfield(L, -2, "lua"); total += count; // This is informational only (i.e. don't double count with the previous field). // It doesn't actually seem to represent the real usage of lua, but maybe // someone is interested. lua_pushnumber ( L, lua_gc ( L, LUA_GCCOUNTB, 0 ) / divisor ); lua_setfield ( L, -2, "_luagc_count" ); count = MOAIGfxDevice::Get ().GetTextureMemoryUsage (); lua_pushnumber ( L, count / divisor ); lua_setfield ( L, -2, "texture" ); total += count; #if defined(_WIN32) PROCESS_MEMORY_COUNTERS pmc; // Print the process identifier. if ( GetProcessMemoryInfo( GetCurrentProcess(), &pmc, sizeof(pmc)) ) { lua_pushnumber(L, pmc.PagefileUsage / divisor); lua_setfield(L, -2, "_sys_vs"); lua_pushnumber(L, pmc.WorkingSetSize / divisor); lua_setfield(L, -2, "_sys_rss"); } #elif defined(__APPLE__) //&& defined(TARGET_IPHONE_SIMULATOR) // Tricky undocumented mach polling of memory struct task_basic_info t_info; mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; kern_return_t kr = task_info(mach_task_self(), TASK_BASIC_INFO, reinterpret_cast<task_info_t>(&t_info), &t_info_count); // Most likely cause for failure: |task| is a zombie. if( kr == KERN_SUCCESS ) { lua_pushnumber(L, t_info.virtual_size / divisor); lua_setfield(L, -2, "_sys_vs"); lua_pushnumber(L, t_info.resident_size / divisor); lua_setfield(L, -2, "_sys_rss"); } #endif lua_pushnumber(L, total / divisor); lua_setfield(L, -2, "total"); return 1; }
//Func: GetPlayer( clientNum ) //Retn: Player object int GetPlayer( lua_State *L ) { #if defined(PROJECT_GAME) int clientNum; if ( lua_type( L, 1 ) == LUA_TNUMBER ) { clientNum = lua_tointeger( L, 1 ); } else if ( lua_type( L, 1 ) == LUA_TSTRING ) { const char *name = lua_tostring( L, 1 ); clientNum = G_ClientFromString( NULL, name, FINDCL_SUBSTR ); if ( clientNum == -1 ) { lua_pushnil( L ); return 1; } } else { lua_pushnil( L ); return 1; } Player_CreateRef( L, clientNum ); return 1; #elif defined(PROJECT_CGAME) int num = -1; uint32_t clientsFound = 0; if ( lua_type( L, 1 ) == LUA_TNUMBER ) { num = lua_tointeger( L, 1 ); } else if ( lua_type( L, 1 ) == LUA_TSTRING ) { const char *name = lua_tostring( L, 1 ); int numFound = 0; //RAZTODO: copy G_ClientFromString for ( int i = 0; i < cgs.maxclients; i++ ) { char nameClean[MAX_NETNAME], nameClean2[MAX_NETNAME]; if ( !cgs.clientinfo[i].infoValid ) { continue; } Q_strncpyz( nameClean, cgs.clientinfo[i].name, sizeof(nameClean) ); Q_strncpyz( nameClean2, name, sizeof(nameClean2) ); Q_CleanString( nameClean, STRIP_COLOUR ); Q_CleanString( nameClean2, STRIP_COLOUR ); if ( !Q_stricmp( nameClean, nameClean2 ) ) { num = i; clientsFound |= (1 << i); numFound++; } } if ( numFound > 1 ) { int top = 0; lua_pushnil( L ); lua_pushstring( L, "Multiple matches" ); lua_newtable( L ); top = lua_gettop( L ); for ( int i = 0; i < cgs.maxclients; i++ ) { if ( clientsFound & (1 << i) ) { lua_pushnumber( L, i ); Player_CreateRef( L, i ); lua_settable( L, top ); } } return 3; } else if ( !numFound ) { lua_pushnil( L ); return 1; } } else {//if ( lua_type( L, 1 ) == LUA_TNIL ) num = cg.clientNum; } Player_CreateRef( L, num ); return 1; #endif }
inline type get(types<type>, lua_State* L, int index = -1) { return static_cast<type>(lua_type(L, index)); }
void LuaScriptUtilities::DumpStack( lua_State* const luaVM, const Ogre::LogMessageLevel messageLevel) { Ogre::LogManager* const logManager = Ogre::LogManager::getSingletonPtr(); char buffer[2048]; int top = lua_gettop(luaVM); sprintf_s(buffer, sizeof(buffer), "Lua Stack: size %d", top); logManager->logMessage(buffer, messageLevel); for (int index = 1; index <= top; index++) { int type = lua_type(luaVM, index); switch (type) { case LUA_TSTRING: sprintf_s( buffer, sizeof(buffer), " %d: [%s] %s", index, lua_typename(luaVM, type), lua_tostring(luaVM, index)); break; case LUA_TBOOLEAN: sprintf_s( buffer, sizeof(buffer), " %d: [%s] %s", index, lua_typename(luaVM, type), lua_toboolean(luaVM, index) ? "true" : "false"); break; case LUA_TNUMBER: sprintf_s( buffer, sizeof(buffer), " %d: [%s] %g", index, lua_typename(luaVM, type), lua_tonumber(luaVM, index)); break; case LUA_TUSERDATA: sprintf_s( buffer, sizeof(buffer), " %d: [%s] %s", index, lua_typename(luaVM, type), GetUserdataTypeName(luaVM, index).c_str()); break; case LUA_TNIL: sprintf_s( buffer, sizeof(buffer), " %d: [%s]", index, lua_typename(luaVM, type)); break; default: sprintf_s( buffer, sizeof(buffer), " %d: [%s]", index, lua_typename(luaVM, type)); break; } logManager->logMessage(buffer, messageLevel); } }
AString cLuaState::GetTypeText(int a_StackPos) { return lua_typename(m_LuaState, lua_type(m_LuaState, a_StackPos)); }
int lua_Curve_evaluate(lua_State* state) { // Get the number of parameters. int paramCount = lua_gettop(state); // Attempt to match the parameters to a valid binding. switch (paramCount) { case 3: { do { if ((lua_type(state, 1) == LUA_TUSERDATA) && lua_type(state, 2) == LUA_TNUMBER && (lua_type(state, 3) == LUA_TTABLE || lua_type(state, 3) == LUA_TLIGHTUSERDATA)) { // Get parameter 1 off the stack. float param1 = (float)luaL_checknumber(state, 2); // Get parameter 2 off the stack. gameplay::ScriptUtil::LuaArray<float> param2 = gameplay::ScriptUtil::getFloatPointer(3); Curve* instance = getInstance(state); instance->evaluate(param1, param2); return 0; } } while (0); lua_pushstring(state, "lua_Curve_evaluate - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } case 6: { do { if ((lua_type(state, 1) == LUA_TUSERDATA) && lua_type(state, 2) == LUA_TNUMBER && lua_type(state, 3) == LUA_TNUMBER && lua_type(state, 4) == LUA_TNUMBER && lua_type(state, 5) == LUA_TNUMBER && (lua_type(state, 6) == LUA_TTABLE || lua_type(state, 6) == LUA_TLIGHTUSERDATA)) { // Get parameter 1 off the stack. float param1 = (float)luaL_checknumber(state, 2); // Get parameter 2 off the stack. float param2 = (float)luaL_checknumber(state, 3); // Get parameter 3 off the stack. float param3 = (float)luaL_checknumber(state, 4); // Get parameter 4 off the stack. float param4 = (float)luaL_checknumber(state, 5); // Get parameter 5 off the stack. gameplay::ScriptUtil::LuaArray<float> param5 = gameplay::ScriptUtil::getFloatPointer(6); Curve* instance = getInstance(state); instance->evaluate(param1, param2, param3, param4, param5); return 0; } } while (0); lua_pushstring(state, "lua_Curve_evaluate - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } default: { lua_pushstring(state, "Invalid number of parameters (expected 3 or 6)."); lua_error(state); break; } } return 0; }
bool Settings::save() { auto& actions = m_app.getActions(); Lumix::FS::OsFile file; auto& allocator = m_app.getWorldEditor()->getAllocator(); if (!file.open(SETTINGS_PATH, Lumix::FS::Mode::CREATE_AND_WRITE, allocator)) return false; file << "window = { x = " << m_window.x << ", y = " << m_window.y << ", w = " << m_window.w << ", h = " << m_window.h << " }\n"; file << "maximized = " << (m_is_maximized ? "true" : "false") << "\n"; auto writeBool = [&file](const char* name, bool value) { file << name << " = " << (value ? "true\n" : "false\n"); }; writeBool("settings_opened", m_is_opened); writeBool("asset_browser_opened", m_is_asset_browser_opened); writeBool("entity_list_opened", m_is_entity_list_opened); writeBool("entity_template_list_opened", m_is_entity_template_list_opened); writeBool("log_opened", m_is_log_opened); writeBool("profiler_opened", m_is_profiler_opened); writeBool("properties_opened", m_is_properties_opened); writeBool("error_reporting_enabled", m_is_crash_reporting_enabled); file << "mouse_sensitivity_x = " << m_mouse_sensitivity_x << "\n"; file << "mouse_sensitivity_y = " << m_mouse_sensitivity_y << "\n"; saveStyle(file); file << "data_dir = \""; const char* c = m_data_dir; while (*c) { if (*c == '\\') file << "\\\\"; else file << *c; ++c; } file << "\"\n"; file << "custom = {\n"; lua_getglobal(m_state, "custom"); lua_pushnil(m_state); bool first = true; while (lua_next(m_state, -2)) { if (!first) file << ",\n"; const char* name = lua_tostring(m_state, -2); switch (lua_type(m_state, -1)) { case LUA_TBOOLEAN: file << name << " = " << (lua_toboolean(m_state, -1) != 0 ? "true" : "false"); break; case LUA_TNUMBER: file << name << " = " << (int)lua_tonumber(m_state, -1); break; default: ASSERT(false); break; } lua_pop(m_state, 1); first = false; } lua_pop(m_state, 1); file << "}\n"; file << "actions = {\n"; for (int i = 0; i < actions.size(); ++i) { file << "\t" << actions[i]->name << " = {" << actions[i]->shortcut[0] << ", " << actions[i]->shortcut[1] << ", " << actions[i]->shortcut[2] << "},\n"; } file << "}\n"; file << "toolbar = {\n"; for (auto* action : m_app.getToolbarActions()) { file << "\t\"" << action->name << "\",\n"; } file << "}\n"; ImGui::SaveDock(file); file.close(); return true; }