LUADOC - Farming Simulator 19

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

applyDesign

Description
Apply design config
Definition
applyDesign(integer xmlFile, Integer configDesignId)
Arguments
integerxmlFileid of xml object
IntegerconfigDesignIdid of design to apply
Code
91function ConfigurationUtil.applyDesign(object, xmlFile, configDesignId)
92 local designKey = string.format("vehicle.designConfigurations.designConfiguration(%d)", configDesignId-1)
93 if not hasXMLProperty(xmlFile, designKey) then
94 print("Warning: Invalid design configuration " .. configDesignId);
95 return
96 end
97 local i = 0
98 while true do
99 local materialKey = string.format(designKey..".material(%d)", i)
100 if not hasXMLProperty(xmlFile, materialKey) then
101 break
102 end
103 local baseMaterialNode = I3DUtil.indexToObject(object.components, getXMLString(xmlFile, materialKey.."#node"), object.i3dMappings);
104 local refMaterialNode = I3DUtil.indexToObject(object.components, getXMLString(xmlFile, materialKey.."#refNode"), object.i3dMappings);
105 if baseMaterialNode ~= nil and refMaterialNode ~= nil then
106 local oldMaterial = getMaterial(baseMaterialNode, 0)
107 local newMaterial = getMaterial(refMaterialNode, 0)
108 for _, component in pairs(object.components) do
109 ConfigurationUtil.replaceMaterialRec(object, component.node, oldMaterial, newMaterial);
110 end;
111 end
112
113 local materialName = getXMLString(xmlFile, materialKey .. "#name")
114 if materialName ~= nil then
115 local shaderParameterName = getXMLString(xmlFile, materialKey .. "#shaderParameter")
116 if shaderParameterName ~= nil then
117 local colorStr = getXMLString(xmlFile, materialKey.."#color")
118 if colorStr ~= nil then
119 local color = g_brandColorManager:getBrandColorByName(colorStr)
120 if color == nil then
121 color = ConfigurationUtil.getColorFromString(colorStr)
122 end
123 if color ~= nil then
124 local materialId = getXMLInt(xmlFile, materialKey.."#materialId")
125 if object.setBaseMaterialColor ~= nil then
126 object:setBaseMaterialColor(materialName, shaderParameterName, color, materialId)
127 end
128 end
129 end
130 end
131 end
132
133 i = i + 1
134 end
135 ObjectChangeUtil.updateObjectChanges(xmlFile, "vehicle.designConfigurations.designConfiguration", configDesignId, object.components, object);
136end

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 local configId = object.configurations[configName]
57 if configId ~= nil then
58 local item = g_storeManager:getItemByXMLFilename(object.configFileName)
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
66 return nil
67end

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
349function ConfigurationUtil.getColorFromString(colorString)
350 if colorString ~= nil then
351 local colorVector = g_brandColorManager:getBrandColorByName(colorString) or {StringUtil.getVectorFromString(colorString)}
352
353 if colorVector == nil or #colorVector < 3 or #colorVector > 4 then
354 print("Error: Invalid color string '" .. colorString .. "'")
355 return nil
356 end
357 return colorVector;
358 end
359 return nil;
360end

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
280function ConfigurationUtil.getConfigColorPostLoad(xmlFile, baseKey, baseDir, customEnvironment, isMod, configurationItems, storeItem)
281 local defaultColorIndex = getXMLInt(xmlFile, baseKey.."#defaultColorIndex")
282
283 if Utils.getNoNil(getXMLBool(xmlFile, baseKey.."#useDefaultColors"), false) then
284 local price = Utils.getNoNil(getXMLInt(xmlFile, baseKey.."#price"), 1000);
285
286 for i, color in pairs(g_vehicleColors) do
287 local configItem = StoreItemUtil.addConfigurationItem(configurationItems, "", "", price, 0, false)
288 if color.r ~= nil and color.g ~= nil and color.b ~= nil then
289 configItem.color = {color.r, color.g, color.b, 1}
290 elseif color.brandColor ~= nil then
291 configItem.color = g_brandColorManager:getBrandColorByName(color.brandColor)
292
293 if configItem.color == nil then
294 configItem.color = {1, 1, 1, 1}
295 g_logManager:warning("Unable to find brandColor '%s' in g_vehicleColors", color.brandColor)
296 end
297 end
298
299 configItem.name = g_i18n:convertText(color.name)
300
301 if i == defaultColorIndex then
302 configItem.isDefault = true
303 configItem.price = 0
304 end
305 end
306 end;
307
308 if defaultColorIndex == nil then
309 local defaultIsDefined = false
310 for _, item in ipairs(configurationItems) do
311 if item.isDefault ~= nil and item.isDefault then
312 defaultIsDefined = true
313 end
314 end
315
316 if not defaultIsDefined then
317 if #configurationItems > 0 then
318 configurationItems[1].isDefault = true
319 configurationItems[1].price = 0
320 end
321 end
322 end
323end

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
258function ConfigurationUtil.getConfigColorSingleItemLoad(xmlFile, baseXMLName, baseDir, customEnvironment, isMod, configItem)
259
260 local colorStr = Utils.getNoNil(getXMLString(xmlFile, baseXMLName.."#color"), "1 1 1 1")
261 local color = g_brandColorManager:getBrandColorByName(colorStr)
262 if color == nil then
263 color = ConfigurationUtil.getColorFromString(colorStr)
264 end
265
266 configItem.color = color
267 configItem.material = getXMLInt(xmlFile, baseXMLName.."#material")
268
269 configItem.name = XMLUtil.getXMLI18NValue(xmlFile, baseXMLName.."#name", getXMLString, "", "", customEnvironment, false)
270end

getConfigMaterialSingleItemLoad

Description
Definition
getConfigMaterialSingleItemLoad()
Code
328function ConfigurationUtil.getConfigMaterialSingleItemLoad(xmlFile, baseXMLName, baseDir, customEnvironment, isMod, configItem)
329 configItem.color = ConfigurationUtil.getColorFromString(Utils.getNoNil(getXMLString(xmlFile, baseXMLName.."#color"), "1 1 1 1"));
330 configItem.material = getXMLInt(xmlFile, baseXMLName.."#material")
331end

getConfigurationValue

Description
Get value of configuration
Definition
getConfigurationValue(integer xmlFile, string key, string subKey, string param, function xmlFunc, any_type defaultValue, string fallbackConfigKey, string fallbackOldgKey)
Arguments
integerxmlFileid of xml object
stringkeykey
stringsubKeysub key
stringparamparameter
functionxmlFuncxml function
any_typedefaultValuedefault value
stringfallbackConfigKeyfallback config key
stringfallbackOldgKeyfallback old key
Return Values
any_typevaluevalue of config
Code
206function ConfigurationUtil.getConfigurationValue(xmlFile, key, subKey, param, xmlFunc, defaultValue, fallbackConfigKey, fallbackOldKey)
207 if type(subKey) == "table" then
208 printCallstack();
209 end
210 local value = nil;
211 if key ~= nil then
212 value = xmlFunc(xmlFile, key..subKey..param);
213 end;
214
215 if value == nil and fallbackConfigKey ~= nil then
216 value = xmlFunc(xmlFile, fallbackConfigKey..subKey..param); -- Check for default configuration (xml index 0)
217 end;
218 if value == nil and fallbackOldKey ~= nil then
219 value = xmlFunc(xmlFile, fallbackOldKey..subKey..param); -- Fallback to old xml setup
220 end;
221 return Utils.getNoNil(value, defaultValue); -- using default value
222end

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
74function ConfigurationUtil.getMaterialByConfigId(object, configName, configId)
75 local configId = object.configurations[configName]
76 if configId ~= nil then
77 local item = g_storeManager:getItemByXMLFilename(object.configFileName)
78 local config = item.configurations[configName][configId]
79 if config ~= nil then
80 return config.material
81 end
82 end
83
84 return nil
85end

getStoreAddtionalConfigData

Description
Get store additional config data
Definition
getStoreAddtionalConfigData(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
341function ConfigurationUtil.getStoreAddtionalConfigData(xmlFile, baseXMLName, baseDir, customEnvironment, isMod, configItem)
342 configItem.vehicleType = getXMLString(xmlFile, baseXMLName.."#vehicleType")
343end

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
233function ConfigurationUtil.getXMLConfigurationKey(xmlFile, index, key, defaultKey, configurationKey)
234 local configIndex = Utils.getNoNil(index, 1);
235 local configKey = string.format(key.."(%d)", configIndex-1);
236 if index ~= nil and not hasXMLProperty(xmlFile, configKey) then
237 print("Warning: Invalid "..configurationKey.." index '"..tostring(index).."' in '"..key.."'. Using default "..configurationKey.." settings instead!");
238 end;
239
240 if not hasXMLProperty(xmlFile, configKey) then
241 configKey = key.."(0)";
242 end;
243 if not hasXMLProperty(xmlFile, configKey) then
244 configKey = defaultKey;
245 end;
246
247 return configKey, configIndex;
248end

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

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
143function ConfigurationUtil.replaceMaterialRec(object, node, oldMaterial, newMaterial)
144 if getHasClassId(node, ClassIds.SHAPE) then
145 local nodeMaterial = getMaterial(node, 0);
146 if nodeMaterial == oldMaterial then
147 setMaterial(node, newMaterial, 0)
148 end
149 end;
150
151 local numChildren = getNumOfChildren(node);
152 if numChildren > 0 then
153 for i=0, numChildren-1 do
154 ConfigurationUtil.replaceMaterialRec(object, getChildAt(node, i), oldMaterial, newMaterial)
155 end;
156 end;
157end

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
164function ConfigurationUtil.setColor(object, xmlFile, configName, configColorId)
165 local color = ConfigurationUtil.getColorByConfigId(object, configName, configColorId)
166 if color ~= nil then
167 local r,g,b,mat = unpack(color);
168 local i = 0;
169 while true do
170 local colorKey = string.format("vehicle.%sConfigurations.colorNode(%d)", configName, i)
171 if not hasXMLProperty(xmlFile, colorKey) then
172 break;
173 end
174
175 local node = I3DUtil.indexToObject(object.components, getXMLString(xmlFile, colorKey .. "#node"), object.i3dMappings);
176 if node ~= nil then
177 if getHasClassId(node, ClassIds.SHAPE) then
178 if mat == nil then
179 _,_,_,mat = getShaderParameter(node, "colorScale")
180 end
181 if Utils.getNoNil(getXMLBool(xmlFile, colorKey .. "#recursive"), false) then
182 I3DUtil.setShaderParameterRec(node, "colorScale", r, g, b, mat);
183 else
184 setShaderParameter(node, "colorScale", r, g, b, mat, false);
185 end
186 else
187 print("Warning: Could not set vehicle color to '"..getName(node).."' because node is not a shape!")
188 end
189 end
190 i = i + 1;
191 end
192 end
193end

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