Skip to content

Commit a9a990d

Browse files
committed
Pull Command and ParseCommand out of MMPatchLoader
Would be nice if enums allowed static methods
1 parent 80cefc3 commit a9a990d

6 files changed

Lines changed: 184 additions & 82 deletions

File tree

ModuleManager/Command.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace ModuleManager
7+
{
8+
public enum Command
9+
{
10+
Insert,
11+
12+
Delete,
13+
14+
Edit,
15+
16+
Replace,
17+
18+
Copy,
19+
20+
Rename,
21+
22+
Paste,
23+
24+
Special,
25+
26+
Create
27+
}
28+
}

ModuleManager/CommandParser.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace ModuleManager
7+
{
8+
public static class CommandParser
9+
{
10+
public static Command Parse(string name, out string valueName)
11+
{
12+
if (name.Length == 0)
13+
{
14+
valueName = string.Empty;
15+
return Command.Insert;
16+
}
17+
Command ret;
18+
switch (name[0])
19+
{
20+
case '@':
21+
ret = Command.Edit;
22+
break;
23+
24+
case '%':
25+
ret = Command.Replace;
26+
break;
27+
28+
case '-':
29+
case '!':
30+
ret = Command.Delete;
31+
break;
32+
33+
case '+':
34+
case '$':
35+
ret = Command.Copy;
36+
break;
37+
38+
case '|':
39+
ret = Command.Rename;
40+
break;
41+
42+
case '#':
43+
ret = Command.Paste;
44+
break;
45+
46+
case '*':
47+
ret = Command.Special;
48+
break;
49+
50+
case '&':
51+
ret = Command.Create;
52+
break;
53+
54+
default:
55+
valueName = name;
56+
return Command.Insert;
57+
}
58+
valueName = name.Substring(1);
59+
return ret;
60+
}
61+
}
62+
}

ModuleManager/MMPatchLoader.cs

Lines changed: 5 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private void PrePatchInit()
184184
modlist += "Non-DLL mods added (:FOR[xxx]):\n";
185185
foreach (UrlDir.UrlConfig cfgmod in GameDatabase.Instance.root.AllConfigs)
186186
{
187-
if (ParseCommand(cfgmod.type, out string name) != Command.Insert)
187+
if (CommandParser.Parse(cfgmod.type, out string name) != Command.Insert)
188188
{
189189
progress.PatchAdded();
190190
if (name.Contains(":FOR["))
@@ -940,7 +940,7 @@ private void PurgeUnused()
940940
{
941941
string name = RemoveWS(mod.type);
942942

943-
if (ParseCommand(name, out name) != Command.Insert)
943+
if (CommandParser.Parse(name, out name) != Command.Insert)
944944
mod.parent.configs.Remove(mod);
945945
}
946946
}
@@ -968,7 +968,7 @@ public IEnumerator ApplyPatch(string Stage)
968968
try
969969
{
970970
string name = RemoveWS(mod.type);
971-
Command cmd = ParseCommand(name, out string tmp);
971+
Command cmd = CommandParser.Parse(name, out string tmp);
972972

973973
if (cmd != Command.Insert)
974974
{
@@ -1124,7 +1124,7 @@ public static ConfigNode ModifyNode(NodeStack original, ConfigNode mod, PatchCon
11241124
vals += "\n " + modVal.name + "= " + modVal.value;
11251125
#endif
11261126

1127-
Command cmd = ParseCommand(modVal.name, out string valName);
1127+
Command cmd = CommandParser.Parse(modVal.name, out string valName);
11281128

11291129
if (cmd == Command.Special)
11301130
{
@@ -1424,7 +1424,7 @@ public static ConfigNode ModifyNode(NodeStack original, ConfigNode mod, PatchCon
14241424
}
14251425

14261426
string subName = subMod.name;
1427-
Command command = ParseCommand(subName, out string tmp);
1427+
Command command = CommandParser.Parse(subName, out string tmp);
14281428

14291429
if (command == Command.Insert)
14301430
{
@@ -2068,83 +2068,6 @@ private static string FindAndReplaceValue(
20682068

20692069
#endregion Applying Patches
20702070

2071-
#region Command Parsing
2072-
2073-
private enum Command
2074-
{
2075-
Insert,
2076-
2077-
Delete,
2078-
2079-
Edit,
2080-
2081-
Replace,
2082-
2083-
Copy,
2084-
2085-
Rename,
2086-
2087-
Paste,
2088-
2089-
Special,
2090-
2091-
Create
2092-
}
2093-
2094-
private static Command ParseCommand(string name, out string valueName)
2095-
{
2096-
if (name.Length == 0)
2097-
{
2098-
valueName = string.Empty;
2099-
return Command.Insert;
2100-
}
2101-
Command ret;
2102-
switch (name[0])
2103-
{
2104-
case '@':
2105-
ret = Command.Edit;
2106-
break;
2107-
2108-
case '%':
2109-
ret = Command.Replace;
2110-
break;
2111-
2112-
case '-':
2113-
case '!':
2114-
ret = Command.Delete;
2115-
break;
2116-
2117-
case '+':
2118-
case '$':
2119-
ret = Command.Copy;
2120-
break;
2121-
2122-
case '|':
2123-
ret = Command.Rename;
2124-
break;
2125-
2126-
case '#':
2127-
ret = Command.Paste;
2128-
break;
2129-
2130-
case '*':
2131-
ret = Command.Special;
2132-
break;
2133-
2134-
case '&':
2135-
ret = Command.Create;
2136-
break;
2137-
2138-
default:
2139-
valueName = name;
2140-
return Command.Insert;
2141-
}
2142-
valueName = name.Substring(1);
2143-
return ret;
2144-
}
2145-
2146-
#endregion Command Parsing
2147-
21482071
#region Sanity checking & Utility functions
21492072

21502073
public static bool IsBracketBalanced(string str)

ModuleManager/ModuleManager.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
<Compile Include="Cats\CatMover.cs" />
3737
<Compile Include="Cats\CatOrbiter.cs" />
3838
<Compile Include="Collections\ImmutableStack.cs" />
39+
<Compile Include="Command.cs" />
40+
<Compile Include="CommandParser.cs" />
3941
<Compile Include="Extensions\NodeStackExtensions.cs" />
4042
<Compile Include="IPatchProgress.cs" />
4143
<Compile Include="Logging\IBasicLogger.cs" />
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Xunit;
6+
using ModuleManager;
7+
8+
namespace ModuleManagerTests
9+
{
10+
public class CommandParserTest
11+
{
12+
[Fact]
13+
public void TestParse__Insert()
14+
{
15+
Assert.Equal(Command.Insert, CommandParser.Parse("PART", out string newName));
16+
Assert.Equal("PART", newName);
17+
}
18+
19+
[Fact]
20+
public void TestParse__Delete()
21+
{
22+
Assert.Equal(Command.Delete, CommandParser.Parse("!PART", out string newName1));
23+
Assert.Equal("PART", newName1);
24+
Assert.Equal(Command.Delete, CommandParser.Parse("-PART", out string newName2));
25+
Assert.Equal("PART", newName2);
26+
}
27+
28+
[Fact]
29+
public void TestParse__Edit()
30+
{
31+
Assert.Equal(Command.Edit, CommandParser.Parse("@PART", out string newName));
32+
Assert.Equal("PART", newName);
33+
}
34+
35+
[Fact]
36+
public void TestParse__Replace()
37+
{
38+
Assert.Equal(Command.Replace, CommandParser.Parse("%PART", out string newName));
39+
Assert.Equal("PART", newName);
40+
}
41+
42+
[Fact]
43+
public void TestParse__Copy()
44+
{
45+
Assert.Equal(Command.Copy, CommandParser.Parse("+PART", out string newName1));
46+
Assert.Equal("PART", newName1);
47+
Assert.Equal(Command.Copy, CommandParser.Parse("$PART", out string newName2));
48+
Assert.Equal("PART", newName2);
49+
}
50+
51+
[Fact]
52+
public void TestParse__Rename()
53+
{
54+
Assert.Equal(Command.Rename, CommandParser.Parse("|PART", out string newName));
55+
Assert.Equal("PART", newName); ;
56+
}
57+
58+
[Fact]
59+
public void TestParse__Paste()
60+
{
61+
Assert.Equal(Command.Paste, CommandParser.Parse("#PART", out string newName));
62+
Assert.Equal("PART", newName);
63+
}
64+
65+
[Fact]
66+
public void TestParse__Special()
67+
{
68+
Assert.Equal(Command.Special, CommandParser.Parse("*PART", out string newName));
69+
Assert.Equal("PART", newName);
70+
}
71+
72+
[Fact]
73+
public void TestParse__Special__Chained()
74+
{
75+
Assert.Equal(Command.Special, CommandParser.Parse("*@PART", out string newName));
76+
Assert.Equal("@PART", newName);
77+
}
78+
79+
[Fact]
80+
public void TestParse__Create()
81+
{
82+
Assert.Equal(Command.Create, CommandParser.Parse("&PART", out string newName));
83+
Assert.Equal("PART", newName);
84+
}
85+
}
86+
}

ModuleManagerTests/ModuleManagerTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
</ItemGroup>
5151
<ItemGroup>
5252
<Compile Include="Collections\ImmutableStackTest.cs" />
53+
<Compile Include="CommandParserTest.cs" />
5354
<Compile Include="DummyTest.cs" />
5455
<Compile Include="Extensions\NodeStackExtensionsTest.cs" />
5556
<Compile Include="Logging\ModLoggerTest.cs" />

0 commit comments

Comments
 (0)