diff --git a/TGS.Server/App.config b/TGS.Server/App.config index 91aa70ce1c..892916e112 100644 --- a/TGS.Server/App.config +++ b/TGS.Server/App.config @@ -1,27 +1,8 @@  - -
- - - - - C:\Python27 - - - True - - - 7 - - - 38607 - - - diff --git a/TGS.Server/DeprecatedInstanceConfig.cs b/TGS.Server/DeprecatedInstanceConfig.cs index 091a43f39b..3297e7423b 100644 --- a/TGS.Server/DeprecatedInstanceConfig.cs +++ b/TGS.Server/DeprecatedInstanceConfig.cs @@ -66,7 +66,7 @@ namespace TGS.Server //Which is fucking retarded //This hooks into the settings provider and forces it to load it anyway var Config = Properties.Settings.Default; - var Provider = Config.Properties[nameof(Config.SettingsVersion)].Provider; //nameof for sanity + var Provider = Config.Properties["SettingsVersion"].Provider; var sp = new SettingsProperty(property) { diff --git a/TGS.Server/Instance/Repository.cs b/TGS.Server/Instance/Repository.cs index 6c2486193d..277127bc33 100644 --- a/TGS.Server/Instance/Repository.cs +++ b/TGS.Server/Instance/Repository.cs @@ -1271,9 +1271,8 @@ namespace TGS.Server return null; } - var Config = Properties.Settings.Default; - - var PythonFile = Path.Combine(Config.PythonPath, "python.exe"); + var pp = Server.Config.PythonPath; + var PythonFile = Path.Combine(pp, "python.exe"); if (!File.Exists(PythonFile)) { error = "Cannot locate python!"; @@ -1308,7 +1307,7 @@ namespace TGS.Server } //update pip deps and try again - string PipFile = Config.PythonPath + "/scripts/pip.exe"; + string PipFile = Path.Combine(pp, "scripts", "pip.exe"); foreach(var I in RConfig.PipDependancies) using (var pip = new Process()) { diff --git a/TGS.Server/Properties/Settings.Designer.cs b/TGS.Server/Properties/Settings.Designer.cs index 49ea4aae51..c667781590 100644 --- a/TGS.Server/Properties/Settings.Designer.cs +++ b/TGS.Server/Properties/Settings.Designer.cs @@ -22,64 +22,5 @@ namespace TGS.Server.Properties { return defaultInstance; } } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("C:\\Python27")] - public string PythonPath { - get { - return ((string)(this["PythonPath"])); - } - set { - this["PythonPath"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("True")] - public bool UpgradeRequired { - get { - return ((bool)(this["UpgradeRequired"])); - } - set { - this["UpgradeRequired"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("7")] - public int SettingsVersion { - get { - return ((int)(this["SettingsVersion"])); - } - set { - this["SettingsVersion"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("38607")] - public ushort RemoteAccessPort { - get { - return ((ushort)(this["RemoteAccessPort"])); - } - set { - this["RemoteAccessPort"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - public global::System.Collections.Specialized.StringCollection InstancePaths { - get { - return ((global::System.Collections.Specialized.StringCollection)(this["InstancePaths"])); - } - set { - this["InstancePaths"] = value; - } - } } } diff --git a/TGS.Server/Properties/Settings.settings b/TGS.Server/Properties/Settings.settings index d3f9248e61..8e615f25fd 100644 --- a/TGS.Server/Properties/Settings.settings +++ b/TGS.Server/Properties/Settings.settings @@ -1,21 +1,5 @@  - + - - - C:\Python27 - - - True - - - 7 - - - 38607 - - - - - + \ No newline at end of file diff --git a/TGS.Server/Server.cs b/TGS.Server/Server.cs index 83980bef48..23f8d9d2b1 100644 --- a/TGS.Server/Server.cs +++ b/TGS.Server/Server.cs @@ -22,6 +22,11 @@ namespace TGS.Server /// public const byte LoggingID = 0; + /// + /// The directory to use when importing a .NET settings based config + /// + public const string MigrationConfigDirectory = "C:\\TGSSettingUpgradeTempDir"; + /// /// The service version based on the /// @@ -32,6 +37,16 @@ namespace TGS.Server /// public static ILogger Logger { get; private set; } + /// + /// The for the + /// + public static ServerConfig Config { get; private set; } + + /// + /// The directory to load and save s to + /// + static readonly string DefaultConfigDirectory = Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "TGS.Server")).FullName; + /// /// Cancels WCF's user impersonation to allow clean access to writing log files /// @@ -87,23 +102,6 @@ namespace TGS.Server OnlineAllHosts(); } - /// - /// Migrates the .NET config from to + 1 - /// - /// The version to migrate from - void MigrateSettings(int oldVersion) - { - var Config = Properties.Settings.Default; - switch (oldVersion) - { - case 6: //switch to per-instance configs - var IC = DeprecatedInstanceConfig.CreateFromNETSettings(); - IC.Save(); - Config.InstancePaths.Add(IC.Directory); - break; - } - } - /// /// Enumerates configured s. Detaches those that fail to load /// @@ -113,7 +111,7 @@ namespace TGS.Server var pathsToRemove = new List(); lock (this) { - var IPS = Properties.Settings.Default.InstancePaths; + var IPS = Config.InstancePaths; foreach (var I in IPS) { IInstanceConfig ic; @@ -135,13 +133,11 @@ namespace TGS.Server } /// - /// Overrides and saves the configured if requested by command line parameters + /// Overrides and saves the configured if requested by command line parameters /// /// The command line parameters for the void ChangePortFromCommandLine(string[] args) { - var Config = Properties.Settings.Default; - for (var I = 0; I < args.Length - 1; ++I) if (args[I].ToLower() == "-port") { @@ -156,45 +152,34 @@ namespace TGS.Server { throw new Exception("Invalid argument for \"-port\"", e); } - Config.Save(); + Config.Save(DefaultConfigDirectory); break; } } - /// - /// Writes some changes to the that always need to be done. - /// - void PrePrepConfig() - { - var Config = Properties.Settings.Default; - - if (Config.InstancePaths == null) - Config.InstancePaths = new StringCollection(); - } - /// /// Upgrades up the service configuration /// void SetupConfig() { - var Config = Properties.Settings.Default; - if (Config.UpgradeRequired) + try { - var newVersion = Config.SettingsVersion; - Config.Upgrade(); - - PrePrepConfig(); - - for (var oldVersion = Config.SettingsVersion; oldVersion < newVersion; ++oldVersion) - MigrateSettings(oldVersion); - - Config.SettingsVersion = newVersion; - - Config.UpgradeRequired = false; - Config.Save(); + Config = ServerConfig.Load(DefaultConfigDirectory); + } + catch (FileNotFoundException) + { + try + { + //assume we're upgrading + Config = ServerConfig.Load(MigrationConfigDirectory); + Directory.Delete(MigrationConfigDirectory, true); + } + catch(FileNotFoundException) + { + //new baby + Config = new ServerConfig(); + } } - else - PrePrepConfig(); } /// @@ -219,21 +204,21 @@ namespace TGS.Server } /// - /// Creates a for using the default pipe, CloseTimeout for the , and the configured + /// Creates a for using the default pipe, CloseTimeout for the , and the configured /// /// The /// The URL to access components on the /// The created static ServiceHost CreateHost(object singleton, string endpointPostfix) { - return new ServiceHost(singleton, new Uri[] { new Uri(String.Format("net.pipe://localhost/{0}", endpointPostfix)), new Uri(String.Format("https://localhost:{0}/{1}", Properties.Settings.Default.RemoteAccessPort, endpointPostfix)) }) + return new ServiceHost(singleton, new Uri[] { new Uri(String.Format("net.pipe://localhost/{0}", endpointPostfix)), new Uri(String.Format("https://localhost:{0}/{1}", Config.RemoteAccessPort, endpointPostfix)) }) { CloseTimeout = new TimeSpan(0, 0, 5) }; } /// - /// Creates s for all s as listed in , detaches bad ones + /// Creates s for all s as listed in , detaches bad ones /// void SetupInstances() { @@ -253,7 +238,7 @@ namespace TGS.Server seenNames.Add(I.Name); } foreach (var I in pathsToRemove) - Properties.Settings.Default.InstancePaths.Remove(I); + Config.InstancePaths.Remove(I); } /// @@ -371,7 +356,7 @@ namespace TGS.Server } serviceHost.Close(); } - Properties.Settings.Default.Save(); + Config.Save(DefaultConfigDirectory); } /// @@ -387,7 +372,7 @@ namespace TGS.Server /// public ushort RemoteAccessPort() { - return Properties.Settings.Default.RemoteAccessPort; + return Config.RemoteAccessPort; } /// @@ -395,7 +380,7 @@ namespace TGS.Server { if (port == 0) return "Cannot bind to port 0"; - Properties.Settings.Default.RemoteAccessPort = port; + Config.RemoteAccessPort = port; return null; } @@ -410,14 +395,14 @@ namespace TGS.Server { if (!Directory.Exists(path)) return false; - Properties.Settings.Default.PythonPath = Path.GetFullPath(path); + Config.PythonPath = Path.GetFullPath(path); return true; } /// public string PythonPath() { - return Properties.Settings.Default.PythonPath; + return Config.PythonPath; } /// @@ -445,7 +430,6 @@ namespace TGS.Server return res; if (File.Exists(path) || Directory.Exists(path)) return "Cannot create instance at pre-existing path!"; - var Config = Properties.Settings.Default; lock (this) { if (Config.InstancePaths.Contains(path)) @@ -462,7 +446,7 @@ namespace TGS.Server }; Directory.CreateDirectory(path); ic.Save(); - Properties.Settings.Default.InstancePaths.Add(path); + Config.InstancePaths.Add(path); } catch (Exception e) { @@ -488,7 +472,7 @@ namespace TGS.Server host.Open(); else lock (this) - Properties.Settings.Default.InstancePaths.Remove(config.Directory); + Config.InstancePaths.Remove(config.Directory); return null; } catch (Exception e) @@ -501,7 +485,6 @@ namespace TGS.Server public string ImportInstance(string path) { path = Helpers.NormalizePath(path); - var Config = Properties.Settings.Default; lock (this) { if (Config.InstancePaths.Contains(path)) @@ -516,7 +499,7 @@ namespace TGS.Server if(ic.Name == oic.Name) return String.Format("Instance named {0} already exists!", oic.Name); ic.Save(); - Properties.Settings.Default.InstancePaths.Add(path); + Config.InstancePaths.Add(path); } catch (Exception e) { @@ -652,7 +635,7 @@ namespace TGS.Server } if (path == null) return String.Format("No instance named {0} exists!", name); - Properties.Settings.Default.InstancePaths.Remove(path); + Config.InstancePaths.Remove(path); return null; } } diff --git a/TGS.Server/ServerConfig.cs b/TGS.Server/ServerConfig.cs new file mode 100644 index 0000000000..b294a85767 --- /dev/null +++ b/TGS.Server/ServerConfig.cs @@ -0,0 +1,65 @@ +using System.Collections.Generic; +using System.IO; +using System.Web.Script.Serialization; + +namespace TGS.Server +{ + /// + /// Class for storing server wide settings + /// + public sealed class ServerConfig + { + /// + /// The filename to save the as + /// + [ScriptIgnore] + const string JSONFilename = "ServerConfig.json"; + /// + /// The most recent version of the config file + /// + [ScriptIgnore] + const ulong CurrentVersion = 8; + + /// + /// The version of the + /// + public ulong Version { get; private set; } = CurrentVersion; + + /// + /// List of paths that contain s + /// + public List InstancePaths { get; private set; } = new List(); + + /// + /// Port used to access the remotely + /// + public ushort RemoteAccessPort { get; set; } = 38607; + + /// + /// Path to the directory containing the Python2.7 installation + /// + public string PythonPath { get; set; } = "C:\\Python27"; + + /// + /// Saves the to a target + /// + /// The directory in which to save the + public void Save(string directory) + { + var data = new JavaScriptSerializer().Serialize(this); + var path = Path.Combine(directory, JSONFilename); + File.WriteAllText(path, data); + } + + /// + /// Load a from a given + /// + /// The directory containing the + /// The loaded + public static ServerConfig Load(string directory) + { + var configtext = File.ReadAllText(Path.Combine(directory, JSONFilename)); + return new JavaScriptSerializer().Deserialize(configtext); + } + } +} diff --git a/TGS.Server/TGS.Server.csproj b/TGS.Server/TGS.Server.csproj index ff1f370ebc..570fbb8cf0 100644 --- a/TGS.Server/TGS.Server.csproj +++ b/TGS.Server/TGS.Server.csproj @@ -120,6 +120,7 @@ +