| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Windows.Media;
- using Comal.Classes;
- using Comal.Stores;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.Logging;
- using InABox.Scripting;
- using InABox.Wpf;
- using InABox.Wpf.Reports;
- using InABox.WPF;
- using InABox.WPF.Themes;
- using PRS.Shared;
- using PRSServer.Forms.Version9Update;
- using PRSServices;
- using Syncfusion.Licensing;
- using Color = System.Windows.Media.Color;
- using ColorConverter = System.Windows.Media.ColorConverter;
- namespace PRSServer;
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class Configuration : ThemableWindow
- {
- private AutoUpdateSettings _autoUpdateSettings;
- public Configuration()
- {
- ThemeManager.BaseColor = Colors.CornflowerBlue;
- var database = PRSService.GetConfiguration().LoadAll().FirstOrDefault(x => x.Value.Type == ServerType.Database).Value;
- if(database is not null)
- {
- var properties = (database.DeserializeServerProperties() as DatabaseServerProperties)!;
-
- try
- {
- ThemeManager.BaseColor = (Color)ColorConverter.ConvertFromString(properties.ColorScheme);
- }
- catch
- {
- }
- }
- DynamicGridUtils.SelectionBackground = ThemeManager.SelectionBackgroundBrush;
- DynamicGridUtils.SelectionForeground = ThemeManager.SelectionForegroundBrush;
- Progress.DisplayImage = Properties.Resources.appstore.AsBitmapImage(200, 200);
- Progress.ShowModal("Starting...", progress =>
- {
- _autoUpdateSettings = new LocalConfiguration<AutoUpdateSettings>().Load();
- StoreUtils.RegisterClasses();
- CoreUtils.RegisterClasses();
- ComalUtils.RegisterClasses();
- PRSSharedUtils.RegisterClasses();
- ReportUtils.RegisterClasses();
- WPFUtils.RegisterClasses();
- ConfigurationUtils.RegisterClasses();
- CoreUtils.RegisterClasses(typeof(Configuration).Assembly);
- DatabaseUpdateScripts.RegisterScripts();
- Logger.OnLog += MainLogger.Send;
- MainLogger.AddLogger(new LogFileLogger(CoreUtils.GetPath(), false));
- Logger.Send(LogType.Information, "", string.Format("Config Path: {0}", CoreUtils.GetPath()));
- ScriptDocument.DefaultAssemblies.AddRange(
- Assembly.Load("RoslynPad.Roslyn.Windows"),
- Assembly.Load("RoslynPad.Editor.Windows")
- );
- ScriptDocument.Initialize();
- });
- InitializeComponent();
- if (CheckForUpdates())
- {
- Close();
- return;
- }
- else if (CheckForMajorVersionUpdate(9))
- {
- Close();
- return;
- }
- DatabaseEngine.MoveUpdateFiles();
- Title = string.Format("PRS Server Manager (v{0})", CoreUtils.GetVersion());
- Logger.Send(LogType.Information, "", "Doing refresh");
- Servers.Refresh(true, true);
- }
- #region Major Version Update
- private bool CheckForMajorVersionUpdate(int targetMajorVersion)
- {
- var location = $"{_autoUpdateSettings.Location}/v{targetMajorVersion}";
- var result = Update.GetRemoteFile($"{GetChannelLocation(location, allowAlpha: true)}/version.txt");
- if(result.StatusCode != System.Net.HttpStatusCode.OK)
- {
- return false;
- }
- var res = MessageWindow.New()
- .Title($"PRS Version {targetMajorVersion}")
- .Message($"PRS Version {targetMajorVersion} is available!")
- .AddOKButton("Proceed")
- .AddCancelButton()
- .Display()
- .Result;
- if(res != MessageWindowResult.OK)
- {
- return false;
- }
- var installerNameResult = Progress.ShowModal<string?>("Retrieving Update", progress =>
- {
- return Result.Ok(Update.DownloadInstaller(
- location =>
- {
- var result = Update.GetRemoteFile($"{GetChannelLocation(location, allowAlpha: true)}/PRSSetup.exe");
- if(result.StatusCode == System.Net.HttpStatusCode.OK)
- {
- return result.RawBytes;
- }
- else
- {
- return null;
- }
- },
- location,
- "PRSSetup.exe"));
- });
- if(!installerNameResult.GetOk(out var installerName) || installerName is null)
- {
- MessageWindow.ShowMessage($"Could not retrieve installer; cancelling update to version {targetMajorVersion}.", "Download failed.");
- return false;
- }
- Servers.BeforeUpdate();
- if (!UpdateDatabaseFiles.UpdateDatabases(targetMajorVersion))
- {
- return false;
- }
- bool? bOK = null;
- Progress.ShowModal("Launching Update", progress =>
- {
- bOK = Update.RunInstaller(installerName, BeforeUpdate, true, progress);
- });
- if (bOK is null || bOK == true)
- {
- // In this case, the Progress window was closed by the installer, and so we must close our current window.
- return true;
- }
- else
- {
- return false;
- }
- }
- #endregion
- #region Update
- private string GetChannelLocation(string location, bool allowAlpha = false)
- {
- if (allowAlpha)
- {
- return _autoUpdateSettings.Channel switch
- {
- AutoUpdateChannel.Unstable => $"{location}/Alpha",
- AutoUpdateChannel.PreRelease => $"{location}/PreRelease",
- AutoUpdateChannel.Stable or _ => $"{location}/Stable",
- };
- }
- else
- {
- return _autoUpdateSettings.Channel switch
- {
- AutoUpdateChannel.PreRelease or AutoUpdateChannel.Unstable => $"{location}/PreRelease",
- AutoUpdateChannel.Stable or _ => $"{location}/Stable",
- };
- }
- }
- private string GetUpdateLocation()
- {
- return _autoUpdateSettings.Location;
- }
- private string GetLatestVersion(string location)
- {
- return Update.GetRemoteFile($"{GetChannelLocation(location)}/version.txt").Content;
- }
- private string GetReleaseNotes(string location)
- {
- return Update.GetRemoteFile($"{location}/Release Notes.txt").Content;
- }
- private byte[]? GetInstaller(string location)
- {
- return Update.GetRemoteFile($"{GetChannelLocation(location)}/PRSSetup.exe").RawBytes;
- }
- private void BeforeUpdate()
- {
- Servers.BeforeUpdate();
- }
- private bool CheckForUpdates()
- {
- return Update.CheckForUpdates(GetUpdateLocation, GetLatestVersion, GetReleaseNotes, GetInstaller, BeforeUpdate, true, "PRSSetup.exe", true);
- }
- #endregion
- private void Window_Closing(object sender, CancelEventArgs e)
- {
- //if (CoreUtils.GetVersion() == "???")
- // Servers.StopAll();
- }
- }
|