-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
308 lines (264 loc) · 11.3 KB
/
Program.cs
File metadata and controls
308 lines (264 loc) · 11.3 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using LMP.Core.Data;
using LMP.Core.Data.Repositories;
using LMP.Core.Services;
using LMP.Features.Home;
using LMP.Features.Library;
using LMP.Features.Player;
using LMP.Features.Playlist;
using LMP.Features.Search;
using LMP.Features.Settings;
using LMP.Features.Shell;
using Avalonia.ReactiveUI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Avalonia;
using AsyncImageLoader;
using LMP.UI.Dialogs;
using LMP.Core.Audio.Cache;
using LMP.Core.Youtube.Bridge.NToken;
using LMP.Core.Audio.Http;
using LMP.Core.Youtube.Bridge.SigCipher;
using LMP.Core.Youtube.Bridge.Common;
using LMP.Features.Notifications;
using LMP.Core.Models;
namespace LMP;
class Program
{
public static IServiceProvider Services { get; private set; } = null!;
[STAThread]
public static void Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.InputEncoding = System.Text.Encoding.UTF8;
SetupGlobalExceptionHandlers();
try
{
// ═══ ЭТАП 1: Создать папки ПЕРВЫМ ═══
G.Folder.Create();
// ═══ ЭТАП 2: Логгер ═══
Log.Initialize();
Log.Info($"{G.AppId} starting...");
// ═══ ЭТАП 3: Bootstrap настройки (быстро, без БД) ═══
BootstrapSettings.Initialize();
// ═══ ЭТАП 4: DI контейнер ═══
var services = new ServiceCollection();
ConfigureServices(services);
Services = services.BuildServiceProvider();
// ═══ ЭТАП 5: КРИТИЧНО — Мигрировать БД ДО создания сервисов ═══
MigrateDatabaseSync();
// ═══ ЭТАП 6: Avalonia ═══
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}
catch (Exception ex)
{
Log.Fatal($"Global crash: {ex.Message}\n{ex.StackTrace}");
}
finally
{
// ═══ ГАРАНТИРОВАННЫЙ SHUTDOWN логгера ═══
Log.Shutdown();
}
}
/// <summary>
/// Синхронно выполняет миграцию БД ДО старта Avalonia и создания сервисов.
/// Гарантирует что NotificationService и LibraryService найдут готовую схему.
/// </summary>
private static void MigrateDatabaseSync()
{
try
{
var dbFactory = Services.GetRequiredService<IDbContextFactory<LibraryDbContext>>();
using var ctx = dbFactory.CreateDbContext();
ctx.Database.EnsureCreated();
ctx.MigrateSchemaAsync(CancellationToken.None).GetAwaiter().GetResult();
ctx.OptimizeAsync(CancellationToken.None).GetAwaiter().GetResult();
ctx.EnsureFtsTablesAsync(CancellationToken.None).GetAwaiter().GetResult();
Log.Info("[DB] Schema migration complete");
}
catch (Exception ex)
{
Log.Error($"[DB] Migration failed: {ex.Message}");
throw;
}
}
/// <summary>
/// Регистрирует глобальные обработчики исключений.
/// Должен вызываться ДО создания любых Task, HttpClient, etc.
/// </summary>
private static void SetupGlobalExceptionHandlers()
{
// 1. Unobserved Task Exceptions — подавляем ВСЕ, логируем когда можем.
// Это ловит exceptions из Task'ов которые не были await'нуты
// и собираются GC.
TaskScheduler.UnobservedTaskException += (_, e) =>
{
e.SetObserved(); // Предотвращает crash в любом случае
try
{
var msg = e.Exception?.InnerException?.Message
?? e.Exception?.Message
?? "unknown";
Log.Debug($"[UnobservedTask] Suppressed: {msg}");
}
catch
{
// Логгер ещё не инициализирован — молча подавляем
}
};
// 2. Unhandled Exceptions на уровне AppDomain —
// ловит исключения из IO completion threads, finalizer thread, etc.
// IsTerminating=true означает что CLR уже решил крашить процесс,
// но мы хотя бы залогируем.
AppDomain.CurrentDomain.UnhandledException += (_, e) =>
{
try
{
if (e.ExceptionObject is Exception ex)
{
// SSL/TLS ошибки из SocketsHttpHandler — не фатальные
if (IsSslRelatedException(ex))
{
Log.Warn($"[AppDomain] SSL/TLS exception suppressed: {ex.Message}");
return;
}
Log.Error($"[AppDomain] Unhandled: {ex.Message}", ex);
}
else
{
Log.Error($"[AppDomain] Unhandled non-exception: {e.ExceptionObject}");
}
}
catch
{
// Логгер не готов — ничего не можем сделать
}
};
}
/// <summary>
/// Проверяет, связано ли исключение с SSL/TLS (known .NET HTTP/2 issue).
/// </summary>
private static bool IsSslRelatedException(Exception ex)
{
var current = ex;
while (current != null)
{
var typeName = current.GetType().FullName ?? "";
var msg = current.Message ?? "";
if (typeName.Contains("SslStream", StringComparison.Ordinal) ||
typeName.Contains("Ssl", StringComparison.OrdinalIgnoreCase) ||
typeName.Contains("Tls", StringComparison.OrdinalIgnoreCase) ||
msg.Contains("SSL", StringComparison.OrdinalIgnoreCase) ||
msg.Contains("TLS", StringComparison.OrdinalIgnoreCase) ||
msg.Contains("secure channel", StringComparison.OrdinalIgnoreCase) ||
msg.Contains("authentication", StringComparison.OrdinalIgnoreCase) ||
msg.Contains("EnsureFullTlsFrame", StringComparison.Ordinal))
{
return true;
}
current = current.InnerException;
}
// Проверяем AggregateException
if (ex is AggregateException agg)
{
foreach (var inner in agg.InnerExceptions)
{
if (IsSslRelatedException(inner))
return true;
}
}
return false;
}
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
#if DEBUG
.LogToTrace()
#endif
.UseReactiveUI();
private static void ConfigureServices(IServiceCollection services)
{
Log.Info("Configuring services...");
// === Bootstrap Settings (уже загружены) ===
services.AddSingleton(_ => BootstrapSettings.Current);
// === Database ===
var dbPath = G.FilePath.Database;
services.AddDbContextFactory<LibraryDbContext>(options =>
{
options.UseSqlite($"Data Source={dbPath};Cache=Shared");
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
#if DEBUG
options.EnableSensitiveDataLogging();
options.EnableDetailedErrors();
#endif
});
// === Repositories ===
services.AddSingleton<ITrackRepository, TrackRepository>();
services.AddSingleton<IPlaylistRepository, PlaylistRepository>();
services.AddSingleton<ISettingsRepository, SettingsRepository>();
services.AddSingleton<INotificationRepository, NotificationRepository>();
// === Core Services ===
services.AddSingleton(sp =>
{
var trackRepo = sp.GetRequiredService<ITrackRepository>();
var playlistRepo = sp.GetRequiredService<IPlaylistRepository>();
return new TrackRegistry(trackRepo, playlistRepo);
});
services.AddSingleton<IAsyncImageLoader>(sp =>
new CachedImageLoader(sp.GetRequiredService<ImageCacheService>(), ImageQuality.Low));
services.AddSingleton<LibraryService>();
services.AddSingleton<ThemeManagerService>();
services.AddSingleton<CookieAuthService>();
services.AddSingleton<YoutubeProvider>();
services.AddSingleton<YoutubeUserDataService>();
services.AddSingleton<MusicLibraryManager>();
services.AddSingleton<DialogHostViewModel>();
services.AddSingleton(sp =>
{
var auth = sp.GetRequiredService<CookieAuthService>();
var notifications = sp.GetRequiredService<NotificationService>();
// Lazy accessor — избегаем циклической зависимости
// MainWindowViewModel создаётся позже, чем DialogService
DialogHostViewModel GetDialogHost()
{
var mainWindow = sp.GetRequiredService<MainWindowViewModel>();
return mainWindow.DialogHost;
}
return new DialogService(auth, notifications, GetDialogHost);
});
services.AddSingleton(_ => new PlayerContextManager(SharedHttpClient.Instance));
services.AddSingleton<SigCipherDecryptor>();
services.AddSingleton<NTokenDecryptor>();
services.AddSingleton<PlaylistSyncService>();
services.AddSingleton<PlaylistEditService>();
// === Caching ===
services.AddSingleton<SearchCacheService>();
services.AddSingleton<ImageCacheService>();
// === Audio & Downloads ====
services.AddSingleton<AudioCacheManager>();
services.AddSingleton<AudioEngine>();
services.AddSingleton<DownloadService>();
// === NOTIFICATION SYSTEM ===
services.AddSingleton<NotificationService>();
services.AddTransient<NotificationButtonViewModel>();
services.AddTransient<NotificationPanelViewModel>();
services.AddTransient<ToastOverlayViewModel>();
// === ERROR ORCHESTRATOR ===
services.AddSingleton<PlaybackErrorOrchestrator>();
// === Other Services ===
services.AddSingleton<DominantColorService>();
services.AddSingleton<PlayerControlService>();
// === ViewModels ===
services.AddTransient<HomeViewModel>();
services.AddTransient<SearchViewModel>();
services.AddTransient<LibraryViewModel>();
services.AddTransient<QueueViewModel>();
services.AddTransient<SettingsViewModel>();
services.AddTransient<PlaylistViewModel>();
services.AddTransient<SyncSelectionViewModel>();
services.AddSingleton<TrackViewModelFactory>();
services.AddSingleton<MainWindowViewModel>();
services.AddSingleton<PlayerBarViewModel>();
Log.Info("Services registered.");
}
}