My Project
NPLScriptingState.h
1 #pragma once
2 #include <map>
3 #include "NPLCommon.h"
4 
5 namespace NPL
6 {
7  class CNPLStateMemAllocator;
8 }
9 
10 namespace ParaScripting
11 {
12  using namespace luabind;
13 
24  {
25  public:
28  {
29  // using the system's default malloc/realloc/free functions, please note that NPL makes heavy use of realloc
30  MEM_ALLOC_TYPE_SYS_MALLOC,
31  // using fixed sized pool. I thought this one should be fastest, but it turns out to the slowest.
32  // perhaps due to lua memory allocator heavily depends on realloc instead of malloc.
33  MEM_ALLOC_TYPE_POOL_MALLOC,
34  // using the Doug Lea memory allocator. This is a fast thread local memory allocator.
35  MEM_ALLOC_TYPE_DL_MALLOC,
36  };
37 
42  CNPLScriptingState(bool bCreateState=true);
43  virtual ~CNPLScriptingState(void);
44 
46  int GetLastReturnValue() const;
47  void SetLastReturnValue(int val);
48  public:
50  lua_State* GetLuaState();
51 
53  bool IsValid();
54 
56  void LoadNPLLib();
57 
59  void LoadParaLib();
60 
62  // following are individual group of HAPI
64 
66  void LoadHAPI_Globals();
68  void LoadHAPI_SceneManager();
70  void LoadHAPI_ResourceManager();
72  void LoadHAPI_UI();
74  void LoadHAPI_UI_Extension();
76  void LoadHAPI_Audio();
78  void LoadHAPI_Network();
80  void LoadHAPI_AI();
82  void LoadHAPI_NPL();
84  void LoadHAPI_Jabber();
85  public:
90  static int GetNPLCodeFromFile(ParaEngine::CParaFile* pFile, char** pBuffer, int* pBufferSize);
91 
96  static void AddSearchPath(const char* sSearchPath, bool bIsAdding = true);
97 
104  static uint32 GetScriptDiskPath(const string& filePath, string& sFileName);
105 
112  bool IsScriptFileLoaded(const string& filepath);
113 
124  bool LoadFile(const string& filePath, bool bReload, lua_State* L = 0, bool bNoReturn = false);
125 
133  int DoString(const char* sCode, int nLength = 0, const char* sFileName = NULL, bool bPopReturnValue = true);
134 
140  NPL::NPLReturnCode ActivateFile(const string& filepath, const char * code = NULL, int nLength=0);
141 
149  bool BindFileActivateFunc(const object& funcActivate, const std::string& filename);
150 
152  static NPL::NPLRuntimeState_ptr GetRuntimeStateFromLuaObject(const object& obj);
153  static NPL::NPLRuntimeState_ptr GetRuntimeStateFromLuaState(lua_State* L);
154 
156  const string& GetFileName();
160  const char* GetCurrentFileName(lua_State* L = 0);
161 
167  bool CreateSetState(lua_State* pLuaState = NULL);
168 
171  void SetOwnLuaState(bool bOwn);
172 
174  int NPL_export(lua_State* L = 0);
175 
180  const std::map <std::string, int32>& GetLoadedFiles();
181  protected:
182 
186  void DestroyState();
187 
189  void LoadLuabind();
190 
192  void SetRuntimeState(NPL::NPLRuntimeState_ptr runtime_state);
193 
199  void ProcessResult(int nResult, lua_State* L = 0);
200 
204  int CacheFileModule(const std::string& filename, int nResult, lua_State* L = 0);
205 
209  int PopFileModule(const std::string& filename, lua_State* L = 0);
210 
212  std::string GetModuleFilePath(const std::string& modulename, lua_State* L = 0);
213 
216  int GetFileLoadStatus(const string& filepath);
217  void SetFileLoadStatus(const string& filepath, int nStatus);
218 
219  private:
221  class CFileNameStack
222  {
223  public:
224  CFileNameStack(CNPLScriptingState* pState, const string& filename):m_pState(pState){
225  if(m_pState)
226  m_pState->PushFileName(filename);
227  }
228  ~CFileNameStack(){
229  if(m_pState)
230  m_pState->PopFileName();
231  }
232  CNPLScriptingState* m_pState;
233  };
234 
237  void PushFileName(const string& filename);
238 
241  void PopFileName();
242 
243 
244  void LoadParaScene();
245  void LoadParaWorld();
246 
247  lua_State* m_pState;
248  /* whether we own the luastate. true by default. false if luastate is set externally.*/
249  bool m_bOwnLuaState;
250 
252  int m_nStackSize;
253 
255  int m_nLastReturnValue;
256 
263  NPL_MemAllocatorType m_nMemAllocatorType;
264 
266  union
267  {
268  NPL::CNPLStateMemAllocator* m_pMemAlloc;
269  void* m_mspace;
270  };
271 
275  std::map <std::string, int32> m_loaded_files;
276 
278  std::stack <std::string> m_stack_current_file;
279 
280  /* currently only a single search path is supported. */
281  static std::string m_searchpath;
282  };
283 }
NPL_MemAllocatorType
NPL runs in its own thread.
Definition: NPLScriptingState.h:27
define this to enable debugging of NPL code in visual studio
Definition: INPL.h:9
Definition: class.hpp:124
a NPL scripting state (wrapper of lua State), for binding c++ classes to lua.
Definition: NPLScriptingState.h:23
memory allocator for NPL runtime state.
Definition: NPLStateMemAllocator.h:32
Definition: minilua.c:461
Definition: PEtypes.h:507
it presents a real or virtual file in ParaEngine.
Definition: ParaFile.h:31
Definition: inftrees.h:24
for luabind, The main drawback of this approach is that the compilation time will increase for the fi...
Definition: luaSQLite.cpp:1971