(GameRes): persistent resource settings infrastructure.

This commit is contained in:
morkt 2018-01-08 19:55:28 +04:00
parent 444a15b94a
commit a3ce27da5f
10 changed files with 203 additions and 4 deletions

View File

@ -110,6 +110,7 @@ namespace GARbro.GUI
{ {
try try
{ {
FormatCatalog.Instance.SaveSettings();
Settings.Default.Save(); Settings.Default.Save();
} }
catch (Exception X) catch (Exception X)

View File

@ -107,6 +107,11 @@ namespace GameRes
} }
} }
public void SaveSettings ()
{
Properties.Settings.Default.Save();
}
private void AddResourceImpl (IEnumerable<IResource> formats, CompositionContainer container) private void AddResourceImpl (IEnumerable<IResource> formats, CompositionContainer container)
{ {
foreach (var impl in formats) foreach (var impl in formats)

View File

@ -102,7 +102,10 @@ namespace GameRes
/// <summary>Filename extensions peculiar to the resource.</summary> /// <summary>Filename extensions peculiar to the resource.</summary>
public IEnumerable<string> Extensions { get; protected set; } public IEnumerable<string> Extensions { get; protected set; }
/// <summary>Resource settings suitable for serialization.</summary> /// <summary>Persistent resource settings.</summary>
public IEnumerable<IResourceSetting> Settings { get; protected set; }
/// <summary>Resource access scheme suitable for serialization.</summary>
public virtual ResourceScheme Scheme { get; set; } public virtual ResourceScheme Scheme { get; set; }
/// <summary> /// <summary>

View File

@ -85,7 +85,14 @@
<Compile Include="ImageTIFF.cs" /> <Compile Include="ImageTIFF.cs" />
<Compile Include="MultiDict.cs" /> <Compile Include="MultiDict.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
<Compile Include="ResourceSettings.cs" />
<Compile Include="ScriptText.cs" /> <Compile Include="ScriptText.cs" />
<Compile Include="Properties\Settings.cs" />
<Compile Include="Strings\garStrings.Designer.cs"> <Compile Include="Strings\garStrings.Designer.cs">
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
@ -107,7 +114,12 @@
<EmbeddedResource Include="Strings\garStrings.zh-Hans.resx" /> <EmbeddedResource Include="Strings\garStrings.zh-Hans.resx" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="app.config" />
<None Include="packages.config" /> <None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>PublicSettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup> <PropertyGroup>

View File

@ -46,24 +46,33 @@ namespace GameRes
} }
[Export(typeof(ImageFormat))] [Export(typeof(ImageFormat))]
public class BmpFormat : ImageFormat public sealed class BmpFormat : ImageFormat
{ {
public override string Tag { get { return "BMP"; } } public override string Tag { get { return "BMP"; } }
public override string Description { get { return "Windows device independent bitmap"; } } public override string Description { get { return "Windows device independent bitmap"; } }
public override uint Signature { get { return 0; } } public override uint Signature { get { return 0; } }
public override bool CanWrite { get { return true; } } public override bool CanWrite { get { return true; } }
public BmpFormat ()
{
Settings = new[] { EnableExtensions };
}
#pragma warning disable 649 #pragma warning disable 649
[ImportMany(typeof(IBmpExtension))] [ImportMany(typeof(IBmpExtension))]
private IEnumerable<IBmpExtension> m_extensions; private IEnumerable<IBmpExtension> m_extensions;
#pragma warning restore 649 #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) public override ImageData Read (IBinaryStream file, ImageMetaData info)
{ {
var bmp_info = info as BmpMetaData; var bmp_info = info as BmpMetaData;
if (bmp_info != null && EnableExtensions && file.AsStream.CanSeek) if (bmp_info != null && EnableExtensions.Get<bool>() && file.AsStream.CanSeek)
{ {
foreach (var ext in m_extensions) foreach (var ext in m_extensions)
{ {

38
GameRes/Properties/Settings.Designer.cs generated Normal file
View File

@ -0,0 +1,38 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 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.
// </auto-generated>
//------------------------------------------------------------------------------
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;
}
}
}
}

View File

@ -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.
}
}
}

View File

@ -0,0 +1,9 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="GameRes.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="BMPEnableExtensions" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>

View File

@ -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
{
/// <summary>
/// Interface to assembly app.config settings.
/// </summary>
public interface IResourceSetting
{
/// <summary>
/// Internal setting name, should match the name defined in app.config.
/// </summary>
string Name { get; }
/// <summary>
/// Short text description of the setting (suitable for use in GUI dialog).
/// </summary>
string Text { get; }
/// <summary>
/// More elaborate setting description.
/// </summary>
string Description { get; }
/// <summary>
/// Actual setting value.
/// </summary>
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<TValue> ()
{
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; }
}
}
}

15
GameRes/app.config Normal file
View File

@ -0,0 +1,15 @@
<?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="GameRes.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<GameRes.Properties.Settings>
<setting name="BMPEnableExtensions" serializeAs="String">
<value>True</value>
</setting>
</GameRes.Properties.Settings>
</userSettings>
</configuration>