Skip to content

Commit 48bff40

Browse files
author
Swamp Ig
committed
Fixed bug when combining index and operator
Switched to using regexp to parse nodes.
1 parent b51f309 commit 48bff40

File tree

2 files changed

+123
-49
lines changed

2 files changed

+123
-49
lines changed

Tests/ValueCopy.cfg

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
MMTEST
3+
{
4+
name = valueCopy
5+
MODULE
6+
{
7+
name = module1
8+
multiVal = one
9+
multiVal = two
10+
numeric = 0
11+
}
12+
}
13+
14+
// Adds value to module2
15+
@MMTEST[valueCopy]
16+
{
17+
// Copy new module 2
18+
@MODULE[module1]
19+
{
20+
// Unindexed
21+
+multiVal = three
22+
// regexp with index
23+
+multiVal,1 ^= :$:duplicate:
24+
// Arithmetic
25+
+numeric += 5
26+
}
27+
}
28+
29+
MMTEST_EXPECT
30+
{
31+
MMTEST
32+
{
33+
name = valueCopy
34+
MODULE
35+
{
36+
name = module1
37+
multiVal = one
38+
multiVal = two
39+
numeric = 0
40+
multiVal = three
41+
multiVal = twoduplicate
42+
numeric = 5
43+
}
44+
}
45+
}

moduleManager.cs

Lines changed: 78 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,11 @@ orderby ass.GetName().Version descending, a.path ascending
195195
errors += errorFiles[file] + " error" + (errorFiles[file] > 1 ? "s" : "") + " in GameData/" + file + "\n";
196196

197197

198-
status = "ModuleManager: "
199-
+ needsUnsatisfiedCount + " unsatisfied need" + (needsUnsatisfiedCount != 1 ? "s" : "")
200-
+ ", " + patchCount + " patch" + (patchCount != 1 ? "es" : "") + " applied";
198+
status = "ModuleManager: "
199+
+ patchCount + " patch" + (patchCount != 1 ? "es" : "") + " applied"
200+
+ ", "
201+
+ needsUnsatisfiedCount + " hidden item" + (needsUnsatisfiedCount != 1 ? "s" : "");
202+
201203
if(errorCount > 0)
202204
status += ", found " + errorCount + " error" + (errorCount != 1 ? "s" : "");
203205

@@ -478,6 +480,9 @@ public void ApplyPatch(List<String> excludePaths, string Stage)
478480
}
479481
}
480482

483+
// Name is group 1, index is group 2, operator is group 3
484+
private static Regex parseValue = new Regex(@"([\w\?\*]*)(?:,(-?[0-9]+))?(?:\s([+\-*/^]))?");
485+
481486
// ModifyNode applies the ConfigNode mod as a 'patch' to ConfigNode original, then returns the patched ConfigNode.
482487
// it uses FindConfigNodeIn(src, nodeType, nodeName, nodeTag) to recurse.
483488
public ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
@@ -492,50 +497,74 @@ public ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
492497
string valName;
493498
Command cmd = ParseCommand(modVal.name, out valName);
494499

495-
if (cmd == Command.Replace)
500+
Match match = parseValue.Match(valName);
501+
if (!match.Success)
496502
{
497-
if(valName.Contains(','))
498-
{
499-
print("[ModuleManager] Cannot use index with replace (%) value: " + mod.name);
500-
continue;
501-
}
502-
if (valName.Contains('*') || valName.Contains('?'))
503-
{
504-
print("[ModuleManager] Cannot use wildcards with replace (%) value: " + mod.name);
505-
continue;
506-
}
507-
// Replace is pretty simplistic.
508-
newNode.RemoveValues(valName);
509-
newNode.AddValue(valName, modVal.value);
503+
print("[ModuleManager] Cannot parse value modifying command: " + valName);
510504
continue;
511505
}
512506

513-
int index, insertIndex;
507+
// Get the bits and pieces from the regexp
508+
509+
valName = match.Groups[1].Value;
514510

515511
// In this case insert the value at position index (with the same node names)
516-
if (valName.Contains(",") && int.TryParse(valName.Split(',')[1], out index))
512+
int index = 0;
513+
if (match.Groups[2].Success)
517514
{
518-
insertIndex = index;
519-
valName = valName.Split(',')[0];
515+
// can have "node,n *" (for *= ect)
516+
if(!int.TryParse(match.Groups[2].Value, out index))
517+
{
518+
Debug.LogError("Unable to parse number as number. Very odd.");
519+
continue;
520+
}
520521
}
521-
else
522+
523+
char op = ' ';
524+
if (match.Groups[3].Success)
522525
{
523-
index = 0;
524-
insertIndex = int.MaxValue;
526+
op = match.Groups[3].Value[0];
525527
}
526528

527529
switch (cmd)
528530
{
529531
case Command.Insert:
530-
InsertValue(newNode, insertIndex, valName, modVal.value);
532+
if (match.Groups[3].Success || valName.Contains('*') || valName.Contains('?'))
533+
{
534+
if (match.Groups[3].Success)
535+
print("[ModuleManager] Cannot use operators with insert value: " + mod.name);
536+
if (valName.Contains('*') || valName.Contains('?'))
537+
print("[ModuleManager] Cannot use wildcards (* or ?) with insert value: " + mod.name);
538+
}
539+
else
540+
{
541+
// Insert at the end by default
542+
InsertValue(newNode, match.Groups[2].Success ? index : int.MaxValue, valName, modVal.value);
543+
}
544+
break;
545+
case Command.Replace:
546+
if (match.Groups[2].Success || match.Groups[3].Success || valName.Contains('*') || valName.Contains('?'))
547+
{
548+
if (match.Groups[2].Success)
549+
print("[ModuleManager] Cannot use index with replace (%) value: " + mod.name);
550+
if (match.Groups[3].Success)
551+
print("[ModuleManager] Cannot use operators with replace (%) value: " + mod.name);
552+
if (valName.Contains('*') || valName.Contains('?'))
553+
print("[ModuleManager] Cannot use wildcards (* or ?) with replace (%) value: " + mod.name);
554+
}
555+
else
556+
{
557+
newNode.RemoveValues(valName);
558+
newNode.AddValue(valName, modVal.value);
559+
}
531560
break;
532561
case Command.Edit:
533562
case Command.Copy:
534563
// Format is @key = value or @key *= value or @key += value or @key -= value
535564
// or @key,index = value or @key,index *= value or @key,index += value or @key,index -= value
536565

537566
ConfigNode.Value origVal;
538-
string value = FindAndReplaceValue(mod, ref valName, modVal.value, newNode, index, out origVal);
567+
string value = FindAndReplaceValue(mod, ref valName, modVal.value, newNode, op, index, out origVal);
539568

540569
if (value != null)
541570
{
@@ -550,16 +579,20 @@ public ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
550579

551580
break;
552581
case Command.Delete:
553-
// Default is to delete ALL values that match. (backwards compatibility)
554-
// If there is an index, use it.
555-
if (index != insertIndex)
582+
if (match.Groups[3].Success)
583+
print("[ModuleManager] Cannot use operators with delete (- or !) value: " + mod.name);
584+
else if (match.Groups[2].Success)
556585
{
586+
// If there is an index, use it.
557587
ConfigNode.Value v = FindValueIn(newNode, valName, index);
558588
if (v != null)
559589
newNode.values.Remove(modVal);
560590
}
561591
else
592+
{
593+
// Default is to delete ALL values that match. (backwards compatibility)
562594
newNode.RemoveValues(valName);
595+
}
563596
break;
564597
}
565598
}
@@ -721,21 +754,8 @@ public ConfigNode ModifyNode(ConfigNode original, ConfigNode mod)
721754
return newNode;
722755
}
723756

724-
private static string FindAndReplaceValue(ConfigNode mod, ref string valName, string value, ConfigNode newNode, int index, out ConfigNode.Value origVal)
757+
private static string FindAndReplaceValue(ConfigNode mod, ref string valName, string value, ConfigNode newNode, char op, int index, out ConfigNode.Value origVal)
725758
{
726-
char op = ' ';
727-
if (valName.EndsWith(" *")) // @key *= val
728-
op = '*';
729-
else if (valName.EndsWith(" +")) // @key += val
730-
op = '+';
731-
else if (valName.EndsWith(" -")) // @key -= val
732-
op = '-';
733-
else if (valName.EndsWith(" ^"))
734-
op = '^';
735-
736-
if(op != ' ')
737-
valName = valName.Substring(0, valName.IndexOf(' '));
738-
739759
origVal = FindValueIn(newNode, valName, index);
740760
if (origVal == null)
741761
return null;
@@ -759,12 +779,21 @@ private static string FindAndReplaceValue(ConfigNode mod, ref string valName, st
759779
}
760780
else if (double.TryParse(value, out s) && double.TryParse(ovalue, out os))
761781
{
762-
if (op == '*')
763-
value = (s * os).ToString();
764-
else if (op == '+')
765-
value = (s + os).ToString();
766-
else if (op == '-')
767-
value = (s - os).ToString();
782+
switch (op)
783+
{
784+
case '*':
785+
value = (s * os).ToString();
786+
break;
787+
case '/':
788+
value = (s / os).ToString();
789+
break;
790+
case '+':
791+
value = (s + os).ToString();
792+
break;
793+
case '-':
794+
value = (s - os).ToString();
795+
break;
796+
}
768797
}
769798
else
770799
{

0 commit comments

Comments
 (0)