public static bool IsInstalledDarkInjectionCompatible() { string di230 = "di_9_r_beta2-3-0".ToLowerInvariant(); try { bool returnValue = true; //InstalledMods mods = new InstalledMods(); //mods.Load(); MessageDisplay.DebugShowMessageBox("BEGIN MOD CONFIGURATION NAMES"); foreach (IInstalledMod configuration in ModsManager.InstalledMods) { MessageDisplay.DebugShowMessageBox(configuration.RealName); if (configuration.Unique.ToLowerInvariant().Contains(di230)) { returnValue = false; break; } } MessageDisplay.DebugShowMessageBox("END MOD CONFIGURATION NAMES"); if (!returnValue) { //TODO: Show error message SporeLauncher.OpenDarkInjectionPage(); } return(returnValue); } catch (Exception ex) { MessageDisplay.DebugShowMessageBox("FAILED TO INSPECT MOD CONFIGURATION NAMES\n" + ex); return(true); } }
/*public static void ShowError(Exception ex) * { * //MessageBox.Show(Strings.GalacticAdventuresNotExecuted + "\n" + ModApiHelpThreadURL + "\n\n" + ex.GetType() + "\n\n" + ex.Message + "\n\n" + ex.StackTrace, CommonStrings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); * if (ex is System.ComponentModel.Win32Exception) * { * var exc = ex as System.ComponentModel.Win32Exception; * //"NativeErrorCode: " + exc.NativeErrorCode.ToString() + "\n" + * MessageBox.Show("ErrorCode: " + exc.ErrorCode.ToString() + "\n" + * "NativeErrorCode: " + exc.NativeErrorCode.ToString() + "\n" + * exc.StackTrace * /* + * "HResult: " + exc.HResult.ToString() + "\n"/, "Additional Win32Exception Error Info"); * * if (exc.InnerException != null) * { * MessageBox.Show("ErrorCode: " + exc.InnerException.GetType() + "\n\n" + exc.InnerException.Message + "\n\n" + exc.InnerException.StackTrace, "Win32Exception InnerException Error Info"); * } * } * Process.Start(ModApiHelpThreadURL); * }*/ static void InjectDLLs(string dllEnding) { string coreDllInPath = CoreDllRetriever.GetStoredCoreDllPath(_executableType); string coreDllOutPath = CoreDllRetriever.GetInjectableCoreDllPath(_executableType); string libInPath = CoreDllRetriever.GetStoredCoreDllPath(_executableType, true); string libOutPath = CoreDllRetriever.GetInjectableCoreDllPath(_executableType, true); //Copy Core DLL and LIB File.Copy(coreDllInPath, coreDllOutPath, true); File.Copy(libInPath, libOutPath, true); Permissions.GrantAccessFile(coreDllOutPath); Permissions.GrantAccessFile(libOutPath); //Inject Core DLL Injector.InjectDLL(_processInfo, coreDllOutPath); //Inject Legacy DLL string legacyModApiDLLName = "SporeModAPI-" + dllEnding + ".dll"; string legacyModApiDLLPath = Path.Combine(Settings.LegacyLibsPath, legacyModApiDLLName); Injector.InjectDLL(_processInfo, legacyModApiDLLPath); CurrentError = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); //GetStoredCoreDllPath foreach (string s in Directory.EnumerateFiles(Settings.ModLibsPath).Where(x => (!Path.GetFileName(x).Equals(Path.GetFileName(coreDllOutPath), StringComparison.OrdinalIgnoreCase)) && x.ToLowerInvariant().EndsWith(".dll"))) { string debugData = "Now injecting: " + s; Console.WriteLine(debugData); MessageDisplay.DebugShowMessageBox(debugData); Injector.InjectDLL(_processInfo, s); } if (Directory.Exists(Settings.LegacyLibsPath)) { foreach (var file in Directory.EnumerateFiles(Settings.LegacyLibsPath).Where(x => x.EndsWith((dllEnding + ".dll").ToLowerInvariant())))//"*" + dllEnding + ".dll")) { string fileName = Path.GetFileName(file); MessageDisplay.DebugShowMessageBox("5.* Preparing " + fileName); // the ModAPI dll should already be loaded if (fileName.ToLowerInvariant() != legacyModApiDLLName.ToLowerInvariant()) { MessageDisplay.DebugShowMessageBox("5.* Injecting " + fileName); Injector.InjectDLL(_processInfo, file); CurrentError = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); } else { MessageDisplay.DebugShowMessageBox("5.* " + fileName + " was already injected!"); } } } }
private const uint MAXWAIT = 15000; //10000; public static void InjectDLL(PROCESS_INFORMATION pi, string dllPath) { Console.WriteLine("Injecting: " + dllPath); IntPtr retLib = NativeMethods.GetProcAddress(NativeMethods.GetModuleHandle("kernel32.dll"), "LoadLibraryA"); if (retLib == IntPtr.Zero) { throw new InjectException("LoadLibrary unreachable."); } /*IntPtr hProc; * if (Program.processHandle == IntPtr.Zero)*/ IntPtr hProc = NativeMethods.OpenProcess(NativeMethods.AccessRequired, false, pi.dwProcessId); //Open the process with all access if (hProc != IntPtr.Zero) {// Allocate memory to hold the path to the DLL file in the process' memory IntPtr objPtr = NativeMethods.VirtualAllocEx(hProc, IntPtr.Zero, (uint)dllPath.Length + 1, AllocationType.Commit, MemoryProtection.ReadWrite); if (objPtr == IntPtr.Zero) { int lastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); MessageDisplay.ShowMessageBox("Error: Virtual alloc failure: \n" + lastError.ToString() + "\n" + "hProc: " + hProc.ToString() + "\nProgram.processHandle: " + (SporeLauncher._processHandle == IntPtr.Zero)); throw new System.ComponentModel.Win32Exception(lastError); //throw new InjectException("Virtual alloc failure."); } //Write the path to the DLL file in the location just created var bytes = new byte[dllPath.Length + 1]; for (int i = 0; i < dllPath.Length; i++) { bytes[i] = (byte)dllPath[i]; } bytes[dllPath.Length] = 0; UIntPtr numBytesWritten; MessageDisplay.DebugShowMessageBox("Beginning WriteProcessMemory"); bool writeProcessMemoryOutput = NativeMethods.WriteProcessMemory(hProc, objPtr, bytes, (uint)bytes.Length, out numBytesWritten); if (!writeProcessMemoryOutput || numBytesWritten.ToUInt32() != bytes.Length) { SporeLauncher.ThrowWin32Exception("Write process memory failed."); } MessageDisplay.DebugShowMessageBox("WriteProcessMemory output: " + writeProcessMemoryOutput.ToString()); // Create a remote thread that begins at the LoadLibrary function and is passed as memory pointer IntPtr lpThreadId; IntPtr hRemoteThread = NativeMethods.CreateRemoteThread(hProc, IntPtr.Zero, 0, retLib, objPtr, 0, out lpThreadId); // Wait for the thread to finish if (hRemoteThread != IntPtr.Zero) { if (NativeMethods.WaitForSingleObject(hRemoteThread, MAXWAIT) == WAIT_TIMEOUT) { //throw new InjectException("Wait for single object failed."); SporeLauncher.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."); } } else { //throw new InjectException("Create remote thread failed."); int lastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); MessageDisplay.ShowMessageBox("Error: Create remote thread failed: \n" + lastError.ToString()); throw new System.ComponentModel.Win32Exception(lastError); } NativeMethods.VirtualFreeEx(hProc, objPtr, (uint)bytes.Length, AllocationType.Release); NativeMethods.CloseHandle(hProc); } else { SporeLauncher.ThrowWin32Exception("OpenProcess failed!"); } }
public static void LaunchGame() { try { _executablePath = Path.Combine(SporebinEP1, "SporeApp.exe"); if (File.Exists(_executablePath)) { //IEnumerable<int> rawExeSizes = Enum.GetValues(typeof(GameInfo.)).Cast<long>(); /*long[] exeSizes = new long[rawExeSizes.Length]; * //exeSizes.CopyTo(rawExeSizes, 0); * for (int i = 0; i < rawExeSizes.Length; i++) * exeSizes[i] = (long)rawExeSizes.GetValue(i);*/ bool exeSizeRecognized = ExecutableFileGameTypes.Keys.Contains(new FileInfo(_executablePath).Length); if (IsValidExe()) { try { _executableType = GetExecutableType(); } catch (Exception ex) { MessageDisplay.RaiseError(new ErrorEventArgs(ex)); //ex.Message + "\n\n" + ex.StackTrace return; } // Steam users need to do something different...something which doesn't even work most of the time. if (!SporeIsInstalledOnSteam()) { MessageDisplay.DebugShowMessageBox("2. Executable type: " + _executableType); if (_executableType == GameExecutableType.None) { // don't execute the game if the user closed the dialog return; } // get the correct executable path _executablePath = Path.Combine(SporebinEP1, ExecutableFileNames[_executableType]); if ((_executableType == GameExecutableType.Origin__1_5_1 || _executableType == GameExecutableType.Origin__March2017) && !File.Exists(_executablePath)) { var startInfo = new ProcessStartInfo( Path.Combine(Settings.ManagerInstallLocationPath, "Spore Mod Launcher.exe"), "--modapifix " + Directory.GetParent(_executablePath).FullName) { Verb = "runas" }; var p = Process.Start(startInfo); p.WaitForExit(); } string dllEnding = GetExecutableDllSuffix(_executableType); MessageDisplay.DebugShowMessageBox("4. DLL suffix: " + dllEnding); if (dllEnding == null) { MessageDisplay.DebugShowMessageBox(Settings.GetLanguageString(3, "NullDllSuffix")); //MessageBox.Show(Strings.VersionNotDetected, CommonStrings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } InjectNormalSporeProcess(dllEnding); } else { InjectSteamSporeProcess(); } } } int lastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); if ((CurrentError != 0) && (CurrentError != 18) && (CurrentError != 87) && (CurrentError != lastError)) { try { ThrowWin32Exception("Something went wrong", CurrentError); } catch (Exception ex) { MessageDisplay.RaiseError(new ErrorEventArgs(ex)); } } if ((lastError != 0) && (lastError != 18)) { ThrowWin32Exception("Something went wrong", lastError); } } catch (Exception ex) { MessageDisplay.RaiseError(new ErrorEventArgs(ex)); return; } }
static void CreateSporeProcess() { var sb = new StringBuilder(); if ((!Environment.GetCommandLineArgs().Contains(UpdaterService.IgnoreUpdatesArg)) && (Environment.GetCommandLineArgs().Length > 1) && (Environment.GetCommandLineArgs()[1] == Settings.LaunchSporeWithoutManagerOptions)) { int i = 0; foreach (string arg in Environment.GetCommandLineArgs()) { if ((i != 0) && (arg.ToLowerInvariant() != Settings.LaunchSporeWithoutManagerOptions.ToLowerInvariant())) { sb.Append(arg); sb.Append(" "); } i++; } } else { if (Settings.ForceGameWindowingMode) { if (Settings.ForceWindowedMode == 1) { sb.Append("-f"); } else// if (Settings.ForceWindowedMode == 0) { sb.Append("-w"); } sb.Append(" "); string size = "-r:"; var monitor = NativeMethods.AllMonitors[0]; if (Settings.ForceWindowedMode == 2) { size += (monitor.rcMonitor.Right - monitor.rcMonitor.Left).ToString() + "x" + (monitor.rcMonitor.Bottom - monitor.rcMonitor.Top).ToString(); } else if (Settings.ForceGameWindowBounds) { //MessageDisplay.DebugShowMessageBox("MONITOR: " + monitor.rcMonitor.Left + ", " + monitor.rcMonitor.Top + ", " + monitor.rcMonitor.Right + ", " + monitor.rcMonitor.Bottom + "\n" + +monitor.rcWork.Left + ", " + monitor.rcWork.Top + ", " + monitor.rcWork.Right + ", " + monitor.rcWork.Bottom); if (Settings.AutoGameWindowBounds) { MessageDisplay.DebugShowMessageBox("Settings.AutoGameWindowBounds is true"); if (Settings.ForceGameWindowingMode) { MessageDisplay.DebugShowMessageBox("Settings.ForceGameWindowingMode is true, Settings.ForceWindowedMode is " + Settings.ForceWindowedMode); if (Settings.ForceWindowedMode == 0) { size += (monitor.rcWork.Right - monitor.rcWork.Left); } else// if (Settings.ForceWindowedMode == 1) { size += (monitor.rcMonitor.Right - monitor.rcMonitor.Left); } /*else * size += Settings.ForcedGameWindowWidth;*/ } else { size += Settings.ForcedGameWindowWidth; } } else { size += Settings.ForcedGameWindowWidth; } size += "x"; if (Settings.AutoGameWindowBounds) { if (Settings.ForceGameWindowingMode) { int maximizedTitlebarHeight = CaptionHeight; if (Settings.ForceWindowedMode == 0) { size += ((monitor.rcWork.Bottom - monitor.rcWork.Top) - maximizedTitlebarHeight); } else if (Settings.ForceWindowedMode == 1) { size += ((monitor.rcMonitor.Bottom - monitor.rcMonitor.Top) - maximizedTitlebarHeight); } else { size += Settings.ForcedGameWindowHeight; } } else { size += Settings.ForcedGameWindowHeight; } } else { size += Settings.ForcedGameWindowHeight; } } else { MessageDisplay.DebugShowMessageBox("Settings.ForceGameWindowBounds is false!"); } sb.Append(size); sb.Append(" "); } if (Settings.ForceGameLocale && (!string.IsNullOrWhiteSpace(Settings.ForcedGameLocale))) { string option = "-locale:"; if (Settings.ForcedGameLocale.StartsWith("-")) { option += Settings.ForcedGameLocale.Substring(1); } else { option += Settings.ForcedGameLocale; } sb.Append(option); sb.Append(" "); } if (Settings.UseCustomGameState && (!string.IsNullOrWhiteSpace(Settings.GameState))) { sb.Append("-state:" + Settings.GameState); sb.Append(" "); } if (!string.IsNullOrWhiteSpace(Settings.CommandLineOptions)) { string[] additionalOptions; if (Settings.CommandLineOptions.Contains(" ")) { additionalOptions = Settings.CommandLineOptions.Split(" ".ToCharArray()); } else { additionalOptions = new string[] { Settings.CommandLineOptions } }; foreach (string arg in additionalOptions) { sb.Append(arg); sb.Append(" "); } } } /*string currentSporebinPath = string.Empty; * if (LauncherSettings.ForcedSporebinEP1Path != null) * currentSporebinPath = LauncherSettings.ForcedSporebinEP1Path; * else * currentSporebinPath = this.SporebinPath;*/ MessageDisplay.DebugShowMessageBox("SporebinPath: " + GameInfo.SporebinEP1 + "\nExecutablePath: " + _executablePath + "\nCommand Line Options: " + GetGameCommandLineOptions()); if (!NativeMethods.CreateProcess(null, "\"" + _executablePath + "\" " + sb.ToString(), IntPtr.Zero, IntPtr.Zero, false, ProcessCreationFlags.CREATE_SUSPENDED, IntPtr.Zero, GameInfo.SporebinEP1, ref _startupInfo, out _processInfo)) { //throw new InjectException(Strings.ProcessNotStarted); int lastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); MessageDisplay.ShowMessageBox(Settings.GetLanguageString(3, "SporeProcessCreationFailed") + lastError.ToString()); //Strings.ProcessNotStarted); throw new System.ComponentModel.Win32Exception(lastError); } }
static string GetGameCommandLineOptions() { var sb = new StringBuilder(); if ((!Environment.GetCommandLineArgs().Contains(UpdaterService.IgnoreUpdatesArg)) && (Environment.GetCommandLineArgs().Length > 1) && (Environment.GetCommandLineArgs()[1] == Settings.LaunchSporeWithoutManagerOptions)) { int i = 0; foreach (string arg in Environment.GetCommandLineArgs()) { if ((i != 0) && (arg.ToLowerInvariant() != Settings.LaunchSporeWithoutManagerOptions.ToLowerInvariant())) { sb.Append(arg); sb.Append(" "); } i++; } } else { if (Settings.ForceGameWindowingMode) { if (Settings.ForceWindowedMode == 1) { sb.Append("-f"); } else// if (Settings.ForceWindowedMode == 0) { sb.Append("-w"); } sb.Append(" "); string size = "-r:"; var monitor = NativeMethods.AllMonitors[0]; if (Settings.ForceWindowedMode == 2) { size += (monitor.rcMonitor.Right - monitor.rcMonitor.Left).ToString() + "x" + (monitor.rcMonitor.Bottom - monitor.rcMonitor.Top).ToString(); } else if (Settings.ForceGameWindowBounds) { //MessageDisplay.DebugShowMessageBox("MONITOR: " + monitor.rcMonitor.Left + ", " + monitor.rcMonitor.Top + ", " + monitor.rcMonitor.Right + ", " + monitor.rcMonitor.Bottom + "\n" + +monitor.rcWork.Left + ", " + monitor.rcWork.Top + ", " + monitor.rcWork.Right + ", " + monitor.rcWork.Bottom); if (Settings.AutoGameWindowBounds) { MessageDisplay.DebugShowMessageBox("Settings.AutoGameWindowBounds is true"); if (Settings.ForceGameWindowingMode) { MessageDisplay.DebugShowMessageBox("Settings.ForceGameWindowingMode is true, Settings.ForceWindowedMode is " + Settings.ForceWindowedMode); if (Settings.ForceWindowedMode == 0) { size += (monitor.rcWork.Right - monitor.rcWork.Left); } else// if (Settings.ForceWindowedMode == 1) { size += (monitor.rcMonitor.Right - monitor.rcMonitor.Left); } /*else * size += Settings.ForcedGameWindowWidth;*/ } else { size += Settings.ForcedGameWindowWidth; } } else { size += Settings.ForcedGameWindowWidth; } size += "x"; if (Settings.AutoGameWindowBounds) { if (Settings.ForceGameWindowingMode) { int maximizedTitlebarHeight = CaptionHeight; if (Settings.ForceWindowedMode == 0) { size += ((monitor.rcWork.Bottom - monitor.rcWork.Top) - maximizedTitlebarHeight); } else if (Settings.ForceWindowedMode == 1) { size += ((monitor.rcMonitor.Bottom - monitor.rcMonitor.Top) - maximizedTitlebarHeight); } else { size += Settings.ForcedGameWindowHeight; } } else { size += Settings.ForcedGameWindowHeight; } } else { size += Settings.ForcedGameWindowHeight; } } else { MessageDisplay.DebugShowMessageBox("Settings.ForceGameWindowBounds is false!"); } sb.Append(size); sb.Append(" "); } if (Settings.ForceGameLocale && (!string.IsNullOrWhiteSpace(Settings.ForcedGameLocale))) { string option = "-locale:"; if (Settings.ForcedGameLocale.StartsWith("-")) { option += Settings.ForcedGameLocale.Substring(1); } else { option += Settings.ForcedGameLocale; } sb.Append(option); sb.Append(" "); } if (Settings.UseCustomGameState && (!string.IsNullOrWhiteSpace(Settings.GameState))) { sb.Append("-state:" + Settings.GameState); sb.Append(" "); } if (!string.IsNullOrWhiteSpace(Settings.CommandLineOptions)) { string[] additionalOptions; if (Settings.CommandLineOptions.Contains(" ")) { additionalOptions = Settings.CommandLineOptions.Split(" ".ToCharArray()); } else { additionalOptions = new string[] { Settings.CommandLineOptions } }; foreach (string arg in additionalOptions) { sb.Append(arg); sb.Append(" "); } } } return(sb.ToString()); }