Configuration.xaml.cs 7.7 KB

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