From 8d786cf907423ceae5e85419f12b0b2e7407add6 Mon Sep 17 00:00:00 2001 From: Miran Date: Sun, 1 Dec 2024 03:00:43 +0100 Subject: [PATCH] Removed GetInstance() in favor of straight CleoInstance use. Added _Orig suffix to all variables storing original hook addresses. --- source/CCodeInjector.h | 14 +++---- source/CCustomOpcodeSystem.cpp | 74 +++++++++++++++++----------------- source/CDebug.cpp | 5 +-- source/CDmaFix.cpp | 2 +- source/CGameMenu.cpp | 8 ++-- source/CPluginSystem.cpp | 6 +-- source/CScriptEngine.cpp | 66 +++++++++++++++--------------- source/CleoBase.cpp | 73 +++++++++++++++------------------ source/CleoBase.h | 16 ++++---- source/dllmain.cpp | 6 +-- 10 files changed, 128 insertions(+), 142 deletions(-) diff --git a/source/CCodeInjector.h b/source/CCodeInjector.h index 0cafb842..8567cab6 100644 --- a/source/CCodeInjector.h +++ b/source/CCodeInjector.h @@ -35,17 +35,13 @@ namespace CLEO bool bAccessOpen; public: - CCodeInjector() { + CCodeInjector() + { bAccessOpen = false; - - // moved here so that access is open for plugins - OpenReadWriteAccess(); // we might as well leave it open, too - //GetInstance().Start(); + OpenReadWriteAccess(); // moved here so that access is open for plugins } - ~CCodeInjector() - { - //GetInstance().Stop(); - }; + + ~CCodeInjector() = default; void OpenReadWriteAccess(); void CloseReadWriteAccess(); diff --git a/source/CCustomOpcodeSystem.cpp b/source/CCustomOpcodeSystem.cpp index 0349987f..0db0f2ba 100644 --- a/source/CCustomOpcodeSystem.cpp +++ b/source/CCustomOpcodeSystem.cpp @@ -107,7 +107,7 @@ namespace CLEO // execute registered callbacks OpcodeResult result = OR_NONE; - for (void* func : GetInstance().GetCallbacks(eCallbackId::ScriptOpcodeProcess)) + for (void* func : CleoInstance.GetCallbacks(eCallbackId::ScriptOpcodeProcess)) { typedef OpcodeResult WINAPI callback(CRunningScript*, DWORD); result = ((callback*)func)(thread, opcode); @@ -135,7 +135,7 @@ namespace CLEO if (opcode > LastOriginalOpcode) { - auto extensionMsg = GetInstance().OpcodeInfoDb.GetExtensionMissingMessage(opcode); + auto extensionMsg = CleoInstance.OpcodeInfoDb.GetExtensionMissingMessage(opcode); if (!extensionMsg.empty()) extensionMsg = " " + extensionMsg; SHOW_ERROR("Custom opcode [%04X] not registered!%s\nCalled in script %s\nPreviously called opcode: [%04X]\nScript suspended.", @@ -152,7 +152,7 @@ namespace CLEO if(result == OR_ERROR) { - auto extensionMsg = GetInstance().OpcodeInfoDb.GetExtensionMissingMessage(opcode); + auto extensionMsg = CleoInstance.OpcodeInfoDb.GetExtensionMissingMessage(opcode); if (!extensionMsg.empty()) extensionMsg = " " + extensionMsg; SHOW_ERROR("Opcode [%04X] not found!%s\nCalled in script %s\nPreviously called opcode: [%04X]\nScript suspended.", @@ -167,7 +167,7 @@ namespace CLEO // execute registered callbacks OpcodeResult callbackResult = OR_NONE; - for (void* func : GetInstance().GetCallbacks(eCallbackId::ScriptOpcodeProcessFinished)) + for (void* func : CleoInstance.GetCallbacks(eCallbackId::ScriptOpcodeProcessFinished)) { typedef OpcodeResult WINAPI callback(CRunningScript*, DWORD, OpcodeResult); auto res = ((callback*)func)(thread, opcode, result); @@ -182,7 +182,7 @@ namespace CLEO { TRACE("Cleaning up script data..."); - GetInstance().CallCallbacks(eCallbackId::ScriptsFinalize); + CleoInstance.CallCallbacks(eCallbackId::ScriptsFinalize); // clean up after opcode_0AB1 ScmFunction::Clear(); @@ -191,7 +191,7 @@ namespace CLEO void CCustomOpcodeSystem::Inject(CCodeInjector& inj) { TRACE("Injecting CustomOpcodeSystem..."); - CGameVersionManager& gvm = GetInstance().VersionManager; + CGameVersionManager& gvm = CleoInstance.VersionManager; // replace all handlers in original table // store original opcode handlers for later use @@ -810,7 +810,7 @@ namespace CLEO OpcodeResult __stdcall CCustomOpcodeSystem::opcode_004E(CRunningScript* thread) { - GetInstance().ScriptEngine.RemoveScript(thread); + CleoInstance.ScriptEngine.RemoveScript(thread); return OR_INTERRUPT; } @@ -819,7 +819,7 @@ namespace CLEO if (thread->SP == 0 && !IsLegacyScript(thread)) // CLEO5 - allow use of GOSUB `return` to exit cleo calls too { SetScriptCondResult(thread, false); - return GetInstance().OpcodeSystem.CleoReturnGeneric(0x0051, thread, false); // try CLEO's function return + return CleoInstance.OpcodeSystem.CleoReturnGeneric(0x0051, thread, false); // try CLEO's function return } if (thread->SP == 0) @@ -851,7 +851,7 @@ namespace CLEO SetScriptCondResult(thread, cs && cs->IsOK()); if (cs && cs->IsOK()) { - GetInstance().ScriptEngine.AddCustomScript(cs); + CleoInstance.ScriptEngine.AddCustomScript(cs); TransmitScriptParams(thread, cs); cs->SetDebugMode(reinterpret_cast(thread)->GetDebugMode()); } @@ -875,7 +875,7 @@ namespace CLEO return OR_CONTINUE; // legacy behavior } - GetInstance().ScriptEngine.RemoveScript(thread); + CleoInstance.ScriptEngine.RemoveScript(thread); return OR_INTERRUPT; } @@ -895,7 +895,7 @@ namespace CLEO auto csscript = reinterpret_cast(thread); if (csscript->IsCustom()) cs->SetCompatibility(csscript->GetCompatibility()); - GetInstance().ScriptEngine.AddCustomScript(cs); + CleoInstance.ScriptEngine.AddCustomScript(cs); memset(missionLocals, 0, 1024 * sizeof(SCRIPT_VAR)); // same as CTheScripts::WipeLocalVariableMemoryForMissionScript TransmitScriptParams(thread, (CRunningScript*)((BYTE*)missionLocals - 0x3C)); cs->SetDebugMode(reinterpret_cast(thread)->GetDebugMode()); @@ -939,7 +939,7 @@ namespace CLEO //0AA9=0, is_game_version_original OpcodeResult __stdcall opcode_0AA9(CRunningScript *thread) { - auto gv = GetInstance().VersionManager.GetGameVersion(); + auto gv = CleoInstance.VersionManager.GetGameVersion(); auto cs = (CCustomScript*)thread; SetScriptCondResult(thread, gv == GV_US10 || (cs->IsCustom() && cs->GetCompatibility() <= CLEO_VER_4_MIN && gv == GV_EU10)); return OR_CONTINUE; @@ -1000,7 +1000,7 @@ namespace CLEO modulePath = reinterpret_cast(thread)->ResolvePath(modulePath.c_str(), DIR_SCRIPT); // by default search relative to current script location // get export reference - auto scriptRef = GetInstance().ModuleSystem.GetExport(modulePath, strExport); + auto scriptRef = CleoInstance.ModuleSystem.GetExport(modulePath, strExport); if (!scriptRef.Valid()) { SHOW_ERROR("Not found module '%s' export '%s', requested by opcode [0AB1] in script %s", modulePath.c_str(), moduleTxt.c_str(), ((CCustomScript*)thread)->GetInfoStr().c_str()); @@ -1129,7 +1129,7 @@ namespace CLEO returnParamCount = declaredParamCount; } - return GetInstance().OpcodeSystem.CleoReturnGeneric(0x0AB2, thread, true, returnParamCount, !IsLegacyScript(thread)); + return CleoInstance.OpcodeSystem.CleoReturnGeneric(0x0AB2, thread, true, returnParamCount, !IsLegacyScript(thread)); } //0AB3=2,set_cleo_shared_var %1d% = %2d% @@ -1154,7 +1154,7 @@ namespace CLEO } GetScriptParams(thread, 1); - GetInstance().ScriptEngine.CleoVariables[varIdx].dwParam = opcodeParams[0].dwParam; + CleoInstance.ScriptEngine.CleoVariables[varIdx].dwParam = opcodeParams[0].dwParam; return OR_CONTINUE; } @@ -1177,7 +1177,7 @@ namespace CLEO return thread->Suspend(); } - opcodeParams[0].dwParam = GetInstance().ScriptEngine.CleoVariables[varIdx].dwParam; + opcodeParams[0].dwParam = CleoInstance.ScriptEngine.CleoVariables[varIdx].dwParam; CLEO_RecordOpcodeParams(thread, 1); return OR_CONTINUE; } @@ -1220,7 +1220,7 @@ namespace CLEO OpcodeResult __stdcall opcode_0AB6(CRunningScript *thread) { // steam offset is different, so get it manually for now - CGameVersionManager& gvm = GetInstance().VersionManager; + CGameVersionManager& gvm = CleoInstance.VersionManager; DWORD hMarker = gvm.GetGameVersion() != GV_STEAM ? MenuManager->m_nTargetBlipIndex : *((DWORD*)0xC3312C); CMarker *pMarker; if (hMarker && (pMarker = &RadarBlips[LOWORD(hMarker)]) && /*pMarker->m_nPoolIndex == HIWORD(hMarker) && */pMarker->m_nBlipDisplay) @@ -1533,7 +1533,7 @@ namespace CLEO argCount--; SetScriptCondResult(thread, result != 0); - return GetInstance().OpcodeSystem.CleoReturnGeneric(0x2002, thread, true, argCount); + return CleoInstance.OpcodeSystem.CleoReturnGeneric(0x2002, thread, true, argCount); } //2003=-1, cleo_return_fail @@ -1547,7 +1547,7 @@ namespace CLEO } SetScriptCondResult(thread, false); - return GetInstance().OpcodeSystem.CleoReturnGeneric(0x2003, thread); + return CleoInstance.OpcodeSystem.CleoReturnGeneric(0x2003, thread); } } @@ -1571,7 +1571,7 @@ extern "C" BOOL WINAPI CLEO_RegisterCommand(const char* commandName, CustomOpcodeHandler callback) { - WORD opcode = GetInstance().OpcodeInfoDb.GetOpcode(commandName); + WORD opcode = CleoInstance.OpcodeInfoDb.GetOpcode(commandName); if (opcode == 0xFFFF) { LOG_WARNING(0, "Failed to register opcode [%s]! Command name not found in the database.", commandName); @@ -1657,14 +1657,14 @@ extern "C" // store state auto param = opcodeParams[0]; auto ip = thread->CurrentIP; - auto count = GetInstance().OpcodeSystem.handledParamCount; + auto count = CleoInstance.OpcodeSystem.handledParamCount; GetScriptParams(thread, 1); DWORD result = opcodeParams[0].dwParam; // restore state thread->CurrentIP = ip; - GetInstance().OpcodeSystem.handledParamCount = count; + CleoInstance.OpcodeSystem.handledParamCount = count; opcodeParams[0] = param; return result; @@ -1675,14 +1675,14 @@ extern "C" // store state auto param = opcodeParams[0]; auto ip = thread->CurrentIP; - auto count = GetInstance().OpcodeSystem.handledParamCount; + auto count = CleoInstance.OpcodeSystem.handledParamCount; GetScriptParams(thread, 1); float result = opcodeParams[0].fParam; // restore state thread->CurrentIP = ip; - GetInstance().OpcodeSystem.handledParamCount = count; + CleoInstance.OpcodeSystem.handledParamCount = count; opcodeParams[0] = param; return result; @@ -1692,13 +1692,13 @@ extern "C" { // store state auto ip = thread->CurrentIP; - auto count = GetInstance().OpcodeSystem.handledParamCount; + auto count = CleoInstance.OpcodeSystem.handledParamCount; auto result = GetScriptParamPointer(thread); // restore state thread->CurrentIP = ip; - GetInstance().OpcodeSystem.handledParamCount = count; + CleoInstance.OpcodeSystem.handledParamCount = count; return result; } @@ -1710,7 +1710,7 @@ extern "C" DWORD WINAPI CLEO_GetParamsHandledCount() { - return GetInstance().OpcodeSystem.handledParamCount; + return CleoInstance.OpcodeSystem.handledParamCount; } void WINAPI CLEO_WriteStringOpcodeParam(CLEO::CRunningScript* thread, const char* str) @@ -1789,7 +1789,7 @@ extern "C" } } - GetInstance().OpcodeSystem.handledParamCount += count; + CleoInstance.OpcodeSystem.handledParamCount += count; } void WINAPI CLEO_SkipUnusedVarArgs(CLEO::CRunningScript* thread) @@ -1804,7 +1804,7 @@ extern "C" void WINAPI CLEO_TerminateScript(CLEO::CRunningScript* thread) { - GetInstance().ScriptEngine.RemoveScript(thread); + CleoInstance.ScriptEngine.RemoveScript(thread); } int WINAPI CLEO_GetOperandType(const CLEO::CRunningScript* thread) @@ -1855,12 +1855,12 @@ extern "C" if (fromThread) SetScriptCondResult(fromThread, cs && cs->IsOK()); if (cs && cs->IsOK()) { - GetInstance().ScriptEngine.AddCustomScript(cs); + CleoInstance.ScriptEngine.AddCustomScript(cs); if (fromThread) TransmitScriptParams(fromThread, cs); cs->SetDebugMode(fromThread ? reinterpret_cast(fromThread)->GetDebugMode() : // from parent - GetInstance().ScriptEngine.NativeScriptsDebugMode); // global + CleoInstance.ScriptEngine.NativeScriptsDebugMode); // global } else { @@ -1875,17 +1875,17 @@ extern "C" CLEO::CRunningScript* WINAPI CLEO_GetLastCreatedCustomScript() { - return GetInstance().ScriptEngine.LastScriptCreated; + return CleoInstance.ScriptEngine.LastScriptCreated; } CLEO::CRunningScript* WINAPI CLEO_GetScriptByName(const char* threadName, BOOL standardScripts, BOOL customScripts, DWORD resultIndex) { - return GetInstance().ScriptEngine.FindScriptNamed(threadName, standardScripts, customScripts, resultIndex); + return CleoInstance.ScriptEngine.FindScriptNamed(threadName, standardScripts, customScripts, resultIndex); } CLEO::CRunningScript* WINAPI CLEO_GetScriptByFilename(const char* path, DWORD resultIndex) { - return GetInstance().ScriptEngine.FindScriptByFilename(path, resultIndex); + return CleoInstance.ScriptEngine.FindScriptByFilename(path, resultIndex); } void WINAPI CLEO_AddScriptDeleteDelegate(FuncScriptDeleteDelegateT func) @@ -1910,7 +1910,7 @@ extern "C" BOOL WINAPI CLEO_IsScriptRunning(const CLEO::CRunningScript* thread) { - return GetInstance().ScriptEngine.IsActiveScriptPtr(thread); + return CleoInstance.ScriptEngine.IsActiveScriptPtr(thread); } void WINAPI CLEO_GetScriptInfoStr(CLEO::CRunningScript* thread, bool currLineInfo, char* buf, DWORD bufSize) @@ -1930,8 +1930,8 @@ extern "C" void WINAPI CLEO_GetScriptParamInfoStr(int idexOffset, char* buf, DWORD bufSize) { - auto curr = idexOffset - 1 + GetInstance().OpcodeSystem.handledParamCount; - auto name = GetInstance().OpcodeInfoDb.GetArgumentName(GetInstance().OpcodeSystem.lastOpcode, curr); + auto curr = idexOffset - 1 + CleoInstance.OpcodeSystem.handledParamCount; + auto name = CleoInstance.OpcodeInfoDb.GetArgumentName(CleoInstance.OpcodeSystem.lastOpcode, curr); curr++; // 1-based argument index display diff --git a/source/CDebug.cpp b/source/CDebug.cpp index 4c57e443..e365033c 100644 --- a/source/CDebug.cpp +++ b/source/CDebug.cpp @@ -18,10 +18,9 @@ void CDebug::Trace(CLEO::eLogLevel level, const char* msg) #endif // output to callbacks - auto& cleo = GetInstance(); - if (cleo.IsStarted()) + if (CleoInstance.IsStarted()) { - for (void* func : cleo.GetCallbacks(eCallbackId::Log)) + for (void* func : CleoInstance.GetCallbacks(eCallbackId::Log)) { typedef void WINAPI callback(eLogLevel, const char*); ((callback*)func)(level, msg); diff --git a/source/CDmaFix.cpp b/source/CDmaFix.cpp index d09d044e..6bde6256 100644 --- a/source/CDmaFix.cpp +++ b/source/CDmaFix.cpp @@ -9,7 +9,7 @@ namespace CLEO void CDmaFix::Inject(CCodeInjector& inj) { TRACE("Injecting DmaFix..."); - CGameVersionManager& gvm = GetInstance().VersionManager; + CGameVersionManager& gvm = CleoInstance.VersionManager; switch (gvm.GetGameVersion()) { case GV_EU10: diff --git a/source/CGameMenu.cpp b/source/CGameMenu.cpp index dbc2666b..727d6410 100644 --- a/source/CGameMenu.cpp +++ b/source/CGameMenu.cpp @@ -24,7 +24,7 @@ namespace CLEO void __fastcall OnDrawMenuBackground(void *texture, int dummy, RwRect2D *rect, RwRGBA *color) { - GetInstance().Start(CCleoInstance::InitStage::OnDraw); // late initialization + CleoInstance.Start(CCleoInstance::InitStage::OnDraw); // late initialization CTexture_DrawInRect(texture, rect, color); // call original @@ -47,8 +47,8 @@ namespace CLEO float posX = 25.0f * sizeX; // left margin float posY = RsGlobal.maximumHeight - 15.0f * sizeY; // bottom margin - auto cs_count = GetInstance().ScriptEngine.WorkingScriptsCount(); - auto plugin_count = GetInstance().PluginSystem.GetNumPlugins(); + auto cs_count = CleoInstance.ScriptEngine.WorkingScriptsCount(); + auto plugin_count = CleoInstance.PluginSystem.GetNumPlugins(); if (cs_count || plugin_count) { posY -= 15.0f * sizeY; // add space for bottom text @@ -82,7 +82,7 @@ namespace CLEO void CGameMenu::Inject(CCodeInjector& inj) { TRACE("Injecting MenuStatusNotifier..."); - CGameVersionManager& gvm = GetInstance().VersionManager; + CGameVersionManager& gvm = CleoInstance.VersionManager; MenuManager = gvm.TranslateMemoryAddress(MA_MENU_MANAGER); inj.MemoryReadOffset(gvm.TranslateMemoryAddress(MA_CALL_CTEXTURE_DRAW_BG_RECT).address + 1, CTexture__DrawInRect, true); diff --git a/source/CPluginSystem.cpp b/source/CPluginSystem.cpp index f33fbc97..7988bf56 100644 --- a/source/CPluginSystem.cpp +++ b/source/CPluginSystem.cpp @@ -46,12 +46,12 @@ void CPluginSystem::LoadPlugins() { names.insert(name); paths.emplace_back(files.strings[i]); - TRACE(" - '%s'", files.strings[i]); + TRACE(" %s", files.strings[i]); } else { skippedPaths.emplace(files.strings[i]); - LOG_WARNING(0, " - '%s' skipped, duplicate of '%s' plugin", files.strings[i], name.c_str()); + LOG_WARNING(0, " %s - skipped, duplicate of '%s' plugin", files.strings[i], name.c_str()); } } @@ -90,7 +90,7 @@ void CPluginSystem::LoadPlugins() } else { - TRACE(" - nothing found"); + TRACE(" nothing found"); } pluginsLoaded = true; diff --git a/source/CScriptEngine.cpp b/source/CScriptEngine.cpp index f6635237..8db28faf 100644 --- a/source/CScriptEngine.cpp +++ b/source/CScriptEngine.cpp @@ -83,7 +83,7 @@ namespace CLEO call FUNC_GetScriptParams } - GetInstance().OpcodeSystem.handledParamCount += count; + CleoInstance.OpcodeSystem.handledParamCount += count; } void __fastcall _TransmitScriptParams(CRunningScript *pScript, int dummy, CRunningScript *pScriptB) @@ -105,7 +105,7 @@ namespace CLEO call FUNC_SetScriptParams } - GetInstance().OpcodeSystem.handledParamCount += count; + CleoInstance.OpcodeSystem.handledParamCount += count; } void __fastcall _SetScriptCondResult(CRunningScript *pScript, int dummy, int val) @@ -161,7 +161,7 @@ namespace CLEO } else if (paramType == DT_VARLEN_STRING) { - GetInstance().OpcodeSystem.handledParamCount++; + CleoInstance.OpcodeSystem.handledParamCount++; thread->IncPtr(1); // already processed paramType DWORD length = *thread->GetBytePointer(); // as unsigned byte! @@ -183,7 +183,7 @@ namespace CLEO { case DT_TEXTLABEL: { - GetInstance().OpcodeSystem.handledParamCount++; + CleoInstance.OpcodeSystem.handledParamCount++; memcpy(buff, str, min(buffLen, 8)); thread->IncPtr(8); // text data return buff; @@ -191,7 +191,7 @@ namespace CLEO case DT_STRING: { - GetInstance().OpcodeSystem.handledParamCount++; + CleoInstance.OpcodeSystem.handledParamCount++; memcpy(buff, str, min(buffLen, 16)); thread->IncPtr(16); // ext data return buff; @@ -237,7 +237,7 @@ namespace CLEO SCRIPT_VAR* GetScriptParamPointer(CRunningScript* thread) { SCRIPT_VAR* ptr = GetScriptParamPointer2(thread, 0); - GetInstance().OpcodeSystem.handledParamCount++; // TODO: hook game's GetScriptParamPointer1 and GetScriptParamPointer2 procedures so this is always incremented + CleoInstance.OpcodeSystem.handledParamCount++; // TODO: hook game's GetScriptParamPointer1 and GetScriptParamPointer2 procedures so this is always incremented return ptr; } @@ -270,7 +270,7 @@ namespace CLEO LPCSTR WINAPI CLEO_GetScriptFilename(const CRunningScript* thread) { - if (!GetInstance().ScriptEngine.IsValidScriptPtr(thread)) + if (!CleoInstance.ScriptEngine.IsValidScriptPtr(thread)) { return nullptr; } @@ -319,10 +319,10 @@ namespace CLEO void OnSaveScmData(void) { TRACE("Saving scripts save data..."); - GetInstance().ScriptEngine.SaveState(); - GetInstance().ScriptEngine.UnregisterAllScripts(); + CleoInstance.ScriptEngine.SaveState(); + CleoInstance.ScriptEngine.UnregisterAllScripts(); SaveScmData(); - GetInstance().ScriptEngine.ReregisterAllScripts(); + CleoInstance.ScriptEngine.ReregisterAllScripts(); } struct CleoSafeHeader @@ -402,11 +402,11 @@ namespace CLEO void __fastcall HOOK_ProcessScript(CCustomScript * pScript, int) { - GetInstance().ScriptEngine.GameBegin(); // all initialized and ready to process scripts + CleoInstance.ScriptEngine.GameBegin(); // all initialized and ready to process scripts // run registered callbacks bool process = true; - for (void* func : GetInstance().GetCallbacks(eCallbackId::ScriptProcess)) + for (void* func : CleoInstance.GetCallbacks(eCallbackId::ScriptProcess)) { typedef bool WINAPI callback(CRunningScript*); process = process && ((callback*)func)(pScript); @@ -422,7 +422,7 @@ namespace CLEO void HOOK_DrawScriptStuff(char bBeforeFade) { - GetInstance().ScriptEngine.DrawScriptStuff(bBeforeFade); + CleoInstance.ScriptEngine.DrawScriptStuff(bBeforeFade); if(bBeforeFade) DrawScriptStuff_H(bBeforeFade); @@ -430,7 +430,7 @@ namespace CLEO DrawScriptStuff(bBeforeFade); // run registered callbacks - for (void* func : GetInstance().GetCallbacks(eCallbackId::ScriptDraw)) + for (void* func : CleoInstance.GetCallbacks(eCallbackId::ScriptDraw)) { typedef void WINAPI callback(bool); ((callback*)func)(bBeforeFade != 0); @@ -581,7 +581,7 @@ namespace CLEO bool CCustomScript::GetDebugMode() const { if (!bIsCustom) - return GetInstance().ScriptEngine.NativeScriptsDebugMode; + return CleoInstance.ScriptEngine.NativeScriptsDebugMode; return bDebugMode; } @@ -589,7 +589,7 @@ namespace CLEO void CCustomScript::SetDebugMode(bool enabled) { if (!bIsCustom) - GetInstance().ScriptEngine.NativeScriptsDebugMode = enabled; + CleoInstance.ScriptEngine.NativeScriptsDebugMode = enabled; else bDebugMode = enabled; } @@ -597,7 +597,7 @@ namespace CLEO const char* CCustomScript::GetScriptFileDir() const { if(!bIsCustom) - return GetInstance().ScriptEngine.MainScriptFileDir.c_str(); + return CleoInstance.ScriptEngine.MainScriptFileDir.c_str(); return scriptFileDir.c_str(); } @@ -605,7 +605,7 @@ namespace CLEO void CCustomScript::SetScriptFileDir(const char* directory) { if (!bIsCustom) - GetInstance().ScriptEngine.MainScriptFileDir = directory; + CleoInstance.ScriptEngine.MainScriptFileDir = directory; else scriptFileDir = directory; } @@ -613,7 +613,7 @@ namespace CLEO const char* CCustomScript::GetScriptFileName() const { if (!bIsCustom) - return GetInstance().ScriptEngine.MainScriptFileName.c_str(); + return CleoInstance.ScriptEngine.MainScriptFileName.c_str(); return scriptFileName.c_str(); } @@ -621,7 +621,7 @@ namespace CLEO void CCustomScript::SetScriptFileName(const char* filename) { if (!bIsCustom) - GetInstance().ScriptEngine.MainScriptFileName = filename; + CleoInstance.ScriptEngine.MainScriptFileName = filename; else scriptFileName = filename; } @@ -637,7 +637,7 @@ namespace CLEO const char* CCustomScript::GetWorkDir() const { if (!bIsCustom) - return GetInstance().ScriptEngine.MainScriptCurWorkDir.c_str(); + return CleoInstance.ScriptEngine.MainScriptCurWorkDir.c_str(); return workDir.c_str(); } @@ -650,7 +650,7 @@ namespace CLEO auto resolved = ResolvePath(directory); if (!bIsCustom) - GetInstance().ScriptEngine.MainScriptCurWorkDir = resolved; + CleoInstance.ScriptEngine.MainScriptCurWorkDir = resolved; else workDir = resolved; } @@ -770,7 +770,7 @@ namespace CLEO ss << " - "; ss << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << CCustomOpcodeSystem::lastOpcode; - auto commandName = GetInstance().OpcodeInfoDb.GetCommandName(CCustomOpcodeSystem::lastOpcode); + auto commandName = CleoInstance.OpcodeInfoDb.GetCommandName(CCustomOpcodeSystem::lastOpcode); if (commandName != nullptr) { ss << ": " << commandName; @@ -836,7 +836,7 @@ namespace CLEO void CScriptEngine::Inject(CCodeInjector& inj) { TRACE("Injecting ScriptEngine..."); - CGameVersionManager& gvm = GetInstance().VersionManager; + CGameVersionManager& gvm = CleoInstance.VersionManager; // Global Events crashfix //inj.MemoryWrite(0xA9AF6C, 0, 4); @@ -939,8 +939,8 @@ namespace CLEO NativeScriptsDebugMode = GetPrivateProfileInt("General", "DebugMode", 0, Filepath_Config.c_str()) != 0; MainScriptCurWorkDir = Filepath_Game; - GetInstance().ModuleSystem.LoadCleoModules(); - LoadState(GetInstance().saveSlot); + CleoInstance.ModuleSystem.LoadCleoModules(); + LoadState(CleoInstance.saveSlot); // keep already loaded scripts at front of processing queue auto head = *activeThreadQueue; @@ -965,7 +965,7 @@ namespace CLEO gameInProgress = false; RemoveAllCustomScripts(); - GetInstance().ModuleSystem.Clear(); + CleoInstance.ModuleSystem.Clear(); memset(CleoVariables, 0, sizeof(CleoVariables)); } @@ -1137,7 +1137,7 @@ namespace CLEO CleoSafeHeader header = { CleoSafeHeader::sign, savedThreads.size(), InactiveScriptHashes.size() }; // steam offset is different, so get it manually for now - CGameVersionManager& gvm = GetInstance().VersionManager; + CGameVersionManager& gvm = CleoInstance.VersionManager; int nSlot = gvm.GetGameVersion() != GV_STEAM ? *(BYTE*)&MenuManager->m_nSelectedSaveGame : *((BYTE*)MenuManager + 0x15B); char safe_name[MAX_PATH]; @@ -1334,7 +1334,7 @@ namespace CLEO cs->SetActive(true); // run registered callbacks - for (void* func : GetInstance().GetCallbacks(eCallbackId::ScriptRegister)) + for (void* func : CleoInstance.GetCallbacks(eCallbackId::ScriptRegister)) { typedef void WINAPI callback(CCustomScript*); ((callback*)func)(cs); @@ -1360,7 +1360,7 @@ namespace CLEO void CScriptEngine::RemoveCustomScript(CCustomScript *cs) { // run registered callbacks - for (void* func : GetInstance().GetCallbacks(eCallbackId::ScriptUnregister)) + for (void* func : CleoInstance.GetCallbacks(eCallbackId::ScriptUnregister)) { typedef void WINAPI callback(CCustomScript*); ((callback*)func)(cs); @@ -1570,7 +1570,7 @@ namespace CLEO } else { - bDebugMode = GetInstance().ScriptEngine.NativeScriptsDebugMode; // global setting + bDebugMode = CleoInstance.ScriptEngine.NativeScriptsDebugMode; // global setting workDir = Filepath_Game; // game root } @@ -1612,7 +1612,7 @@ namespace CLEO memcpy(Name, fName.c_str(), len); } } - GetInstance().ScriptEngine.LastScriptCreated = this; + CleoInstance.ScriptEngine.LastScriptCreated = this; bOK = true; } catch (std::exception& e) @@ -1630,7 +1630,7 @@ namespace CLEO if (BaseIP && !bIsMission) delete[] BaseIP; RunScriptDeleteDelegate(reinterpret_cast(this)); - if (GetInstance().ScriptEngine.LastScriptCreated == this) GetInstance().ScriptEngine.LastScriptCreated = nullptr; + if (CleoInstance.ScriptEngine.LastScriptCreated == this) CleoInstance.ScriptEngine.LastScriptCreated = nullptr; } float VectorSqrMagnitude(CVector vector) { return vector.x * vector.x + vector.y * vector.y + vector.z * vector.z; } diff --git a/source/CleoBase.cpp b/source/CleoBase.cpp index 4c5f0940..76fb89a6 100644 --- a/source/CleoBase.cpp +++ b/source/CleoBase.cpp @@ -5,7 +5,6 @@ namespace CLEO { CCleoInstance CleoInstance; - CCleoInstance& GetInstance() { return CleoInstance; } inline CCleoInstance::~CCleoInstance() { @@ -17,7 +16,7 @@ namespace CLEO CleoInstance.CallCallbacks(eCallbackId::GameProcess); // execute registered callbacks static DWORD dwFunc; - dwFunc = (DWORD)(CleoInstance.UpdateGameLogics); + dwFunc = (DWORD)(CleoInstance.UpdateGameLogics_Orig); _asm jmp dwFunc } @@ -25,11 +24,10 @@ namespace CLEO { CleoSingletonCheck(); // check once for CLEO.asi duplicates - auto& base = GetInstance(); - auto window = base.CreateMainWnd_Orig(hinst); // call original + auto window = CleoInstance.CreateMainWnd_Orig(hinst); // call original // redirect window handling procedure - *((size_t*)&base.MainWndProc_Orig) = GetWindowLongPtr(window, GWLP_WNDPROC); // store original address + *((size_t*)&CleoInstance.MainWndProc_Orig) = GetWindowLongPtr(window, GWLP_WNDPROC); // store original address SetWindowLongPtr(window, GWLP_WNDPROC, (LONG)OnMainWndProc); return window; @@ -37,72 +35,67 @@ namespace CLEO LRESULT __stdcall CCleoInstance::OnMainWndProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam) { - auto& base = GetInstance(); - switch (msg) { case WM_ACTIVATE: - base.CallCallbacks(eCallbackId::MainWindowFocus, wparam != 0); + CleoInstance.CallCallbacks(eCallbackId::MainWindowFocus, wparam != 0); break; case WM_KILLFOCUS: - base.CallCallbacks(eCallbackId::MainWindowFocus, false); + CleoInstance.CallCallbacks(eCallbackId::MainWindowFocus, false); break; } - return base.MainWndProc_Orig(wnd, msg, wparam, lparam); + return CleoInstance.MainWndProc_Orig(wnd, msg, wparam, lparam); } void CCleoInstance::OnScmInit1() { - auto& base = GetInstance(); - base.ScmInit1_Orig(); // call original - base.GameBegin(); + CleoInstance.ScmInit1_Orig(); // call original + CleoInstance.GameBegin(); } void CCleoInstance::OnScmInit2() // load save { - auto& base = GetInstance(); - base.ScmInit2_Orig(); // call original - base.GameBegin(); + CleoInstance.ScmInit2_Orig(); // call original + CleoInstance.GameBegin(); } void CCleoInstance::OnScmInit3() { - auto& base = GetInstance(); - base.ScmInit3_Orig(); // call original - base.GameBegin(); + CleoInstance.ScmInit3_Orig(); // call original + CleoInstance.GameBegin(); } void __declspec(naked) CCleoInstance::OnGameShutdown() { - GetInstance().GameEnd(); + CleoInstance.GameEnd(); static DWORD oriFunc; - oriFunc = (DWORD)(GetInstance().GameShutdown); + oriFunc = (DWORD)(CleoInstance.GameShutdown_Orig); _asm jmp oriFunc } void __declspec(naked) CCleoInstance::OnGameRestart1() { - GetInstance().GameEnd(); + CleoInstance.GameEnd(); static DWORD oriFunc; - oriFunc = (DWORD)(GetInstance().GameRestart1); + oriFunc = (DWORD)(CleoInstance.GameRestart1_Orig); _asm jmp oriFunc } void __declspec(naked) CCleoInstance::OnGameRestart2() { - GetInstance().GameEnd(); + CleoInstance.GameEnd(); static DWORD oriFunc; - oriFunc = (DWORD)(GetInstance().GameRestart2); + oriFunc = (DWORD)(CleoInstance.GameRestart2_Orig); _asm jmp oriFunc } void __declspec(naked) CCleoInstance::OnGameRestart3() { - GetInstance().GameEnd(); + CleoInstance.GameEnd(); static DWORD oriFunc; - oriFunc = (DWORD)(GetInstance().GameRestart3); + oriFunc = (DWORD)(CleoInstance.GameRestart3_Orig); _asm jmp oriFunc } @@ -110,7 +103,7 @@ namespace CLEO { CleoInstance.CallCallbacks(eCallbackId::DrawingFinished); // execute registered callbacks static DWORD oriFunc; - oriFunc = (DWORD)(GetInstance().DebugDisplayTextBuffer); + oriFunc = (DWORD)(CleoInstance.DebugDisplayTextBuffer_Orig); if (oriFunc != (DWORD)nullptr) _asm jmp oriFunc else @@ -143,17 +136,17 @@ namespace CLEO CodeInjector.ReplaceFunction(OnCreateMainWnd, VersionManager.TranslateMemoryAddress(MA_CALL_CREATE_MAIN_WINDOW), &CreateMainWnd_Orig); - CodeInjector.ReplaceFunction(OnUpdateGameLogics, VersionManager.TranslateMemoryAddress(MA_CALL_UPDATE_GAME_LOGICS), &UpdateGameLogics); + CodeInjector.ReplaceFunction(OnUpdateGameLogics, VersionManager.TranslateMemoryAddress(MA_CALL_UPDATE_GAME_LOGICS), &UpdateGameLogics_Orig); CodeInjector.ReplaceFunction(OnScmInit1, VersionManager.TranslateMemoryAddress(MA_CALL_INIT_SCM1), &ScmInit1_Orig); CodeInjector.ReplaceFunction(OnScmInit2, VersionManager.TranslateMemoryAddress(MA_CALL_INIT_SCM2), &ScmInit2_Orig); CodeInjector.ReplaceFunction(OnScmInit3, VersionManager.TranslateMemoryAddress(MA_CALL_INIT_SCM3), &ScmInit3_Orig); - CodeInjector.ReplaceFunction(OnGameShutdown, VersionManager.TranslateMemoryAddress(MA_CALL_GAME_SHUTDOWN), &GameShutdown); + CodeInjector.ReplaceFunction(OnGameShutdown, VersionManager.TranslateMemoryAddress(MA_CALL_GAME_SHUTDOWN), &GameShutdown_Orig); - CodeInjector.ReplaceFunction(OnGameRestart1, VersionManager.TranslateMemoryAddress(MA_CALL_GAME_RESTART_1), &GameRestart1); - CodeInjector.ReplaceFunction(OnGameRestart2, VersionManager.TranslateMemoryAddress(MA_CALL_GAME_RESTART_2), &GameRestart2); - CodeInjector.ReplaceFunction(OnGameRestart3, VersionManager.TranslateMemoryAddress(MA_CALL_GAME_RESTART_3), &GameRestart3); + CodeInjector.ReplaceFunction(OnGameRestart1, VersionManager.TranslateMemoryAddress(MA_CALL_GAME_RESTART_1), &GameRestart1_Orig); + CodeInjector.ReplaceFunction(OnGameRestart2, VersionManager.TranslateMemoryAddress(MA_CALL_GAME_RESTART_2), &GameRestart2_Orig); + CodeInjector.ReplaceFunction(OnGameRestart3, VersionManager.TranslateMemoryAddress(MA_CALL_GAME_RESTART_3), &GameRestart3_Orig); OpcodeSystem.Init(); PluginSystem.LoadPlugins(); @@ -164,7 +157,7 @@ namespace CLEO { TRACE("CLEO initialization: Phase 2"); - CodeInjector.ReplaceJump(OnDebugDisplayTextBuffer, VersionManager.TranslateMemoryAddress(MA_DEBUG_DISPLAY_TEXT_BUFFER), &DebugDisplayTextBuffer); + CodeInjector.ReplaceJump(OnDebugDisplayTextBuffer, VersionManager.TranslateMemoryAddress(MA_DEBUG_DISPLAY_TEXT_BUFFER), &DebugDisplayTextBuffer_Orig); PluginSystem.LogLoadedPlugins(); } @@ -193,7 +186,7 @@ namespace CLEO TRACE("Starting new game, save slot: %d", saveSlot); // execute registered callbacks - GetInstance().CallCallbacks(eCallbackId::GameBegin, saveSlot); + CleoInstance.CallCallbacks(eCallbackId::GameBegin, saveSlot); } void CCleoInstance::GameEnd() @@ -202,7 +195,7 @@ namespace CLEO m_bGameInProgress = false; TRACE("Ending current game"); - GetInstance().CallCallbacks(eCallbackId::GameEnd); // execute registered callbacks + CleoInstance.CallCallbacks(eCallbackId::GameEnd); // execute registered callbacks ScriptEngine.GameEnd(); OpcodeSystem.FinalizeScriptObjects(); @@ -226,7 +219,7 @@ namespace CLEO void CCleoInstance::CallCallbacks(eCallbackId id) { - for (void* func : GetInstance().GetCallbacks(id)) + for (void* func : GetCallbacks(id)) { typedef void WINAPI callback(void); ((callback*)func)(); @@ -235,7 +228,7 @@ namespace CLEO void CCleoInstance::CallCallbacks(eCallbackId id, DWORD arg) { - for (void* func : GetInstance().GetCallbacks(id)) + for (void* func : GetCallbacks(id)) { typedef void WINAPI callback(DWORD); ((callback*)func)(arg); @@ -244,12 +237,12 @@ namespace CLEO void WINAPI CLEO_RegisterCallback(eCallbackId id, void* func) { - GetInstance().AddCallback(id, func); + CleoInstance.AddCallback(id, func); } void WINAPI CLEO_UnregisterCallback(eCallbackId id, void* func) { - GetInstance().RemoveCallback(id, func); + CleoInstance.RemoveCallback(id, func); } DWORD WINAPI CLEO_GetInternalAudioStream(CLEO::CRunningScript* unused, DWORD audioStreamPtr) diff --git a/source/CleoBase.h b/source/CleoBase.h index 4423c548..9e362abd 100644 --- a/source/CleoBase.h +++ b/source/CleoBase.h @@ -56,9 +56,7 @@ namespace CLEO void CallCallbacks(eCallbackId id); void CallCallbacks(eCallbackId id, DWORD arg); - static void __cdecl OnDrawingFinished(); - - void(__cdecl * UpdateGameLogics)() = nullptr; + void(__cdecl* UpdateGameLogics_Orig)() = nullptr; static void __cdecl OnUpdateGameLogics(); // call for InitInstance @@ -78,19 +76,19 @@ namespace CLEO static void OnScmInit3(); // call for Game::Shutdown - void(__cdecl* GameShutdown)() = nullptr; + void(__cdecl* GameShutdown_Orig)() = nullptr; static void OnGameShutdown(); // calls for Game::ShutDownForRestart - void(__cdecl* GameRestart1)() = nullptr; - void(__cdecl* GameRestart2)() = nullptr; - void(__cdecl* GameRestart3)() = nullptr; + void(__cdecl* GameRestart1_Orig)() = nullptr; + void(__cdecl* GameRestart2_Orig)() = nullptr; + void(__cdecl* GameRestart3_Orig)() = nullptr; static void OnGameRestart1(); static void OnGameRestart2(); static void OnGameRestart3(); // empty function called after everything else is drawn - memory_pointer DebugDisplayTextBuffer = nullptr; + memory_pointer DebugDisplayTextBuffer_Orig = nullptr; static void OnDebugDisplayTextBuffer(); private: @@ -99,6 +97,6 @@ namespace CLEO std::map> m_callbacks; }; - CCleoInstance& GetInstance(); + extern CCleoInstance CleoInstance; } diff --git a/source/dllmain.cpp b/source/dllmain.cpp index 784ba0da..08e7ac28 100644 --- a/source/dllmain.cpp +++ b/source/dllmain.cpp @@ -7,7 +7,7 @@ class Starter static Starter dummy; Starter() { - auto gv = CLEO::GetInstance().VersionManager.GetGameVersion(); + auto gv = CLEO::CleoInstance.VersionManager.GetGameVersion(); TRACE("Started on game of version: %s", (gv == CLEO::GV_US10) ? "SA 1.0 us" : (gv == CLEO::GV_EU11) ? "SA 1.01 eu" : @@ -31,12 +31,12 @@ class Starter " 10) gta_sa.exe, decrypted 3.0 steam executable, 5 697 536 bytes." ); - CLEO::GetInstance().Start(CLEO::CCleoInstance::InitStage::Initial); + CLEO::CleoInstance.Start(CLEO::CCleoInstance::InitStage::Initial); } ~Starter() { - CLEO::GetInstance().Stop(); + CLEO::CleoInstance.Stop(); } };