From a3ce27da5f2f98c5af97703c1f442592f2d4e1cf Mon Sep 17 00:00:00 2001 From: morkt Date: Mon, 8 Jan 2018 19:55:28 +0400 Subject: [PATCH] (GameRes): persistent resource settings infrastructure. --- GUI/App.xaml.cs | 1 + GameRes/FormatCatalog.cs | 5 ++ GameRes/GameRes.cs | 5 +- GameRes/GameRes.csproj | 12 ++++ GameRes/ImageBMP.cs | 15 ++++- GameRes/Properties/Settings.Designer.cs | 38 ++++++++++++ GameRes/Properties/Settings.cs | 28 +++++++++ GameRes/Properties/Settings.settings | 9 +++ GameRes/ResourceSettings.cs | 79 +++++++++++++++++++++++++ GameRes/app.config | 15 +++++ 10 files changed, 203 insertions(+), 4 deletions(-) create mode 100644 GameRes/Properties/Settings.Designer.cs create mode 100644 GameRes/Properties/Settings.cs create mode 100644 GameRes/Properties/Settings.settings create mode 100644 GameRes/ResourceSettings.cs create mode 100644 GameRes/app.config diff --git a/GUI/App.xaml.cs b/GUI/App.xaml.cs index d21504da..edb9447f 100644 --- a/GUI/App.xaml.cs +++ b/GUI/App.xaml.cs @@ -110,6 +110,7 @@ namespace GARbro.GUI { try { + FormatCatalog.Instance.SaveSettings(); Settings.Default.Save(); } catch (Exception X) diff --git a/GameRes/FormatCatalog.cs b/GameRes/FormatCatalog.cs index 9135d527..8d62ee28 100644 --- a/GameRes/FormatCatalog.cs +++ b/GameRes/FormatCatalog.cs @@ -107,6 +107,11 @@ namespace GameRes } } + public void SaveSettings () + { + Properties.Settings.Default.Save(); + } + private void AddResourceImpl (IEnumerable formats, CompositionContainer container) { foreach (var impl in formats) diff --git a/GameRes/GameRes.cs b/GameRes/GameRes.cs index ea3ef349..f09a4056 100644 --- a/GameRes/GameRes.cs +++ b/GameRes/GameRes.cs @@ -102,7 +102,10 @@ namespace GameRes /// Filename extensions peculiar to the resource. public IEnumerable Extensions { get; protected set; } - /// Resource settings suitable for serialization. + /// Persistent resource settings. + public IEnumerable Settings { get; protected set; } + + /// Resource access scheme suitable for serialization. public virtual ResourceScheme Scheme { get; set; } /// diff --git a/GameRes/GameRes.csproj b/GameRes/GameRes.csproj index 3f0f895e..6d74b9ac 100644 --- a/GameRes/GameRes.csproj +++ b/GameRes/GameRes.csproj @@ -85,7 +85,14 @@ + + True + True + Settings.settings + + + True True @@ -107,7 +114,12 @@ + + + PublicSettingsSingleFileGenerator + Settings.Designer.cs + diff --git a/GameRes/ImageBMP.cs b/GameRes/ImageBMP.cs index 0e427dd1..1ff1e8b7 100644 --- a/GameRes/ImageBMP.cs +++ b/GameRes/ImageBMP.cs @@ -46,24 +46,33 @@ namespace GameRes } [Export(typeof(ImageFormat))] - public class BmpFormat : ImageFormat + public sealed class BmpFormat : ImageFormat { public override string Tag { get { return "BMP"; } } public override string Description { get { return "Windows device independent bitmap"; } } public override uint Signature { get { return 0; } } public override bool CanWrite { get { return true; } } + public BmpFormat () + { + Settings = new[] { EnableExtensions }; + } + #pragma warning disable 649 [ImportMany(typeof(IBmpExtension))] private IEnumerable m_extensions; #pragma warning restore 649 - bool EnableExtensions = true; + LocalResourceSetting EnableExtensions = new LocalResourceSetting { + Name = "BMPEnableExtensions", + Text = "Enable BMP format extensions", + Description = "Enables various extensions, such as transparency support.", + }; public override ImageData Read (IBinaryStream file, ImageMetaData info) { var bmp_info = info as BmpMetaData; - if (bmp_info != null && EnableExtensions && file.AsStream.CanSeek) + if (bmp_info != null && EnableExtensions.Get() && file.AsStream.CanSeek) { foreach (var ext in m_extensions) { diff --git a/GameRes/Properties/Settings.Designer.cs b/GameRes/Properties/Settings.Designer.cs new file mode 100644 index 00000000..0c57d68a --- /dev/null +++ b/GameRes/Properties/Settings.Designer.cs @@ -0,0 +1,38 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace GameRes.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "12.0.0.0")] + public sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("True")] + public bool BMPEnableExtensions { + get { + return ((bool)(this["BMPEnableExtensions"])); + } + set { + this["BMPEnableExtensions"] = value; + } + } + } +} diff --git a/GameRes/Properties/Settings.cs b/GameRes/Properties/Settings.cs new file mode 100644 index 00000000..86733109 --- /dev/null +++ b/GameRes/Properties/Settings.cs @@ -0,0 +1,28 @@ +namespace GameRes.Properties { + + + // This class allows you to handle specific events on the settings class: + // The SettingChanging event is raised before a setting's value is changed. + // The PropertyChanged event is raised after a setting's value is changed. + // The SettingsLoaded event is raised after the setting values are loaded. + // The SettingsSaving event is raised before the setting values are saved. + public sealed partial class Settings { + + public Settings() { + // // To add event handlers for saving and changing settings, uncomment the lines below: + // + // this.SettingChanging += this.SettingChangingEventHandler; + // + // this.SettingsSaving += this.SettingsSavingEventHandler; + // + } + + private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) { + // Add code to handle the SettingChangingEvent event here. + } + + private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) { + // Add code to handle the SettingsSaving event here. + } + } +} diff --git a/GameRes/Properties/Settings.settings b/GameRes/Properties/Settings.settings new file mode 100644 index 00000000..52b5370a --- /dev/null +++ b/GameRes/Properties/Settings.settings @@ -0,0 +1,9 @@ + + + + + + True + + + \ No newline at end of file diff --git a/GameRes/ResourceSettings.cs b/GameRes/ResourceSettings.cs new file mode 100644 index 00000000..233cd9f2 --- /dev/null +++ b/GameRes/ResourceSettings.cs @@ -0,0 +1,79 @@ +//! \file ResourceSettings.cs +//! \date 2018 Jan 08 +//! \brief Persistent resource settings implementation. +// +// Copyright (C) 2018 by morkt +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +// + +namespace GameRes +{ + /// + /// Interface to assembly app.config settings. + /// + public interface IResourceSetting + { + /// + /// Internal setting name, should match the name defined in app.config. + /// + string Name { get; } + + /// + /// Short text description of the setting (suitable for use in GUI dialog). + /// + string Text { get; } + + /// + /// More elaborate setting description. + /// + string Description { get; } + + + /// + /// Actual setting value. + /// + object Value { get; set; } + } + + public abstract class ResourceSettingBase : IResourceSetting + { + public string Name { get; set; } + public string Text { get; set; } + public string Description { get; set; } + + public abstract object Value { get; set; } + + public TValue Get () + { + var value = this.Value; + if (null == value || !(value is TValue)) + return default(TValue); + return (TValue)value; + } + } + + internal class LocalResourceSetting : ResourceSettingBase + { + public override object Value { + get { return GameRes.Properties.Settings.Default[Name]; } + set { GameRes.Properties.Settings.Default[Name] = value; } + } + } +} diff --git a/GameRes/app.config b/GameRes/app.config new file mode 100644 index 00000000..c5e2a39c --- /dev/null +++ b/GameRes/app.config @@ -0,0 +1,15 @@ + + + + +
+ + + + + + True + + + + \ No newline at end of file