Skip to content

Commit 75faee9

Browse files
committed
Feature: Automatically convert mod config value instead of falling to default
1 parent 05cac4b commit 75faee9

File tree

1 file changed

+77
-23
lines changed

1 file changed

+77
-23
lines changed

api/ModConfig.cs

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,12 @@ public object GetValue()
315315
{
316316
return Type switch
317317
{
318-
ConfigItemType.SWITCH => BoolVal,
319-
ConfigItemType.SLIDER => FloatVal,
318+
ConfigItemType.SWITCH => BoolVal,
319+
ConfigItemType.SLIDER => FloatVal,
320320
ConfigItemType.INT_SLIDER => IntVal,
321-
ConfigItemType.TEXT => TextVal,
322-
ConfigItemType.SELECT => IntVal,
323-
_ => throw new ArgumentOutOfRangeException()
321+
ConfigItemType.TEXT => TextVal,
322+
ConfigItemType.SELECT => IntVal,
323+
_ => throw new ArgumentOutOfRangeException()
324324
};
325325
}
326326
}
@@ -338,8 +338,8 @@ public object GetValue()
338338
/// </remarks>
339339
public class ModConfig
340340
{
341-
private readonly string _path;
342-
internal Dictionary<string, Dictionary<string, ModConfigItem>> _config = new();
341+
private readonly string _path;
342+
internal Dictionary<string, Dictionary<string, ModConfigItem>> _config = new();
343343

344344
/// <summary>
345345
/// Create a new <see cref="ModConfig" /> instance from <paramref name="path" />
@@ -431,13 +431,67 @@ public void MergeWith(ModConfig pDefaultConfig)
431431
group[item_id].CallBack = default_group[item_id].CallBack;
432432
if (group[item_id].Type != default_group[item_id].Type)
433433
{
434-
AddConfigItem(group_id, item_id, default_group[item_id].Type, default_group[item_id].GetValue(),
434+
// Convert old config item to new type(value conversion)
435+
var new_value = default_group[item_id].GetValue();
436+
switch (default_group[item_id].Type)
437+
{
438+
case ConfigItemType.SLIDER:
439+
switch (group[item_id].Type)
440+
{
441+
case ConfigItemType.TEXT:
442+
if (float.TryParse(new_value.ToString(), out var float_value))
443+
new_value = float_value;
444+
break;
445+
case ConfigItemType.SWITCH:
446+
new_value = (bool)group[item_id].GetValue() ? 1 : 0;
447+
break;
448+
case ConfigItemType.INT_SLIDER:
449+
new_value = (int)group[item_id].GetValue();
450+
break;
451+
}
452+
453+
group[item_id].SetFloatRange(default_group[item_id].MinFloatVal,
454+
default_group[item_id].MaxFloatVal);
455+
break;
456+
case ConfigItemType.INT_SLIDER:
457+
switch (group[item_id].Type)
458+
{
459+
case ConfigItemType.TEXT:
460+
if (int.TryParse(new_value.ToString(), out var int_value)) new_value = int_value;
461+
break;
462+
case ConfigItemType.SWITCH:
463+
new_value = (bool)group[item_id].GetValue() ? 1 : 0;
464+
break;
465+
case ConfigItemType.SLIDER:
466+
new_value = (float)group[item_id].GetValue();
467+
break;
468+
}
469+
470+
group[item_id].SetIntRange(default_group[item_id].MinIntVal,
471+
default_group[item_id].MaxIntVal);
472+
break;
473+
case ConfigItemType.SWITCH:
474+
switch (group[item_id].Type)
475+
{
476+
case ConfigItemType.TEXT:
477+
if (bool.TryParse(new_value.ToString(), out var bool_value))
478+
new_value = bool_value;
479+
if (int.TryParse(new_value.ToString(), out var int_value))
480+
new_value = int_value != 0;
481+
break;
482+
case ConfigItemType.SLIDER:
483+
new_value = (float)group[item_id].GetValue() != 0;
484+
break;
485+
case ConfigItemType.INT_SLIDER:
486+
new_value = (int)group[item_id].GetValue() != 0;
487+
break;
488+
}
489+
490+
break;
491+
}
492+
493+
AddConfigItem(group_id, item_id, default_group[item_id].Type, new_value,
435494
default_group[item_id].IconPath, default_group[item_id].CallBack);
436-
if (default_group[item_id].Type == ConfigItemType.SLIDER)
437-
group[item_id].SetFloatRange(default_group[item_id].MinFloatVal,
438-
default_group[item_id].MaxFloatVal);
439-
else if (default_group[item_id].Type == ConfigItemType.INT_SLIDER)
440-
group[item_id].SetIntRange(default_group[item_id].MinIntVal, default_group[item_id].MaxIntVal);
441495
}
442496
else if (group[item_id].Type == ConfigItemType.SLIDER)
443497
{
@@ -462,14 +516,14 @@ public void MergeWith(ModConfig pDefaultConfig)
462516
foreach (var item in default_group.Keys.Where(item => !group.ContainsKey(item)))
463517
if (default_group[item].Type == ConfigItemType.SLIDER)
464518
AddConfigSliderItemWithRange(group_id, item, (float)default_group[item].GetValue(),
465-
default_group[item].MinFloatVal, default_group[item].MaxFloatVal,
466-
default_group[item].IconPath, default_group[item].CallBack);
519+
default_group[item].MinFloatVal, default_group[item].MaxFloatVal,
520+
default_group[item].IconPath, default_group[item].CallBack);
467521
else if (default_group[item].Type == ConfigItemType.INT_SLIDER)
468522
AddConfigSliderItemWithIntRange(group_id, item, (int)default_group[item].GetValue(),
469-
default_group[item].MinIntVal, default_group[item].MaxIntVal,
470-
default_group[item].IconPath, default_group[item].CallBack);
523+
default_group[item].MinIntVal, default_group[item].MaxIntVal,
524+
default_group[item].IconPath, default_group[item].CallBack);
471525
else
472-
AddConfigItem(group_id, item, default_group[item].Type, default_group[item].GetValue(),
526+
AddConfigItem(group_id, item, default_group[item].Type, default_group[item].GetValue(),
473527
default_group[item].IconPath, default_group[item].CallBack);
474528
}
475529
}
@@ -520,8 +574,8 @@ public void CreateGroup(string pId)
520574
/// <param name="pIconPath"></param>
521575
/// <param name="pCallback"></param>
522576
/// <returns></returns>
523-
public ModConfigItem AddConfigItem(string pGroupId, string pId, ConfigItemType pType, object pDefaultValue,
524-
string pIconPath = "", string pCallback = "")
577+
public ModConfigItem AddConfigItem(string pGroupId, string pId, ConfigItemType pType, object pDefaultValue,
578+
string pIconPath = "", string pCallback = "")
525579
{
526580
if (!_config.TryGetValue(pGroupId, out var group))
527581
{
@@ -561,7 +615,7 @@ public ModConfigItem AddConfigItem(string pGroupId, string pId, ConfigItem
561615
/// <param name="pCallback"></param>
562616
/// <returns></returns>
563617
public ModConfigItem AddConfigSliderItemWithRange(string pGroupId, string pId, float pDefaultValue, float pMinValue,
564-
float pMaxValue, string pIconPath = "", string pCallback = "")
618+
float pMaxValue, string pIconPath = "", string pCallback = "")
565619
{
566620
if (!_config.TryGetValue(pGroupId, out var group))
567621
{
@@ -601,8 +655,8 @@ public ModConfigItem AddConfigSliderItemWithRange(string pGroupId, string pId, f
601655
/// <param name="pIconPath"></param>
602656
/// <param name="pCallback"></param>
603657
/// <returns></returns>
604-
public ModConfigItem AddConfigSliderItemWithIntRange(string pGroupId, string pId, int pDefaultValue, int pMinValue,
605-
int pMaxValue, string pIconPath = "", string pCallback = "")
658+
public ModConfigItem AddConfigSliderItemWithIntRange(string pGroupId, string pId, int pDefaultValue, int pMinValue,
659+
int pMaxValue, string pIconPath = "", string pCallback = "")
606660
{
607661
if (!_config.TryGetValue(pGroupId, out var group))
608662
{

0 commit comments

Comments
 (0)