-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
108 lines (93 loc) · 4.61 KB
/
Copy pathmain.lua
File metadata and controls
108 lines (93 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
-- =========================================================
-- FS25_TimeGuard - mod entry point
-- =========================================================
-- Author: TisonK
-- =========================================================
-- Loads the Time Guard modules, publishes the g_timeGuard handle, and
-- hooks the FS25 mission lifecycle:
-- Mission00.load -> publish the cross-mod bridge handle
-- Mission00.loadMission00Finished -> seed the clock, subscribe to the
-- calendar events, bind bedrock
-- FSBaseMission.update -> drive the client join-sync retry
-- FSCareerMissionInfo.saveToXMLFile -> persist the accrual cursors (own-file
-- fallback; StateLedger handles it when present)
-- FSBaseMission.delete -> unsubscribe + drop the handle
--
-- Load order: Time Guard is the 5th core service (after StateLedger,
-- NetworkSync, MasterHUD, SettingsHub). The handle is published as soon as
-- this file runs so companion mods that load after it can subscribe /
-- registerAccrual during their own module load, before the mission exists.
-- =========================================================
local modDirectory = g_currentModDirectory
source(modDirectory .. "src/Logger.lua")
source(modDirectory .. "src/TimeGuardScheduler.lua")
source(modDirectory .. "src/TimeGuard.lua")
-- Create the instance and publish the handle immediately (before any
-- mission hook), so early companion registrations are never missed.
local timeGuard = TimeGuard.new()
getfenv(0)["g_timeGuard"] = timeGuard
-- ---------------------------------------------------------
-- Mission lifecycle hooks
-- ---------------------------------------------------------
local function onMissionLoad(mission)
-- g_currentMission is a shared C++ object visible to all mods, unlike
-- getfenv(0) which is per-mod scoped. Publish the cross-mod bridge here.
if mission ~= nil then
mission.timeGuard = timeGuard
end
TGLogger.info("Time Guard active (mod 5, economic clock)")
end
local function onMissionLoadedFinished()
timeGuard:onMissionLoaded()
end
local function onMissionUpdate(mission, dt)
timeGuard:update(dt)
end
local function onMissionSave()
timeGuard:save()
end
-- Save originals before appending so they can be restored on mission delete.
local _origMission00Load = Mission00.load
local _origMission00LoadMission00Finished = Mission00.loadMission00Finished
local _origFSBaseMissionUpdate = FSBaseMission.update
local _origFSBaseMissionDelete = FSBaseMission.delete
local _origSaveToXMLFile = FSCareerMissionInfo ~= nil and FSCareerMissionInfo.saveToXMLFile or nil
local function onMissionDelete()
timeGuard:onMissionDelete()
getfenv(0)["g_timeGuard"] = nil
if g_currentMission ~= nil then
g_currentMission.timeGuard = nil
end
-- Restore original functions to prevent hook stacking on mission reload.
Mission00.load = _origMission00Load
Mission00.loadMission00Finished = _origMission00LoadMission00Finished
FSBaseMission.update = _origFSBaseMissionUpdate
FSBaseMission.delete = _origFSBaseMissionDelete
if FSCareerMissionInfo ~= nil then
FSCareerMissionInfo.saveToXMLFile = _origSaveToXMLFile
end
end
-- appendedFunction so the base game's own handler runs first.
Mission00.load = Utils.appendedFunction(Mission00.load, onMissionLoad)
Mission00.loadMission00Finished = Utils.appendedFunction(Mission00.loadMission00Finished, onMissionLoadedFinished)
FSBaseMission.update = Utils.appendedFunction(FSBaseMission.update, onMissionUpdate)
-- Save via FSCareerMissionInfo:saveToXMLFile, the window where
-- savegameDirectory points at the tempsavegame that FS25 then copies over the
-- real savegame. Hooking the non-existent Mission00.saveToXMLFile is a known
-- trap that silently never fires.
if FSCareerMissionInfo ~= nil and FSCareerMissionInfo.saveToXMLFile ~= nil then
FSCareerMissionInfo.saveToXMLFile = Utils.appendedFunction(
FSCareerMissionInfo.saveToXMLFile,
function() onMissionSave() end
)
else
TGLogger.warning("FSCareerMissionInfo.saveToXMLFile not found - accrual cursors will NOT be saved")
end
FSBaseMission.delete = Utils.prependedFunction(FSBaseMission.delete, onMissionDelete)
-- ---------------------------------------------------------
-- Console command: tgStatus (target + method, the proven pattern)
-- ---------------------------------------------------------
if addConsoleCommand ~= nil then
addConsoleCommand("tgStatus", "Show Time Guard clock state, subscribers, and accruals",
"consoleCommandStatus", timeGuard)
end