@@ -15,7 +15,7 @@ You should have received a copy of the SKL Standard License 1.0
1515
1616*/
1717using System ;
18- 18+ using SIO = System . IO ;
1919using System . Collections . Generic ;
2020using System . Linq ;
2121
@@ -51,13 +51,13 @@ internal UpdateData(string name, string sourceFilename, string targetFilename)
5151 }
5252 internal static string UpdateIfNeeded ( UpdateData ud )
5353 {
54- string sourceFilename = System . IO . Path . Combine ( SanityLib . CalcGameData ( ) , ud . sourceFilename ) ;
55- string targetFilename = System . IO . Path . Combine ( SanityLib . CalcGameData ( ) , ud . targetFilename ) ;
54+ string sourceFilename = SIO . Path . Combine ( SanityLib . CalcGameData ( ) , ud . sourceFilename ) ;
55+ string targetFilename = SIO . Path . Combine ( SanityLib . CalcGameData ( ) , ud . targetFilename ) ;
5656
5757 Log . dbg ( "UpdateIfNeeded from {0} to {1}" , sourceFilename , targetFilename ) ;
58- if ( System . IO . File . Exists ( sourceFilename ) )
58+ if ( SIO . File . Exists ( sourceFilename ) )
5959 {
60- if ( System . IO . File . Exists ( targetFilename ) )
60+ if ( SIO . File . Exists ( targetFilename ) )
6161 {
6262 {
6363 System . Diagnostics . FileVersionInfo sourceVersionInfo = System . Diagnostics . FileVersionInfo . GetVersionInfo ( sourceFilename ) ;
@@ -75,20 +75,17 @@ internal static string UpdateIfNeeded(UpdateData ud)
7575 return Update ( ud . name , sourceFilename , targetFilename ) ;
7676 }
7777 }
78+ 79+ if ( ShouldUpdate ( sourceFilename , targetFilename ) )
80+ {
81+ Log . info ( "File {0} is older then {1}. This is going to cause trouble, updating it!" , targetFilename , ud . name ) ;
82+ Delete ( targetFilename ) ; // Remove the file or the update will not work.
83+ return Update ( ud . name , sourceFilename , targetFilename ) ;
84+ }
85+ else
7886 {
79- System . Reflection . Assembly sourceAsm = System . Reflection . Assembly . LoadFile ( sourceFilename ) ;
80- System . Reflection . Assembly targetAsm = System . Reflection . Assembly . LoadFile ( targetFilename ) ;
81- if ( ! sourceAsm . GetName ( ) . Version . Equals ( targetAsm . GetName ( ) . Version ) )
82- {
83- Log . info ( "File {0} is older then {1}. This is going to cause trouble, updating it!" , targetFilename , ud . name ) ;
84- Delete ( targetFilename ) ; // Remove the file or the update will not work.
85- return Update ( ud . name , sourceFilename , targetFilename ) ;
86- }
87- else
88- {
89- Delete ( sourceFilename ) ;
90- return null ;
91- }
87+ Delete ( sourceFilename ) ;
88+ return null ;
9289 }
9390 }
9491 else return SanityLib . Update ( ud . name , sourceFilename , targetFilename ) ;
@@ -97,6 +94,33 @@ internal static string UpdateIfNeeded(UpdateData ud)
9794 return null ;
9895 }
9996
97+ private static bool ShouldUpdate ( string srcfn , string tgtfn )
98+ {
99+ // Squad, In your endless eagerness to do anything but the technically correct solution, royally screwed up the
100+ // Assembly Loader/Resolver defeating any attempt to check for the Assemblies' metadata.
101+ //
102+ // You can load the Asm into a byte array if you want, the thing will be shortcircuited no matter what somewhere
103+ // in the loading process to whatever was loaded first.
104+ //
105+ // So we will rely on file versions, what's semantically incorrect but it should work as long no one forks KSPe e changes something.
106+ if ( 1 == Versioning . version_major && Versioning . version_minor >= 8 )
107+ {
108+ System . Diagnostics . FileVersionInfo sourceVersionInfo = System . Diagnostics . FileVersionInfo . GetVersionInfo ( srcfn ) ;
109+ System . Diagnostics . FileVersionInfo targetVersionInfo = System . Diagnostics . FileVersionInfo . GetVersionInfo ( tgtfn ) ;
110+ 111+ Log . dbg ( "1.12.x src:{0} ; tgt:{1}" , sourceVersionInfo , targetVersionInfo ) ;
112+ // We already know the files are "sane", otherwise we would not be here. So let's check only the version:
113+ return 0 != sourceVersionInfo . FileVersion . CompareTo ( targetVersionInfo . FileVersion ) ;
114+ }
115+ 116+ System . Reflection . Assembly sourceAsm = System . Reflection . Assembly . ReflectionOnlyLoad ( SIO . File . ReadAllBytes ( srcfn ) ) ;
117+ System . Reflection . Assembly targetAsm = System . Reflection . Assembly . ReflectionOnlyLoad ( SIO . File . ReadAllBytes ( tgtfn ) ) ;
118+ 119+ Log . dbg ( "src:{0} ; tgt:{1}" , sourceAsm , targetAsm ) ;
120+ 121+ return ! sourceAsm . GetName ( ) . Version . Equals ( targetAsm . GetName ( ) . Version ) ;
122+ }
123+ 100124 private static string Update ( string name , string sourceFilename , string targetFilename )
101125 {
102126 Log . dbg ( "Update from {0} to {1}" , sourceFilename , targetFilename ) ;
@@ -116,29 +140,29 @@ private static string Update(string name, string sourceFilename, string targetFi
116140 private static void Copy ( string sourceFilename , string targetFilename )
117141 {
118142 Log . dbg ( "Copying {0} to {1}" , sourceFilename , targetFilename ) ;
119- System . IO . File . Copy ( sourceFilename , targetFilename ) ;
143+ SIO . File . Copy ( sourceFilename , targetFilename ) ;
120144 }
121145
122146 private static void Delete ( string filename )
123147 {
124148 Log . dbg ( "Deleting {0}" , filename ) ;
125- if ( System . IO . File . Exists ( filename ) )
126- System . IO . File . Delete ( filename ) ;
149+ if ( SIO . File . Exists ( filename ) )
150+ SIO . File . Delete ( filename ) ;
127151 }
128152
129153 private static string GAMEDATA = null ;
130154 private static string CalcGameData ( )
131155 {
132156 if ( null != GAMEDATA ) return GAMEDATA ;
133157 System . Reflection . Assembly asm = System . Reflection . Assembly . GetAssembly ( typeof ( UnityEngine . MonoBehaviour ) ) ;
134- string path = System . IO . Path . GetDirectoryName ( asm . Location ) ;
135- string candidate = System . IO . Path . Combine ( path , "GameData" ) ;
158+ string path = SIO . Path . GetDirectoryName ( asm . Location ) ;
159+ string candidate = SIO . Path . Combine ( path , "GameData" ) ;
136160 try
137161 {
138- while ( ! System . IO . Directory . Exists ( candidate ) )
162+ while ( ! SIO . Directory . Exists ( candidate ) )
139163 {
140- path = System . IO . Path . GetDirectoryName ( path ) ;
141- candidate = System . IO . Path . Combine ( path , "GameData" ) ;
164+ path = SIO . Path . GetDirectoryName ( path ) ;
165+ candidate = SIO . Path . Combine ( path , "GameData" ) ;
142166 }
143167 Log . dbg ( "GameData found on {0}" , candidate ) ;
144168 return ( GAMEDATA = candidate ) ;
0 commit comments