Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legacy modes support for native scripts #257

Merged
merged 5 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cleo_plugins/Text/CTextManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ namespace CLEO
}
}
CLEO::CLEO_StringListFree(list);

TRACE(""); // separator
}

void CTextManager::Clear()
Expand Down
24 changes: 10 additions & 14 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 @@ -1087,13 +1088,12 @@ namespace CLEO
// pass arguments as new scope local variables
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
// initialize (clear) rest of new scope local variables
if (CLEO_GetScriptVersion(thread) >= CLEO_VER_4_MIN) // CLEO 3 did not cleared 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 +1859,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
36 changes: 25 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,20 @@ namespace CLEO
}

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

// global native scripts legacy mode
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;
default:
NativeScriptsVersion = eCLEO_Version::CLEO_VER_CUR;
ver = 0;
break;
}
if (ver != 0) TRACE("Legacy mode for native scripts active: CLEO%d", ver);

MainScriptCurWorkDir = Filepath_Game;

CleoInstance.ModuleSystem.LoadCleoModules();
Expand Down Expand Up @@ -1537,13 +1551,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 +1568,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