Skip to content

Commit

Permalink
PatchList
Browse files Browse the repository at this point in the history
list of patches, 'nuff said
  • Loading branch information
blowfishpro committed Sep 14, 2017
1 parent 24f2143 commit 7138bbc
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions ModuleManager/ModuleManager.csproj
Expand Up @@ -47,6 +47,7 @@
<Compile Include="MMPatchLoader.cs" />
<Compile Include="ModuleManager.cs" />
<Compile Include="PatchContext.cs" />
<Compile Include="PatchList.cs" />
<Compile Include="PatchProgress.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs" />
Expand Down
66 changes: 66 additions & 0 deletions ModuleManager/PatchList.cs
@@ -0,0 +1,66 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ModuleManager.Collections;

namespace ModuleManager
{
public class PatchList
{
public class ModPass
{
public readonly List<UrlDir.UrlConfig> beforePatches = new List<UrlDir.UrlConfig>(0);
public readonly List<UrlDir.UrlConfig> forPatches = new List<UrlDir.UrlConfig>(0);
public readonly List<UrlDir.UrlConfig> afterPatches = new List<UrlDir.UrlConfig>(0);

public readonly string name;

public ModPass(string name)
{
this.name = name;
}
}

public class ModPassCollection : IEnumerable<ModPass>
{
private readonly ModPass[] passesArray;
private readonly Dictionary<string, ModPass> passesDict;

public ModPassCollection(IEnumerable<string> modList)
{
int count = modList.Count();
passesArray = new ModPass[count];
passesDict = new Dictionary<string, ModPass>(count);

int i = 0;
foreach (string mod in modList)
{
ModPass pass = new ModPass(mod);
passesArray[i] = pass;
passesDict.Add(mod, pass);
i++;
}
}

public ModPass this[string name] => passesDict[name];

public bool HasMod(string name) => passesDict.ContainsKey(name);

public ArrayEnumerator<ModPass> GetEnumerator() => new ArrayEnumerator<ModPass>(passesArray);
IEnumerator<ModPass> IEnumerable<ModPass>.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

public readonly List<UrlDir.UrlConfig> firstPatches = new List<UrlDir.UrlConfig>();
public readonly List<UrlDir.UrlConfig> legacyPatches = new List<UrlDir.UrlConfig>();
public readonly List<UrlDir.UrlConfig> finalPatches = new List<UrlDir.UrlConfig>();

public readonly ModPassCollection modPasses;

public PatchList(IEnumerable<string> modList)
{
modPasses = new ModPassCollection(modList);
}
}
}
1 change: 1 addition & 0 deletions ModuleManagerTests/ModuleManagerTests.csproj
Expand Up @@ -49,6 +49,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="PatchListTest.cs" />
<Compile Include="Collections\ArrayEnumeratorTest.cs" />
<Compile Include="Collections\ImmutableStackTest.cs" />
<Compile Include="CommandParserTest.cs" />
Expand Down
74 changes: 74 additions & 0 deletions ModuleManagerTests/PatchListTest.cs
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using Xunit;
using ModuleManager;

namespace ModuleManagerTests
{
public class PatchListTest
{
[Fact]
public void TestConstructor()
{
PatchList list = new PatchList(new string[0]);

Assert.NotNull(list.firstPatches);
Assert.NotNull(list.legacyPatches);
Assert.NotNull(list.finalPatches);
Assert.NotNull(list.modPasses);
}

[Fact]
public void TestModPasses__HasMod()
{
PatchList list = new PatchList(new[] { "mod1", "mod2" });

PatchList.ModPassCollection collection = list.modPasses;

Assert.True(collection.HasMod("mod1"));
Assert.True(collection.HasMod("mod2"));
Assert.False(collection.HasMod("mod3"));
}

[Fact]
public void TestModPasses__Accessor()
{
PatchList list = new PatchList(new[] { "mod1", "mod2" });

PatchList.ModPass pass1 = list.modPasses["mod1"];
Assert.NotNull(pass1);
Assert.Equal("mod1", pass1.name);
Assert.NotNull(pass1.beforePatches);
Assert.Equal(0, pass1.beforePatches.Capacity);
Assert.NotNull(pass1.forPatches);
Assert.Equal(0, pass1.forPatches.Capacity);
Assert.NotNull(pass1.afterPatches);
Assert.Equal(0, pass1.afterPatches.Capacity);

PatchList.ModPass pass2 = list.modPasses["mod2"];
Assert.NotNull(pass2);
Assert.Equal("mod2", pass2.name);
Assert.NotNull(pass2.beforePatches);
Assert.Equal(0, pass2.beforePatches.Capacity);
Assert.NotNull(pass2.forPatches);
Assert.Equal(0, pass2.forPatches.Capacity);
Assert.NotNull(pass2.afterPatches);
Assert.Equal(0, pass2.afterPatches.Capacity);

Assert.Throws<KeyNotFoundException>(delegate
{
PatchList.ModPass mod3 = list.modPasses["mod3"];
});
}

[Fact]
public void TestModPasses__Enumeration()
{
PatchList list = new PatchList(new[] { "mod1", "mod2" });

PatchList.ModPass[] passes = new PatchList.ModPass[] { list.modPasses["mod1"], list.modPasses["mod2"] };

Assert.Equal(passes, list.modPasses);
}
}
}

0 comments on commit 7138bbc

Please sign in to comment.