00001 #include "lugre_prefix.h"
00002 #include <assert.h>
00003 #include <stdarg.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <cstdio>
00007 #include <string>
00008 #include <vector>
00009 #include "lugre_net.h"
00010 #include "lugre_fifo.h"
00011 #include "lugre_game.h"
00012 #include "lugre_listener.h"
00013 #include "lugre_scripting.h"
00014 #include "lugre_input.h"
00015 #include "lugre_robstring.h"
00016 #include "lugre_gfx3D.h"
00017 #include "lugre_gfx2D.h"
00018 #include "lugre_widget.h"
00019 #include "lugre_luabind.h"
00020 #include "lugre_luabind_direct.h"
00021 #include "lugre_shell.h"
00022 #include "lugre_timer.h"
00023 #include "lugre_bitmask.h"
00024 #include "lugre_camera.h"
00025 #include "lugre_viewport.h"
00026 #include "lugre_rendertexture.h"
00027 #include "lugre_sound.h"
00028 #include "lugre_luaxml.h"
00029 #include "lugre_meshshape.h"
00030 #include "lugre_random.h"
00031 #include "lugre_thread.h"
00032 #include "lugre_ode.h"
00033 #include "lugre_cadune.h"
00034 #include "lugre_md5.h"
00035 #include "lugre_paged_geometry.h"
00036 #include "lugre_caelum.h"
00037 #include "lugre_texatlas.h"
00038 #include "lugre_image.h"
00039 #include "lugre_commondialog.h"
00040 #include "lugre_main.h"
00041 #include "lugre_robstring.h"
00042 #include "lugre_utils.h"
00043
00044
00045 #if LUGRE_PLATFORM == LUGRE_PLATFORM_LINUX
00046 #include <sys/types.h>
00047 #include <unistd.h>
00048 #endif
00049
00050
00051 using namespace Lugre;
00052
00053
00054 namespace Lugre {
00055 extern bool gMeshBuffer_PrintStacktraceOnLoad;
00056 void DisplayNotice (const char* szMsg);
00057 void DisplayErrorMessage (const char* szMsg);
00058 void Material_LuaRegister (void *L);
00059 void Beam_LuaRegister (void *L);
00060 void LuaRegister_VertexBuffer (lua_State *L);
00061 void LuaRegister_LuaBinds_Ogre (lua_State *L);
00062 void PrintLuaStackTrace ();
00063 void ProfileDumpCallCount ();
00064
00065 void rob_dirlist (const char* path,std::vector<std::string>& res,const bool bDirs,const bool bFiles);
00066
00067 int rob_mkdir (const char* path,int perm);
00068 int rob_rmdir (const char* path);
00069 };
00070
00071
00072
00073
00075 static int l_GetAllKeyNames (lua_State *L) { PROFILE
00076 lua_newtable(L);
00077 for (int i=0;i<cInput::GetKeyNameCount();++i) {
00078 lua_pushstring( L, cInput::GetKeyNameByIndex(i) );
00079 lua_rawseti( L, -2, i );
00080 }
00081 return 1;
00082 }
00083
00085 static int l_GetNamedKey (lua_State *L) { PROFILE
00086 const char *keyname = luaL_checkstring(L, 1);
00087 lua_pushnumber(L,cInput::GetSingleton().GetNamedKey(keyname));
00088 return 1;
00089 }
00090
00092 static int l_GetKeyName (lua_State *L) { PROFILE
00093 int keycode = luaL_checkint(L, 1);
00094 lua_pushstring(L,cInput::GetSingleton().GetKeyName(keycode));
00095 return 1;
00096 }
00097
00099 static int l_Terminate (lua_State *L) { PROFILE
00100 cShell::mbAlive = false;
00101 return 0;
00102 }
00103
00104 static int l_GetPointerSize (lua_State *L) { PROFILE
00105 lua_pushnumber(L,sizeof(void*));
00106 return 1;
00107 }
00108
00110 static int l_Client_IsAlive (lua_State *L) { PROFILE
00111 lua_pushboolean(L,cShell::mbAlive);
00112 return 1;
00113 }
00114
00116 static int l_PollInput (lua_State *L) { PROFILE
00117 lua_pushnumber(L,cInput::iMouse[0]);
00118 lua_pushnumber(L,cInput::iMouse[1]);
00119 lua_pushnumber(L,cInput::iMouseWheel);
00120 lua_pushnumber(L,cInput::iMouseWheel_pressed);
00121 lua_pushnumber(L,cInput::iMouseWheel_all_since_last_step);
00122 lua_pushnumber(L,cInput::iMouseWheel_pressed_since_last_step);
00123 return 6;
00124 }
00125
00126
00127 static int l_ProfileDumpCallCount (lua_State *L) { PROFILE
00128 ProfileDumpCallCount();
00129
00130 #ifdef PROFILE_LUACALLCOUNT
00131 printf("LuaCallCounts:\n\n");
00132
00133 std::multiset<std::pair<const char*,int>,cPROFILE_LUACALLCOUNTSetCmp> myCallCountProfileSet;
00134 typedef std::multiset<std::pair<const char*,int>,cPROFILE_LUACALLCOUNTSetCmp>::iterator tLuaCallCountProfileSetItor;
00135 { for (std::map<const char*,int>::iterator itor=gPROFILE_LUACALLCOUNT.begin();itor != gPROFILE_LUACALLCOUNT.end();++itor)
00136 myCallCountProfileSet.insert(std::make_pair((*itor).first,(*itor).second));
00137 }
00138 int i=0;
00139 for (tLuaCallCountProfileSetItor itor=myCallCountProfileSet.begin();itor != myCallCountProfileSet.end();++itor) {
00140
00141 printf("LuaCallCallCount %16d %s\n",(*itor).second,(*itor).first);
00142 }
00143 #endif
00144
00145 return 0;
00146 }
00147
00148
00149 static int l_Client_GetCurFPS (lua_State *L) { PROFILE
00150 lua_pushnumber(L, (cTimer::miTimeSinceLastFrame > 0) ? (float(1000.0) / float(cTimer::miTimeSinceLastFrame)) : 0.0 );
00151 return 1;
00152 }
00153
00154 static int l_Client_GetFrameNum (lua_State *L) { PROFILE
00155 lua_pushnumber(L, cTimer::miCurFrameNum );
00156 return 1;
00157 }
00158
00159 static int l_Client_GetTicks (lua_State *L) { PROFILE
00160 lua_pushnumber(L, cShell::GetTicks());
00161 return 1;
00162 }
00163
00164 static int l_Client_GetMemoryUsage (lua_State *L) { PROFILE
00165 uint32 memory = 0;
00166
00167 #if LUGRE_PLATFORM == LUGRE_PLATFORM_LINUX
00168 try {
00169 pid_t pid = getpid();
00170
00171 std::stringstream filename;
00172 filename << "/proc/" << uint32(pid) << "/stat";
00173 std::string content = GetFileContent(filename.str());
00174
00175 size_t found = content.rfind(')');
00176
00177 if (found != std::string::npos){
00178 std::string sub = content.substr(found + 2);
00179 std::vector< std::string > l;
00180 explodestr(" ", sub.c_str(), l);
00181
00182
00183 memory = atoi(l[20].c_str());
00184 }
00185 }catch(...){
00186 memory = 0;
00187 }
00188 #endif
00189
00190 lua_pushnumber(L, memory);
00191 return 1;
00192 }
00193
00195 static int l_Uo16Color2Rgb (lua_State *L) { PROFILE
00196 unsigned short color = luaL_checkint(L,1);
00197
00198 float r = float((color >> 10) & 0x1F)/float(0x1f);
00199 float g = float((color >> 5) & 0x1F)/float(0x1f);
00200 float b = float((color >> 0) & 0x1F)/float(0x1f);
00201
00202 lua_pushnumber(L, r);
00203 lua_pushnumber(L, g);
00204 lua_pushnumber(L, b);
00205 return 3;
00206 }
00207
00208
00210 static int l_Client_Sleep (lua_State *L) { PROFILE
00211
00212 #ifndef WIN32
00213 sleep(luaL_checkint(L,1));
00214 #else
00215 Sleep(luaL_checkint(L,1)*1000);
00216 #endif
00217 return 0;
00218 }
00219
00221 static int l_Client_USleep (lua_State *L) { PROFILE
00222
00223 #ifndef WIN32
00224 usleep((uint32)luaL_checkint(L,1)*(uint32)1000L);
00225 #else
00226 Sleep(luaL_checkint(L,1));
00227 #endif
00228 return 0;
00229 }
00230
00231
00232 static int l_Client_GetPhysStepTime (lua_State *L) { PROFILE
00233 lua_pushnumber(L,cTimer::mfPhysStepTime);
00234 return 1;
00235 }
00236
00237
00238 static int l_file_exists (lua_State *L) { PROFILE
00239 std::ifstream myFileStream(luaL_checkstring(L,1));
00240 lua_pushboolean(L,myFileStream?true:false);
00241 return 1;
00242 }
00243
00244 static int l_remove_file (lua_State *L) { PROFILE
00245 remove(luaL_checkstring(L,1));
00246 return 0;
00247 }
00248
00249
00250 static int l_file_size (lua_State *L) { PROFILE
00251 std::ifstream myFileStream(luaL_checkstring(L,1));
00252 int iFileSize = 0;
00253 if (myFileStream) {
00254 myFileStream.seekg(0, std::ios::end);
00255 iFileSize = myFileStream.tellg();
00256 }
00257 lua_pushnumber(L,iFileSize);
00258 return 1;
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 static int l_mkdir (lua_State *L) { PROFILE
00270 std::string sPath = luaL_checkstring(L,1);
00271 int iMode = cLuaBindDirectQuickWrapHelper::ParamIntDefault(L,2,0700);
00272 lua_pushnumber(L,rob_mkdir(sPath.c_str(),iMode));
00273 return 1;
00274 }
00275
00276
00277
00278 static int l_rmdir (lua_State *L) { PROFILE
00279 std::string sPath = luaL_checkstring(L,1);
00280 lua_pushnumber(L,rob_rmdir(sPath.c_str()));
00281 return 1;
00282 }
00283
00285 static int l_dirlist (lua_State *L) { PROFILE
00286 std::string sDirPath = luaL_checkstring(L,1);
00287 std::vector<std::string> res;
00288 rob_dirlist(sDirPath.c_str(),res,lua_toboolean(L,2),lua_toboolean(L,3));
00289 lua_newtable(L);
00290 for (unsigned int i=0;i<res.size();++i) {
00291 lua_pushstring( L, res[i].c_str() );
00292 lua_rawseti( L, -2, i+1 );
00293 }
00294 return 1;
00295 }
00296
00297
00298
00299
00300
00301 static int l_GetRandomHexString (lua_State *L) { PROFILE lua_pushstring( L, strprintf("0x%08x",rand()).c_str()); return 1; }
00302
00304 static int l_Hex2Num (lua_State *L) { PROFILE
00305 std::string hexcode = luaL_checkstring(L,1);
00306 uint32 res = 0;
00307 sscanf(hexcode.c_str(),"0x%x",&res);
00308 lua_pushnumber(L, res);
00309 return 1;
00310 }
00311
00312
00313 static int l_BitwiseAND (lua_State *L) { PROFILE
00314
00315 double g = luaL_checknumber(L,1);
00316 double h = luaL_checknumber(L,2);
00317 uint32 a = uint32(g);
00318 uint32 b = uint32(h);
00319 uint32 c = a & b;
00320 lua_pushnumber( L, c);
00321 return 1;
00322 }
00323 static int l_BitwiseOR (lua_State *L) { PROFILE
00324
00325 double g = luaL_checknumber(L,1);
00326 double h = luaL_checknumber(L,2);
00327 uint32 a = uint32(g);
00328 uint32 b = uint32(h);
00329 uint32 c = a | b;
00330 lua_pushnumber( L, c);
00331 return 1;
00332 }
00333
00334
00335 static int l_BitwiseXOR (lua_State *L) { PROFILE
00336
00337 double g = luaL_checknumber(L,1);
00338 double h = luaL_checknumber(L,2);
00339 uint32 a = uint32(g);
00340 uint32 b = uint32(h);
00341 uint32 c = a ^ b;
00342 lua_pushnumber( L, c);
00343 return 1;
00344 }
00345
00346
00347 static int l_BitwiseSHL (lua_State *L) { PROFILE
00348
00349 double g = luaL_checknumber(L,1);
00350 double h = luaL_checknumber(L,2);
00351 uint32 a = uint32(g);
00352 uint32 b = uint32(h);
00353 uint32 c = a << b;
00354 lua_pushnumber( L, c);
00355 return 1;
00356 }
00357
00358 static int l_BitwiseSHR (lua_State *L) { PROFILE
00359
00360 double g = luaL_checknumber(L,1);
00361 double h = luaL_checknumber(L,2);
00362 uint32 a = uint32(g);
00363 uint32 b = uint32(h);
00364 uint32 c = a >> b;
00365 lua_pushnumber( L, c);
00366 return 1;
00367 }
00368
00369
00370 static int l_TestBit (lua_State *L) { PROFILE lua_pushboolean(L,(uint32(luaL_checknumber(L,1)) & (uint32(1) << luaL_checkint(L,2))) != 0); return 1; }
00371 static int l_SetBit (lua_State *L) { PROFILE lua_pushnumber( L,(uint32(luaL_checknumber(L,1)) | (uint32(1) << luaL_checkint(L,2)))); return 1; }
00372 static int l_ClearBit (lua_State *L) { PROFILE
00373 uint32 input = uint32(luaL_checknumber(L,1));
00374 uint32 mask = 1L << uint32(luaL_checknumber(L,2));
00375 lua_pushnumber(L,(input & mask)?(input ^ mask):(input));
00376 return 1;
00377 }
00378
00379
00380 static int l_Exit (lua_State *L) { PROFILE
00381 exit((lua_gettop(L) >= 1 && !lua_isnil(L,1)) ? luaL_checkint(L,1) : 0);
00382 return 0;
00383 }
00384
00385 static int l_Crash (lua_State *L) { PROFILE
00386 DisplayErrorMessage("CRASH triggered from script, see console for info (start with commandline option -c)");
00387 exit(88);
00388 return 0;
00389 }
00390
00392 static int l_CrashSegFault (lua_State *L) { PROFILE
00393 DisplayErrorMessage("testing segfault handler...");
00394 *((char*)0) = 0;
00395 return 0;
00396 }
00397
00398
00399 static int l_DisplayNotice (lua_State *L) { PROFILE
00400 DisplayNotice(luaL_checkstring(L,1));
00401 return 0;
00402 }
00403
00404
00405 static int l_FatalErrorMessage (lua_State *L) { PROFILE
00406 DisplayErrorMessage(luaL_checkstring(L,1));
00407 exit(77);
00408 return 0;
00409 }
00410
00411 static int l_GetMainWorkingDir (lua_State *L) { PROFILE
00412 lua_pushstring(L,GetMainWorkingDir().c_str());
00413 return 1;
00414 }
00415
00416 static int l_GetLugreLuaPath (lua_State *L) { PROFILE
00417 lua_pushstring(L,GetLugreLuaPath().c_str());
00418 return 1;
00419 }
00420
00421
00422
00423
00425 static int l_LugreMessageBox (lua_State *L) { PROFILE
00426 int iType = luaL_checkint(L, 1);
00427 std::string sTitle = luaL_checkstring(L,2);
00428 std::string sText = luaL_checkstring(L,3);
00429 lua_pushnumber(L,LugreMessageBox((eLugreMessageBoxType)iType,sTitle,sText));
00430 return 1;
00431 }
00432
00434 static int l_OpenBrowser (lua_State *L) { PROFILE
00435 std::string sURL = luaL_checkstring(L,1);
00436 if (!OpenBrowser(sURL)) return 0;
00437 lua_pushboolean(L,true);
00438 return 1;
00439 }
00440
00442 static int l_FileOpenDialog (lua_State *L) { PROFILE
00443 std::string sInitialDir = luaL_checkstring(L,1);
00444 std::string sFilePattern = luaL_checkstring(L,2);
00445 std::string sTitle = luaL_checkstring(L,3);
00446 std::string sFilePath;
00447 if (!FileOpenDialog(sInitialDir,sFilePattern,sTitle,sFilePath)) return 0;
00448 lua_pushstring(L,sFilePath.c_str());
00449 return 1;
00450 }
00451
00453 static int l_FileSaveDialog (lua_State *L) { PROFILE
00454 std::string sInitialDir = luaL_checkstring(L,1);
00455 std::string sFilePattern = luaL_checkstring(L,2);
00456 std::string sTitle = luaL_checkstring(L,3);
00457 std::string sFilePath;
00458 if (!FileSaveDialog(sInitialDir,sFilePattern,sTitle,sFilePath)) return 0;
00459 lua_pushstring(L,sFilePath.c_str());
00460 return 1;
00461 }
00462
00463
00464
00465 void RegisterLua_General_GlobalFunctions (lua_State* L) {
00466
00467
00468 LUABIND_QUICKWRAP_STATIC(SetMeshBuffer_PrintStacktraceOnLoad,{ gMeshBuffer_PrintStacktraceOnLoad = ParamBool(L,2); })
00469
00470 lua_register(L,"LugreMessageBox", l_LugreMessageBox);
00471 lua_register(L,"OpenBrowser", l_OpenBrowser);
00472 lua_register(L,"FileOpenDialog", l_FileOpenDialog);
00473 lua_register(L,"FileSaveDialog", l_FileSaveDialog);
00474 lua_register(L,"GetMainWorkingDir", l_GetMainWorkingDir);
00475 lua_register(L,"GetLugreLuaPath", l_GetLugreLuaPath);
00476 lua_register(L,"GetAllKeyNames", l_GetAllKeyNames);
00477 lua_register(L,"GetNamedKey", l_GetNamedKey);
00478 lua_register(L,"GetKeyName", l_GetKeyName);
00479 lua_register(L,"PollInput", l_PollInput);
00480 lua_register(L,"Terminate", l_Terminate);
00481 lua_register(L,"GetPointerSize", l_GetPointerSize);
00482 lua_register(L,"Client_IsAlive", l_Client_IsAlive);
00483
00484
00485
00486
00487
00488
00489
00490
00491 lua_register(L,"Client_GetCurFPS", l_Client_GetCurFPS);
00492 lua_register(L,"Client_GetFrameNum", l_Client_GetFrameNum);
00493
00494
00495
00496 lua_register(L,"Client_GetMemoryUsage", l_Client_GetMemoryUsage);
00497 lua_register(L,"Client_GetTicks", l_Client_GetTicks);
00498 lua_register(L,"Client_Sleep", l_Client_Sleep);
00499 lua_register(L,"Client_USleep", l_Client_USleep);
00500 lua_register(L,"Client_GetPhysStepTime", l_Client_GetPhysStepTime);
00501 lua_register(L,"file_exists", l_file_exists);
00502 lua_register(L,"file_size", l_file_size);
00503 lua_register(L,"mkdir", l_mkdir);
00504 lua_register(L,"rmdir", l_rmdir);
00505 lua_register(L,"remove_file", l_remove_file);
00506 lua_register(L,"dirlist", l_dirlist);
00507 lua_register(L,"Hex2Num", l_Hex2Num);
00508 lua_register(L,"GetRandomHexString", l_GetRandomHexString);
00509 lua_register(L,"BitwiseAND", l_BitwiseAND);
00510 lua_register(L,"BitwiseOR", l_BitwiseOR);
00511 lua_register(L,"BitwiseXOR", l_BitwiseXOR);
00512 lua_register(L,"BitwiseSHL", l_BitwiseSHL);
00513 lua_register(L,"BitwiseSHR", l_BitwiseSHR);
00514 lua_register(L,"TestBit", l_TestBit);
00515 lua_register(L,"SetBit", l_SetBit);
00516 lua_register(L,"ClearBit", l_ClearBit);
00517 lua_register(L,"Exit", l_Exit);
00518 lua_register(L,"Crash", l_Crash);
00519 lua_register(L,"CrashSegFault", l_CrashSegFault);
00520 lua_register(L,"DisplayNotice", l_DisplayNotice);
00521 lua_register(L,"FatalErrorMessage", l_FatalErrorMessage);
00522 lua_register(L,"ProfileDumpCallCount", l_ProfileDumpCallCount);
00523 lua_register(L,"Uo16Color2Rgb", l_Uo16Color2Rgb);
00524
00525
00526 bool bIsWin = false;
00527 #ifdef WIN32
00528 bIsWin = true;
00529 #endif
00530
00531 lua_pushboolean(L,bIsWin);
00532 lua_setglobal(L,"WIN32");
00533
00534 #define BIND_LUA_CONSTANT(name) cScripting::SetGlobal(L,#name,name);
00535 BIND_LUA_CONSTANT(kLugreMessageBoxResult_Ok )
00536 BIND_LUA_CONSTANT(kLugreMessageBoxResult_Yes )
00537 BIND_LUA_CONSTANT(kLugreMessageBoxResult_No )
00538 BIND_LUA_CONSTANT(kLugreMessageBoxResult_Cancel )
00539 BIND_LUA_CONSTANT(kLugreMessageBoxResult_BoxNotImplemented )
00540 BIND_LUA_CONSTANT(kLugreMessageBoxResult_Unknown )
00541 BIND_LUA_CONSTANT(kLugreMessageBoxType_Ok)
00542 BIND_LUA_CONSTANT(kLugreMessageBoxType_OkCancel)
00543 BIND_LUA_CONSTANT(kLugreMessageBoxType_YesNo)
00544 BIND_LUA_CONSTANT(kLugreMessageBoxType_YesNoCancel)
00545 }
00546
00547
00548
00549 void RegisterLua_General_Classes (lua_State* L) {
00550 LuaRegister_LuaBinds_Ogre(L);
00551 LuaRegister_VertexBuffer(L);
00552 RegisterLuaXML(L);
00553 LuaRegisterFIFO(L);
00554 LuaRegisterNet(L);
00555 LuaRegisterThreading(L);
00556 cGfx3D::LuaRegister(L);
00557 cGfx2D::LuaRegister(L);
00558 cBitMask::LuaRegister(L);
00559 cDialog::LuaRegister(L);
00560 cWidget::LuaRegister(L);
00561 cSoundSource::LuaRegister(L);
00562 cRandom::LuaRegister(L);
00563 cTexAtlas::LuaRegister(L);
00564 cImage::LuaRegister(L);
00565
00566 #ifdef USE_LUGRE_LIB_CADUNE_TREE
00567 LuaRegisterCaduneTree(L);
00568 #endif
00569
00570 #ifdef USE_LUGRE_LIB_PAGED_GEOMETRY
00571 LuaRegisterPagedGeometry(L);
00572 #endif
00573
00574 #ifdef USE_LUGRE_LIB_MD5
00575 LuaRegisterMD5(L);
00576 #endif
00577
00578 #ifdef USE_LUGRE_LIB_CAELUM
00579 LuaRegisterCaelum(L);
00580 #endif
00581
00582 #ifdef ENABLE_ODE
00583 RegisterLua_Ode_GlobalFunctions(L);
00584 OdeLuaRegister(L);
00585 #endif
00586 }
00587