From cf6b2588a34a0a89b5b1f3d811601dfc63abc1e9 Mon Sep 17 00:00:00 2001 From: yomunsam Date: Wed, 24 Feb 2021 21:13:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=94=94=E5=A7=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Runtime/Scripts/Interfaces/IOptions.cs | 17 ++++ Runtime/Scripts/Interfaces/IOptions.cs.meta | 11 +++ Runtime/Scripts/Interfaces/IOptionsManager.cs | 15 ++++ .../Interfaces/IOptionsManager.cs.meta | 11 +++ Runtime/Scripts/Systems/Options.meta | 8 ++ Runtime/Scripts/Systems/Options/Options.cs | 67 ++++++++++++++++ .../Scripts/Systems/Options/Options.cs.meta | 11 +++ .../Scripts/Systems/Options/OptionsManager.cs | 78 +++++++++++++++++++ .../Systems/Options/OptionsManager.cs.meta | 11 +++ .../OptionsServiceContainerExtensions.cs | 70 +++++++++++++++++ .../OptionsServiceContainerExtensions.cs.meta | 11 +++ package.json | 2 +- 12 files changed, 311 insertions(+), 1 deletion(-) create mode 100644 Runtime/Scripts/Interfaces/IOptions.cs create mode 100644 Runtime/Scripts/Interfaces/IOptions.cs.meta create mode 100644 Runtime/Scripts/Interfaces/IOptionsManager.cs create mode 100644 Runtime/Scripts/Interfaces/IOptionsManager.cs.meta create mode 100644 Runtime/Scripts/Systems/Options.meta create mode 100644 Runtime/Scripts/Systems/Options/Options.cs create mode 100644 Runtime/Scripts/Systems/Options/Options.cs.meta create mode 100644 Runtime/Scripts/Systems/Options/OptionsManager.cs create mode 100644 Runtime/Scripts/Systems/Options/OptionsManager.cs.meta create mode 100644 Runtime/Scripts/Systems/Options/OptionsServiceContainerExtensions.cs create mode 100644 Runtime/Scripts/Systems/Options/OptionsServiceContainerExtensions.cs.meta diff --git a/Runtime/Scripts/Interfaces/IOptions.cs b/Runtime/Scripts/Interfaces/IOptions.cs new file mode 100644 index 0000000..e22cea0 --- /dev/null +++ b/Runtime/Scripts/Interfaces/IOptions.cs @@ -0,0 +1,17 @@ +namespace TinaX.Options +{ + + public interface IOptions + { + object OptionValue { get; } + } + + + public interface IOptions where TOption : class + { + TOption Value { get; } + } + + + +} diff --git a/Runtime/Scripts/Interfaces/IOptions.cs.meta b/Runtime/Scripts/Interfaces/IOptions.cs.meta new file mode 100644 index 0000000..0181f6b --- /dev/null +++ b/Runtime/Scripts/Interfaces/IOptions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b30dc114e3cfba943a8cad53730118c8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Scripts/Interfaces/IOptionsManager.cs b/Runtime/Scripts/Interfaces/IOptionsManager.cs new file mode 100644 index 0000000..c3baa0d --- /dev/null +++ b/Runtime/Scripts/Interfaces/IOptionsManager.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TinaX.Options +{ + public interface IOptionsManager + { + IOptions GetOptions(string typeName, string name); + IOptions GetOptions(Type type); + IOptions GetOptions(string typeName); + } +} diff --git a/Runtime/Scripts/Interfaces/IOptionsManager.cs.meta b/Runtime/Scripts/Interfaces/IOptionsManager.cs.meta new file mode 100644 index 0000000..242cfb0 --- /dev/null +++ b/Runtime/Scripts/Interfaces/IOptionsManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 96a3e4e12b2049f46b1bfb30692d46bb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Scripts/Systems/Options.meta b/Runtime/Scripts/Systems/Options.meta new file mode 100644 index 0000000..91a42e0 --- /dev/null +++ b/Runtime/Scripts/Systems/Options.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 053cbbd330e77d7449db66ce064a2ded +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Scripts/Systems/Options/Options.cs b/Runtime/Scripts/Systems/Options/Options.cs new file mode 100644 index 0000000..b87ab4d --- /dev/null +++ b/Runtime/Scripts/Systems/Options/Options.cs @@ -0,0 +1,67 @@ +using System; +using TinaX.Container; + + +namespace TinaX.Options.Internal +{ + public class Options : IOptions + { + public const string DefaultName = "default"; + + public object OptionValue + { + get + { + if(_optionValue == null) + { + _optionValue = Core.CreateInstance(m_OptionType); + m_ConfigureOptions(_optionValue); + } + return _optionValue; + } + } + + + private IXCore Core + { + get + { + if (_core == null) + _core = m_Services.Get(); + + return _core; + } + } + private IXCore _core; + + private readonly Action m_ConfigureOptions; + private readonly IServiceContainer m_Services; + private readonly Type m_OptionType; + + private object _optionValue; + + public Options(Action configureOptions, IServiceContainer services, Type optionType) + { + m_ConfigureOptions = configureOptions ?? throw new ArgumentNullException(nameof(configureOptions)); + m_Services = services ?? throw new ArgumentNullException(nameof(services)); + m_OptionType = optionType ?? throw new ArgumentNullException(nameof(optionType)); + } + + + } + + + public class Options : IOptions where T: class + { + private readonly Options m_Options; + + public T Value => (T)m_Options.OptionValue; + + public Options(Options options) + { + this.m_Options = options; + } + + } +} + diff --git a/Runtime/Scripts/Systems/Options/Options.cs.meta b/Runtime/Scripts/Systems/Options/Options.cs.meta new file mode 100644 index 0000000..3562cc1 --- /dev/null +++ b/Runtime/Scripts/Systems/Options/Options.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 01973b2ab389b784fa0f146e61438e31 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Scripts/Systems/Options/OptionsManager.cs b/Runtime/Scripts/Systems/Options/OptionsManager.cs new file mode 100644 index 0000000..0d80a32 --- /dev/null +++ b/Runtime/Scripts/Systems/Options/OptionsManager.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ICSharpCode.SharpZipLib.Zip; + +namespace TinaX.Options.Internal +{ + public class OptionsManager : IOptionsManager + { + private Hashtable m_OptionsTable = new Hashtable(); + /* + * m_OptionsTable = { + * ["typeName"] = + * { + * ["name"] = options + * } + * } + * + * Hashtable[总表, key:typeName] --> Hashtable[key: name] + */ + + public void Set(string typeName, string name ,Options options) + { + Hashtable subTable; + if (m_OptionsTable.ContainsKey(typeName)) + { + subTable = (Hashtable)m_OptionsTable[typeName]; + } + else + { + subTable = new Hashtable(); + } + + if (subTable.ContainsKey(name)) + { + subTable[name] = options; + } + else + { + subTable.Add(name, options); + } + } + + public bool TryGet(string typeName, string name, out Options options) + { + if (m_OptionsTable.ContainsKey(typeName)) + { + var subTable = (Hashtable)m_OptionsTable[typeName]; + if(subTable.ContainsKey(name)) + { + options = (Options)subTable[name]; + return true; + } + } + + options = default; + return false; + } + + public IOptions GetOptions(Type type) => GetOptions(type.FullName, Options.DefaultName); + public IOptions GetOptions(Type type, string name) => GetOptions(type.FullName, name); + + public IOptions GetOptions(string typeName) => GetOptions(typeName, Options.DefaultName); + + public IOptions GetOptions(string typeName , string name) + { + if(TryGet(typeName, name, out var opt)) + { + return opt; + } + return null; + } + + } +} diff --git a/Runtime/Scripts/Systems/Options/OptionsManager.cs.meta b/Runtime/Scripts/Systems/Options/OptionsManager.cs.meta new file mode 100644 index 0000000..1ab6d7a --- /dev/null +++ b/Runtime/Scripts/Systems/Options/OptionsManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5fd30113cdc81764e8edf216b7332ce7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Scripts/Systems/Options/OptionsServiceContainerExtensions.cs b/Runtime/Scripts/Systems/Options/OptionsServiceContainerExtensions.cs new file mode 100644 index 0000000..42bf9ce --- /dev/null +++ b/Runtime/Scripts/Systems/Options/OptionsServiceContainerExtensions.cs @@ -0,0 +1,70 @@ +using System; +using TinaX.Container; +using TinaX.Options.Internal; +using UnityEngine; + +namespace TinaX.Options +{ + public static class OptionsServiceContainerExtensions + { + public static IServiceContainer AddOptions(this IServiceContainer services) + { + if(services.SingletonIf(out var bd)) + { + bd.SetAlias(); + } + return services; + } + + + /// + /// Configure and inject it into the service container + /// 添加配置,并注入到服务容器 + /// + /// + /// + /// + /// + public static IServiceContainer Configure(this IServiceContainer services, Action configureOptions) where TOptions : class + { + var options = services.DoConfigure(typeof(TOptions), (opt)=> + { + configureOptions.Invoke((TOptions)opt); + }, null); + var generic_options = new Options(options); + services.Instance>(generic_options); + return services; + } + + /// + /// Configure + /// + /// + /// + /// + /// + public static IServiceContainer Configure(this IServiceContainer services, Type type, Action configureOptions) + { + services.DoConfigure(type, configureOptions, null); + return services; + } + + + private static Internal.Options DoConfigure(this IServiceContainer services, Type type, Action configureOptions, string typeName = null) + { + var options = new TinaX.Options.Internal.Options(configureOptions, services, type); + if (services.TryGet(out var optionsMgr)) + { + optionsMgr.Set(typeName ?? type.FullName, Internal.Options.DefaultName, options); + } + else + { + Debug.LogError("[TinaX]Options not enable. Please invoke \"IServiceContainer.AddOptions\"."); + } + + return options; + } + + + } +} diff --git a/Runtime/Scripts/Systems/Options/OptionsServiceContainerExtensions.cs.meta b/Runtime/Scripts/Systems/Options/OptionsServiceContainerExtensions.cs.meta new file mode 100644 index 0000000..33aca69 --- /dev/null +++ b/Runtime/Scripts/Systems/Options/OptionsServiceContainerExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b781a20fe2d4aad4e8bfad8af64a4283 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/package.json b/package.json index b0d8c0e..28cea66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "io.nekonya.tinax.core", - "version": "6.6.21", + "version": "6.6.22", "displayName": "TinaX.Core", "description": "TinaX Framework.", "unity": "2019.4",