Advertisement
Guest User

Untitled

a guest
Jun 7th, 2012
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 5.23 KB | None | 0 0
  1. diff --git a/src/luascript.cpp b/src/luascript.cpp
  2. index 5eeb6d8..8cde298 100644
  3. --- a/src/luascript.cpp
  4. +++ b/src/luascript.cpp
  5. @@ -72,6 +72,8 @@ ScriptEnviroment::ThingMap ScriptEnviroment::m_globalMap;
  6.  ScriptEnviroment::StorageMap ScriptEnviroment::m_storageMap;
  7.  ScriptEnviroment::TempItemListMap ScriptEnviroment::m_tempItems;
  8.  
  9. +lua_State *LuaInterface::m_luaState = NULL;
  10. +
  11.  ScriptEnviroment::ScriptEnviroment()
  12.  {
  13.     m_lastUID = 70000;
  14. @@ -637,7 +639,6 @@ int32_t LuaInterface::m_scriptEnvIndex = -1;
  15.  
  16.  LuaInterface::LuaInterface(std::string interfaceName)
  17.  {
  18. -   m_luaState = NULL;
  19.     m_interfaceName = interfaceName;
  20.     m_lastTimer = 1000;
  21.     m_errors = true;
  22. @@ -749,8 +750,7 @@ bool LuaInterface::loadDirectory(const std::string& dir, Npc* npc/* = NULL*/)
  23.  
  24.  int32_t LuaInterface::getEvent(const std::string& eventName)
  25.  {
  26. -   //get our events table
  27. -   lua_getfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
  28. +    lua_rawgeti(m_luaState, LUA_REGISTRYINDEX, m_eventTableRef);
  29.     if(!lua_istable(m_luaState, -1))
  30.     {
  31.         lua_pop(m_luaState, 1);
  32. @@ -831,7 +831,7 @@ void LuaInterface::error(const char* function, const std::string& desc)
  33.  
  34.  bool LuaInterface::pushFunction(int32_t function)
  35.  {
  36. -   lua_getfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
  37. +    lua_rawgeti(m_luaState, LUA_REGISTRYINDEX, m_eventTableRef);
  38.     if(lua_istable(m_luaState, -1))
  39.     {
  40.         lua_pushnumber(m_luaState, function);
  41. @@ -847,9 +847,8 @@ bool LuaInterface::pushFunction(int32_t function)
  42.  
  43.  bool LuaInterface::initState()
  44.  {
  45. +    if(!m_luaState) {
  46.     m_luaState = luaL_newstate();
  47. -   if(!m_luaState)
  48. -       return false;
  49.  
  50.     luaL_openlibs(m_luaState);
  51.  #ifdef __LUAJIT__
  52. @@ -859,9 +858,10 @@ bool LuaInterface::initState()
  53.     registerFunctions();
  54.     if(!loadDirectory(getFilePath(FILE_TYPE_OTHER, "lib/"), NULL))
  55.         std::cout << "[Warning - LuaInterface::initState] Cannot load " << getFilePath(FILE_TYPE_OTHER, "lib/") << std::endl;
  56. +    }
  57.  
  58.     lua_newtable(m_luaState);
  59. -   lua_setfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
  60. +    m_eventTableRef = luaL_ref(m_luaState, LUA_REGISTRYINDEX);
  61.     m_runningEvent = EVENT_ID_USER;
  62.     return true;
  63.  }
  64. @@ -883,6 +883,8 @@ bool LuaInterface::closeState()
  65.  
  66.     m_timerEvents.clear();
  67.     lua_close(m_luaState);
  68. +    m_luaState = NULL;
  69. +    std::cout << "[LuaInterface::closeState]: This must be watched, never tested." << std::endl;
  70.     return true;
  71.  }
  72.  
  73. diff --git a/src/luascript.h b/src/luascript.h
  74. index 92de902..50b3366 100644
  75. --- a/src/luascript.h
  76. +++ b/src/luascript.h
  77. @@ -737,7 +737,8 @@ class LuaInterface
  78.         static int32_t luaStdSHA512(lua_State* L);
  79.         static int32_t luaStdVAHash(lua_State* L);
  80.  
  81. -       lua_State* m_luaState;
  82. +        int m_eventTableRef;
  83. +        static lua_State* m_luaState;
  84.         bool m_errors;
  85.         std::string m_lastError;
  86.  
  87. diff --git a/src/movement.cpp b/src/movement.cpp
  88. index cbc4f4c..41962c3 100644
  89. --- a/src/movement.cpp
  90. +++ b/src/movement.cpp
  91. @@ -35,9 +35,20 @@ extern MoveEvents* g_moveEvents;
  92.  
  93.  MoveEvent* MoveEventScript::event = NULL;
  94.  
  95. +MoveEventScript::MoveEventScript() :
  96. +    LuaInterface("MoveEvents Interface")
  97. +{
  98. +    initState();
  99. +
  100. +    static bool registerLuaFunctions = false;
  101. +    if(!registerLuaFunctions) { // only 1 luastate now, no need to register functions more than once.
  102. +        registerFunctions();
  103. +        registerLuaFunctions = true;
  104. +    }
  105. +}
  106. +
  107.  void MoveEventScript::registerFunctions()
  108.  {
  109. -   LuaInterface::registerFunctions();
  110.     lua_register(m_luaState, "callFunction", MoveEventScript::luaCallFunction);
  111.  }
  112.  
  113. @@ -126,7 +137,6 @@ int32_t MoveEventScript::luaCallFunction(lua_State* L)
  114.  MoveEvents::MoveEvents():
  115.     m_lastCacheTile(NULL)
  116.  {
  117. -   m_interface.initState();
  118.  }
  119.  
  120.  inline void MoveEvents::clearMap(MoveListMap& map)
  121. diff --git a/src/movement.h b/src/movement.h
  122. index 9d32a54..46ee781 100644
  123. --- a/src/movement.h
  124. +++ b/src/movement.h
  125. @@ -25,7 +25,7 @@ class MoveEvent;
  126.  class MoveEventScript : public LuaInterface
  127.  {
  128.     public:
  129. -       MoveEventScript() : LuaInterface("MoveEvents Interface") {}
  130. +        MoveEventScript();
  131.         virtual ~MoveEventScript() {}
  132.  
  133.         static MoveEvent* event;
  134. diff --git a/src/npc.cpp b/src/npc.cpp
  135. index b167665..8eb3a3c 100644
  136. --- a/src/npc.cpp
  137. +++ b/src/npc.cpp
  138. @@ -93,11 +93,7 @@ bool Npc::load()
  139.  
  140.     reset();
  141.     if(!m_interface)
  142. -   {
  143.         m_interface = new NpcScript();
  144. -       m_interface->loadFile(getFilePath(FILE_TYPE_OTHER, "npc/lib/npc.lua"));
  145. -       m_interface->loadFile(getFilePath(FILE_TYPE_OTHER, "npc/lib/npcsystem/main.lua"));
  146. -   }
  147.  
  148.     loaded = loadFromXml(m_filename);
  149.     return isLoaded();
  150. @@ -2400,11 +2396,18 @@ NpcScript::NpcScript() :
  151.     LuaInterface("NpcScript Interface")
  152.  {
  153.     initState();
  154. +
  155. +    static bool registerLuaFunctions = false;
  156. +    if(!registerLuaFunctions) { // only 1 luastate now, no need to register functions more than once.
  157. +        registerFunctions();
  158. +        loadFile(getFilePath(FILE_TYPE_OTHER, "npc/lib/npc.lua"));
  159. +        loadFile(getFilePath(FILE_TYPE_OTHER, "npc/lib/npcsystem/main.lua"));
  160. +        registerLuaFunctions = true;
  161. +    }
  162.  }
  163.  
  164.  void NpcScript::registerFunctions()
  165.  {
  166. -   LuaInterface::registerFunctions();
  167.     lua_register(m_luaState, "selfFocus", NpcScript::luaActionFocus);
  168.     lua_register(m_luaState, "selfSay", NpcScript::luaActionSay);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement