LUADOC - Farming Simulator 19

VehicleTypeManager

Description
This class handles all vehicle types
Parent
AbstractManager
Functions

addSpecialization

Description
Definition
addSpecialization()
Code
194function VehicleTypeManager:addSpecialization(typeName, specName)
195 local typeEntry = self.vehicleTypes[typeName]
196 if typeEntry ~= nil then
197 if typeEntry.specializationsByName[specName] == nil then
198 local spec = g_specializationManager:getSpecializationObjectByName(specName)
199 if spec == nil then
200 print("Error: Vehicle type '"..tostring(typeName).."' has unknown specialization '" .. tostring(specName).."'!")
201 return false
202 end
203
204 table.insert(typeEntry.specializations, spec)
205 table.insert(typeEntry.specializationNames, specName)
206 typeEntry.specializationsByName[specName] = spec
207 else
208 g_logManager:error("Specialization '%s' already exists for vehicle type '%s'!", specName, typeName)
209 end
210 else
211 g_logManager:error("VehicleType '%s' is not defined!", typeName)
212 end
213end

finalizeVehicleTypes

Description
Definition
finalizeVehicleTypes()
Code
233function VehicleTypeManager:finalizeVehicleTypes()
234 -- local checkSpecs = {}
235 -- for name, spec in pairs(g_specializationManager:getSpecializations()) do
236 -- checkSpecs[name] = {}
237 -- for funcName, obj in pairs(g_specializationManager:getSpecializationObjectByName(name)) do
238 -- if type(obj) == "function" and not StringUtil.startsWith(funcName, "consoleCommand") and not StringUtil.startsWith(funcName, "action") then
239 -- checkSpecs[name][funcName] = true
240 -- end
241 -- end
242 -- end
243
244 -- local ignoreList = {}
245 -- ignoreList["registerJointType"] = true
246 -- ignoreList["initSpecialization"] = true
247 -- ignoreList["registerEventListeners"] = true
248 -- ignoreList["registerOverwrittenFunctions"] = true
249 -- ignoreList["registerFunctions"] = true
250 -- ignoreList["registerEvents"] = true
251 -- ignoreList["prerequisitesPresent"] = true
252 -- ignoreList["saveToXMLFile"] = true
253 -- ignoreList["getDefaultSpeedLimit"] = true
254 -- ignoreList["updateDebugValues"] = true
255
256 -- for _, data in pairs(checkSpecs) do
257 -- for func, _ in pairs(ignoreList) do
258 -- data[func] = nil
259 -- end
260 -- end
261
262 -- local registeredEvent = {}
263 -- local registeredEventListeners = {}
264
265 for typeName, typeEntry in pairs(self.vehicleTypes) do
266 g_deferredLoadingManager:addSubtask(function()
267 local classObject = ClassUtil.getClassObject(typeEntry.className)
268 if classObject.registerEvents ~= nil then
269 classObject.registerEvents(typeEntry)
270 end
271
272 if classObject.registerFunctions ~= nil then
273 classObject.registerFunctions(typeEntry)
274 end
275
276 -- register events, functions, and overwritten functions for all specializations
277 for _,specialization in ipairs(typeEntry.specializations) do
278 if specialization.registerEvents ~= nil then
279 specialization.registerEvents(typeEntry)
280 end
281 end
282
283 for _,specialization in ipairs(typeEntry.specializations) do
284 if specialization.registerFunctions ~= nil then
285 specialization.registerFunctions(typeEntry)
286 end
287 end
288
289 for _,specialization in ipairs(typeEntry.specializations) do
290 if specialization.registerOverwrittenFunctions ~= nil then
291 specialization.registerOverwrittenFunctions(typeEntry)
292 end
293 end
294
295 for _,specialization in ipairs(typeEntry.specializations) do
296 if specialization.registerEventListeners ~= nil then
297 specialization.registerEventListeners(typeEntry)
298 end
299 end
300
301 if typeEntry.customEnvironment ~= "" then
302 print(" Register vehicle type: " .. typeName)
303 end
304 end)
305 end
306
307 -- -- remove used events
308 -- for name,_ in pairs(registeredEvent) do
309 -- if registeredEventListeners[name] ~= nil then
310 -- log("removed", name)
311 -- registeredEventListeners[name] = nil
312 -- registeredEvent[name] = nil
313 -- end
314 -- end
315
316 -- for name,_ in pairs(registeredEvent) do
317 -- log("Unused Event", name)
318 -- end
319 -- for name,_ in pairs(registeredEventListeners) do
320 -- log("Undefined Event", name)
321 -- end
322
323
324 -- for specName, data in pairs(checkSpecs) do
325 -- for funcName, _ in pairs(data) do
326 -- log(specName, funcName)
327 -- end
328 -- end
329
330 return true
331end

getVehicleTypeByName

Description
Definition
getVehicleTypeByName()
Code
341function VehicleTypeManager:getVehicleTypeByName(typeName)
342 if typeName ~= nil then
343 return self.vehicleTypes[typeName]
344 end
345end

getVehicleTypes

Description
Definition
getVehicleTypes()
Code
335function VehicleTypeManager:getVehicleTypes()
336 return self.vehicleTypes
337end

initDataStructures

Description
Initialize data structures
Definition
initDataStructures()
Code
24function VehicleTypeManager:initDataStructures()
25 self.vehicleTypes = {}
26end

loadMapData

Description
Load data on map load
Definition
loadMapData()
Return Values
booleantrueif loading was successful else false
Code
31function VehicleTypeManager:loadMapData()
32 VehicleTypeManager:superClass().loadMapData(self)
33
34 local xmlFile = loadXMLFile("VehicleTypesXML", "dataS/vehicleTypes.xml")
35
36 local i = 0
37 while true do
38 local key = string.format("vehicleTypes.type(%d)", i)
39 if not hasXMLProperty(xmlFile, key) then
40 break
41 end
42
43 g_deferredLoadingManager:addSubtask(function()
44 self:loadVehicleTypeFromXML(xmlFile, key, nil, nil, nil)
45 end)
46
47 i = i + 1
48 end
49
50 g_deferredLoadingManager:addSubtask(function()
51 delete(xmlFile)
52 end)
53
54 g_deferredLoadingManager:addSubtask(function()
55 print(" Loaded vehicle types")
56 end)
57
58 return true
59end

loadVehicleTypeFromXML

Description
Definition
loadVehicleTypeFromXML()
Code
109function VehicleTypeManager:loadVehicleTypeFromXML(xmlFile, key, isDLC, modDir, modName)
110
111 local typeName = getXMLString(xmlFile, key.. "#name")
112 local parentName = getXMLString(xmlFile, key.. "#parent")
113
114 if typeName == nil and parentName == nil then
115 g_logManager:error("Missing name or parent for vehicleType '%s'", key)
116 return false
117 end
118
119 local parent
120 if parentName ~= nil then
121 parent = self.vehicleTypes[parentName]
122 if parent == nil then
123 g_logManager:error("Parent vehicle type '%s' is not defined!", parentName)
124 return false
125 end
126 end
127
128 local className = getXMLString(xmlFile, key.. "#className")
129 local filename = getXMLString(xmlFile, key.. "#filename")
130 if parent ~= nil then
131 className = className or parent.className
132 filename = filename or parent.filename
133 end
134
135 if modName ~= nil and modName ~= "" then
136 typeName = modName.."."..typeName
137 end
138
139 if className ~= nil and filename ~= nil then
140 local customEnvironment = nil
141 if modDir ~= nil then
142 local useModDirectory = true
143 filename, useModDirectory = Utils.getFilename(filename, modDir)
144 if useModDirectory then
145 customEnvironment = modName
146 className = modName.."."..className
147 end
148 end
149
150
151 if not GS_IS_CONSOLE_VERSION or isDLC or customEnvironment == nil then
152 self:addVehicleType(typeName, className, filename, customEnvironment)
153
154 -- add parent specializations
155 if parent ~= nil then
156 for _, specName in ipairs(parent.specializationNames) do
157 self:addSpecialization(typeName, specName)
158 end
159 end
160
161 -- add vehicle type specializations
162 local j = 0
163 while true do
164 local specKey = string.format("%s.specialization(%d)", key, j)
165 if not hasXMLProperty(xmlFile, specKey) then
166 break
167 end
168
169 local specName = getXMLString(xmlFile, specKey.. "#name")
170 local entry = g_specializationManager:getSpecializationByName(specName)
171 if entry == nil then
172 specName = modName.."."..specName;
173 end
174
175 if specName ~= nil then
176 self:addSpecialization(typeName, specName)
177 end
178
179 j = j + 1
180 end
181
182 return true
183
184 else
185 g_logManager:error("Can't register vehicle type '%s' with scripts on consoles.", typeName)
186 end
187 end
188
189 return false
190end

new

Description
Creating manager
Definition
new()
Return Values
tableinstanceinstance of object
Code
16function VehicleTypeManager:new(customMt)
17 local self = AbstractManager:new(customMt or VehicleTypeManager_mt)
18
19 return self
20end

removeVehicleType

Description
Definition
removeVehicleType()
Code
103function VehicleTypeManager:removeVehicleType(typeName)
104 self.vehicleTypes[typeName] = nil
105end

validateVehicleTypes

Description
Definition
validateVehicleTypes()
Code
217function VehicleTypeManager:validateVehicleTypes()
218 for typeName, typeEntry in pairs(self.vehicleTypes) do
219 g_deferredLoadingManager:addSubtask(function()
220 for _, specName in ipairs(typeEntry.specializationNames) do
221 local spec = typeEntry.specializationsByName[specName]
222 if not spec.prerequisitesPresent(typeEntry.specializations) then
223 print("Error: Not all prerequisites of specialization " .. specName .. " are fulfilled")
224 self:removeVehicleType(typeName)
225 end
226 end
227 end)
228 end
229end