MainWindow.xaml.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using Vinno.IUS.Common.Log;
  15. using Vinno.IUS.Common.Network.Transfer;
  16. using Vinno.vCloud.Common.Storage.ObjectStorageInfo.Interface;
  17. using Vinno.vCloud.Common.Storage.ObjectStorageInfo.SignatureClient.Connect;
  18. using Vinno.vCloud.Common.Storage.ObjectStorageInfo.SignatureClient.UFile;
  19. using Vinno.vCloud.Common.Storage.ObjectStorageInfo.Solid;
  20. using Vinno.vCloud.Protocol.Infrastructures.Storage;
  21. using Vinno.vCloud.Protocol.Initializers;
  22. using Vinno.vCloud.Protocol.Messages.Common;
  23. using Vinno.vCloud.Protocol.Messages.Upgrade;
  24. namespace vCloud.Windows.ForceUpgrade
  25. {
  26. /// <summary>
  27. /// MainWindow.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class MainWindow : Window
  30. {
  31. private ILeafAgency _leafAgency;
  32. private UpgradeFilePathReader _filePathReader = new UpgradeFilePathReader();
  33. private string _fileListPath =Path.Combine(AppDomain.CurrentDomain.BaseDirectory , "FileList.json");
  34. private double _pie;
  35. private double _leftMargin;
  36. private double _currentProcess;
  37. private bool _isDownloadFinish = false;
  38. private IUFileStorageHelper _uFileStorageHelper;
  39. public MainWindow()
  40. {
  41. UpgradeTagsInitializer.Initialize();
  42. InitializeComponent();
  43. InitProcess();
  44. try
  45. {
  46. Task.Run(() =>
  47. {
  48. try
  49. {
  50. string upgradeServerUrl = string.Empty;
  51. var dic = CheckVersion(out upgradeServerUrl, App.GetVersion);
  52. if (dic.Count > 0)
  53. {
  54. KillVcloudClientProcess();
  55. Thread.Sleep(1000);
  56. KillVcloudClientProcess();
  57. Thread.Sleep(1000);
  58. RunUpgrade(dic, OnUpload);
  59. }
  60. else
  61. {
  62. OnUpload(100);
  63. RestartClient();
  64. }
  65. }
  66. catch (NetWorkException ex)
  67. {
  68. FileHelper.AppendWriteFile("UpgradeLog.txt", ex.Message + "|" + ex.StackTrace);
  69. Dispatcher.Invoke(() =>
  70. {
  71. MessageBox.Show(this, $"{App.GetOfflineShow}");
  72. Environment.Exit(0);
  73. });
  74. }
  75. catch (Exception ex)
  76. {
  77. FileHelper.AppendWriteFile("UpgradeLog.txt", ex.Message + "|" + ex.StackTrace);
  78. Dispatcher.Invoke(() =>
  79. {
  80. MessageBox.Show(this, $"{App.UpgradeError}");
  81. Environment.Exit(0);
  82. });
  83. }
  84. });
  85. }
  86. catch (Exception ex)
  87. {
  88. FileHelper.AppendWriteFile("UpgradeLog.txt", ex.Message + "|" + ex.StackTrace);
  89. Dispatcher.Invoke(() =>
  90. {
  91. MessageBox.Show(this, $"{App.UpgradeError}");
  92. Environment.Exit(0);
  93. });
  94. }
  95. }
  96. private void InitProcess()
  97. {
  98. ImageBrush backImage = new ImageBrush();
  99. var backImageUri = new Uri("pack://application:,,,/backImage.png");
  100. if (!App.IsChinese)
  101. {
  102. backImageUri = new Uri("pack://application:,,,/enBackImage.png");
  103. }
  104. backImage.ImageSource = new BitmapImage(backImageUri);
  105. backImage.Stretch = Stretch.Fill;
  106. Background = backImage;
  107. InstallProcess.Content = "1%";
  108. _pie = 551 / 100d;
  109. _leftMargin = 14;
  110. }
  111. /// <summary>
  112. /// 检查版本
  113. /// </summary>
  114. /// <param name="upgradeServerUrl"></param>
  115. /// <returns></returns>
  116. private Dictionary<string, UpgradeFileInfo> CheckVersion(out string upgradeServerUrl, string newVersion, bool forced = false)
  117. {
  118. upgradeServerUrl = string.Empty;
  119. string fileList = string.Empty;
  120. Dictionary<string, UpgradeFileInfo> srcDic = null;
  121. if (!File.Exists(_fileListPath) || forced == true)
  122. {
  123. srcDic = _filePathReader.StartReader(AppDomain.CurrentDomain.BaseDirectory);
  124. }
  125. else
  126. {
  127. fileList = File.ReadAllText(_fileListPath);
  128. srcDic = JsonConvert.DeserializeObject<Dictionary<string, UpgradeFileInfo>>(fileList);
  129. }
  130. //排除不存在的文件
  131. if (srcDic != null && srcDic.Count > 0)
  132. {
  133. var srcArray = srcDic.ToArray();
  134. foreach (var finfo in srcArray)
  135. {
  136. var filePath = finfo.Key.Replace("RootPath", $"{AppDomain.CurrentDomain.BaseDirectory}");
  137. if (!File.Exists(filePath))
  138. {
  139. srcDic.Remove(finfo.Key);
  140. }
  141. }
  142. fileList = JsonConvert.SerializeObject(srcDic);
  143. File.WriteAllText(_fileListPath, fileList);
  144. }
  145. if (string.IsNullOrWhiteSpace(newVersion) || newVersion == "New Version")
  146. {
  147. newVersion = GetNewPackageVersion(App.GetServerUrl)?.ToString();
  148. Dispatcher.Invoke(() =>
  149. {
  150. InstallVersion.Content = newVersion;
  151. });
  152. }
  153. string storageUrl = string.Empty;
  154. using (_leafAgency = new SimpleLeafAgency(App.GetServerUrl))
  155. {
  156. var request = new GetStorageUrlRequest();
  157. var urlMessage = _leafAgency.AgencySend(request);
  158. var storageUrlMessage = GetStorageUrlResult.Convert(urlMessage);
  159. if (storageUrlMessage != null)
  160. {
  161. storageUrl = storageUrlMessage.StorageUrl;
  162. }
  163. }
  164. if (!string.IsNullOrWhiteSpace(storageUrl))
  165. {
  166. _leafAgency = new SimpleLeafAgency(storageUrl);
  167. _uFileStorageHelper = new UFileStorageServerHelper(_leafAgency);
  168. var tempFileListPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ServerFileList.json");
  169. if (File.Exists(tempFileListPath))
  170. {
  171. File.Delete(tempFileListPath);
  172. }
  173. var isDownloadSuccess = _uFileStorageHelper.DownloadFile(
  174. new ObjectFileDownloadInfo
  175. {
  176. FileName = $"FileList[{newVersion}].json",
  177. FileLocalPath = tempFileListPath
  178. },
  179. StorageNodeType.UpgradeStorageCDN);
  180. if (isDownloadSuccess)
  181. {
  182. var json1 = File.ReadAllText(tempFileListPath);
  183. var json2 = File.ReadAllText(_fileListPath);
  184. var dic = _filePathReader.DiffReaderJson(json1, json2);
  185. using (Process ps = new Process())
  186. {
  187. ps.StartInfo.FileName = "cmd.exe";
  188. ps.StartInfo.UseShellExecute = false;
  189. ps.StartInfo.RedirectStandardOutput = true;
  190. ps.StartInfo.RedirectStandardInput = true;
  191. ps.StartInfo.CreateNoWindow = true;
  192. ps.StartInfo.Verb = "runas";
  193. ps.Start();
  194. ps.StandardInput.WriteLine($"taskkill /f /t /FI \"imagename eq vCloud.Windows.exe\"");
  195. ps.WaitForExit(1000);
  196. ps.StandardInput.WriteLine("exit");
  197. }
  198. return dic;
  199. }
  200. }
  201. return new Dictionary<string, UpgradeFileInfo>();
  202. }
  203. /// <summary>
  204. /// 客户端更新
  205. /// </summary>
  206. /// <param name="dic"></param>
  207. /// <param name="progressCallback"></param>
  208. private void RunUpgrade(Dictionary<string, UpgradeFileInfo> dic, Action<double> progressCallback = null)
  209. {
  210. if (dic != null && dic.Count > 0)
  211. {
  212. var fileCounts = dic.Count(f=>f.Value.Type == UpgradeOperateType.Add && f.Key.IndexOf("vCloud.Windows.ForceUpgrade.exe")==-1);
  213. int finishFileCount = 0;
  214. var totalFileProgress = 100F / (dic.Count);
  215. finishFileCount = AddFilesInStepUFileStorage(dic, progressCallback, finishFileCount, totalFileProgress);
  216. if (finishFileCount != fileCounts)
  217. {
  218. FileHelper.AppendWriteFile("UpgradeLog.txt", $"Upgrade Fail {App.PartFilesFail}");
  219. }
  220. else
  221. {
  222. File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"vCloud.Windows.exe"));
  223. }
  224. _isDownloadFinish = true;
  225. var updateTemp = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UpdateTemp");
  226. MoveFiles(updateTemp, AppDomain.CurrentDomain.BaseDirectory);
  227. DelEmptyFolder(AppDomain.CurrentDomain.BaseDirectory);
  228. Directory.Delete(updateTemp, true);
  229. if (File.Exists(_fileListPath))
  230. {
  231. var oldfileList = File.ReadAllText(_fileListPath);
  232. var oldDic = JsonConvert.DeserializeObject<Dictionary<string, UpgradeFileInfo>>(oldfileList);
  233. foreach (var newfile in dic)
  234. {
  235. if (newfile.Value.Type == UpgradeOperateType.Add)
  236. {
  237. if (!oldDic.Keys.Contains(newfile.Key))
  238. {
  239. oldDic.Add(newfile.Key, newfile.Value);
  240. }
  241. else
  242. {
  243. oldDic[newfile.Key] = newfile.Value;
  244. }
  245. }
  246. else
  247. {
  248. if (oldDic.Keys.Contains(newfile.Key))
  249. {
  250. oldDic.Remove(newfile.Key);
  251. }
  252. }
  253. }
  254. OnUpload(100);
  255. var fileList = JsonConvert.SerializeObject(oldDic);
  256. File.WriteAllText(_fileListPath, fileList);
  257. }
  258. }
  259. RestartClient();
  260. }
  261. /// <summary>
  262. /// 版本更新下载文件(UFile)
  263. /// </summary>
  264. /// <param name="dic"></param>
  265. /// <param name="progressCallback"></param>
  266. /// <param name="finishFileCount"></param>
  267. /// <param name="totalFileProgress"></param>
  268. /// <param name="works"></param>
  269. /// <returns></returns>
  270. private int AddFilesInStepUFileStorage(Dictionary<string, UpgradeFileInfo> dic, Action<double> progressCallback, int finishFileCount, float totalFileProgress)
  271. {
  272. var updateTempFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UpdateTemp");
  273. Directory.CreateDirectory(updateTempFolder);
  274. var downloadTempFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DownloadTemp");
  275. Directory.CreateDirectory(downloadTempFolder);
  276. if (dic != null && dic.Count > 0)
  277. {
  278. var allUpgradeFiles = dic.Where(f => f.Value.Type == UpgradeOperateType.Add && f.Key.IndexOf("vCloud.Windows.ForceUpgrade.exe") == -1).ToArray();
  279. if (allUpgradeFiles != null && allUpgradeFiles.Length > 0)
  280. {
  281. var ufiles = allUpgradeFiles.Select(f => new RootPathFileInfo
  282. {
  283. RootPath = f.Key + ".zip",
  284. UFileName = f.Value.Md5Code + ".zip"
  285. }).ToList();
  286. var success = _uFileStorageHelper.DownloadFiles(downloadTempFolder, StorageNodeType.UpgradeStorageCDN, ufiles, progressCallback);
  287. if (success)
  288. {
  289. var zipFiles = Directory.GetFiles(downloadTempFolder, "*.zip", SearchOption.AllDirectories);
  290. foreach (var file in ufiles)
  291. {
  292. if (file != null && !string.IsNullOrWhiteSpace(file.RootPath))
  293. {
  294. var realFileName = Path.GetFileName(file.RootPath.Substring(0, file.RootPath.LastIndexOf('.')));
  295. string inPath = file.RootPath.Replace("RootPath", downloadTempFolder);
  296. string outPath = Path.GetDirectoryName(file.RootPath.Replace("RootPath", updateTempFolder));
  297. Directory.CreateDirectory(outPath);
  298. DeZip(inPath, outPath);
  299. Dispatcher.Invoke(() =>
  300. {
  301. finishFileCount++;
  302. });
  303. }
  304. }
  305. Directory.Delete(downloadTempFolder, true);
  306. progressCallback?.Invoke(100);
  307. }
  308. }
  309. }
  310. return finishFileCount;
  311. }
  312. /// <summary>
  313. /// 解压文件
  314. /// </summary>
  315. /// <param name="filePath"></param>
  316. /// <param name="outPath"></param>
  317. private void DeZip(string filePath, string outPath)
  318. {
  319. using (Process ps = new Process())
  320. {
  321. ps.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Commons", "7z", "7za.exe");
  322. ps.StartInfo.Arguments = $"x \"{filePath}\" -o\"{outPath}\" -y";
  323. ps.StartInfo.UseShellExecute = false;
  324. ps.StartInfo.RedirectStandardOutput = false;
  325. ps.StartInfo.RedirectStandardInput = false;
  326. ps.StartInfo.CreateNoWindow = true;
  327. ps.StartInfo.Verb = "runas";
  328. ps.Start();
  329. ps.WaitForExit(15000);
  330. }
  331. }
  332. /// <summary>
  333. /// 关闭所有客户端
  334. /// </summary>
  335. private void KillVcloudClientProcess()
  336. {
  337. var mainProcesses = Process.GetProcessesByName("vCloud.Windows");
  338. foreach (var process in mainProcesses)
  339. {
  340. process.Kill();
  341. }
  342. var registerProcesses = Process.GetProcessesByName("vCloud.Windows.Register");
  343. foreach (var process in registerProcesses)
  344. {
  345. process.Kill();
  346. }
  347. var videoManagementProcesses = Process.GetProcessesByName("vCloud.Video.Management");
  348. foreach (var process in videoManagementProcesses)
  349. {
  350. process.Kill();
  351. }
  352. var reportProcesses = Process.GetProcessesByName("ReportTemplateDesigner");
  353. foreach (var process in reportProcesses)
  354. {
  355. process.Kill();
  356. }
  357. }
  358. /// <summary>
  359. /// 重启客户端
  360. /// </summary>
  361. private void RestartClient(bool moveTempFolder = true)
  362. {
  363. if (moveTempFolder)
  364. {
  365. if (Directory.Exists($"{ AppDomain.CurrentDomain.BaseDirectory}\\UpdateTemp"))
  366. {
  367. Directory.Delete($"{ AppDomain.CurrentDomain.BaseDirectory}\\UpdateTemp", true);
  368. }
  369. //if (Directory.Exists($"{ AppDomain.CurrentDomain.BaseDirectory}\\DownloadTemp"))
  370. //{
  371. // Directory.Delete($"{ AppDomain.CurrentDomain.BaseDirectory}\\DownloadTemp", true);
  372. //}
  373. }
  374. var clientPath = AppDomain.CurrentDomain.BaseDirectory + "vCloud.Windows.exe";
  375. Process.Start(clientPath);
  376. Thread.Sleep(100);
  377. Environment.Exit(0);
  378. }
  379. /// <summary>
  380. ///
  381. /// </summary>
  382. /// <param name="srcFolder"></param>
  383. /// <param name="targetFolder"></param>
  384. private void MoveFiles(string srcFolder, string targetFolder)
  385. {
  386. Directory.CreateDirectory(targetFolder);
  387. var files = Directory.GetFiles(srcFolder);
  388. foreach (var file in files)
  389. {
  390. File.Copy(file, file.Replace(srcFolder, targetFolder), true);
  391. }
  392. var dirs = Directory.GetDirectories(srcFolder);
  393. foreach (var dir in dirs)
  394. {
  395. MoveFiles(dir, dir.Replace(srcFolder, targetFolder));
  396. }
  397. }
  398. private void FilterWhiteList(Dictionary<string, UpgradeFileInfo> dic)
  399. {
  400. //过滤掉删除项暂时只同步新增文件
  401. var fileArray = dic.Where(f => f.Value.Type == UpgradeOperateType.Remove).ToArray();
  402. foreach (var fileInfo in fileArray)
  403. {
  404. dic.Remove(fileInfo.Key);
  405. }
  406. var whiteDic = dic.ToArray();
  407. foreach (var fileInfo in whiteDic)
  408. {
  409. if (WhiteList(fileInfo.Key))
  410. {
  411. dic.Remove(fileInfo.Key);
  412. }
  413. }
  414. }
  415. /// <summary>
  416. /// 白名单
  417. /// </summary>
  418. /// <param name="key"></param>
  419. /// <returns></returns>
  420. private bool WhiteList(string key)
  421. {
  422. if (Regex.IsMatch(key, "RootPath[\\\\]FileList.json"))
  423. {
  424. return true;
  425. }
  426. if (Regex.IsMatch(key, "RootPath[\\\\]UpdateTemp[\\\\/].*"))
  427. {
  428. return true;
  429. }
  430. if (Regex.IsMatch(key, ".*UserCommonSetting.conf.*"))
  431. {
  432. return true;
  433. }
  434. if (Regex.IsMatch(key, ".*vCloud.Windows.Upgrade.*"))
  435. {
  436. return true;
  437. }
  438. if (Regex.IsMatch(key, ".*Commons[\\\\/]CefSharp[\\\\/](x86|x64).*"))
  439. {
  440. return true;
  441. }
  442. if (Regex.IsMatch(key, ".*Commons[\\\\/]Emgu[\\\\/](x86|x64).*"))
  443. {
  444. return true;
  445. }
  446. if (Regex.IsMatch(key, "RootPath[\\\\]Newtonsoft.Json.*") || Regex.IsMatch(key, "RootPath[\\\\]SharpCompress.dll"))
  447. {
  448. return true;
  449. }
  450. if (Regex.IsMatch(key, "RootPath[\\\\]debug.log") || Regex.IsMatch(key, "RootPath[\\\\]InstallLog.txt") || Regex.IsMatch(key, "RootPath[\\\\]UpgradeLog.txt"))
  451. {
  452. return true;
  453. }
  454. return false;
  455. }
  456. private void OnUpload(double progress)
  457. {
  458. Dispatcher.Invoke(() => {
  459. InstallProcess.Content = progress + "%";
  460. var addProgressValue = (progress - _currentProcess) * _pie;
  461. if (addProgressValue > 0)
  462. {
  463. ProcessImage.Width -= (ProcessImage.Width > addProgressValue ? addProgressValue : ProcessImage.Width);
  464. _leftMargin -= addProgressValue;
  465. ProcessImage.Margin = new Thickness(14, 32, _leftMargin, 30);
  466. _currentProcess = progress;
  467. }
  468. if (progress == 100)
  469. {
  470. InstallContent.Content = App.GetFinishShow;
  471. }
  472. });
  473. Thread.Sleep(1);
  474. }
  475. private static string GetJson(Stream stream)
  476. {
  477. using (stream)
  478. {
  479. using (StreamReader streamReader = new StreamReader(stream, Encoding.UTF8))
  480. {
  481. return streamReader.ReadToEnd();
  482. }
  483. }
  484. }
  485. /// <summary>
  486. /// 清除空文件夹
  487. /// </summary>
  488. /// <param name="folder"></param>
  489. /// <returns></returns>
  490. private static bool DelEmptyFolder(string folder)
  491. {
  492. bool allChildFolderIsDelete = true;
  493. var files = Directory.GetFiles(folder);
  494. var childFolders = Directory.GetDirectories(folder);
  495. if (files.Length > 0)
  496. {
  497. if (childFolders.Length > 0)
  498. {
  499. foreach (var childFolder in childFolders)
  500. {
  501. if (DelEmptyFolder(childFolder) == false)
  502. {
  503. allChildFolderIsDelete = false;
  504. }
  505. }
  506. }
  507. return false;
  508. }
  509. else if (childFolders.Length > 0)
  510. {
  511. foreach (var childFolder in childFolders)
  512. {
  513. if (DelEmptyFolder(childFolder) == false)
  514. {
  515. allChildFolderIsDelete = false;
  516. }
  517. }
  518. }
  519. else
  520. {
  521. Directory.Delete(folder, true);
  522. return true;
  523. }
  524. if (allChildFolderIsDelete && Directory.Exists(folder))
  525. {
  526. Directory.Delete(folder, true);
  527. return true;
  528. }
  529. return false;
  530. }
  531. private Version GetNewPackageVersion(string serverUrlValue)
  532. {
  533. try
  534. {
  535. using (var leafAgency = new SimpleLeafAgency(serverUrlValue))
  536. {
  537. using (var getUpgradeVersionRequest = MessagePool.GetMessage<GetUpgradeVersionRequest>())
  538. {
  539. getUpgradeVersionRequest.Platform = UpgradePlatform.PC;
  540. getUpgradeVersionRequest.Type = UpgradeType.Client;
  541. var result = leafAgency.AgencySend(getUpgradeVersionRequest);
  542. var versionResult = GetUpgradeVersionResult.Convert(result);
  543. if (versionResult != null)
  544. {
  545. return versionResult.Version;
  546. }
  547. }
  548. }
  549. }
  550. catch (Exception ex)
  551. {
  552. Logger.WriteLineError($"Get package info error:{ex}");
  553. }
  554. return null;
  555. }
  556. private void Label_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
  557. {
  558. if (_isDownloadFinish == false)
  559. {
  560. Environment.Exit(0);
  561. }
  562. }
  563. }
  564. }