新生命码神工具,代码生成、网络工具、API工具、串口工具、正则工具、图标工具、加解密工具、地图接口。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.4 KiB

6 years ago
6 years ago
6 years ago
5 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Windows.Forms;
  7. using NewLife;
  8. using NewLife.Log;
  9. using NewLife.Net;
  10. using NewLife.Threading;
  11. using NewLife.Xml;
  12. using XCode.DataAccessLayer;
  13. using System.Threading.Tasks;
  14. #if !NET4
  15. using Stardust;
  16. #endif
  17. namespace XCoder
  18. {
  19. static class Program
  20. {
  21. /// <summary>应用程序的主入口点。</summary>
  22. [STAThread]
  23. static void Main()
  24. {
  25. #if NET5_0_OR_GREATER
  26. XTrace2.UseWinForm();
  27. Application.SetHighDpiMode(HighDpiMode.SystemAware);
  28. #else
  29. XTrace.UseWinForm();
  30. #endif
  31. MachineInfo.RegisterAsync();
  32. #if !NET4
  33. StartClient();
  34. #endif
  35. StringHelper.EnableSpeechTip = XConfig.Current.SpeechTip;
  36. if (XConfig.Current.IsNew) "学无先后达者为师,欢迎使用新生命码神工具!".SpeechTip();
  37. Application.EnableVisualStyles();
  38. Application.SetCompatibleTextRenderingDefault(false);
  39. Application.Run(new FrmMDI());
  40. }
  41. #if !NET4
  42. static TimerX _timer;
  43. static StarClient _Client;
  44. //static ServiceManager _Manager;
  45. private static void StartClient()
  46. {
  47. var set = XConfig.Current;
  48. var server = set.Server;
  49. if (server.IsNullOrEmpty()) return;
  50. XTrace.WriteLine("初始化服务端地址:{0}", server);
  51. var client = new StarClient(server)
  52. {
  53. Code = set.Code,
  54. Secret = set.Secret,
  55. Log = XTrace.Log,
  56. };
  57. // 登录后保存证书
  58. client.OnLogined += (s, e) =>
  59. {
  60. var inf = client.Info;
  61. if (inf != null && !inf.Code.IsNullOrEmpty())
  62. {
  63. set.Code = inf.Code;
  64. set.Secret = inf.Secret;
  65. set.Save();
  66. }
  67. };
  68. client.UseTrace();
  69. Application.ApplicationExit += (s, e) => client.Logout("ApplicationExit");
  70. // 可能需要多次尝试
  71. _timer = new TimerX(TryConnectServer, client, 0, 5_000) { Async = true };
  72. _Client = client;
  73. }
  74. private static async Task TryConnectServer(Object state)
  75. {
  76. var client = state as StarClient;
  77. var set = XConfig.Current;
  78. await client.Login();
  79. await CheckUpgrade(client, set.Channel);
  80. // 登录成功,销毁定时器
  81. //TimerX.Current.Period = 0;
  82. _timer.TryDispose();
  83. _timer = null;
  84. }
  85. private static String _lastVersion;
  86. private static async Task CheckUpgrade(StarClient client, String channel)
  87. {
  88. var ug = new Stardust.Web.Upgrade { Log = XTrace.Log };
  89. // 检查更新
  90. var ur = await client.Upgrade(channel);
  91. if (ur != null && ur.Version != _lastVersion)
  92. {
  93. ug.Url = ur.Source;
  94. await ug.Download();
  95. // 检查文件完整性
  96. if (ur.FileHash.IsNullOrEmpty() || ug.CheckFileHash(ur.FileHash))
  97. {
  98. // 执行更新,解压缩覆盖文件
  99. var rs = ug.Update();
  100. if (rs && !ur.Executor.IsNullOrEmpty()) ug.Run(ur.Executor);
  101. _lastVersion = ur.Version;
  102. // 去除多余入口文件
  103. ug.Trim("StarAgent");
  104. // 强制更新时,马上重启
  105. if (rs && ur.Force)
  106. {
  107. //StopWork("Upgrade");
  108. // 重新拉起进程
  109. var star = "XCoder.exe";
  110. XTrace.WriteLine("强制升级,拉起进程 {0} -upgrade", star.GetFullPath());
  111. Process.Start(star.GetFullPath(), "-upgrade");
  112. //var p = Process.GetCurrentProcess();
  113. //p.Close();
  114. //p.Kill();
  115. Application.Exit();
  116. }
  117. }
  118. }
  119. }
  120. #endif
  121. }
  122. }