17 #ifndef AGXLUA_SCRIPTMANAGER_H
18 #define AGXLUA_SCRIPTMANAGER_H
21 # pragma warning( push )
22 # pragma warning( disable: 4512 ) // Disable warning about assignment operator could not be generated due to reference members.
26 #include <agx/config/AGX_USE_LUA.h>
27 #include <agx/config.h>
56 #define LUA_SCRIPT_PLUGIN_VERSION "1.0"
67 static ScriptManager* instance(
bool erase=
false );
73 friend class ScriptManager;
79 ScriptPlugin() { m_script_mgr = agxLua::ScriptManager::instance(); }
84 virtual std::string getName()=0;
87 std::string getPluginVersion() {
return LUA_SCRIPT_PLUGIN_VERSION; }
93 void addDependentPlugin(
const std::string pluginName )
95 m_dependentPlugins.push_back( pluginName );
104 void registerDependentPlugins(
void );
107 virtual void init(lua_State* lua)=0;
110 const StringVector& getDependentPlugins()
const {
return m_dependentPlugins; }
115 virtual void close()=0;
118 virtual ~ScriptPlugin() {}
119 ScriptManager* m_script_mgr;
128 class RegisterScriptPluginProxy
131 RegisterScriptPluginProxy()
133 if (ScriptManager::instance())
138 if (m_plugin->getPluginVersion() != ScriptManager::instance()->getPluginVersion())
140 "Plugin version (" << m_plugin->getPluginVersion() <<
141 ") does not match required version (" <<
142 ScriptManager::instance()->getPluginVersion() <<
")" <<
LOGGER_END();
145 ScriptPlugin::StringVector::const_iterator it = m_plugin->getDependentPlugins().begin();
146 for(; it != m_plugin->getDependentPlugins().
end(); ++it)
148 if (!ScriptManager::instance()->requestScriptPlugin( *it ))
153 m_plugin->init( ScriptManager::instance()->getLua());
156 ScriptManager::instance()->addScriptPlugin(m_plugin.get());
160 ~RegisterScriptPluginProxy()
162 if (ScriptManager::instance())
165 ScriptManager::instance()->removeScriptPlugin(m_plugin.get());
201 bool registerLuaEntry(
const void* ptr,
const char* functionName);
202 bool unregisterLuaEntry(
const void* ptr);
204 #define ScriptManager_REGISTER_OBJECT( P, NAME ) agxLua::ScriptManager::instance()->registerLuaEntry(P, NAME) //this, typeid(this).name())
205 #define ScriptManager_UNREGISTER_OBJECT( P ) agxLua::ScriptManager::instance()->unregisterLuaEntry(P) //this, typeid(this).name())
208 void addScriptPlugin(ScriptPlugin *);
211 void removeScriptPlugin(ScriptPlugin *);
214 bool requestScriptPlugin(
const std::string& libraryName);
217 bool isLoaded(
const std::string& libraryName);
223 void setDebugMode(
bool flag =
true);
226 void setLogPath(
const std::string& path) { m_log_path=path; }
229 void init(lua_State* L=
nullptr,
unsigned int stacksize=1024);
232 lua_State* getLua() {
return m_lua_state; }
235 const char* getLogPath() {
return m_log_path.c_str(); }
238 bool doFile(
const std::string& path);
241 bool doString(
const std::string&
string);
250 void setGarbageCollectionEnabled(
bool b );
251 bool getGarbageCollectionEnabled()
const {
return m_garbage_collection_enabled; };
253 void setGarbageCollectionFrequency(
agx::Real f ) { m_garbage_collection_frequency = f; };
254 agx::Real getGarbageCollectionFrequency()
const {
return m_garbage_collection_frequency; };
256 std::string getPluginVersion() {
return m_plugin_version; }
260 bool getTreatInvalidAsErrors()
const {
return m_treatInvalidAsErrors;}
261 void setTreatInvalidAsErrors(
const bool treatInvalidAsErrors) {m_treatInvalidAsErrors = treatInvalidAsErrors;}
264 int getMemoryUsed()
const;
266 std::string getLastErrorMessage()
const {
return m_lastErrorMessage; }
267 void setLastErrorMessage(
const std::string& errorMessage) { m_lastErrorMessage = errorMessage; }
270 SINGLETON_CLASSNAME_METHOD();
273 void registerFunctions();
277 bool m_garbage_collection_enabled;
278 bool m_garbage_collection_requested;
279 agx::Real m_garbage_collection_frequency;
283 typedef ScriptPluginVector::iterator ScriptPluginVectorIterator;
285 ScriptPluginVector m_plugin_vector;
288 bool closeLibrary(
const std::string& fileName);
291 std::string createLibraryNameForExtension(
const std::string& ext);
294 DynamicLibraryList::iterator getLibraryItr(
const std::string& fileName);
300 DynamicLibraryList m_dllist;
302 std::string m_log_path;
303 lua_State* m_lua_state;
306 virtual ~ScriptManager();
309 ScriptManager(
void );
311 const std::string m_plugin_version;
315 ObjectReferenceMap m_object_references;
318 int m_incrementalSize;
319 bool m_shutdownInProgress;
320 bool m_treatInvalidAsErrors;
321 std::string m_lastErrorMessage;
323 static ScriptManager* s_instance;
326 #define g_luaScriptManager agxLua::ScriptManager::instance()
332 LuaCallback(
void*
self,
const char* className) : m_self(self), m_lua(g_luaScriptManager->getLua())
334 ScriptManager_REGISTER_OBJECT(
self, className);
337 virtual ~LuaCallback()
339 ScriptManager_UNREGISTER_OBJECT(m_self);
351 #if defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW32__) || defined( __BCPLUSPLUS__) || defined( __MWERKS__)
352 # if defined( AGX_LIBRARY_STATIC )
353 # define AGXLUAPLUGIN_EXPORT
355 # define AGXLUAPLUGIN_EXPORT __declspec(dllexport)
357 #elif !defined(_MSC_VER)
360 #define AGXLUAPLUGIN_EXPORT __attribute__ ((visibility("default")))
362 #define AGXLUAPLUGIN_EXPORT
365 #define AGXLUAPLUGIN_EXPORT
372 # pragma warning(pop)