Skip to content

Commit

Permalink
chore: move app init from ctor to startup event
Browse files Browse the repository at this point in the history
Fixes a WPF bug regarding messageboxes in the ctor blocking the update loop, kinda dangerous
  • Loading branch information
goaaats committed Sep 17, 2022
1 parent e37571b commit 7f7d482
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 177 deletions.
356 changes: 182 additions & 174 deletions src/XIVLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public class CmdLineOptions

public static CmdLineOptions CommandLine { get; private set; }

private readonly FileInfo _dalamudRunnerOverride = null;
private FileInfo _dalamudRunnerOverride = null;
private MainWindow _mainWindow;

public static bool GlobalIsDisableAutologin { get; private set; }
Expand All @@ -101,169 +101,6 @@ public class CmdLineOptions
new(Color.FromArgb(0xFF, 0xFF, 0xd7, 0x00), 0.5f),
}, 0.7f);

public App()
{
// HW rendering commonly causes issues with material design, so we turn it off by default for now
try
{
if (!EnvironmentSettings.IsHardwareRendered)
RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
}
catch
{
// ignored
}

try
{
LogInit.Setup(
Path.Combine(Paths.RoamingPath, "output.log"),
Environment.GetCommandLineArgs());

Log.Information("========================================================");
Log.Information("Starting a session(v{Version} - {Hash})", AppUtil.GetAssemblyVersion(), AppUtil.GetGitHash());

#if !DEBUG
AppDomain.CurrentDomain.UnhandledException += EarlyInitExceptionHandler;
TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
#endif

SerilogEventSink.Instance.LogLine += OnSerilogLogLine;
}
catch (Exception ex)
{
MessageBox.Show("Could not set up logging. Please report this error.\n\n" + ex.Message, "XIVLauncher", MessageBoxButton.OK, MessageBoxImage.Error);
}

try
{
var helpWriter = new StringWriter();
var parser = new Parser(config => config.HelpWriter = helpWriter);
var result = parser.ParseArguments<CmdLineOptions>(Environment.GetCommandLineArgs());

if (result.Errors.Any())
{
MessageBox.Show(helpWriter.ToString(), "Help");
}

CommandLine = result.Value ?? new CmdLineOptions();

if (!string.IsNullOrEmpty(CommandLine.RoamingPath))
{
Paths.OverrideRoamingPath(CommandLine.RoamingPath);
}

if (!string.IsNullOrEmpty(CommandLine.RunnerOverride))
{
this._dalamudRunnerOverride = new FileInfo(CommandLine.RunnerOverride);
}

if (CommandLine.NoAutoLogin)
{
GlobalIsDisableAutologin = true;
}

if (!string.IsNullOrEmpty(CommandLine.DoGenerateIntegrity))
{
GenerateIntegrity(CommandLine.DoGenerateIntegrity);
}

if (CommandLine.DoGenerateLocalizables)
{
GenerateLocalizables();
}
}
catch (Exception ex)
{
MessageBox.Show("Could not parse command line arguments. Please report this error.\n\n" + ex.Message, "XIVLauncher", MessageBoxButton.OK, MessageBoxImage.Error);
}

try
{
SetupSettings();
}
catch (Exception e)
{
Log.Error(e, "Settings were corrupted, resetting");
File.Delete(GetConfigPath("launcher"));
SetupSettings();
}

#if !XL_LOC_FORCEFALLBACKS
try
{
if (App.Settings.LauncherLanguage == null)
{
var currentUiLang = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
App.Settings.LauncherLanguage = App.Settings.LauncherLanguage.GetLangFromTwoLetterIso(currentUiLang);
}

Log.Information("Trying to set up Loc for language code {0}", App.Settings.LauncherLanguage.GetLocalizationCode());

if (!App.Settings.LauncherLanguage.IsDefault())
{
Loc.Setup(AppUtil.GetFromResources($"XIVLauncher.Resources.Loc.xl.xl_{App.Settings.LauncherLanguage.GetLocalizationCode()}.json"));
}
else
{
Loc.SetupWithFallbacks();
}
}
catch (Exception ex)
{
Log.Error(ex, "Could not get language information. Setting up fallbacks.");
Loc.Setup("{}");
}
#else
// Force all fallbacks
Loc.Setup("{}");
#endif

try
{
Steam = new WindowsSteam();
}
catch (Exception ex)
{
Log.Error(ex, "Could not set up Steam");
}

#if !XL_NOAUTOUPDATE
if (!EnvironmentSettings.IsDisableUpdates)
{
try
{
Log.Information("Starting update check...");

_updateWindow = new UpdateLoadingDialog();
_updateWindow.Show();

var updateMgr = new Updates();
updateMgr.OnUpdateCheckFinished += OnUpdateCheckFinished;

ChangelogWindow changelogWindow = null;
try
{
changelogWindow = new ChangelogWindow(EnvironmentSettings.IsPreRelease);
}
catch (Exception ex)
{
Log.Error(ex, "Could not load changelog window");
}

Task.Run(() => updateMgr.Run(EnvironmentSettings.IsPreRelease, changelogWindow));
}
catch (Exception ex)
{
MessageBox.Show(
"XIVLauncher could not contact the update server. Please check your internet connection or try again.\n\n" + ex,
"XIVLauncher Error", MessageBoxButton.OK, MessageBoxImage.Error);
Environment.Exit(0);
}
}
#endif
}

private static void OnSerilogLogLine(object sender, (string Line, LogEventLevel Level, DateTimeOffset TimeStamp, Exception? Exception) e)
{
if (e.Exception == null)
Expand Down Expand Up @@ -434,23 +271,194 @@ private void EarlyInitExceptionHandler(object sender, UnhandledExceptionEventArg

private void App_OnStartup(object sender, StartupEventArgs e)
{
if (App.Settings.LauncherLanguage == LauncherLanguage.Russian)
// HW rendering commonly causes issues with material design, so we turn it off by default for now
try
{
var dict = new ResourceDictionary
if (!EnvironmentSettings.IsHardwareRendered)
RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
}
catch
{
// ignored
}

try
{
LogInit.Setup(
Path.Combine(Paths.RoamingPath, "output.log"),
Environment.GetCommandLineArgs());

Log.Information("========================================================");
Log.Information("Starting a session(v{Version} - {Hash})", AppUtil.GetAssemblyVersion(), AppUtil.GetGitHash());

#if !DEBUG
AppDomain.CurrentDomain.UnhandledException += EarlyInitExceptionHandler;
TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;
#endif

SerilogEventSink.Instance.LogLine += OnSerilogLogLine;
}
catch (Exception ex)
{
MessageBox.Show("Could not set up logging. Please report this error.\n\n" + ex.Message, "XIVLauncher", MessageBoxButton.OK, MessageBoxImage.Error);
}

try
{
var helpWriter = new StringWriter();
var parser = new Parser(config => config.HelpWriter = helpWriter);
var result = parser.ParseArguments<CmdLineOptions>(Environment.GetCommandLineArgs());

if (result.Errors.Any())
{
MessageBox.Show(helpWriter.ToString(), "Help");
}

CommandLine = result.Value ?? new CmdLineOptions();

if (!string.IsNullOrEmpty(CommandLine.RoamingPath))
{
Paths.OverrideRoamingPath(CommandLine.RoamingPath);
}

if (!string.IsNullOrEmpty(CommandLine.RunnerOverride))
{
{ "PrimaryHueLightBrush", UaBrush },
//{"PrimaryHueLightForegroundBrush", uaBrush},
{ "PrimaryHueMidBrush", UaBrush },
//{"PrimaryHueMidForegroundBrush", uaBrush},
{ "PrimaryHueDarkBrush", UaBrush },
//{"PrimaryHueDarkForegroundBrush", uaBrush},
};
this.Resources.MergedDictionaries.Add(dict);
this._dalamudRunnerOverride = new FileInfo(CommandLine.RunnerOverride);
}

if (CommandLine.NoAutoLogin)
{
GlobalIsDisableAutologin = true;
}

if (!string.IsNullOrEmpty(CommandLine.DoGenerateIntegrity))
{
GenerateIntegrity(CommandLine.DoGenerateIntegrity);
}

if (CommandLine.DoGenerateLocalizables)
{
GenerateLocalizables();
}
}
catch (Exception ex)
{
MessageBox.Show("Could not parse command line arguments. Please report this error.\n\n" + ex.Message, "XIVLauncher", MessageBoxButton.OK, MessageBoxImage.Error);
}

try
{
SetupSettings();
}
catch (Exception ex)
{
Log.Error(ex, "Settings were corrupted, resetting");
File.Delete(GetConfigPath("launcher"));
SetupSettings();
}

#if !XL_LOC_FORCEFALLBACKS
try
{
if (App.Settings.LauncherLanguage == null)
{
var currentUiLang = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
App.Settings.LauncherLanguage = App.Settings.LauncherLanguage.GetLangFromTwoLetterIso(currentUiLang);
}

Log.Information("Trying to set up Loc for language code {0}", App.Settings.LauncherLanguage.GetLocalizationCode());

if (!App.Settings.LauncherLanguage.IsDefault())
{
Loc.Setup(AppUtil.GetFromResources($"XIVLauncher.Resources.Loc.xl.xl_{App.Settings.LauncherLanguage.GetLocalizationCode()}.json"));
}
else
{
Loc.SetupWithFallbacks();
}
}
catch (Exception ex)
{
Log.Error(ex, "Could not get language information. Setting up fallbacks.");
Loc.Setup("{}");
}
#else
// Force all fallbacks
Loc.Setup("{}");
#endif

try
{
Steam = new WindowsSteam();
}
catch (Exception ex)
{
Log.Error(ex, "Could not set up Steam");
}

#if !XL_NOAUTOUPDATE
if (!EnvironmentSettings.IsDisableUpdates)
{
try
{
Log.Information("Starting update check...");

_updateWindow = new UpdateLoadingDialog();
_updateWindow.Show();

var updateMgr = new Updates();
updateMgr.OnUpdateCheckFinished += OnUpdateCheckFinished;

ChangelogWindow changelogWindow = null;

try
{
changelogWindow = new ChangelogWindow(EnvironmentSettings.IsPreRelease);
}
catch (Exception ex)
{
Log.Error(ex, "Could not load changelog window");
}

Task.Run(() => updateMgr.Run(EnvironmentSettings.IsPreRelease, changelogWindow));
}
catch (Exception ex)
{
Log.Error(ex, "Could not dispatch update check");
MessageBox.Show(
"XIVLauncher could not check for updates. Please check your internet connection or try again.\n\n" + ex,
"XIVLauncher Error", MessageBoxButton.OK, MessageBoxImage.Error);
Environment.Exit(0);
return;
}
}
#endif

try
{
if (App.Settings.LauncherLanguage == LauncherLanguage.Russian)
{
var dict = new ResourceDictionary
{
{ "PrimaryHueLightBrush", UaBrush },
//{"PrimaryHueLightForegroundBrush", uaBrush},
{ "PrimaryHueMidBrush", UaBrush },
//{"PrimaryHueMidForegroundBrush", uaBrush},
{ "PrimaryHueDarkBrush", UaBrush },
//{"PrimaryHueDarkForegroundBrush", uaBrush},
};
this.Resources.MergedDictionaries.Add(dict);
}
}
catch
{
// ignored
}

if (EnvironmentSettings.IsDisableUpdates)
{
OnUpdateCheckFinished(true);
return;
}

#if XL_NOAUTOUPDATE
Expand Down
6 changes: 3 additions & 3 deletions src/XIVLauncher/Updates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public async Task Run(bool downloadPrerelease, ChangelogWindow changelogWindow)
{
Log.Error(ex, "Update failed");
CustomMessageBox.Show(Loc.Localize("updatefailureerror", "XIVLauncher failed to check for updates. This may be caused by connectivity issues to GitHub. Wait a few minutes and try again.\nDisable your VPN, if you have one. You may also have to exclude XIVLauncher from your antivirus.\nIf this continues to fail after several minutes, please check out the FAQ."),
"XIVLauncher",
MessageBoxButton.OK,
MessageBoxImage.Error, showOfficialLauncher: true);
"XIVLauncher",
MessageBoxButton.OK,
MessageBoxImage.Error, showOfficialLauncher: true);
System.Environment.Exit(1);
}

Expand Down

0 comments on commit 7f7d482

Please sign in to comment.