00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "PluginLoader.h"
00018 #include "Log.h"
00019
00020 namespace nrEngine{
00021
00022
00023 ScriptFunctionDec(loadPlugin, PluginLoader)
00024 {
00025
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
00032 Engine::instance()->loadPlugin("", args[2], args[1]);
00033
00034
00035 return ScriptResult();
00036 }
00037
00038
00039 PluginLoader::PluginLoader() : IResourceLoader("PluginLoader")
00040 {
00041 initializeResourceLoader();
00042
00043
00044 Engine::sScriptEngine()->add("loadPlugin", loadPlugin);
00045 }
00046
00047
00048
00049 PluginLoader::~PluginLoader()
00050 {
00051
00052 Engine::sScriptEngine()->del("loadPlugin");
00053 }
00054
00055
00056 Result PluginLoader::initializeResourceLoader()
00057 {
00058
00059
00060 declareSupportedResourceType("Plugin");
00061 declareSupportedResourceType("nrPlugin");
00062
00063
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
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
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
00107 name = name.substr(0, name.length() - 4);
00108 }
00109 #endif
00110
00111
00112 std::list<std::string> flist;
00113 flist.push_back(name);
00114 res->setResourceFilename(flist);
00115
00116
00117 return res->reload(param);
00118 }
00119
00120
00121 IResource* PluginLoader::createResource(const std::string& resourceType, PropertyList* params)
00122 {
00123
00124 return new Plugin();
00125 }
00126
00127
00128
00129 IResource* PluginLoader::createEmptyResource(const std::string& resourceType)
00130 {
00131
00132 return new EmptyPlugin();
00133 }
00134
00135
00136 };
00137