#include "uox3.h" #include "CJSMapping.h" #include "cScript.h" #include "scriptc.h" #include "ssection.h" #include "StringUtility.hpp" #include using namespace std::string_literals; CJSMapping *JSMapping = nullptr; //o-----------------------------------------------------------------------------------------------o //| File - CJSMapping.cpp //| Date - Feb 7, 2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - JavaScript File Mapping //o-----------------------------------------------------------------------------------------------o //| Changes - Version History //| 1.0 Feb 7, 2005 //| Initial Implementation //| //| 1.1 Feb 8, 2005 //| Added the ability to reload JS files on a per-script basis //| //| 1.2 Feb 28, 2005 //| Added the ability to reload JS files on a per-section basis //o-----------------------------------------------------------------------------------------------o //o-----------------------------------------------------------------------------------------------o //| Class - CJSMapping //| Date - 2/7/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Base global class that holds within it an array of JS Mapping Sections //o-----------------------------------------------------------------------------------------------o /* CJSMapping::CJSMapping() { // But it doesnt ? //Console.print( "Loading JS Scripts\n" ); } */ CJSMapping::~CJSMapping() { //Cleanup(); } //o-----------------------------------------------------------------------------------------------o //| Function - void ResetDefaults( void ) //| Date - 2/7/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Resets all parameters of the CJSMapping class to default //o-----------------------------------------------------------------------------------------------o void CJSMapping::ResetDefaults( void ) { for( size_t i = SCPT_NORMAL; i < SCPT_COUNT; ++i ) { mapSection[i] = new CJSMappingSection( static_cast(i) ); } envokeByID = new CEnvoke( "object" ); envokeByType = new CEnvoke( "type" ); Parse(); } //o-----------------------------------------------------------------------------------------------o //| Function - void Cleanup( void ) //| Date - 2/7/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Frees all memory used by CJSMapping //o-----------------------------------------------------------------------------------------------o void CJSMapping::Cleanup( void ) { for( size_t i = SCPT_NORMAL; i < SCPT_COUNT; ++i ) { if( mapSection[1] != nullptr ) { delete mapSection[i]; mapSection[i] = nullptr; } } if( envokeByID != nullptr ) { delete envokeByID; envokeByID = nullptr; } if( envokeByType != nullptr ) { delete envokeByType; envokeByType = nullptr; } } //o-----------------------------------------------------------------------------------------------o //| Function - void Reload( UI16 scriptID ) //| Date - 2/7/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Reloads the JS Scripts //o-----------------------------------------------------------------------------------------------o void CJSMapping::Reload( UI16 scriptID ) { if( scriptID != 0xFFFF ) { Console.print( oldstrutil::format("CMD: Attempting Reload of JavaScript (ScriptID %u)\n", scriptID) ); for( size_t i = SCPT_NORMAL; i < SCPT_COUNT; ++i ) { if( mapSection[i]->IsInMap( scriptID ) ) { mapSection[i]->Reload( scriptID ); return; } } Console.warning( oldstrutil::format("Unable to locate specified JavaScript in the map (ScriptID %u)", scriptID) ); } else { Console.print( oldstrutil::format("CMD: Loading JSE Scripts... \n") ); Cleanup(); ResetDefaults(); envokeByID->Parse(); envokeByType->Parse(); } } //o-----------------------------------------------------------------------------------------------o //| Function - void Reload( SCRIPTTYPE sectionID ) //| Date - 2/28/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Reloads a specific section of the JS Scripts //o-----------------------------------------------------------------------------------------------o void CJSMapping::Reload( SCRIPTTYPE sectionID ) { Console.print( oldstrutil::format("CMD: Attempting Reload of JavaScript (SectionID %u)\n", static_cast(sectionID) )); if( mapSection[sectionID] != nullptr ) { delete mapSection[sectionID]; mapSection[sectionID] = new CJSMappingSection( sectionID ); Parse( sectionID ); } } //o-----------------------------------------------------------------------------------------------o //| Function - void Parse( SCRIPTTYPE toParse ) //| Date - 2/7/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Parses through jse_fileassociations.scp doling out the work //| to each CJSMappingSection Parse() routine. //o-----------------------------------------------------------------------------------------------o void CJSMapping::Parse( SCRIPTTYPE toParse ) { std::string scpFileName = cwmWorldState->ServerData()->Directory( CSDDP_SCRIPTS ) + "jse_fileassociations.scp"; if( !FileExists( scpFileName ) ) { Console.error( oldstrutil::format("Failed to open %s", scpFileName.c_str() )); return; } Script *fileAssocData = new Script( scpFileName, NUM_DEFS, false ); if( fileAssocData != nullptr ) { if( toParse != SCPT_COUNT ) { if( mapSection[toParse] != nullptr ) mapSection[toParse]->Parse( fileAssocData ); } else { for( size_t i = SCPT_NORMAL; i < SCPT_COUNT; ++i ) { if( mapSection[i] != nullptr ) mapSection[i]->Parse( fileAssocData ); } } delete fileAssocData; } } //o-----------------------------------------------------------------------------------------------o //| Function - CJSMappingSection * GetSection( SCRIPTTYPE toGet ) //| Date - 2/7/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Returns a pointer to the specified JSMappingSection //o-----------------------------------------------------------------------------------------------o CJSMappingSection * CJSMapping::GetSection( SCRIPTTYPE toGet ) { if( mapSection[toGet] != nullptr ) return mapSection[toGet]; return nullptr; } //o-----------------------------------------------------------------------------------------------o //| Function - UI16 GetScriptID( JSObject *toFind ) //| Date - 2/7/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Returns the scriptID relating to the specified JSObject //o-----------------------------------------------------------------------------------------------o UI16 CJSMapping::GetScriptID( JSObject *toFind ) { UI16 retVal = 0xFFFF; for( size_t i = SCPT_NORMAL; i < SCPT_COUNT; ++i ) { retVal = mapSection[i]->GetScriptID( toFind ); if( retVal != 0xFFFF ) break; } return retVal; } //o-----------------------------------------------------------------------------------------------o //| Function - cScript * GetScript( JSObject *toFind ) //| Date - 2/7/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Returns the cScript relating to the specified JSObject //o-----------------------------------------------------------------------------------------------o cScript * CJSMapping::GetScript( JSObject *toFind ) { cScript *retVal = nullptr; cScript *toCheck = nullptr; for( size_t i = SCPT_NORMAL; i < SCPT_COUNT; ++i ) { toCheck = mapSection[i]->GetScript( toFind ); if( toCheck != nullptr ) { retVal = toCheck; break; } } return retVal; } //o-----------------------------------------------------------------------------------------------o //| Function - cScript * GetScript( UI16 toFind ) //| Date - 2/7/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Returns the cScript relating to the specified scriptID //o-----------------------------------------------------------------------------------------------o cScript * CJSMapping::GetScript( UI16 toFind ) { cScript *retVal = nullptr; cScript *toCheck = nullptr; for( size_t i = SCPT_NORMAL; i < SCPT_COUNT; ++i ) { toCheck = mapSection[i]->GetScript( toFind ); if( toCheck != nullptr ) { retVal = toCheck; break; } } return retVal; } //o-----------------------------------------------------------------------------------------------o //| Function - CEnvoke * GetEnvokeByID( void ) //| Date - 2/7/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Returns a pointer to the CEnvoke class handling EnvokeByID //o-----------------------------------------------------------------------------------------------o CEnvoke * CJSMapping::GetEnvokeByID( void ) { return envokeByID; } //o-----------------------------------------------------------------------------------------------o //| Function - CEnvoke * GetEnvokeByType( void ) //| Date - 2/7/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Returns a pointer to the CEnvoke class handling EnvokeByType //o-----------------------------------------------------------------------------------------------o CEnvoke * CJSMapping::GetEnvokeByType( void ) { return envokeByType; } //o-----------------------------------------------------------------------------------------------o //| Class - CJSMappingSection( SCRIPTTYPE sT ) //| Date - 2/7/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Class containing the specified section of jse_fileassociations.scp //o-----------------------------------------------------------------------------------------------o CJSMappingSection::CJSMappingSection( SCRIPTTYPE sT ) { scriptType = sT; scriptIDMap.clear(); scriptJSMap.clear(); scriptIDIter = scriptIDMap.end(); } CJSMappingSection::~CJSMappingSection() { for( std::map< UI16, cScript * >::const_iterator sIter = scriptIDMap.begin(); sIter != scriptIDMap.end(); ++sIter ) { cScript *toDelete = sIter->second; if( toDelete != nullptr ) delete toDelete; } scriptIDMap.clear(); scriptJSMap.clear(); } //o-----------------------------------------------------------------------------------------------o //| Function - void Parse( Script *fileAssocData ) //| Date - 2/7/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Parses through specified section of jse_fileassociations.scp //| populating the map with each entry. //o-----------------------------------------------------------------------------------------------o auto CJSMappingSection::Parse( Script *fileAssocData )->void{ auto basePath = cwmWorldState->ServerData()->Directory( CSDDP_SCRIPTS ); auto mSection = fileAssocData->FindEntry( ScriptNames[scriptType] ); UI08 runTime = 0; if( scriptType == SCPT_CONSOLE ){ runTime = 1; } if(mSection) { UI16 scriptID = 0xFFFF; size_t i = 0; for (const auto &sec :mSection->collection()){ auto tag = sec->tag ; auto data = sec->data; scriptID = static_cast(std::stoul(tag, nullptr, 0)); auto fullPath = basePath + data; if( !FileExists( fullPath ) ) Console.error( oldstrutil::format("SE mapping of %i to %s failed, file does not exist!", scriptID, data.c_str() )); else { try { auto toAdd = new cScript( fullPath, runTime ); if(toAdd) { scriptIDMap[scriptID] = toAdd; scriptJSMap[toAdd->Object()] = scriptID; ++i; } } catch( std::runtime_error &e ) { Console.error( oldstrutil::format("Compiling %s caused a construction failure (Details: %s)", fullPath.c_str(), e.what()) ); } } } Console.print( " o Loaded " ); Console.TurnYellow(); Console.print( oldstrutil::format("%4u ", i) ); Console.TurnNormal(); Console.print( "scripts from section " ); Console.TurnYellow(); Console.print( oldstrutil::format("%s\n", ScriptNames[scriptType].c_str()) ); Console.TurnNormal(); } else{ Console.warning( oldstrutil::format("No JS file mappings found in section %s", ScriptNames[scriptType].c_str()) ); } } //o-----------------------------------------------------------------------------------------------o //| Function - void Reload( UI16 toLoad ) //| Date - 2/8/2005 //o-----------------------------------------------------------------------------------------------o //| Purpose - Reloads the specified JS file (by its scriptID) //o-----------------------------------------------------------------------------------------------o auto CJSMappingSection::Reload( UI16 toLoad ) ->void { auto scpFileName = cwmWorldState->ServerData()->Directory( CSDDP_SCRIPTS ) + "jse_fileassociations.scp"s; if( FileExists( scpFileName ) ) { auto fileAssocData = std::make_unique