From fa29804c49faf9135b8c1929cc49e201024521c6 Mon Sep 17 00:00:00 2001 From: Nikolay Korolev Date: Sun, 3 May 2020 01:42:46 +0300 Subject: script compatibility --- src/control/Script.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++++++-- src/control/Script.h | 14 ++++----- 2 files changed, 83 insertions(+), 11 deletions(-) (limited to 'src/control') diff --git a/src/control/Script.cpp b/src/control/Script.cpp index b63a5d87..433e0449 100644 --- a/src/control/Script.cpp +++ b/src/control/Script.cpp @@ -128,6 +128,14 @@ uint16 CTheScripts::CommandsExecuted; uint16 CTheScripts::ScriptsUpdated; int32 ScriptParams[32]; + +const uint32 CRunningScript::nSaveStructSize = +#ifdef COMPATIBLE_SAVES + 136; +#else + sizeof(CRunningScript); +#endif + CMissionCleanup::CMissionCleanup() { Init(); @@ -11188,7 +11196,7 @@ INITSAVEBUF uint32 runningScripts = 0; for (CRunningScript* pScript = pActiveScripts; pScript; pScript = pScript->GetNext()) runningScripts++; - *size = sizeof(CRunningScript) * runningScripts + varSpace + SCRIPT_DATA_SIZE + SAVE_HEADER_SIZE + 3 * sizeof(uint32); + *size = CRunningScript::nSaveStructSize * runningScripts + varSpace + SCRIPT_DATA_SIZE + SAVE_HEADER_SIZE + 3 * sizeof(uint32); WriteSaveHeader(buf, 'S', 'C', 'R', '\0', *size - SAVE_HEADER_SIZE); WriteSaveBuf(buf, varSpace); for (uint32 i = 0; i < varSpace; i++) @@ -11260,7 +11268,7 @@ INITSAVEBUF WriteSaveBuf(buf, (uint16)0); WriteSaveBuf(buf, runningScripts); for (CRunningScript* pScript = pActiveScripts; pScript; pScript = pScript->GetNext()) - WriteSaveBuf(buf, *pScript); + pScript->Save(buf); VALIDATESAVEBUF(*size) } @@ -11336,7 +11344,7 @@ INITSAVEBUF ReadSaveBuf(buf); uint32 runningScripts = ReadSaveBuf(buf); for (uint32 i = 0; i < runningScripts; i++) - StartNewScript(0)->BuildFromSaved(ReadSaveBuf(buf)); + StartNewScript(0)->Load(buf); VALIDATESAVEBUF(size) } @@ -11611,3 +11619,69 @@ void CTheScripts::ReadMultiScriptFileOffsetsFromScript() MultiScriptArray[i] = Read4BytesFromScript(&ip); } } + +void CRunningScript::Save(uint8*& buf) +{ +#ifdef COMPATIBLE_SAVES + SkipSaveBuf(buf, 8); + for (int i = 0; i < 8; i++) + WriteSaveBuf(buf, m_abScriptName[i]); + WriteSaveBuf(buf, m_nIp); + static_assert(MAX_STACK_DEPTH == 6, "Compatibility loss: MAX_STACK_DEPTH != 6"); + for (int i = 0; i < MAX_STACK_DEPTH; i++) + WriteSaveBuf(buf, m_anStack[i]); + WriteSaveBuf(buf, m_nStackPointer); + SkipSaveBuf(buf, 2); + static_assert(NUM_LOCAL_VARS + NUM_TIMERS == 18, "Compatibility loss: NUM_LOCAL_VARS + NUM_TIMERS != 18"); + for (int i = 0; i < NUM_LOCAL_VARS + NUM_TIMERS; i++) + WriteSaveBuf(buf, m_anLocalVariables[i]); + WriteSaveBuf(buf, m_bCondResult); + WriteSaveBuf(buf, m_bIsMissionScript); + WriteSaveBuf(buf, m_bSkipWakeTime); + SkipSaveBuf(buf, 1); + WriteSaveBuf(buf, m_nWakeTime); + WriteSaveBuf(buf, m_nAndOrState); + WriteSaveBuf(buf, m_bNotFlag); + WriteSaveBuf(buf, m_bDeatharrestEnabled); + WriteSaveBuf(buf, m_bDeatharrestExecuted); + WriteSaveBuf(buf, m_bMissionFlag); + SkipSaveBuf(buf, 2); +#else + WriteSaveBuf(buf, *this); +#endif +} + +void CRunningScript::Load(uint8*& buf) +{ +#ifdef COMPATIBLE_SAVES + SkipSaveBuf(buf, 8); + for (int i = 0; i < 8; i++) + m_abScriptName[i] = ReadSaveBuf(buf); + m_nIp = ReadSaveBuf(buf); + static_assert(MAX_STACK_DEPTH == 6, "Compatibility loss: MAX_STACK_DEPTH != 6"); + for (int i = 0; i < MAX_STACK_DEPTH; i++) + m_anStack[i] = ReadSaveBuf(buf); + m_nStackPointer = ReadSaveBuf(buf); + SkipSaveBuf(buf, 2); + static_assert(NUM_LOCAL_VARS + NUM_TIMERS == 18, "Compatibility loss: NUM_LOCAL_VARS + NUM_TIMERS != 18"); + for (int i = 0; i < NUM_LOCAL_VARS + NUM_TIMERS; i++) + m_anLocalVariables[i] = ReadSaveBuf(buf); + m_bCondResult = ReadSaveBuf(buf); + m_bIsMissionScript = ReadSaveBuf(buf); + m_bSkipWakeTime = ReadSaveBuf(buf); + SkipSaveBuf(buf, 1); + m_nWakeTime = ReadSaveBuf(buf); + m_nAndOrState = ReadSaveBuf(buf); + m_bNotFlag = ReadSaveBuf(buf); + m_bDeatharrestEnabled = ReadSaveBuf(buf); + m_bDeatharrestExecuted = ReadSaveBuf(buf); + m_bMissionFlag = ReadSaveBuf(buf); + SkipSaveBuf(buf, 2); +#else + CRunningScript* n = next; + CRunningScript* p = prev; + *this = ReadSaveBuf(buf); + next = n; + prev = p; +#endif +} diff --git a/src/control/Script.h b/src/control/Script.h index 2eed29fe..01cad269 100644 --- a/src/control/Script.h +++ b/src/control/Script.h @@ -423,14 +423,10 @@ class CRunningScript public: void SetIP(uint32 ip) { m_nIp = ip; } CRunningScript* GetNext() const { return next; } - void BuildFromSaved(const CRunningScript& pSaved) - { - CRunningScript* n = next; - CRunningScript* p = prev; - *this = pSaved; - next = n; - prev = p; - } + + void Save(uint8*& buf); + void Load(uint8*& buf); + void UpdateTimers(float timeStep) { m_anLocalVariables[NUM_LOCAL_VARS] += timeStep; m_anLocalVariables[NUM_LOCAL_VARS + 1] += timeStep; @@ -442,6 +438,8 @@ public: void RemoveScriptFromList(CRunningScript**); void AddScriptToList(CRunningScript**); + static const uint32 nSaveStructSize; + private: void CollectParameters(uint32*, int16); int32 CollectNextParameterWithoutIncreasingPC(uint32); -- cgit v1.2.3