This commit is contained in:
rpa committed 2026-10-12 20:10:00 -04:00
commit 3b59cf7bf1
9 files changed
+409

No files matched your search

+138
View File
@@ -0,0 +1,138 @@
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;
using Amnesia.Services;
using Microsoft.Win32;
namespace Amnesia;
public partial class MainWindow : Window
{
private LocalStore? store;
private Preferences preferences = new();
private Account? current;
private bool register, busy;
private int failures;
private DateTime nextAttempt = DateTime.MinValue;
public MainWindow()
{
InitializeComponent();
Loaded += (_, _) => InitializeSession();
}
private void InitializeSession()
{
try
{
store = new LocalStore();
try { preferences = store.ReadPreferences(); }
catch { AuthError.Text = "Preferences could not be read; using defaults."; }
Opacity = preferences.Opacity;
var account = store.Restore();
if (account is not null) ShowHome(account, true);
else Username.Focus();
}
catch { AuthError.Text = "Unable to read local account data. Check access to %LOCALAPPDATA%\\Amnesia."; AuthButton.IsEnabled = false; }
}
private void Fade(FrameworkElement element)
{
element.BeginAnimation(OpacityProperty, null);
if (preferences.ReduceMotion) { element.Opacity = 1; return; }
element.BeginAnimation(OpacityProperty, new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(280)) { EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut } });
}
private void ToggleMode(object sender, RoutedEventArgs e)
{
if (busy) return;
register = !register;
AuthTitle.Text = register ? "Your adventure starts." : "Welcome back.";
AuthSubtitle.Text = register ? "Create a local account. Use a unique password." : "Sign in to your local launcher account.";
AuthButton.Content = register ? "Create account →" : "Sign in →";
ModeButton.Content = register ? "Already have an account? Sign in" : "New here? Create an account";
AuthError.Text = ""; Password.Clear(); Username.Focus();
}
private async void Authenticate(object sender, RoutedEventArgs e)
{
if (busy || store is null) return;
if (DateTime.UtcNow < nextAttempt) { AuthError.Text = "Please wait a few seconds before trying again."; return; }
string username = Username.Text.Trim(), password = Password.Password;
if (username.Length == 0 || password.Length == 0) { AuthError.Text = "Enter your username and password."; return; }
busy = true; AuthButton.IsEnabled = ModeButton.IsEnabled = false;
AuthError.Text = "";
try
{
bool creating = register;
var account = await Task.Run(() => creating ? store.Register(username, password) : store.Login(username, password));
bool remember = Remember.IsChecked == true;
string? notice = null;
try { if (remember) store.Remember(account); else store.Forget(); }
catch { notice = "Signed in, but session storage failed. Remember Me may not have been updated."; }
failures = 0; Password.Clear(); ShowHome(account, remember && notice is null);
if (notice is not null) MessageBox.Show(this, notice, "Session storage", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (ArgumentException ex)
{
AuthError.Text = ex.Message;
if (!register) { failures++; nextAttempt = DateTime.UtcNow.AddSeconds(Math.Min(failures * 2, 30)); }
}
catch { AuthError.Text = "Could not access account storage. Check %LOCALAPPDATA%\\Amnesia."; }
finally { busy = false; AuthButton.IsEnabled = ModeButton.IsEnabled = true; }
}
private void ShowHome(Account account, bool remembered)
{
current = account;
Greeting.Text = $"Welcome, {account.Username}.";
SessionLabel.Text = remembered ? "Remembered on this PC · 30 days" : "Signed in for this session";
LoginView.Visibility = Visibility.Collapsed; HomeView.Visibility = Visibility.Visible;
Fade(HomeView);
}
private void SignOut(object sender, RoutedEventArgs e)
{
try { store?.Forget(); }
catch { MessageBox.Show(this, "Could not clear the saved session. Check access to %LOCALAPPDATA%\\Amnesia before signing out.", "Sign out"); return; }
current = null; Password.Clear(); Remember.IsChecked = false; AuthError.Text = "";
if (register) ToggleMode(sender, e);
HomeView.Visibility = Visibility.Collapsed; LoginView.Visibility = Visibility.Visible;
Fade(LoginView); Username.Focus();
}
private void FillSettings(Preferences p)
{
LauncherPath.Text = p.LauncherPath; MinimizeOnLaunch.IsChecked = p.MinimizeOnLaunch;
ReduceMotion.IsChecked = p.ReduceMotion; OpacitySlider.Value = p.Opacity;
}
private void OpenSettings(object sender, RoutedEventArgs e)
{
FillSettings(preferences); SettingsError.Text = ""; HomeView.IsEnabled = false;
SettingsOverlay.Visibility = Visibility.Visible; Fade(SettingsOverlay); LauncherPath.Focus();
}
private void CancelSettings(object sender, RoutedEventArgs e) { SettingsOverlay.Visibility = Visibility.Collapsed; HomeView.IsEnabled = true; }
private void ResetSettings(object sender, RoutedEventArgs e) { FillSettings(new Preferences()); SettingsError.Text = "Defaults restored in this form. Save to apply."; }
private void Browse(object sender, RoutedEventArgs e)
{
var picker = new OpenFileDialog { Title = "Choose your launcher", Filter = "Windows applications (*.exe)|*.exe", CheckFileExists = true };
if (picker.ShowDialog(this) == true) LauncherPath.Text = picker.FileName;
}
private void SaveSettings(object sender, RoutedEventArgs e)
{
string path = LauncherPath.Text.Trim();
if (path.Length > 0 && (!File.Exists(path) || !path.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))) { SettingsError.Text = "Choose an existing .exe file, or reset the path."; return; }
var p = new Preferences { LauncherPath = path, MinimizeOnLaunch = MinimizeOnLaunch.IsChecked == true, ReduceMotion = ReduceMotion.IsChecked == true, Opacity = OpacitySlider.Value };
try { store!.SavePreferences(p); preferences = p; Opacity = p.Opacity; CancelSettings(sender, e); }
catch { SettingsError.Text = "Unable to save preferences. Check folder permissions."; }
}
private void Launch(object sender, RoutedEventArgs e)
{
if (current is null) return;
if (string.IsNullOrWhiteSpace(preferences.LauncherPath)) { OpenSettings(sender, e); SettingsError.Text = "Choose your launcher executable to get started."; return; }
try
{
if (!File.Exists(preferences.LauncherPath)) throw new FileNotFoundException();
Process.Start(new ProcessStartInfo(preferences.LauncherPath) { UseShellExecute = true, WorkingDirectory = Path.GetDirectoryName(preferences.LauncherPath)! });
if (preferences.MinimizeOnLaunch) WindowState = WindowState.Minimized;
}
catch { MessageBox.Show(this, "The launcher could not be opened. Check the executable selected in Settings.", "Unable to launch", MessageBoxButton.OK, MessageBoxImage.Information); }
}
private void PasswordKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) Authenticate(sender, e); }
private void DragTitle(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed && !busy) DragMove(); }
private void Minimize(object sender, RoutedEventArgs e) => WindowState = WindowState.Minimized;
private void CloseApp(object sender, RoutedEventArgs e) { if (!busy) Close(); }
}