Skip to content

Commit 9a151b6

Browse files
committed
Merge branch 'upstream/master' into merge
Conflicts: ModuleManager/CustomConfigsManager.cs ModuleManager/FilePathRepository.cs ModuleManager/Logging/StreamLogger.cs ModuleManager/Logging/UnityLogger.cs ModuleManager/Properties/AssemblyInfo.cs Source/ModuleManager/ExceptionIntercept/InterceptLogHandler.cs Source/ModuleManager/MMPatchLoader.cs Source/ModuleManager/MMPatchRunner.cs Source/ModuleManager/ModuleManager.cs Source/ModuleManager/ModuleManager.csproj Source/TestUtilsTests/TestUtilsTests.csproj
2 parents 53af7c8 + 86e60c3 commit 9a151b6

File tree

5 files changed

+89
-56
lines changed

5 files changed

+89
-56
lines changed
Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,40 @@
11
using System;
2-
using System.IO;
32
using UnityEngine;
43

54
using static ModuleManager.FilePathRepository;
65

76
namespace ModuleManager
87
{
9-
[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
10-
public class CustomConfigsManager : MonoBehaviour
11-
{
8+
[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
9+
public class CustomConfigsManager : MonoBehaviour
10+
{
1211
internal static bool start_techtree_loaded = false;
13-
internal static bool start_physics_loaded = false;
14-
internal void Start()
15-
{
12+
internal void Start()
13+
{
1614
#if false
1715
Log("Blah");
18-
Log(HighLogic.CurrentGame.Parameters.Career.TechTreeUrl);
16+
Log(HighLogic.CurrentGame.Parameters.Career.TechTreeUrl);
1917
Log(TECHTREE_CONFIG.Path);
2018
Log(TECHTREE_CONFIG.IsLoadable.ToString());
21-
#endif
19+
#endif
2220
if (start_techtree_loaded)
23-
{
24-
if (HighLogic.CurrentGame.Parameters.Career.TechTreeUrl != TECHTREE_CONFIG.KspPath)
25-
Log(string.Format("Tech tree was changed by third party to [{0}].", HighLogic.CurrentGame.Parameters.Career.TechTreeUrl));
21+
{
22+
if (HighLogic.CurrentGame.Parameters.Career.TechTreeUrl != TECHTREE_CONFIG.KspPath)
23+
Log(string.Format("Tech tree was changed by third party to [{0}].", HighLogic.CurrentGame.Parameters.Career.TechTreeUrl));
2624
}
27-
else if (TECHTREE_CONFIG.IsLoadable)
28-
{
29-
Log("Setting modded tech tree as the active one");
30-
HighLogic.CurrentGame.Parameters.Career.TechTreeUrl = TECHTREE_CONFIG.KspPath;
25+
else if (TECHTREE_CONFIG.IsLoadable)
26+
{
27+
Log("Setting modded tech tree as the active one");
28+
HighLogic.CurrentGame.Parameters.Career.TechTreeUrl = TECHTREE_CONFIG.KspPath;
3129
start_techtree_loaded = true;
32-
}
33-
34-
if (start_physics_loaded)
35-
{
36-
if (PhysicsGlobals.PhysicsDatabaseFilename != PHYSICS_CONFIG.Path)
37-
Log(string.Format("Physics changed by third party to [{0}].", PhysicsGlobals.PhysicsDatabaseFilename));
3830
}
39-
else if (PHYSICS_CONFIG.IsLoadable)
40-
{
41-
Log("Setting modded physics as the active one");
42-
PhysicsGlobals.PhysicsDatabaseFilename = PHYSICS_CONFIG.Path;
43-
if (!PhysicsGlobals.Instance.LoadDatabase())
44-
Log("Something went wrong while setting the active physics config.");
45-
start_physics_loaded = true;
46-
}
47-
}
31+
}
4832

4933
private static readonly KSPe.Util.Log.Logger log = KSPe.Util.Log.Logger.CreateThreadUnsafeForType<CustomConfigsManager>(); // No need to use thread safe logging. Yet.
50-
private static void Log(String s)
51-
{
52-
log.info(s);
53-
}
34+
private static void Log(String s)
35+
{
36+
log.info(s);
37+
}
5438

55-
}
39+
}
5640
}

Source/ModuleManager/Extensions/StringExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,14 @@ public static string RemoveWS(this string withWhite)
2424
{
2525
return whitespaceRegex.Replace(withWhite, "");
2626
}
27+
28+
public static bool Contains(this string str, string value, out int index)
29+
{
30+
if (str == null) throw new ArgumentNullException(nameof(str));
31+
if (value == null) throw new ArgumentNullException(nameof(value));
32+
33+
index = str.IndexOf(value, StringComparison.CurrentCultureIgnoreCase);
34+
return index != -1;
35+
}
2736
}
2837
}

Source/ModuleManager/MMPatchLoader.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
namespace ModuleManager
2424
{
25-
[SuppressMessage("ReSharper", "StringLastIndexOfIsCultureSpecific.1")]
26-
[SuppressMessage("ReSharper", "StringIndexOfIsCultureSpecific.1")]
2725
public class MMPatchLoader : LoadingSystem
2826
{
2927
private const string PHYSICS_NODE_NAME = "PHYSICSGLOBALS";
@@ -902,7 +900,7 @@ public static ConfigNode ModifyNode(NodeStack original, ConfigNode mod, PatchCon
902900

903901
ConfigNode newSubMod = new ConfigNode(toPaste.name);
904902
newSubMod = ModifyNode(nodeStack.Push(newSubMod), toPaste, context);
905-
if (subName.LastIndexOf(",") > 0 && int.TryParse(subName.Substring(subName.LastIndexOf(",") + 1), out int index))
903+
if (subName.LastIndexOf(',') > 0 && int.TryParse(subName.Substring(subName.LastIndexOf(',') + 1), out int index))
906904
{
907905
// In this case insert the node at position index
908906
InsertNode(newNode, newSubMod, index);
@@ -925,11 +923,10 @@ public static ConfigNode ModifyNode(NodeStack original, ConfigNode mod, PatchCon
925923
// NODE,n will match the nth node (NODE is the same as NODE,0)
926924
// NODE,* will match ALL nodes
927925
// NODE:HAS[condition] will match ALL nodes with condition
928-
if (subName.Contains(":HAS["))
926+
if (subName.Contains(":HAS[", out int hasStart))
929927
{
930-
int start = subName.IndexOf(":HAS[");
931-
constraints = subName.Substring(start + 5, subName.LastIndexOf(']') - start - 5);
932-
subName = subName.Substring(0, start);
928+
constraints = subName.Substring(hasStart + 5, subName.LastIndexOf(']') - hasStart - 5);
929+
subName = subName.Substring(0, hasStart);
933930
}
934931

935932
if (subName.Contains(","))
@@ -1097,11 +1094,10 @@ private static ConfigNode RecurseNodeSearch(string path, NodeStack nodeStack, Pa
10971094
string constraint = "";
10981095

10991096
int index = 0;
1100-
if (subName.Contains(":HAS["))
1097+
if (subName.Contains(":HAS[", out int hasStart))
11011098
{
1102-
int start = subName.IndexOf(":HAS[");
1103-
constraint = subName.Substring(start + 5, subName.LastIndexOf(']') - start - 5);
1104-
subName = subName.Substring(0, start);
1099+
constraint = subName.Substring(hasStart + 5, subName.LastIndexOf(']') - hasStart - 5);
1100+
subName = subName.Substring(0, hasStart);
11051101
}
11061102
else if (subName.Contains(","))
11071103
{
@@ -1272,13 +1268,12 @@ private static ConfigNode.Value RecurseVariableSearch(string path, NodeStack nod
12721268
string constraint = "";
12731269
string nodeType, nodeName;
12741270
int index = 0;
1275-
if (subName.Contains(":HAS["))
1271+
if (subName.Contains(":HAS[", out int hasStart))
12761272
{
1277-
int start = subName.IndexOf(":HAS[");
1278-
constraint = subName.Substring(start + 5, subName.LastIndexOf(']') - start - 5);
1279-
subName = subName.Substring(0, start);
1273+
constraint = subName.Substring(hasStart + 5, subName.LastIndexOf(']') - hasStart - 5);
1274+
subName = subName.Substring(0, hasStart);
12801275
}
1281-
else if (subName.Contains(","))
1276+
else if (subName.Contains(','))
12821277
{
12831278
string tag = subName.Split(',')[1];
12841279
subName = subName.Split(',')[0];
@@ -1533,11 +1528,11 @@ public static bool CheckConstraints(ConfigNode node, string constraints)
15331528
constraints = constraintList[0];
15341529

15351530
string remainingConstraints = "";
1536-
if (constraints.Contains("HAS["))
1531+
if (constraints.Contains(":HAS[", out int hasStart))
15371532
{
1538-
int start = constraints.IndexOf("HAS[") + 4;
1539-
remainingConstraints = constraints.Substring(start, constraintList[0].LastIndexOf(']') - start);
1540-
constraints = constraints.Substring(0, start - 5);
1533+
hasStart += 5;
1534+
remainingConstraints = constraints.Substring(hasStart, constraintList[0].LastIndexOf(']') - hasStart);
1535+
constraints = constraints.Substring(0, hasStart - 5);
15411536
}
15421537

15431538
string[] splits = constraints.Split(contraintSeparators, 3);

Source/ModuleManager/PostPatchLoader.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ private IEnumerator Run()
127127
logger.Info("Reloading Part Upgrades");
128128
PartUpgradeManager.Handler.FillUpgrades();
129129

130+
LoadModdedPhysics();
131+
130132
yield return null;
131133

132134
progressTitle = "ModuleManager: Running post patch callbacks";
@@ -206,5 +208,18 @@ private IEnumerator Run()
206208

207209
ready = true;
208210
}
211+
212+
private void LoadModdedPhysics()
213+
{
214+
if (PHYSICS_CONFIG.IsLoadable)
215+
{
216+
logger.Info("Setting modded physics as the active one");
217+
PhysicsGlobals.PhysicsDatabaseFilename = PHYSICS_CONFIG.Path;
218+
if (!PhysicsGlobals.Instance.LoadDatabase())
219+
logger.Info("Something went wrong while setting the active physics config.");
220+
}
221+
else
222+
logger.Error("Physics file not found");
223+
}
209224
}
210225
}

Source/ModuleManagerTests/Extensions/StringExtensionsTest.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,35 @@ public void TestRemoveWS()
3939
{
4040
Assert.Equal("abcdef", " abc \tdef\r\n\t ".RemoveWS());
4141
}
42+
43+
44+
[InlineData("abc", "b", true, 1)]
45+
[InlineData("abc", "x", false, -1)]
46+
[Theory]
47+
public void TestContains(string str, string test, bool expectedResult, int expectedIndex)
48+
{
49+
bool result = str.Contains(test, out int index);
50+
Assert.Equal(expectedResult, result);
51+
Assert.Equal(expectedIndex, index);
52+
}
53+
54+
[Fact]
55+
public void TestContains__NullStr()
56+
{
57+
string s = null;
58+
Assert.Throws<ArgumentNullException>(delegate
59+
{
60+
s.Contains("x", out int _x);
61+
});
62+
}
63+
64+
[Fact]
65+
public void TestContains__NullValue()
66+
{
67+
Assert.Throws<ArgumentNullException>(delegate
68+
{
69+
"abc".Contains(null, out int _x);
70+
});
71+
}
4272
}
4373
}

0 commit comments

Comments
 (0)