-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMauiProgram.cs
More file actions
152 lines (132 loc) · 5.51 KB
/
Copy pathMauiProgram.cs
File metadata and controls
152 lines (132 loc) · 5.51 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
using MDTadusMod.Data;
using MDTadusMod.Services;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Components.WebView.Maui;
using System.Net.Http;
using System.Text;
using System.Diagnostics;
#if WINDOWS
using Microsoft.Win32;
#endif
namespace MDTadusMod
{
public interface IAppPaths
{
string DataDir { get; }
string Combine(params string[] parts);
}
public sealed class AppPaths : IAppPaths
{
public string DataDir { get; }
public AppPaths(string dataDir)
{
DataDir = dataDir;
Directory.CreateDirectory(DataDir);
}
public string Combine(params string[] parts) =>
Path.Combine(new[] { DataDir }.Concat(parts).ToArray());
public static string Resolve()
{
#if WINDOWS
var reg = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Muledump.NET", "DataDir", null) as string;
var dir = string.IsNullOrWhiteSpace(reg)
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Muledump.NET")
: reg;
return dir;
#else
return Microsoft.Maui.Storage.FileSystem.AppDataDirectory;
#endif
}
}
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var dataDir = AppPaths.Resolve();
#if WINDOWS
var wv2 = Path.Combine(dataDir, "WebView2");
Directory.CreateDirectory(wv2);
Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", wv2, EnvironmentVariableTarget.Process);
#endif
var appPaths = new AppPaths(dataDir);
var builder = MauiApp.CreateBuilder();
builder.Services.AddSingleton<IAppPaths>(appPaths);
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts => fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"));
builder.Services.AddMauiBlazorWebView();
#if DEBUG
builder.Services.AddBlazorWebViewDeveloperTools();
#endif
// Core services
builder.Services.AddSingleton<AccountService>();
builder.Services.AddSingleton<RotmgApiService>();
builder.Services.AddSingleton<AssetService>();
builder.Services.AddSingleton<SettingsService>();
builder.Services.AddSingleton<UpdaterService>();
builder.Services.AddSingleton<ReloadQueueService>();
builder.Services.AddSingleton<AccountImportService>();
builder.Services.AddHttpClient();
// Logging buffer first
builder.Services.AddSingleton<ILogBuffer, LogBuffer>();
// Register our custom provider (picked up automatically)
builder.Services.AddSingleton<ILoggerProvider, MDTadusMod.Services.BufferLoggerProvider>();
// Ensure the in-memory buffer captures everything (even in Release)
builder.Logging.AddFilter<MDTadusMod.Services.BufferLoggerProvider>(null, LogLevel.Debug);
// Keep other providers at your desired levels
builder.Logging.AddFilter("System.Net.Http.HttpClient", LogLevel.Warning);
#if DEBUG
builder.Logging
.AddDebug()
#if WINDOWS
// Compact timestamp in Output window: 034337 (HHmmss)
.AddSimpleConsole(o =>
{
o.TimestampFormat = "HHmmss ";
o.UseUtcTimestamp = false; // set true if you prefer UTC
o.SingleLine = true;
})
#endif
.SetMinimumLevel(LogLevel.Debug);
#else
builder.Logging
.SetMinimumLevel(LogLevel.Information);
#endif
// HTTP logging handler (optional – currently not used by RotmgApiService)
builder.Services.AddTransient<LoggingHttpMessageHandler>();
builder.Services.AddHttpClient<IApiClient, ApiClient>(client =>
{
client.BaseAddress = new Uri("https://api.example.com/");
client.Timeout = TimeSpan.FromSeconds(30);
}).AddHttpMessageHandler<LoggingHttpMessageHandler>();
RegisterGlobalExceptionHooks(builder.Services);
var app = builder.Build();
// Add Debug/Trace -> buffer listener (after DI ready)
var logBuffer = app.Services.GetRequiredService<ILogBuffer>();
var forwarding = new MDTadusMod.Services.DebugForwardingTraceListener(logBuffer);
System.Diagnostics.Trace.Listeners.Add(forwarding);
return app;
}
private static void RegisterGlobalExceptionHooks(IServiceCollection services)
{
#if DEBUG
// Prevent crash on background exceptions in debug (already present)
TaskScheduler.UnobservedTaskException += (s, e) => { e.SetObserved(); };
AppDomain.CurrentDomain.UnhandledException += (s, e) => { };
#endif
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
{
if (services.BuildServiceProvider().GetService<ILogBuffer>() is { } lb)
lb.LogCritical("AppDomain.UnhandledException", e.ExceptionObject as Exception);
};
TaskScheduler.UnobservedTaskException += (s, e) =>
{
if (services.BuildServiceProvider().GetService<ILogBuffer>() is { } lb)
{
lb.LogError("TaskScheduler.UnobservedTaskException", e.Exception);
e.SetObserved();
}
};
}
}
}