Skip to content

Commit

Permalink
Removed GetInstance() in favor of straight CleoInstance use.
Browse files Browse the repository at this point in the history
Added _Orig suffix to all variables storing original hook addresses.
  • Loading branch information
MiranDMC committed Dec 1, 2024
1 parent 27e9f27 commit 8d786cf
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 142 deletions.
14 changes: 5 additions & 9 deletions source/CCodeInjector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
74 changes: 37 additions & 37 deletions source/CCustomOpcodeSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.",
Expand All @@ -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.",
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -810,7 +810,7 @@ namespace CLEO

OpcodeResult __stdcall CCustomOpcodeSystem::opcode_004E(CRunningScript* thread)
{
GetInstance().ScriptEngine.RemoveScript(thread);
CleoInstance.ScriptEngine.RemoveScript(thread);
return OR_INTERRUPT;
}

Expand All @@ -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)
Expand Down Expand Up @@ -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<CCustomScript*>(thread)->GetDebugMode());
}
Expand All @@ -875,7 +875,7 @@ namespace CLEO
return OR_CONTINUE; // legacy behavior
}

GetInstance().ScriptEngine.RemoveScript(thread);
CleoInstance.ScriptEngine.RemoveScript(thread);
return OR_INTERRUPT;
}

Expand All @@ -895,7 +895,7 @@ namespace CLEO
auto csscript = reinterpret_cast<CCustomScript*>(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<CCustomScript*>(thread)->GetDebugMode());
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1000,7 +1000,7 @@ namespace CLEO
modulePath = reinterpret_cast<CCustomScript*>(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());
Expand Down Expand Up @@ -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%
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -1547,7 +1547,7 @@ namespace CLEO
}

SetScriptCondResult(thread, false);
return GetInstance().OpcodeSystem.CleoReturnGeneric(0x2003, thread);
return CleoInstance.OpcodeSystem.CleoReturnGeneric(0x2003, thread);
}
}

Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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)
Expand Down Expand Up @@ -1789,7 +1789,7 @@ extern "C"
}
}

GetInstance().OpcodeSystem.handledParamCount += count;
CleoInstance.OpcodeSystem.handledParamCount += count;
}

void WINAPI CLEO_SkipUnusedVarArgs(CLEO::CRunningScript* thread)
Expand All @@ -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)
Expand Down Expand Up @@ -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<CCustomScript*>(fromThread)->GetDebugMode() : // from parent
GetInstance().ScriptEngine.NativeScriptsDebugMode); // global
CleoInstance.ScriptEngine.NativeScriptsDebugMode); // global
}
else
{
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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

Expand Down
5 changes: 2 additions & 3 deletions source/CDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion source/CDmaFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions source/CGameMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 8d786cf

Please sign in to comment.