-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
550 lines (495 loc) · 24.2 KB
/
Copy pathProgram.cs
File metadata and controls
550 lines (495 loc) · 24.2 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using YTPlayer.Core;
using YTPlayer.Utils;
namespace YTPlayer
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
TryEnableLegacyAccessibility();
// ✅ 初始化日志系统(优先执行)
DebugLogger.Initialize();
DebugLogger.Log(DebugLogger.LogLevel.Info, "Program", "════════════════════════════════════════");
DebugLogger.Log(DebugLogger.LogLevel.Info, "Program", "应用程序启动");
DebugLogger.Log(DebugLogger.LogLevel.Info, "Program", $"命令行参数: {string.Join(" ", args)}");
DebugLogger.Log(DebugLogger.LogLevel.Info, "Program", "════════════════════════════════════════");
// ✅ 配置依赖加载路径(托管与本机)
ConfigurePrivateLibPath();
// 与 .NET Framework 4.8 视觉一致(默认字体/高 DPI 行为)
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
Application.SetDefaultFont(new Font("Microsoft YaHei UI", 10.5f));
// ✅ 注册全局异常处理器
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
Application.ThreadException += OnThreadException;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// 清理旧日志(保留7天)
DebugLogger.CleanOldLogs(7);
try
{
// 检查命令行参数
if (args.Length > 0 && args[0] == "--check-vip")
{
DebugLogger.Log(DebugLogger.LogLevel.Info, "Program", "运行 VIP 诊断模式");
// 运行VIP诊断
CheckVIPLevelAsync().GetAwaiter().GetResult();
return;
}
// ✅ 初始化质量管理器(设置默认音质为"极高")
InitializeQualityManager();
// 正常启动GUI
DebugLogger.Log(DebugLogger.LogLevel.Info, "Program", "启动 GUI 模式");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ThemeManager.Initialize();
bool shouldForceForeground = ForegroundWindowHelper.ConsumeForegroundRequestFlag();
using var mainForm = new MainForm();
if (shouldForceForeground)
{
mainForm.Shown += (_, __) =>
{
mainForm.BeginInvoke(new Action(() =>
{
bool activated = ForegroundWindowHelper.TryActivateWindow(
mainForm.Handle,
message => DebugLogger.Log(DebugLogger.LogLevel.Info, "Foreground", message),
attempts: 10,
delayMs: 80);
if (!activated)
{
DebugLogger.Log(DebugLogger.LogLevel.Warning, "Foreground", "启动时主窗口前台激活失败。");
}
}));
};
}
Application.Run(mainForm);
}
catch (Exception ex)
{
DebugLogger.LogException("Program", ex, "Main 函数顶层异常");
MessageBox.Show(
$"应用程序启动失败:\n\n{ex.Message}\n\n详细信息已记录到日志文件。",
"严重错误",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
finally
{
DebugLogger.Log(DebugLogger.LogLevel.Info, "Program", "应用程序退出");
DebugLogger.Shutdown();
}
}
/// <summary>
/// 初始化质量管理器
/// 设置默认音质为"极高",处理登录状态联动
/// </summary>
private static void InitializeQualityManager()
{
try
{
var configManager = ConfigManager.Instance;
var config = configManager.Load();
// 检查登录状态并设置默认音质
// 如果未登录且配置的音质不可用,降级到"极高音质"
var accountStore = new AccountStateStore();
var accountState = accountStore.Load();
bool isLoggedIn = accountState.IsLoggedIn;
if (string.IsNullOrEmpty(config.DefaultQuality))
{
config.DefaultQuality = "极高音质"; // 默认极高
configManager.Save(config);
}
else if (!isLoggedIn)
{
// 未登录时,仅允许标准和极高
if (config.DefaultQuality != "标准音质" && config.DefaultQuality != "极高音质")
{
DebugLogger.Log(DebugLogger.LogLevel.Info, "Program",
$"User not logged in, downgrading quality from {config.DefaultQuality} to 极高音质");
config.DefaultQuality = "极高音质";
configManager.Save(config);
}
}
DebugLogger.Log(DebugLogger.LogLevel.Info, "Program",
$"✓ 默认音质已设置为: {config.DefaultQuality}");
}
catch (Exception ex)
{
DebugLogger.LogException("Program", ex, "初始化默认音质失败(非致命)");
}
}
private static void TryEnableLegacyAccessibility()
{
try
{
AppContext.SetSwitch("Switch.System.Windows.Forms.UseLegacyAccessibilityFeatures", true);
AppContext.SetSwitch("Switch.System.Windows.Forms.UseLegacyAccessibilityFeatures.2", true);
AppContext.SetSwitch("Switch.System.Windows.Forms.UseLegacyAccessibilityFeatures.3", true);
bool value1;
bool value2;
bool value3;
AppContext.TryGetSwitch("Switch.System.Windows.Forms.UseLegacyAccessibilityFeatures", out value1);
AppContext.TryGetSwitch("Switch.System.Windows.Forms.UseLegacyAccessibilityFeatures.2", out value2);
AppContext.TryGetSwitch("Switch.System.Windows.Forms.UseLegacyAccessibilityFeatures.3", out value3);
DebugLogger.Log(DebugLogger.LogLevel.Info, "Program",
$"Legacy accessibility switches: {value1}, {value2}, {value3}");
}
catch
{
}
}
/// <summary>
/// 将 libs 目录加入依赖搜索路径(托管与本机)
/// </summary>
private static void ConfigurePrivateLibPath()
{
try
{
string libsPath = PathHelper.LibsDirectory;
string? runtimeNativePath = GetRuntimeNativePath();
bool hasLibs = Directory.Exists(libsPath);
bool hasRuntimeNative = !string.IsNullOrWhiteSpace(runtimeNativePath) &&
Directory.Exists(runtimeNativePath);
if (!hasLibs && !hasRuntimeNative)
{
return;
}
if (hasLibs)
{
SetDllDirectory(libsPath);
}
else if (hasRuntimeNative)
{
SetDllDirectory(runtimeNativePath!);
}
string currentPath = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
var pathEntries = currentPath.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.ToList();
var pathSet = new HashSet<string>(pathEntries, StringComparer.OrdinalIgnoreCase);
bool pathChanged = false;
if (hasLibs && pathSet.Add(libsPath))
{
pathEntries.Insert(0, libsPath);
pathChanged = true;
}
if (hasRuntimeNative && runtimeNativePath != null && pathSet.Add(runtimeNativePath))
{
pathEntries.Insert(0, runtimeNativePath);
pathChanged = true;
}
if (pathChanged)
{
Environment.SetEnvironmentVariable("PATH", string.Join(";", pathEntries));
}
if (!hasLibs)
{
return;
}
Assembly? ResolveFromLibs(AssemblyName name)
{
try
{
if (string.IsNullOrWhiteSpace(name.Name))
{
return null;
}
var loaded = AssemblyLoadContext.Default.Assemblies
.FirstOrDefault(asm => string.Equals(asm.GetName().Name, name.Name, StringComparison.OrdinalIgnoreCase));
if (loaded != null)
{
return loaded;
}
string candidate = Path.Combine(libsPath, name.Name + ".dll");
if (File.Exists(candidate))
{
return AssemblyLoadContext.Default.LoadFromAssemblyPath(candidate);
}
}
catch (Exception ex)
{
DebugLogger.LogException("Program", ex, $"AssemblyResolve 处理失败: {name.FullName}");
}
return null;
}
// 托管依赖兜底解析(确保 Newtonsoft.Json 等从 libs 解析)
AssemblyLoadContext.Default.Resolving += (_, name) => ResolveFromLibs(name);
AppDomain.CurrentDomain.AssemblyResolve += (_, args) => ResolveFromLibs(new AssemblyName(args.Name));
}
catch (Exception ex)
{
DebugLogger.LogException("Program", ex, "配置依赖目录失败(非致命)");
}
}
private static string? GetRuntimeNativePath()
{
string? rid = RuntimeInformation.ProcessArchitecture switch
{
Architecture.X64 => "win-x64",
Architecture.X86 => "win-x86",
Architecture.Arm64 => "win-arm64",
Architecture.Arm => "win-arm",
_ => null
};
if (string.IsNullOrWhiteSpace(rid))
{
return null;
}
return Path.Combine(PathHelper.RuntimesDirectory, rid, "native");
}
[DllImport("kernel32", SetLastError = true)]
private static extern bool SetDllDirectory(string lpPathName);
/// <summary>
/// 捕获未处理的异常(非UI线程)
/// </summary>
private static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var ex = e.ExceptionObject as Exception;
DebugLogger.LogException("UnhandledException", ex ?? new Exception(e.ExceptionObject?.ToString()),
$"IsTerminating: {e.IsTerminating}");
if (e.IsTerminating)
{
MessageBox.Show(
$"应用程序遇到致命错误即将崩溃:\n\n{ex?.Message ?? e.ExceptionObject?.ToString()}\n\n日志已保存。",
"致命错误",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
/// <summary>
/// 捕获UI线程异常
/// </summary>
private static void OnThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
DebugLogger.LogException("ThreadException", e.Exception, "UI线程异常");
var result = MessageBox.Show(
$"应用程序遇到错误:\n\n{e.Exception.Message}\n\n详细信息已记录到日志。\n\n是否继续运行?",
"错误",
MessageBoxButtons.YesNo,
MessageBoxIcon.Error);
if (result == DialogResult.No)
{
RequestShutdown();
}
}
private static void RequestShutdown()
{
try
{
var forms = Application.OpenForms.Cast<Form>().ToArray();
if (forms.Length == 0)
{
Application.ExitThread();
return;
}
foreach (var form in forms)
{
try
{
if (form.IsDisposed)
{
continue;
}
if (form.InvokeRequired)
{
form.BeginInvoke(new Action(() =>
{
try
{
if (!form.IsDisposed)
{
form.Close();
}
}
catch (Exception closeEx)
{
DebugLogger.LogException("Program", closeEx, "关闭窗体失败");
}
}));
}
else
{
form.Close();
}
}
catch (Exception ex)
{
DebugLogger.LogException("Program", ex, "请求关闭窗体失败");
}
}
}
catch (Exception outer)
{
DebugLogger.LogException("Program", outer, "RequestShutdown 失败");
Application.ExitThread();
}
}
/// <summary>
/// 检查VIP等级的命令行工具
/// </summary>
private static async Task CheckVIPLevelAsync()
{
Console.WriteLine("╔═══════════════════════════════════════════════════════════╗");
Console.WriteLine("║ NetEase Cloud Music - VIP Level Verification Tool ║");
Console.WriteLine("╚═══════════════════════════════════════════════════════════╝");
Console.WriteLine();
// 读取配置文件
string configPath = "config.json";
if (!File.Exists(configPath))
{
Console.WriteLine("❌ Config file not found: config.json");
Console.WriteLine(" Please run from project root directory.");
return;
}
string configJson = File.ReadAllText(configPath);
var config = JObject.Parse(configJson);
string musicU = config["MusicU"]?.Value<string>() ?? string.Empty;
string csrfToken = config["CsrfToken"]?.Value<string>() ?? string.Empty;
string deviceId = config["DeviceId"]?.Value<string>() ?? string.Empty;
if (string.IsNullOrEmpty(musicU))
{
Console.WriteLine("❌ MUSIC_U not found in config.json");
Console.WriteLine(" Please login first.");
return;
}
Console.WriteLine("✅ Config loaded");
Console.WriteLine($" MUSIC_U length: {musicU.Length} characters");
Console.WriteLine();
// 创建API客户端
var apiClient = new NeteaseApiClient(musicU, csrfToken, deviceId);
Console.WriteLine("──────────────────────────────────────────────────────────");
Console.WriteLine("Checking account information...");
Console.WriteLine("──────────────────────────────────────────────────────────");
Console.WriteLine();
try
{
var userInfo = await apiClient.GetUserInfoAsync();
if (userInfo == null)
{
Console.WriteLine("❌ Failed to get user info");
Console.WriteLine(" Cookie may be expired or invalid.");
return;
}
// 显示用户信息
Console.WriteLine("╔═══════════════════════════════════════════════════════════╗");
Console.WriteLine("║ Account Information ║");
Console.WriteLine("╚═══════════════════════════════════════════════════════════╝");
Console.WriteLine();
Console.WriteLine($"User ID: {userInfo.UserId}");
Console.WriteLine($"Nickname: {userInfo.Nickname}");
Console.WriteLine($"VIP Type: {userInfo.VipType}");
Console.WriteLine();
// 分析VIP等级
Console.WriteLine("──────────────────────────────────────────────────────────");
Console.WriteLine("VIP Level Analysis:");
Console.WriteLine("──────────────────────────────────────────────────────────");
Console.WriteLine();
switch (userInfo.VipType)
{
case 0:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("❌ No VIP Subscription");
Console.ResetColor();
Console.WriteLine();
Console.WriteLine("Available: standard (128k), exhigh (320k)");
Console.WriteLine("Unavailable: lossless, hires, jymaster, sky (require VIP/SVIP)");
break;
case 1:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("⚠️ Regular VIP (黑胶VIP)");
Console.ResetColor();
Console.WriteLine();
Console.WriteLine("Available Quality:");
Console.WriteLine(" ✅ standard (128 kbps)");
Console.WriteLine(" ✅ exhigh (320 kbps)");
Console.WriteLine(" ✅ lossless (1411 kbps)");
Console.WriteLine();
Console.WriteLine("Unavailable Quality:");
Console.WriteLine(" ❌ hires (2822 kbps) - Requires SVIP");
Console.WriteLine(" ❌ jyeffect - Requires SVIP");
Console.WriteLine(" ❌ sky - Requires SVIP");
Console.WriteLine(" ❌ jymaster - Requires SVIP");
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("⚠️ THIS IS WHY YOU CAN ONLY GET LOSSLESS!");
Console.WriteLine(" To access jymaster/sky/hires:");
Console.WriteLine(" → Upgrade to 黑胶SVIP (VipType=11)");
Console.ResetColor();
break;
case 11:
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("✅ SVIP (黑胶SVIP)");
Console.ResetColor();
Console.WriteLine();
Console.WriteLine("✅ You have access to ALL quality levels:");
Console.WriteLine(" • standard, exhigh, lossless");
Console.WriteLine(" • hires, jyeffect, sky, jymaster");
Console.WriteLine();
Console.WriteLine("Note: If you can't get SVIP quality:");
Console.WriteLine(" 1. Song may not have ultra-HD version");
Console.WriteLine(" 2. Try newer songs");
Console.WriteLine(" 3. Check debug logs for server downgrade");
break;
default:
Console.WriteLine($"⚠️ Unknown VIP Type: {userInfo.VipType}");
break;
}
Console.WriteLine();
Console.WriteLine("──────────────────────────────────────────────────────────");
Console.WriteLine("VIP Type Reference:");
Console.WriteLine(" 0 = No VIP");
Console.WriteLine(" 1 = Regular VIP (黑胶VIP)");
Console.WriteLine(" 11 = SVIP (黑胶SVIP)");
Console.WriteLine("──────────────────────────────────────────────────────────");
Console.WriteLine();
// 结论
if (userInfo.VipType == 11)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("✅ Your account has SVIP privileges.");
Console.WriteLine(" You should be able to access ultra-HD quality.");
Console.ResetColor();
}
else if (userInfo.VipType == 1)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("⚠️ ROOT CAUSE FOUND!");
Console.WriteLine();
Console.WriteLine(" Your account is Regular VIP, NOT SVIP.");
Console.WriteLine(" This explains the lossless-only limitation.");
Console.WriteLine();
Console.WriteLine(" Solution: Upgrade to 黑胶SVIP");
Console.WriteLine(" (No code fix can bypass this)");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("❌ No VIP subscription detected.");
Console.WriteLine(" Purchase 黑胶SVIP to access ultra-HD quality.");
Console.ResetColor();
}
Console.WriteLine();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"❌ Error: {ex.Message}");
Console.ResetColor();
}
}
}
}