Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/ceguilua-0.5.0b/ceguilua/CEGUILua.cpp @ 1825

Last change on this file since 1825 was 1810, checked in by rgrieder, 16 years ago

merged ceguilua branch back to trunk

  • Property svn:eol-style set to native
File size: 9.6 KB
Line 
1/***********************************************************************
2        filename: CEGUILua.cpp
3        created:  16/3/2005
4        author:   Tomas Lindquist Olsen
5       
6        purpose:  Implementation for LuaScriptModule class
7*************************************************************************/
8/***************************************************************************
9 *   Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
10 *
11 *   Permission is hereby granted, free of charge, to any person obtaining
12 *   a copy of this software and associated documentation files (the
13 *   "Software"), to deal in the Software without restriction, including
14 *   without limitation the rights to use, copy, modify, merge, publish,
15 *   distribute, sublicense, and/or sell copies of the Software, and to
16 *   permit persons to whom the Software is furnished to do so, subject to
17 *   the following conditions:
18 *
19 *   The above copyright notice and this permission notice shall be
20 *   included in all copies or substantial portions of the Software.
21 *
22 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 *   OTHER DEALINGS IN THE SOFTWARE.
29 ***************************************************************************/
30#include "CEGUI.h"
31#include "CEGUIPropertyHelper.h"
32#include "CEGUILua.h"
33#include "CEGUILuaFunctor.h"
34#include <vector>
35
36// include Lua libs and tolua++
37#include "lua/lua.hpp"
38#include "tolua/tolua++.h"
39
40#include "tolua++.h"
41
42// prototype for bindings initialisation function
43int tolua_CEGUI_open(lua_State* tolua_S);
44
45
46// Start of CEGUI namespace section
47namespace CEGUI
48{
49
50/*************************************************************************
51        Constructor (creates Lua state)
52*************************************************************************/
53LuaScriptModule::LuaScriptModule()
54{
55    static const luaL_Reg lualibs[] = {
56        {"", luaopen_base},
57        {LUA_LOADLIBNAME, luaopen_package},
58        {LUA_TABLIBNAME, luaopen_table},
59        {LUA_IOLIBNAME, luaopen_io},
60        {LUA_OSLIBNAME, luaopen_os},
61        {LUA_STRLIBNAME, luaopen_string},
62        {LUA_MATHLIBNAME, luaopen_math},
63    #if defined(DEBUG) || defined (_DEBUG)
64        {LUA_DBLIBNAME, luaopen_debug},
65    #endif
66        {0, 0}
67    };
68
69    // create a lua state
70    d_ownsState = true;
71    d_state = lua_open();
72
73    // init all standard libraries
74    const luaL_Reg *lib = lualibs;
75    for (; lib->func; lib++)
76    {
77        lua_pushcfunction(d_state, lib->func);
78        lua_pushstring(d_state, lib->name);
79        lua_call(d_state, 1, 0);
80    }
81
82    setModuleIdentifierString();
83}
84
85
86/*************************************************************************
87        Constructor (uses given Lua state)
88*************************************************************************/
89LuaScriptModule::LuaScriptModule(lua_State* state)
90{
91        // just use the given state
92        d_ownsState = false;
93        d_state = state;
94
95        setModuleIdentifierString();
96}
97
98
99/*************************************************************************
100        Destructor
101*************************************************************************/
102LuaScriptModule::~LuaScriptModule()
103{
104        if ( d_ownsState && d_state )
105        {
106                lua_close( d_state );
107        }
108}
109
110
111/*************************************************************************
112        Execute script file
113*************************************************************************/
114void LuaScriptModule::executeScriptFile(const String& filename, const String& resourceGroup)
115{
116        // load file
117        RawDataContainer raw;
118        System::getSingleton().getResourceProvider()->loadRawDataContainer(filename,
119        raw, resourceGroup.empty() ? d_defaultResourceGroup : resourceGroup);
120
121        // load code into lua
122        int top = lua_gettop(d_state);
123        int loaderr = luaL_loadbuffer(d_state, (char*)raw.getDataPtr(), raw.getSize(), filename.c_str());
124        System::getSingleton().getResourceProvider()->unloadRawDataContainer( raw );
125        if (loaderr)
126        {
127            String errMsg = lua_tostring(d_state,-1);
128                lua_settop(d_state,top);
129                throw ScriptException("Unable to execute Lua script file: '"+filename+"'\n\n"+errMsg+"\n");
130        }
131   
132    // call it
133        if (lua_pcall(d_state,0,0,0))
134        {
135            String errMsg = lua_tostring(d_state,-1);
136                lua_settop(d_state,top);
137                throw ScriptException("Unable to execute Lua script file: '"+filename+"'\n\n"+errMsg+"\n");
138        }
139
140        lua_settop(d_state,top); // just in case :P
141}
142
143
144/*************************************************************************
145        Execute global script function
146*************************************************************************/
147int     LuaScriptModule::executeScriptGlobal(const String& function_name)
148{
149    int top = lua_gettop(d_state);
150
151    // get the function from lua
152    lua_getglobal(d_state, function_name.c_str());
153
154    // is it a function
155    if (!lua_isfunction(d_state,-1))
156    {
157        lua_settop(d_state,top);
158        throw ScriptException("Unable to get Lua global: '"+function_name+"' as name not represent a global Lua function" );
159    }
160
161    // call it
162    int error = lua_pcall(d_state,0,1,0);               
163
164    // handle errors
165    if (error)
166    {
167        String errMsg = lua_tostring(d_state,-1);
168        lua_pop(d_state,1);
169        throw ScriptException("Unable to evaluate Lua global: '"+function_name+"\n\n"+errMsg+"\n");
170    }
171
172    // get return value
173    if (!lua_isnumber(d_state,-1))
174    {
175        // log that return value is invalid. return -1 and move on.
176        lua_settop(d_state,top);
177        ScriptException("Unable to get Lua global : '"+function_name+"' return value as it's not a number" );
178        return -1;
179    }
180
181    int ret = (int)lua_tonumber(d_state,-1);
182    lua_pop(d_state,1);
183
184    // return it
185    return ret;
186}
187
188
189/*************************************************************************
190        Execute scripted event handler
191*************************************************************************/
192bool LuaScriptModule::executeScriptedEventHandler(const String& handler_name, const EventArgs& e)
193{
194    LuaFunctor::pushNamedFunction(d_state, handler_name);
195
196    // push EventArgs as the first parameter
197    tolua_pushusertype(d_state,(void*)&e,"const CEGUI::EventArgs");
198
199    // call it
200    int error = lua_pcall(d_state,1,0,0);
201
202    // handle errors
203    if (error)
204    {
205        String errStr(lua_tostring(d_state,-1));
206        lua_pop(d_state,1);
207        throw ScriptException("Unable to evaluate the Lua event handler: '"+handler_name+"'\n\n"+errStr+"\n");
208    }
209
210    return true;
211}
212
213
214/*************************************************************************
215        Execute script code string
216*************************************************************************/
217void LuaScriptModule::executeString(const String& str)
218{
219    int top = lua_gettop(d_state);
220
221    // load code into lua and call it
222    int error = luaL_loadbuffer(d_state, str.c_str(), str.length(), str.c_str()) || lua_pcall(d_state,0,0,0);
223
224    // handle errors
225    if (error)
226    {
227        String errMsg = lua_tostring(d_state,-1);
228        lua_settop(d_state,top);
229        throw ScriptException("Unable to execute Lua script string: '"+str+"'\n\n"+errMsg+"\n");
230    }
231}
232       
233
234/*************************************************************************
235        Create Lua bindings
236*************************************************************************/
237void LuaScriptModule::createBindings(void)
238{
239        CEGUI::Logger::getSingleton().logEvent( "---- Creating Lua bindings ----" );
240        // init CEGUI module
241        tolua_CEGUI_open(d_state);
242}
243
244
245/*************************************************************************
246        Destroy Lua bindings
247*************************************************************************/
248void LuaScriptModule::destroyBindings(void)
249{
250        CEGUI::Logger::getSingleton().logEvent( "---- Destroying Lua bindings ----" );
251        // is this ok ?
252        lua_pushnil(d_state);
253        lua_setglobal(d_state,"CEGUI");
254}
255
256
257/*************************************************************************
258        Set the ID string for the module
259*************************************************************************/
260void LuaScriptModule::setModuleIdentifierString()
261{
262    // set ID string
263    d_identifierString = "CEGUI::LuaScriptModule - Official Lua based scripting module for CEGUI";
264}
265
266
267/*************************************************************************
268        Subscribe to a scripted event handler
269*************************************************************************/
270Event::Connection LuaScriptModule::subscribeEvent(EventSet* target, const String& event_name, const String& subscriber_name)
271{
272    // do the real subscription
273    LuaFunctor functor(d_state,subscriber_name,LUA_NOREF);
274    Event::Connection con = target->subscribeEvent(event_name, Event::Subscriber(functor));
275    // make sure we don't release the reference we just made when this call returns
276    functor.index = LUA_NOREF;
277
278    // return the event connection
279    return con;
280}
281
282
283/*************************************************************************
284        Subscribe to a scripted event handler
285*************************************************************************/
286Event::Connection LuaScriptModule::subscribeEvent(EventSet* target, const String& event_name, Event::Group group, const String& subscriber_name)
287{
288    // do the real subscription
289    LuaFunctor functor(d_state,subscriber_name,LUA_NOREF);
290    Event::Connection con = target->subscribeEvent(event_name, group, Event::Subscriber(functor));
291
292    // return the event connection
293    return con;
294}
295
296} // namespace CEGUI
Note: See TracBrowser for help on using the repository browser.