1- /**
2- * Copyright (fundCfg) 2014, Majiir
3- * All rights reserved.
4- *
5- * Redistribution and use in source and binary forms, with or without modification, are permitted
6- * provided that the following conditions are met:
7- *
8- * 1. Redistributions of source code must retain the above copyright notice, this list of
9- * conditions and the following disclaimer.
10- *
11- * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
12- * conditions and the following disclaimer in the documentation and/or other materials provided
13- * with the distribution.
14- *
15- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23- * POSSIBILITY OF SUCH DAMAGE.
24- */
1+
2+ /**
3+ * Copyright (c) 2014, Majiir
4+ * All rights reserved.
5+ *
6+ * Redistribution and use in source and binary forms, with or without modification, are permitted
7+ * provided that the following conditions are met:
8+ *
9+ * 1. Redistributions of source code must retain the above copyright notice, this list of
10+ * conditions and the following disclaimer.
11+ *
12+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
13+ * conditions and the following disclaimer in the documentation and/or other materials provided
14+ * with the distribution.
15+ *
16+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
17+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
18+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+ * POSSIBILITY OF SUCH DAMAGE.
25+ */
2526
2627using System ;
2728using System . Collections . Generic ;
3031using UnityEngine ;
3132
3233/*-----------------------------------------*\
33- | SUBSTITUTE YOUR MOD'S NAMESPACE HERE. |
34+ | SUBSTITUTE YOUR MOD'S NAMESPACE HERE. |
3435\*-----------------------------------------*/
3536namespace TweakScale
3637{
3738
3839 /**
39- * This utility displays destination warning with destination list of mods that determine themselves
40- * to be incompatible with the current running version of Kerbal Space Program.
41- *
42- * See this forum thread for details:
43- * http://forum.kerbalspaceprogram.com/threads/65395-Voluntarily-Locking-Plugins-to-destination -Particular-KSP-Version
44- */
45-
46- [ KSPAddon ( KSPAddon . Startup . MainMenu , true ) ]
40+ * This utility displays a warning with a list of mods that determine themselves
41+ * to be incompatible with the current running version of Kerbal Space Program.
42+ *
43+ * See this forum thread for details:
44+ * http://forum.kerbalspaceprogram.com/threads/65395-Voluntarily-Locking-Plugins-to-a -Particular-KSP-Version
45+ */
46+
47+ [ KSPAddon ( KSPAddon . Startup . Instantly , true ) ]
4748 internal class CompatibilityChecker : MonoBehaviour
4849 {
4950 public static bool IsCompatible ( )
5051 {
5152 /*-----------------------------------------------*\
52- | BEGIN IMPLEMENTATION-SPECIFIC EDITS HERE. |
53+ | BEGIN IMPLEMENTATION-SPECIFIC EDITS HERE. |
5354 \*-----------------------------------------------*/
5455
5556 // TODO: Implement your own compatibility check.
5657 //
5758 // If you want to disable some behavior when incompatible, other parts of the plugin
5859 // should query this method:
5960 //
60- // if (!CompatibilityChecker.IsCompatible ()) {
61- // ...disable some features...
62- // }
61+ // if (!CompatibilityChecker.IsAllCompatible ()) {
62+ // ...disable some features...
63+ // }
6364 //
6465 // Even if you don't lock down functionality, you should return true if your users
65- // can expect destination future update to be available.
66+ // can expect a future update to be available.
6667 //
68+ return Versioning . version_major == 0 && Versioning . version_minor == 25 && Versioning . Revision == 0 ;
6769
68- if ( Versioning . version_minor != 24 )
69- {
70- return false ;
71- }
70+ /*-----------------------------------------------*\
71+ | IMPLEMENTERS SHOULD NOT EDIT BEYOND THIS POINT! |
72+ \*-----------------------------------------------*/
73+ }
74+
75+ public static bool IsUnityCompatible ( )
76+ {
77+ /*-----------------------------------------------*\
78+ | BEGIN IMPLEMENTATION-SPECIFIC EDITS HERE. |
79+ \*-----------------------------------------------*/
80+
81+ // TODO: Implement your own Unity compatibility check.
82+ //
83+ // Not going to care about the fact that KSP .25 OSX uses a different Unity...
7284 return true ;
7385
7486 /*-----------------------------------------------*\
@@ -77,7 +89,7 @@ public static bool IsCompatible()
7789 }
7890
7991 // Version of the compatibility checker itself.
80- private static int _version = 2 ;
92+ private static int _version = 4 ;
8193
8294 public void Start ( )
8395 {
@@ -113,23 +125,82 @@ public void Start()
113125 }
114126 catch ( Exception e )
115127 {
116- // If destination mod throws an exception from IsCompatible, it's not compatible.
128+ // If a mod throws an exception from IsCompatible, it's not compatible.
117129 Debug . LogWarning ( String . Format ( "[CompatibilityChecker] Exception while invoking IsCompatible() from '{0}':\n \n {1}" , m . DeclaringType . Assembly . GetName ( ) . Name , e ) ) ;
118130 return true ;
119131 }
120132 } )
121133 . Select ( m => m . DeclaringType . Assembly . GetName ( ) . Name )
122134 . ToArray ( ) ;
123135
136+ // A mod is incompatible with Unity if its compatibility checker has an IsUnityCompatible method which returns false.
137+ String [ ] incompatibleUnity =
138+ fields
139+ . Select ( f => f . DeclaringType . GetMethod ( "IsUnityCompatible" , Type . EmptyTypes ) )
140+ . Where ( m => m != null ) // Mods without IsUnityCompatible() are assumed to be compatible.
141+ . Where ( m => m . IsStatic )
142+ . Where ( m => m . ReturnType == typeof ( bool ) )
143+ . Where ( m =>
144+ {
145+ try
146+ {
147+ return ! ( bool ) m . Invoke ( null , new object [ 0 ] ) ;
148+ }
149+ catch ( Exception e )
150+ {
151+ // If a mod throws an exception from IsUnityCompatible, it's not compatible.
152+ Debug . LogWarning ( String . Format ( "[CompatibilityChecker] Exception while invoking IsUnityCompatible() from '{0}':\n \n {1}" , m . DeclaringType . Assembly . GetName ( ) . Name , e ) ) ;
153+ return true ;
154+ }
155+ } )
156+ . Select ( m => m . DeclaringType . Assembly . GetName ( ) . Name )
157+ . ToArray ( ) ;
158+
124159 Array . Sort ( incompatible ) ;
160+ Array . Sort ( incompatibleUnity ) ;
161+
162+ String message = String . Empty ;
163+
164+ if ( IsWin64 ( ) )
165+ {
166+ message += "WARNING: You are using 64-bit KSP on Windows. This version of KSP is known to cause crashes. It's highly recommended that you use either 32-bit KSP on Windows or switch to Linux." ;
167+ }
168+
169+ if ( ( incompatible . Length > 0 ) || ( incompatibleUnity . Length > 0 ) )
170+ {
171+ message += ( ( message == String . Empty ) ? "Some" : "\n \n Additionally, some" ) + " installed mods may be incompatible with this version of Kerbal Space Program. Features may be broken or disabled. Please check for updates to the listed mods." ;
172+
173+ if ( incompatible . Length > 0 )
174+ {
175+ Debug . LogWarning ( "[CompatibilityChecker] Incompatible mods detected: " + String . Join ( ", " , incompatible ) ) ;
176+ message += String . Format ( "\n \n These mods are incompatible with KSP {0}.{1}.{2}:\n \n " , Versioning . version_major , Versioning . version_minor , Versioning . Revision ) ;
177+ message += String . Join ( "\n " , incompatible ) ;
178+ }
179+
180+ if ( incompatibleUnity . Length > 0 )
181+ {
182+ Debug . LogWarning ( "[CompatibilityChecker] Incompatible mods (Unity) detected: " + String . Join ( ", " , incompatibleUnity ) ) ;
183+ message += String . Format ( "\n \n These mods are incompatible with Unity {0}:\n \n " , Application . unityVersion ) ;
184+ message += String . Join ( "\n " , incompatibleUnity ) ;
185+ }
186+ }
125187
126- if ( incompatible . Length > 0 )
188+ if ( ( incompatible . Length > 0 ) || ( incompatibleUnity . Length > 0 ) || IsWin64 ( ) )
127189 {
128- Debug . LogWarning ( "[CompatibilityChecker] Incompatible mods detected: " + String . Join ( ", " , incompatible ) ) ;
129- PopupDialog . SpawnPopupDialog ( "Incompatible Mods Detected" , "Some installed mods are incompatible with this version of Kerbal Space Program. Some features may be broken or disabled. Please check for updates to the following mods:\n \n " + String . Join ( "\n " , incompatible ) , "OK" , false , HighLogic . Skin ) ;
190+ PopupDialog . SpawnPopupDialog ( "Incompatible Mods Detected" , message , "OK" , true , HighLogic . Skin ) ;
130191 }
131192 }
132193
194+ public static bool IsWin64 ( )
195+ {
196+ return ( IntPtr . Size == 8 ) && ( Environment . OSVersion . Platform == PlatformID . Win32NT ) ;
197+ }
198+
199+ public static bool IsAllCompatible ( )
200+ {
201+ return IsCompatible ( ) && IsUnityCompatible ( ) && ! IsWin64 ( ) ;
202+ }
203+
133204 private static IEnumerable < Type > getAllTypes ( )
134205 {
135206 foreach ( var assembly in AppDomain . CurrentDomain . GetAssemblies ( ) )
@@ -151,4 +222,4 @@ private static IEnumerable<Type> getAllTypes()
151222 }
152223 }
153224 }
154- }
225+ }
0 commit comments