/// <summary> /// Starts Factorio if the command line specifies to do so. /// </summary> /// <param name="commandLine">The programs command line.</param> /// <param name="createApp">Specifies whether an app should be created.</param> /// <returns>Returns true if the game was started, otherwise false.</returns> private static bool StartGameIfSpecified(CommandLine commandLine, bool createApp) { FactorioVersion factorioVersion; if (commandLine.TryGetArgument('n', "factorio-name", out string name)) { // Sets 'Application.Current' if (createApp) { CreateApp(commandLine); } if (!TryGetFactorioVersion(name, false, out factorioVersion)) { MessageBox.Show( $"A Factorio installation named '{name}' was not found.", "Error starting game!", MessageBoxButton.OK, MessageBoxImage.Error); return(true); } } else if (commandLine.TryGetArgument('f', "factorio-version", out string versionString)) { // Sets 'Application.Current' if (createApp) { CreateApp(commandLine); } if (!TryGetFactorioVersion(versionString, true, out factorioVersion)) { MessageBox.Show( $"A Factorio installation with version {versionString} was not found.", "Error starting game!", MessageBoxButton.OK, MessageBoxImage.Error); return(true); } } else { return(false); } if (factorioVersion == null) { return(true); } ActivateMods(commandLine); string args = BuildArguments(commandLine); factorioVersion.Run(args); return(true); }
private static void ActivateMods(CommandLine commandLine) { var mods = new ModCollection(); var modpacks = new ModpackCollection(); ModManager.BeginUpdateTemplates(); Mod.LoadMods(mods, modpacks); ModpackTemplateList.Instance.PopulateModpackList(mods, modpacks, null); mods.ForEach(mod => mod.Active = false); string modpackName; if (commandLine.TryGetArgument('p', "modpack", out modpackName)) { Modpack modpack = modpacks.Find(modpackName); if (modpack != null) { modpack.Active = true; } else { MessageBox.Show( $"No modpack named '{modpackName}' found.\nThe game will be launched without any mods enabled.", "Error loading modpack!", MessageBoxButton.OK, MessageBoxImage.Warning); } } ModManager.EndUpdateTemplates(true); ModManager.SaveTemplates(); }
private static string BuildArguments(CommandLine commandLine) { var sb = new StringBuilder(); if (commandLine.TryGetArgument('s', "savegame", out string savegameName)) { sb.Append($"--load-game \"{savegameName}\""); } if (commandLine.TryGetArgument('c', "commands", out string factorioCommandline)) { if (sb.Length > 0) { sb.Append(' '); } sb.Append(factorioCommandline.Replace('\'', '"')); } return(sb.ToString()); }
/// <summary> /// Creates the app as specified by the command line. /// </summary> /// <param name="commandLine">The programs command line.</param> private static App CreateApp(CommandLine commandLine) { // Do not create crash logs when debugging. bool createCrashLog = !commandLine.IsSet('l', "no-logs"); // Custom AppData path for debugging purposes only. string appDataPath; bool hasCustomAppDataPath = commandLine.TryGetArgument('a', "appdata-path", out appDataPath); if (hasCustomAppDataPath) { return(new App(createCrashLog, appDataPath)); } else { return(new App(createCrashLog)); } }
/// <summary> /// Creates the app as specified by the command line. /// </summary> /// <param name="commandLine">The programs command line.</param> private static App CreateApp(CommandLine commandLine) { // Do not create crash logs when debugging. bool createCrashLog = !commandLine.IsSet('l', "no-logs"); // Do not register fily type associations when debugging. bool registerFileTypes = !commandLine.IsSet('t', "no-register-filetype"); // Custom AppData path for debugging purposes only. string appDataPath; bool hasCustomAppDataPath = commandLine.TryGetArgument('a', "appdata-path", out appDataPath); if (hasCustomAppDataPath) { return(new App(createCrashLog, registerFileTypes, appDataPath)); } else { return(new App(createCrashLog, registerFileTypes)); } }
/// <summary> /// Starts Factorio if the command line specifies to do so. /// </summary> /// <param name="commandLine">The programs command line.</param> /// <param name="createApp">Specifies whether an app should be created.</param> /// <returns>Returns true if the game was started, otherwise false.</returns> private static bool StartGameIfSpecified(CommandLine commandLine, bool createApp) { string versionString; if (commandLine.TryGetArgument('f', "factorio-version", out versionString)) { // Variable not used but sets 'Application.Current'. App app = null; if (createApp) { app = CreateApp(commandLine); } var versions = FactorioVersion.GetInstalledVersions(); FactorioVersion steamVersion; if (FactorioSteamVersion.TryLoad(out steamVersion)) { versions.Add(steamVersion); } FactorioVersion factorioVersion = null; if (string.Equals(versionString, FactorioVersion.LatestKey, StringComparison.InvariantCultureIgnoreCase)) { factorioVersion = versions.MaxBy(item => item.Version, new VersionComparer()); } else { factorioVersion = versions.Find(item => string.Equals(item.VersionString, versionString, StringComparison.InvariantCultureIgnoreCase)); } if (factorioVersion != null) { var mods = new List <Mod>(); var modpacks = new List <Modpack>(); ModManager.BeginUpdateTemplates(); Mod.LoadMods(mods, modpacks); ModpackTemplateList.Instance.PopulateModpackList(mods, modpacks, null); mods.ForEach(mod => mod.Active = false); string modpackName; if (commandLine.TryGetArgument('p', "modpack", out modpackName)) { Modpack modpack = modpacks.FirstOrDefault(item => item.Name == modpackName); if (modpack != null) { modpack.Active = true; } else { MessageBox.Show( $"No modpack named '{modpackName}' found.\nThe game will be launched without any mods enabled.", "Error loading modpack!", MessageBoxButton.OK, MessageBoxImage.Warning); } } ModManager.EndUpdateTemplates(true); ModManager.SaveTemplates(); Process.Start(factorioVersion.ExecutablePath); } else { MessageBox.Show( $"Factorio version {versionString} is not available.\nCheck your installed Factorio versions.", "Error starting game!", MessageBoxButton.OK, MessageBoxImage.Error); } return(true); } return(false); }
public static int Main(string[] args) { var commandLine = new CommandLine(args, 'a', 'n', 'f', 'p', 's', 'c'); // Only display help. if (commandLine.IsSet('h', "help")) { Program.DisplayHelp(); return(0); } // Wait for updater to exit. if (commandLine.TryGetArgument(null, "update-complete", out string pidStr)) { if (int.TryParse(pidStr, out int pid)) { try { var updaterProcess = Process.GetProcessById(pid); updaterProcess.WaitForExit(); } catch { } } } string appGuid = Program.Guid.ToString(); string mutexId = $"{{{appGuid}}}"; bool createdNew; using (var mutex = new Mutex(false, mutexId, out createdNew)) { var hasHandle = false; try { try { hasHandle = mutex.WaitOne(100, false); if (!hasHandle) // App already running. { bool gameStarted = StartGameIfSpecified(commandLine, true); var sendArgs = args; if (gameStarted) { sendArgs = sendArgs.Append(NewInstanceGameStartedSpecifier).ToArray(); } SendNewInstanceStartedMessage(sendArgs); return(0); } } catch (AbandonedMutexException) { hasHandle = true; } // App not running. App app = CreateApp(commandLine); if (StartGameIfSpecified(commandLine, false)) { return(0); } var cancellationSource = new CancellationTokenSource(); Task listenTask = ListenForNewInstanceStarted(cancellationSource.Token); int result = Program.Run(commandLine, app); cancellationSource.Cancel(); listenTask.Wait(); return(result); } finally { if (hasHandle) { mutex.ReleaseMutex(); } } } }