Files
StarterNG/StarterNG/App.axaml.cs
maj00r 943b855f20 feat(warehouse): the old starter's magazyn.ini joins the warehouse at every start
The old starter read starter\magazyn.ini each time it opened and wrote it on
every change; this one kept its warehouse apart and took the file in only from
the list's menu, so a consist saved there never showed here.

- At the start, ahead of the scenery list, the consists of magazyn.ini not
  taken in before join the warehouse. Each one taken is remembered in
  userpresets.json by its name and vehicles, so one removed here stays removed
  while the old starter keeps it as it was.
- A consist changed there replaces the one here when that one is as an earlier
  start brought it; one of the same name made here stays, and the old one comes
  in beside it as "name (magazyn.ini)".
- A warehouse file that cannot be read is left alone by the import.
- The file is read as UTF-8 or code page 1250, whichever it is, by hand too.
2026-11-17 14:55:04 +01:00

228 lines
8.4 KiB
C#

using System;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using StarterNG.Application;
using StarterNG.Classes;
using StarterNG.Controls;
using StarterNG.Infrastructure;
using StarterNG.Services;
namespace StarterNG;
public partial class App : Avalonia.Application
{
/// <summary>
/// The translation table, as a static source for the XAML bindings. Code
/// should take <see cref="Application.Abstractions.ILocalizedStrings"/>
/// through its constructor rather than reading this.
/// </summary>
public static LocalizationService Loc => AppServices.Current.Localization;
public override void Initialize()
{
// Before any control exists: the names are given as controls are filled in.
ScreenReaderNames.Install();
DialogKeyboard.Install();
AvaloniaXamlLoader.Load(this);
AppServices.Current.SettingsStore.Load();
AppServices.Current.SettingsStore.StartAtHighWithoutOwnSettings();
KeyboardConfig.Instance.Load();
CultureInfo ci = CultureInfo.InstalledUICulture ;
var langName = AppServices.Current.Settings.LanguageWasSet
? AppServices.Current.Settings.Language
: ci.Name switch
{
"pl-PL" => "Polski",
_ => "English"
};
AppServices.Current.Settings.Language = langName;
// What eu07.ini asked for, before the starter picks a language it can
// actually display.
string requestedCode = AppServices.Current.Settings.LanguageCode;
// Subscribed before any view, so the views rebuild in the new culture.
Loc.LanguageChanged += () => ApplyCulture(Loc.CurrentLangCode);
Loc.Keep(requestedCode);
ApplyLanguage(langName);
// The starter ships no translation for this code, so its own interface
// falls back - but the simulator has one and keeps it. Overwriting the key
// here would silently move eu07.exe to English on the next save.
if (!string.IsNullOrWhiteSpace(requestedCode) &&
!string.Equals(requestedCode, Loc.CurrentLangCode, StringComparison.OrdinalIgnoreCase))
AppServices.Current.Settings.LanguageCode = requestedCode;
}
public static void ApplyLanguage(string langNameOrCode)
{
var resolvedName = Loc.Load(langNameOrCode);
// The service is the authority on which language actually loaded: the code
// goes to eu07.ini for the simulator, the name to the picker.
AppServices.Current.Settings.Language = resolvedName;
AppServices.Current.Settings.LanguageCode = Loc.CurrentLangCode;
}
/// <summary>
/// A language picked by the user, written to eu07.ini straight away as the old
/// starter did - not left waiting for the Save button of a different tab.
/// Only what is already in the settings goes with it, not other unsaved edits.
/// </summary>
public static void ChangeLanguage(string code)
{
if (string.Equals(code, Loc.CurrentLangCode, StringComparison.OrdinalIgnoreCase))
return;
ApplyLanguage(code);
AppServices.Current.SettingsStore.Save();
}
private static void ApplyCulture(string langCode)
{
CultureInfo culture;
try
{
culture = langCode switch
{
"" or "en" => CultureInfo.InvariantCulture,
"cz" => CultureInfo.GetCultureInfo("cs"),
_ => CultureInfo.GetCultureInfo(langCode)
};
}
catch (CultureNotFoundException)
{
culture = CultureInfo.InvariantCulture;
}
// Only the defaults: a culture set on the thread is an async local, so one set
// from the picker's handler was undone as soon as the handler returned, and
// the UI kept the first language's decimal comma and month names.
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
desktop.ShutdownRequested += (_, _) =>
{
try { AppServices.Current.SettingsStore.CaptureAndSave(); }
catch (Exception ex) { Diagnostics.Log("Settings save", ex); }
AppServices.Current.MissingVehicleLog.Dump();
try
{
if (KeyboardConfig.Instance.Dirty)
KeyboardConfig.Instance.Save();
}
catch (Exception ex) { Diagnostics.Log("Keyboard config save", ex); }
Diagnostics.Flush();
};
SplashWindow? splash = null;
bool mainReady = false;
var sw = System.Diagnostics.Stopwatch.StartNew();
Task.Run(async () =>
{
var progress = new Progress<LoadStatus>(status =>
{
Dispatcher.UIThread.Post(() =>
{
if (mainReady)
return;
if (sw.ElapsedMilliseconds > 300 && splash == null)
{
splash = new SplashWindow();
desktop.MainWindow = splash;
splash.Show();
}
splash?.Report(status);
});
});
// Ahead of the sceneries, so the list knows which still have their .sbt.
try { AppServices.Current.BinaryTerrain.CleanUpOnce(); }
catch (Exception ex) { Diagnostics.Log("sbt cleanup", ex); }
// Before any window lists the warehouse; it catches what it meets itself.
PresetStore.TakeFromOldStarter();
AppServices.Current.Library.Load(progress);
}).ContinueWith(load =>
{
Dispatcher.UIThread.Post(async () =>
{
MainWindow main;
try
{
main = new MainWindow();
}
catch (Exception ex)
{
Diagnostics.Log("Main window", ex);
Diagnostics.Flush();
Console.Error.WriteLine(ex);
mainReady = true;
splash?.Close();
desktop.Shutdown(1);
return;
}
mainReady = true;
desktop.MainWindow = main;
main.Show();
splash?.Close();
splash = null;
if (load.IsFaulted && load.Exception is { } error)
{
var inner = error.GetBaseException();
Diagnostics.Log("Game data load", inner);
await Diagnostics.ReportAsync(
$"{Loc["FaultLoadData"]}{Environment.NewLine}{Environment.NewLine}{Loc["FaultDetail"]} {inner.Message}");
}
var check = Diagnostics.InstallationCheck();
var faults = check.Run();
if (faults.Count > 0)
{
await Diagnostics.ReportAsync(
string.Join(Environment.NewLine, faults) + Environment.NewLine + Environment.NewLine +
Loc["FaultBadInstall"]);
}
if (check.ExecutableMissing())
await main.OfferExecutablePickAsync();
// A build without AVX2 in the folder is started instead, and needs no notice.
var services = AppServices.Current;
if (services.Settings.CpuNotice
&& services.Executables.Resolve(services.Settings, out _) is var executable
&& services.Executables.WillNotRunHere(executable))
await main.ShowCpuNoticeAsync(executable);
});
});
}
base.OnFrameworkInitializationCompleted();
}
}