2011-11-18 20:36:39 +08:00
|
|
|
/****************************************************************************
|
2011-11-29 15:44:54 +08:00
|
|
|
Copyright (c) 2011 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCLuaEngine.h"
|
2011-11-23 13:37:32 +08:00
|
|
|
#include "tolua++.h"
|
|
|
|
#include "tolua_fix.h"
|
2011-11-18 20:36:39 +08:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "lualib.h"
|
|
|
|
#include "lauxlib.h"
|
2011-11-23 13:37:32 +08:00
|
|
|
#include "lualoadexts.h"
|
2011-11-18 20:36:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "LuaCocos2d.h"
|
2011-11-23 13:37:32 +08:00
|
|
|
#include "LuaSimpleAudioEngine.h"
|
2011-11-18 20:36:39 +08:00
|
|
|
#include "LuaGameInterfaces.h"
|
2011-11-29 15:44:54 +08:00
|
|
|
#include "CCArray.h"
|
|
|
|
#include "CCTimer.h"
|
2011-11-18 20:36:39 +08:00
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
namespace cocos2d
|
|
|
|
{
|
|
|
|
|
|
|
|
CCSchedulerFuncEntry* CCSchedulerFuncEntry::entryWithFunctionRefID(int functionRefID, ccTime fInterval, bool bPaused)
|
|
|
|
{
|
|
|
|
CCSchedulerFuncEntry* entry = new CCSchedulerFuncEntry();
|
|
|
|
entry->initWithFunctionRefID(functionRefID, fInterval, bPaused);
|
|
|
|
entry->autorelease();
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCSchedulerFuncEntry::initWithFunctionRefID(int functionRefID, ccTime fInterval, bool bPaused)
|
|
|
|
{
|
|
|
|
m_timer = new CCTimer();
|
|
|
|
m_timer->initWithScriptFunc(functionRefID, fInterval);
|
|
|
|
m_timer->autorelease();
|
|
|
|
m_timer->retain();
|
|
|
|
m_functionRefID = functionRefID;
|
|
|
|
m_paused = bPaused;
|
|
|
|
CCLOG("[LUA] ADD function refID: %04d, add schedule entryID: %d", m_functionRefID, m_entryID);
|
|
|
|
return true;
|
|
|
|
}
|
2011-11-18 20:36:39 +08:00
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
CCSchedulerFuncEntry::CCSchedulerFuncEntry(void)
|
|
|
|
: m_timer(NULL)
|
|
|
|
, m_functionRefID(0)
|
|
|
|
, m_paused(true)
|
|
|
|
, m_isMarkDeleted(false)
|
|
|
|
{
|
|
|
|
static int entryIDCount = 0;
|
|
|
|
++entryIDCount;
|
|
|
|
m_entryID = entryIDCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCSchedulerFuncEntry::~CCSchedulerFuncEntry(void)
|
|
|
|
{
|
|
|
|
m_timer->release();
|
|
|
|
CCLuaEngine::sharedEngine()->removeLuaFunctionRef(m_functionRefID);
|
|
|
|
CCLOG("[LUA] DEL function refID: %04d, remove schedule entryID: %d", m_functionRefID, m_entryID);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------
|
2011-11-18 20:36:39 +08:00
|
|
|
|
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
CCLuaEngine* CCLuaEngine::s_engine = NULL;
|
|
|
|
|
|
|
|
CCLuaEngine::CCLuaEngine()
|
2011-11-18 20:36:39 +08:00
|
|
|
{
|
|
|
|
m_state = lua_open();
|
|
|
|
luaL_openlibs(m_state);
|
|
|
|
tolua_Cocos2d_open(m_state);
|
|
|
|
tolua_SimpleAudioEngine_open(m_state);
|
|
|
|
tolua_prepare_ccobject_table(m_state);
|
|
|
|
tolua_LuaGameInterfaces_open(m_state);
|
2011-11-23 13:55:07 +08:00
|
|
|
luax_loadexts(m_state);
|
2011-11-18 20:36:39 +08:00
|
|
|
}
|
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
CCLuaEngine::~CCLuaEngine()
|
2011-11-18 20:36:39 +08:00
|
|
|
{
|
|
|
|
lua_close(m_state);
|
|
|
|
s_engine = NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
CCLuaEngine* CCLuaEngine::sharedEngine()
|
2011-11-18 20:36:39 +08:00
|
|
|
{
|
|
|
|
if (!s_engine)
|
|
|
|
{
|
2011-11-29 15:44:54 +08:00
|
|
|
s_engine = new CCLuaEngine();
|
2011-11-18 20:36:39 +08:00
|
|
|
}
|
|
|
|
return s_engine;
|
|
|
|
}
|
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
void CCLuaEngine::purgeSharedEngine()
|
2011-11-18 20:36:39 +08:00
|
|
|
{
|
|
|
|
if (s_engine) delete s_engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------
|
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
void CCLuaEngine::removeCCObject(CCObject *object)
|
2011-11-18 20:36:39 +08:00
|
|
|
{
|
|
|
|
tolua_remove_ccobject_by_refid(m_state, object->m_refID);
|
|
|
|
}
|
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
void CCLuaEngine::removeLuaFunctionRef(int functionRefID)
|
2011-11-18 20:36:39 +08:00
|
|
|
{
|
2011-11-29 15:44:54 +08:00
|
|
|
tolua_remove_function_by_refid(m_state, functionRefID);
|
2011-11-18 20:36:39 +08:00
|
|
|
}
|
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
void CCLuaEngine::addSearchPath(const char* path)
|
2011-11-18 20:36:39 +08:00
|
|
|
{
|
|
|
|
lua_getglobal(m_state, "package"); /* stack: package */
|
|
|
|
lua_getfield(m_state, -1, "path"); /* get package.path, stack: package path */
|
|
|
|
const char* cur_path = lua_tostring(m_state, -1);
|
|
|
|
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: - */
|
|
|
|
}
|
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
int CCLuaEngine::executeScriptFile(const char* filename)
|
2011-11-18 20:36:39 +08:00
|
|
|
{
|
|
|
|
int nRet = luaL_dofile(m_state, filename);
|
2011-11-29 15:44:54 +08:00
|
|
|
// lua_gc(m_state, LUA_GCCOLLECT, 0);
|
2011-11-18 20:36:39 +08:00
|
|
|
|
|
|
|
if (nRet != 0)
|
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] %s", lua_tostring(m_state, -1));
|
|
|
|
lua_pop(m_state, 1);
|
2011-11-29 15:44:54 +08:00
|
|
|
return nRet;
|
2011-11-18 20:36:39 +08:00
|
|
|
}
|
2011-11-29 15:44:54 +08:00
|
|
|
return 0;
|
2011-11-18 20:36:39 +08:00
|
|
|
}
|
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
int CCLuaEngine::executeGlobalFunction(const char* function_name)
|
2011-11-18 20:36:39 +08:00
|
|
|
{
|
|
|
|
lua_getglobal(m_state, function_name); /* query function by name, stack: function */
|
|
|
|
if (!lua_isfunction(m_state, -1))
|
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] name '%s' does not represent a Lua function", function_name);
|
|
|
|
lua_pop(m_state, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int error = lua_pcall(m_state, 0, 1, 0); /* call function, stack: ret */
|
2011-11-29 15:44:54 +08:00
|
|
|
// lua_gc(m_state, LUA_GCCOLLECT, 0);
|
2011-11-18 20:36:39 +08:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] %s", lua_tostring(m_state, - 1));
|
|
|
|
lua_pop(m_state, 1); // clean error message
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get return value
|
|
|
|
if (!lua_isnumber(m_state, -1))
|
|
|
|
{
|
|
|
|
lua_pop(m_state, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-11-29 15:44:54 +08:00
|
|
|
|
2011-11-18 20:36:39 +08:00
|
|
|
int ret = lua_tointeger(m_state, -1);
|
|
|
|
lua_pop(m_state, 1); /* stack: - */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
int CCLuaEngine::executeFunctionByRefID(int functionRefId, int numArgs)
|
2011-11-18 20:36:39 +08:00
|
|
|
{
|
2011-11-29 15:44:54 +08:00
|
|
|
lua_pushstring(m_state, TOLUA_REFID_FUNC_MAPPING);
|
|
|
|
lua_rawget(m_state, LUA_REGISTRYINDEX); /* stack: refid_func */
|
|
|
|
lua_pushinteger(m_state, functionRefId); /* stack: refid_func refid */
|
|
|
|
lua_rawget(m_state, -2); /* stack: refid_func func */
|
|
|
|
|
2011-11-18 20:36:39 +08:00
|
|
|
if (!lua_isfunction(m_state, -1))
|
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] function refid '%d' does not reference a Lua function", functionRefId);
|
|
|
|
lua_pop(m_state, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-11-29 15:44:54 +08:00
|
|
|
|
|
|
|
if (numArgs > 0)
|
|
|
|
{
|
|
|
|
int lo = -2 - numArgs;
|
|
|
|
while (lo <= -3)
|
|
|
|
{
|
|
|
|
tolua_pushvalue(m_state, lo); /* stack: refid_func func (...) */
|
|
|
|
++lo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int error = lua_pcall(m_state, numArgs, 1, 0); /* stack: refid_func ret */
|
2011-11-18 20:36:39 +08:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] %s", lua_tostring(m_state, - 1));
|
2011-11-29 15:44:54 +08:00
|
|
|
lua_pop(m_state, 2); // clean error message
|
2011-11-18 20:36:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get return value
|
|
|
|
if (!lua_isnumber(m_state, -1))
|
|
|
|
{
|
2011-11-29 15:44:54 +08:00
|
|
|
lua_pop(m_state, 2);
|
2011-11-18 20:36:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = lua_tointeger(m_state, -1);
|
2011-11-29 15:44:54 +08:00
|
|
|
lua_pop(m_state, 2);
|
2011-11-18 20:36:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-29 17:11:20 +08:00
|
|
|
int CCLuaEngine::executeFunctionWithIntegerData(int functionRefId, int data)
|
|
|
|
{
|
|
|
|
lua_pushinteger(m_state, data);
|
|
|
|
int ret = executeFunctionByRefID(functionRefId, 1);
|
|
|
|
lua_pop(m_state, 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCLuaEngine::executeFunctionWithFloatData(int functionRefId, float data)
|
|
|
|
{
|
|
|
|
lua_pushnumber(m_state, data);
|
|
|
|
int ret = executeFunctionByRefID(functionRefId, 1);
|
|
|
|
lua_pop(m_state, 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCLuaEngine::executeFunctionWithBooleanData(int functionRefId, bool data)
|
|
|
|
{
|
|
|
|
lua_pushboolean(m_state, data);
|
|
|
|
int ret = executeFunctionByRefID(functionRefId, 1);
|
|
|
|
lua_pop(m_state, 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-18 20:36:39 +08:00
|
|
|
// functions for excute touch event
|
2011-11-29 15:44:54 +08:00
|
|
|
int CCLuaEngine::executeTouchEvent(int functionRefId, CCTouch *pTouch)
|
2011-11-18 20:36:39 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
int CCLuaEngine::executeTouchesEvent(int functionRefId, CCSet *pTouches)
|
2011-11-18 20:36:39 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
int CCLuaEngine::executeSchedule(int functionRefID, ccTime dt)
|
2011-11-18 20:36:39 +08:00
|
|
|
{
|
2011-11-29 15:44:54 +08:00
|
|
|
lua_pushnumber(m_state, dt);
|
|
|
|
int ret = executeFunctionByRefID(functionRefID, 1);
|
|
|
|
lua_pop(m_state, 1);
|
|
|
|
return ret;
|
2011-11-18 20:36:39 +08:00
|
|
|
}
|
|
|
|
|
2011-11-29 15:44:54 +08:00
|
|
|
} // namespace cocos2d
|