axmol/cocos/scripting/lua-bindings/manual/CCLuaStack.h

368 lines
13 KiB
C
Raw Normal View History

2014-03-10 14:04:58 +08:00
/****************************************************************************
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CC_LUA_STACK_H_
#define __CC_LUA_STACK_H_
extern "C" {
#include "lua.h"
}
#include "cocos2d.h"
#include "CCLuaValue.h"
2015-03-26 22:07:44 +08:00
/**
* @addtogroup lua
* @{
*/
2014-03-10 14:04:58 +08:00
NS_CC_BEGIN
2015-03-19 16:45:53 +08:00
/**
* LuaStack is used to manager the operation on the lua_State,eg., push data onto the lua_State, execute the function depended on the lua_State.
* In the current mechanism, there is only one lua_State in one LuaStack object.
2015-03-27 15:27:08 +08:00
*
* @lua NA
* @js NA
2015-03-19 16:45:53 +08:00
*/
2014-03-10 14:04:58 +08:00
class LuaStack : public Ref
{
public:
2015-03-19 16:45:53 +08:00
/**
* Create a LuaStack object, it will new a lua_State.
*/
2014-03-10 14:04:58 +08:00
static LuaStack *create(void);
2015-03-19 16:45:53 +08:00
/**
2015-03-26 22:07:44 +08:00
* Create a LuaStack object with the existed lua_State.
2015-03-19 16:45:53 +08:00
*/
2014-03-10 14:04:58 +08:00
static LuaStack *attach(lua_State *L);
2015-03-19 16:45:53 +08:00
/** Destructor. */
2014-03-10 14:04:58 +08:00
virtual ~LuaStack();
/**
2015-03-19 16:45:53 +08:00
* Method used to get a pointer to the lua_State that the script module is attached to.
2015-03-26 22:07:44 +08:00
*
2015-03-19 16:45:53 +08:00
* @return A pointer to the lua_State that the script module is attached to.
2014-03-10 14:04:58 +08:00
*/
lua_State* getLuaState(void) {
return _state;
}
/**
2015-03-19 16:45:53 +08:00
* Add a path to find lua files in.
2015-03-26 22:07:44 +08:00
*
2015-03-19 16:45:53 +08:00
* @param path to be added to the Lua search path.
2014-03-10 14:04:58 +08:00
*/
virtual void addSearchPath(const char* path);
/**
2015-03-19 16:45:53 +08:00
* Add lua loader.
*
* @param func a function pointer point to the loader function.
2014-03-10 14:04:58 +08:00
*/
virtual void addLuaLoader(lua_CFunction func);
/**
2015-03-19 16:45:53 +08:00
* Reload script code corresponding to moduleFileName.
* If value of package["loaded"][moduleFileName] is existed, it would set the vaule nil.Then,it calls executeString function.
*
* @param moduleFileName String object holding the filename of the script file that is to be executed.
* @return 0 if the string is excuted correctly or other if the string is excuted wrongly.
2014-03-10 14:04:58 +08:00
*/
virtual int reload(const char* moduleFileName);
/**
2015-03-19 16:45:53 +08:00
* Remove the related reference about the Ref object stored in the Lua table by set the value of corresponding key nil:
* The related Lua tables are toluafix_refid_ptr_mapping,toluafix_refid_type_mapping,tolua_value_root and object_Metatable["tolua_ubox"] or tolua_ubox.
2015-10-14 11:42:51 +08:00
* Meanwhile set the corresponding userdata nullptr and remove the all the lua function reference corresponding to this object.
2015-03-19 16:45:53 +08:00
*
* In current mechanism, this function is called in the destructor of Ref object, developer don't call this functions.
*
* @param object the key object to remove script object.
2014-03-10 14:04:58 +08:00
*/
virtual void removeScriptObjectByObject(Ref* object);
/**
2015-03-19 16:45:53 +08:00
* Remove Lua function reference by nHandler by setting toluafix_refid_function_mapping[nHandle] nil.
*
2015-10-14 11:42:51 +08:00
* @param nHandler the function reference index to find the correspoinding Lua function pointer.
2014-03-10 14:04:58 +08:00
*/
virtual void removeScriptHandler(int nHandler);
/**
2015-10-14 11:42:51 +08:00
* Reallocate Lua function reference index to the Lua function pointer to add reference.
2015-03-19 16:45:53 +08:00
*
2015-10-14 11:42:51 +08:00
* @param nHandler the function reference index to find the correspoinding Lua function pointer.
2014-03-10 14:04:58 +08:00
*/
virtual int reallocateScriptHandler(int nHandler);
/**
2015-03-19 16:45:53 +08:00
* Execute script code contained in the given string.
*
* @param codes holding the valid script code that should be executed.
2015-10-14 11:42:51 +08:00
* @return 0 if the string is executed correctly, other if the string is executed wrongly.
2014-03-10 14:04:58 +08:00
*/
virtual int executeString(const char* codes);
/**
2015-03-19 16:45:53 +08:00
* Execute a script file.
*
* @param filename String object holding the filename of the script file that is to be executed.
* @return the return values by calling executeFunction.
2014-03-10 14:04:58 +08:00
*/
virtual int executeScriptFile(const char* filename);
/**
2015-03-19 16:45:53 +08:00
* Execute a scripted global function.
* The function should not take any parameters and should return an integer.
*
* @param functionName String object holding the name of the function, in the global script environment, that is to be executed.
* @return The integer value returned from the script function.
2014-03-10 14:04:58 +08:00
*/
virtual int executeGlobalFunction(const char* functionName);
2015-03-19 16:45:53 +08:00
/**
* Set the stack top index 0.
*/
2014-03-10 14:04:58 +08:00
virtual void clean(void);
2015-03-19 16:45:53 +08:00
/**
* Pushes a integer number with value intVaule onto the stack.
*
* @param intValue a integer number.
*/
2014-03-10 14:04:58 +08:00
virtual void pushInt(int intValue);
2015-03-19 16:45:53 +08:00
/**
* Pushes a float number with value floatValue onto the stack.
*
* @param floatValue a float number.
*/
2014-03-10 14:04:58 +08:00
virtual void pushFloat(float floatValue);
2015-03-19 16:45:53 +08:00
/**
* Pushes a long number with value longValue onto the stack.
*
* @param longValue a long number.
*/
2014-03-10 14:04:58 +08:00
virtual void pushLong(long longValue);
2015-03-19 16:45:53 +08:00
/**
* Pushes a bool value with boolValue onto the stack.
*
* @param boolValue a bool value.
*/
2014-03-10 14:04:58 +08:00
virtual void pushBoolean(bool boolValue);
2015-03-19 16:45:53 +08:00
/**
* Pushes the zero-terminated string pointed to by stringValue onto the stack.
*
* @param stringValue a pointer point to a zero-terminated string stringValue.
*/
2014-03-10 14:04:58 +08:00
virtual void pushString(const char* stringValue);
2015-03-19 16:45:53 +08:00
/**
* Pushes the string pointed to by stringValue with size length onto the stack.
*
* @param stringValue a pointer point to the string stringValue.
* @param length the size.
*/
2014-03-10 14:04:58 +08:00
virtual void pushString(const char* stringValue, int length);
2015-03-19 16:45:53 +08:00
/**
* Pushes a nil value onto the stack.
*/
2014-03-10 14:04:58 +08:00
virtual void pushNil(void);
2015-03-19 16:45:53 +08:00
/**
* Pushes a Ref object onto the stack.
*
* @see toluafix_pushusertype_ccobject.
*/
2014-03-10 14:04:58 +08:00
virtual void pushObject(Ref* objectValue, const char* typeName);
2015-03-19 16:45:53 +08:00
/**
* According to the type of LuaValue, it would called the other push function in the internal.
* type function
* LuaValueTypeInt pushInt
* LuaValueTypeFloat pushFloat
* LuaValueTypeBoolean pushBoolean
* LuaValueTypeString pushString
* LuaValueTypeDict pushLuaValueDict
* LuaValueTypeArray pushLuaValueArray
* LuaValueTypeObject pushObject
*
* @param value a LuaValue object.
*/
2014-03-10 14:04:58 +08:00
virtual void pushLuaValue(const LuaValue& value);
2015-03-19 16:45:53 +08:00
/**
* Pushes a lua table onto the stack.
* The key of table is the key of LuaValueDict which is std::map.
* The value of table is according to the the type of LuaValue of LuaValueDict by calling pushLuaValue,@see pushLuaValue.
*
2015-03-26 22:07:44 +08:00
* @param dict a LuaValueDict object.
2015-03-19 16:45:53 +08:00
*/
2014-03-10 14:04:58 +08:00
virtual void pushLuaValueDict(const LuaValueDict& dict);
2015-03-19 16:45:53 +08:00
/**
* Pushes a lua array table onto the stack.
* The index of array table is begin at 1.
* The value of array table is according to the the type of LuaValue of LuaValueDict by calling pushLuaValue,@see pushLuaValue.
*/
virtual void pushLuaValueArray(const LuaValueArray& array);
/**
* Get the lua function pointer from toluafix_refid_function_mapping table by giving nHanlder.
2015-03-26 22:07:44 +08:00
* If the lua function pointer corresponding to the nHanlder isn't found, it would push nil on the top index of stack, then it would output the error log in the debug model.
2015-03-19 16:45:53 +08:00
*
* @return true if get the no-null function pointer otherwise false.
*/
2014-03-10 14:04:58 +08:00
virtual bool pushFunctionByHandler(int nHandler);
2015-03-19 16:45:53 +08:00
/**
* Execute the lua function on the -(numArgs + 1) index on the stack by the numArgs variables passed.
*
* @param numArgs the number of variables.
* @return 0 if it happen the error or it hasn't return value, otherwise it return the value by calling the lua function.
*/
2014-03-10 14:04:58 +08:00
virtual int executeFunction(int numArgs);
2015-03-19 16:45:53 +08:00
/**
* Execute the lua function corresponding to the nHandler by the numArgs variables passed.
*
* @param nHandler the index count corresponding to the lua function.
* @param numArgs the number of variables.
2015-03-26 22:07:44 +08:00
* @return the return value is the same as executeFunction,please @see executeFunction.
2015-03-19 16:45:53 +08:00
*/
2014-03-10 14:04:58 +08:00
virtual int executeFunctionByHandler(int nHandler, int numArgs);
2015-03-19 16:45:53 +08:00
/**
* Execute the lua function corresponding to the handler by the numArgs variables passed.
* By calling this function, the number of return value is numResults(may be > 1).
* All the return values are stored in the resultArray.
*
2015-03-27 11:13:28 +08:00
* @param handler the index count corresponding to the lua function.
2015-03-19 16:45:53 +08:00
* @param numArgs the number of variables.
* @param numResults the number of return value.
* @param resultArray a array used to store the return value.
* @return 0 if it happen error or it hasn't return value, otherwise return 1.
*/
2014-03-11 17:35:09 +08:00
virtual int executeFunctionReturnArray(int handler,int numArgs,int numResults,__Array& resultArray);
2015-03-19 16:45:53 +08:00
/**
* Execute the lua function corresponding to the handler by the numArgs variables passed.
* By calling this function, the number of return value is numResults(may be > 1).
* All the return values are used in the callback func.
*
* @param handler the index count corresponding to the lua function.
* @param numArgs the number of variables.
* @param numResults the number of return value.
* @param func callback function which is called if the numResults > 0.
* @return 0 if it happen error or it hasn't return value, otherwise return 1.
*/
2014-03-10 14:04:58 +08:00
virtual int executeFunction(int handler, int numArgs, int numResults, const std::function<void(lua_State*,int)>& func);
2015-03-19 16:45:53 +08:00
/**
* Handle the assert message.
*
* @return return true if current _callFromLua of LuaStack is not equal to 0 otherwise return false.
*/
2014-03-10 14:04:58 +08:00
virtual bool handleAssert(const char *msg);
2015-03-19 16:45:53 +08:00
/**
* Set the key and sign for xxtea encryption algorithm.
*
* @param key a string pointer
* @param keyLen the length of key
* @param sign a string sign
* @param signLen the length of sign
*/
virtual void setXXTEAKeyAndSign(const char *key, int keyLen, const char *sign, int signLen);
2015-03-19 16:45:53 +08:00
/**
* free the key and sign for xxtea encryption algorithm.
*/
virtual void cleanupXXTEAKeyAndSign();
2015-03-19 16:45:53 +08:00
/**
* Loads a buffer as a Lua chunk.This function uses lua_load to load the Lua chunk in the buffer pointed to by chunk with size chunkSize.
* If it supports xxtea encryption algorithm, the chunk and the chunkSize would be processed by calling xxtea_decrypt to the real buffer and buffer size.
*
* @param L the current lua_State.
* @param chunk the buffer pointer.
2015-03-27 11:13:28 +08:00
* @param chunkSize the size of buffer.
2015-03-19 16:45:53 +08:00
* @param chunkName the name of chunk pointer.
* @return 0, LUA_ERRSYNTAX or LUA_ERRMEM:.
*/
int luaLoadBuffer(lua_State *L, const char *chunk, int chunkSize, const char *chunkName);
2015-03-19 16:45:53 +08:00
/**
* Load the Lua chunks from the zip file
*
* @param zipFilePath file path to zip file.
2015-10-14 11:42:51 +08:00
* @return 1 if load successfully otherwise 0.
2015-03-19 16:45:53 +08:00
*/
2014-10-08 17:30:14 +08:00
int loadChunksFromZIP(const char *zipFilePath);
2015-03-19 16:45:53 +08:00
/**
* Load the Lua chunks from current lua_State.
*
2015-03-27 11:13:28 +08:00
* @param L the current lua_State.
2015-10-14 11:42:51 +08:00
* @return 1 if load successfully otherwise 0.
2015-03-19 16:45:53 +08:00
*/
2014-10-08 17:30:14 +08:00
int luaLoadChunksFromZIP(lua_State *L);
2014-03-10 14:04:58 +08:00
protected:
LuaStack(void)
: _state(nullptr)
2014-03-10 14:04:58 +08:00
, _callFromLua(0)
, _xxteaEnabled(false)
, _xxteaKey(nullptr)
, _xxteaKeyLen(0)
, _xxteaSign(nullptr)
, _xxteaSignLen(0)
2014-03-10 14:04:58 +08:00
{
}
bool init(void);
bool initWithLuaState(lua_State *L);
lua_State *_state;
int _callFromLua;
bool _xxteaEnabled;
char* _xxteaKey;
int _xxteaKeyLen;
char* _xxteaSign;
int _xxteaSignLen;
2014-03-10 14:04:58 +08:00
};
NS_CC_END
2015-03-26 22:07:44 +08:00
// end group
/// @}
2014-03-10 14:04:58 +08:00
#endif // __CC_LUA_STACK_H_