-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMauiProgram.cs
More file actions
191 lines (175 loc) · 6.15 KB
/
MauiProgram.cs
File metadata and controls
191 lines (175 loc) · 6.15 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using Microsoft.Extensions.Logging;
using WorkshopUploader.Localization;
using WorkshopUploader.Services;
using WorkshopUploader.Steam;
namespace WorkshopUploader;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
AppFileLog.StartSession();
AppDomain.CurrentDomain.ProcessExit += (_, _) => AppFileLog.EndSession();
AppFileLog.Info("CreateMauiApp entry");
// #region agent log
DebugNdjsonSessionLog.Write("H1", "MauiProgram.CreateMauiApp", "entry", new
{
baseDir = AppContext.BaseDirectory,
tempLog = DebugNdjsonSessionLog.LogPath,
args = Environment.GetCommandLineArgs(),
});
// #endregion
S.ApplySavedCulture();
// #region agent log
AppDomain.CurrentDomain.UnhandledException += (_, e) =>
{
var ex = e.ExceptionObject as Exception;
AppFileLog.MarkCrash("AppDomain.UnhandledException", ex);
AppFileLog.Error($"UnhandledException (terminating={e.IsTerminating})", ex);
DebugSessionLog.Write("H1", "MauiProgram.UnhandledException", "unhandled", new
{
e.IsTerminating,
message = ex?.Message,
stack = ex?.StackTrace,
});
};
TaskScheduler.UnobservedTaskException += (_, e) =>
{
AppFileLog.MarkCrash("TaskScheduler.UnobservedTaskException", e.Exception);
AppFileLog.Error("UnobservedTaskException", e.Exception);
DebugSessionLog.Write("H1", "MauiProgram.UnobservedTaskException", "unobserved", new
{
e.Observed,
message = e.Exception?.Message,
stack = e.Exception?.StackTrace,
});
e.SetObserved();
};
// #endregion
try
{
// #region agent log
DebugSessionLog.Write("META", "MauiProgram.CreateMauiApp", "log_path", new { path = DebugSessionLog.LogFilePath });
DebugSessionLog.Write("H4", "MauiProgram.CreateMauiApp", "entry", new
{
baseDir = AppContext.BaseDirectory,
args = Environment.GetCommandLineArgs(),
});
// #endregion
try
{
try
{
var webViewDataDir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"GregToolsModmanager",
"webview2");
Directory.CreateDirectory(webViewDataDir);
Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", webViewDataDir);
AppFileLog.Info($"WEBVIEW2_USER_DATA_FOLDER={webViewDataDir}");
}
catch (Exception ex)
{
AppFileLog.Error("Failed to configure WebView2 user data folder.", ex);
}
var baseDir = AppContext.BaseDirectory;
if (!string.IsNullOrEmpty(baseDir))
{
if (IsDirectoryWritable(baseDir))
{
Directory.SetCurrentDirectory(baseDir);
AppFileLog.Info($"CurrentDirectory set to: {baseDir}");
}
else
{
AppFileLog.Warn($"Skip SetCurrentDirectory for non-writable path: {baseDir}");
}
}
}
catch
{
// ignored — Steam init may still work if cwd is already correct
}
var steamOk = SteamApiNativeLoader.TryPreload();
AppFileLog.Info($"SteamApiNativeLoader.TryPreload={steamOk}");
// #region agent log
DebugNdjsonSessionLog.Write("H4", "MauiProgram.CreateMauiApp", "after_steam_preload", new { steamOk });
// #endregion
if (HeadlessRunner.TryHandle(Environment.GetCommandLineArgs(), out var exitCode))
{
// #region agent log
DebugNdjsonSessionLog.Write("H3", "MauiProgram.CreateMauiApp", "headless_exit", new { exitCode });
DebugSessionLog.Write("H4", "MauiProgram.CreateMauiApp", "headless_exit", new { exitCode });
// #endregion
Environment.Exit(exitCode);
throw new InvalidOperationException("Unreachable: process should have exited.");
}
// #region agent log
DebugSessionLog.Write("H4", "MauiProgram.CreateMauiApp", "gui_path", new { message = "not headless" });
// #endregion
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>();
builder.Services.AddSingleton<AppLogService>();
builder.Services.AddSingleton<ReproBundleService>();
builder.Services.AddSingleton<SteamWorkshopService>();
builder.Services.AddSingleton<WorkspaceService>();
builder.Services.AddSingleton<ModDependencyService>();
builder.Services.AddSingleton<FfmPluginChannelRegistry>(sp =>
{
var registry = new FfmPluginChannelRegistry();
registry.Register(new StablePluginSource(sp.GetRequiredService<ModDependencyService>()));
registry.Register(new BetaPluginSource());
return registry;
});
builder.Services.AddSingleton<RalphSyncService>();
builder.Services.AddSingleton<VdfGeneratorService>();
builder.Services.AddSingleton<ProjectsPage>();
builder.Services.AddSingleton<NewProjectPage>();
builder.Services.AddSingleton<MyUploadsPage>();
builder.Services.AddSingleton<ModManagerPage>();
builder.Services.AddSingleton<SettingsPage>();
builder.Services.AddTransient<EditorPage>();
builder.Services.AddTransient<NativeConfigEditorPage>(sp =>
new NativeConfigEditorPage(sp.GetRequiredService<WorkspaceService>()));
builder.Services.AddTransient<ItemDetailPage>();
builder.Services.AddSingleton<AppShell>();
#if DEBUG
builder.Logging.AddDebug();
#endif
// #region agent log
DebugNdjsonSessionLog.Write("H1", "MauiProgram.CreateMauiApp", "before_build", null);
DebugSessionLog.Write("H1", "MauiProgram.CreateMauiApp", "before_build", null);
// #endregion
var app = builder.Build();
AppFileLog.Info("CreateMauiApp success");
// #region agent log
DebugNdjsonSessionLog.Write("H1", "MauiProgram.CreateMauiApp", "after_build", new { ok = true });
DebugSessionLog.Write("H1", "MauiProgram.CreateMauiApp", "after_build", null);
// #endregion
return app;
}
catch (Exception ex)
{
AppFileLog.Error("CreateMauiApp exception", ex);
// #region agent log
DebugNdjsonSessionLog.Write("H1", "MauiProgram.CreateMauiApp", "exception", new { ex.Message, exType = ex.GetType().FullName, ex.StackTrace });
DebugSessionLog.Write("H1", "MauiProgram.CreateMauiApp", "exception", new { ex.Message, ex.StackTrace });
// #endregion
throw;
}
}
private static bool IsDirectoryWritable(string directoryPath)
{
try
{
var testFile = Path.Combine(directoryPath, $".write-test-{Environment.ProcessId}.tmp");
File.WriteAllText(testFile, "ok");
File.Delete(testFile);
return true;
}
catch
{
return false;
}
}
}