diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-05-16 18:22:47 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-05-16 18:22:47 +0300 |
| commit | b9ff3e2b899b22090b5d206c1f95189aa7fd7ecb (patch) | |
| tree | 2e2851d21fbf8304ff2ada7e3089c342547dd95d /Software/Visual_Studio/Tango.Settings | |
| parent | 62600b93f932aa2751d6f0c00f4699a7eb2d7c7b (diff) | |
| download | Tango-b9ff3e2b899b22090b5d206c1f95189aa7fd7ecb.tar.gz Tango-b9ff3e2b899b22090b5d206c1f95189aa7fd7ecb.zip | |
New Settings Library.
Diffstat (limited to 'Software/Visual_Studio/Tango.Settings')
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/OLD/DataBase.cs (renamed from Software/Visual_Studio/Tango.Settings/DataBase.cs) | 0 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/OLD/Integration.cs (renamed from Software/Visual_Studio/Tango.Settings/Integration.cs) | 0 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/DeveloperModule.cs (renamed from Software/Visual_Studio/Tango.Settings/MachineStudioSettings/DeveloperModule.cs) | 0 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/MachineStudio.cs (renamed from Software/Visual_Studio/Tango.Settings/MachineStudioSettings/MachineStudio.cs) | 0 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/StubsModule.cs (renamed from Software/Visual_Studio/Tango.Settings/MachineStudioSettings/StubsModule.cs) | 0 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/SynchronizationModule.cs (renamed from Software/Visual_Studio/Tango.Settings/MachineStudioSettings/SynchronizationModule.cs) | 0 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/TechnicianModule.cs (renamed from Software/Visual_Studio/Tango.Settings/MachineStudioSettings/TechnicianModule.cs) | 0 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/OLD/SettingsCollection.cs (renamed from Software/Visual_Studio/Tango.Settings/SettingsCollection.cs) | 0 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/OLD/SettingsManager.cs | 162 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/OLD/StubsUI.cs (renamed from Software/Visual_Studio/Tango.Settings/StubsUI.cs) | 0 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/SettingsBase.cs | 28 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/SettingsManager.cs | 190 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/Tango.Settings.csproj | 24 | ||||
| -rw-r--r-- | Software/Visual_Studio/Tango.Settings/packages.config | 4 |
14 files changed, 279 insertions, 129 deletions
diff --git a/Software/Visual_Studio/Tango.Settings/DataBase.cs b/Software/Visual_Studio/Tango.Settings/OLD/DataBase.cs index 22bbfe9a9..22bbfe9a9 100644 --- a/Software/Visual_Studio/Tango.Settings/DataBase.cs +++ b/Software/Visual_Studio/Tango.Settings/OLD/DataBase.cs diff --git a/Software/Visual_Studio/Tango.Settings/Integration.cs b/Software/Visual_Studio/Tango.Settings/OLD/Integration.cs index d670cad8e..d670cad8e 100644 --- a/Software/Visual_Studio/Tango.Settings/Integration.cs +++ b/Software/Visual_Studio/Tango.Settings/OLD/Integration.cs diff --git a/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/DeveloperModule.cs b/Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/DeveloperModule.cs index 837dfcc2e..837dfcc2e 100644 --- a/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/DeveloperModule.cs +++ b/Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/DeveloperModule.cs diff --git a/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/MachineStudio.cs b/Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/MachineStudio.cs index 6895f30a1..6895f30a1 100644 --- a/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/MachineStudio.cs +++ b/Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/MachineStudio.cs diff --git a/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/StubsModule.cs b/Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/StubsModule.cs index b9263237a..b9263237a 100644 --- a/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/StubsModule.cs +++ b/Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/StubsModule.cs diff --git a/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/SynchronizationModule.cs b/Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/SynchronizationModule.cs index c487fdd42..c487fdd42 100644 --- a/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/SynchronizationModule.cs +++ b/Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/SynchronizationModule.cs diff --git a/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/TechnicianModule.cs b/Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/TechnicianModule.cs index 4f3ced8d7..4f3ced8d7 100644 --- a/Software/Visual_Studio/Tango.Settings/MachineStudioSettings/TechnicianModule.cs +++ b/Software/Visual_Studio/Tango.Settings/OLD/MachineStudioSettings/TechnicianModule.cs diff --git a/Software/Visual_Studio/Tango.Settings/SettingsCollection.cs b/Software/Visual_Studio/Tango.Settings/OLD/SettingsCollection.cs index f72e70647..f72e70647 100644 --- a/Software/Visual_Studio/Tango.Settings/SettingsCollection.cs +++ b/Software/Visual_Studio/Tango.Settings/OLD/SettingsCollection.cs diff --git a/Software/Visual_Studio/Tango.Settings/OLD/SettingsManager.cs b/Software/Visual_Studio/Tango.Settings/OLD/SettingsManager.cs new file mode 100644 index 000000000..7bc15d950 --- /dev/null +++ b/Software/Visual_Studio/Tango.Settings/OLD/SettingsManager.cs @@ -0,0 +1,162 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Tango.Logging; +using Tango.Serialization; + +namespace Tango.Settings +{ + /// <summary> + /// Represents a settings manager for loading and saving application settings. + /// </summary> + public static class SettingsManager + { + private static LogManager LogManager = LogManager.Default; + + /// <summary> + /// Gets or sets a value indicating whether the settings manager is initialized. + /// </summary> + public static bool IsInitialized { get; private set; } + + /// <summary> + /// Gets or sets the default settings. + /// </summary> + /// <value> + /// The default. + /// </value> + public static SettingsCollection Default { get; private set; } + + /// <summary> + /// Gets or sets the default file path. + /// </summary> + /// <value> + /// The default file path. + /// </value> + public static String DefaultFilePath { get; private set; } + + /// <summary> + /// Gets or sets the default folder for application settings. + /// </summary> + public static String DefaultFolder { get; private set; } + + /// <summary> + /// Saves application settings to XML file. + /// </summary> + /// <param name="filePath">The path to the XML file.</param> + /// <param name="stCollection">The instance of the SettingsCollection to save.</param> + public static void SaveToXML(String filePath, SettingsCollection stCollection) + { + LogManager.Log("Saving application configuration to " + "'" + filePath + "'"); + XmlDataSerializer serializer = new XmlDataSerializer(); + serializer.SerializeToFile<SettingsCollection>(stCollection, filePath); + } + + /// <summary> + /// Loads application settings from XML file. + /// </summary> + /// <param name="filePath">The file path.</param> + /// <returns></returns> + public static SettingsCollection LoadFromXML(String filePath) + { + LogManager.Log("Loading application configuration from " + "'" + filePath + "'"); + XmlDataSerializer serialier = new XmlDataSerializer(); + return serialier.DeserializeFromFile<SettingsCollection>(filePath); + } + + /// <summary> + /// Saves the default settings. + /// </summary> + public static void SaveDefaultSettings() + { + SaveToXML(DefaultFilePath, Default); + } + + /// <summary> + /// Restores to default. + /// </summary> + public static void RestoreToDefault() + { + Default = new SettingsCollection(); + SaveDefaultSettings(); + } + + /// <summary> + /// Initializes the <see cref="SettingsManager"/> class. + /// </summary> + static SettingsManager() + { + Initialize(); + } + + /// <summary> + /// Initializes the settings manager. + /// </summary> + private static void Initialize() + { + if (IsInitialized) return; + + IsInitialized = true; + + LogManager.Log("Initializing application configuration..."); + + DefaultFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Settings.xml"); + DefaultFolder = Path.GetDirectoryName(DefaultFilePath); + + bool waited = false; + + Retry: + + try + { + Default = LoadFromXML(DefaultFilePath); + } + catch (Exception ex) + { + if (!waited) + { + LogManager.Log(ex, "Could not load application configuration. Retrying in 1 second..."); + waited = true; + Thread.Sleep(1000); + goto Retry; + } + else + { + LogManager.Log(ex, "Could not load application configuration."); + } + + try + { + LogManager.Log("Creating application configuration directory structure..."); + Directory.CreateDirectory(DefaultFolder); + } + catch (Exception ex2) + { + LogManager.Log(ex2, "Could not generate directory structure."); + } + } + + if (Default == null) + { + Default = new SettingsCollection(); + + try + { + LogManager.Log("Generating default configuration file."); + SaveToXML(DefaultFilePath, Default); + } + catch (Exception ex) + { + LogManager.Log(ex, "Could not generate default configuration file."); + } + } + else + { + LogManager.Log("Application configuration loaded successfully."); + } + } + } +} diff --git a/Software/Visual_Studio/Tango.Settings/StubsUI.cs b/Software/Visual_Studio/Tango.Settings/OLD/StubsUI.cs index 9944ffc11..9944ffc11 100644 --- a/Software/Visual_Studio/Tango.Settings/StubsUI.cs +++ b/Software/Visual_Studio/Tango.Settings/OLD/StubsUI.cs diff --git a/Software/Visual_Studio/Tango.Settings/SettingsBase.cs b/Software/Visual_Studio/Tango.Settings/SettingsBase.cs new file mode 100644 index 000000000..29ec767c9 --- /dev/null +++ b/Software/Visual_Studio/Tango.Settings/SettingsBase.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Settings +{ + /// <summary> + /// Represents a settings object base class. + /// </summary> + public abstract class SettingsBase + { + internal Action SaveAction { get; set; } + + /// <summary> + /// Saves settings. + /// </summary> + /// <exception cref="System.InvalidOperationException">This settings instance is not registered with any settings manager.</exception> + public virtual void Save() + { + if (SaveAction == null) + { + throw new InvalidOperationException("This settings instance is not registered with any settings manager."); + } + } + } +} diff --git a/Software/Visual_Studio/Tango.Settings/SettingsManager.cs b/Software/Visual_Studio/Tango.Settings/SettingsManager.cs index 7bc15d950..1f0083bb5 100644 --- a/Software/Visual_Studio/Tango.Settings/SettingsManager.cs +++ b/Software/Visual_Studio/Tango.Settings/SettingsManager.cs @@ -1,162 +1,128 @@ -using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; -using System.Threading; using System.Threading.Tasks; -using Tango.Logging; -using Tango.Serialization; namespace Tango.Settings { /// <summary> - /// Represents a settings manager for loading and saving application settings. + /// Represents a settings manager capable of holding a collection of settings objects, saving and loading them using JSON. /// </summary> - public static class SettingsManager + public class SettingsManager { - private static LogManager LogManager = LogManager.Default; + #region Singleton + private static SettingsManager _default; /// <summary> - /// Gets or sets a value indicating whether the settings manager is initialized. + /// Gets the default settings manager instance. /// </summary> - public static bool IsInitialized { get; private set; } + public static SettingsManager Default + { + get + { + if (_default == null) + { + _default = new SettingsManager(); + } - /// <summary> - /// Gets or sets the default settings. - /// </summary> - /// <value> - /// The default. - /// </value> - public static SettingsCollection Default { get; private set; } + return _default; + } + } - /// <summary> - /// Gets or sets the default file path. - /// </summary> - /// <value> - /// The default file path. - /// </value> - public static String DefaultFilePath { get; private set; } + #endregion - /// <summary> - /// Gets or sets the default folder for application settings. - /// </summary> - public static String DefaultFolder { get; private set; } + private List<SettingsBase> _settingsCollection; + private JsonSerializerSettings _jsonSettings; + private bool _loaded; /// <summary> - /// Saves application settings to XML file. + /// Gets or sets the settings file path. /// </summary> - /// <param name="filePath">The path to the XML file.</param> - /// <param name="stCollection">The instance of the SettingsCollection to save.</param> - public static void SaveToXML(String filePath, SettingsCollection stCollection) - { - LogManager.Log("Saving application configuration to " + "'" + filePath + "'"); - XmlDataSerializer serializer = new XmlDataSerializer(); - serializer.SerializeToFile<SettingsCollection>(stCollection, filePath); - } + public String FilePath { get; protected set; } /// <summary> - /// Loads application settings from XML file. + /// Gets or sets the settings folder. /// </summary> - /// <param name="filePath">The file path.</param> - /// <returns></returns> - public static SettingsCollection LoadFromXML(String filePath) - { - LogManager.Log("Loading application configuration from " + "'" + filePath + "'"); - XmlDataSerializer serialier = new XmlDataSerializer(); - return serialier.DeserializeFromFile<SettingsCollection>(filePath); - } + public String Folder { get; protected set; } /// <summary> - /// Saves the default settings. + /// Prevents a default instance of the <see cref="SettingsManager"/> class from being created. /// </summary> - public static void SaveDefaultSettings() + private SettingsManager() { - SaveToXML(DefaultFilePath, Default); - } + FilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Settings.json"); + Folder = Path.GetDirectoryName(FilePath); + _settingsCollection = new List<SettingsBase>(); - /// <summary> - /// Restores to default. - /// </summary> - public static void RestoreToDefault() - { - Default = new SettingsCollection(); - SaveDefaultSettings(); + _jsonSettings = new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All, + Formatting = Formatting.Indented, + }; + + _jsonSettings.Converters.Add(new StringEnumConverter(false)); } - /// <summary> - /// Initializes the <see cref="SettingsManager"/> class. - /// </summary> - static SettingsManager() + private void EnsureLoaded() { - Initialize(); + if (!_loaded) + { + Load(); + } } /// <summary> - /// Initializes the settings manager. + /// Gets or creates the specified settings object type. /// </summary> - private static void Initialize() + /// <typeparam name="T"></typeparam> + /// <returns></returns> + public T GetOrCreate<T>() where T : SettingsBase { - if (IsInitialized) return; - - IsInitialized = true; + EnsureLoaded(); - LogManager.Log("Initializing application configuration..."); + var settings = _settingsCollection.SingleOrDefault(x => x.GetType() == typeof(T)) as T; - DefaultFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Settings.xml"); - DefaultFolder = Path.GetDirectoryName(DefaultFilePath); - - bool waited = false; - - Retry: - - try + if (settings == null) { - Default = LoadFromXML(DefaultFilePath); + settings = Activator.CreateInstance<T>(); + settings.SaveAction = Save; + _settingsCollection.Add(settings); } - catch (Exception ex) - { - if (!waited) - { - LogManager.Log(ex, "Could not load application configuration. Retrying in 1 second..."); - waited = true; - Thread.Sleep(1000); - goto Retry; - } - else - { - LogManager.Log(ex, "Could not load application configuration."); - } - try - { - LogManager.Log("Creating application configuration directory structure..."); - Directory.CreateDirectory(DefaultFolder); - } - catch (Exception ex2) - { - LogManager.Log(ex2, "Could not generate directory structure."); - } - } + return settings; + } - if (Default == null) + /// <summary> + /// Loads the settings from the <see cref="FilePath"/>. + /// </summary> + protected virtual void Load() + { + if (File.Exists(FilePath)) { - Default = new SettingsCollection(); + _settingsCollection = JsonConvert.DeserializeObject<List<SettingsBase>>(File.ReadAllText(FilePath), _jsonSettings); - try + foreach (var settings in _settingsCollection) { - LogManager.Log("Generating default configuration file."); - SaveToXML(DefaultFilePath, Default); + settings.SaveAction = Save; } - catch (Exception ex) - { - LogManager.Log(ex, "Could not generate default configuration file."); - } - } - else - { - LogManager.Log("Application configuration loaded successfully."); } + + _loaded = true; + } + + /// <summary> + /// Saves settings. + /// </summary> + public virtual void Save() + { + EnsureLoaded(); + + String json = JsonConvert.SerializeObject(_settingsCollection, _jsonSettings); + File.WriteAllText(FilePath, json); } } } diff --git a/Software/Visual_Studio/Tango.Settings/Tango.Settings.csproj b/Software/Visual_Studio/Tango.Settings/Tango.Settings.csproj index fa9eb1f61..6cfa77c3a 100644 --- a/Software/Visual_Studio/Tango.Settings/Tango.Settings.csproj +++ b/Software/Visual_Studio/Tango.Settings/Tango.Settings.csproj @@ -30,6 +30,9 @@ <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> + <Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> + <HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> @@ -43,31 +46,18 @@ <Compile Include="..\Versioning\GlobalVersionInfo.cs"> <Link>GlobalVersionInfo.cs</Link> </Compile> - <Compile Include="DataBase.cs" /> - <Compile Include="Integration.cs" /> - <Compile Include="MachineStudioSettings\DeveloperModule.cs" /> - <Compile Include="MachineStudioSettings\MachineStudio.cs" /> - <Compile Include="MachineStudioSettings\StubsModule.cs" /> - <Compile Include="MachineStudioSettings\SynchronizationModule.cs" /> - <Compile Include="MachineStudioSettings\TechnicianModule.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> - <Compile Include="SettingsCollection.cs" /> + <Compile Include="SettingsBase.cs" /> <Compile Include="SettingsManager.cs" /> - <Compile Include="StubsUI.cs" /> </ItemGroup> <ItemGroup> - <ProjectReference Include="..\Tango.Core\Tango.Core.csproj"> - <Project>{a34ee0f0-649d-41c8-8489-b6f1cc6924ee}</Project> - <Name>Tango.Core</Name> - </ProjectReference> <ProjectReference Include="..\Tango.Logging\Tango.Logging.csproj"> <Project>{bc932dbd-7cdb-488c-99e4-f02cf441f55e}</Project> <Name>Tango.Logging</Name> </ProjectReference> - <ProjectReference Include="..\Tango.Serialization\Tango.Serialization.csproj"> - <Project>{22f87980-e990-4686-be81-be63d562c4d5}</Project> - <Name>Tango.Serialization</Name> - </ProjectReference> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> </Project>
\ No newline at end of file diff --git a/Software/Visual_Studio/Tango.Settings/packages.config b/Software/Visual_Studio/Tango.Settings/packages.config new file mode 100644 index 000000000..92167d083 --- /dev/null +++ b/Software/Visual_Studio/Tango.Settings/packages.config @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="Newtonsoft.Json" version="8.0.3" targetFramework="net46" /> +</packages>
\ No newline at end of file |
