This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 525
/
VehicleConfigurations.lua
149 lines (135 loc) · 6.09 KB
/
VehicleConfigurations.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
--[[
This file is part of Courseplay (https://github.com/Courseplay/courseplay)
Copyright (C) 2021 Peter Vaiko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]
--- A container for custom vehicle configurations.
---
--- Allows to customize vehicle data XML file(s). A standard configuration is installed with Courseplay,
--- custom user configs can be placed in <your save game folder>/modsSettings/Courseplay/vehicleConfigurations.xml
---
--- Custom configs are read last and override the standard config when exist.
---
--- You can use the cpReadVehicleConfigurations console command to reload these files in-game.
---
---@class VehicleConfigurations : CpObject
VehicleConfigurations = CpObject()
VehicleConfigurations.attributes = {
{name = 'toolOffsetX', getXmlFunction = getXMLFloat},
{name = 'noReverse', getXmlFunction = getXMLBool},
{name = 'turnRadius', getXmlFunction = getXMLFloat},
{name = 'workingWidth', getXmlFunction = getXMLFloat},
{name = 'balerUnloadDistance', getXmlFunction = getXMLFloat},
{name = 'directionNodeOffsetZ', getXmlFunction = getXMLFloat},
{name = 'implementWheelAlwaysOnGround', getXmlFunction = getXMLBool},
{name = 'ignoreCollisionBoxesWhenFolded', getXmlFunction = getXMLBool},
{name = 'baleCollectorOffset', getXmlFunction = getXMLFloat},
}
function VehicleConfigurations:init()
self.vehicleConfigurations = {}
if g_currentMission then
self:loadFromXml()
end
end
function VehicleConfigurations:loadFromXml()
self.xmlFileName = Utils.getFilename('config/VehicleConfigurations.xml', courseplay.path)
self.xmlFile = self:loadXmlFile(self.xmlFileName)
self.userXmlFileName = getUserProfileAppPath() .. 'modsSettings/Courseplay/vehicleConfigurations.xml'
self.userXmlFile = self:loadXmlFile(self.userXmlFileName)
end
function VehicleConfigurations:addAttribute(vehicleConfiguration, xmlFile, vehicleElement, attribute, getXmlFunction)
local configValue = getXmlFunction(xmlFile, vehicleElement .. '#' .. attribute)
if configValue then
vehicleConfiguration[attribute] = configValue
local valueAsString = ''
if type(configValue) == 'number' then
valueAsString = string.format('%.1f', configValue)
else
valueAsString = string.format('%s', tostring(configValue))
end
courseplay.info('\\__ %s = %s', attribute, valueAsString)
end
end
function VehicleConfigurations:readVehicle(xmlFile, vehicleElement)
local vehicleConfiguration = {}
local name = getXMLString(xmlFile, vehicleElement .. "#name")
courseplay.info('Reading configuration for %s', name)
for _, attribute in ipairs(self.attributes) do
self:addAttribute(vehicleConfiguration, xmlFile, vehicleElement, attribute.name, attribute.getXmlFunction)
end
self.vehicleConfigurations[name] = vehicleConfiguration
end
function VehicleConfigurations:loadXmlFile(fileName)
courseplay.info('Loading vehicle configuration from %s ...', fileName)
if fileExists(fileName) then
local xmlFile = loadXMLFile('vehicleConfigurations', fileName);
local rootElement = 'VehicleConfigurations'
if xmlFile and hasXMLProperty(xmlFile, rootElement) then
local i = 0
while true do
local vehicleElement = string.format('%s.Vehicle(%d)', rootElement, i)
if hasXMLProperty(xmlFile, vehicleElement) then
self:readVehicle(xmlFile, vehicleElement)
else
break
end
i = i + 1
end
return xmlFile
end
else
courseplay.info('Vehicle configuration file %s does not exist.', fileName)
end
end
--- Get a custom configuration value for a single vehicle/implement
--- @param object table vehicle or implement object. This function uses the object's configFileName to uniquely
--- identify the vehicle/implement.
--- @param attribute string configuration attribute to get
--- @return any the value of the configuration attribute or nil if there's no custom config for it
function VehicleConfigurations:get(object, attribute)
if not g_server then
courseplay.info("Error: VehicleConfigurations:get() %s",attribute)
return
end
if object and object.configFileName then
local vehicleXmlFileName = courseplay.utils:getFileNameFromPath(object.configFileName)
if self.vehicleConfigurations[vehicleXmlFileName] then
return self.vehicleConfigurations[vehicleXmlFileName][attribute]
else
return nil
end
end
end
--- Get a custom configuration value for an object and its attached implements.
--- First checks the vehicle itself, then all its attached implements until the attribute is found. If the same
--- attribute is defined on multiple implements, only the first is returned
--- @param vehicle table vehicle
--- @param attribute string configuration attribute to get
--- @return any the value of the configuration attribute or nil if there's no custom config for it
function VehicleConfigurations:getRecursively(object, attribute)
if not g_server then
courseplay.info("Error: VehicleConfigurations:getRecursively() %s",attribute)
return
end
local value = self:get(object, attribute)
if value then
return value
end
for _, implement in pairs(object:getAttachedImplements()) do
value = self:getRecursively(implement.object, attribute)
if value then
return value
end
end
return nil
end
g_vehicleConfigurations = VehicleConfigurations()