Skip to content

Commit

Permalink
Merge pull request #78 from sarbian/AddTests
Browse files Browse the repository at this point in the history
Add tests (and other misc improvements)
  • Loading branch information
sarbian committed Sep 12, 2017
2 parents 8179c44 + 47ce015 commit 80cefc3
Show file tree
Hide file tree
Showing 51 changed files with 1,355 additions and 84 deletions.
32 changes: 29 additions & 3 deletions ModuleManager.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModuleManager", "ModuleManager.csproj", "{02C8E3AF-69F9-4102-AB60-DD6DE60662D3}"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModuleManager", "ModuleManager\ModuleManager.csproj", "{02C8E3AF-69F9-4102-AB60-DD6DE60662D3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModuleManagerTests", "ModuleManagerTests\ModuleManagerTests.csproj", "{BC2A08C8-64EF-4823-A40B-8889C1CCFD75}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUtils", "TestUtils\TestUtils.csproj", "{20EAAFE6-510D-4374-8D2F-6B52D0178E85}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUtilsTests", "TestUtilsTests\TestUtilsTests.csproj", "{E695C11F-4217-4014-9B51-7232A654C205}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -13,6 +21,24 @@ Global
{02C8E3AF-69F9-4102-AB60-DD6DE60662D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{02C8E3AF-69F9-4102-AB60-DD6DE60662D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02C8E3AF-69F9-4102-AB60-DD6DE60662D3}.Release|Any CPU.Build.0 = Release|Any CPU
{BC2A08C8-64EF-4823-A40B-8889C1CCFD75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BC2A08C8-64EF-4823-A40B-8889C1CCFD75}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC2A08C8-64EF-4823-A40B-8889C1CCFD75}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC2A08C8-64EF-4823-A40B-8889C1CCFD75}.Release|Any CPU.Build.0 = Release|Any CPU
{20EAAFE6-510D-4374-8D2F-6B52D0178E85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{20EAAFE6-510D-4374-8D2F-6B52D0178E85}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20EAAFE6-510D-4374-8D2F-6B52D0178E85}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20EAAFE6-510D-4374-8D2F-6B52D0178E85}.Release|Any CPU.Build.0 = Release|Any CPU
{E695C11F-4217-4014-9B51-7232A654C205}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E695C11F-4217-4014-9B51-7232A654C205}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E695C11F-4217-4014-9B51-7232A654C205}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E695C11F-4217-4014-9B51-7232A654C205}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4DCC10BA-6036-4DCF-A78B-086DF4487922}
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = ModuleManager.csproj
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ internal void Start()
{
if (HighLogic.CurrentGame.Parameters.Career.TechTreeUrl != MMPatchLoader.techTreeFile && File.Exists(MMPatchLoader.techTreePath))
{
log("Setting modded tech tree as the active one");
Log("Setting modded tech tree as the active one");
HighLogic.CurrentGame.Parameters.Career.TechTreeUrl = MMPatchLoader.techTreeFile;
}

if (PhysicsGlobals.PhysicsDatabaseFilename != MMPatchLoader.physicsFile && File.Exists(MMPatchLoader.physicsPath))
{
log("Setting modded physics as the active one");
Log("Setting modded physics as the active one");

PhysicsGlobals.PhysicsDatabaseFilename = MMPatchLoader.physicsFile;

if (!PhysicsGlobals.Instance.LoadDatabase())
log("Something went wrong while setting the active physics config.");
Log("Something went wrong while setting the active physics config.");
}
}

public static void log(String s)
public static void Log(String s)
{
print("[CustomConfigsManager] " + s);
}
Expand Down
File renamed without changes.
12 changes: 7 additions & 5 deletions IPatchProgress.cs → ModuleManager/IPatchProgress.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using NodeStack = ModuleManager.Collections.ImmutableStack<ConfigNode>;

namespace ModuleManager
{
Expand All @@ -17,11 +18,12 @@ public interface IPatchProgress
void Error(UrlDir.UrlConfig url, string message);
void Exception(string message, Exception exception);
void Exception(UrlDir.UrlConfig url, string message, Exception exception);
void NeedsUnsatisfiedNode(string url, string path);
void NeedsUnsatisfiedValue(string url, string path, string valName);
void ApplyingCopy(string url, string patchUrl);
void ApplyingDelete(string url, string patchUrl);
void ApplyingUpdate(string url, string patchUrl);
void NeedsUnsatisfiedRoot(UrlDir.UrlConfig url);
void NeedsUnsatisfiedNode(UrlDir.UrlConfig url, NodeStack path);
void NeedsUnsatisfiedValue(UrlDir.UrlConfig url, NodeStack path, string valName);
void ApplyingCopy(UrlDir.UrlConfig original, UrlDir.UrlConfig patch);
void ApplyingDelete(UrlDir.UrlConfig original, UrlDir.UrlConfig patch);
void ApplyingUpdate(UrlDir.UrlConfig original, UrlDir.UrlConfig patch);
void PatchAdded();
void PatchApplied();
}
Expand Down
File renamed without changes.
File renamed without changes.
85 changes: 36 additions & 49 deletions MMPatchLoader.cs → ModuleManager/MMPatchLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void Update()
StatusUpdate();
}

public static void addPostPatchCallback(ModuleManagerPostPatchCallback callback)
public static void AddPostPatchCallback(ModuleManagerPostPatchCallback callback)
{
if (!postPatchCallbacks.Contains(callback))
postPatchCallbacks.Add(callback);
Expand Down Expand Up @@ -184,8 +184,7 @@ private void PrePatchInit()
modlist += "Non-DLL mods added (:FOR[xxx]):\n";
foreach (UrlDir.UrlConfig cfgmod in GameDatabase.Instance.root.AllConfigs)
{
string name;
if (ParseCommand(cfgmod.type, out name) != Command.Insert)
if (ParseCommand(cfgmod.type, out string name) != Command.Insert)
{
progress.PatchAdded();
if (name.Contains(":FOR["))
Expand Down Expand Up @@ -585,7 +584,7 @@ private bool CheckFilesChange(UrlDir.UrlFile[] files, ConfigNode shaConfigNode)

for (int i = 0; i < files.Length; i++)
{
ConfigNode fileNode = getFileNode(shaConfigNode, files[i].url);
ConfigNode fileNode = GetFileNode(shaConfigNode, files[i].url);
string fileSha = fileNode?.GetValue("SHA");

if (fileNode == null)
Expand All @@ -599,7 +598,7 @@ private bool CheckFilesChange(UrlDir.UrlFile[] files, ConfigNode shaConfigNode)
}
for (int i = 0; i < files.Length; i++)
{
ConfigNode fileNode = getFileNode(shaConfigNode, files[i].url);
ConfigNode fileNode = GetFileNode(shaConfigNode, files[i].url);

if (fileNode == null)
{
Expand All @@ -618,7 +617,7 @@ private bool CheckFilesChange(UrlDir.UrlFile[] files, ConfigNode shaConfigNode)
return noChange;
}

private ConfigNode getFileNode(ConfigNode shaConfigNode, string filename)
private ConfigNode GetFileNode(ConfigNode shaConfigNode, string filename)
{
for (int i = 0; i < shaConfigNode.nodes.Count; i++)
{
Expand Down Expand Up @@ -798,7 +797,7 @@ private void CheckNeeds()

if (!CheckNeeds(ref type))
{
progress.NeedsUnsatisfiedNode(currentMod.parent.url, currentMod.type);
progress.NeedsUnsatisfiedRoot(currentMod);
continue;
}

Expand Down Expand Up @@ -837,7 +836,7 @@ private void CheckNeeds(NodeStack stack, PatchContext context)
else
{
needsCopy = true;
context.progress.NeedsUnsatisfiedValue(context.patchUrl.url, stack.GetPath(), val.name);
context.progress.NeedsUnsatisfiedValue(context.patchUrl, stack, val.name);
}
}
catch (ArgumentOutOfRangeException e)
Expand Down Expand Up @@ -874,7 +873,7 @@ private void CheckNeeds(NodeStack stack, PatchContext context)
else
{
needsCopy = true;
progress.NeedsUnsatisfiedNode(context.patchUrl.url, stack.Push(node).GetPath());
progress.NeedsUnsatisfiedNode(context.patchUrl, stack.Push(node));
}
}
catch (ArgumentOutOfRangeException e)
Expand Down Expand Up @@ -969,8 +968,7 @@ public IEnumerator ApplyPatch(string Stage)
try
{
string name = RemoveWS(mod.type);
string tmp;
Command cmd = ParseCommand(name, out tmp);
Command cmd = ParseCommand(name, out string tmp);

if (cmd != Command.Insert)
{
Expand Down Expand Up @@ -1029,15 +1027,15 @@ public IEnumerator ApplyPatch(string Stage)
switch (cmd)
{
case Command.Edit:
progress.ApplyingUpdate(url.url, mod.url);
progress.ApplyingUpdate(url, mod);
url.config = ModifyNode(new NodeStack(url.config), mod.config, context);
break;

case Command.Copy:
ConfigNode clone = ModifyNode(new NodeStack(url.config), mod.config, context);
if (url.config.name != mod.name)
{
progress.ApplyingCopy(url.url, mod.url);
progress.ApplyingCopy(url, mod);
url.parent.configs.Add(new UrlDir.UrlConfig(url.parent, clone));
}
else
Expand All @@ -1048,7 +1046,7 @@ public IEnumerator ApplyPatch(string Stage)
break;

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

Expand Down Expand Up @@ -1112,7 +1110,7 @@ public IEnumerator ApplyPatch(string Stage)
// it uses FindConfigNodeIn(src, nodeType, nodeName, nodeTag) to recurse.
public static ConfigNode ModifyNode(NodeStack original, ConfigNode mod, PatchContext context)
{
ConfigNode newNode = DeepCopy(original.value);
ConfigNode newNode = original.value.CreateCopy();
NodeStack nodeStack = original.ReplaceValue(newNode);

#region Values
Expand All @@ -1125,8 +1123,8 @@ public static ConfigNode ModifyNode(NodeStack original, ConfigNode mod, PatchCon
#if LOGSPAM
vals += "\n " + modVal.name + "= " + modVal.value;
#endif
string valName;
Command cmd = ParseCommand(modVal.name, out valName);

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

if (cmd == Command.Special)
{
Expand All @@ -1149,8 +1147,7 @@ public static ConfigNode ModifyNode(NodeStack original, ConfigNode mod, PatchCon

if (assignMatch.Groups[2].Success)
{
double os, s;
if (double.TryParse(modVal.value, out s) && double.TryParse(val.value, out os))
if (double.TryParse(modVal.value, out double s) && double.TryParse(val.value, out double os))
{
switch (assignMatch.Groups[2].Value[0])
{
Expand Down Expand Up @@ -1295,9 +1292,19 @@ public static ConfigNode ModifyNode(NodeStack original, ConfigNode mod, PatchCon

if (varValue != null)
{
ConfigNode.Value origVal;
string value = FindAndReplaceValue(mod, ref valName, varValue, newNode, op, index,
out origVal, context, match.Groups[3].Success, position, isPosStar, seperator);
string value = FindAndReplaceValue(
mod,
ref valName,
varValue, newNode,
op,
index,
out ConfigNode.Value origVal,
context,
match.Groups[3].Success,
position,
isPosStar,
seperator
);

if (value != null)
{
Expand Down Expand Up @@ -1417,16 +1424,14 @@ public static ConfigNode ModifyNode(NodeStack original, ConfigNode mod, PatchCon
}

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

if (command == Command.Insert)
{
ConfigNode newSubMod = new ConfigNode(subMod.name);
newSubMod = ModifyNode(nodeStack.Push(newSubMod), subMod, context);
subName = newSubMod.name;
int index;
if (subName.Contains(",") && int.TryParse(subName.Split(',')[1], out index))
if (subName.Contains(",") && int.TryParse(subName.Split(',')[1], out int index))
{
// In this case insert the node at position index (with the same node names)
newSubMod.name = subName.Split(',')[0];
Expand Down Expand Up @@ -1461,8 +1466,7 @@ public static ConfigNode ModifyNode(NodeStack original, ConfigNode mod, PatchCon

ConfigNode newSubMod = new ConfigNode(toPaste.name);
newSubMod = ModifyNode(nodeStack.Push(newSubMod), toPaste, context);
int index;
if (subName.LastIndexOf(",") > 0 && int.TryParse(subName.Substring(subName.LastIndexOf(",") + 1), out index))
if (subName.LastIndexOf(",") > 0 && int.TryParse(subName.Substring(subName.LastIndexOf(",") + 1), out int index))
{
// In this case insert the node at position index
InsertNode(newNode, newSubMod, index);
Expand Down Expand Up @@ -1917,8 +1921,7 @@ private static ConfigNode.Value RecurseVariableSearch(string path, NodeStack nod
if (match.Groups[3].Success)
{
ConfigNode.Value newVal = new ConfigNode.Value(cVal.name, cVal.value);
int splitIdx = 0;
int.TryParse(match.Groups[3].Value, out splitIdx);
int.TryParse(match.Groups[3].Value, out int splitIdx);

char sep = ',';
if (match.Groups[4].Success)
Expand Down Expand Up @@ -1998,7 +2001,6 @@ private static string ProcessVariableSearch(string value, NodeStack nodeStack, P
oValue = strArray[posIndex];
if (op != ' ')
{
double s, os;
if (op == '^')
{
try
Expand All @@ -2024,7 +2026,7 @@ private static string ProcessVariableSearch(string value, NodeStack nodeStack, P
return null;
}
}
else if (double.TryParse(value, out s) && double.TryParse(oValue, out os))
else if (double.TryParse(value, out double s) && double.TryParse(oValue, out double os))
{
switch (op)
{
Expand Down Expand Up @@ -2316,8 +2318,7 @@ public static bool WildcardMatchValues(ConfigNode node, string type, string valu
if (!compare && WildcardMatch(values[i], value))
return true;

double val2;
if (compare && Double.TryParse(values[i], out val2)
if (compare && Double.TryParse(values[i], out double val2)
&& ((value[0] == '<' && val2 < val) || (value[0] == '>' && val2 > val)))
{
return true;
Expand All @@ -2332,8 +2333,7 @@ public static bool WildcardMatch(string s, string wildcard)
return true;
string pattern = "^" + Regex.Escape(wildcard).Replace(@"\*", ".*").Replace(@"\?", ".") + "$";

Regex regex;
if (!regexCache.TryGetValue(pattern, out regex))
if (!regexCache.TryGetValue(pattern, out Regex regex))
{
regex = new Regex(pattern);
regexCache.Add(pattern, regex);
Expand Down Expand Up @@ -2390,19 +2390,6 @@ private static void ShallowCopy(ConfigNode from, ConfigNode to)
to.nodes.Add(node);
}

private static ConfigNode DeepCopy(ConfigNode from)
{
ConfigNode to = new ConfigNode(from.name);
foreach (ConfigNode.Value value in from.values)
to.AddValue(value.name, value.value);
foreach (ConfigNode node in from.nodes)
{
ConfigNode newNode = DeepCopy(node);
to.nodes.Add(newNode);
}
return to;
}

private string PrettyConfig(UrlDir.UrlConfig config)
{
StringBuilder sb = new StringBuilder();
Expand Down

0 comments on commit 80cefc3

Please sign in to comment.