Skip to content

Commit

Permalink
唔姆
Browse files Browse the repository at this point in the history
  • Loading branch information
yomunsam committed Feb 24, 2021
1 parent 4562f05 commit cf6b258
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Runtime/Scripts/Interfaces/IOptions.cs
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; }
}



}
11 changes: 11 additions & 0 deletions Runtime/Scripts/Interfaces/IOptions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Runtime/Scripts/Interfaces/IOptionsManager.cs
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);
}
}
11 changes: 11 additions & 0 deletions Runtime/Scripts/Interfaces/IOptionsManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime/Scripts/Systems/Options.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions Runtime/Scripts/Systems/Options/Options.cs
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;
}

}
}

11 changes: 11 additions & 0 deletions Runtime/Scripts/Systems/Options/Options.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions Runtime/Scripts/Systems/Options/OptionsManager.cs
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;
}

}
}
11 changes: 11 additions & 0 deletions Runtime/Scripts/Systems/Options/OptionsManager.cs.meta

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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit cf6b258

Please # to comment.