Skip to content

Commit

Permalink
Pull Command and ParseCommand out of MMPatchLoader
Browse files Browse the repository at this point in the history
Would be nice if enums allowed static methods
  • Loading branch information
blowfishpro committed Sep 14, 2017
1 parent 80cefc3 commit a9a990d
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 82 deletions.
28 changes: 28 additions & 0 deletions ModuleManager/Command.cs
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ModuleManager
{
public enum Command
{
Insert,

Delete,

Edit,

Replace,

Copy,

Rename,

Paste,

Special,

Create
}
}
62 changes: 62 additions & 0 deletions ModuleManager/CommandParser.cs
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ModuleManager
{
public static class CommandParser
{
public static Command Parse(string name, out string valueName)
{
if (name.Length == 0)
{
valueName = string.Empty;
return Command.Insert;
}
Command ret;
switch (name[0])
{
case '@':
ret = Command.Edit;
break;

case '%':
ret = Command.Replace;
break;

case '-':
case '!':
ret = Command.Delete;
break;

case '+':
case '$':
ret = Command.Copy;
break;

case '|':
ret = Command.Rename;
break;

case '#':
ret = Command.Paste;
break;

case '*':
ret = Command.Special;
break;

case '&':
ret = Command.Create;
break;

default:
valueName = name;
return Command.Insert;
}
valueName = name.Substring(1);
return ret;
}
}
}
87 changes: 5 additions & 82 deletions ModuleManager/MMPatchLoader.cs
Expand Up @@ -184,7 +184,7 @@ private void PrePatchInit()
modlist += "Non-DLL mods added (:FOR[xxx]):\n";
foreach (UrlDir.UrlConfig cfgmod in GameDatabase.Instance.root.AllConfigs)
{
if (ParseCommand(cfgmod.type, out string name) != Command.Insert)
if (CommandParser.Parse(cfgmod.type, out string name) != Command.Insert)
{
progress.PatchAdded();
if (name.Contains(":FOR["))
Expand Down Expand Up @@ -940,7 +940,7 @@ private void PurgeUnused()
{
string name = RemoveWS(mod.type);

if (ParseCommand(name, out name) != Command.Insert)
if (CommandParser.Parse(name, out name) != Command.Insert)
mod.parent.configs.Remove(mod);
}
}
Expand Down Expand Up @@ -968,7 +968,7 @@ public IEnumerator ApplyPatch(string Stage)
try
{
string name = RemoveWS(mod.type);
Command cmd = ParseCommand(name, out string tmp);
Command cmd = CommandParser.Parse(name, out string tmp);

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

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

if (cmd == Command.Special)
{
Expand Down Expand Up @@ -1424,7 +1424,7 @@ public static ConfigNode ModifyNode(NodeStack original, ConfigNode mod, PatchCon
}

string subName = subMod.name;
Command command = ParseCommand(subName, out string tmp);
Command command = CommandParser.Parse(subName, out string tmp);

if (command == Command.Insert)
{
Expand Down Expand Up @@ -2068,83 +2068,6 @@ private static string ProcessVariableSearch(string value, NodeStack nodeStack, P

#endregion Applying Patches

#region Command Parsing

private enum Command
{
Insert,

Delete,

Edit,

Replace,

Copy,

Rename,

Paste,

Special,

Create
}

private static Command ParseCommand(string name, out string valueName)
{
if (name.Length == 0)
{
valueName = string.Empty;
return Command.Insert;
}
Command ret;
switch (name[0])
{
case '@':
ret = Command.Edit;
break;

case '%':
ret = Command.Replace;
break;

case '-':
case '!':
ret = Command.Delete;
break;

case '+':
case '$':
ret = Command.Copy;
break;

case '|':
ret = Command.Rename;
break;

case '#':
ret = Command.Paste;
break;

case '*':
ret = Command.Special;
break;

case '&':
ret = Command.Create;
break;

default:
valueName = name;
return Command.Insert;
}
valueName = name.Substring(1);
return ret;
}

#endregion Command Parsing

#region Sanity checking & Utility functions

public static bool IsBracketBalanced(string str)
Expand Down
2 changes: 2 additions & 0 deletions ModuleManager/ModuleManager.csproj
Expand Up @@ -36,6 +36,8 @@
<Compile Include="Cats\CatMover.cs" />
<Compile Include="Cats\CatOrbiter.cs" />
<Compile Include="Collections\ImmutableStack.cs" />
<Compile Include="Command.cs" />
<Compile Include="CommandParser.cs" />
<Compile Include="Extensions\NodeStackExtensions.cs" />
<Compile Include="IPatchProgress.cs" />
<Compile Include="Logging\IBasicLogger.cs" />
Expand Down
86 changes: 86 additions & 0 deletions ModuleManagerTests/CommandParserTest.cs
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using ModuleManager;

namespace ModuleManagerTests
{
public class CommandParserTest
{
[Fact]
public void TestParse__Insert()
{
Assert.Equal(Command.Insert, CommandParser.Parse("PART", out string newName));
Assert.Equal("PART", newName);
}

[Fact]
public void TestParse__Delete()
{
Assert.Equal(Command.Delete, CommandParser.Parse("!PART", out string newName1));
Assert.Equal("PART", newName1);
Assert.Equal(Command.Delete, CommandParser.Parse("-PART", out string newName2));
Assert.Equal("PART", newName2);
}

[Fact]
public void TestParse__Edit()
{
Assert.Equal(Command.Edit, CommandParser.Parse("@PART", out string newName));
Assert.Equal("PART", newName);
}

[Fact]
public void TestParse__Replace()
{
Assert.Equal(Command.Replace, CommandParser.Parse("%PART", out string newName));
Assert.Equal("PART", newName);
}

[Fact]
public void TestParse__Copy()
{
Assert.Equal(Command.Copy, CommandParser.Parse("+PART", out string newName1));
Assert.Equal("PART", newName1);
Assert.Equal(Command.Copy, CommandParser.Parse("$PART", out string newName2));
Assert.Equal("PART", newName2);
}

[Fact]
public void TestParse__Rename()
{
Assert.Equal(Command.Rename, CommandParser.Parse("|PART", out string newName));
Assert.Equal("PART", newName); ;
}

[Fact]
public void TestParse__Paste()
{
Assert.Equal(Command.Paste, CommandParser.Parse("#PART", out string newName));
Assert.Equal("PART", newName);
}

[Fact]
public void TestParse__Special()
{
Assert.Equal(Command.Special, CommandParser.Parse("*PART", out string newName));
Assert.Equal("PART", newName);
}

[Fact]
public void TestParse__Special__Chained()
{
Assert.Equal(Command.Special, CommandParser.Parse("*@PART", out string newName));
Assert.Equal("@PART", newName);
}

[Fact]
public void TestParse__Create()
{
Assert.Equal(Command.Create, CommandParser.Parse("&PART", out string newName));
Assert.Equal("PART", newName);
}
}
}
1 change: 1 addition & 0 deletions ModuleManagerTests/ModuleManagerTests.csproj
Expand Up @@ -50,6 +50,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Collections\ImmutableStackTest.cs" />
<Compile Include="CommandParserTest.cs" />
<Compile Include="DummyTest.cs" />
<Compile Include="Extensions\NodeStackExtensionsTest.cs" />
<Compile Include="Logging\ModLoggerTest.cs" />
Expand Down

0 comments on commit a9a990d

Please sign in to comment.