// *************************************************************** // Function: ScriptManager.cpp // *************************************************************** #include "ScriptManager.h" #include "../../src/dirent.h" #include #include using namespace std; //************************************ // Name: ScriptManager // // Class constructor // // Parameters: // pGwlplog - Pointer to a GWLPLog // // Return: // //************************************ ScriptManager::ScriptManager(GWLPLog* pGwlplog) { mpVirtualMachine=new gmMachine; mpLog=pGwlplog; } //************************************ // Name: ~ScriptManager // // Class deconstructor // // Parameters: // // Return: // //************************************ ScriptManager::~ScriptManager() { if (mpVirtualMachine!=NULL) { delete mpVirtualMachine; } std::list::iterator itr; for (itr=mModules.begin(); itr!=mModules.end(); itr++) { delete *itr; } } //************************************ // Name: GetVM // // Accesses the script virtual machine. // // Parameters: // // Return: // - Pointer to the gmMachine for the script manager //************************************ gmMachine* ScriptManager::GetVM() { return this->mpVirtualMachine; } //************************************ // Name: AddModule // // Adds a script module to be managed by the script manager. // // Parameters: // pMod - Pointer to a script module // // Return: // //************************************ void ScriptManager::AddModule(ScriptModule* pMod) { mModules.push_back(pMod); } //************************************ // Name: ReloadScriptModule // // Reloads the scripts within a script module. // // Parameters: // pModuleName - Script module name // // Return: // 0 - ok // -1 - errors //************************************ int ScriptManager::ReloadScriptModule(char* pModuleName) { std::list::iterator itr; for (itr=mModules.begin(); itr!=mModules.end(); itr++) { if (strcmp(pModuleName, (*itr)->mpModuleName)==0) { return (*itr)->Reload(); } } return -1; } //************************************ // Name: CompileScript // // Compiles a game monkey script file. // // Parameters: // pScript - Pointer to start of script data // pFilename - Filename of script // // Return: // 0 - compilation successful // -1 - compilation failed //************************************ int ScriptManager::CompileScript(unsigned char* pScript, char* pFilename) { if (mpVirtualMachine->ExecuteString((char*)pScript)!=0) { gmLog &gmlog = mpVirtualMachine->GetLog(); // Get the first error bool first_error = true; const char *pError = gmlog.GetEntry(first_error); mpLog->Write(OPT_ALWAYS_LEVEL | OPT_C5, "-----------------------------------------------------------\n"); mpLog->error("%s - Failed to Compile", pFilename); while ( pError ) { mpLog->debug("[*] %s", pError); pError = gmlog.GetEntry( first_error ); } mpLog->Write(OPT_ALWAYS_LEVEL, "Code Dump: \n"); mpLog->Write(OPT_ALWAYS_LEVEL, (char*)pScript); mpLog->Write(OPT_ALWAYS_LEVEL | OPT_NEWLINE, ""); return -1; } mpLog->notice("%s - Compiled Successfully", pFilename); return 0; } //---------------------------------Script Module----------------------------// //************************************ // Name: ScriptModule // // Script module constructor. // // Parameters: // pName - Script module name // pScriptMan - Script manager to use // // Return: // //************************************ ScriptModule::ScriptModule(char* pName, void* pScriptMan) { this->mpModuleName=pName; this->mpScriptMan=pScriptMan; this->reload_clean=NULL; } //************************************ // Name: SetReloadClean // // Set the function to call when reloading scripts. This enables cleanup of custom handling. // // Parameters: // rc - Pointer to cleanup function // // Return: // //************************************ int ScriptModule::SetReloadClean(int (*rc)(void)) { this->reload_clean=rc; return 0; } //************************************ // Name: Reload // // Reloads all scripts in the module. Calls the custom cleaning function first if needed. // // Parameters: // // Return: // 0 - ok // <0 - errors //************************************ int ScriptModule::Reload() { if (reload_clean!=NULL) { reload_clean(); } return ProcessFiles(mpDirectoryPath, process_func); } //************************************ // Name: LoadDir // // Loads all script files in the specified directory and subdirectories. // // Parameters: // pDir - Directory path // LoadProcessing - Function pointer for load processing if needed // // Return: // 0 - ok // <0 - errors //************************************ int ScriptModule::LoadDir(char* pDir, int (*LoadProcessing)(char* filename)) { ((ScriptManager*)mpScriptMan)->mpLog->notice("Loading Directory: %s", pDir); ((ScriptManager*)mpScriptMan)->mpLog->Write(OPT_ALWAYS_LEVEL | OPT_NEWLINE, ""); this->mpDirectoryPath=pDir; this->process_func=LoadProcessing; return ProcessFiles(pDir, LoadProcessing); } //************************************ // Name: ProcessFiles // // Loads specified path and handles all the require processing. (Internal only) // // Parameters: // pDirName - Document // LoadProcessing - Document // // Return: // //************************************ int ScriptModule::ProcessFiles(char* pDirName, int (*LoadProcessing)(char* filename)) { DIR* pDir=NULL; struct dirent* pDirent=NULL; pDir=opendir(pDirName); if (pDir==NULL) { return -1; } //traverse through all the files in the directory, loading them while((pDirent=readdir(pDir))) { if (pDirent->d_name[0]!='.') { //try to open the file char fn[300]; strcpy(fn, pDirName); strcat(fn, pDirent->d_name); assert(strlen(fn)<290); ifstream fin(fn, ios::binary); // in Linux this won't fail if its a directory *REVISE* if (fin.fail()) { //try and load this file as a directory strcat(fn, "/"); if (ProcessFiles(fn, LoadProcessing)<0) { closedir(pDir); return -2; } } else { //calculate the file size int begin=fin.tellg(); fin.seekg(0, ios::end); int end=fin.tellg(); int size=(end-begin); #ifndef WIN32 //under Linux this is probably a directory if (size == 0) { fin.close(); //try and load this file as a directory strcat(fn, "/"); if (ProcessFiles(fn, LoadProcessing)<0) { return -2; } } #endif fin.seekg(0, ios::beg); //Load the file unsigned char* pFileData=new unsigned char[size+1]; assert(pFileData!=NULL); memset(pFileData, 0, size+1); for (int i=0; iCompileScript(pFileData, fn)==0) { //Process the loaded data if (LoadProcessing!=NULL) { if (LoadProcessing(strrchr(fn, '/')+1)!=0) { ((ScriptManager*)mpScriptMan)->mpLog->error("File %s failed in custom load processing function.", fn); } } } //Free the memory used by the script delete [] pFileData; //close the file and output success fin.close(); } } } //close the directory closedir(pDir); return 0; }