LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

ConfigurationUtil

Description
Vehicle configuration util class
Functions

addBoughtConfiguration

Description
Add bought configuration
Definition
addBoughtConfiguration(string name, Integer id)
Arguments
stringnameof bought configuration type
Integeridid of bought configuration
Code
20function ConfigurationUtil.addBoughtConfiguration(object, name, id)
21 if g_configurationManager:getConfigurationIndexByName(name) ~= nil then
22 if object.boughtConfigurations[name] == nil then
23 object.boughtConfigurations[name] = {}
24 end
25 object.boughtConfigurations[name][id] = true
26 end
27end

applyConfigMaterials

Description
Apply materials defined in the given config
Definition
applyConfigMaterials(table object, table xmlFile, string configName, integer configId)
Arguments
tableobjectobject (vehicle)
tablexmlFilexml file object
stringconfigNamename of config
integerconfigIdid of config to apply
Code
138function ConfigurationUtil.applyConfigMaterials(object, xmlFile, configName, configId)
139 local configuration = g_configurationManager:getConfigurationDescByName(configName)
140 local xmlKey = configuration.xmlKey
141 if xmlKey ~= nil then
142 xmlKey = "."..xmlKey
143 else
144 xmlKey = ""
145 end
146
147 local configKey = string.format("vehicle%s.%sConfigurations.%sConfiguration(%d)", xmlKey, configName, configName, configId-1)
148 if xmlFile:hasProperty(configKey) then
149 xmlFile:iterate(configKey .. ".material", function(_, key)
150 local baseMaterialNode = xmlFile:getValue(key.."#node", nil, object.components, object.i3dMappings)
151 local refMaterialNode = xmlFile:getValue(key.."#refNode", nil, object.components, object.i3dMappings)
152 if baseMaterialNode ~= nil and refMaterialNode ~= nil then
153 local oldMaterial = getMaterial(baseMaterialNode, 0)
154 local newMaterial = getMaterial(refMaterialNode, 0)
155 for _, component in pairs(object.components) do
156 ConfigurationUtil.replaceMaterialRec(object, component.node, oldMaterial, newMaterial)
157 end
158 end
159
160 local materialName = xmlFile:getValue(key .. "#name")
161 if materialName ~= nil then
162 local shaderParameterName = xmlFile:getValue(key .. "#shaderParameter")
163 if shaderParameterName ~= nil then
164 local color = xmlFile:getValue(key.."#color", nil, true)
165 if color ~= nil then
166 local materialId = xmlFile:getValue(key.."#materialId")
167 if object.setBaseMaterialColor ~= nil then
168 object:setBaseMaterialColor(materialName, shaderParameterName, color, materialId)
169 end
170 end
171 end
172 end
173 end)
174 end
175end

getColorByConfigId

Description
Returns color of config id
Definition
getColorByConfigId(string configName, Integer configId)
Arguments
stringconfigNamename if config
IntegerconfigIdid of config to get color
Return Values
tablecolorcolor and material(r, g, b, mat)
Code
55function ConfigurationUtil.getColorByConfigId(object, configName, configId)
56 if configId ~= nil then
57 local item = g_storeManager:getItemByXMLFilename(object.configFileName)
58 if item.configurations ~= nil then
59 local config = item.configurations[configName][configId]
60 if config ~= nil then
61 local r, g, b = unpack(config.color)
62 return {r, g, b, config.material}
63 end
64 end
65 end
66
67 return nil
68end

getColorFromString

Description
Get color from string
Definition
getColorFromString(string colorString)
Arguments
stringcolorStringcolor rgba string or brand color identifier
Return Values
tablecolorcolor (r, g, b)
Code
422function ConfigurationUtil.getColorFromString(colorString)
423 if colorString ~= nil then
424 local colorVector = g_brandColorManager:getBrandColorByName(colorString) or {colorString:getVector()}
425
426 if colorVector == nil or #colorVector < 3 or #colorVector > 4 then
427 print("Error: Invalid color string '" .. colorString .. "'")
428 return nil
429 end
430 return colorVector
431 end
432 return nil
433end

getConfigColorPostLoad

Description
Get config color post load
Definition
getConfigColorPostLoad(integer xmlFile, string baseKey, string baseDir, string customEnvironment, boolean isMod, table configurationItems)
Arguments
integerxmlFileid of xml object
stringbaseKeybase key
stringbaseDirbase directory
stringcustomEnvironmentcustom environment
booleanisModis mod
tableconfigurationItemsconfig items
Code
353function ConfigurationUtil.getConfigColorPostLoad(xmlFile, baseKey, baseDir, customEnvironment, isMod, configurationItems, storeItem)
354 local defaultColorIndex = xmlFile:getValue(baseKey.."#defaultColorIndex")
355
356 if xmlFile:getValue(baseKey.."#useDefaultColors", false) then
357 local price = xmlFile:getValue(baseKey.."#price", 1000)
358
359 for i, color in pairs(g_vehicleColors) do
360 local configItem = StoreItemUtil.addConfigurationItem(configurationItems, "", "", price, 0, false)
361 if color.r ~= nil and color.g ~= nil and color.b ~= nil then
362 configItem.color = {color.r, color.g, color.b, 1}
363 elseif color.brandColor ~= nil then
364 configItem.color = g_brandColorManager:getBrandColorByName(color.brandColor)
365
366 if configItem.color == nil then
367 configItem.color = {1, 1, 1, 1}
368 Logging.warning("Unable to find brandColor '%s' in g_vehicleColors", color.brandColor)
369 end
370 end
371
372 configItem.name = g_i18n:convertText(color.name)
373
374 if i == defaultColorIndex then
375 configItem.isDefault = true
376 configItem.price = 0
377 end
378 end
379 end
380
381 if defaultColorIndex == nil then
382 local defaultIsDefined = false
383 for _, item in ipairs(configurationItems) do
384 if item.isDefault ~= nil and item.isDefault then
385 defaultIsDefined = true
386 end
387 end
388
389 if not defaultIsDefined then
390 if #configurationItems > 0 then
391 configurationItems[1].isDefault = true
392 configurationItems[1].price = 0
393 end
394 end
395 end
396end

getConfigColorSingleItemLoad

Description
Get config color single item load
Definition
getConfigColorSingleItemLoad(integer xmlFile, string baseXMLName, string baseDir, string customEnvironment, boolean isMod, table configItem)
Arguments
integerxmlFileid of xml object
stringbaseXMLNamebase xml name
stringbaseDirbase directory
stringcustomEnvironmentcustom environment
booleanisModis mod
tableconfigItemconfig item
Code
337function ConfigurationUtil.getConfigColorSingleItemLoad(xmlFile, baseXMLName, baseDir, customEnvironment, isMod, configItem)
338 configItem.color = xmlFile:getValue(baseXMLName.."#color", "1 1 1", true)
339 configItem.uiColor = xmlFile:getValue(baseXMLName.."#uiColor", configItem.color, true)
340 configItem.material = xmlFile:getValue(baseXMLName.."#material")
341
342 configItem.name = ConfigurationUtil.loadConfigurationNameFromXML(xmlFile, baseXMLName, customEnvironment)
343end

getConfigIdBySaveId

Description
Returns config id from given save identifier
Definition
getConfigIdBySaveId(table object, string configName, string saveId)
Arguments
tableobjectobject
stringconfigNamename if config
stringsaveIdsave identifier
Return Values
integerconfigIdconfig id
Code
97function ConfigurationUtil.getConfigIdBySaveId(configFileName, configName, configId)
98 local item = g_storeManager:getItemByXMLFilename(configFileName)
99 if item.configurations ~= nil then
100 local configs = item.configurations[configName]
101 if configs ~= nil then
102 for j=1, #configs do
103 if configs[j].saveId == configId then
104 return configs[j].index
105 end
106 end
107 end
108 end
109
110 return 1
111end

getConfigMaterialSingleItemLoad

Description
Definition
getConfigMaterialSingleItemLoad()
Code
401function ConfigurationUtil.getConfigMaterialSingleItemLoad(xmlFile, baseXMLName, baseDir, customEnvironment, isMod, configItem)
402 configItem.color = xmlFile:getValue(baseXMLName.."#color", "1 1 1", true)
403 configItem.material = xmlFile:getValue(baseXMLName.."#material")
404end

getConfigurationValue

Description
Get value of configuration
Definition
getConfigurationValue(integer xmlFile, string key, string subKey, string param, any_type defaultValue, string fallbackConfigKey, string fallbackOldgKey)
Arguments
integerxmlFileid of xml object
stringkeykey
stringsubKeysub key
stringparamparameter
any_typedefaultValuedefault value
stringfallbackConfigKeyfallback config key
stringfallbackOldgKeyfallback old key
Return Values
any_typevaluevalue of config
Code
285function ConfigurationUtil.getConfigurationValue(xmlFile, key, subKey, param, defaultValue, fallbackConfigKey, fallbackOldKey)
286 if type(subKey) == "table" then
287 printCallstack()
288 end
289 local value = nil
290 if key ~= nil then
291 value = xmlFile:getValue(key..subKey..param)
292 end
293
294 if value == nil and fallbackConfigKey ~= nil then
295 value = xmlFile:getValue(fallbackConfigKey..subKey..param) -- Check for default configuration (xml index 0)
296 end
297 if value == nil and fallbackOldKey ~= nil then
298 value = xmlFile:getValue(fallbackOldKey..subKey..param) -- Fallback to old xml setup
299 end
300 return Utils.getNoNil(value, defaultValue) -- using default value
301end

getMaterialByConfigId

Description
Returns material of config id
Definition
getMaterialByConfigId(string configName, Integer configId)
Arguments
stringconfigNamename if config
IntegerconfigIdid of config to get color
Return Values
integermaterialmaterial
Code
118function ConfigurationUtil.getMaterialByConfigId(object, configName, configId)
119 if configId ~= nil then
120 local item = g_storeManager:getItemByXMLFilename(object.configFileName)
121 if item.configurations ~= nil then
122 local config = item.configurations[configName][configId]
123 if config ~= nil then
124 return config.material
125 end
126 end
127 end
128
129 return nil
130end

getOverwrittenMaterialColors

Description
Searches in all configurations for defined materials with the given search name and returns the color and material
Definition
getOverwrittenMaterialColors(table object, table xmlFile, table targetTable)
Arguments
tableobjectobject (vehicle)
tablexmlFilexml file object
tabletargetTabletarget table with names as indices
Return Values
tablecolorcolor
integermaterialIdmaterial id
Code
184function ConfigurationUtil.getOverwrittenMaterialColors(object, xmlFile, targetTable)
185 for configName, configId in pairs(object.configurations) do
186 local configuration = g_configurationManager:getConfigurationDescByName(configName)
187 local xmlKey = configuration.xmlKey
188 if xmlKey ~= nil then
189 xmlKey = "."..xmlKey
190 else
191 xmlKey = ""
192 end
193
194 local configKey = string.format("vehicle%s.%sConfigurations.%sConfiguration(%d)", xmlKey, configName, configName, configId-1)
195 if xmlFile:hasProperty(configKey) then
196 xmlFile:iterate(configKey .. ".material", function(_, key)
197 local materialName = xmlFile:getValue(key .. "#name")
198 if materialName ~= nil then
199 for name, _ in pairs(targetTable) do
200 if materialName == name then
201 local color = xmlFile:getValue(key.."#color", nil, true)
202 if color ~= nil and #color > 0 then
203 local materialId = xmlFile:getValue(key.."#materialId")
204
205 targetTable[name][1] = color[1]
206 targetTable[name][2] = color[2]
207 targetTable[name][3] = color[3]
208 targetTable[name][4] = materialId
209 end
210 end
211 end
212 end
213 end)
214 end
215 end
216end

getSaveIdByConfigId

Description
Returns save identifier from given config id
Definition
getSaveIdByConfigId(table object, string configName, Integer configId)
Arguments
tableobjectobject
stringconfigNamename if config
IntegerconfigIdid of config to get color
Return Values
stringsaveIdsave identifier
Code
76function ConfigurationUtil.getSaveIdByConfigId(configFileName, configName, configId)
77 local item = g_storeManager:getItemByXMLFilename(configFileName)
78 if item.configurations ~= nil then
79 local configs = item.configurations[configName]
80 if configs ~= nil then
81 local config = configs[configId]
82 if config ~= nil then
83 return config.saveId
84 end
85 end
86 end
87
88 return nil
89end

getStoreAdditionalConfigData

Description
Get store additional config data
Definition
getStoreAdditionalConfigData(integer xmlFile, string baseXMLName, string baseDir, string customEnvironment, boolean isMod, table configItem)
Arguments
integerxmlFileid of xml object
stringbaseXMLNamebase xml name
stringbaseDirbase directory
stringcustomEnvironmentcustom environment
booleanisModis mod
tableconfigItemconfig item
Code
414function ConfigurationUtil.getStoreAdditionalConfigData(xmlFile, baseXMLName, baseDir, customEnvironment, isMod, configItem)
415 configItem.vehicleType = xmlFile:getValue(baseXMLName.."#vehicleType")
416end

getXMLConfigurationKey

Description
Get xml configuration key
Definition
getXMLConfigurationKey(integer xmlFile, Integer index, string key, string defaultKey, string configurationKey)
Arguments
integerxmlFileid of xml object
Integerindexindex
stringkeykey
stringdefaultKeydefault key
stringconfigurationKeyconfiguration key
Return Values
stringconfigKeykey of configuration
IntegerconfigIndexindex of configuration
Code
312function ConfigurationUtil.getXMLConfigurationKey(xmlFile, index, key, defaultKey, configurationKey)
313 local configIndex = Utils.getNoNil(index, 1)
314 local configKey = string.format(key.."(%d)", configIndex-1)
315 if index ~= nil and not xmlFile:hasProperty(configKey) then
316 print("Warning: Invalid "..configurationKey.." index '"..tostring(index).."' in '"..key.."'. Using default "..configurationKey.." settings instead!")
317 end
318
319 if not xmlFile:hasProperty(configKey) then
320 configKey = key.."(0)"
321 end
322 if not xmlFile:hasProperty(configKey) then
323 configKey = defaultKey
324 end
325
326 return configKey, configIndex
327end

hasBoughtConfiguration

Description
Returns true if configuration has been bought
Definition
hasBoughtConfiguration(string name, Integer id)
Arguments
stringnameof bought configuration type
Integeridid of bought configuration
Return Values
booleanconfigurationHasBeenBoughtconfiguration has been bought
Code
34function ConfigurationUtil.hasBoughtConfiguration(object, name, id)
35 if object.boughtConfigurations[name] ~= nil and object.boughtConfigurations[name][id] then
36 return true
37 end
38 return false
39end

isColorMetallic

Description
Get whether a material is visualized as metallic in UI
Definition
isColorMetallic()
Code
487function ConfigurationUtil.isColorMetallic(materialId)
488 return materialId == 2
489 or materialId == 3
490 or materialId == 19
491 or materialId == 30
492 or materialId == 31
493 or materialId == 35
494end

loadConfigurationNameFromXML

Description
Returns formatted config name that is loaded from xml
Definition
loadConfigurationNameFromXML(integer xmlFile, string configKey, string customEnvironment)
Arguments
integerxmlFileid of xml object
stringconfigKeyconfiguration key
stringcustomEnvironmentcustom environment
Return Values
stringconfigNameconfig name
Code
441function ConfigurationUtil.loadConfigurationNameFromXML(xmlFile, configKey, customEnvironment)
442 local configName = xmlFile:getValue(configKey.."#name", nil, customEnvironment, false)
443 local params = xmlFile:getValue(configKey.."#params")
444 if params ~= nil then
445 params = params:split("|")
446 for i=1, #params do
447 params[i] = g_i18n:convertText(params[i])
448 end
449 configName = string.format(configName, unpack(params))
450 end
451
452 return configName
453end

registerColorConfigurationXMLPaths

Description
Register color configuration paths
Definition
registerColorConfigurationXMLPaths()
Code
457function ConfigurationUtil.registerColorConfigurationXMLPaths(schema, configurationName)
458 local baseKey = string.format("vehicle.%sConfigurations", configurationName)
459 schema:register(XMLValueType.INT, baseKey .. "#defaultColorIndex", "Default color index on start")
460 schema:register(XMLValueType.BOOL, baseKey .. "#useDefaultColors", "Use default colors", false)
461 schema:register(XMLValueType.INT, baseKey .. "#price", "Default color price", 1000)
462
463 schema:register(XMLValueType.NODE_INDEX, baseKey .. ".colorNode(?)#node", "Color node")
464 schema:register(XMLValueType.BOOL, baseKey .. ".colorNode(?)#recursive", "Apply recursively")
465
466 local itemKey = string.format("%s.%sConfiguration(?)", baseKey, configurationName)
467 schema:register(XMLValueType.COLOR, itemKey .. "#color", "Configuration color", "1 1 1 1")
468 schema:register(XMLValueType.COLOR, itemKey .. "#uiColor", "Configuration UI color", "1 1 1 1")
469 schema:register(XMLValueType.INT, itemKey .. "#material", "Configuration material")
470 schema:register(XMLValueType.L10N_STRING, itemKey .. "#name", "Color name")
471end

registerMaterialConfigurationXMLPaths

Description
Register material configuration paths
Definition
registerMaterialConfigurationXMLPaths()
Code
475function ConfigurationUtil.registerMaterialConfigurationXMLPaths(schema, configurationName)
476 schema:register(XMLValueType.NODE_INDEX, configurationName .. ".material(?)#node", "Material node")
477 schema:register(XMLValueType.NODE_INDEX, configurationName .. ".material(?)#refNode", "Material reference node")
478
479 schema:register(XMLValueType.STRING, configurationName .. ".material(?)#name", "Material name")
480 schema:register(XMLValueType.STRING, configurationName .. ".material(?)#shaderParameter", "Material shader parameter name")
481 schema:register(XMLValueType.COLOR, configurationName .. ".material(?)#color", "Color")
482 schema:register(XMLValueType.INT, configurationName .. ".material(?)#materialId", "Material id")
483end

replaceMaterialRec

Description
Replace material of node
Definition
replaceMaterialRec(Integer node, Integer oldMaterial, Integer newMaterial)
Arguments
Integernodeid of node
IntegeroldMaterialid of old material
IntegernewMaterialid of new material
Code
223function ConfigurationUtil.replaceMaterialRec(object, node, oldMaterial, newMaterial)
224 if getHasClassId(node, ClassIds.SHAPE) then
225 local nodeMaterial = getMaterial(node, 0)
226 if nodeMaterial == oldMaterial then
227 setMaterial(node, newMaterial, 0)
228 end
229 end
230
231 local numChildren = getNumOfChildren(node)
232 if numChildren > 0 then
233 for i=0, numChildren-1 do
234 ConfigurationUtil.replaceMaterialRec(object, getChildAt(node, i), oldMaterial, newMaterial)
235 end
236 end
237end

setColor

Description
Sets color of vehicle
Definition
setColor(integer xmlFile, string configName, Integer configColorId)
Arguments
integerxmlFileid of xml object
stringconfigNamename of config
IntegerconfigColorIdid of config color to use
Code
244function ConfigurationUtil.setColor(object, xmlFile, configName, configColorId)
245 local color = ConfigurationUtil.getColorByConfigId(object, configName, configColorId)
246 if color ~= nil then
247 local r,g,b,mat = unpack(color)
248 local i = 0
249 while true do
250 local colorKey = string.format("vehicle.%sConfigurations.colorNode(%d)", configName, i)
251 if not xmlFile:hasProperty(colorKey) then
252 break
253 end
254
255 local node = xmlFile:getValue(colorKey .. "#node", nil, object.components, object.i3dMappings)
256 if node ~= nil then
257 if getHasClassId(node, ClassIds.SHAPE) then
258 if mat == nil then
259 _,_,_,mat = getShaderParameter(node, "colorScale")
260 end
261 if xmlFile:getValue(colorKey .. "#recursive", false) then
262 I3DUtil.setShaderParameterRec(node, "colorScale", r, g, b, mat)
263 else
264 setShaderParameter(node, "colorScale", r, g, b, mat, false)
265 end
266 else
267 print("Warning: Could not set vehicle color to '"..getName(node).."' because node is not a shape!")
268 end
269 end
270 i = i + 1
271 end
272 end
273end

setConfiguration

Description
Set configuration value
Definition
setConfiguration(string name, Integer id)
Arguments
stringnamename of configuration type
Integeridid of configuration value
Code
45function ConfigurationUtil.setConfiguration(object, name, id)
46 object.configurations[name] = id
47end