App.xaml.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using System.Windows;
  9. using vCloud.Windows.ForceUpgrade.Common;
  10. using Vinno.IUS.Common.Network;
  11. namespace vCloud.Windows.ForceUpgrade
  12. {
  13. /// <summary>
  14. /// App.xaml 的交互逻辑
  15. /// </summary>
  16. public partial class App : Application
  17. {
  18. private static string UpgradeVersion { get; set; } = "New Version";
  19. private static string ServerUrl { get; set; }
  20. Mutex mutex;
  21. protected override void OnStartup(StartupEventArgs e)
  22. {
  23. Thread.Sleep(1000);//等待其他程序退出;
  24. bool ret;
  25. mutex = new Mutex(true, "vCloud.Windows.ForceUpgrade", out ret);
  26. if (!ret)
  27. {
  28. Environment.Exit(0);
  29. }
  30. else
  31. {
  32. IsChinese = e.Args.Contains("-zh");
  33. InitLanguage();
  34. foreach (var arg in e.Args)
  35. {
  36. var matchValue = Regex.Match(arg, "-v([^ ]*)");
  37. if (matchValue.Success && matchValue.Groups.Count > 1)
  38. {
  39. UpgradeVersion = matchValue.Groups[1].Value;
  40. }
  41. }
  42. CommonInitializer.Initialize(Path.GetPathRoot(AppDomain.CurrentDomain.BaseDirectory) + "\\Vinno.VCloud");
  43. }
  44. try
  45. {
  46. if (!IsChinese)
  47. {
  48. ServerUrl = GetClientSettings(out string language);
  49. if (language == "Chinese")
  50. {
  51. IsChinese = true;
  52. InitLanguage();
  53. }
  54. }
  55. var ectype = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "vCloud.Windows.exe");
  56. var original = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "vCloud.Windows.ForceUpgrade.exe");
  57. if (File.Exists(ectype) && !File.Exists(original))
  58. {
  59. File.Copy(ectype, original, true);
  60. Process.Start(new ProcessStartInfo()
  61. {
  62. WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory,
  63. Verb = "runas",
  64. FileName = "vCloud.Windows.ForceUpgrade.exe",
  65. UseShellExecute = false
  66. });
  67. Thread.Sleep(100);
  68. Environment.Exit(0);
  69. }
  70. else
  71. {
  72. using (Process ps = new Process())
  73. {
  74. ps.StartInfo.FileName = $"cmd.exe";
  75. ps.StartInfo.UseShellExecute = false;
  76. ps.StartInfo.RedirectStandardOutput = true;
  77. ps.StartInfo.RedirectStandardInput = true;
  78. ps.StartInfo.CreateNoWindow = true;
  79. ps.StartInfo.Verb = "runas";
  80. ps.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
  81. ps.Start();
  82. ps.StandardInput.WriteLine($"{Path.GetPathRoot(AppDomain.CurrentDomain.BaseDirectory)}:");
  83. ps.StandardInput.WriteLine($"cd { AppDomain.CurrentDomain.BaseDirectory }");
  84. ps.StandardInput.WriteLine($"taskkill /f /t /im vCloud.Windows.exe");
  85. ps.WaitForExit(100);
  86. ps.StandardInput.WriteLine($"exit");
  87. ps.Close();
  88. }
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. File.AppendAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"), $"{ex}");
  94. }
  95. }
  96. private void InitLanguage()
  97. {
  98. TranslateHelper.CurrentLanguage = IsChinese ? TranslateHelper.ChineseLanguageKey : TranslateHelper.EnglishLanguageKey;
  99. InitializeLanguage(TranslateHelper.CurrentLanguage);
  100. }
  101. /// <summary>
  102. /// 读取配置
  103. /// </summary>
  104. /// <param name="language"></param>
  105. /// <returns></returns>
  106. private static string GetClientSettings(out string language)
  107. {
  108. var serverUrlValue = string.Empty;
  109. language = "English";
  110. var userSetting = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings", "Client", "UserCommonSetting.conf");
  111. var defalutSetting = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings", "Client", "Setting.conf");
  112. string userSettingContent = File.Exists(userSetting) ? File.ReadAllText(userSetting) : string.Empty;
  113. string defalutSettingContent = File.Exists(defalutSetting) ? File.ReadAllText(defalutSetting) : string.Empty;
  114. JToken userSettingJToken = !string.IsNullOrWhiteSpace(userSettingContent) ? JToken.Parse(userSettingContent) : null;
  115. JToken defalutSettingJToken = !string.IsNullOrWhiteSpace(defalutSettingContent) ? JToken.Parse(defalutSettingContent) : null;
  116. JToken languageToken = null;
  117. if (userSettingJToken != null)
  118. {
  119. //获取语言设置
  120. languageToken = userSettingJToken.SelectToken("$.UserCommonSetting.Language");
  121. if (languageToken != null)
  122. {
  123. var languageTokenValue = languageToken?.Value<string>();
  124. if (string.IsNullOrWhiteSpace(languageTokenValue))
  125. {
  126. languageToken = userSettingJToken.SelectToken("$.GeneralInfo.Language.DefaultValue");
  127. if (languageToken != null)
  128. {
  129. language = languageToken.Value<string>();
  130. }
  131. }
  132. }
  133. //获取ServerUrl设置
  134. var selectToken = userSettingJToken.SelectToken("$.UserCommonSetting.Server.DefaultValue");
  135. if (selectToken != null)
  136. {
  137. var selectItem = selectToken?.Value<string>();
  138. var serverUrlItem = userSettingJToken.SelectToken($"$.UserCommonSetting.['{selectItem}']");
  139. serverUrlValue = serverUrlItem?.Value<string>();
  140. }
  141. }
  142. if (defalutSettingJToken != null)
  143. {
  144. //获取语言设置
  145. if (languageToken == null)
  146. {
  147. languageToken = defalutSettingJToken.SelectToken("$.GeneralInfo.Language.DefaultValue");
  148. if (languageToken != null)
  149. {
  150. language = languageToken.Value<string>();
  151. }
  152. }
  153. //获取ServerUrl设置
  154. if (string.IsNullOrWhiteSpace(serverUrlValue))
  155. {
  156. var selectToken = userSettingJToken.SelectToken("$.ConnectInfo.Server.DefaultValue");
  157. if (selectToken != null)
  158. {
  159. var selectItem = selectToken?.Value<string>();
  160. var serverUrlItem = userSettingJToken.SelectToken($"$.ConnectInfo.['{selectItem}']");
  161. serverUrlValue = serverUrlItem?.Value<string>();
  162. }
  163. }
  164. }
  165. return serverUrlValue;
  166. }
  167. private void InitializeLanguage(string currentLanguage)
  168. {
  169. var assemblyName = GetType().Assembly.GetName().Name;
  170. foreach (var supportedLanguage in TranslateHelper.SupportedLanguages)
  171. {
  172. var resourceName = $"{assemblyName}.Common.Languages.{supportedLanguage}.json";
  173. using (var languageStream = GetType().Assembly.GetManifestResourceStream(resourceName))
  174. {
  175. if (languageStream != null)
  176. {
  177. var reader = new StreamReader(languageStream);
  178. var content = reader.ReadToEnd();
  179. TranslateHelper.AddLanguageResource(supportedLanguage, content);
  180. }
  181. }
  182. }
  183. if (TranslateHelper.SupportedLanguages.FirstOrDefault(v => v == currentLanguage) != null)
  184. {
  185. TranslateHelper.CurrentLanguage = currentLanguage;
  186. }
  187. else
  188. {
  189. TranslateHelper.CurrentLanguage = TranslateHelper.SupportedLanguages.FirstOrDefault();
  190. }
  191. }
  192. /// <summary>
  193. /// 是否是中文环境
  194. /// </summary>
  195. public static bool IsChinese { get; set; } = false;
  196. /// <summary>
  197. /// 更新版本号
  198. /// </summary>
  199. public static string GetVersion
  200. {
  201. get
  202. {
  203. return UpgradeVersion;
  204. }
  205. }
  206. public static string GetServerUrl
  207. {
  208. get
  209. {
  210. return ServerUrl;
  211. }
  212. }
  213. /// <summary>
  214. /// 语言
  215. /// </summary>
  216. public static bool GetIsChinese
  217. {
  218. get
  219. {
  220. return IsChinese;
  221. }
  222. }
  223. public static string GetTitle => TranslateHelper.Translate("Updating");
  224. public static string GetFinishShow => TranslateHelper.Translate("UpdateCompleted");
  225. public static string GetOfflineShow => TranslateHelper.Translate("OffLine");
  226. public static string UpgradeError => TranslateHelper.Translate("UpgradeError");
  227. public static string PartFilesFail => TranslateHelper.Translate("PartFilesFail");
  228. }
  229. }