Skip to content

Commit

Permalink
fix: initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
DorielRivalet committed Feb 13, 2024
1 parent 989532b commit d36eb2f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
18 changes: 17 additions & 1 deletion MHFZ_Overlay/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,28 @@ public partial class App : Application
/// <inheritdoc/>
protected override void OnStartup(StartupEventArgs e)
{
Logger.Info("Starting up App");
Logger.Info("Starting up App...");
this.DispatcherUnhandledException += App_DispatcherUnhandledException;

var loggingRules = NLog.LogManager.Configuration.LoggingRules;
var s = (Settings)App.Current.TryFindResource("Settings");
loggingRules[0].SetLoggingLevels(LoggingService.GetLogLevel(s.LogLevel), NLog.LogLevel.Fatal);
Logger.Info(CultureInfo.InvariantCulture, "Started WPF application");
Logger.Trace(CultureInfo.InvariantCulture, "Call stack: {0}", new StackTrace().ToString());
Logger.Debug(CultureInfo.InvariantCulture, "OS: {0}, is64BitOS: {1}, is64BitProcess: {2}, CLR version: {3}", Environment.OSVersion, Environment.Is64BitOperatingSystem, Environment.Is64BitProcess, Environment.Version);
SetRenderingMode(s.RenderingMode);

base.OnStartup(e);
}

private static void SetRenderingMode(string renderingMode)
{
RenderOptions.ProcessRenderMode = renderingMode == "Hardware"
? RenderMode.Default
: RenderMode.SoftwareOnly;
Logger.Info(CultureInfo.InvariantCulture, $"Rendering mode: {renderingMode}");
}

private static void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) =>

// Log/inspect the inspection here
Expand Down
1 change: 1 addition & 0 deletions MHFZ_Overlay/MHFZ_Overlay.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,7 @@
<PackageReference Include="MouseKeyHook" Version="5.7.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
<PackageReference Include="NuGet.CommandLine" Version="6.8.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
33 changes: 11 additions & 22 deletions MHFZ_Overlay/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ public static void Main(string[] args)
typeof(FrameworkElement),
new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.Name)));

var loggingRules = NLog.LogManager.Configuration.LoggingRules;
var s = (Settings)App.Current.TryFindResource("Settings");
loggingRules[0].SetLoggingLevels(LoggingService.GetLogLevel(s.LogLevel), NLog.LogLevel.Fatal);
Logger.Info(CultureInfo.InvariantCulture, "Started WPF application");
Logger.Trace(CultureInfo.InvariantCulture, "Call stack: {0}", new StackTrace().ToString());
Logger.Debug(CultureInfo.InvariantCulture, "OS: {0}, is64BitOS: {1}, is64BitProcess: {2}, CLR version: {3}", Environment.OSVersion, Environment.Is64BitOperatingSystem, Environment.Is64BitProcess, Environment.Version);

// TODO: test if this doesnt conflict with squirrel update
CurrentProgramVersion = $"v{GetAssemblyVersion}";
if (CurrentProgramVersion == "v0.0.0")
Expand All @@ -129,12 +122,14 @@ public static void Main(string[] args)
}
// Logging is essential for debugging! Ideally you should write it to a file.
// Log = new MemoryLogger();
var loggerFactory = new NLog.Extensions.Logging.NLogLoggerFactory();
var velopackLogger = loggerFactory.CreateLogger("VelopackLogger");

// It's important to Run() the VelopackApp as early as possible in app startup.
VelopackApp.Build()
.WithRestarted((v) => VelopackUpdatedAndRestarted(v))
.WithFirstRun((v) => VelopackFirstRun(v))
.Run((Microsoft.Extensions.Logging.ILogger?)Logger);
.Run(velopackLogger);

// ... other app init code after ...
RestoreSettings();
Expand All @@ -149,16 +144,18 @@ public static void Main(string[] args)
// .Where(x => x.Key == "WpfSampleReleaseDir")
// .Single().Value;

// We can now launch the WPF application as normal.
var app = new App();
app.InitializeComponent();
app.Run();
stopwatch.Stop();

// Get the elapsed time in milliseconds
var elapsedTimeMs = stopwatch.Elapsed.TotalMilliseconds;
// Print the elapsed time
Logger.Debug($"App ctor Elapsed Time: {elapsedTimeMs} ms");
SetRenderingMode(s.RenderingMode);
Logger.Debug($"Program initialization Elapsed Time: {elapsedTimeMs} ms");

// We can now launch the WPF application as normal.
var app = new App();
app.InitializeComponent();
app.Run();

}
catch (Exception ex)
{
Expand Down Expand Up @@ -224,14 +221,6 @@ public static async Task UpdateMyApp()
}
}

private static void SetRenderingMode(string renderingMode)
{
RenderOptions.ProcessRenderMode = renderingMode == "Hardware"
? RenderMode.Default
: RenderMode.SoftwareOnly;
Logger.Info(CultureInfo.InvariantCulture, $"Rendering mode: {renderingMode}");
}

// https://github.com/Squirrel/Squirrel.Windows/issues/198#issuecomment-299262613
// Indeed you can use the methods below to backup your settings,
// typically just after your update has completed,
Expand Down

0 comments on commit d36eb2f

Please sign in to comment.