using System;
using System.Configuration;
namespace TGServerService
{
///
/// Used to migrate old config settings
///
class DeprecatedInstanceConfig : InstanceConfig
{
///
/// The default directory
///
const string DefaultInstallationPath = "C:\\tgstation-server-3";
///
/// Convert the settings version 6 .NET settings file to a config json
///
/// An based off the old .NET setting file
public static IInstanceConfig CreateFromNETSettings()
{
var Config = Properties.Settings.Default;
var result = new DeprecatedInstanceConfig(Program.NormalizePath(LoadPreviousNetPropertyOrDefault("ServerDirectory", "C:\\tgstation-server-3")));
// using nameof for sanity where possible
result.ProjectName = LoadPreviousNetPropertyOrDefault(nameof(ProjectName), result.ProjectName);
result.Port = LoadPreviousNetPropertyOrDefault("ServerPort", result.Port);
result.CommitterName = LoadPreviousNetPropertyOrDefault(nameof(CommitterName), result.CommitterName);
result.CommitterEmail = LoadPreviousNetPropertyOrDefault(nameof(CommitterEmail), result.CommitterEmail);
result.Security = LoadPreviousNetPropertyOrDefault("ServerSecurity", result.Security);
result.Autostart = LoadPreviousNetPropertyOrDefault("DDAutoStart", result.Autostart);
result.ChatProviderData = LoadPreviousNetPropertyOrDefault(nameof(ChatProviderData), result.ChatProviderData);
result.ChatProviderEntropy = LoadPreviousNetPropertyOrDefault(nameof(ChatProviderEntropy), result.ChatProviderEntropy);
result.ReattachRequired = LoadPreviousNetPropertyOrDefault("ReattachToDD", result.ReattachRequired);
result.ReattachProcessID = LoadPreviousNetPropertyOrDefault("ReattachPID", result.ReattachProcessID);
result.ReattachPort = LoadPreviousNetPropertyOrDefault(nameof(ReattachPort), result.ReattachPort);
result.ReattachCommsKey = LoadPreviousNetPropertyOrDefault(nameof(ReattachCommsKey), result.ReattachCommsKey);
result.ReattachAPIVersion = LoadPreviousNetPropertyOrDefault(nameof(ReattachAPIVersion), result.ReattachAPIVersion);
result.AutoUpdateInterval = LoadPreviousNetPropertyOrDefault(nameof(AutoUpdateInterval), result.AutoUpdateInterval);
result.AuthorizedUserGroupSID = LoadPreviousNetPropertyOrDefault("AuthorizedGroupSID", result.AuthorizedUserGroupSID);
result.MigrateToCurrentVersion();
return result;
}
///
/// Loads a previous .NET config and returns it or some if it wasn't set
///
/// The type of the
/// The config key of the property
/// The default value of the
/// The if it exists, otherwise the
static T LoadPreviousNetPropertyOrDefault(string property, T defaultValue)
{
//try it the simple way first
try
{
var result = (T)Properties.Settings.Default.GetPreviousVersion(property);
return result == null ? defaultValue : result;
}
catch
{
try
{
//.NET is fucking stupid
//If we don't have the correct property in our *CURRENT* .settings file it will automatically throw an exception when it tries to load it
//Which means we can never fucking delete config settings
//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 sp = new SettingsProperty(property)
{
PropertyType = typeof(T),
DefaultValue = defaultValue,
Provider = Provider
};
var ProviderInterface = Provider as IApplicationSettingsProvider;
var result = ProviderInterface.GetPreviousVersion(Config.Context, sp);
if (result != null && result.PropertyValue != null)
return (T)result.PropertyValue;
}
catch { }
//f u c k i t
return defaultValue;
}
}
///
/// Construct a . Used by the deserializer
///
[Obsolete("This method is for use by the deserializer only.", true)]
public DeprecatedInstanceConfig() : base(DefaultInstallationPath) { }
///
/// Construct a for a at
///
/// The path to the
public DeprecatedInstanceConfig(string path) : base(path) { }
///
/// Migrates the from to
///
public void MigrateToCurrentVersion()
{
for (; Version < CurrentVersion; ++Version)
Migrate();
}
///
/// Migrates the from to + 1
///
void Migrate()
{
//Not needed so far
}
}
}