Skip to content

Commit

Permalink
Fixes and refactoring of legacy CLEO support.
Browse files Browse the repository at this point in the history
Added legacy option for native scripts to config.ini
  • Loading branch information
MiranDMC committed Dec 28, 2024
1 parent 68aaad2 commit 3afcc1e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
23 changes: 10 additions & 13 deletions source/CCustomOpcodeSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,9 +892,6 @@ namespace CLEO
SetScriptCondResult(thread, cs && cs->IsOK());
if (cs && cs->IsOK())
{
auto csscript = reinterpret_cast<CCustomScript*>(thread);
if (csscript->IsCustom())
cs->SetCompatibility(csscript->GetCompatibility());
CleoInstance.ScriptEngine.AddCustomScript(cs);
memset(missionLocals, 0, 1024 * sizeof(SCRIPT_VAR)); // same as CTheScripts::WipeLocalVariableMemoryForMissionScript
TransmitScriptParams(thread, (CRunningScript*)((BYTE*)missionLocals - 0x3C));
Expand Down Expand Up @@ -939,9 +936,13 @@ namespace CLEO
//0AA9=0, is_game_version_original
OpcodeResult __stdcall opcode_0AA9(CRunningScript *thread)
{
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));
auto gameVer = CleoInstance.VersionManager.GetGameVersion();
auto scriptVer = CLEO_GetScriptVersion(thread);

bool result = (gameVer == GV_US10) ||
(scriptVer <= CLEO_VER_4_MIN && gameVer == GV_EU10);

OPCODE_CONDITION_RESULT(result);
return OR_CONTINUE;
}

Expand Down Expand Up @@ -1088,12 +1089,12 @@ namespace CLEO
memcpy(locals, arguments, nParams * sizeof(SCRIPT_VAR));

// initialize rest of new scope local variables
auto cs = reinterpret_cast<CCustomScript*>(thread);
if (cs->IsCustom() && cs->GetCompatibility() >= CLEO_VER_4_MIN) // CLEO 3 did not initialised local variables
auto scriptVer = CLEO_GetScriptVersion(thread);
if (scriptVer >= CLEO_VER_4_MIN) // CLEO 3 did not initialised local variables
{
for (DWORD i = nParams; i < 32; i++)
{
cs->SetIntVar(i, 0); // fill with zeros
thread->SetIntVar(i, 0); // fill with zeros
}
}

Expand Down Expand Up @@ -1859,10 +1860,6 @@ extern "C"
{
CleoInstance.ScriptEngine.AddCustomScript(cs);
if (fromThread) TransmitScriptParams(fromThread, cs);

cs->SetDebugMode(fromThread ?
reinterpret_cast<CCustomScript*>(fromThread)->GetDebugMode() : // from parent
CleoInstance.ScriptEngine.NativeScriptsDebugMode); // global
}
else
{
Expand Down
32 changes: 21 additions & 11 deletions source/CScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ namespace CLEO
if (thread->IsCustom())
return reinterpret_cast<const CCustomScript*>(thread)->GetCompatibility();
else
return CLEO::eCLEO_Version::CLEO_VER_CUR;
return CleoInstance.ScriptEngine.NativeScriptsVersion;
}

LPCSTR WINAPI CLEO_GetScriptFilename(const CRunningScript* thread)
Expand Down Expand Up @@ -937,6 +937,16 @@ namespace CLEO
}

NativeScriptsDebugMode = GetPrivateProfileInt("General", "DebugMode", 0, Filepath_Config.c_str()) != 0;

int ver = GetPrivateProfileInt("General", "MainScmLegacyMode", 0, Filepath_Config.c_str());
switch(ver)
{
case 3: NativeScriptsVersion = eCLEO_Version::CLEO_VER_3; break;
case 4: NativeScriptsVersion = eCLEO_Version::CLEO_VER_4; break;
case 5: NativeScriptsVersion = eCLEO_Version::CLEO_VER_5; break;
default: NativeScriptsVersion = eCLEO_Version::CLEO_VER_CUR; break;
}

MainScriptCurWorkDir = Filepath_Game;

CleoInstance.ModuleSystem.LoadCleoModules();
Expand Down Expand Up @@ -1537,13 +1547,13 @@ namespace CLEO
if (path.extension() == cs4_ext)
CompatVer = CLEO_VER_4;
else
if (path.extension() == cs3_ext)
CompatVer = CLEO_VER_3;
if (path.extension() == cs3_ext)
CompatVer = CLEO_VER_3;

if (CompatVer == CLEO_VER_CUR && parent != nullptr && parent - IsCustom())
if (CompatVer == CLEO_VER_CUR && parent != nullptr)
{
// inherit compatibility mode from parent
CompatVer = ((CCustomScript*)parent)->GetCompatibility();
CompatVer = CLEO_GetScriptVersion(parent);

// try loading file with same compatibility mode filetype extension
auto compatPath = path;
Expand All @@ -1554,12 +1564,12 @@ namespace CLEO
path = compatPath;
}
else
if (CompatVer == CLEO_VER_3)
{
compatPath.replace_extension(cs3_ext);
if (FS::is_regular_file(compatPath))
path = compatPath;
}
if (CompatVer == CLEO_VER_3)
{
compatPath.replace_extension(cs3_ext);
if (FS::is_regular_file(compatPath))
path = compatPath;
}
}

scriptFileDir = path.parent_path().string();
Expand Down
1 change: 1 addition & 0 deletions source/CScriptEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ namespace CLEO
CCustomScript *LoadScript(const char *szFilePath);

bool NativeScriptsDebugMode; // debug mode enabled?
CLEO::eCLEO_Version NativeScriptsVersion; // allows using legacy modes
std::string MainScriptFileDir;
std::string MainScriptFileName;
std::string MainScriptCurWorkDir;
Expand Down
5 changes: 4 additions & 1 deletion source/cleo_config.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[General]
; debug opcodes, on screen prints etc.: 0 - off, 1 - enabled
; default debug mode state for all scripts (see opcode debug_on/debug_off): 0 - off, 1 - enabled
DebugMode=0

; legacy mode for main script(s): 0 - off, 3 - CLEO3, 4 - CLEO4
MainScmLegacyMode=0

0 comments on commit 3afcc1e

Please sign in to comment.