App.xaml.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using ClashDotNetFramework.Utils;
  2. using Garbage_Collector;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Reactive.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using SingleInstance;
  11. using ClashDotNetFramework.Models.Enums;
  12. using ClashDotNetFramework.Models;
  13. using ClashDotNetFramework.Controllers;
  14. using WindowsProxy;
  15. namespace ClashDotNetFramework
  16. {
  17. /// <summary>
  18. /// App.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class App : Application
  21. {
  22. public GarbageCollector garbageCollector = new GarbageCollector();
  23. public void ChangeLanguage(LanguageType language)
  24. {
  25. Global.Settings.Language = language;
  26. foreach (ResourceDictionary dict in Resources.MergedDictionaries)
  27. {
  28. if (dict is LanguageResourceDictionary languageDict)
  29. languageDict.UpdateSource();
  30. else
  31. dict.Source = dict.Source;
  32. }
  33. }
  34. public void ChangeTheme(ThemeType theme)
  35. {
  36. Global.Settings.Theme = theme;
  37. foreach (ResourceDictionary dict in Resources.MergedDictionaries)
  38. {
  39. if (dict is ThemeResourceDictionary themeDict)
  40. themeDict.UpdateSource();
  41. else
  42. dict.Source = dict.Source;
  43. }
  44. }
  45. protected override void OnStartup(StartupEventArgs e)
  46. {
  47. // 开启单实例
  48. StartSingleInstance(e);
  49. // 加载配置
  50. Configuration.Load();
  51. // 启动Clash实例
  52. Global.clashController = new ClashController();
  53. Global.clashController.Start();
  54. // 注册URI Scheme
  55. RegisterURIScheme();
  56. // 设置主题
  57. ChangeTheme(Global.Settings.Theme);
  58. // 设置语言
  59. ChangeLanguage(Global.Settings.Language);
  60. // 开始垃圾回收, 间隔 = 30秒w
  61. garbageCollector.StartCollecting();
  62. base.OnStartup(e);
  63. }
  64. protected override void OnExit(ExitEventArgs e)
  65. {
  66. try
  67. {
  68. // 杀死TrayIcon
  69. Global.iconController.Stop();
  70. // 关闭系统代理
  71. UnsetSystemProxy();
  72. // 杀死Redirector进程
  73. Global.nfController.Stop();
  74. // 杀死Clash进程
  75. Global.clashController.Stop();
  76. // 保存配置
  77. Configuration.Save();
  78. }
  79. finally
  80. {
  81. base.OnExit(e);
  82. }
  83. }
  84. private void StartSingleInstance(StartupEventArgs e)
  85. {
  86. var singleInstance = new SingleInstance.SingleInstance(@"Global\ClashDotNetFramework");
  87. if (!singleInstance.IsFirstInstance)
  88. {
  89. singleInstance.PassArgumentsToFirstInstance(e.Args);
  90. Environment.Exit(-1);
  91. }
  92. singleInstance.ArgumentsReceived.ObserveOnDispatcher().Subscribe(args => {
  93. foreach (var arg in args)
  94. {
  95. URIHelper.ProcessURI(arg);
  96. }
  97. });
  98. singleInstance.ListenForArgumentsFromSuccessiveInstances();
  99. }
  100. private void RegisterURIScheme()
  101. {
  102. if (!URISchemeHelper.Check())
  103. {
  104. URISchemeHelper.Set();
  105. }
  106. }
  107. private void UnsetSystemProxy()
  108. {
  109. if (Global.Settings.SystemProxy)
  110. {
  111. using var service = new ProxyService();
  112. service.Direct();
  113. }
  114. }
  115. }
  116. }