@@ -191,53 +191,57 @@ public void ShowError(Exception ex)
191191 }
192192 }
193193
194- List < string > GetDLLsToInject ( string dllEnding )
194+ string [ ] GetDLLsToInject ( string dllEnding )
195195 {
196196 List < string > dlls = new List < string > ( ) ;
197197
198198 //coreLibs and mLibs
199- var baseFolder = new FileInfo ( Assembly . GetEntryAssembly ( ) . Location ) . Directory ;
200- var coreFolder = Path . Combine ( baseFolder . FullName , "coreLibs" ) ;
201- var mFolder = Path . Combine ( baseFolder . FullName , "mLibs" ) ;
202- if ( ! Directory . Exists ( mFolder ) )
203- Directory . CreateDirectory ( mFolder ) ;
204-
205- string libName = "SporeModAPI.lib" ;
206- string MODAPI_DLL = "SporeModAPI-" + dllEnding + ".dll" ;
207- string coreDllName = GameVersion . GetNewDLLName ( ExecutableType ) ;
208- string coreDllOutPath = Path . Combine ( mFolder , "SporeModAPI.dll" ) ;
209-
210- if ( coreDllName == null )
199+ string basePath = Path . GetDirectoryName ( Assembly . GetEntryAssembly ( ) . Location ) ;
200+ string coreLibsPath = Path . Combine ( basePath , "coreLibs" ) ;
201+ string mLibsPath = Path . Combine ( basePath , "mLibs" ) ;
202+
203+ if ( ! Directory . Exists ( mLibsPath ) )
204+ Directory . CreateDirectory ( mLibsPath ) ;
205+
206+ string coreLibFile = "SporeModAPI.lib" ;
207+ string coreLibPath = Path . Combine ( coreLibsPath , coreLibFile ) ;
208+ string coreLegacyDllName = "SporeModAPI-" + dllEnding + ".dll" ;
209+ string coreLegacyDllPath = Path . Combine ( basePath , coreLegacyDllName ) ;
210+ string coreDllPath = Path . Combine ( coreLibsPath , GameVersion . GetNewDLLName ( this . ExecutableType ) ) ;
211+ string coreDllOutPath = Path . Combine ( mLibsPath , "SporeModAPI.dll" ) ;
212+
213+ // ensure ModAPI DLLs exist
214+ foreach ( string dll in new string [ ] { coreLegacyDllPath , coreDllPath } )
211215 {
212- throw new Exception ( "Failed to retrieve DLL name!" ) ;
216+ if ( ! File . Exists ( dll ) )
217+ {
218+ throw new FileNotFoundException ( "Required ModAPI DLL was not found: " + dll ) ;
219+ }
213220 }
214221
215- File . Copy ( Path . Combine ( coreFolder , libName ) , Path . Combine ( mFolder , libName ) , true ) ;
216- File . Copy ( Path . Combine ( coreFolder , coreDllName ) , coreDllOutPath , true ) ;
222+ File . Copy ( coreLibPath , Path . Combine ( mLibsPath , coreLibFile ) , true ) ;
223+ File . Copy ( coreDllPath , coreDllOutPath , true ) ;
217224
218225 dlls . Add ( coreDllOutPath ) ;
219- dlls . Add ( Path . GetFullPath ( MODAPI_DLL ) ) ;
220-
221- foreach ( string s in Directory . EnumerateFiles ( mFolder )
222- . Where ( x => ! Path . GetFileName ( x ) . Equals ( coreDllName , StringComparison . OrdinalIgnoreCase ) &&
223- x . ToLowerInvariant ( ) . EndsWith ( ".dll" ) &&
224- ! dlls . Contains ( x ) ) )
226+ dlls . Add ( coreLegacyDllPath ) ;
227+
228+ foreach ( string file in Directory . EnumerateFiles ( mLibsPath )
229+ . Where ( x =>
230+ {
231+ x = x . ToLowerInvariant ( ) ;
232+ return x . EndsWith ( ".dll" ) && x != coreDllOutPath . ToLowerInvariant ( ) ;
233+ } ) )
225234 {
226- dlls . Add ( s ) ;
235+ dlls . Add ( file ) ;
227236 }
228237
229- foreach ( var file in baseFolder . EnumerateFiles ( "*" + dllEnding + ".dll" )
230- . Where ( x => x . Name != MODAPI_DLL ) )
238+ foreach ( string file in Directory . EnumerateFiles ( basePath , "*" + dllEnding + ".dll" )
239+ . Where ( x => x . ToLowerInvariant ( ) != coreLegacyDllPath . ToLowerInvariant ( ) ) )
231240 {
232- // the ModAPI dll should already be loaded
233- if ( file . Name != MODAPI_DLL )
234- {
235- dlls . Add ( file . FullName ) ;
236- }
241+ dlls . Add ( file ) ;
237242 }
238243
239-
240- return dlls ;
244+ return dlls . ToArray ( ) ;
241245 }
242246
243247 void InjectSporeProcess ( string dllEnding )
@@ -249,7 +253,7 @@ void InjectSporeProcess(string dllEnding)
249253 const string MOD_API_DLL_INJECTOR = "ModAPI.DLLInjector.dll" ;
250254 IntPtr hDLLInjectorHandle = Injector . InjectDLL ( this . ProcessInfo , Path . GetFullPath ( MOD_API_DLL_INJECTOR ) ) ;
251255
252- List < string > dlls = GetDLLsToInject ( dllEnding ) ;
256+ string [ ] dlls = GetDLLsToInject ( dllEnding ) ;
253257
254258 Injector . SetInjectionData ( this . ProcessInfo , hDLLInjectorHandle , dllEnding == "disk" , dlls ) ;
255259
@@ -280,20 +284,16 @@ void InjectSporeProcess(string dllEnding)
280284 void CreateSporeProcess ( )
281285 {
282286 var sb = new StringBuilder ( ) ;
283- int i = 0 ;
284- foreach ( string arg in Environment . GetCommandLineArgs ( ) )
287+ // skip first argument because it's the launcher executable
288+ foreach ( string arg in Environment . GetCommandLineArgs ( ) . Skip ( 1 ) )
285289 {
286- // the first argument is the path
287- if ( i != 0 )
288- {
289- sb . Append ( arg ) ;
290- sb . Append ( " " ) ;
291- }
292- i ++ ;
290+ sb . Append ( arg ) ;
291+ sb . Append ( " " ) ;
293292 }
294293
295294 if ( ! NativeMethods . CreateProcess ( null , "\" " + this . ExecutablePath + "\" " + sb ,
296- IntPtr . Zero , IntPtr . Zero , false , NativeTypes . ProcessCreationFlags . CREATE_SUSPENDED , IntPtr . Zero , this . SporebinPath , ref this . StartupInfo , out this . ProcessInfo ) )
295+ IntPtr . Zero , IntPtr . Zero , false , NativeTypes . ProcessCreationFlags . CREATE_SUSPENDED , IntPtr . Zero ,
296+ this . SporebinPath , ref this . StartupInfo , out this . ProcessInfo ) )
297297 {
298298 ThrowWin32Exception ( Strings . ProcessNotStarted ) ;
299299 }
0 commit comments