-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.lua
More file actions
205 lines (177 loc) · 7.71 KB
/
Copy pathmain.lua
File metadata and controls
205 lines (177 loc) · 7.71 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
-- FS25_MarketDynamics / main.lua
-- Entry point. Loads all modules in dependency order, then hooks into the game lifecycle.
-- Log prefix: [MDM] | Global: g_MarketDynamics
--
-- Authors: TheCodingDad (tison) — core systems
-- LeGrizzly — GUI systems
local modDirectory = g_currentModDirectory
local modName = g_currentModName
-- Menu icon global (resolved by XML imageFilename via hook below)
g_MDMIconMenu = Utils.getFilename("images/menuIcon.dds", g_currentModDirectory)
-- Resolve mod icon globals in XML imageFilename attributes (EmployeeManager pattern)
local MDM_ICON_GLOBALS = {
g_MDMIconMenu = true,
}
local function mdmResolveFilename(self, superFunc)
local filename = superFunc(self)
if MDM_ICON_GLOBALS[filename] then
return _G[filename]
end
return filename
end
GuiOverlay.resolveFilename = Utils.overwrittenFunction(GuiOverlay.resolveFilename, mdmResolveFilename)
-- ---------------------------------------------------------------------------
-- Lifecycle hooks
-- ---------------------------------------------------------------------------
local mdm = nil -- will hold the MarketDynamics instance
local function onLoad(mission)
mdm = MarketDynamics.new(modDirectory, modName)
getfenv(0)["g_MarketDynamics"] = mdm
if g_currentMission then
g_currentMission.MarketDynamics = mdm
end
-- BUILD 11:43: publish the graph class the same way the instance is published above.
-- The Esc door is hosted by whichever suite mod bootstrapped first, and from that
-- environment a bare MDMMarketScreenGraph is nil. It was reachable only by guessing
-- our g_modEnvironments key, which is exactly what failed on the Dairy door
-- (GATE log: graph=false). Putting it in the real global table and on the mission
-- makes it findable without anyone knowing our mod name.
if MDMMarketScreenGraph ~= nil then
getfenv(0)["MDMMarketScreenGraph"] = MDMMarketScreenGraph
if g_currentMission then
g_currentMission.MDMMarketScreenGraph = MDMMarketScreenGraph
end
end
-- BUILD 12:32: the guest needs the same treatment. It sits in exactly the position
-- the graph class was in before 11:43 - reachable from a foreign door only through
-- belts that evidently do not reach - so the host resolved nil, selectCommodityIndex
-- never fired, and picking a row moved the highlight without moving the trend.
if MdRfPdaGuest ~= nil then
getfenv(0)["MdRfPdaGuest"] = MdRfPdaGuest
if g_currentMission then
g_currentMission.MdRfPdaGuest = MdRfPdaGuest
end
end
end
local function onLoadFinished(mission)
if mdm then
mdm:onMissionLoaded(mission)
end
end
local function onStartMission(mission)
if mdm then
mdm:onStartMission(mission)
end
end
local function onUpdate(mission, dt)
if mdm then
mdm:update(dt)
end
end
local function onDraw(mission)
if mdm then
mdm:draw()
end
end
local function onMouseEvent(mission, posX, posY, isDown, isUp, button)
if mdm then
mdm:mouseEvent(posX, posY, isDown, isUp, button, false)
end
end
local function onSave(mission, xmlFile)
if mdm then
mdm:save(xmlFile)
end
end
local function onDelete(mission)
if mdm then
mdm:delete()
mdm = nil
getfenv(0)["g_MarketDynamics"] = nil
getfenv(0)["MDMMarketScreenGraph"] = nil
getfenv(0)["MdRfPdaGuest"] = nil
if g_currentMission then
g_currentMission.MarketDynamics = nil
g_currentMission.MDMMarketScreenGraph = nil
g_currentMission.MdRfPdaGuest = nil
end
end
end
-- ---------------------------------------------------------------------------
-- Input Handling (RVB Pattern)
-- ---------------------------------------------------------------------------
local function mdmSettingsActionCallback(self, actionName, inputValue, callbackState, isAnalog)
if inputValue <= 0 then return end
if not mdm then return end
-- Don't open while another dialog is showing
if g_gui and (g_gui:getIsGuiVisible() or g_gui:getIsDialogVisible()) then return end
mdm:toggleSettings()
end
local function mdmMarketScreenActionCallback(self, actionName, inputValue, callbackState, isAnalog)
if inputValue <= 0 then return end
MDMMarketScreen.toggle()
end
local function mdmCreateContractActionCallback(self, actionName, inputValue, callbackState, isAnalog)
if inputValue <= 0 then return end
MDMMarketScreen.onGlobalCreateContract()
end
local function hookMDMInput()
if PlayerInputComponent == nil or PlayerInputComponent.registerActionEvents == nil then
print("[MDM] PlayerInputComponent.registerActionEvents not available")
return
end
local originalRegister = PlayerInputComponent.registerActionEvents
PlayerInputComponent.registerActionEvents = function(inputComponent, ...)
originalRegister(inputComponent, ...)
if inputComponent.player ~= nil and inputComponent.player.isOwner then
g_inputBinding:beginActionEventsModification(PlayerInputComponent.INPUT_CONTEXT_NAME)
-- Market Screen (F10)
local screenActionId = InputAction.MDM_MARKET_SCREEN
if screenActionId ~= nil then
local success, eventId = g_inputBinding:registerActionEvent(
screenActionId, nil, mdmMarketScreenActionCallback,
false, true, false, true
)
if success and eventId then
g_inputBinding:setActionEventTextVisibility(eventId, false)
end
end
-- Create Contract (N)
local contractActionId = InputAction.MDM_CREATE_CONTRACT
if contractActionId ~= nil then
local success, eventId = g_inputBinding:registerActionEvent(
contractActionId, nil, mdmCreateContractActionCallback,
false, true, false, true
)
if success and eventId then
g_inputBinding:setActionEventTextVisibility(eventId, false)
end
end
-- Settings (F5)
local settingsActionId = InputAction.MDM_OPEN_SETTINGS
if settingsActionId ~= nil then
local success, eventId = g_inputBinding:registerActionEvent(
settingsActionId, nil, mdmSettingsActionCallback,
false, true, false, true
)
if success and eventId then
g_inputBinding:setActionEventTextVisibility(eventId, false)
g_inputBinding:setActionEventActive(eventId, true)
MDMLog.info("[MDM] F5 Settings panel toggle registered via hook")
end
end
g_inputBinding:endActionEventsModification()
end
end
end
-- Call hook at source time
hookMDMInput()
-- Attach to game hooks
Mission00.load = Utils.prependedFunction(Mission00.load, onLoad)
Mission00.loadMission00Finished = Utils.appendedFunction(Mission00.loadMission00Finished, onLoadFinished)
Mission00.onStartMission = Utils.appendedFunction(Mission00.onStartMission, onStartMission)
FSBaseMission.update = Utils.appendedFunction(FSBaseMission.update, onUpdate)
FSBaseMission.draw = Utils.appendedFunction(FSBaseMission.draw, onDraw)
FSBaseMission.mouseEvent = Utils.appendedFunction(FSBaseMission.mouseEvent, onMouseEvent)
FSBaseMission.saveSavegame = Utils.appendedFunction(FSBaseMission.saveSavegame, onSave)
FSBaseMission.delete = Utils.appendedFunction(FSBaseMission.delete, onDelete)