UpgradeManager.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Avalonia.Controls.Shapes;
  2. using fis.Upgrader.Helpers;
  3. using FisTools;
  4. using System;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace fis.Upgrader.Managers
  10. {
  11. internal class UpgradeManager
  12. {
  13. private static UpgradeManager _instance;
  14. private double _lastProgress;
  15. public event EventHandler<double> OnUpgradeProgressChanged;
  16. public static UpgradeManager Instance
  17. {
  18. get {
  19. if (_instance == null)
  20. {
  21. _instance = new UpgradeManager();
  22. }
  23. return _instance;
  24. }
  25. }
  26. private UpgradeManager() {
  27. }
  28. public void RunFullUpgrade()
  29. {
  30. Task.Run(() => {
  31. Thread.Sleep(30000);
  32. OnProgressChanged(0.98);
  33. Thread.Sleep(15000);
  34. OnProgressChanged(1.0);
  35. });
  36. ToolManager.Instance.LoaderCenter.KillProcessWithGivenName("fis");
  37. ToolManager.Instance.UpgradeCenter.ExecuteFullUpgrade(Logger.WriteLine, OnProgressChanged);
  38. }
  39. private void OnProgressChanged(double progress)
  40. {
  41. if (_lastProgress > progress)
  42. {
  43. Logger.WriteLine($"{progress} is returned ");
  44. return;
  45. }
  46. _lastProgress = progress;
  47. OnUpgradeProgressChanged?.Invoke(null, progress);
  48. if (progress >= 1.0)
  49. {
  50. Task.Run(() =>
  51. {
  52. try
  53. {
  54. Thread.Sleep(1000);
  55. var currentDir = AppDomain.CurrentDomain.BaseDirectory;
  56. Logger.WriteLine($"Current dir is{currentDir}");
  57. var fisDir = Directory.GetParent(currentDir)!.Parent!.FullName;
  58. Logger.WriteLine($"fis dir is{fisDir}");
  59. var fileFullName = System.IO.Path.Combine(fisDir, "fis.exe");
  60. if (!File.Exists(fileFullName))
  61. {
  62. Logger.WriteLine($"{fileFullName} not exist");
  63. }
  64. Logger.WriteLine($"Full name is{fileFullName}");
  65. ToolManager.Instance.LoaderCenter.StartProcessDirectly(fileFullName);
  66. }
  67. catch (Exception ex)
  68. {
  69. Logger.WriteLine($"Exception happened {ex} ");
  70. }
  71. finally
  72. {
  73. ToolManager.Instance.LoaderCenter.KillProcessWithGivenName("fis.Upgrader");
  74. }
  75. });
  76. }
  77. else {
  78. Logger.WriteLine($"Current progress is {progress} ");
  79. }
  80. }
  81. }
  82. }