Skip to content

Commit dcb56ba

Browse files
committed
Spore ModAPI Launcher: more cleanup
1 parent 1f9439c commit dcb56ba

File tree

4 files changed

+24
-80
lines changed

4 files changed

+24
-80
lines changed

Spore ModAPI Launcher/Injector.cs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Text;
43
using System.Threading;
54
using ModAPI.Common;
@@ -9,16 +8,16 @@ namespace SporeModAPI_Launcher
98
public class Injector
109
{
1110
private const int WAIT_TIMEOUT = 0x102;
12-
private const uint MAXWAIT = 15000; //10000;
13-
private const uint AccessRequired = 0x0002 | 0x0020 | 0x0008 | 0x0400 | 0x0010; //0x0002 | 0x0020 | 0x0008; //0xF0000 | 0x00100000 | 0xFFFF;
11+
private const uint MAXWAIT = 15000;
12+
private const uint AccessRequired = 0x0002 | 0x0020 | 0x0008 | 0x0400 | 0x0010;
1413

1514
public static IntPtr InjectDLL(NativeTypes.PROCESS_INFORMATION pi, string dllPath)
1615
{
1716
IntPtr retLib = NativeMethods.GetProcAddress(NativeMethods.GetModuleHandle("Kernel32.dll"), "LoadLibraryA");
1817

1918
if (retLib == IntPtr.Zero)
2019
{
21-
throw new InjectException("LoadLibrary unreachable.");
20+
throw new Exception("LoadLibrary unreachable.");
2221
}
2322

2423
IntPtr hProc = NativeMethods.OpenProcess(AccessRequired, false, pi.dwProcessId); //Open the process with all access
@@ -27,7 +26,7 @@ public static IntPtr InjectDLL(NativeTypes.PROCESS_INFORMATION pi, string dllPat
2726
IntPtr objPtr = NativeMethods.VirtualAllocEx(hProc, IntPtr.Zero, (uint)dllPath.Length + 1, NativeTypes.AllocationType.Commit, NativeTypes.MemoryProtection.ReadWrite);
2827
if (objPtr == IntPtr.Zero)
2928
{
30-
string additionalErrorText = "\n" + "hProc: " + hProc.ToString() + "\nProgram.processHandle: " + (Program.processHandle == IntPtr.Zero);
29+
string additionalErrorText = "\n" + "hProc: " + hProc.ToString();
3130
Program.ThrowWin32Exception("Virtual alloc failure.", additionalErrorText);
3231
}
3332

@@ -38,15 +37,14 @@ public static IntPtr InjectDLL(NativeTypes.PROCESS_INFORMATION pi, string dllPat
3837
}
3938
bytes[dllPath.Length] = 0;
4039

41-
UIntPtr numBytesWritten;
42-
bool writeProcessMemoryOutput = NativeMethods.WriteProcessMemory(hProc, objPtr, bytes, (uint)bytes.Length, out numBytesWritten);
40+
bool writeProcessMemoryOutput = NativeMethods.WriteProcessMemory(hProc, objPtr, bytes, (uint)bytes.Length, out UIntPtr numBytesWritten);
4341
if (!writeProcessMemoryOutput || numBytesWritten.ToUInt32() != bytes.Length)
4442
{
4543
Program.ThrowWin32Exception("Write process memory failed.");
4644
}
4745

4846
// Create a remote thread that begins at the LoadLibrary function and is passed as memory pointer
49-
IntPtr hRemoteThread = NativeMethods.CreateRemoteThread(hProc, IntPtr.Zero, 0, retLib, objPtr, 0, out var lpThreadId);
47+
IntPtr hRemoteThread = NativeMethods.CreateRemoteThread(hProc, IntPtr.Zero, 0, retLib, objPtr, 0, out _);
5048

5149
// Wait for the thread to finish
5250
uint thread_result;
@@ -84,7 +82,7 @@ public static void SetInjectionData(NativeTypes.PROCESS_INFORMATION pi, IntPtr h
8482

8583
if (SetInjectDataPtr == IntPtr.Zero)
8684
{
87-
string additionalErrorText = "\n" + "hDLLInjectorHandle: " + hDLLInjectorHandle.ToString() + "\nProgram.processHandle: " + (Program.processHandle == IntPtr.Zero);
85+
string additionalErrorText = "\n" + "hDLLInjectorHandle: " + hDLLInjectorHandle.ToString();
8886
Program.ThrowWin32Exception("Get proc address failure.", additionalErrorText);
8987
}
9088

@@ -106,7 +104,7 @@ public static void SetInjectionData(NativeTypes.PROCESS_INFORMATION pi, IntPtr h
106104
IntPtr objPtr = NativeMethods.VirtualAllocEx(hProc, IntPtr.Zero, (uint)total_alloc_size, NativeTypes.AllocationType.Commit, NativeTypes.MemoryProtection.ReadWrite);
107105
if (objPtr == IntPtr.Zero)
108106
{
109-
string additionalErrorText = "\n" + "hProc: " + hProc.ToString() + "\nProgram.processHandle: " + (Program.processHandle == IntPtr.Zero);
107+
string additionalErrorText = "\n" + "hProc: " + hProc.ToString();
110108
Program.ThrowWin32Exception("Virtual alloc failure.", additionalErrorText);
111109
}
112110

@@ -141,7 +139,7 @@ public static void SetInjectionData(NativeTypes.PROCESS_INFORMATION pi, IntPtr h
141139
{
142140
if (NativeMethods.WaitForSingleObject(hRemoteThread, MAXWAIT) == WAIT_TIMEOUT)
143141
{
144-
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.");
142+
Program.ThrowWin32Exception("Wait for single object failed.");
145143
}
146144
while (NativeMethods.GetExitCodeThread(hRemoteThread, out var thread_result))
147145
{
@@ -160,17 +158,4 @@ public static void SetInjectionData(NativeTypes.PROCESS_INFORMATION pi, IntPtr h
160158
NativeMethods.CloseHandle(hProc);
161159
}
162160
}
163-
164-
[Serializable()]
165-
public class InjectException : Exception
166-
{
167-
public InjectException() : base() { }
168-
public InjectException(string message) : base(message) { }
169-
public InjectException(string message, System.Exception inner) : base(message, inner) { }
170-
171-
// A constructor is needed for serialization when an
172-
// exception propagates from a remoting server to the client.
173-
protected InjectException(System.Runtime.Serialization.SerializationInfo info,
174-
System.Runtime.Serialization.StreamingContext context) { }
175-
}
176161
}

Spore ModAPI Launcher/Program.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ namespace SporeModAPI_Launcher
2020

2121
class Program
2222
{
23-
public static IntPtr processHandle = IntPtr.Zero;
24-
2523
private const string ModAPIFixDownloadURL = "https://davoonline.com/sporemodder/emd4600/SporeApp_ModAPIFix.zip";
2624
private const string ModApiHelpThreadURL = "https://launcherkit.sporecommunity.com/support";
2725

@@ -33,6 +31,8 @@ class Program
3331
private NativeTypes.STARTUPINFO StartupInfo;
3432
private NativeTypes.PROCESS_INFORMATION ProcessInfo;
3533

34+
private string LauncherKitPath;
35+
3636
/// <summary>
3737
/// The main entry point for the application.
3838
/// </summary>
@@ -71,6 +71,9 @@ void Execute()
7171
{
7272
try
7373
{
74+
// store the launcher kit path for later
75+
this.LauncherKitPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
76+
7477
// We try a new approach for Steam users.
7578
// Before, we used Steam to launch the game and tried to find the new process and inject it.
7679
// However, when the injection happens the game already executed a bit, so mods fail.
@@ -154,14 +157,7 @@ void Execute()
154157
}
155158
}
156159

157-
string dllEnding = GameVersion.GetVersionName(this.ExecutableType);
158-
if (dllEnding == null)
159-
{
160-
MessageBox.Show(Strings.VersionNotDetected, CommonStrings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
161-
return;
162-
}
163-
164-
InjectSporeProcess(dllEnding);
160+
InjectSporeProcess(GameVersion.GetVersionName(this.ExecutableType));
165161
}
166162
catch (Exception ex)
167163
{
@@ -177,9 +173,9 @@ public void ShowError(Exception ex)
177173
versionInfo += "\n\nSpore version: " + FileVersionInfo.GetVersionInfo(this.ExecutablePath).FileVersion + " - " + this.ExecutableType + "\nSpore path: " + this.ExecutablePath;
178174
}
179175
MessageBox.Show(Strings.GalacticAdventuresNotExecuted + "\n" + ModApiHelpThreadURL + "\n\n" + ex.GetType() + "\n\n" + ex.Message + "\n\n" + ex.StackTrace + "\n\n" + versionInfo, CommonStrings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
180-
if (ex is System.ComponentModel.Win32Exception)
176+
if (ex is Win32Exception)
181177
{
182-
var exc = ex as System.ComponentModel.Win32Exception;
178+
var exc = ex as Win32Exception;
183179
MessageBox.Show("ErrorCode: " + exc.ErrorCode + "\n" +
184180
"NativeErrorCode: " + exc.NativeErrorCode + "\n" +
185181
"HResult: " + exc.HResult + "\n", "Additional Win32Exception Error Info");
@@ -196,17 +192,16 @@ string[] GetDLLsToInject(string dllEnding)
196192
List<string> dlls = new List<string>();
197193

198194
//coreLibs and mLibs
199-
string basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
200-
string coreLibsPath = Path.Combine(basePath, "coreLibs");
201-
string mLibsPath = Path.Combine(basePath, "mLibs");
195+
string coreLibsPath = Path.Combine(this.LauncherKitPath, "coreLibs");
196+
string mLibsPath = Path.Combine(this.LauncherKitPath, "mLibs");
202197

203198
if (!Directory.Exists(mLibsPath))
204199
Directory.CreateDirectory(mLibsPath);
205200

206-
string coreLibFile = "SporeModAPI.lib";
201+
const string coreLibFile = "SporeModAPI.lib";
207202
string coreLibPath = Path.Combine(coreLibsPath, coreLibFile);
208203
string coreLegacyDllName = "SporeModAPI-" + dllEnding + ".dll";
209-
string coreLegacyDllPath = Path.Combine(basePath, coreLegacyDllName);
204+
string coreLegacyDllPath = Path.Combine(this.LauncherKitPath, coreLegacyDllName);
210205
string coreDllPath = Path.Combine(coreLibsPath, GameVersion.GetNewDLLName(this.ExecutableType));
211206
string coreDllOutPath = Path.Combine(mLibsPath, "SporeModAPI.dll");
212207

@@ -235,7 +230,7 @@ string[] GetDLLsToInject(string dllEnding)
235230
dlls.Add(file);
236231
}
237232

238-
foreach (string file in Directory.EnumerateFiles(basePath, "*" + dllEnding + ".dll")
233+
foreach (string file in Directory.EnumerateFiles(this.LauncherKitPath, "*" + dllEnding + ".dll")
239234
.Where(x => x.ToLowerInvariant() != coreLegacyDllPath.ToLowerInvariant()))
240235
{
241236
dlls.Add(file);
@@ -250,8 +245,8 @@ void InjectSporeProcess(string dllEnding)
250245

251246
try
252247
{
253-
const string MOD_API_DLL_INJECTOR = "ModAPI.DLLInjector.dll";
254-
IntPtr hDLLInjectorHandle = Injector.InjectDLL(this.ProcessInfo, Path.GetFullPath(MOD_API_DLL_INJECTOR));
248+
const string modApiDllInjector = "ModAPI.DLLInjector.dll";
249+
IntPtr hDLLInjectorHandle = Injector.InjectDLL(this.ProcessInfo, Path.Combine(this.LauncherKitPath, modApiDllInjector));
255250

256251
string[] dlls = GetDLLsToInject(dllEnding);
257252

Spore ModAPI Launcher/Strings.resx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@
120120
<data name="DownloadFixTitle" xml:space="preserve">
121121
<value>Downloading ModAPI Fix</value>
122122
</data>
123-
<data name="Downloading" xml:space="preserve">
124-
<value>Downloading</value>
125-
</data>
126123
<data name="DownloadOriginFix" xml:space="preserve">
127124
<value>EA App and Origin users need to download an additional file in order for mods to work. If you press OK, the launcher will download and apply this additional file.</value>
128125
</data>
@@ -132,18 +129,12 @@
132129
<data name="GalacticAdventuresNotExecuted" xml:space="preserve">
133130
<value>Something went wrong while starting or injecting your mods into Spore. The game may start, but some mods will not work correctly. Please report the issue, along with a screenshot of this error, using the instructions at the following website:</value>
134131
</data>
135-
<data name="MightNotWork" xml:space="preserve">
136-
<value>The Launcher Kit requires Galactic Adventures and one of the following three game versions: 3.0.0.2818 (disc only), 3.1.0.22, or 3.1.0.29. If your game crashes or mods do not work, download the latest version of Spore and Galactic Adventures. Disc users must instead manually install patch 1.5.1 after installing Galactic Adventures.</value>
137-
</data>
138132
<data name="ProcessNotResumed" xml:space="preserve">
139133
<value>Process thread could not be resumed.</value>
140134
</data>
141135
<data name="ProcessNotStarted" xml:space="preserve">
142136
<value>Could not start process.</value>
143137
</data>
144-
<data name="VersionNotDetected" xml:space="preserve">
145-
<value>Your version could not be detected, ModAPI won't load.</value>
146-
</data>
147138
<data name="CannotApplySteamFix" xml:space="preserve">
148139
<value>Could not apply the Steam fix. To apply it, you can do one of these two options:
149140
- Re-execute the Launcher with Administrator Privileges.

Spore ModAPI Launcher/Strings1.Designer.cs

Lines changed: 0 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)