Skip to content

Commit

Permalink
Fix :NEEDS causing nodes to be reodered
Browse files Browse the repository at this point in the history
Fixes #90
  • Loading branch information
blowfishpro committed Feb 8, 2018
1 parent b4a2f10 commit 61b1c45
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
37 changes: 28 additions & 9 deletions ModuleManager/NeedsChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,48 @@ public static void CheckNeeds(UrlDir gameDatabaseRoot, IEnumerable<string> mods,
" has config.name == null");
}

UrlDir.UrlConfig newMod;

if (currentMod.type.IndexOf(":NEEDS[", StringComparison.OrdinalIgnoreCase) >= 0)
{
mod.parent.configs.Remove(currentMod);
string type = currentMod.type;

if (!CheckNeeds(ref type, mods))
if (CheckNeeds(ref type, mods))
{

ConfigNode copy = new ConfigNode(type);
copy.ShallowCopyFrom(currentMod.config);
int index = mod.parent.configs.IndexOf(currentMod);
newMod = new UrlDir.UrlConfig(currentMod.parent, copy);
mod.parent.configs[index] = newMod;
}
else
{
progress.NeedsUnsatisfiedRoot(currentMod);
mod.parent.configs.Remove(currentMod);
continue;
}

ConfigNode copy = new ConfigNode(type);
copy.ShallowCopyFrom(currentMod.config);
currentMod = new UrlDir.UrlConfig(currentMod.parent, copy);
mod.parent.configs.Add(currentMod);
}
else
{
newMod = currentMod;
}

// Recursively check the contents
PatchContext context = new PatchContext(currentMod, gameDatabaseRoot, logger, progress);
CheckNeeds(new NodeStack(currentMod.config), context, mods);
PatchContext context = new PatchContext(newMod, gameDatabaseRoot, logger, progress);
CheckNeeds(new NodeStack(newMod.config), context, mods);
}
catch (Exception ex)
{
try
{
mod.parent.configs.Remove(currentMod);
}
catch(Exception ex2)
{
logger.Exception("Exception while attempting to ensure config removed" ,ex2);
}

try
{
progress.Exception(mod, "Exception while checking needs on root node :\n" + mod.PrettyPrint(), ex);
Expand Down
25 changes: 25 additions & 0 deletions ModuleManagerTests/NeedsCheckerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,31 @@ public void TestCheckNeeds__Root__CaseInsensitive()
}
}

[Fact]
public void TestCheckNeeds__Root__KeepsOrder()
{
string[] modList = { "mod1", "mod2" };

UrlDir.UrlConfig config1 = UrlBuilder.CreateConfig(new ConfigNode("NODE_1"), file);
UrlDir.UrlConfig config2 = UrlBuilder.CreateConfig(new ConfigNode("NODE_2:NEEDS[mod1]"), file);
UrlDir.UrlConfig config3 = UrlBuilder.CreateConfig(new ConfigNode("NODE_3:NEEDS[mod2]"), file);
UrlDir.UrlConfig config4 = UrlBuilder.CreateConfig(new ConfigNode("NODE_4"), file);

NeedsChecker.CheckNeeds(root, modList, progress, logger);

progress.DidNotReceiveWithAnyArgs().Exception(null, null);
progress.DidNotReceiveWithAnyArgs().Exception(null, null, null);
progress.DidNotReceiveWithAnyArgs().Error(null, null);

UrlDir.UrlConfig[] configs = root.AllConfigs.ToArray();
Assert.Equal(4, configs.Length);

Assert.Same(config1, configs[0]);
AssertUrlCorrect("NODE_2", config2, configs[1]);
AssertUrlCorrect("NODE_3", config3, configs[2]);
Assert.Same(config4, configs[3]);
}

[Fact]
public void TestCheckNeeds__Nested()
{
Expand Down

0 comments on commit 61b1c45

Please sign in to comment.