Skip to content

Commit fdf0aee

Browse files
author
Swamp Ig
committed
Prevent multiple copies running
1 parent a244311 commit fdf0aee

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

SaveGameFixer.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.ObjectModel;
44
using System.IO;
55
using System.Linq;
6+
using System.Reflection;
67
using System.Text;
78
using UnityEngine;
89

@@ -51,14 +52,18 @@ internal void Awake()
5152
{
5253
try
5354
{
54-
if (!RunTypeElection(typeof(SaveGameFixer), "ModuleManager"))
55-
return;
56-
5755
// Guard against multiple copies of the same DLL
5856
if (hasRun)
57+
{
58+
Assembly currentAssembly = Assembly.GetExecutingAssembly();
59+
Debug.Log("[SaveGameFixer] Multiple copies of current version. Using the first copy. Version: " + currentAssembly.GetName().Version);
5960
return;
61+
}
6062
hasRun = true;
6163

64+
if (!RunTypeElection(typeof(SaveGameFixer), "ModuleManager"))
65+
return;
66+
6267
// So at this point we know we have won the election, and will be using the class versions as in this assembly.
6368

6469
UpdateSaves();

moduleManager.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,27 @@ public class ConfigManager : MonoBehaviour
3131
#endregion
3232

3333
#region Top Level - Update
34+
private static bool loadedInScene = false;
35+
36+
internal void Awake()
37+
{
38+
// Ensure that only one copy of the service is run per scene change.
39+
if (loadedInScene)
40+
{
41+
Assembly currentAssembly = Assembly.GetExecutingAssembly();
42+
log("[ModuleManager] Multiple copies of current version. Using the first copy. Version: " + currentAssembly.GetName().Version);
43+
Destroy(gameObject);
44+
return;
45+
}
46+
Update();
47+
loadedInScene = true;
48+
}
49+
3450
public void Update()
3551
{
52+
// Unset the loadedInScene flag. All the other copies will have this sorted out during Start, so safe to do here.
53+
loadedInScene = false;
54+
3655
#region Initialization
3756
/*
3857
* It should be a code to reload when the Reload Database debug button is used.
@@ -56,16 +75,20 @@ public void Update()
5675
}
5776

5877
if (loaded)
78+
{
79+
Destroy(gameObject);
5980
return;
81+
}
6082

6183
patchCount = 0;
6284
errorCount = 0;
6385
needsUnsatisfiedCount = 0;
6486
errorFiles = new Dictionary<string, int>();
6587
#endregion
6688

67-
6889
#region Type election
90+
91+
6992
// Check for old version and MMSarbianExt
7093
var oldMM = AssemblyLoader.loadedAssemblies.Where(a => a.assembly.GetName().Name == Assembly.GetExecutingAssembly().GetName().Name).Where(a => a.assembly.GetName().Version.CompareTo(new System.Version(1, 5, 0)) == -1);
7194
var oldAssemblies = oldMM.Concat(AssemblyLoader.loadedAssemblies.Where(a => a.assembly.GetName().Name == "MMSarbianExt"));
@@ -93,6 +116,7 @@ orderby ass.GetName().Version descending, a.path ascending
93116
{
94117
loaded = true;
95118
print("[ModuleManager] version " + currentAssembly.GetName().Version + " at " + currentAssembly.Location + " lost the election");
119+
Destroy(gameObject);
96120
return;
97121
}
98122
else
@@ -104,6 +128,7 @@ orderby ass.GetName().Version descending, a.path ascending
104128
if (candidates.Length > 0)
105129
print("[ModuleManager] version " + currentAssembly.GetName().Version + " at " + currentAssembly.Location + " won the election against\n" + candidates);
106130
}
131+
107132
#endregion
108133

109134
#region Excluding directories
@@ -212,6 +237,7 @@ orderby ass.GetName().Version descending, a.path ascending
212237
RunTestCases();
213238
#endif
214239
}
240+
215241
#endregion
216242

217243
#region Needs checking
@@ -1149,7 +1175,7 @@ public static void log(String s)
11491175

11501176
public void OnGUI()
11511177
{
1152-
if (HighLogic.LoadedScene != GameScenes.LOADING)
1178+
if (HighLogic.LoadedScene != GameScenes.LOADING || !loaded)
11531179
return;
11541180

11551181
var centeredStyle = new GUIStyle(GUI.skin.GetStyle("Label"));

0 commit comments

Comments
 (0)