-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
141 lines (120 loc) · 4.84 KB
/
Copy pathApp.xaml.cs
File metadata and controls
141 lines (120 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System.Windows;
using LSPDFRManager.Core;
using LSPDFRManager.Domain;
using LSPDFRManager.LocalApi;
using LSPDFRManager.Services;
using LSPDFRManager.ViewModels;
namespace LSPDFRManager;
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += (s, ex) =>
{
AppLogger.Error("[UNHANDLED_EXCEPTION]", (Exception)ex.ExceptionObject);
};
DispatcherUnhandledException += (s, ex) =>
{
AppLogger.Error("[UI_EXCEPTION]", ex.Exception);
ex.Handled = false;
};
// Route library mutations through the in-process service so the WPF in-memory
// collection stays consistent with API changes rather than both sides touching library.json.
LocalApiHost.SyncLibraryCallback = () => ModLibraryService.Instance.SyncWithDirectory();
LocalApiHost.SetEnabledCallback = (id, enabled) => ModLibraryService.Instance.SetEnabled(id, enabled);
LocalApiHost.UpdateNotesCallback = (id, notes) => ModLibraryService.Instance.UpdateNotes(id, notes);
// Start local API in-process (non-blocking; React UI nav waits on PortTask)
_ = Task.Run(async () =>
{
try { await LocalApiHost.StartAsync(); }
catch (Exception ex) { AppLogger.Error("[LOCALAPI] Failed to start", ex); }
});
try
{
base.OnStartup(e);
ValidateStartup();
var vm = new MainViewModel();
var window = new MainWindow(vm);
window.Show();
}
catch (Exception ex)
{
AppLogger.Error("[APP_STARTUP] Failed", ex);
throw;
}
}
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
_ = LocalApiHost.StopAsync();
}
private static void ValidateStartup()
{
var issues = new List<string>();
try
{
AppDataPaths.EnsureRootExists();
var probe = Path.Combine(AppDataPaths.Root, $".write_probe_{Guid.NewGuid():N}");
File.WriteAllText(probe, "");
File.Delete(probe);
}
catch (Exception ex)
{
issues.Add($"App data folder is not writable:\n {AppDataPaths.Root}\n ({ex.Message})");
}
var gtaPath = AppConfig.Instance.GtaPath;
var wizardWillRun = AppConfig.Instance.ShowSetupWizardOnStartup
|| string.IsNullOrWhiteSpace(gtaPath);
if (!wizardWillRun)
{
if (!Directory.Exists(gtaPath))
{
issues.Add($"GTA V installation folder not found:\n {gtaPath}\n Open Settings to set the correct path.");
}
else
{
if (LspdfrInstallLocator.FindGtaExe(gtaPath) is null)
issues.Add($"GTA V executable not found in:\n {gtaPath}\n Verify Settings points at the GTA V installation folder.");
var writeProbe = Path.Combine(gtaPath, ".lspdfrmanager_write_test");
try
{
File.WriteAllText(writeProbe, "");
File.Delete(writeProbe);
}
catch
{
issues.Add($"GTA V folder is not writable:\n {gtaPath}\n The app must run as Administrator to install mods into a protected directory.");
}
}
}
AddDiskSpaceIssueIfNeeded(issues, AppDataPaths.Root, "App data");
if (!string.IsNullOrWhiteSpace(gtaPath) && Directory.Exists(gtaPath))
AddDiskSpaceIssueIfNeeded(issues, gtaPath, "GTA V install");
if (issues.Count == 0)
return;
var message = string.Join("\n\n", issues) +
"\n\nThe app will open but some features may not work correctly.";
MessageBox.Show(message, "LSPDFR Manager — Startup Issues",
MessageBoxButton.OK, MessageBoxImage.Warning);
}
private static void AddDiskSpaceIssueIfNeeded(List<string> issues, string path, string label)
{
try
{
var root = Path.GetPathRoot(Path.GetFullPath(path));
if (string.IsNullOrWhiteSpace(root))
return;
var drive = new DriveInfo(root);
var requiredBytes = (long)AppConfig.Instance.MinimumFreeDiskSpaceMb * 1024 * 1024;
if (drive.AvailableFreeSpace < requiredBytes)
{
issues.Add(
$"{label} drive is low on free space:\n {root}\n Available: {drive.AvailableFreeSpace / 1024 / 1024:N0} MB; required: {AppConfig.Instance.MinimumFreeDiskSpaceMb:N0} MB.");
}
}
catch (Exception ex)
{
AppLogger.Warning($"Disk-space check failed for '{path}': {ex.Message}");
}
}
}