ScriptLoader.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 "ScriptLoader.h"
00018 #include "Log.h"
00019 #include "FileStream.h"
00020 #include "Kernel.h"
00021 
00022 namespace nrEngine{
00023 
00024         //----------------------------------------------------------------------------------
00025         ScriptLoader::ScriptLoader() : IResourceLoader("ScriptLoader")
00026         {
00027                 initializeResourceLoader();
00028         }
00029 
00030 
00031         //----------------------------------------------------------------------------------
00032         ScriptLoader::~ScriptLoader()
00033         {
00034 
00035         }
00036 
00037         //----------------------------------------------------------------------------------
00038         Result ScriptLoader::initializeResourceLoader()
00039         {
00040                 // fill out supported resource types;
00041                 declareSupportedResourceType("Script");
00042                 declareSupportedResourceType("Config");
00043                 declareSupportedResourceType("nrScript");
00044 
00045                 // we do only support dll files in windows version of our engine
00046                 declareSupportedFileType("script");
00047                 declareSupportedFileType("nrscript");
00048                 declareSupportedFileType("cfg");
00049 
00050                 // now delcare mapping of the types
00051                 declareTypeMap("nrscript", "nrScript");
00052                 declareTypeMap("script", "nrScript");
00053                 declareTypeMap("cfg", "Config");
00054 
00055                 return OK;
00056         }
00057         
00058         //----------------------------------------------------------------------------------
00059         Result ScriptLoader::loadResource(IResource* res, const std::string& fileName, PropertyList* param)
00060         {
00061                 // load a file so we use its content as a script
00062                 FileStream* fStream = new FileStream();
00063                 Result ret = fStream->open(fileName);
00064                 if (ret == OK){
00065 
00066                         // get the whole file content
00067                         std::string str = fStream->getAsString();
00068 
00069                         // cast the resource to the iscript interface
00070                         IScript* scr = dynamic_cast<IScript*>(res);
00071 
00072                         // load the script from a string
00073                         ret = scr->loadFromString(str);
00074 
00075                 }
00076                 delete fStream;
00077 
00078                 // return the last error
00079                 return ret;
00080         }
00081 
00082 
00083         //----------------------------------------------------------------------------------
00084         IResource* ScriptLoader::createResource(const std::string& resourceType, PropertyList* params)
00085         {
00086                 // check if we want to create just a script
00087                 if (resourceType == "Script" || resourceType == "nrScript")
00088                         return new Script();
00089                 else if (resourceType == "Config"){
00090                         // config scripts does run completely not stepwise
00091                         Script* scr = new Script();
00092                         scr->setRunStepwise(false);
00093                         return scr;
00094                 }
00095                 return NULL;
00096         }
00097 
00098 
00099         //----------------------------------------------------------------------------------
00100         IResource* ScriptLoader::createEmptyResource(const std::string& resourceType)
00101         {
00102                 // create an instance of empty plugin
00103                 return new EmptyScript(resourceType);
00104         }
00105 
00106 
00107 };
00108 

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