DayZ 1.24
Loading...
Searching...
No Matches
PluginConfigHandler.c
Go to the documentation of this file.
2{
4
6 {
7 m_CfgParams = new array<ref CfgParam>;
8
9 LoadConfigFile();
10 }
11
12 override void OnInit()
13 {
14 super.OnInit();
15 }
16
17 override string GetFileName()
18 {
20 }
21
23 {
24 LoadFile();
25
26 m_CfgParams.Clear();
27
28 for (int j = 0; j < m_FileContent.Count(); j++)
29 m_CfgParams.Insert(ParseText(m_FileContent.Get(j)));
30 }
31
33 {
34 ClearFileNoSave();
35
36 for (int i = 0; i < m_CfgParams.Count(); ++i)
37 {
38 string s = ParseParam(m_CfgParams.Get(i));
39
40 m_FileContent.Insert(s);
41 }
42
43 SaveFile();
44 }
45
47 {
49
50 for (int i = 0; i < m_CfgParams.Count(); ++i)
51 {
52 param = m_CfgParams.Get(i);
53
54 if (param.GetName() == name)
55 return param;
56 }
57
58 // cfg_type == -1 returns NULL
60 m_CfgParams.Insert(param);
61 return param;
62 }
63
65 {
66 return m_CfgParams;
67 }
68
70 {
71 m_CfgParams.Clear();
72 }
73
75 {
76 for (int i = 0; i < m_CfgParams.Count(); ++i)
77 {
78 CfgParam param = m_CfgParams.Get(i);
79
80 if (param.GetName() == name)
81 {
82 m_CfgParams.Remove(i);
83 return;
84 }
85 }
86 }
87
88 void RenameParam(string name, string new_name)
89 {
90 for (int i = 0; i < m_CfgParams.Count(); ++i)
91 {
92 CfgParam param = m_CfgParams.Get(i);
93
94 if (param.GetName() == name)
95 {
96 param.SetName(new_name);
97 return;
98 }
99 }
100 }
101
102 bool ParamExist(string name)
103 {
105
106 for (int i = 0; i < m_CfgParams.Count(); ++i)
107 {
108 param = m_CfgParams.Get(i);
109
110 if (param.GetName() == name)
111 return true;
112 }
113
114 return false;
115 }
116
118 {
119 string text = STRING_EMPTY;
120
121 if (param.GetName() != STRING_EMPTY)
122 text += param.GetName() + "=";
123
124 switch (param.GetType())
125 {
126 case CFG_TYPE_ARRAY:
127 {
128 CfgParamArray param_array = CfgParamArray.Cast(param);
130
131 text += "{";
132
133 for (int i = 0; i < params.Count(); ++i)
134 {
135 if (i > 0)
136 text += ",";
137
138 text += ParseParam(params.Get(i));
139 }
140
141 text += "}";
142
143 break;
144 }
145 case CFG_TYPE_STRING:
146 {
147 CfgParamString param_string = CfgParamString.Cast(param);
148 text += "\"" + param_string.GetValue() + "\"";
149
150 break;
151 }
152 case CFG_TYPE_INT:
153 {
154 CfgParamInt param_int = CfgParamInt.Cast(param);
155 text += param_int.GetValue().ToString();
156
157 break;
158 }
159 case CFG_TYPE_FLOAT:
160 {
161 CfgParamFloat param_float = CfgParamFloat.Cast(param);
162 string s = param_float.GetValue().ToString();
163
164 if (s.IndexOf(".") == -1)
165 s += ".0";
166
167 text += s;
168
169 break;
170 }
171 case CFG_TYPE_BOOL:
172 {
173 CfgParamBool param_bool = CfgParamBool.Cast(param);
174
175 if (param_bool.GetValue())
176 text += "true";
177 else
178 text += "false";
179
180 break;
181 }
182 }
183
184 return text;
185 }
186
188 {
189 string param_name = STRING_EMPTY;
190 string param_value = text.Trim();;
191 int text_type = GetTextType(param_value);
192
193 //Log("ParseText -> text_type: "+itoa(text_type));
194
196 {
197 int pos = param_value.IndexOf("=");
198
199 if (pos > 0)
200 {
201 param_name = param_value.Substring(0, pos).Trim();
202 int pos_end = param_value.Length() - pos;
203 param_value = param_value.Substring(pos + 1, pos_end - 1).Trim();
204
205 //Log("ParseText -> param -> "+param_name+" = "+param_value);
206
207 CfgParam param = ParseText(param_value);
208 param.SetName(param_name);
209
210 return param;
211 }
212 }
213 else if (text_type == CFG_TYPE_ARRAY)
214 {
215 //Log("ParseText -> array -> "+param_name+" = "+param_value);
216
217 CfgParamArray param_array = new CfgParamArray(param_name);
218 array<ref CfgParam> array_params = ParseToValueArray(param_value);
219 param_array.SetParams(array_params);
220
221 return param_array;
222 }
223 else if (text_type == CFG_TYPE_STRING)
224 {
225 //Log("ParseText -> string -> "+param_value);
226
227 CfgParamString param_string = new CfgParamString(param_name);
228 param_string.SetValue(ParseToValueString(param_value));
229
230 return param_string;
231 }
232 else if (text_type == CFG_TYPE_INT)
233 {
234 //Log("ParseText -> int -> "+param_name+" = "+param_value);
235
236 CfgParamInt param_int = new CfgParamInt(param_name);
237 param_int.SetValue(ParseToValueInt(param_value));
238
239 return param_int;
240 }
241 else if (text_type == CFG_TYPE_FLOAT)
242 {
243 //Log("ParseText -> float -> "+param_name+" = "+param_value);
244
245 CfgParamFloat param_float = new CfgParamFloat(param_name);
246 param_float.SetValue(ParseToValueFloat(param_value));
247
248 return param_float;
249 }
250 else if (text_type == CFG_TYPE_BOOL)
251 {
252 //Log("ParseText -> bool -> "+param_name+" = "+param_value);
253
254 CfgParamBool param_bool = new CfgParamBool(param_name);
255 param_bool.SetValue(ParseToValueBool(param_value));
256
257 return param_bool;
258 }
259
260 //Log("ParseText -> none -> "+param_name+" = "+param_value);
261
262 return NULL;
263 }
264
265 protected int GetTextType(string text)
266 {
267 int lenght = text.Length();
268
269 // Check Array
270 int pos1 = text.IndexOf("{");
271 if (pos1 == 0 && text.Substring(lenght - 1, 1) == "}")
272 return CFG_TYPE_ARRAY;
273
274 // Check String
275 pos1 = text.IndexOf("\"");
276
277 if (pos1 == 0 && text.Substring(lenght - 1, 1) == "\"")
278 {
279 string string_text = text.Substring(1, lenght - 2);
280 if (string_text.IndexOf("\"") == -1)
281 return CFG_TYPE_STRING;
282 }
283
284 // Check Param
285 pos1 = text.IndexOf("=");
286 if (pos1 > -1)
287 return CFG_TYPE_PARAM;
288
289 // Check Param True
290 pos1 = text.IndexOf("true");
291 if (lenght == 4 && pos1 > -1)
292 return CFG_TYPE_BOOL;
293
294 // Check Param False
295 pos1 = text.IndexOf("false");
296 if (lenght == 5 && pos1 > -1)
297 return CFG_TYPE_BOOL;
298
299 // Check Param Float
300 pos1 = text.IndexOf(".");
301 if (pos1 > -1)
302 return CFG_TYPE_FLOAT;
303
304 // Is Int
305 return CFG_TYPE_INT;
306 }
307
308 protected bool IsString(string text)
309 {
310 if (GetTextType(text) == CFG_TYPE_STRING)
311 return true;
312
313 return false;
314 }
315
316 protected bool IsParam(string text)
317 {
318 if (GetTextType(text) == CFG_TYPE_PARAM)
319 return true;
320
321 return false;
322 }
323
324 protected bool IsArray(string text)
325 {
326 if (GetTextType(text) == CFG_TYPE_ARRAY)
327 return true;
328
329 return false;
330 }
331
332 protected bool IsFloat(string text)
333 {
334 if (GetTextType(text) == CFG_TYPE_FLOAT)
335 return true;
336
337 return false;
338 }
339
340 protected bool IsInt(string text)
341 {
342 if (GetTextType(text) == CFG_TYPE_INT)
343 return true;
344
345 return false;
346 }
347
348 protected bool IsBool(string text)
349 {
350 if (GetTextType(text) == CFG_TYPE_BOOL)
351 return true;
352
353 return false;
354 }
355
356 protected string ParseToValueString(string param_value)
357 {
358 return param_value.Substring(1, param_value.Length() - 2);
359 }
360
361 protected float ParseToValueFloat(string param_value)
362 {
363 return param_value.ToFloat();
364 }
365
366 protected int ParseToValueInt(string param_value)
367 {
368 return param_value.ToInt();
369 }
370
371 protected bool ParseToValueBool(string param_value)
372 {
373 if (param_value.Contains("true"))
374 return true;
375
376 return false;
377 }
378
380 {
382
384
385 array<int> a = new array<int>;
386 array<int> b = new array<int>;
387
388 int pos_a = 0;
389 int pos_b = 0;
390 int element = 0;
391 int value_start = 0;
392 int value_end = 0;
393
394 while (pos_a > -1 || pos_b > -1)
395 {
396 pos_a = param_value.IndexOfFrom(pos_a, "{");
397 if (pos_a > -1)
398 {
399 a.Insert(pos_a);
400 pos_a++;
401 }
402
403 pos_b = param_value.IndexOfFrom(pos_b, "}");
404 if (pos_b > -1)
405 {
406 b.Insert(pos_b);
407 pos_b++;
408 }
409 }
410
411 pos_a = 0;
412 pos_b = 0;
413
414 for (pos_a = 0; pos_a < a.Count(); ++pos_a)
415 {
416 if (a.Get(pos_a) > b.Get(pos_b))
417 {
418 element--;
419
420 if (element <= 1)
421 {
422 value_end = b.Get(pos_b) - value_start + 1;
423
424 //Log("1 -> "+substr(param_value, value_start, value_end))
425
426 strings.Insert(param_value.Substring(value_start, value_end));
427
428 value_start = b.Get(pos_b) + 1;
429 }
430
431 pos_a--;
432 pos_b++;
433 }
434 else if (a.Get(pos_a) < b.Get(pos_b))
435 {
436 if (pos_a == a.Count() - 1 && pos_b < b.Count() - 1)
437 {
438 element--;
439
440 if (element == 0)
441 {
442 value_end = b.Get(pos_b) - value_start;
443
444 //Log("2 -> "+substr(param_value, value_start + 1, value_end))
445
446 strings.Insert(param_value.Substring(value_start + 1, value_end));
447
448 value_start = b.Get(pos_b) + 1;
449 }
450
451 pos_a--;
452 pos_b++;
453 }
454 else
455 {
456 element++;
457
458 if (element == 2)
459 value_start = a.Get(pos_a);
460 }
461 }
462 }
463
464 //Log(param_value+" => "+itoa(a.Count())+" / "+itoa(b.Count())+" count: "+itoa(strings.Count()));
465
466 if (strings.Count() == 0)
467 {
468 //Log(param_value+" -> ZERO {}");
469
470 string param_value_content = param_value.Substring(1, param_value.Length() - 2);
471
472 //Log(param_value+" -> Trimmed -> "+param_value_content);
473
474 param_value_content.Split(",", strings);
475 }
476
477 for (int i = 0; i < strings.Count(); ++i)
478 {
479 string str = strings.Get(i);
480
481 //Log("To Parse => "+str);
482
483 CfgParam param = ParseText(str);
484
485 param_parent.Insert(param);
486 }
487
488 return param_parent;
489 }
490}
const int CFG_TYPE_BOOL
Definition CfgParam.c:4
const int CFG_TYPE_ARRAY
Definition CfgParam.c:5
const int CFG_TYPE_PARAM
Definition CfgParam.c:6
const int CFG_TYPE_INT
Definition CfgParam.c:2
const int CFG_TYPE_STRING
Definition CfgParam.c:1
const int CFG_TYPE_FLOAT
Definition CfgParam.c:3
PlayerSpawnPresetDiscreteItemSetSlotData name
one set for cargo
static CfgParam CreateParam(string name, int cfg_type)
Definition CfgParam.c:38
string ParseParam(CfgParam param)
int GetTextType(string text)
override string GetFileName()
ref array< ref CfgParam > m_CfgParams
override void OnInit()
bool IsParam(string text)
CfgParam ParseText(string text)
array< ref CfgParam > GetAllParams()
void RenameParam(string name, string new_name)
float ParseToValueFloat(string param_value)
string ParseToValueString(string param_value)
int ParseToValueInt(string param_value)
void RemoveParamByName(string name)
bool IsBool(string text)
bool IsInt(string text)
bool ParamExist(string name)
bool IsString(string text)
bool IsFloat(string text)
CfgParam GetParamByName(string name, int cfg_type)
bool IsArray(string text)
array< ref CfgParam > ParseToValueArray(string param_value)
bool ParseToValueBool(string param_value)
const string CFG_FILE_USER_PROFILE
Definition constants.c:230
array< string > TStringArray
Definition EnScript.c:666
const string STRING_EMPTY
Definition constants.c:54