@@ -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