-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfigEngine.uc
More file actions
214 lines (178 loc) · 7.06 KB
/
ConfigEngine.uc
File metadata and controls
214 lines (178 loc) · 7.06 KB
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
//---------------------------------------------------------------------------------------
// FILE: ConfigEngine.uc
// AUTHOR: Iridar / Enhanced Mod Project Template -- 26/02/2024
// PURPOSE: Helper class for setting up configurable values.
//---------------------------------------------------------------------------------------
class ConfigEngine extends Object config(Game) abstract;
//=======================================================================================
// OVERVIEW
//---------------------------------------------------------------------------------------
//
// Config Engine aims to resolve two issues:
//
// 1. Setting up configuration variables is a chore.
//
// This problem is resolved by removing the need to declare configuration variables.
// You simply specify the name of the property, and use that property in config files.
// Config Engine connects the two automatically.
//
// 2. Other mods have trouble reliably overriding shipped default config.
//
// This happens because config load order is unpredictable from the modmaker's end.
// In other words, while there are ways to enforce particular config load order,
// they must be implemented on the end of the mod user,
// and cannot be enforced by you, the modmaker.
//
// This problem is resolved by adding a Priority value to each configuration entry, which
// has the final word on which config entry gets used, irrespective of config load order
// (unless several entries have the same Priority, in which case the latest entry in
// config load order will be used).
//
//=======================================================================================
// SETTING CONFIG ENTRIES
//---------------------------------------------------------------------------------------
//
//Some examples of specifying config values. Default config file is XComGame.ini,
//but you can change it to whatever you want in the class definition above.
/*
[$ModSafeName$.ConfigEngine]
+Configs = (N = "SomeIntProperty", V = "10")
+Configs = (N = "SomeIntArrayProperty", VA = ("1", "2", "3", "4"))
; Same thing written differently.
+Configs = (N = "SomeIntArrayProperty", VA[0] = "1", VA[1] = "2", VA[2} = "3", VA[3] = "4")
; Specify higher priority to make sure your config entry overrides entries with lower priority (default 50).
; Useful for changing configuration of another mod that uses Config Engine.
+Configs = (N = "SomeIntProperty", V = "20", Priority = 55)
+Configs = (N = "SomeCostProperty",\\
Cost = (ResourceCosts[0] = (ItemTemplateName = "Supplies", Quantity = 48),\\
ResourceCosts[1] = (ItemTemplateName = "AlienAlloy", Quantity = 6),\\
ResourceCosts[2] = (ItemTemplateName = "EleriumDust", Quantity = 6)))
+Configs = (N = "SomeRequirementsProperty",\\
Requirements = (RequiredTechs[0] = "AutopsyArchon", RequiredEngineeringScore=20, bVisibleIfPersonnelGatesNotMet=true))
*/
//=======================================================================================
// GETTING CONFIG ENTRIES
//---------------------------------------------------------------------------------------
//
// You can call Config Engine functions directly, for example:
//
// IntValue = int(class'ConfigEngine'.static.GetConfig("SomeIntProperty").V);
//
// But that can get cumbersome, so helper functions and global macros are used
// to keep code compact.
/*
IntValue = `GetConfigInt("SomeIntProperty");
IntArray = `GetConfigArrayInt("SomeIntArrayProperty");
*/
// While the GetConfig functions and macros accept names,
// it's recommended to use strings to avoid polluting the name table.
//
//=======================================================================================
// CONFIG ENGINE IMPLEMENTATION
//---------------------------------------------------------------------------------------
struct ConfigStruct
{
// Config property name
var string N;
// Properties
var string V; // Value of the property
var array<string> VA; // Array of values (when appropriate)
var WeaponDamageValue Damage;
var StrategyCost Cost;
var StrategyRequirement Requirements;
var array<name> RDLC; // List of required modnames for this config entry to be considered
var int Priority; // Priority. If several config entries share the same name N, the entry with highest priority will be used.
// If priority matches, the latest entry in config load order will be used.
structdefaultproperties
{
Priority = 50
}
};
var private config array<ConfigStruct> Configs;
static final function ConfigStruct GetConfig(const coerce string ConfigName, optional bool bCanBeNull)
{
local ConfigStruct ReturnConfig;
local ConfigStruct CycleConfig;
local ConfigStruct EmptyConfig;
foreach default.Configs(CycleConfig)
{
if (!class'Help'.static.AreModsActive(CycleConfig.RDLC))
{
continue;
}
if (CycleConfig.N == ConfigName && CycleConfig.Priority >= ReturnConfig.Priority)
{
ReturnConfig = CycleConfig;
}
}
if (ReturnConfig == EmptyConfig && !bCanBeNull)
{
`redscreen("WARNING :: Failed to find Config with N name:" @ ConfigName);
`redscreen(GetScriptTrace());
}
return ReturnConfig;
}
static final function string GetConfigValue(const coerce string ConfigName)
{
return GetConfig(ConfigName).V;
}
static final function bool GetConfigBool(const coerce string ConfigName)
{
return bool(GetConfigValue(ConfigName));
}
static final function int GetConfigInt(const coerce string ConfigName)
{
return int(GetConfigValue(ConfigName));
}
static final function float GetConfigFloat(const coerce string ConfigName)
{
return float(GetConfigValue(ConfigName));
}
static final function name GetConfigName(const coerce string ConfigName)
{
return name(GetConfigValue(ConfigName));
}
static final function string GetConfigString(const coerce string ConfigName)
{
return GetConfigValue(ConfigName);
}
static final function array<int> GetConfigArrayInt(const coerce string ConfigName)
{
local array<string> StringArray;
local array<int> ReturnArray;
local int Index;
StringArray = GetConfig(ConfigName).VA;
for (Index = 0; Index < StringArray.Length; Index++)
{
ReturnArray.AddItem(int(StringArray[Index]));
}
return ReturnArray;
}
static final function array<float> GetConfigArrayFloat(const coerce string ConfigName)
{
local array<string> StringArray;
local array<float> ReturnArray;
local int Index;
StringArray = GetConfig(ConfigName).VA;
for (Index = 0; Index < StringArray.Length; Index++)
{
ReturnArray.AddItem(float(StringArray[Index]));
}
return ReturnArray;
}
static final function array<name> GetConfigArrayName(const coerce string ConfigName, optional bool bCanBeNull)
{
local array<string> StringArray;
local array<name> ReturnArray;
local int Index;
StringArray = GetConfig(ConfigName, bCanBeNull).VA;
for (Index = 0; Index < StringArray.Length; Index++)
{
ReturnArray.AddItem(name(StringArray[Index]));
}
return ReturnArray;
}
static final function WeaponDamageValue GetConfigDamage(const coerce string ConfigName)
{
return GetConfig(ConfigName).Damage;
}