12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064 |
- using System.Threading.Tasks;
- using System.Collections.Generic;
- using System;
- using UnityEngine;
- using System.Reflection;
- namespace Blue
- {
- #region Architecture
-
-
-
-
-
-
- public interface IArchitecture
- {
- void SetArchitecture(IArchitecture instance);
- void InitArchitecture();
- void RegisterService<T>(T service) where T : IService;
- void RegisterModel<T>(T Model) where T : IModel;
- void RegisterUtility<T>(T utility) where T : IUtility;
- T GetService<T>() where T : class, IService;
- T GetModel<T>() where T : class, IModel;
- T GetUtility<T>() where T : class, IUtility;
- }
- public abstract class AbstractArchitecture<T> : IArchitecture where T : AbstractArchitecture<T>, new()
- {
- private IOCContainer mContainer = new IOCContainer();
- private ICommandHandler commandHandler = new DefaultCommandHandler();
- private IQueryHandler queryHandler = new DefaultQueryHandler();
- private ITypeEventSystem mTypeEventSystem = new TypeEventSystem();
- private HashSet<IService> mService;
- private HashSet<IModel> mModels;
- protected abstract void Init();
- private static IArchitecture mArchitectureInstance;
- void IArchitecture.SetArchitecture(IArchitecture instance)
- {
- mArchitectureInstance = instance;
- CanGetModelExtension.SetArchitecture(mArchitectureInstance);
- CanGetServiceExtension.SetArchitecture(mArchitectureInstance);
- CanGetUtilityExtension.SetArchitecture(mArchitectureInstance);
- CanSendCommandExtension.SetCommandHandler(commandHandler);
- CanSendQueryExtension.SetQueryHandler(queryHandler);
- CanRegisterEventExtension.SetTypeEventSystem(mTypeEventSystem);
- CanSendEventExtension.SetTypeEventSystem(mTypeEventSystem);
- }
- void IArchitecture.InitArchitecture()
- {
- StartInit();
- FinishInit();
- }
- private void StartInit()
- {
- mService = new HashSet<IService>();
- mModels = new HashSet<IModel>();
- Init();
- foreach (var architectureModel in mModels)
- {
- architectureModel.OnInit();
- }
- foreach (var service in mService)
- {
- service.OnInit();
- }
- }
- private void FinishInit()
- {
- mService.Clear();
- mModels.Clear();
- mService = null;
- mModels = null;
- }
- public void RegisterService<T>(T service) where T : IService
- {
- mContainer.Register<T>(service);
- mService.Add(service);
- }
- public void RegisterModel<T>(T model) where T : IModel
- {
- mContainer.Register<T>(model);
- mModels.Add(model);
- }
- public void RegisterUtility<T>(T utility) where T : IUtility
- {
- mContainer.Register<T>(utility);
- }
- public T GetService<T>() where T : class, IService
- {
- return mContainer.Get<T>();
- }
- public T GetModel<T>() where T : class, IModel
- {
- return mContainer.Get<T>();
- }
- public T GetUtility<T>() where T : class, IUtility
- {
- return mContainer.Get<T>();
- }
- }
-
-
-
- internal sealed class ArchitectureInitiator
- {
- private ArchitectureInitiator() { }
- private static ArchitectureInitiator mInstance;
- private IArchitecture architectureInstance = null;
- private Type architectureType = typeof(IArchitecture);
- private bool architectureInited;
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
- private static void Initiate()
- {
- mInstance = new ArchitectureInitiator();
- Type[] typeArr = Assembly.GetExecutingAssembly().GetTypes();
- for (int i = 0; i < typeArr.Length; i++)
- {
- Type tmpType = typeArr[i];
- if (!tmpType.IsInterface)
- {
- if (mInstance.IsArchitecture(tmpType))
- {
- mInstance.CreateArchitectureInstance(tmpType);
- }
- }
- }
- mInstance.RegisterArchitecture();
- mInstance.InitArchitecture();
- }
- private void RegisterArchitecture()
- {
- if (architectureInstance != null)
- {
- architectureInstance.SetArchitecture(architectureInstance);
- }
- }
- private void InitArchitecture()
- {
- if (architectureInstance != null)
- {
- architectureInstance.InitArchitecture();
- }
- }
- private bool IsArchitecture(Type tmpType)
- {
- if (architectureInited)
- {
- return false;
- }
- if (tmpType.IsAbstract)
- {
- return false;
- }
- return architectureType.IsAssignableFrom(tmpType);
- }
- private void CreateArchitectureInstance(Type archiType)
- {
- if (architectureInstance == null)
- {
- architectureInstance = (IArchitecture)Activator.CreateInstance(archiType);
- architectureInited = true;
- }
- else
- {
- throw new Exception("More than one Architecture sub-class in the application,it should be only one!");
- }
- }
- }
- public interface IOnEvent<T>
- {
- void OnEvent(T e);
- }
- public static class OnGlobalEventExtension
- {
- public static IUnRegister RegisterEvent<T>(this IOnEvent<T> self) where T :struct,IEvent
- {
- return TypeEventSystem.Global.Register<T>(self.OnEvent);
- }
- public static void UnRegisterEvent<T>(this IOnEvent<T> self) where T : struct,IEvent
- {
- TypeEventSystem.Global.UnRegister<T>(self.OnEvent);
- }
- }
- #endregion
- #region Controller
- public interface IController : ICanGetUtility, ICanGetService, ICanGetModel,
- ICanRegisterEvent, ICanSendQuery, ICanSendCommand
- {
- }
- #endregion
- #region Service
- public interface IService : ICanGetService, ICanGetModel, ICanGetUtility,
- ICanRegisterEvent, ICanSendEvent
- {
- void OnInit();
- }
- #endregion
- #region Model
- public interface IModel : ICanGetUtility, ICanSendEvent
- {
- void OnInit();
- }
- #endregion
- #region Utility
- public interface IUtility
- {
- }
- #endregion
- #region Command
- public interface ICommand : ICanGetService, ICanGetModel, ICanGetUtility,
- ICanSendEvent, ICanSendCommand, ICanSendQuery
- {
- void OnExcute();
- }
- public interface ICommand<T> : ICanGetService, ICanGetModel, ICanGetUtility,
- ICanSendEvent, ICanSendCommand, ICanSendQuery
- {
- T OnExcute();
- }
- public interface ICommandHandler
- {
- void ExcuteCommand(ICommand command);
- void ExcuteCommand<T>() where T : ICommand, new();
- T ExcuteCommand<T>(ICommand<T> command);
- }
- public class DefaultCommandHandler : ICommandHandler
- {
- public void ExcuteCommand(ICommand command)
- {
- command.OnExcute();
- }
- public void ExcuteCommand<T>() where T : ICommand, new()
- {
- ExcuteCommand(new T());
- }
- public T ExcuteCommand<T>(ICommand<T> command)
- {
- return command.OnExcute();
- }
- }
- #endregion
- #region Query
- public interface IQuery<T> : ICanGetModel, ICanGetService, ICanSendQuery
- {
- T DoQuery();
- }
- public interface IQueryAsync : ICanGetModel, ICanGetService, ICanSendQuery
- {
- void DoQueryAsync<T>(Action<T> onQueryCompleted);
- }
- public interface IQueryResult<T>
- {
- void OnQuerySucceed(Action<T> onQuerySucceed);
- void OnQueryFailed(Action onQueryFailed);
- }
- public interface IQueryHandler
- {
- T DoQuery<T>(IQuery<T> query);
- K DoQuery<T, K>() where T : IQuery<K>, new();
- IQueryResult<T> DoQueryAsync<T>(IQuery<T> query);
- IQueryResult<K> DoQueryAsync<T, K>() where T : IQuery<K>, new();
- }
- public class DefaultQueryHandler : IQueryHandler
- {
- public T DoQuery<T>(IQuery<T> query)
- {
- return query.DoQuery();
- }
- public K DoQuery<T, K>() where T : IQuery<K>, new()
- {
- return DoQuery(new T());
- }
- public IQueryResult<T> DoQueryAsync<T>(IQuery<T> query)
- {
- DefaultQueryResult<T> queryResult = new DefaultQueryResult<T>();
- var queryTask = Task.Run(() =>
- {
- return query.DoQuery();
- });
- var awaiter = queryTask.GetAwaiter();
- awaiter.OnCompleted(() =>
- {
- if (queryTask.IsFaulted | queryTask.IsCanceled)
- {
- queryResult.TriggerFailed();
- }
- else
- {
- queryResult.TriggerSuccess(awaiter.GetResult());
- }
- });
- return queryResult;
- }
- public IQueryResult<K> DoQueryAsync<T, K>() where T : IQuery<K>, new()
- {
- return DoQueryAsync(new T());
- }
- }
- public class DefaultQueryResult<T> : IQueryResult<T>
- {
- private Action<T> mOnQuerySucceed;
- private Action mOnQueryFailed;
- public void OnQuerySucceed(Action<T> onQuerySucceed)
- {
- mOnQuerySucceed += onQuerySucceed;
- }
- public void OnQueryFailed(Action onQueryFailed)
- {
- mOnQueryFailed += onQueryFailed;
- }
- public void TriggerSuccess(T result)
- {
- mOnQuerySucceed?.Invoke(result);
- }
- public void TriggerFailed()
- {
- mOnQueryFailed?.Invoke();
- }
- }
- #endregion
- #region Rule
-
-
-
- public interface ICanGetModel
- {
- }
-
-
-
- public static class CanGetModelExtension
- {
- public static T GetModel<T>(this ICanGetModel self) where T : class, IModel
- {
- return _architecture.GetModel<T>();
- }
- private static IArchitecture _architecture;
- public static void SetArchitecture(IArchitecture architecture)
- {
- _architecture = architecture;
- }
- }
-
-
-
- public interface ICanGetService
- {
- }
-
-
-
- public static class CanGetServiceExtension
- {
- public static T GetService<T>(this ICanGetService self) where T : class, IService
- {
- return _architecture.GetService<T>();
- }
- private static IArchitecture _architecture;
- public static void SetArchitecture(IArchitecture architecture)
- {
- _architecture = architecture;
- }
- }
-
-
-
- public interface ICanGetUtility
- {
- }
-
-
-
- public static class CanGetUtilityExtension
- {
- public static T GetUtility<T>(this ICanGetUtility self) where T : class, IUtility
- {
- return _architecture.GetUtility<T>();
- }
- private static IArchitecture _architecture;
- public static void SetArchitecture(IArchitecture architecture)
- {
- _architecture = architecture;
- }
- }
-
-
-
- public interface ICanRegisterEvent
- {
- }
-
-
-
- public static class CanRegisterEventExtension
- {
- public static IUnRegister RegisterEvent<T>(this ICanRegisterEvent self, Action<T> onEvent) where T : IEvent
- {
- return typeEventSystem.Register<T>(onEvent);
- }
- public static void UnRegisterEvent<T>(this ICanRegisterEvent self, Action<T> onEvent) where T : IEvent
- {
- typeEventSystem.UnRegister<T>(onEvent);
- }
- private static ITypeEventSystem typeEventSystem;
- public static void SetTypeEventSystem(ITypeEventSystem mTypeEventSystem)
- {
- typeEventSystem = mTypeEventSystem;
- }
- }
-
-
-
- public interface ICanSendCommand
- {
- }
-
-
-
- public static class CanSendCommandExtension
- {
- public static void SendCommand<T>(this ICanSendCommand self) where T : ICommand, new()
- {
- mCommandHandler.ExcuteCommand<T>();
- }
- public static void SendCommand<T>(this ICanSendCommand self, T command) where T : ICommand
- {
- mCommandHandler.ExcuteCommand(command);
- }
- public static T SendCommand<T>(this ICanSendCommand self, ICommand<T> command)
- {
- return mCommandHandler.ExcuteCommand(command);
- }
- private static ICommandHandler mCommandHandler;
- public static void SetCommandHandler(ICommandHandler commandHandler)
- {
- mCommandHandler = commandHandler;
- }
- }
-
-
-
- public interface ICanSendEvent
- {
- }
-
-
-
- public static class CanSendEventExtension
- {
- public static void SendEvent<T>(this ICanSendEvent self) where T : IEvent, new()
- {
- typeEventSystem.Send<T>();
- }
- public static void SendEvent<T>(this ICanSendEvent self, T e) where T : IEvent
- {
- typeEventSystem.Send<T>(e);
- }
- private static ITypeEventSystem typeEventSystem;
- public static void SetTypeEventSystem(ITypeEventSystem mTypeEventSystem)
- {
- typeEventSystem = mTypeEventSystem;
- }
- }
-
-
-
- public interface ICanSendQuery
- {
- }
-
-
-
- public static class CanSendQueryExtension
- {
- public static T SendQuery<T>(this ICanSendQuery self, IQuery<T> queryInstance)
- {
- return _queryHandler.DoQuery<T>(queryInstance);
- }
- public static K SendQuery<T, K>(this ICanSendQuery self) where T : IQuery<K>, new()
- {
- return _queryHandler.DoQuery<T, K>();
- }
- public static IQueryResult<T> SendQueryAsync<T>(this ICanSendQuery self, IQuery<T> queryInstance)
- {
- return _queryHandler.DoQueryAsync<T>(queryInstance);
- }
- public static IQueryResult<K> SendQueryAsync<T, K>(this ICanSendQuery self) where T : IQuery<K>, new()
- {
- return _queryHandler.DoQueryAsync<T, K>();
- }
- private static IQueryHandler _queryHandler;
- public static void SetQueryHandler(IQueryHandler queryHandler)
- {
- _queryHandler = queryHandler;
- }
- }
- #endregion
- #region TypeEventSystem
-
-
-
- public interface IUnRegister
- {
- void UnRegister();
- }
-
-
-
- public interface IUnRegisterList
- {
- List<IUnRegister> UnRegistersList { get; }
- }
-
-
-
-
-
- public static class IUnRegisterListExtension
- {
- public static void AddToUnRegisterList(this IUnRegister self, IUnRegisterList unRegisterList)
- {
- unRegisterList.UnRegistersList.Add(self);
- }
- public static void UnRegisterAll(this IUnRegisterList self)
- {
- foreach (IUnRegister unRegister in self.UnRegistersList)
- {
- unRegister.UnRegister();
- }
- self.UnRegistersList.Clear();
- }
- }
-
-
-
- public struct CustomUnRegister : IUnRegister
- {
- private Action mOnUnregister { get; set; }
- public CustomUnRegister(Action onUnRegsiter)
- {
- mOnUnregister = onUnRegsiter;
- }
- public void UnRegister()
- {
- mOnUnregister?.Invoke();
- mOnUnregister = null;
- }
- }
-
-
-
- public class UnRegisterOnDestroyTrigger : MonoBehaviour
- {
- private readonly HashSet<IUnRegister> mUnRegisters = new HashSet<IUnRegister>();
- public void AddUnRegister(IUnRegister unRegister)
- {
- mUnRegisters.Add(unRegister);
- }
- public void RemoveUnRegister(IUnRegister unRegister)
- {
- mUnRegisters.Remove(unRegister);
- }
- private void OnDestroy()
- {
- foreach (IUnRegister unRegister in mUnRegisters)
- {
- unRegister.UnRegister();
- }
- mUnRegisters.Clear();
- }
- }
-
-
-
-
-
- public static class UnRegisterExtension
- {
-
-
-
-
-
-
- public static IUnRegister UnRegisterWhenGameObjectDestroyed(this IUnRegister unRegister, GameObject go)
- {
- UnRegisterOnDestroyTrigger trigger = go.GetComponent<UnRegisterOnDestroyTrigger>();
- if (!trigger)
- {
- trigger = go.AddComponent<UnRegisterOnDestroyTrigger>();
- }
- trigger.AddUnRegister(unRegister);
- return unRegister;
- }
-
-
-
-
-
-
- public static IUnRegister UnRegisterWhenGameObjectDestroyed<T>(this IUnRegister unRegister, T component) where T : Component
- {
- return unRegister.UnRegisterWhenGameObjectDestroyed(component.gameObject);
- }
- }
- public interface ITypeEventSystem
- {
- IUnRegister Register<T>(Action<T> onEvent) where T: IEvent;
- void UnRegister<T>(Action<T> onEvent) where T: IEvent;
- void Send<T>() where T: IEvent, new();
- void Send<T>(T e) where T: IEvent;
- }
- public class TypeEventSystem:ITypeEventSystem
- {
- private readonly EasyEvents mEvents = new EasyEvents();
- public static readonly TypeEventSystem Global = new TypeEventSystem();
- public void Send<T>() where T : IEvent,new()
- {
- mEvents.GetEvent<EasyEvent<T>>()?.Trigger(new T());
- }
- public void Send<T>(T e) where T: IEvent
- {
- mEvents.GetEvent<EasyEvent<T>>()?.Trigger(e);
- }
- public IUnRegister Register<T>(Action<T> onEvent)where T: IEvent
- {
- var e = mEvents.GetOrAddEvent<EasyEvent<T>>();
- return e.Register(onEvent);
- }
- public void UnRegister<T>(Action<T> onEvent)where T: IEvent
- {
- var e = mEvents.GetEvent<EasyEvent<T>>();
- if (e != null)
- {
- e.UnRegister(onEvent);
- }
- }
- }
- #endregion
- #region IOC
-
-
-
-
- public class IOCContainer
- {
- private Dictionary<Type, object> mInstances = new Dictionary<Type, object>();
-
-
-
-
-
- public void Register<T>(T instance)
- {
- Type key = typeof(T);
- if (mInstances.ContainsKey(key))
- {
- mInstances[key] = instance;
- }
- else
- {
- mInstances.Add(key, instance);
- }
- }
-
-
-
-
- public T Get<T>() where T : class
- {
- Type key = typeof(T);
- if (mInstances.TryGetValue(key, out object retInstance))
- {
- return retInstance as T;
- }
- return null;
- }
- }
- #endregion
- #region BindableProperty
- public interface IReadonlyBindableProperty<T>
- {
- T Value { get; }
- IUnRegister Register(Action<T> onValueChanged);
- void UnRegister(Action<T> onValueChanged);
- IUnRegister RegisterWithInitValue(Action<T> action);
- }
- public interface IBindableProperty<T> : IReadonlyBindableProperty<T>
- {
- new T Value { get; set; }
- void SetValueWithoutEvent(T newValue);
- }
- public class BindableProperty<T> : IBindableProperty<T>
- {
- private Action<T> mOnValueChanged = (v) => { };
- public BindableProperty(T defaultValue = default)
- {
- mValue = defaultValue;
- }
- protected T mValue;
- public T Value
- {
- get => GetValue();
- set
- {
- if (value == null && mValue == null) return;
- if (value != null && value.Equals(mValue)) return;
- SetValue(value);
- mOnValueChanged?.Invoke(value);
- }
- }
- protected virtual void SetValue(T newValue)
- {
- mValue = newValue;
- }
- protected virtual T GetValue()
- {
- return mValue;
- }
- public void SetValueWithoutEvent(T newValue)
- {
- mValue = newValue;
- }
- public IUnRegister Register(Action<T> onValueChanged)
- {
- mOnValueChanged += onValueChanged;
- return new BindablePropertyUnRegister<T>()
- {
- BindableProperty = this,
- OnValueChanged = onValueChanged
- };
- }
- public IUnRegister RegisterWithInitValue(Action<T> onValueChanged)
- {
- onValueChanged(mValue);
- return Register(onValueChanged);
- }
- public static implicit operator T(BindableProperty<T> property)
- {
- return property.Value;
- }
- public override string ToString()
- {
- return Value.ToString();
- }
- public void UnRegister(Action<T> onValueChanged)
- {
- mOnValueChanged -= onValueChanged;
- }
- }
- public class BindablePropertyUnRegister<T> : IUnRegister
- {
- public BindableProperty<T> BindableProperty { get; set; }
- public Action<T> OnValueChanged { get; set; }
- public void UnRegister()
- {
- BindableProperty.UnRegister(OnValueChanged);
- BindableProperty = null;
- OnValueChanged = null;
- }
- }
- #endregion
- #region EasyEvent
- public interface IEasyEvent
- {
- }
- public class EasyEvent : IEasyEvent
- {
- private Action mOnEvent = () => { };
- public IUnRegister Register(Action onEvent)
- {
- mOnEvent += onEvent;
- return new CustomUnRegister(() =>
- {
- UnRegister(onEvent);
- });
- }
- public void UnRegister(Action onEvent)
- {
- mOnEvent -= onEvent;
- }
- public void Trigger()
- {
- mOnEvent?.Invoke();
- }
- }
- public class EasyEvent<T> : IEasyEvent
- {
- private Action<T> mOnEvent = e => { };
- public IUnRegister Register(Action<T> onEvent)
- {
- mOnEvent += onEvent;
- return new CustomUnRegister(() =>
- {
- UnRegister(onEvent);
- });
- }
- public void UnRegister(Action<T> onEvent)
- {
- mOnEvent -= onEvent;
- }
- public void Trigger(T t)
- {
- mOnEvent?.Invoke(t);
- }
- }
- public class EasyEvent<T, K> : IEasyEvent
- {
- private Action<T, K> mOnEvent = (t, k) => { };
- public IUnRegister Register(Action<T, K> onEvent)
- {
- mOnEvent += onEvent;
- return new CustomUnRegister(() =>
- {
- UnRegister(onEvent);
- });
- }
- public void UnRegister(Action<T, K> onEvent)
- {
- mOnEvent -= onEvent;
- }
- public void Trigger(T t, K k)
- {
- mOnEvent?.Invoke(t, k);
- }
- }
- public class EasyEvent<T, K, S> : IEasyEvent
- {
- private Action<T, K, S> mOnEvent = (t, k, s) => { };
- public IUnRegister Register(Action<T, K, S> onEvent)
- {
- mOnEvent += onEvent;
- return new CustomUnRegister(() =>
- {
- UnRegister(onEvent);
- });
- }
- public void UnRegister(Action<T, K, S> onEvent)
- {
- mOnEvent -= onEvent;
- }
- public void Trigger(T t, K k, S s)
- {
- mOnEvent?.Invoke(t, k, s);
- }
- }
- public class EasyEvents
- {
- private static EasyEvents mGlobalEvents = new EasyEvents();
- public static T Get<T>() where T : IEasyEvent
- {
- return mGlobalEvents.GetEvent<T>();
- }
- public static void Register<T>() where T : IEasyEvent, new()
- {
- mGlobalEvents.AddEvent<T>();
- }
- private Dictionary<Type, IEasyEvent> mTypeEvents = new Dictionary<Type, IEasyEvent>();
- public void AddEvent<T>() where T : IEasyEvent, new()
- {
- mTypeEvents.Add(typeof(T), new T());
- }
- public T GetEvent<T>() where T : IEasyEvent
- {
- if (mTypeEvents.TryGetValue(typeof(T), out IEasyEvent e))
- {
- return (T)e;
- }
- return default;
- }
- public T GetOrAddEvent<T>() where T : IEasyEvent, new()
- {
- var eType = typeof(T);
- if (mTypeEvents.TryGetValue(eType, out var e))
- {
- return (T)e;
- }
- var t = new T();
- mTypeEvents.Add(eType, t);
- return t;
- }
- }
- #endregion
- #region Event
- public interface IEvent
- {
- }
- #endregion
- }
|