-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace TinaX.Options | ||
{ | ||
|
||
public interface IOptions | ||
{ | ||
object OptionValue { get; } | ||
} | ||
|
||
|
||
public interface IOptions<TOption> where TOption : class | ||
{ | ||
TOption Value { get; } | ||
} | ||
|
||
|
||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IXCore>(); | ||
|
||
return _core; | ||
} | ||
} | ||
private IXCore _core; | ||
|
||
private readonly Action<object> m_ConfigureOptions; | ||
private readonly IServiceContainer m_Services; | ||
private readonly Type m_OptionType; | ||
|
||
private object _optionValue; | ||
|
||
public Options(Action<object> 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<T> : IOptions<T> where T: class | ||
{ | ||
private readonly Options m_Options; | ||
|
||
public T Value => (T)m_Options.OptionValue; | ||
|
||
public Options(Options options) | ||
{ | ||
this.m_Options = options; | ||
} | ||
|
||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IOptionsManager, OptionsManager>(out var bd)) | ||
{ | ||
bd.SetAlias<OptionsManager>(); | ||
} | ||
return services; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Configure and inject it into the service container | ||
/// 添加配置,并注入到服务容器 | ||
/// </summary> | ||
/// <typeparam name="TOptions"></typeparam> | ||
/// <param name="services"></param> | ||
/// <param name="configureOptions"></param> | ||
/// <returns></returns> | ||
public static IServiceContainer Configure<TOptions>(this IServiceContainer services, Action<TOptions> configureOptions) where TOptions : class | ||
{ | ||
var options = services.DoConfigure(typeof(TOptions), (opt)=> | ||
{ | ||
configureOptions.Invoke((TOptions)opt); | ||
}, null); | ||
var generic_options = new Options<TOptions>(options); | ||
services.Instance<IOptions<TOptions>>(generic_options); | ||
return services; | ||
} | ||
|
||
/// <summary> | ||
/// Configure | ||
/// </summary> | ||
/// <param name="services"></param> | ||
/// <param name="type"></param> | ||
/// <param name="configureOptions"></param> | ||
/// <returns></returns> | ||
public static IServiceContainer Configure(this IServiceContainer services, Type type, Action<object> configureOptions) | ||
{ | ||
services.DoConfigure(type, configureOptions, null); | ||
return services; | ||
} | ||
|
||
|
||
private static Internal.Options DoConfigure(this IServiceContainer services, Type type, Action<object> configureOptions, string typeName = null) | ||
{ | ||
var options = new TinaX.Options.Internal.Options(configureOptions, services, type); | ||
if (services.TryGet<OptionsManager>(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; | ||
} | ||
|
||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.