Skip to content

Commit

Permalink
Loop only applies to edit patches
Browse files Browse the repository at this point in the history
Saves another indentation level.  Also remove MM_PATCH_LOOP {} after
done
  • Loading branch information
blowfishpro committed Oct 14, 2017
1 parent 20e6192 commit 244af1a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
81 changes: 39 additions & 42 deletions ModuleManager/PatchApplier.cs
Expand Up @@ -83,52 +83,49 @@ private void ApplyPatches(string stage, IEnumerable<UrlDir.UrlConfig> patches)

foreach (UrlDir.UrlConfig url in allConfigs)
{
bool loop = false;
do
if (IsMatch(url.config, type, patterns, condition))
{
if (IsMatch(url.config, type, patterns, condition))
switch (cmd)
{
switch (cmd)
{
case Command.Edit:
case Command.Edit:
ConfigNode node = url.config;
bool loop = mod.config.HasNode("MM_PATCH_LOOP");
if (loop) logger.Info("Looping on " + mod.SafeUrl() + " to " + url.SafeUrl());

do
{
progress.ApplyingUpdate(url, mod);
url.config = MMPatchLoader.ModifyNode(new NodeStack(url.config), mod.config, context);
break;

case Command.Copy:
ConfigNode clone = MMPatchLoader.ModifyNode(new NodeStack(url.config), mod.config, context);
if (url.config.HasValue("name") && url.config.GetValue("name") == clone.GetValue("name"))
{
progress.Error(mod, $"Error - when applying copy {mod.SafeUrl()} to {url.SafeUrl()} - the copy needs to have a different name than the parent (use @name = xxx)");
}
else
{
progress.ApplyingCopy(url, mod);
url.parent.configs.Add(new UrlDir.UrlConfig(url.parent, clone));
}
break;

case Command.Delete:
progress.ApplyingDelete(url, mod);
url.parent.configs.Remove(url);
break;

default:
logger.Warning("Invalid command encountered on a root node: " + mod.SafeUrl());
break;
}
// When this special node is found then try to apply the patch once more on the same NODE
if (mod.config.HasNode("MM_PATCH_LOOP"))
{
logger.Info("Looping on " + mod.SafeUrl() + " to " + url.SafeUrl());
loop = true;
}
}
else
{
loop = false;
node = MMPatchLoader.ModifyNode(new NodeStack(node), mod.config, context);
} while (loop && IsMatch(node, type, patterns, condition));

if (loop) node.RemoveNodes("MM_PATCH_LOOP");

url.config = node;
break;

case Command.Copy:
ConfigNode clone = MMPatchLoader.ModifyNode(new NodeStack(url.config), mod.config, context);
if (url.config.HasValue("name") && url.config.GetValue("name") == clone.GetValue("name"))
{
progress.Error(mod, $"Error - when applying copy {mod.SafeUrl()} to {url.SafeUrl()} - the copy needs to have a different name than the parent (use @name = xxx)");
}
else
{
progress.ApplyingCopy(url, mod);
url.parent.configs.Add(new UrlDir.UrlConfig(url.parent, clone));
}
break;

case Command.Delete:
progress.ApplyingDelete(url, mod);
url.parent.configs.Remove(url);
break;

default:
logger.Warning("Invalid command encountered on a root node: " + mod.SafeUrl());
break;
}
} while (loop);
}
}
progress.PatchApplied();
}
Expand Down
13 changes: 8 additions & 5 deletions ModuleManagerTests/PatchApplierTest.cs
Expand Up @@ -740,6 +740,7 @@ public void TestApplyPatches__Loop()
UrlDir.UrlConfig patch1 = new UrlDir.UrlConfig(file, new TestConfigNode("@PART:HAS[~aaa[>10]]")
{
{ "@aaa *", "2" },
{ "bbb", "002" },
new ConfigNode("MM_PATCH_LOOP"),
});

Expand All @@ -750,7 +751,9 @@ public void TestApplyPatches__Loop()
EnsureNoErrors();

progress.Received(1).PatchApplied();
progress.Received().ApplyingUpdate(config1, patch1);
progress.Received(4).ApplyingUpdate(config1, patch1);

logger.Received().Info("Looping on abc/def/@PART:HAS[~aaa[>10]] to abc/def/PART");

UrlDir.UrlConfig[] allConfigs = databaseRoot.AllConfigs.ToArray();
Assert.Equal(1, allConfigs.Length);
Expand All @@ -759,10 +762,10 @@ public void TestApplyPatches__Loop()
{
{ "name", "000" },
{ "aaa", "16" },
new ConfigNode("MM_PATCH_LOOP"),
new ConfigNode("MM_PATCH_LOOP"),
new ConfigNode("MM_PATCH_LOOP"),
new ConfigNode("MM_PATCH_LOOP"),
{ "bbb", "002" },
{ "bbb", "002" },
{ "bbb", "002" },
{ "bbb", "002" },
}, allConfigs[0].config);
}

Expand Down

0 comments on commit 244af1a

Please sign in to comment.