source: Plugins/luaScripting/Plugin.cpp @ 15

Revision 15, 3.3 KB checked in by art, 12 years ago (diff)
Line 
1#include <nrEngine/nrEngine.h>
2
3#if NR_PLATFORM == NR_PLATFORM_WIN32
4        #define _PluginExport __declspec( dllexport )
5#else
6        #define _PluginExport
7#endif
8
9
10#include "ScriptLoader.h"
11#include "Script.h"
12#include "ScriptConnector.h"
13
14using namespace nrEngine;
15
16//---------------------------------------------------------
17// Some globals
18//---------------------------------------------------------
19Engine*                                 mRoot = NULL;
20ResourceLoader                  mLoader;
21ScriptConnector*                mConnector = NULL;
22bool                                    mLuaStateCreated = false;
23
24//---------------------------------------------------------
25// Get full string according to the plugin version
26//---------------------------------------------------------
27extern "C" _PluginExport char* plgVersionString( void )
28{
29        return "Lua embedded scripting v0.1 for nrEngine";
30}
31
32//---------------------------------------------------------
33// Initialize the plugin
34//---------------------------------------------------------
35extern "C" _PluginExport int plgInitialize(Engine* root, PropertyList* args)
36{
37        // initialize lua subsystem
38        NR_Log(Log::LOG_PLUGIN, "luaScript: %s", plgVersionString());
39
40        // so now check the parameters, if we have a parameter luastate
41        if (args){
42                if (args->exists("luastate")){
43                        Property& prop = (*args)["luastate"];
44                        NR_Log(Log::LOG_PLUGIN, "luaScript: Parameter 'luastate' is given, use it!");
45                        try{
46                                LuaScript::State = prop.get<lua_State*>();
47                        }catch(boost::bad_any_cast& err){
48                                // ok we have got a bad cast, so check for user data in the property
49                                if (!prop.hasUserData()){
50                                        NR_Log(Log::LOG_PLUGIN, "luaScript: Parameter 'luastate' does not contain valid value nor user data!");
51                                        LuaScript::State = NULL;
52                                }else
53                                        LuaScript::State = static_cast<lua_State*>(prop.getUserData());
54                        }
55                }
56        }
57       
58        // setup lua by myself
59        if (LuaScript::State == NULL)
60        {
61                LuaScript::State = luaL_newstate();
62                if (LuaScript::State == NULL){
63                        NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "luaScript: Initialization of lua state failed!");
64                        return -1;
65                }
66                mLuaStateCreated = true;
67                luaL_openlibs(LuaScript::State);
68        }
69
70        // initialize the new kind of loader
71        mRoot = root;
72        mLoader.reset(new LuaScriptLoader());
73        mRoot->sResourceManager()->registerLoader("LuaScriptLoader", mLoader);
74
75        // initialize the script connector
76        mConnector = new LuaScriptConnector();
77       
78        return 0;       
79}
80 
81
82//---------------------------------------------------------
83// Get the engines version number for which this plugin was wrote
84//---------------------------------------------------------
85extern "C" _PluginExport  unsigned int plgEngineVersion( void )
86{
87        return createVersionInteger(0,0,7);
88}
89
90
91//---------------------------------------------------------
92// Return a string according to the given error code
93//---------------------------------------------------------
94extern "C" _PluginExport  char* plgError(int err)
95{
96        return "Unknown";
97}
98
99
100//---------------------------------------------------------
101// Release the plugin
102//---------------------------------------------------------
103extern "C" _PluginExport  void plgRelease( void )
104{
105        // remove the loader from the engine
106        Engine::sResourceManager()->removeLoader("LuaScriptLoader");
107       
108        if (mLuaStateCreated && LuaScript::State != NULL){
109                lua_close(LuaScript::State);
110        }
111       
112        mLoader.reset();
113
114        if (mConnector) delete mConnector;
115               
116        NR_Log(Log::LOG_PLUGIN, "luaScript: Plugin closed and removed");
117
118}
Note: See TracBrowser for help on using the repository browser.