Configuration.xaml.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System.ComponentModel;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Windows.Media;
  5. using Comal.Classes;
  6. using Comal.Stores;
  7. using InABox.Configuration;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.Logging;
  11. using InABox.Scripting;
  12. using InABox.Wpf;
  13. using InABox.Wpf.Reports;
  14. using InABox.WPF;
  15. using InABox.WPF.Themes;
  16. using PRS.Shared;
  17. using PRSServer.Forms.Version9Update;
  18. using PRSServices;
  19. using Syncfusion.Licensing;
  20. using Color = System.Windows.Media.Color;
  21. using ColorConverter = System.Windows.Media.ColorConverter;
  22. namespace PRSServer;
  23. /// <summary>
  24. /// Interaction logic for MainWindow.xaml
  25. /// </summary>
  26. public partial class Configuration : ThemableWindow
  27. {
  28. private AutoUpdateSettings _autoUpdateSettings;
  29. public Configuration()
  30. {
  31. ThemeManager.BaseColor = Colors.CornflowerBlue;
  32. var database = PRSService.GetConfiguration().LoadAll().FirstOrDefault(x => x.Value.Type == ServerType.Database).Value;
  33. if(database is not null)
  34. {
  35. var properties = (database.DeserializeServerProperties() as DatabaseServerProperties)!;
  36. try
  37. {
  38. ThemeManager.BaseColor = (Color)ColorConverter.ConvertFromString(properties.ColorScheme);
  39. }
  40. catch
  41. {
  42. }
  43. }
  44. DynamicGridUtils.SelectionBackground = ThemeManager.SelectionBackgroundBrush;
  45. DynamicGridUtils.SelectionForeground = ThemeManager.SelectionForegroundBrush;
  46. Progress.DisplayImage = Properties.Resources.appstore.AsBitmapImage(200, 200);
  47. Progress.ShowModal("Starting...", progress =>
  48. {
  49. _autoUpdateSettings = new LocalConfiguration<AutoUpdateSettings>().Load();
  50. StoreUtils.RegisterClasses();
  51. CoreUtils.RegisterClasses();
  52. ComalUtils.RegisterClasses();
  53. PRSSharedUtils.RegisterClasses();
  54. ReportUtils.RegisterClasses();
  55. WPFUtils.RegisterClasses();
  56. ConfigurationUtils.RegisterClasses();
  57. CoreUtils.RegisterClasses(typeof(Configuration).Assembly);
  58. DatabaseUpdateScripts.RegisterScripts();
  59. Logger.OnLog += MainLogger.Send;
  60. MainLogger.AddLogger(new LogFileLogger(CoreUtils.GetPath(), false));
  61. Logger.Send(LogType.Information, "", string.Format("Config Path: {0}", CoreUtils.GetPath()));
  62. ScriptDocument.DefaultAssemblies.AddRange(
  63. Assembly.Load("RoslynPad.Roslyn.Windows"),
  64. Assembly.Load("RoslynPad.Editor.Windows")
  65. );
  66. ScriptDocument.Initialize();
  67. });
  68. InitializeComponent();
  69. if (CheckForUpdates())
  70. {
  71. Close();
  72. return;
  73. }
  74. else if (CheckForMajorVersionUpdate(9))
  75. {
  76. Close();
  77. return;
  78. }
  79. DatabaseEngine.MoveUpdateFiles();
  80. Title = string.Format("PRS Server Manager (v{0})", CoreUtils.GetVersion());
  81. Logger.Send(LogType.Information, "", "Doing refresh");
  82. Servers.Refresh(true, true);
  83. }
  84. #region Major Version Update
  85. private bool CheckForMajorVersionUpdate(int targetMajorVersion)
  86. {
  87. var location = $"{_autoUpdateSettings.Location}/v{targetMajorVersion}";
  88. var result = Update.GetRemoteFile($"{GetChannelLocation(location, allowAlpha: true)}/version.txt");
  89. if(result.StatusCode != System.Net.HttpStatusCode.OK)
  90. {
  91. return false;
  92. }
  93. var res = MessageWindow.New()
  94. .Title($"PRS Version {targetMajorVersion}")
  95. .Message($"PRS Version {targetMajorVersion} is available!")
  96. .AddOKButton("Proceed")
  97. .AddCancelButton()
  98. .Display()
  99. .Result;
  100. if(res != MessageWindowResult.OK)
  101. {
  102. return false;
  103. }
  104. var installerNameResult = Progress.ShowModal<string?>("Retrieving Update", progress =>
  105. {
  106. return Result.Ok(Update.DownloadInstaller(
  107. location =>
  108. {
  109. var result = Update.GetRemoteFile($"{GetChannelLocation(location, allowAlpha: true)}/PRSSetup.exe");
  110. if(result.StatusCode == System.Net.HttpStatusCode.OK)
  111. {
  112. return result.RawBytes;
  113. }
  114. else
  115. {
  116. return null;
  117. }
  118. },
  119. location,
  120. "PRSSetup.exe"));
  121. });
  122. if(!installerNameResult.GetOk(out var installerName) || installerName is null)
  123. {
  124. MessageWindow.ShowMessage($"Could not retrieve installer; cancelling update to version {targetMajorVersion}.", "Download failed.");
  125. return false;
  126. }
  127. Servers.BeforeUpdate();
  128. if (!UpdateDatabaseFiles.UpdateDatabases(targetMajorVersion))
  129. {
  130. return false;
  131. }
  132. bool? bOK = null;
  133. Progress.ShowModal("Launching Update", progress =>
  134. {
  135. bOK = Update.RunInstaller(installerName, BeforeUpdate, true, progress);
  136. });
  137. if (bOK is null || bOK == true)
  138. {
  139. // In this case, the Progress window was closed by the installer, and so we must close our current window.
  140. return true;
  141. }
  142. else
  143. {
  144. return false;
  145. }
  146. }
  147. #endregion
  148. #region Update
  149. private string GetChannelLocation(string location, bool allowAlpha = false)
  150. {
  151. if (allowAlpha)
  152. {
  153. return _autoUpdateSettings.Channel switch
  154. {
  155. AutoUpdateChannel.Unstable => $"{location}/Alpha",
  156. AutoUpdateChannel.PreRelease => $"{location}/PreRelease",
  157. AutoUpdateChannel.Stable or _ => $"{location}/Stable",
  158. };
  159. }
  160. else
  161. {
  162. return _autoUpdateSettings.Channel switch
  163. {
  164. AutoUpdateChannel.PreRelease or AutoUpdateChannel.Unstable => $"{location}/PreRelease",
  165. AutoUpdateChannel.Stable or _ => $"{location}/Stable",
  166. };
  167. }
  168. }
  169. private string GetUpdateLocation()
  170. {
  171. return _autoUpdateSettings.Location;
  172. }
  173. private string GetLatestVersion(string location)
  174. {
  175. return Update.GetRemoteFile($"{GetChannelLocation(location)}/version.txt").Content;
  176. }
  177. private string GetReleaseNotes(string location)
  178. {
  179. return Update.GetRemoteFile($"{location}/Release Notes.txt").Content;
  180. }
  181. private byte[]? GetInstaller(string location)
  182. {
  183. return Update.GetRemoteFile($"{GetChannelLocation(location)}/PRSSetup.exe").RawBytes;
  184. }
  185. private void BeforeUpdate()
  186. {
  187. Servers.BeforeUpdate();
  188. }
  189. private bool CheckForUpdates()
  190. {
  191. return Update.CheckForUpdates(GetUpdateLocation, GetLatestVersion, GetReleaseNotes, GetInstaller, BeforeUpdate, true, "PRSSetup.exe", true);
  192. }
  193. #endregion
  194. private void Window_Closing(object sender, CancelEventArgs e)
  195. {
  196. //if (CoreUtils.GetVersion() == "???")
  197. // Servers.StopAll();
  198. }
  199. }