forked from Courseplay/Courseplay_FS22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourseplay.lua
282 lines (243 loc) · 10.7 KB
/
Courseplay.lua
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
--- Global class
Courseplay = CpObject()
Courseplay.MOD_NAME = g_currentModName
Courseplay.BASE_DIRECTORY = g_currentModDirectory
Courseplay.BASE_KEY = "Courseplay."
function Courseplay:init()
self:registerConsoleCommands()
self:showUserInformation()
end
------------------------------------------------------------------------------------------------------------------------
-- User info with github reference and update notification.
------------------------------------------------------------------------------------------------------------------------
function Courseplay:showUserInformation()
local showInfoDialog = true
local currentVersion = g_modManager:getModByName(self.MOD_NAME).version
local path = getUserProfileAppPath() .. "game.xml"
if fileExists(path) then
local xmlFile = loadXMLFile("gameXml", path)
local lastLoadedVersion = getXMLString(xmlFile,"game.courseplay.lastVersion")
if lastLoadedVersion then
if currentVersion == lastLoadedVersion then
showInfoDialog = false
end
end
if showInfoDialog then
g_gui:showInfoDialog({
text = string.format(g_i18n:getText("CP_infoText"), currentVersion)
})
setXMLString(xmlFile,"game.courseplay.lastVersion",currentVersion)
end
saveXMLFile(xmlFile)
delete(xmlFile)
end
end
------------------------------------------------------------------------------------------------------------------------
-- Global Giants functions listener
------------------------------------------------------------------------------------------------------------------------
--- This function is called on loading a savegame.
---@param filename string
function Courseplay:loadMap(filename)
self.globalSettings = CpGlobalSettings()
self:registerSchema()
self:load()
self:setupGui()
if g_currentMission.missionInfo.savegameDirectory ~= nil then
local filePath = g_currentMission.missionInfo.savegameDirectory .. "/Courseplay.xml"
self.xmlFile = XMLFile.load("cpXml", filePath , self.schema)
if self.xmlFile == nil then return end
self.globalSettings:loadFromXMLFile(self.xmlFile,g_Courseplay.BASE_KEY)
self.xmlFile:delete()
end
end
function Courseplay:registerSchema()
self.schema = XMLSchema.new("Courseplay")
self.globalSettings:registerSchema(self.schema,self.BASE_KEY)
end
function Courseplay:setupGui()
CpVehicleSettingsFrame.init()
CpGlobalSettingsFrame.init()
CpCourseManagerFrame.init(self.courseStorage)
end
function Courseplay:loadMapDataHelpLineManager(xmlFile, missionInfo)
self:loadFromXML(Utils.getFilename("config/HelpMenu.xml",Courseplay.BASE_DIRECTORY))
end
HelpLineManager.loadMapData = Utils.appendedFunction( HelpLineManager.loadMapData,Courseplay.loadMapDataHelpLineManager)
function Courseplay.saveToXMLFile(missionInfo)
if missionInfo.isValid then
local xmlFile = XMLFile.create("cpXml",missionInfo.savegameDirectory.. "/Courseplay.xml", "Courseplay", g_Courseplay.schema)
g_Courseplay.globalSettings:saveToXMLFile(xmlFile,g_Courseplay.BASE_KEY)
xmlFile:save()
xmlFile:delete()
end
end
FSCareerMissionInfo.saveToXMLFile = Utils.appendedFunction(FSCareerMissionInfo.saveToXMLFile,Courseplay.saveToXMLFile)
function Courseplay:update(dt)
g_devHelper:update()
end
function Courseplay:draw()
g_devHelper:draw()
CpDebug:draw()
end
---@param posX number
---@param posY number
---@param isDown boolean
---@param isUp boolean
---@param button number
function Courseplay:mouseEvent(posX, posY, isDown, isUp, button)
end
---@param unicode number
---@param sym number
---@param modifier number
---@param isDown boolean
function Courseplay:keyEvent(unicode, sym, modifier, isDown)
g_devHelper:keyEvent(unicode, sym, modifier, isDown)
end
function Courseplay:load()
--- Base cp folder
self.baseDir = getUserProfileAppPath() .. "modSettings/" .. Courseplay.MOD_NAME .. "/"
createFolder(self.baseDir)
--- Sub folder for debug information
self.debugDir = self.baseDir .. "Debug/"
createFolder(self.debugDir)
--- Sub folder for debug prints
self.debugPrintDir = self.debugDir .. "DebugPrints/"
createFolder(self.debugPrintDir)
--- Default path to save prints without an explicit name.
self.defaultDebugPrintPath = self.debugDir .. "DebugPrint.xml"
self.courseDir = self.baseDir .. "Courses"
createFolder(self.courseDir)
self.courseStorage = FileSystem(self.courseDir,g_currentMission.missionInfo.mapId)
g_courseManger = self.courseStorage
g_courseDisplay = CourseDisplay()
g_vehicleConfigurations:loadFromXml()
end
function Courseplay:registerConsoleCommands()
addConsoleCommand( 'cpAddMoney', 'adds money', 'addMoney',self)
addConsoleCommand( 'cpRestartSaveGame', 'Load and start a savegame', 'restartSaveGame',self)
addConsoleCommand( 'print', 'Print a variable', 'printVariable', self )
addConsoleCommand( 'printGlobalCpVariable', 'Print a global cp variable', 'printGlobalCpVariable', self )
addConsoleCommand( 'printVehicleVariable', 'Print g_currentMission.controlledVehicle.variable', 'printVehicleVariable', self )
addConsoleCommand( 'printStrategyVariable', 'Print g_currentMission.controlledVehicle.spec_aiFieldWorker.driveStrategies[2].variable', 'printStrategyVariable', self )
addConsoleCommand( 'cpLoadFile', 'Load a lua file', 'loadFile', self )
addConsoleCommand( 'cpToggleDevHelper', 'Toggle development helper visual debug info', 'toggleDevHelper', self )
addConsoleCommand( 'cpSaveAllFields', 'Save all fields of the map to an XML file for offline debugging', 'saveAllFields', self )
end
---@param saveGameNumber number
function Courseplay:restartSaveGame(saveGameNumber)
if g_server then
doRestart(true, " -autoStartSavegameId " .. saveGameNumber)
Courseplay.info('Restarting savegame %d', saveGameNumber)
end
end
---@param amount number
function Courseplay:addMoney(amount)
g_currentMission:addMoney(amount ~= nil and tonumber(amount) or 0, g_currentMission.player.farmId, MoneyType.OTHER)
end
---Prints a variable to the console or a xmlFile.
---@param variableName string name of the variable, can be multiple levels
---@param maxDepth number maximum depth, 1 by default
---@param printToXML number should the variable be printed to an xml file ? (optional)
---@param printToSeparateXmlFiles number should the variable be printed to an xml file named after the variable ? (optional)
function Courseplay:printVariable(variableName, maxDepth,printToXML, printToSeparateXmlFiles)
if printToXML and tonumber(printToXML) and tonumber(printToXML)>0 then
CpUtil.printVariableToXML(variableName, maxDepth,printToSeparateXmlFiles)
return
end
CpUtil.printVariable(variableName, maxDepth)
end
function Courseplay:printVariableInternal(prefix, variableName, maxDepth,printToXML,printToSeparateXmlFiles)
if not string.startsWith(variableName, ':') and not string.startsWith(variableName, '.') then
-- allow to omit the . at the beginning of the variable name.
prefix = prefix .. '.'
end
self:printVariable(prefix .. variableName, maxDepth,printToXML,printToSeparateXmlFiles)
end
--- Print the variable in the selected vehicle's namespace
-- You can omit the dot for data members but if you want to call a function, you must start the variable name with a colon
function Courseplay:printVehicleVariable(variableName, maxDepth, printToXML,printToSeparateXmlFiles)
local prefix = variableName and 'g_currentMission.controlledVehicle' or 'g_currentMission'
variableName = variableName or 'controlledVehicle'
self:printVariableInternal( prefix, variableName, maxDepth, printToXML,printToSeparateXmlFiles)
end
function Courseplay:printStrategyVariable(variableName, maxDepth, printToXML,printToSeparateXmlFiles)
local prefix = 'g_currentMission.controlledVehicle.spec_aiFieldWorker.driveStrategies[2]'
self:printVariableInternal( prefix, variableName, maxDepth, printToXML,printToSeparateXmlFiles)
end
function Courseplay:printGlobalCpVariable(variableName, maxDepth, printToXML,printToSeparateXmlFiles)
if variableName then
self:printVariableInternal( 'g_Courseplay', variableName, maxDepth, printToXML,printToSeparateXmlFiles)
else
self:printVariable('g_Courseplay', maxDepth, printToXML,printToSeparateXmlFiles)
end
end
--- Load a Lua file
--- This is to reload scripts without restarting the game.
function Courseplay:loadFile(fileName)
fileName = fileName or 'reload.xml'
local path = Courseplay.BASE_DIRECTORY .. '/' .. fileName
if fileExists(path) then
g_xmlFile = loadXMLFile('loadFile', path)
end
if not g_xmlFile then
return 'Could not load ' .. path
else
local code = getXMLString(g_xmlFile, 'code')
local f = getfenv(0).loadstring('setfenv(1, '.. Courseplay.MOD_NAME .. '); ' .. code)
if f then
f()
return 'OK: ' .. path .. ' loaded.'
else
return 'ERROR: ' .. path .. ' could not be compiled.'
end
end
end
function Courseplay:toggleDevHelper()
g_devHelper:toggle()
end
function Courseplay:saveAllFields()
CpFieldUtil.saveAllFields()
end
function Courseplay.info(...)
local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0
local timestamp = getDate( ":%S")
print(string.format('%s [info lp%d] %s', timestamp, updateLoopIndex, string.format( ... )))
end
function Courseplay.infoVehicle(vehicle, ...)
local vehicleName = vehicle and vehicle:getName() or "Unknown vehicle"
local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0
local timestamp = getDate( ":%S")
print(string.format('%s [info lp%d] %s: %s', timestamp, updateLoopIndex, vehicleName, string.format( ... )))
end
function Courseplay.error(str,...)
Courseplay.info("error: "..str,...)
end
--- Fixes global translations.
function Courseplay.getText(i18n,superFunc,name,customEnv)
return superFunc(i18n,name,customEnv or Courseplay.MOD_NAME)
end
I18N.getText = Utils.overwrittenFunction(I18N.getText,Courseplay.getText)
--- Registers all cp specializations.
---@param typeManager TypeManager
function Courseplay.register(typeManager)
--- TODO: make this function async.
for typeName, typeEntry in pairs(typeManager.types) do
if CourseplaySpec.prerequisitesPresent(typeEntry.specializations) then
typeManager:addSpecialization(typeName, Courseplay.MOD_NAME .. ".courseplaySpec")
end
if CpVehicleSettings.prerequisitesPresent(typeEntry.specializations) then
typeManager:addSpecialization(typeName, Courseplay.MOD_NAME .. ".cpVehicleSettings")
end
if CpCourseGeneratorSettings.prerequisitesPresent(typeEntry.specializations) then
typeManager:addSpecialization(typeName, Courseplay.MOD_NAME .. ".cpCourseGeneratorSettings")
end
if CpCourseManager.prerequisitesPresent(typeEntry.specializations) then
typeManager:addSpecialization(typeName, Courseplay.MOD_NAME .. ".cpCourseManager")
end
--- TODO: Implement this specialization
CpAIFieldWorker.register(typeManager,typeName,typeEntry.specializations)
end
end
TypeManager.finalizeTypes = Utils.prependedFunction(TypeManager.finalizeTypes, Courseplay.register)
g_Courseplay = Courseplay()
addModEventListener(g_Courseplay)