Skip to content

Commit

Permalink
PatchList handles sorting consistently
Browse files Browse the repository at this point in the history
eliminates private class that mostly just passed methods through
  • Loading branch information
blowfishpro committed Jul 7, 2020
1 parent c4ad2c3 commit 8a95d37
Showing 1 changed file with 9 additions and 37 deletions.
46 changes: 9 additions & 37 deletions ModuleManager/PatchList.cs
Expand Up @@ -37,53 +37,25 @@ public ModPass(string name)
public void AddLastPatch(IPatch patch) => lastPass.Add(patch ?? throw new ArgumentNullException(nameof(patch)));
}

private 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.ToLowerInvariant(), pass);
i++;
}
}

public ModPass this[string name] => passesDict[name.ToLowerInvariant()];
public ModPass this[int index] => passesArray[index];

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

public int Count => passesArray.Length;

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

private readonly Pass insertPatches = new Pass(":INSERT (initial)");
private readonly Pass firstPatches = new Pass(":FIRST");
private readonly Pass legacyPatches = new Pass(":LEGACY (default)");
private readonly Pass finalPatches = new Pass(":FINAL");

private readonly ModPassCollection modPasses;
private readonly SortedDictionary<string, ModPass> modPasses = new SortedDictionary<string, ModPass>(StringComparer.InvariantCultureIgnoreCase);
private readonly SortedDictionary<string, Pass> lastPasses = new SortedDictionary<string, Pass>(StringComparer.InvariantCultureIgnoreCase);

public PatchList(IEnumerable<string> modList, IEnumerable<IPatch> patches, IPatchProgress progress)
{
modPasses = new ModPassCollection(modList ?? throw new ArgumentNullException(nameof(modList)));
if (modList == null) throw new ArgumentNullException(nameof(modList));
if (patches == null) throw new ArgumentNullException(nameof(patches));
if (progress == null) throw new ArgumentNullException(nameof(progress));

foreach (string mod in modList)
{
modPasses.Add(mod, new ModPass(mod));
}

foreach (IPatch patch in patches)
{
if (patch.PassSpecifier is InsertPassSpecifier)
Expand Down Expand Up @@ -141,7 +113,7 @@ public IEnumerator<IPass> GetEnumerator()
yield return firstPatches;
yield return legacyPatches;

foreach (ModPass modPass in modPasses)
foreach (ModPass modPass in modPasses.Values)
{
yield return modPass.beforePass;
yield return modPass.forPass;
Expand All @@ -162,7 +134,7 @@ private void EnsureMod(string mod)
{
if (mod == null) throw new ArgumentNullException(nameof(mod));
if (mod == string.Empty) throw new ArgumentException("can't be empty", nameof(mod));
if (!modPasses.HasMod(mod)) throw new KeyNotFoundException($"Mod '{mod}' not found");
if (!modPasses.ContainsKey(mod)) throw new KeyNotFoundException($"Mod '{mod}' not found");
}
}
}

0 comments on commit 8a95d37

Please sign in to comment.