My Project
|
a NPL scripting state (wrapper of lua State), for binding c++ classes to lua. More...
#include <NPLScriptingState.h>
Public Types | |
enum | NPL_MemAllocatorType { MEM_ALLOC_TYPE_SYS_MALLOC, MEM_ALLOC_TYPE_POOL_MALLOC, MEM_ALLOC_TYPE_DL_MALLOC } |
NPL runs in its own thread. More... | |
Public Member Functions | |
CNPLScriptingState (bool bCreateState=true) | |
split registration to save compiling time. More... | |
int | GetLastReturnValue () const |
get the last return value from lua_pcall. More... | |
void | SetLastReturnValue (int val) |
lua_State * | GetLuaState () |
get the lua state. | |
bool | IsValid () |
return true if the runtime state is valid | |
void | LoadNPLLib () |
load only NPL related functions. More... | |
void | LoadParaLib () |
load only Para related functions. More... | |
void | LoadHAPI_Globals () |
load functions for scene management | |
void | LoadHAPI_SceneManager () |
load functions for scene management | |
void | LoadHAPI_ResourceManager () |
load functions for resource or assets management | |
void | LoadHAPI_UI () |
load functions for GUI controls and sounds | |
void | LoadHAPI_UI_Extension () |
this function is called by LoadHAPI_UI() | |
void | LoadHAPI_Audio () |
load functions for Audio Engine More... | |
void | LoadHAPI_Network () |
load functions for NPL Network Layer | |
void | LoadHAPI_AI () |
load functions for AI | |
void | LoadHAPI_NPL () |
load functions for neural parallel language | |
void | LoadHAPI_Jabber () |
load jabber related functions | |
bool | IsScriptFileLoaded (const string &filepath) |
whether a given script file is loaded. More... | |
bool | LoadFile (const string &filePath, bool bReload, lua_State *L=0, bool bNoReturn=false) |
load a new NPL script file without running it. More... | |
int | DoString (const char *sCode, int nLength=0, const char *sFileName=NULL, bool bPopReturnValue=true) |
do string in the current state. More... | |
NPL::NPLReturnCode | ActivateFile (const string &filepath, const char *code=NULL, int nLength=0) |
Activate a local file. More... | |
bool | BindFileActivateFunc (const object &funcActivate, const std::string &filename) |
bind the activation function. More... | |
const string & | GetFileName () |
get current file name which is being processed now. More... | |
const char * | GetCurrentFileName (lua_State *L=0) |
get current file that is being loaded or where the current code is defined. More... | |
bool | CreateSetState (lua_State *pLuaState=NULL) |
load or restore the runtime state no need to call LoadLuabind(), if the script runtime is created by this function More... | |
void | SetOwnLuaState (bool bOwn) |
if true, we will delete the luastate when this class is destroyed. More... | |
int | NPL_export (lua_State *L=0) |
set/get exported file module. More... | |
const std::map< std::string, int32 > & | GetLoadedFiles () |
NOT thread-safe: all loaded files mapping from filename to number of cached objects. More... | |
Static Public Member Functions | |
static int | GetNPLCodeFromFile (ParaEngine::CParaFile *pFile, char **pBuffer, int *pBufferSize) |
get the NPL code buffer and size according to a given file. More... | |
static void | AddSearchPath (const char *sSearchPath, bool bIsAdding=true) |
static function: More... | |
static uint32 | GetScriptDiskPath (const string &filePath, string &sFileName) |
we will first find if there is an up-to-date compiled version in the script/bin directory. More... | |
static NPL::NPLRuntimeState_ptr | GetRuntimeStateFromLuaObject (const object &obj) |
get pointer to NPLRuntimeState from the lua state object. More... | |
static NPL::NPLRuntimeState_ptr | GetRuntimeStateFromLuaState (lua_State *L) |
Protected Member Functions | |
void | DestroyState () |
destroy the runtime state careful with this function, it will make the state invalid. | |
void | LoadLuabind () |
the function must be called for the lua state before binding anything to it | |
void | SetRuntimeState (NPL::NPLRuntimeState_ptr runtime_state) |
set the current runtime state. More... | |
void | ProcessResult (int nResult, lua_State *L=0) |
Process return result after calling a function or loading a file in Lua. More... | |
int | CacheFileModule (const std::string &filename, int nResult, lua_State *L=0) |
save nResult objects on stack to file modules More... | |
int | PopFileModule (const std::string &filename, lua_State *L=0) |
pop file module to stack for a given file. More... | |
std::string | GetModuleFilePath (const std::string &modulename, lua_State *L=0) |
get module file path by module name | |
int | GetFileLoadStatus (const string &filepath) |
wrapping the m_loaded_files | |
void | SetFileLoadStatus (const string &filepath, int nStatus) |
a NPL scripting state (wrapper of lua State), for binding c++ classes to lua.
Then call Load*() functions to load ParaEngine classes and APIs to the specified runtime, so that scripts in the runtime can use these classes and functions
NPL runs in its own thread.
and constantly reallocate 32, 64, 128, 256, 512 sized objects.
ParaScripting::CNPLScriptingState::CNPLScriptingState | ( | bool | bCreateState = true | ) |
split registration to save compiling time.
bCreateState | if false, no interface state is created. this is usually the case when we wants to create an empty scripting state for DLL runtime state. |
void register_part1(class_<X>& x){ x.def(...); } void register_part2(class_<X>& x){ x.def(...); } void register_(lua_State* L){ class_<X> x("x"); register_part1(x); register_part2(x); module(L) [ x ]; }
NPL::NPLReturnCode ParaScripting::CNPLScriptingState::ActivateFile | ( | const string & | filepath, |
const char * | code = NULL , |
||
int | nLength = 0 |
||
) |
Activate a local file.
The file should be loaded already.
filepath | pointer to the file path. |
the activation function is stored in __glia global variable, indexed by the the file name. There activation function may be nil.
get the "__act" global table in the runtime
get activation function of the file name
|
static |
static function:
sSearchPath | for example "src/" in android, etc. |
bIsAdding | default to true. |
bool ParaScripting::CNPLScriptingState::BindFileActivateFunc | ( | const object & | funcActivate, |
const std::string & | filename | ||
) |
bind the activation function.
Usually, it is for the script function NPL.this(funcActivate).
nPreemptiveInstructionCount | optional parameter. if omitted, the activate function will run non-preemptive (it is the programmer's job to let the function finish in short time). If a number is specified here, the activate function will be preemptive like in Erlang. When this number of instructions are executed, the activate function will be paused. |
create the activate table, if it does not exist
add the current file name to the __activate table
|
protected |
save nResult objects on stack to file modules
bool ParaScripting::CNPLScriptingState::CreateSetState | ( | lua_State * | pLuaState = NULL | ) |
load or restore the runtime state no need to call LoadLuabind(), if the script runtime is created by this function
pLuaState | default to NULL, if not, it will be an external luastate (that we do not own, calling SetOwnLuaState(false) internally) |
make this a reasonable size
int ParaScripting::CNPLScriptingState::DoString | ( | const char * | sCode, |
int | nLength = 0 , |
||
const char * | sFileName = NULL , |
||
bool | bPopReturnValue = true |
||
) |
do string in the current state.
This function is usually called from the scripting interface.
sCode | the string to executed. |
nLength | length in bytes.if this is 0, length will be calculated, but must be smaller than a predefined safe length. If this is positive. any string length is allowed. |
sFileName | NULL or a file name string that is associated with the code chunk. This is mostly used for debugging. |
bPopReturnValue | default to true, whether to pop any return values from the sCode. default to true. |
do sCall as a string Assume caller is authorized, since it is from the local environment. This may lack security, and is only valid for authorized callers.
Load and execute the a buffer of code in protected mode ( lua_pcall() )
Output messages through log interface
const char * ParaScripting::CNPLScriptingState::GetCurrentFileName | ( | lua_State * | L = 0 | ) |
get current file that is being loaded or where the current code is defined.
L | just in case the NPL.load is called from a different coroutine thread, which is different lua_state(stack) than the default one. |
const string & ParaScripting::CNPLScriptingState::GetFileName | ( | ) |
get current file name which is being processed now.
int ParaScripting::CNPLScriptingState::GetLastReturnValue | ( | ) | const |
get the last return value from lua_pcall.
const std::map< std::string, int32 > & ParaScripting::CNPLScriptingState::GetLoadedFiles | ( | ) |
NOT thread-safe: all loaded files mapping from filename to number of cached objects.
If -1, it means that file is being loaded or something went wrong. 0 means no cached object.
|
static |
get the NPL code buffer and size according to a given file.
Please note that BOM is read from the beginning of the file. if it is a utf8 encoded file with BOM byte mask, only the data section is returned
|
static |
get pointer to NPLRuntimeState from the lua state object.
it just retrieves from a secret lua_status variable.
|
static |
we will first find if there is an up-to-date compiled version in the script/bin directory.
if there is, we will load the compiled version, otherwise we will use the text version.
filePath | the logic file path |
sFileName | the output file path. return uint32 of FileLocation enumeration. 0 if not found. |
bool ParaScripting::CNPLScriptingState::IsScriptFileLoaded | ( | const string & | filepath | ) |
whether a given script file is loaded.
filePath | the local file path in the following format: [dir0]/[dir1]/[dir2]/[filename.lua] |
bool ParaScripting::CNPLScriptingState::LoadFile | ( | const string & | filePath, |
bool | bReload, | ||
lua_State * | L = 0 , |
||
bool | bNoReturn = false |
||
) |
load a new NPL script file without running it.
If the file is already loaded, it will not be loaded again.
filePath | the local NPL script file path |
bReload | if true, the file will be reloaded even if it is already loaded. otherwise, the file will only be loaded if it is not loaded yet. |
L | just in case the NPL.load is called from a different coroutine thread, which is different lua_state(stack) than the default one. |
bNoReturn | generate no return on lua_state's stack. |
for standard lua file, Load and execute the a buffer of code in protected mode ( lua_pcall() ) Output messages through log interface
void CNPLScriptingState::LoadHAPI_Audio | ( | ) |
load functions for Audio Engine
for luabind, The main drawback of this approach is that the compilation time will increase for the file that does the registration, it is therefore recommended that you register everything in the same cpp-file.
void ParaScripting::CNPLScriptingState::LoadParaLib | ( | ) |
load only Para related functions.
load all Para libs
int ParaScripting::CNPLScriptingState::NPL_export | ( | lua_State * | L = 0 | ) |
set/get exported file module.
|
protected |
pop file module to stack for a given file.
Return true, if file is loaded before or false if not.
|
protected |
Process return result after calling a function or loading a file in Lua.
nResult | return result returned by luaL_loadbuffer() or lua_pcall(). |
L | just in case the NPL.load is called from a different coroutine thread, which is different lua_state(stack) than the default one. |
report error to log;
void ParaScripting::CNPLScriptingState::SetOwnLuaState | ( | bool | bOwn | ) |
if true, we will delete the luastate when this class is destroyed.
This function should rarely be called.
|
protected |
set the current runtime state.
this function should be called from the parent class immediately after LoadNPLLib().