Skip to content

Commit c4993da

Browse files
committed
Spore ModAPI Launcher: misc improvements
1 parent fb00b69 commit c4993da

File tree

2 files changed

+48
-47
lines changed

2 files changed

+48
-47
lines changed

Spore ModAPI Launcher/Injector.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public static IntPtr InjectDLL(NativeTypes.PROCESS_INFORMATION pi, string dllPat
2727
IntPtr objPtr = NativeMethods.VirtualAllocEx(hProc, IntPtr.Zero, (uint)dllPath.Length + 1, NativeTypes.AllocationType.Commit, NativeTypes.MemoryProtection.ReadWrite);
2828
if (objPtr == IntPtr.Zero)
2929
{
30-
Program.ThrowWin32Exception("Virtual alloc failure.");
30+
string additionalErrorText = "\n" + "hProc: " + hProc.ToString() + "\nProgram.processHandle: " + (Program.processHandle == IntPtr.Zero);
31+
Program.ThrowWin32Exception("Virtual alloc failure.", additionalErrorText);
3132
}
3233

3334
//Write the path to the DLL file in the location just created
@@ -53,7 +54,7 @@ public static IntPtr InjectDLL(NativeTypes.PROCESS_INFORMATION pi, string dllPat
5354
{
5455
if (NativeMethods.WaitForSingleObject(hRemoteThread, MAXWAIT) == WAIT_TIMEOUT)
5556
{
56-
Program.ThrowWin32Exception("Wait for single object failed. This usually occurs if something has become stuck during injection, or if another error was left open for too long.");
57+
Program.ThrowWin32Exception("Wait for single object failed.");
5758
}
5859
while (NativeMethods.GetExitCodeThread(hRemoteThread, out thread_result))
5960
{
@@ -76,7 +77,7 @@ public static IntPtr InjectDLL(NativeTypes.PROCESS_INFORMATION pi, string dllPat
7677
return new IntPtr((Int64)thread_result);
7778
}
7879

79-
public static void SetInjectionData(NativeTypes.PROCESS_INFORMATION pi, IntPtr hDLLInjectorHandle, bool is_disc_spore, List<string> dlls)
80+
public static void SetInjectionData(NativeTypes.PROCESS_INFORMATION pi, IntPtr hDLLInjectorHandle, bool is_disc_spore, string[] dlls)
8081
{
8182
IntPtr hLocalDLLInjectorHandle = NativeMethods.LoadLibraryEx("ModAPI.DLLInjector.dll", IntPtr.Zero, NativeTypes.LoadLibraryFlags.DONT_RESOLVE_DLL_REFERENCES);
8283
IntPtr SetInjectDataPtr = NativeMethods.GetProcAddress(hLocalDLLInjectorHandle, "SetInjectionData");
@@ -113,7 +114,7 @@ public static void SetInjectionData(NativeTypes.PROCESS_INFORMATION pi, IntPtr h
113114
var bytes = new byte[total_alloc_size];
114115
int byte_offset = 0;
115116
bytes[byte_offset++] = (byte)(is_disc_spore ? 1 : 0);
116-
foreach (byte b in BitConverter.GetBytes((uint)dlls.Count))
117+
foreach (byte b in BitConverter.GetBytes((uint)dlls.Length))
117118
bytes[byte_offset++] = b;
118119

119120
foreach (string dll in dlls)

Spore ModAPI Launcher/Program.cs

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)