PluginLoader.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
00004  *       mailto: <tevs@mpi-sb.mpg.de>                                      *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU General Public License as published by  *
00008  *   the Free Software Foundation; either version 2 of the License, or     *
00009  *   (at your option) any later version.                                   *
00010  *                                                                         *
00011  ***************************************************************************/
00012 
00013 
00014 //----------------------------------------------------------------------------------
00015 // Includes
00016 //----------------------------------------------------------------------------------
00017 #include "PluginLoader.h"
00018 #include "Log.h"
00019 
00020 namespace nrEngine{
00021         
00022         //----------------------------------------------------------------------------------
00023         ScriptFunctionDec(loadPlugin, PluginLoader)
00024         {
00025                 // check if the parameter count is valid
00026                 if (args.size() <= 2){
00027                         NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "PluginLoader: loadPlugin(name, file) : wrong parameter count");
00028                         return ScriptResult(std::string("More parameters required! loadPlugin(name, file)"));
00029                 }
00030                 
00031                 // try to load all the plugins given
00032                 Engine::instance()->loadPlugin("", args[2], args[1]);
00033 
00034                 // ok
00035                 return ScriptResult();
00036         }
00037         
00038         //----------------------------------------------------------------------------------
00039         PluginLoader::PluginLoader() : IResourceLoader("PluginLoader")
00040         {
00041                 initializeResourceLoader();
00042                 
00043                 // register some new functions by the scripting engine
00044                 Engine::sScriptEngine()->add("loadPlugin", loadPlugin);
00045         }
00046 
00047         
00048         //----------------------------------------------------------------------------------
00049         PluginLoader::~PluginLoader()
00050         {
00051                 // remove the functions provided to the scripting engine
00052                 Engine::sScriptEngine()->del("loadPlugin");
00053         }
00054         
00055         //----------------------------------------------------------------------------------
00056         Result PluginLoader::initializeResourceLoader()
00057         {
00058 
00059                 // fill out supported resource types;
00060                 declareSupportedResourceType("Plugin");
00061                 declareSupportedResourceType("nrPlugin");
00062                 
00063                 // we do only support dll files in windows version of our engine
00064 #if NR_PLATFORM == NR_PLATFORM_WIN32
00065                 declareSupportedFileType("dll");
00066                 declareTypeMap("dll", "Plugin");
00067 #elif NR_PLATFORM == NR_PLATFORM_LINUX
00068                 declareSupportedFileType("so");
00069                 declareTypeMap("so", "Plugin");
00070 #endif
00071                 return OK;
00072         }
00073 
00074         //----------------------------------------------------------------------------------
00075         std::string PluginLoader::getSuffix(const std::string& resType)
00076         {
00077 #if NR_PLATFORM == NR_PLATFORM_WIN32
00078                 return std::string("dll");
00079 #elif NR_PLATFORM == NR_PLATFORM_LINUX
00080                 return std::string("so");
00081 #endif
00082         }
00083 
00084         //----------------------------------------------------------------------------------
00085         Result PluginLoader::loadResource(IResource* res, const std::string& fileName, PropertyList* param)
00086         {
00087 
00088                 // check whenever we have a valid file name of the plugin
00089                 if (fileName.length() <= 3){
00090                         NR_Log(Log::LOG_ENGINE, Log::LL_ERROR, "File name of the plugin is not valid %s. It must contain at least 3 characters.", fileName.c_str());
00091                         return RES_BAD_FILETYPE;
00092                 }
00093 
00094                 std::string name = fileName;
00095                 
00096 #if NR_PLATFORM == NR_PLATFORM_LINUX
00097                 if (name.substr(name.length() - 3, 3) != ".so")
00098                 {
00099                         // dlopen() does not add .so to the filename, like windows does for .dll
00100                         NR_Log(Log::LOG_ENGINE, Log::LL_WARNING, "\".so\" added to the plugin file name %s", name.c_str());
00101                         name += ".so";
00102                 }
00103 #elif NR_PLATFORM == NR_PLATFORM_WIN32
00104                 if (name.substr(name.length() - 4, 4) == ".dll")
00105                 {
00106                         // windows does automaticly add .dll to the file name, so we have to cut the name
00107                         name = name.substr(0, name.length() - 4);
00108                 }               
00109 #endif
00110 
00111                 // set filename to the extended one
00112                 std::list<std::string> flist;
00113                 flist.push_back(name);
00114                 res->setResourceFilename(flist);
00115 
00116                 // call reloading method in resource, so it get loaded
00117                 return res->reload(param);              
00118         }
00119         
00120         //----------------------------------------------------------------------------------
00121         IResource* PluginLoader::createResource(const std::string& resourceType, PropertyList* params)
00122         {       
00123                 // create an plugin instance
00124                 return new Plugin();
00125         }
00126         
00127         
00128         //----------------------------------------------------------------------------------
00129         IResource* PluginLoader::createEmptyResource(const std::string& resourceType)
00130         {
00131                 // create an instance of empty plugin
00132                 return new EmptyPlugin();       
00133         }
00134         
00135         
00136 };
00137 

Generated on Wed Sep 12 23:19:42 2007 for nrEngine by  doxygen 1.5.1