Configuration.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. Logger.OnLog += MainLogger.Send;
  59. MainLogger.AddLogger(new LogFileLogger(CoreUtils.GetPath()));
  60. Logger.Send(LogType.Information, "", string.Format("Config Path: {0}", CoreUtils.GetPath()));
  61. ScriptDocument.DefaultAssemblies.AddRange(
  62. Assembly.Load("RoslynPad.Roslyn.Windows"),
  63. Assembly.Load("RoslynPad.Editor.Windows")
  64. );
  65. ScriptDocument.Initialize();
  66. });
  67. InitializeComponent();
  68. if (CheckForUpdates())
  69. {
  70. Close();
  71. return;
  72. }
  73. else if (CheckForVersion9Update())
  74. {
  75. Close();
  76. return;
  77. }
  78. DatabaseEngine.MoveUpdateFiles();
  79. Title = string.Format("PRS Server Manager (v{0})", CoreUtils.GetVersion());
  80. Logger.Send(LogType.Information, "", "Doing refresh");
  81. Servers.Refresh(true, true);
  82. }
  83. #region Version 9 Update
  84. private bool CheckForVersion9Update()
  85. {
  86. var location = $"{_autoUpdateSettings.Location}/v9";
  87. var result = Update.GetRemoteFile($"{GetChannelLocation(location, allowAlpha: true)}/version.txt");
  88. if(result.StatusCode == System.Net.HttpStatusCode.NotFound)
  89. {
  90. return false;
  91. }
  92. var res = MessageWindow.New()
  93. .Title("PRS Version 9")
  94. .Message("PRS Version 9 is available!")
  95. .AddOKButton("Proceed")
  96. .AddCancelButton()
  97. .Display()
  98. .Result;
  99. if(res != MessageWindowResult.OK)
  100. {
  101. return false;
  102. }
  103. var updateDBWindow = new UpdateDatabaseFiles();
  104. if(updateDBWindow.ShowDialog() != true)
  105. {
  106. return false;
  107. }
  108. if(!MessageWindow.ShowYesNo("Proceed with update to version 9?", "Update?"))
  109. {
  110. return false;
  111. }
  112. bool? bOK = null;
  113. Progress.ShowModal("Retrieving Update", progress =>
  114. {
  115. bOK = Update.DownloadAndRunInstaller(
  116. location => Update.GetRemoteFile($"{GetChannelLocation(location, allowAlpha: true)}/PRSSetup.exe").RawBytes,
  117. BeforeUpdate,
  118. true,
  119. location,
  120. "PRSSetup.exe",
  121. progress);
  122. });
  123. if (bOK is null || bOK == true)
  124. {
  125. // In this case, the Progress window was closed by the installer, and so we must close our current window.
  126. return true;
  127. }
  128. else
  129. {
  130. MessageWindow.ShowMessage("Unable to retrieve installer!", "Error");
  131. return false;
  132. }
  133. }
  134. #endregion
  135. #region Update
  136. private string GetChannelLocation(string location, bool allowAlpha = false)
  137. {
  138. if (allowAlpha)
  139. {
  140. return _autoUpdateSettings.Channel switch
  141. {
  142. AutoUpdateChannel.Unstable => $"{location}/Alpha",
  143. AutoUpdateChannel.PreRelease => $"{location}/PreRelease",
  144. AutoUpdateChannel.Stable or _ => $"{location}/Stable",
  145. };
  146. }
  147. else
  148. {
  149. return _autoUpdateSettings.Channel switch
  150. {
  151. AutoUpdateChannel.PreRelease or AutoUpdateChannel.Unstable => $"{location}/PreRelease",
  152. AutoUpdateChannel.Stable or _ => $"{location}/Stable",
  153. };
  154. }
  155. }
  156. private string GetUpdateLocation()
  157. {
  158. return _autoUpdateSettings.Location;
  159. }
  160. private string GetLatestVersion(string location)
  161. {
  162. return Update.GetRemoteFile($"{GetChannelLocation(location)}/version.txt").Content;
  163. }
  164. private string GetReleaseNotes(string location)
  165. {
  166. return Update.GetRemoteFile($"{location}/Release Notes.txt").Content;
  167. }
  168. private byte[]? GetInstaller(string location)
  169. {
  170. return Update.GetRemoteFile($"{GetChannelLocation(location)}/PRSSetup.exe").RawBytes;
  171. }
  172. private void BeforeUpdate()
  173. {
  174. Servers.BeforeUpdate();
  175. }
  176. private bool CheckForUpdates()
  177. {
  178. return Update.CheckForUpdates(GetUpdateLocation, GetLatestVersion, GetReleaseNotes, GetInstaller, BeforeUpdate, true, "PRSSetup.exe", true);
  179. }
  180. #endregion
  181. private void Window_Closing(object sender, CancelEventArgs e)
  182. {
  183. //if (CoreUtils.GetVersion() == "???")
  184. // Servers.StopAll();
  185. }
  186. }