Skip to content

Commit

Permalink
Prevent multiple copies running
Browse files Browse the repository at this point in the history
  • Loading branch information
Swamp Ig committed May 17, 2014
1 parent a244311 commit fdf0aee
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
11 changes: 8 additions & 3 deletions SaveGameFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;

Expand Down Expand Up @@ -51,14 +52,18 @@ internal void Awake()
{
try
{
if (!RunTypeElection(typeof(SaveGameFixer), "ModuleManager"))
return;

// Guard against multiple copies of the same DLL
if (hasRun)
{
Assembly currentAssembly = Assembly.GetExecutingAssembly();
Debug.Log("[SaveGameFixer] Multiple copies of current version. Using the first copy. Version: " + currentAssembly.GetName().Version);
return;
}
hasRun = true;

if (!RunTypeElection(typeof(SaveGameFixer), "ModuleManager"))
return;

// So at this point we know we have won the election, and will be using the class versions as in this assembly.

UpdateSaves();
Expand Down
30 changes: 28 additions & 2 deletions moduleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,27 @@ public class ConfigManager : MonoBehaviour
#endregion

#region Top Level - Update
private static bool loadedInScene = false;

internal void Awake()
{
// Ensure that only one copy of the service is run per scene change.
if (loadedInScene)
{
Assembly currentAssembly = Assembly.GetExecutingAssembly();
log("[ModuleManager] Multiple copies of current version. Using the first copy. Version: " + currentAssembly.GetName().Version);
Destroy(gameObject);
return;
}
Update();
loadedInScene = true;
}

public void Update()
{
// Unset the loadedInScene flag. All the other copies will have this sorted out during Start, so safe to do here.
loadedInScene = false;

#region Initialization
/*
* It should be a code to reload when the Reload Database debug button is used.
Expand All @@ -56,16 +75,20 @@ public void Update()
}

if (loaded)
{
Destroy(gameObject);
return;
}

patchCount = 0;
errorCount = 0;
needsUnsatisfiedCount = 0;
errorFiles = new Dictionary<string, int>();
#endregion


#region Type election


// Check for old version and MMSarbianExt
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);
var oldAssemblies = oldMM.Concat(AssemblyLoader.loadedAssemblies.Where(a => a.assembly.GetName().Name == "MMSarbianExt"));
Expand Down Expand Up @@ -93,6 +116,7 @@ orderby ass.GetName().Version descending, a.path ascending
{
loaded = true;
print("[ModuleManager] version " + currentAssembly.GetName().Version + " at " + currentAssembly.Location + " lost the election");
Destroy(gameObject);
return;
}
else
Expand All @@ -104,6 +128,7 @@ orderby ass.GetName().Version descending, a.path ascending
if (candidates.Length > 0)
print("[ModuleManager] version " + currentAssembly.GetName().Version + " at " + currentAssembly.Location + " won the election against\n" + candidates);
}

#endregion

#region Excluding directories
Expand Down Expand Up @@ -212,6 +237,7 @@ orderby ass.GetName().Version descending, a.path ascending
RunTestCases();
#endif
}

#endregion

#region Needs checking
Expand Down Expand Up @@ -1149,7 +1175,7 @@ public static void log(String s)

public void OnGUI()
{
if (HighLogic.LoadedScene != GameScenes.LOADING)
if (HighLogic.LoadedScene != GameScenes.LOADING || !loaded)
return;

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

0 comments on commit fdf0aee

Please sign in to comment.