2012-02-01 16:55:46 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2011 cocos2d-x.org
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
http://www.cocos2d-x.org
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
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:
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCLuaEngine.h"
|
|
|
|
#include "tolua++.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "lualib.h"
|
|
|
|
#include "lauxlib.h"
|
2012-03-07 20:27:26 +08:00
|
|
|
#include "tolua_fix.h"
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2012-02-02 19:13:50 +08:00
|
|
|
#include "cocos2d.h"
|
2012-02-01 16:55:46 +08:00
|
|
|
#include "LuaCocos2d.h"
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "cocoa/CCArray.h"
|
2012-02-02 15:58:10 +08:00
|
|
|
#include "CCScheduler.h"
|
2012-02-01 16:55:46 +08:00
|
|
|
|
2012-02-07 15:16:15 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
#include "Cocos2dxLuaLoader.h"
|
|
|
|
#endif
|
|
|
|
|
2012-02-09 14:07:11 +08:00
|
|
|
NS_CC_BEGIN
|
2012-02-01 16:55:46 +08:00
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
// #pragma mark -
|
2012-09-11 15:24:25 +08:00
|
|
|
// #pragma mark CCLuaValue
|
2012-09-11 14:02:33 +08:00
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
const CCLuaValue CCLuaValue::intValue(const int intValue)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValue value;
|
|
|
|
value.m_type = CCLuaValueTypeInt;
|
2012-09-11 14:02:33 +08:00
|
|
|
value.m_field.intValue = intValue;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
const CCLuaValue CCLuaValue::floatValue(const float floatValue)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValue value;
|
|
|
|
value.m_type = CCLuaValueTypeFloat;
|
2012-09-11 14:02:33 +08:00
|
|
|
value.m_field.floatValue = floatValue;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
const CCLuaValue CCLuaValue::booleanValue(const bool booleanValue)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValue value;
|
|
|
|
value.m_type = CCLuaValueTypeBoolean;
|
2012-09-11 14:02:33 +08:00
|
|
|
value.m_field.booleanValue = booleanValue;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
const CCLuaValue CCLuaValue::stringValue(const char* stringValue)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValue value;
|
|
|
|
value.m_type = CCLuaValueTypeString;
|
2012-09-11 14:02:33 +08:00
|
|
|
value.m_field.stringValue = new std::string(stringValue);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
const CCLuaValue CCLuaValue::stringValue(const std::string& stringValue)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValue value;
|
|
|
|
value.m_type = CCLuaValueTypeString;
|
2012-09-11 14:02:33 +08:00
|
|
|
value.m_field.stringValue = new std::string(stringValue);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
const CCLuaValue CCLuaValue::dictValue(const CCLuaValueDict& dictValue)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValue value;
|
|
|
|
value.m_type = CCLuaValueTypeDict;
|
|
|
|
value.m_field.dictValue = new CCLuaValueDict(dictValue);
|
2012-09-11 14:02:33 +08:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
const CCLuaValue CCLuaValue::arrayValue(const CCLuaValueArray& arrayValue)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValue value;
|
|
|
|
value.m_type = CCLuaValueTypeArray;
|
|
|
|
value.m_field.arrayValue = new CCLuaValueArray(arrayValue);
|
2012-09-11 14:02:33 +08:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
const CCLuaValue CCLuaValue::ccobjectValue(CCObject* ccobjectValue, const char* objectTypename)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValue value;
|
|
|
|
value.m_type = CCLuaValueTypeCCObject;
|
2012-09-11 14:02:33 +08:00
|
|
|
value.m_field.ccobjectValue = ccobjectValue;
|
|
|
|
ccobjectValue->retain();
|
|
|
|
value.m_ccobjectType = new std::string(objectTypename);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
const CCLuaValue CCLuaValue::ccobjectValue(CCObject* ccobjectValue, const std::string& objectTypename)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
return CCLuaValue::ccobjectValue(ccobjectValue, objectTypename.c_str());
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValue::CCLuaValue(const CCLuaValue& rhs)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
|
|
|
copy(rhs);
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValue& CCLuaValue::operator=(const CCLuaValue& rhs)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
|
|
|
if (this != &rhs) copy(rhs);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValue::~CCLuaValue(void)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
if (m_type == CCLuaValueTypeString)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
|
|
|
delete m_field.stringValue;
|
|
|
|
}
|
2012-09-11 15:24:25 +08:00
|
|
|
else if (m_type == CCLuaValueTypeDict)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
|
|
|
delete m_field.dictValue;
|
|
|
|
}
|
2012-09-11 15:24:25 +08:00
|
|
|
else if (m_type == CCLuaValueTypeArray)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
|
|
|
delete m_field.arrayValue;
|
|
|
|
}
|
2012-09-11 15:24:25 +08:00
|
|
|
else if (m_type == CCLuaValueTypeCCObject)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
|
|
|
m_field.ccobjectValue->release();
|
|
|
|
delete m_ccobjectType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
void CCLuaValue::copy(const CCLuaValue& rhs)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
|
|
|
memcpy(&m_field, &rhs.m_field, sizeof(m_field));
|
|
|
|
m_type = rhs.m_type;
|
2012-09-11 15:24:25 +08:00
|
|
|
if (m_type == CCLuaValueTypeString)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
|
|
|
m_field.stringValue = new std::string(*rhs.m_field.stringValue);
|
|
|
|
}
|
2012-09-11 15:24:25 +08:00
|
|
|
else if (m_type == CCLuaValueTypeDict)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
m_field.dictValue = new CCLuaValueDict(*rhs.m_field.dictValue);
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
2012-09-11 15:24:25 +08:00
|
|
|
else if (m_type == CCLuaValueTypeArray)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
m_field.arrayValue = new CCLuaValueArray(*rhs.m_field.arrayValue);
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
2012-09-11 15:24:25 +08:00
|
|
|
else if (m_type == CCLuaValueTypeCCObject)
|
2012-09-11 14:02:33 +08:00
|
|
|
{
|
|
|
|
m_field.ccobjectValue = rhs.m_field.ccobjectValue;
|
|
|
|
m_field.ccobjectValue->retain();
|
|
|
|
m_ccobjectType = new std::string(*rhs.m_ccobjectType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark CCLuaEngine
|
|
|
|
|
|
|
|
CCLuaEngine* CCLuaEngine::m_defaultEngine = NULL;
|
|
|
|
|
|
|
|
CCLuaEngine* CCLuaEngine::defaultEngine(void)
|
|
|
|
{
|
|
|
|
if (!m_defaultEngine)
|
|
|
|
{
|
|
|
|
m_defaultEngine = CCLuaEngine::create();
|
|
|
|
}
|
|
|
|
return m_defaultEngine;
|
|
|
|
}
|
|
|
|
|
2012-08-25 17:08:33 +08:00
|
|
|
CCLuaEngine* CCLuaEngine::create(void)
|
|
|
|
{
|
|
|
|
CCLuaEngine* pEngine = new CCLuaEngine();
|
|
|
|
pEngine->init();
|
|
|
|
return pEngine;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLuaEngine::~CCLuaEngine(void)
|
2012-02-07 11:43:29 +08:00
|
|
|
{
|
2012-02-09 14:07:11 +08:00
|
|
|
lua_close(m_state);
|
2012-09-11 15:24:25 +08:00
|
|
|
if (this == m_defaultEngine)
|
|
|
|
{
|
|
|
|
m_defaultEngine = NULL;
|
|
|
|
}
|
2012-02-07 11:43:29 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 14:07:11 +08:00
|
|
|
bool CCLuaEngine::init(void)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
|
|
|
m_state = lua_open();
|
|
|
|
luaL_openlibs(m_state);
|
2012-08-28 15:54:19 +08:00
|
|
|
tolua_Cocos2d_open(m_state);
|
2012-08-25 15:05:59 +08:00
|
|
|
toluafix_open(m_state);
|
2012-02-07 15:16:15 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
addLuaLoader(loader_Android);
|
|
|
|
#endif
|
2012-02-09 14:07:11 +08:00
|
|
|
return true;
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
void CCLuaEngine::removeScriptObjectByCCObject(CCObject* pObj)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2012-09-11 14:02:33 +08:00
|
|
|
toluafix_remove_ccobject_by_refid(m_state, pObj->m_nLuaID);
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
void CCLuaEngine::removeScriptHandler(int nHandler)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2012-08-25 15:05:59 +08:00
|
|
|
toluafix_remove_function_by_refid(m_state, nHandler);
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCLuaEngine::addSearchPath(const char* path)
|
|
|
|
{
|
2012-09-02 00:38:57 +08:00
|
|
|
lua_getglobal(m_state, "package"); /* stack: package */
|
|
|
|
lua_getfield(m_state, -1, "path"); /* get package.path, stack: package path */
|
2012-02-01 16:55:46 +08:00
|
|
|
const char* cur_path = lua_tostring(m_state, -1);
|
2012-09-02 00:38:57 +08:00
|
|
|
lua_pop(m_state, 1); /* stack: package */
|
|
|
|
lua_pushfstring(m_state, "%s;%s/?.lua", cur_path, path); /* stack: package newpath */
|
|
|
|
lua_setfield(m_state, -2, "path"); /* package.path = newpath, stack: package */
|
|
|
|
lua_pop(m_state, 1); /* stack: - */
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2012-02-02 19:00:43 +08:00
|
|
|
int CCLuaEngine::executeString(const char *codes)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
int nRet = luaL_dostring(m_state, codes);
|
|
|
|
lua_gc(m_state, LUA_GCCOLLECT, 0);
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-02 19:00:43 +08:00
|
|
|
if (nRet != 0)
|
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] %s", lua_tostring(m_state, -1));
|
|
|
|
lua_pop(m_state, 1);
|
|
|
|
return nRet;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
int CCLuaEngine::executeScriptFile(const char* filename)
|
|
|
|
{
|
|
|
|
int nRet = luaL_dofile(m_state, filename);
|
2012-09-04 11:16:59 +08:00
|
|
|
// lua_gc(m_state, LUA_GCCOLLECT, 0);
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
if (nRet != 0)
|
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] %s", lua_tostring(m_state, -1));
|
|
|
|
lua_pop(m_state, 1);
|
|
|
|
return nRet;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-04 11:16:59 +08:00
|
|
|
int CCLuaEngine::executeGlobalFunction(const char* functionName)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2012-09-02 00:38:57 +08:00
|
|
|
lua_getglobal(m_state, functionName); /* query function by name, stack: function */
|
2012-02-01 16:55:46 +08:00
|
|
|
if (!lua_isfunction(m_state, -1))
|
|
|
|
{
|
2012-02-09 14:07:11 +08:00
|
|
|
CCLOG("[LUA ERROR] name '%s' does not represent a Lua function", functionName);
|
2012-02-01 16:55:46 +08:00
|
|
|
lua_pop(m_state, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-09-02 00:38:57 +08:00
|
|
|
int error = lua_pcall(m_state, 0, 1, 0); /* call function, stack: ret */
|
2012-09-04 11:16:59 +08:00
|
|
|
// lua_gc(m_state, LUA_GCCOLLECT, 0);
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] %s", lua_tostring(m_state, - 1));
|
|
|
|
lua_pop(m_state, 1); // clean error message
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
// get return value
|
|
|
|
if (!lua_isnumber(m_state, -1))
|
|
|
|
{
|
|
|
|
lua_pop(m_state, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-01 16:55:46 +08:00
|
|
|
int ret = lua_tointeger(m_state, -1);
|
2012-09-02 00:38:57 +08:00
|
|
|
lua_pop(m_state, 1); /* stack: - */
|
2012-02-01 16:55:46 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-11 14:02:33 +08:00
|
|
|
int CCLuaEngine::executeNodeEvent(CCNode* pNode, int nAction)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int nScriptHandler = pNode->getScriptHandler();
|
|
|
|
CC_BREAK_IF(0 == nScriptHandler);
|
2012-09-20 23:22:45 +08:00
|
|
|
|
|
|
|
cleanStack();
|
2012-09-11 15:24:25 +08:00
|
|
|
CCLuaValueDict dict;
|
2012-09-11 14:02:33 +08:00
|
|
|
if (nAction == kCCNodeOnEnter)
|
2012-09-11 15:24:25 +08:00
|
|
|
{
|
|
|
|
dict["name"] = CCLuaValue::stringValue("enter");
|
2012-09-11 17:13:45 +08:00
|
|
|
pushCCLuaValueDict(dict);
|
|
|
|
ret = executeFunctionByHandler(nScriptHandler, 1);
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
else if (nAction == kCCNodeOnExit)
|
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
dict["name"] = CCLuaValue::stringValue("exit");
|
2012-09-11 17:13:45 +08:00
|
|
|
pushCCLuaValueDict(dict);
|
|
|
|
ret = executeFunctionByHandler(nScriptHandler, 1);
|
2012-09-11 14:02:33 +08:00
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCLuaEngine::executeMenuItemEvent(CCMenuItem* pMenuItem)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int nScriptHandler = pMenuItem->getScriptTapHandler();
|
2012-09-11 15:24:25 +08:00
|
|
|
CC_BREAK_IF(0 == nScriptHandler);
|
2012-09-20 23:22:45 +08:00
|
|
|
|
|
|
|
cleanStack();
|
|
|
|
pushInt(pMenuItem->getTag());
|
2012-09-11 17:13:45 +08:00
|
|
|
ret = executeFunctionByHandler(nScriptHandler, 1);
|
2012-09-11 14:02:33 +08:00
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCLuaEngine::executeNotificationEvent(CCNotificationCenter* pNotificationCenter, const char* pszName)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int nScriptHandler = pNotificationCenter->getScriptHandler();
|
2012-09-11 15:24:25 +08:00
|
|
|
CC_BREAK_IF(0 == nScriptHandler);
|
2012-09-20 23:22:45 +08:00
|
|
|
|
|
|
|
cleanStack();
|
|
|
|
pushString(pszName);
|
2012-09-11 17:13:45 +08:00
|
|
|
ret = executeFunctionByHandler(nScriptHandler, 1);
|
2012-09-11 14:02:33 +08:00
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCLuaEngine::executeCallFuncActionEvent(CCCallFunc* pAction, CCObject* pTarget/* = NULL*/)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int nScriptHandler = pAction->getScriptHandler();
|
2012-09-11 15:24:25 +08:00
|
|
|
CC_BREAK_IF(0 == nScriptHandler);
|
2012-09-20 23:22:45 +08:00
|
|
|
|
|
|
|
cleanStack();
|
2012-09-11 14:02:33 +08:00
|
|
|
if (pTarget != NULL)
|
|
|
|
{
|
2012-09-20 23:22:45 +08:00
|
|
|
pushCCObject(pTarget, "CCNode");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pushNil();
|
2012-09-11 15:24:25 +08:00
|
|
|
}
|
2012-09-11 17:13:45 +08:00
|
|
|
ret = executeFunctionByHandler(nScriptHandler, 1);
|
2012-09-11 14:02:33 +08:00
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCLuaEngine::executeSchedule(CCTimer* pTimer, float dt, CCNode* pNode/* = NULL*/)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int nScriptHandler = pTimer->getScriptHandler();
|
2012-09-11 15:24:25 +08:00
|
|
|
CC_BREAK_IF(0 == nScriptHandler);
|
2012-09-20 23:22:45 +08:00
|
|
|
|
|
|
|
cleanStack();
|
|
|
|
pushFloat(dt);
|
2012-09-11 17:13:45 +08:00
|
|
|
ret = executeFunctionByHandler(nScriptHandler, 1);
|
2012-09-11 14:02:33 +08:00
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// functions for excute touch event
|
|
|
|
int CCLuaEngine::executeLayerTouchEvent(CCLayer* pLayer, int eventType, CCTouch *pTouch)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
do
|
|
|
|
{
|
2012-12-02 15:17:34 +08:00
|
|
|
CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry();
|
2012-09-11 14:02:33 +08:00
|
|
|
CC_BREAK_IF(NULL == pScriptHandlerEntry);
|
|
|
|
int nScriptHandler = pScriptHandlerEntry->getHandler();
|
|
|
|
CC_BREAK_IF(0 == nScriptHandler);
|
2012-09-20 23:22:45 +08:00
|
|
|
|
|
|
|
cleanStack();
|
2012-09-11 14:02:33 +08:00
|
|
|
CCPoint pt = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
|
|
|
|
lua_pushinteger(m_state, eventType);
|
|
|
|
lua_pushnumber(m_state, pt.x);
|
|
|
|
lua_pushnumber(m_state, pt.y);
|
|
|
|
ret = executeFunctionByHandler(nScriptHandler, 3);
|
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCLuaEngine::executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
do
|
|
|
|
{
|
2012-12-02 15:17:34 +08:00
|
|
|
CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry();
|
2012-09-11 14:02:33 +08:00
|
|
|
CC_BREAK_IF(NULL == pScriptHandlerEntry);
|
|
|
|
int nScriptHandler = pScriptHandlerEntry->getHandler();
|
|
|
|
CC_BREAK_IF(0 == nScriptHandler);
|
2012-09-20 23:22:45 +08:00
|
|
|
|
|
|
|
cleanStack();
|
2012-09-11 14:02:33 +08:00
|
|
|
lua_pushinteger(m_state, eventType);
|
|
|
|
lua_newtable(m_state);
|
|
|
|
|
|
|
|
CCDirector* pDirector = CCDirector::sharedDirector();
|
|
|
|
CCSetIterator it = pTouches->begin();
|
|
|
|
CCTouch* pTouch;
|
|
|
|
int n = 1;
|
|
|
|
while (it != pTouches->end())
|
|
|
|
{
|
|
|
|
pTouch = (CCTouch*)*it;
|
|
|
|
CCPoint pt = pDirector->convertToGL(pTouch->getLocationInView());
|
|
|
|
lua_pushnumber(m_state, pt.x);
|
|
|
|
lua_rawseti(m_state, -2, n++);
|
|
|
|
lua_pushnumber(m_state, pt.y);
|
|
|
|
lua_rawseti(m_state, -2, n++);
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = executeFunctionByHandler(nScriptHandler, 2);
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-12-02 15:17:34 +08:00
|
|
|
int CCLuaEngine::executeLayerKeypadEvent(CCLayer* pLayer, int eventType)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CCScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptKeypadHandlerEntry();
|
|
|
|
CC_BREAK_IF(NULL == pScriptHandlerEntry);
|
|
|
|
int nScriptHandler = pScriptHandlerEntry->getHandler();
|
|
|
|
CC_BREAK_IF(0 == nScriptHandler);
|
|
|
|
|
|
|
|
cleanStack();
|
|
|
|
lua_newtable(m_state);
|
|
|
|
lua_pushinteger(m_state, eventType);
|
|
|
|
ret = executeFunctionByHandler(nScriptHandler, 1);
|
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCLuaEngine::executeAccelerometerEvent(CCLayer* pLayer, CCAcceleration* pAccelerationValue)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CCScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptAccelerateHandlerEntry();
|
|
|
|
CC_BREAK_IF(NULL == pScriptHandlerEntry);
|
|
|
|
int nScriptHandler = pScriptHandlerEntry->getHandler();
|
|
|
|
CC_BREAK_IF(0 == nScriptHandler);
|
|
|
|
|
|
|
|
cleanStack();
|
|
|
|
lua_newtable(m_state);
|
|
|
|
lua_pushnumber(m_state, pAccelerationValue->x);
|
|
|
|
lua_pushnumber(m_state, pAccelerationValue->y);
|
|
|
|
lua_pushnumber(m_state, pAccelerationValue->z);
|
|
|
|
lua_pushnumber(m_state, pAccelerationValue->timestamp);
|
|
|
|
ret = executeFunctionByHandler(nScriptHandler, 4);
|
|
|
|
} while (0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-07 20:27:26 +08:00
|
|
|
int CCLuaEngine::executeFunctionByHandler(int nHandler, int numArgs)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2012-09-02 00:38:57 +08:00
|
|
|
if (pushFunction(nHandler)) /* stack: ... arg1 arg2 ... func */
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2012-03-07 20:27:26 +08:00
|
|
|
if (numArgs > 0)
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2012-03-07 20:27:26 +08:00
|
|
|
lua_insert(m_state, -(numArgs + 1)); /* stack: ... func arg1 arg2 ... */
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
2012-08-25 15:05:59 +08:00
|
|
|
|
|
|
|
int traceback = 0;
|
|
|
|
lua_getglobal(m_state, "__G__TRACKBACK__"); /* stack: ... func arg1 arg2 ... G */
|
|
|
|
if (!lua_isfunction(m_state, -1))
|
|
|
|
{
|
|
|
|
lua_pop(m_state, 1); /* stack: ... func arg1 arg2 ... */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
traceback = -(numArgs + 2);
|
|
|
|
lua_insert(m_state, traceback); /* stack: ... G func arg1 arg2 ... */
|
|
|
|
}
|
|
|
|
|
2012-03-07 20:27:26 +08:00
|
|
|
int error = 0;
|
2012-08-25 15:05:59 +08:00
|
|
|
error = lua_pcall(m_state, numArgs, 1, traceback); /* stack: ... ret */
|
2012-03-07 20:27:26 +08:00
|
|
|
if (error)
|
|
|
|
{
|
2012-08-25 15:05:59 +08:00
|
|
|
if (traceback == 0)
|
|
|
|
{
|
2012-09-02 00:38:57 +08:00
|
|
|
CCLOG("[LUA ERROR] %s", lua_tostring(m_state, - 1)); /* stack: ... error */
|
2012-08-25 15:05:59 +08:00
|
|
|
lua_pop(m_state, 1); // remove error message from stack
|
|
|
|
}
|
2012-03-07 20:27:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-08-25 15:05:59 +08:00
|
|
|
|
2012-03-07 20:27:26 +08:00
|
|
|
// get return value
|
|
|
|
int ret = 0;
|
|
|
|
if (lua_isnumber(m_state, -1))
|
|
|
|
{
|
|
|
|
ret = lua_tointeger(m_state, -1);
|
|
|
|
}
|
|
|
|
else if (lua_isboolean(m_state, -1))
|
|
|
|
{
|
|
|
|
ret = lua_toboolean(m_state, -1);
|
|
|
|
}
|
2012-08-25 15:05:59 +08:00
|
|
|
|
|
|
|
lua_pop(m_state, 1); // remove return value from stack
|
2012-03-07 20:27:26 +08:00
|
|
|
return ret;
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
2012-03-07 20:27:26 +08:00
|
|
|
else
|
2012-02-01 16:55:46 +08:00
|
|
|
{
|
2012-08-25 15:05:59 +08:00
|
|
|
lua_pop(m_state, numArgs); // remove args from stack
|
2012-02-01 16:55:46 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
int CCLuaEngine::pushInt(int data)
|
2012-03-07 20:27:26 +08:00
|
|
|
{
|
|
|
|
lua_pushinteger(m_state, data);
|
|
|
|
return lua_gettop(m_state);
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
int CCLuaEngine::pushFloat(float data)
|
2012-03-07 20:27:26 +08:00
|
|
|
{
|
|
|
|
lua_pushnumber(m_state, data);
|
|
|
|
return lua_gettop(m_state);
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
int CCLuaEngine::pushBoolean(bool data)
|
2012-03-07 20:27:26 +08:00
|
|
|
{
|
|
|
|
lua_pushboolean(m_state, data);
|
|
|
|
return lua_gettop(m_state);
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
int CCLuaEngine::pushString(const char* data)
|
2012-03-07 20:27:26 +08:00
|
|
|
{
|
2012-09-02 00:38:57 +08:00
|
|
|
lua_pushstring(m_state, data);
|
2012-03-07 20:27:26 +08:00
|
|
|
return lua_gettop(m_state);
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
int CCLuaEngine::pushString(const char* data, int length)
|
|
|
|
{
|
|
|
|
lua_pushlstring(m_state, data, length);
|
|
|
|
return lua_gettop(m_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCLuaEngine::pushNil(void)
|
|
|
|
{
|
|
|
|
lua_pushnil(m_state);
|
|
|
|
return lua_gettop(m_state);
|
|
|
|
}
|
|
|
|
|
2012-08-25 15:05:59 +08:00
|
|
|
int CCLuaEngine::pushCCObject(CCObject* pObject, const char* typeName)
|
2012-03-07 20:27:26 +08:00
|
|
|
{
|
2012-08-25 15:05:59 +08:00
|
|
|
toluafix_pushusertype_ccobject(m_state, pObject->m_uID, &pObject->m_nLuaID, pObject, typeName);
|
2012-03-07 20:27:26 +08:00
|
|
|
return lua_gettop(m_state);
|
2012-02-01 16:55:46 +08:00
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
int CCLuaEngine::pushCCLuaValue(const CCLuaValue& value)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
const CCLuaValueType type = value.getType();
|
|
|
|
if (type == CCLuaValueTypeInt)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
return pushInt(value.intValue());
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
2012-09-11 15:24:25 +08:00
|
|
|
else if (type == CCLuaValueTypeFloat)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
return pushFloat(value.floatValue());
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
2012-09-11 15:24:25 +08:00
|
|
|
else if (type == CCLuaValueTypeBoolean)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
return pushBoolean(value.booleanValue());
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
2012-09-11 15:24:25 +08:00
|
|
|
else if (type == CCLuaValueTypeString)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
return pushString(value.stringValue().c_str());
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
2012-09-11 15:24:25 +08:00
|
|
|
else if (type == CCLuaValueTypeDict)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
pushCCLuaValueDict(value.dictValue());
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
2012-09-11 15:24:25 +08:00
|
|
|
else if (type == CCLuaValueTypeArray)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
2012-09-11 15:24:25 +08:00
|
|
|
pushCCLuaValueArray(value.arrayValue());
|
2012-08-25 15:05:59 +08:00
|
|
|
}
|
2012-09-11 15:24:25 +08:00
|
|
|
else if (type == CCLuaValueTypeCCObject)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
|
|
|
pushCCObject(value.ccobjectValue(), value.getCCObjectTypename().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return lua_gettop(m_state);
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
int CCLuaEngine::pushCCLuaValueDict(const CCLuaValueDict& dict)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
|
|
|
lua_newtable(m_state); /* stack: table */
|
2012-09-11 15:24:25 +08:00
|
|
|
for (CCLuaValueDictIterator it = dict.begin(); it != dict.end(); ++it)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
|
|
|
lua_pushstring(m_state, it->first.c_str()); /* stack: table key */
|
2012-09-20 23:22:45 +08:00
|
|
|
pushCCLuaValue(it->second); /* stack: table key value */
|
2012-08-25 15:05:59 +08:00
|
|
|
lua_rawset(m_state, -3); /* table.key = value, stack: table */
|
|
|
|
}
|
|
|
|
|
|
|
|
return lua_gettop(m_state);
|
|
|
|
}
|
|
|
|
|
2012-09-11 15:24:25 +08:00
|
|
|
int CCLuaEngine::pushCCLuaValueArray(const CCLuaValueArray& array)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
|
|
|
lua_newtable(m_state); /* stack: table */
|
|
|
|
int index = 1;
|
2012-09-11 15:24:25 +08:00
|
|
|
for (CCLuaValueArrayIterator it = array.begin(); it != array.end(); ++it)
|
2012-08-25 15:05:59 +08:00
|
|
|
{
|
2012-09-20 23:22:45 +08:00
|
|
|
pushCCLuaValue(*it); /* stack: table value */
|
2012-08-25 15:05:59 +08:00
|
|
|
lua_rawseti(m_state, -2, index); /* table[index] = value, stack: table */
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lua_gettop(m_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCLuaEngine::cleanStack(void)
|
|
|
|
{
|
|
|
|
lua_settop(m_state, 0);
|
|
|
|
}
|
|
|
|
|
2012-02-07 11:43:29 +08:00
|
|
|
void CCLuaEngine::addLuaLoader(lua_CFunction func)
|
|
|
|
{
|
2012-02-09 14:07:11 +08:00
|
|
|
if (!func) return;
|
|
|
|
|
2012-02-07 11:43:29 +08:00
|
|
|
// stack content after the invoking of the function
|
|
|
|
// get loader table
|
|
|
|
lua_getglobal(m_state, "package"); // package
|
|
|
|
lua_getfield(m_state, -1, "loaders"); // package, loaders
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-07 11:43:29 +08:00
|
|
|
// insert loader into index 2
|
|
|
|
lua_pushcfunction(m_state, func); // package, loaders, func
|
|
|
|
for (int i = lua_objlen(m_state, -2) + 1; i > 2; --i)
|
|
|
|
{
|
|
|
|
lua_rawgeti(m_state, -2, i - 1); // package, loaders, func, function
|
|
|
|
// we call lua_rawgeti, so the loader table now is at -3
|
|
|
|
lua_rawseti(m_state, -3, i); // package, loaders, func
|
|
|
|
}
|
|
|
|
lua_rawseti(m_state, -2, 2); // package, loaders
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-07 11:43:29 +08:00
|
|
|
// set loaders into package
|
|
|
|
lua_setfield(m_state, -2, "loaders"); // package
|
2012-03-08 10:57:03 +08:00
|
|
|
|
2012-02-07 11:43:29 +08:00
|
|
|
lua_pop(m_state, 1);
|
|
|
|
}
|
|
|
|
|
2012-08-25 15:05:59 +08:00
|
|
|
bool CCLuaEngine::pushFunction(int nHandler)
|
2012-03-07 20:27:26 +08:00
|
|
|
{
|
2012-09-20 23:22:45 +08:00
|
|
|
toluafix_get_function_by_refid(m_state, nHandler); /* stack: ... func */
|
2012-03-07 20:27:26 +08:00
|
|
|
if (!lua_isfunction(m_state, -1))
|
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] function refid '%d' does not reference a Lua function", nHandler);
|
|
|
|
lua_pop(m_state, 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-09 14:07:11 +08:00
|
|
|
NS_CC_END
|