diff --git a/HeliosPlus/GameLibraries/UplayLibrary.cs b/HeliosPlus/GameLibraries/UplayLibrary.cs index 28bc461..44905a9 100644 --- a/HeliosPlus/GameLibraries/UplayLibrary.cs +++ b/HeliosPlus/GameLibraries/UplayLibrary.cs @@ -304,7 +304,8 @@ namespace HeliosPlus.GameLibraries continue; //Split the record into entrylines - List uplayEntryLines = uplayEntry.Split('\n').ToList(); + string[] delimeters = { "\r\n" }; + List uplayEntryLines = uplayEntry.Split(delimeters, System.StringSplitOptions.RemoveEmptyEntries).ToList(); // Skip any records NOT containing an entryline with ' start_game:' (note 2 leading spaces) // All games contain a start_game entry @@ -320,7 +321,7 @@ namespace HeliosPlus.GameLibraries // Yay us :). // First we want to know the index of the start_game entry to use later - int startGameIndex = uplayEntryLines.FindIndex(a => a == " start_game:"); + int startGameIndex = uplayEntryLines.FindIndex(a => a.StartsWith(" start_game:")); MatchCollection mc; // First we check if there are any localization CONSTANTS that we will need to map later. @@ -396,14 +397,14 @@ namespace HeliosPlus.GameLibraries break; // This line contains the filename - if (uplayEntryLines[i].StartsWith(" relative:") && !gotGameFileName) + if (uplayEntryLines[i].StartsWith(" relative:") && !gotGameFileName) { - mc = Regex.Matches(uplayEntryLines[i], @" relative: (.*)"); + mc = Regex.Matches(uplayEntryLines[i], @"relative: (.*)"); gameFileName = mc[0].Groups[1].ToString(); gotGameFileName = true; } // This line contains the registryKey - if (uplayEntryLines[i].StartsWith(" register: HKEY_LOCAL_MACHINE") && !gotGameId) + if (uplayEntryLines[i].StartsWith(" register: HKEY_LOCAL_MACHINE") && !gotGameId) { // Lookup the GameId within the registry key mc = Regex.Matches(uplayEntryLines[i], @"Installs\\(\d+)\\InstallDir"); diff --git a/HeliosPlus/ShortcutRepository.cs b/HeliosPlus/ShortcutRepository.cs index 0b72c7e..b0be982 100644 --- a/HeliosPlus/ShortcutRepository.cs +++ b/HeliosPlus/ShortcutRepository.cs @@ -617,6 +617,7 @@ namespace HeliosPlus // If the game is a Steam Game we check for that if (shortcutToUse.GameLibrary.Equals(SupportedGameLibrary.Steam)) { + // We now need to get the SteamGame info SteamGame steamGameToRun = SteamLibrary.GetSteamGame(shortcutToUse.GameAppId); @@ -676,7 +677,7 @@ namespace HeliosPlus // ignored }*/ - + // Wait 300ms for the game process to spawn Thread.Sleep(300); // Now check it's actually started @@ -709,17 +710,104 @@ namespace HeliosPlus } // If the game is a Uplay Game we check for that - /*else if (GameLibrary.Equals(SupportedGameLibrary.Uplay)) + else if (shortcutToUse.GameLibrary.Equals(SupportedGameLibrary.Uplay)) { - // We need to look up details about the game - if (!UplayGame.IsInstalled(GameAppId)) + // We now need to get the SteamGame info + UplayGame uplayGameToRun = UplayLibrary.GetUplayGame(shortcutToUse.GameAppId); + + // If the GameAppID matches a Steam game, then lets run it + if (uplayGameToRun is UplayGame) { - return (false, string.Format("The Uplay Game with AppID '{0}' is not installed on this computer.", GameAppId)); + // Prepare to start the steam game using the URI interface + // as used by Steam for it's own desktop shortcuts. + var address = $"uplay://launch/{uplayGameToRun.Id}"; + if (shortcutToUse.GameArgumentsRequired) + { + address += "/" + shortcutToUse.GameArguments; + } + else + { + address += "/0"; + } + + // Start the URI Handler to run Uplay + Console.WriteLine($"Starting Steam Game: {uplayGameToRun.Name}"); + var uplayProcess = Process.Start(address); + + // Wait for Steam game to update if needed + var ticks = 0; + while (ticks < shortcutToUse.StartTimeout * 1000) + { + if (uplayGameToRun.IsRunning) + { + break; + } + + Thread.Sleep(300); + + if (!uplayGameToRun.IsUpdating) + { + ticks += 300; + } + } + + // Store the Steam Process ID for later + IPCService.GetInstance().HoldProcessId = uplayProcess?.Id ?? 0; + IPCService.GetInstance().Status = InstanceStatus.OnHold; + + /*// Add a status notification icon in the status area + NotifyIcon notify = null; + try + { + notify = new NotifyIcon + { + Icon = Properties.Resources.HeliosPlus, + Text = string.Format( + Language.Waiting_for_the_0_to_terminate, + steamGameToRun.GameName), + Visible = true + }; + Application.DoEvents(); + } + catch (Exception ex) + { + Console.WriteLine($"Program/SwitchToSteamGame exception: {ex.Message}: {ex.StackTrace} - {ex.InnerException}"); + // ignored + }*/ + + + // Wait 300ms for the game process to spawn + Thread.Sleep(300); + // Now check it's actually started + if (uplayGameToRun.IsRunning) + { + // Wait for the game to exit + Console.WriteLine($"Waiting for {uplayGameToRun.Name} to exit."); + while (true) + { + if (!uplayGameToRun.IsRunning) + { + break; + } + + Thread.Sleep(300); + } + Console.WriteLine($"{uplayGameToRun.Name} has exited."); + } + + // Remove the status notification icon from the status area + // once we've exited the game + /*if (notify != null) + { + notify.Visible = false; + notify.Dispose(); + Application.DoEvents(); + }*/ + } - }*/ - + } } // Stop the pre-started startPrograms that we'd started earlier