Move away from .NET settings in service

This commit is contained in:
Cyberboss
2017-11-13 01:19:46 -05:00
parent 680ec3dc62
commit ce075dcdc7
8 changed files with 119 additions and 165 deletions
-19
View File
@@ -1,27 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="TGServerService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<userSettings>
<TGServerService.Properties.Settings>
<setting name="PythonPath" serializeAs="String">
<value>C:\Python27</value>
</setting>
<setting name="UpgradeRequired" serializeAs="String">
<value>True</value>
</setting>
<setting name="SettingsVersion" serializeAs="String">
<value>7</value>
</setting>
<setting name="RemoteAccessPort" serializeAs="String">
<value>38607</value>
</setting>
</TGServerService.Properties.Settings>
</userSettings>
</configuration>
+1 -1
View File
@@ -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)
{
+3 -4
View File
@@ -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())
{
-59
View File
@@ -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;
}
}
}
}
+2 -18
View File
@@ -1,21 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="TGS.Server.Properties" GeneratedClassName="Settings">
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
<Profiles />
<Settings>
<Setting Name="PythonPath" Type="System.String" Scope="User">
<Value Profile="(Default)">C:\Python27</Value>
</Setting>
<Setting Name="UpgradeRequired" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="SettingsVersion" Type="System.Int32" Scope="User">
<Value Profile="(Default)">7</Value>
</Setting>
<Setting Name="RemoteAccessPort" Type="System.UInt16" Scope="User">
<Value Profile="(Default)">38607</Value>
</Setting>
<Setting Name="InstancePaths" Type="System.Collections.Specialized.StringCollection" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
<Settings />
</SettingsFile>
+47 -64
View File
@@ -22,6 +22,11 @@ namespace TGS.Server
/// </summary>
public const byte LoggingID = 0;
/// <summary>
/// The directory to use when importing a .NET settings based config
/// </summary>
public const string MigrationConfigDirectory = "C:\\TGSSettingUpgradeTempDir";
/// <summary>
/// The service version <see cref="string"/> based on the <see cref="FileVersionInfo"/>
/// </summary>
@@ -32,6 +37,16 @@ namespace TGS.Server
/// </summary>
public static ILogger Logger { get; private set; }
/// <summary>
/// The <see cref="ServerConfig"/> for the <see cref="Server"/>
/// </summary>
public static ServerConfig Config { get; private set; }
/// <summary>
/// The directory to load and save <see cref="ServerConfig"/>s to
/// </summary>
static readonly string DefaultConfigDirectory = Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "TGS.Server")).FullName;
/// <summary>
/// Cancels WCF's user impersonation to allow clean access to writing log files
/// </summary>
@@ -87,23 +102,6 @@ namespace TGS.Server
OnlineAllHosts();
}
/// <summary>
/// Migrates the .NET config from <paramref name="oldVersion"/> to <paramref name="oldVersion"/> + 1
/// </summary>
/// <param name="oldVersion">The version to migrate from</param>
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;
}
}
/// <summary>
/// Enumerates configured <see cref="IInstanceConfig"/>s. Detaches those that fail to load
/// </summary>
@@ -113,7 +111,7 @@ namespace TGS.Server
var pathsToRemove = new List<string>();
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
}
/// <summary>
/// Overrides and saves the configured <see cref="Properties.Settings.RemoteAccessPort"/> if requested by command line parameters
/// Overrides and saves the configured <see cref="ServerConfig.RemoteAccessPort"/> if requested by command line parameters
/// </summary>
/// <param name="args">The command line parameters for the <see cref="Server"/></param>
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;
}
}
/// <summary>
/// Writes some changes to the <see cref="Properties.Settings"/> that always need to be done.
/// </summary>
void PrePrepConfig()
{
var Config = Properties.Settings.Default;
if (Config.InstancePaths == null)
Config.InstancePaths = new StringCollection();
}
/// <summary>
/// Upgrades up the service configuration
/// </summary>
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();
}
/// <summary>
@@ -219,21 +204,21 @@ namespace TGS.Server
}
/// <summary>
/// Creates a <see cref="ServiceHost"/> for <paramref name="singleton"/> using the default pipe, CloseTimeout for the <see cref="ServiceHost"/>, and the configured <see cref="Properties.Settings.RemoteAccessPort"/>
/// Creates a <see cref="ServiceHost"/> for <paramref name="singleton"/> using the default pipe, CloseTimeout for the <see cref="ServiceHost"/>, and the configured <see cref="ServerConfig.RemoteAccessPort"/>
/// </summary>
/// <param name="singleton">The <see cref="ServiceHost.SingletonInstance"/></param>
/// <param name="endpointPostfix">The URL to access components on the <see cref="ServiceHost"/></param>
/// <returns>The created <see cref="ServiceHost"/></returns>
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)
};
}
/// <summary>
/// Creates <see cref="ServiceHost"/>s for all <see cref="Instance"/>s as listed in <see cref="Properties.Settings.InstancePaths"/>, detaches bad ones
/// Creates <see cref="ServiceHost"/>s for all <see cref="Instance"/>s as listed in <see cref="ServerConfig.InstancePaths"/>, detaches bad ones
/// </summary>
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);
}
/// <summary>
@@ -371,7 +356,7 @@ namespace TGS.Server
}
serviceHost.Close();
}
Properties.Settings.Default.Save();
Config.Save(DefaultConfigDirectory);
}
/// <inheritdoc />
@@ -387,7 +372,7 @@ namespace TGS.Server
/// <inheritdoc />
public ushort RemoteAccessPort()
{
return Properties.Settings.Default.RemoteAccessPort;
return Config.RemoteAccessPort;
}
/// <inheritdoc />
@@ -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;
}
/// <inheritdoc />
public string PythonPath()
{
return Properties.Settings.Default.PythonPath;
return Config.PythonPath;
}
/// <inheritdoc />
@@ -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;
}
}
+65
View File
@@ -0,0 +1,65 @@
using System.Collections.Generic;
using System.IO;
using System.Web.Script.Serialization;
namespace TGS.Server
{
/// <summary>
/// Class for storing server wide settings
/// </summary>
public sealed class ServerConfig
{
/// <summary>
/// The filename to save the <see cref="ServerConfig"/> as
/// </summary>
[ScriptIgnore]
const string JSONFilename = "ServerConfig.json";
/// <summary>
/// The most recent version of the config file
/// </summary>
[ScriptIgnore]
const ulong CurrentVersion = 8;
/// <summary>
/// The version of the <see cref="ServerConfig"/>
/// </summary>
public ulong Version { get; private set; } = CurrentVersion;
/// <summary>
/// List of paths that contain <see cref="Instance"/>s
/// </summary>
public List<string> InstancePaths { get; private set; } = new List<string>();
/// <summary>
/// Port used to access the <see cref="Server"/> remotely
/// </summary>
public ushort RemoteAccessPort { get; set; } = 38607;
/// <summary>
/// Path to the directory containing the Python2.7 installation
/// </summary>
public string PythonPath { get; set; } = "C:\\Python27";
/// <summary>
/// Saves the <see cref="ServerConfig"/> to a target <paramref name="directory"/>
/// </summary>
/// <param name="directory">The directory in which to save the <see cref="ServerConfig"/></param>
public void Save(string directory)
{
var data = new JavaScriptSerializer().Serialize(this);
var path = Path.Combine(directory, JSONFilename);
File.WriteAllText(path, data);
}
/// <summary>
/// Load a <see cref="ServerConfig"/> from a given <paramref name="directory"/>
/// </summary>
/// <param name="directory">The directory containing the <see cref="JSONFilename"/></param>
/// <returns>The loaded <see cref="ServerConfig"/></returns>
public static ServerConfig Load(string directory)
{
var configtext = File.ReadAllText(Path.Combine(directory, JSONFilename));
return new JavaScriptSerializer().Deserialize<ServerConfig>(configtext);
}
}
}
+1
View File
@@ -120,6 +120,7 @@
<Compile Include="Server.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="..\AssemblyInfo.global.cs" />
<Compile Include="ServerConfig.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config">