From d6ee6e9a1040eeaa341cc676de6eef0f947ac356 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Sat, 25 Feb 2023 17:26:24 +0100 Subject: [PATCH] fix(dtmm): Fix patching GameStateMachine The monkey-pacthed function was still called multiple times. --- crates/dtmm/assets/mod_main.lua | 34 ++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/crates/dtmm/assets/mod_main.lua b/crates/dtmm/assets/mod_main.lua index 6564769..02940fb 100644 --- a/crates/dtmm/assets/mod_main.lua +++ b/crates/dtmm/assets/mod_main.lua @@ -56,28 +56,32 @@ end -- Patch `GameStateMachine.init` to add our own state for loading mods. -- In the future, Fatshark might provide us with a dedicated way to do this. local function patch_mod_loading_state() -print("[mod_main] Adding mod loading state") + print("[mod_main] Adding mod loading state") local GameStateMachine = require("scripts/foundation/utilities/game_state_machine") + local patched = false + local GameStateMachine_init = GameStateMachine.init GameStateMachine.init = function(self, parent, start_state, params, ...) - -- Hardcoded position after `StateRequireScripts`. - -- We do want to wait until then, so that most of the game's core - -- systems are at least loaded and can be hooked, even if they aren't - -- running, yet. - local pos = 4 - table.insert(params.states, pos, { - StateBootLoadMods, - { - package_manager = params.package_manager, - }, - }) + if not patched then + patched = true - -- Clean up after us - GameStateMachine.init = GameStateMachine_init + -- Hardcoded position after `StateRequireScripts`. + -- We do want to wait until then, so that most of the game's core + -- systems are at least loaded and can be hooked, even if they aren't + -- running, yet. + local pos = 4 + table.insert(params.states, pos, { + StateBootLoadMods, + { + package_manager = params.package_manager, + }, + }) + end - return GameStateMachine_init(self, parent, start_state, params, ...) + GameStateMachine_init(self, parent, start_state, params, ...) end + print("[mod_main] Mod patching complete") end function init()