Skip to content

Commit

Permalink
Small refactor on HomePage.StartGame
Browse files Browse the repository at this point in the history
- Avoid multiple call on the same parameter
- Avoid unnecessary call on unused parameter
- Use async overload when disposing ResizableWindowHookToken
- Avoid NRE if WatchOutputLog is unexpectedly cancelled before the time
- Suppress some NRE
  • Loading branch information
bagusnl committed Mar 9, 2024
1 parent 840351f commit 42ad076
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,22 +1224,26 @@ private void CancelInstallationDownload()
#endregion

#region Game Start/Stop Method

CancellationTokenSource WatchOutputLog = new CancellationTokenSource();
CancellationTokenSource WatchOutputLog = new();
CancellationTokenSource ResizableWindowHookToken;
private async void StartGame(object sender, RoutedEventArgs e)
{
// Initialize values
IGameSettingsUniversal _Settings = CurrentGameProperty!._GameSettings!.AsIGameSettingsUniversal();
PresetConfigV2 _gamePreset = CurrentGameProperty!._GameVersion!.GamePreset!;

var isGenshin = CurrentGameProperty!._GameVersion.GameType == GameType.Genshin;
var giForceHDR = false;

try
{
bool IsContinue = await CheckMediaPackInstalled();
if (!IsContinue) return;
if (!await CheckMediaPackInstalled()) return;

if (CurrentGameProperty!._GameVersion.GameType == GameType.Genshin && GetAppConfigValue("ForceGIHDREnable").ToBool())
GenshinHDREnforcer();
if (isGenshin)
{
giForceHDR = GetAppConfigValue("ForceGIHDREnable").ToBool();
if (giForceHDR) GenshinHDREnforcer();
}

if (_Settings!.SettingsCollapseMisc != null &&
_Settings.SettingsCollapseMisc.UseAdvancedGameSettings &&
Expand All @@ -1254,8 +1258,7 @@ private async void StartGame(object sender, RoutedEventArgs e)
{
LogWriteLine("[HomePage::StartGame()] Using alternative launch method!", LogType.Warning, true);
proc.StartInfo.WorkingDirectory = (CurrentGameProperty!._GameVersion.GamePreset!.ZoneName == "Bilibili" ||
(CurrentGameProperty._GameVersion.GameType == GameType.Genshin
&& GetAppConfigValue("ForceGIHDREnable").ToBool()) ? NormalizePath(GameDirPath) :
(isGenshin && giForceHDR) ? NormalizePath(GameDirPath) :
Path.GetDirectoryName(NormalizePath(GameDirPath))!)!;
}
else
Expand Down Expand Up @@ -1296,9 +1299,7 @@ private async void StartGame(object sender, RoutedEventArgs e)
}

StartPlaytimeCounter(proc, _gamePreset);

if (GetAppConfigValue("LowerCollapsePrioOnGameLaunch").ToBool())
CollapsePrioControl(proc);
if (GetAppConfigValue("LowerCollapsePrioOnGameLaunch").ToBool()) CollapsePrioControl(proc);

// Set game process priority to Above Normal when GameBoost is on
if (_Settings.SettingsCollapseMisc != null && _Settings.SettingsCollapseMisc.UseGameBoost) GameBoost_Invoke(CurrentGameProperty);
Expand All @@ -1313,6 +1314,8 @@ private async void StartGame(object sender, RoutedEventArgs e)
// Use this method to do something when game is closed
private async void GameRunningWatcher(IGameSettingsUniversal _settings)
{
ArgumentNullException.ThrowIfNull(_settings);

await Task.Delay(5000);
while (_cachedIsGameRunning)
{
Expand All @@ -1323,31 +1326,34 @@ private async void GameRunningWatcher(IGameSettingsUniversal _settings)

if (ResizableWindowHookToken != null)
{
ResizableWindowHookToken.Cancel();
await ResizableWindowHookToken.CancelAsync();
ResizableWindowHookToken.Dispose();
}

// Stopping GameLogWatcher
if (GetAppConfigValue("EnableConsole").ToBool())
WatchOutputLog.Cancel();
{
if (WatchOutputLog == null) return;
await WatchOutputLog.CancelAsync();
}

// Stop PreLaunchCommand process
if (_settings.SettingsCollapseMisc.GamePreLaunchExitOnGameStop) PreLaunchCommand_ForceClose();
if (_settings.SettingsCollapseMisc!.GamePreLaunchExitOnGameStop) PreLaunchCommand_ForceClose();

// Window manager on game closed
switch (GetAppConfigValue("GameLaunchedBehavior").ToString())
{
case "Minimize":
(m_window as MainWindow).Restore();
(m_window as MainWindow)?.Restore();
break;
case "ToTray":
H.NotifyIcon.WindowExtensions.Show(m_window);
(m_window as MainWindow).Restore();
H.NotifyIcon.WindowExtensions.Show(m_window!);
(m_window as MainWindow)?.Restore();
break;
case "Nothing":
break;
default:
(m_window as MainWindow).Restore();
(m_window as MainWindow)?.Restore();
break;
}

Expand Down

0 comments on commit 42ad076

Please sign in to comment.