~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/components/ogre/scripting/LuaScriptingProvider.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// C++ Interface: LuaScriptingProvider
 
3
//
 
4
// Description: 
 
5
//
 
6
//
 
7
// Author: Erik Hjortsberg <erik.hjortsberg@gmail.com>, (C) 2005
 
8
//
 
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.
 
13
// 
 
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.
 
18
// 
 
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.//
 
22
//
 
23
#ifndef EMBEROGRELUASCRIPTINGPROVIDER_H
 
24
#define EMBEROGRELUASCRIPTINGPROVIDER_H
 
25
 
 
26
#include "framework/IScriptingProvider.h"
 
27
 
 
28
#include <CEGUIScriptModule.h>
 
29
#include <CEGUILua.h>
 
30
 
 
31
 
 
32
struct lua_State;
 
33
 
 
34
namespace Ember
 
35
{
 
36
        class ScriptingService;
 
37
}
 
38
 
 
39
namespace EmberOgre {
 
40
 
 
41
class LuaScriptingCallContext;
 
42
 
 
43
/**
 
44
@brief A scripting provider for Lua.
 
45
 
 
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.
 
47
 
 
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
 
50
*/
 
51
class LuaScriptingProvider : public Ember::IScriptingProvider
 
52
{
 
53
public:
 
54
    LuaScriptingProvider();
 
55
 
 
56
    virtual ~LuaScriptingProvider();
 
57
 
 
58
        /**
 
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.
 
61
         */
 
62
        virtual void loadScript(Ember::ResourceWrapper& resWrapper);
 
63
        
 
64
        /**
 
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.
 
69
         */
 
70
        virtual void executeScript(const std::string& scriptCode, Ember::IScriptingCallContext* callContext);
 
71
 
 
72
        virtual void callFunction(const std::string& functionName, int narg, Ember::IScriptingCallContext* callContext);
 
73
 
 
74
        /**
 
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.
 
78
         */
 
79
        virtual bool willLoadScript(const std::string& scriptName);
 
80
        
 
81
        /**
 
82
         *   @brief Gets the unique name of the scripting provider.
 
83
         * @return The name of the scripting provider.
 
84
         */
 
85
        virtual const std::string& getName() const;
 
86
        
 
87
        /**
 
88
         *   @brief Register with  a service to allow for callbacks etc.
 
89
         * @param service The service to register with.
 
90
         */
 
91
        virtual void _registerWithService(Ember::ScriptingService* service);
 
92
        
 
93
        /**
 
94
         *   @brief Forces a full garbage collection.
 
95
         */
 
96
        virtual void forceGC();
 
97
    
 
98
//      virtual void start();
 
99
 
 
100
 
 
101
        /**
 
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.
 
104
         */
 
105
        virtual void stop();
 
106
    
 
107
    
 
108
        /**
 
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.
 
112
         */
 
113
        lua_State* getLuaState();
 
114
        
 
115
        
 
116
//      int getErrorHandlingFunctionIndex() const;
 
117
 
 
118
        /**
 
119
         * @brief Gets the name of the error handling function, if available.
 
120
         * @return The error handling function name (i.e. a lua function).
 
121
         */
 
122
        const std::string& getErrorHandlingFunctionName() const; 
 
123
        
 
124
 
 
125
private:
 
126
 
 
127
        /**
 
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.
 
132
         */
 
133
        void executeScriptImpl(const std::string& scriptCode, LuaScriptingCallContext* luaCallContext, const std::string& scriptName = std::string(""));
 
134
 
 
135
        void callFunctionImpl(const std::string& functionName, int narg, LuaScriptingCallContext* callContext);
 
136
 
 
137
        /**
 
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.
 
140
         */
 
141
        void initialize();
 
142
        
 
143
        
 
144
        /**
 
145
         *    Creates a new Lua virtual machine/state.
 
146
         */
 
147
        void createState();
 
148
//      std::auto_ptr<CEGUI::LuaScriptModule> mLuaScriptModule;
 
149
        
 
150
        /**
 
151
        The main scripting service instance.
 
152
        */
 
153
        Ember::ScriptingService* mService;
 
154
        
 
155
        /**
 
156
        The main lua state. This is the sole entry into the lua virtual machine.
 
157
        */
 
158
        lua_State* mLuaState;
 
159
        
 
160
//      int mErrorHandlingFunctionIndex;
 
161
 
 
162
        /**
 
163
         * @brief The name of the error handling function, if available.
 
164
         */
 
165
        std::string mErrorHandlingFunctionName;
 
166
        
 
167
};
 
168
 
 
169
}
 
170
 
 
171
#endif