2
// C++ Interface: LuaScriptingProvider
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2005
9
// This program is free software; you can redistribute it and/or modify
10
// it under the terms of the GNU General Public License as published by
11
// the Free Software Foundation; either version 2 of the License, or
12
// (at your option) any later version.
14
// This program is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
// GNU General Public License for more details.
19
// You should have received a copy of the GNU General Public License
20
// along with this program; if not, write to the Free Software
21
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.//
23
#ifndef EMBEROGRELUASCRIPTINGPROVIDER_H
24
#define EMBEROGRELUASCRIPTINGPROVIDER_H
26
#include "framework/IScriptingProvider.h"
28
#include <CEGUIScriptModule.h>
36
class ScriptingService;
41
class LuaScriptingCallContext;
44
@brief A scripting provider for Lua.
46
This acts as a bridge between Ember and the Lua scripting environment. Opon creation and destruction it will take care of setting up and tearing down the lua virtual machine. Remember to call stop() before deleting an instance of this to make sure that everything is properly cleaned up.
48
If you want to inspect the return values from calls to lua scripts, pass a pointer to LuaScriptingCallContext to the executeScript methods.
49
@author Erik Hjortsberg
51
class LuaScriptingProvider : public Ember::IScriptingProvider
54
LuaScriptingProvider();
56
virtual ~LuaScriptingProvider();
59
* @brief Loads the script from the wrapper.
60
* @param resourceWrapper A resource wrapper pointing to a valid resource which can be loaded. This should contain a text file with the script contents.
62
virtual void loadScript(Ember::ResourceWrapper& resWrapper);
65
* @brief Executes the supplied string directly into the scripting environment.
66
* Optionally a pointer to a scripting call context can be submitted too, which will then be populated with return values and other scripting environment specific info.
67
* @param scriptCode The code to excute.
68
* @param callContext An optional pointer to a scripting call context. This will be populated with return values and other info. If you don't have any need for such info, leave this empty.
70
virtual void executeScript(const std::string& scriptCode, Ember::IScriptingCallContext* callContext);
72
virtual void callFunction(const std::string& functionName, int narg, Ember::IScriptingCallContext* callContext);
75
* @brief Returns true if the provider will load the supplied script name. This is in most cases decided from the filename suffix.
76
* @param scriptName The name of the script.
77
* @return True if the script can be loaded, else false.
79
virtual bool willLoadScript(const std::string& scriptName);
82
* @brief Gets the unique name of the scripting provider.
83
* @return The name of the scripting provider.
85
virtual const std::string& getName() const;
88
* @brief Register with a service to allow for callbacks etc.
89
* @param service The service to register with.
91
virtual void _registerWithService(Ember::ScriptingService* service);
94
* @brief Forces a full garbage collection.
96
virtual void forceGC();
98
// virtual void start();
102
* @brief Stops the lua environment, which mainly means that all objects are destroyed.
103
* Call this before this object is destroyed to make sure that all held objects and references are properly released. If not, there's a risk of dangling pointers.
109
* @brief Gets the current lua state.
110
* This will always return a valid lua virtual machine, but note that if @see stop() already has been called it will porbably be in an invalid state.
111
* @return The current lua environment.
113
lua_State* getLuaState();
116
// int getErrorHandlingFunctionIndex() const;
119
* @brief Gets the name of the error handling function, if available.
120
* @return The error handling function name (i.e. a lua function).
122
const std::string& getErrorHandlingFunctionName() const;
128
* @brief Executes the supplied script code.
129
* @param scriptCode The code to execute.
130
* @param luaCallContext An optional lua call context, which if present will contain any return values.
131
* @param scriptName The name of the script, mainly used for debugging purpose.
133
void executeScriptImpl(const std::string& scriptCode, LuaScriptingCallContext* luaCallContext, const std::string& scriptName = std::string(""));
135
void callFunctionImpl(const std::string& functionName, int narg, LuaScriptingCallContext* callContext);
138
* Initializes the lua scripting environment. This entails creating a new Lua virtual machine/state, making sure that the correct lua libraries are loaded and a calling tolua bindings registering hooks.
139
If you add a new tolua bindings class, don't forget to alter this method to include a call to the method which registers all classes and structs.
145
* Creates a new Lua virtual machine/state.
148
// std::auto_ptr<CEGUI::LuaScriptModule> mLuaScriptModule;
151
The main scripting service instance.
153
Ember::ScriptingService* mService;
156
The main lua state. This is the sole entry into the lua virtual machine.
158
lua_State* mLuaState;
160
// int mErrorHandlingFunctionIndex;
163
* @brief The name of the error handling function, if available.
165
std::string mErrorHandlingFunctionName;