BlueVersion.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. using System.Threading.Tasks;
  2. using System.Collections.Generic;
  3. using System;
  4. using UnityEngine;
  5. using System.Reflection;
  6. /*
  7. 修改第三版
  8. 将事件从IArchitecture中剥离
  9. */
  10. namespace Blue
  11. {
  12. #region Architecture
  13. /// <summary>
  14. /// 所有功能在 IArchitecture 接口中定义
  15. /// 1. 注册/获取Service、Model、Utility;
  16. /// 2. 发送命令、事件、查询
  17. /// 3. 注册、取消注册事件
  18. /// </summary>
  19. public interface IArchitecture
  20. {
  21. void SetArchitecture(IArchitecture instance);
  22. void InitArchitecture();
  23. void RegisterService<T>(T service) where T : IService;
  24. void RegisterModel<T>(T Model) where T : IModel;
  25. void RegisterUtility<T>(T utility) where T : IUtility;
  26. T GetService<T>() where T : class, IService;
  27. T GetModel<T>() where T : class, IModel;
  28. T GetUtility<T>() where T : class, IUtility;
  29. }
  30. public abstract class AbstractArchitecture<T> : IArchitecture where T : AbstractArchitecture<T>, new()
  31. {
  32. private IOCContainer mContainer = new IOCContainer(); // 将类型、对象存储进IOC容器
  33. private ICommandHandler commandHandler = new DefaultCommandHandler();
  34. private IQueryHandler queryHandler = new DefaultQueryHandler();
  35. private ITypeEventSystem mTypeEventSystem = new TypeEventSystem();
  36. private HashSet<IService> mService;
  37. private HashSet<IModel> mModels;
  38. protected abstract void Init();
  39. private static IArchitecture mArchitectureInstance;
  40. void IArchitecture.SetArchitecture(IArchitecture instance)
  41. {
  42. mArchitectureInstance = instance;
  43. CanGetModelExtension.SetArchitecture(mArchitectureInstance);
  44. CanGetServiceExtension.SetArchitecture(mArchitectureInstance);
  45. CanGetUtilityExtension.SetArchitecture(mArchitectureInstance);
  46. CanSendCommandExtension.SetCommandHandler(commandHandler);
  47. CanSendQueryExtension.SetQueryHandler(queryHandler);
  48. CanRegisterEventExtension.SetTypeEventSystem(mTypeEventSystem);
  49. CanSendEventExtension.SetTypeEventSystem(mTypeEventSystem);
  50. }
  51. void IArchitecture.InitArchitecture()
  52. {
  53. StartInit();
  54. FinishInit();
  55. }
  56. private void StartInit()
  57. {
  58. mService = new HashSet<IService>();
  59. mModels = new HashSet<IModel>();
  60. Init();
  61. foreach (var architectureModel in mModels)
  62. {
  63. architectureModel.OnInit();
  64. }
  65. foreach (var service in mService)
  66. {
  67. service.OnInit();
  68. }
  69. }
  70. private void FinishInit()
  71. {
  72. mService.Clear();
  73. mModels.Clear();
  74. mService = null;
  75. mModels = null;
  76. }
  77. public void RegisterService<TService>(TService service) where TService : IService
  78. {
  79. mContainer.Register<TService>(service);
  80. mService.Add(service);
  81. }
  82. public void RegisterModel<TModel>(TModel model) where TModel : IModel
  83. {
  84. mContainer.Register<TModel>(model);
  85. mModels.Add(model);
  86. }
  87. public void RegisterUtility<TUtility>(TUtility utility) where TUtility : IUtility
  88. {
  89. mContainer.Register<TUtility>(utility);
  90. }
  91. public TService GetService<TService>() where TService : class, IService
  92. {
  93. return mContainer.Get<TService>();
  94. }
  95. public TModel GetModel<TModel>() where TModel : class, IModel
  96. {
  97. return mContainer.Get<TModel>();
  98. }
  99. public TUtility GetUtility<TUtility>() where TUtility : class, IUtility
  100. {
  101. return mContainer.Get<TUtility>();
  102. }
  103. }
  104. /// <summary>
  105. /// 启动 Architecture
  106. /// </summary>
  107. internal sealed class ArchitectureInitiator
  108. {
  109. private ArchitectureInitiator() { }
  110. private static ArchitectureInitiator mInstance;
  111. private IArchitecture architectureInstance = null; // Architecture 实例
  112. private Type architectureType = typeof(IArchitecture);
  113. private bool architectureInited; // Architecture 是否初始化完成
  114. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
  115. private static void Initiate()
  116. {
  117. mInstance = new ArchitectureInitiator();
  118. Type[] typeArr = Assembly.GetExecutingAssembly().GetTypes(); // 获取正在执行的程序集类型数组
  119. for (int i = 0; i < typeArr.Length; i++)
  120. {
  121. Type tmpType = typeArr[i];
  122. if (!tmpType.IsInterface)
  123. {
  124. if (mInstance.IsArchitecture(tmpType))
  125. {
  126. mInstance.CreateArchitectureInstance(tmpType);
  127. }
  128. }
  129. }
  130. mInstance.RegisterArchitecture();
  131. mInstance.InitArchitecture();
  132. }
  133. private void RegisterArchitecture()
  134. {
  135. if (architectureInstance != null)
  136. {
  137. architectureInstance.SetArchitecture(architectureInstance);
  138. }
  139. }
  140. private void InitArchitecture()
  141. {
  142. if (architectureInstance != null)
  143. {
  144. architectureInstance.InitArchitecture();
  145. }
  146. }
  147. private bool IsArchitecture(Type tmpType)
  148. {
  149. if (architectureInited)
  150. {
  151. return false;
  152. }
  153. if (tmpType.IsAbstract)
  154. {
  155. return false;
  156. }
  157. return architectureType.IsAssignableFrom(tmpType); // 确定指定类型 c 的实例是否能分配给当前类型的变量
  158. }
  159. private void CreateArchitectureInstance(Type archiType)
  160. {
  161. if (architectureInstance == null)
  162. {
  163. architectureInstance = (IArchitecture)Activator.CreateInstance(archiType);
  164. architectureInited = true;
  165. }
  166. else
  167. {
  168. throw new Exception("More than one Architecture sub-class in the application,it should be only one!");
  169. }
  170. }
  171. }
  172. public interface IOnEvent<T>
  173. {
  174. void OnEvent(T e);
  175. }
  176. public static class OnGlobalEventExtension
  177. {
  178. public static IUnRegister RegisterEvent<T>(this IOnEvent<T> self) where T : struct, IEvent
  179. {
  180. return TypeEventSystem.Global.Register<T>(self.OnEvent);
  181. }
  182. public static void UnRegisterEvent<T>(this IOnEvent<T> self) where T : struct, IEvent
  183. {
  184. TypeEventSystem.Global.UnRegister<T>(self.OnEvent);
  185. }
  186. }
  187. #endregion
  188. #region Controller
  189. public interface IController : ICanGetUtility, ICanGetService, ICanGetModel,
  190. ICanRegisterEvent, ICanSendQuery, ICanSendCommand
  191. {
  192. }
  193. #endregion
  194. #region Service
  195. public interface IService : ICanGetService, ICanGetModel, ICanGetUtility,
  196. ICanRegisterEvent, ICanSendEvent
  197. {
  198. void OnInit();
  199. }
  200. #endregion
  201. #region Model
  202. public interface IModel : ICanGetUtility, ICanSendEvent
  203. {
  204. void OnInit();
  205. }
  206. #endregion
  207. #region Utility
  208. public interface IUtility
  209. {
  210. }
  211. #endregion
  212. #region Command
  213. public interface ICommand : ICanGetService, ICanGetModel, ICanGetUtility,
  214. ICanSendEvent, ICanSendCommand, ICanSendQuery
  215. {
  216. void OnExcute();
  217. }
  218. public interface ICommand<T> : ICanGetService, ICanGetModel, ICanGetUtility,
  219. ICanSendEvent, ICanSendCommand, ICanSendQuery
  220. {
  221. T OnExcute();
  222. }
  223. public interface ICommandHandler
  224. {
  225. void ExcuteCommand(ICommand command);
  226. void ExcuteCommand<T>() where T : ICommand, new();
  227. T ExcuteCommand<T>(ICommand<T> command);
  228. }
  229. public class DefaultCommandHandler : ICommandHandler
  230. {
  231. public void ExcuteCommand(ICommand command)
  232. {
  233. command.OnExcute();
  234. }
  235. public void ExcuteCommand<T>() where T : ICommand, new()
  236. {
  237. ExcuteCommand(new T());
  238. }
  239. public T ExcuteCommand<T>(ICommand<T> command)
  240. {
  241. return command.OnExcute();
  242. }
  243. }
  244. #endregion
  245. #region Query
  246. public interface IQuery<T> : ICanGetModel, ICanGetService, ICanSendQuery
  247. {
  248. T DoQuery();
  249. }
  250. public interface IQueryAsync : ICanGetModel, ICanGetService, ICanSendQuery
  251. {
  252. void DoQueryAsync<T>(Action<T> onQueryCompleted);
  253. }
  254. public interface IQueryResult<T>
  255. {
  256. void OnQuerySucceed(Action<T> onQuerySucceed);
  257. void OnQueryFailed(Action onQueryFailed);
  258. }
  259. public interface IQueryHandler
  260. {
  261. T DoQuery<T>(IQuery<T> query);
  262. K DoQuery<T, K>() where T : IQuery<K>, new();
  263. IQueryResult<T> DoQueryAsync<T>(IQuery<T> query);
  264. IQueryResult<K> DoQueryAsync<T, K>() where T : IQuery<K>, new();
  265. }
  266. public class DefaultQueryHandler : IQueryHandler
  267. {
  268. public T DoQuery<T>(IQuery<T> query)
  269. {
  270. return query.DoQuery();
  271. }
  272. public K DoQuery<T, K>() where T : IQuery<K>, new()
  273. {
  274. return DoQuery(new T());
  275. }
  276. public IQueryResult<T> DoQueryAsync<T>(IQuery<T> query)
  277. {
  278. DefaultQueryResult<T> queryResult = new DefaultQueryResult<T>();
  279. var queryTask = Task.Run(() =>
  280. {
  281. return query.DoQuery();
  282. });
  283. var awaiter = queryTask.GetAwaiter();
  284. awaiter.OnCompleted(() =>
  285. {
  286. if (queryTask.IsFaulted | queryTask.IsCanceled)
  287. {
  288. queryResult.TriggerFailed();
  289. }
  290. else
  291. {
  292. queryResult.TriggerSuccess(awaiter.GetResult());
  293. }
  294. });
  295. return queryResult;
  296. }
  297. public IQueryResult<K> DoQueryAsync<T, K>() where T : IQuery<K>, new()
  298. {
  299. return DoQueryAsync(new T());
  300. }
  301. }
  302. public class DefaultQueryResult<T> : IQueryResult<T>
  303. {
  304. private Action<T> mOnQuerySucceed;
  305. private Action mOnQueryFailed;
  306. public void OnQuerySucceed(Action<T> onQuerySucceed)
  307. {
  308. mOnQuerySucceed += onQuerySucceed;
  309. }
  310. public void OnQueryFailed(Action onQueryFailed)
  311. {
  312. mOnQueryFailed += onQueryFailed;
  313. }
  314. public void TriggerSuccess(T result)
  315. {
  316. mOnQuerySucceed?.Invoke(result);
  317. }
  318. public void TriggerFailed()
  319. {
  320. mOnQueryFailed?.Invoke();
  321. }
  322. }
  323. #endregion
  324. #region Rule
  325. /// <summary>
  326. /// 能够 GetModel 的接口
  327. /// </summary>
  328. public interface ICanGetModel
  329. {
  330. }
  331. /// <summary>
  332. /// ICanGetModel 的静态扩展 GetModel
  333. /// </summary>
  334. public static class CanGetModelExtension
  335. {
  336. public static T GetModel<T>(this ICanGetModel self) where T : class, IModel
  337. {
  338. return _architecture.GetModel<T>();
  339. }
  340. private static IArchitecture _architecture;
  341. public static void SetArchitecture(IArchitecture architecture)
  342. {
  343. _architecture = architecture;
  344. }
  345. }
  346. /// <summary>
  347. /// 能够 GetService 的接口
  348. /// </summary>
  349. public interface ICanGetService
  350. {
  351. }
  352. /// <summary>
  353. /// ICanGetService 的静态扩展 GetService
  354. /// </summary>
  355. public static class CanGetServiceExtension
  356. {
  357. public static T GetService<T>(this ICanGetService self) where T : class, IService
  358. {
  359. return _architecture.GetService<T>();
  360. }
  361. private static IArchitecture _architecture;
  362. public static void SetArchitecture(IArchitecture architecture)
  363. {
  364. _architecture = architecture;
  365. }
  366. }
  367. /// <summary>
  368. /// 能够 GetUtility 的接口
  369. /// </summary>
  370. public interface ICanGetUtility
  371. {
  372. }
  373. /// <summary>
  374. /// ICanGetUtility 的静态扩展 GetUtility
  375. /// </summary>
  376. public static class CanGetUtilityExtension
  377. {
  378. public static T GetUtility<T>(this ICanGetUtility self) where T : class, IUtility
  379. {
  380. return _architecture.GetUtility<T>();
  381. }
  382. private static IArchitecture _architecture;
  383. public static void SetArchitecture(IArchitecture architecture)
  384. {
  385. _architecture = architecture;
  386. }
  387. }
  388. /// <summary>
  389. /// 能够 RegisterEvent 的接口
  390. /// </summary>
  391. public interface ICanRegisterEvent
  392. {
  393. }
  394. /// <summary>
  395. /// ICanRegisterEvent 的静态扩展 RegisterEvent
  396. /// </summary>
  397. public static class CanRegisterEventExtension
  398. {
  399. public static IUnRegister RegisterEvent<T>(this ICanRegisterEvent self, Action<T> onEvent) where T : IEvent
  400. {
  401. if (typeEventSystem == null) return null;
  402. return typeEventSystem.Register<T>(onEvent);
  403. }
  404. public static void UnRegisterEvent<T>(this ICanRegisterEvent self, Action<T> onEvent) where T : IEvent
  405. {
  406. typeEventSystem.UnRegister<T>(onEvent);
  407. }
  408. private static ITypeEventSystem typeEventSystem;
  409. public static void SetTypeEventSystem(ITypeEventSystem mTypeEventSystem)
  410. {
  411. typeEventSystem = mTypeEventSystem;
  412. }
  413. }
  414. /// <summary>
  415. /// 能够 SendCommand 的接口
  416. /// </summary>
  417. public interface ICanSendCommand
  418. {
  419. }
  420. /// <summary>
  421. /// ICanSendCommand 的静态扩展 SendCommand
  422. /// </summary>
  423. public static class CanSendCommandExtension
  424. {
  425. public static void SendCommand<T>(this ICanSendCommand self) where T : ICommand, new()
  426. {
  427. mCommandHandler.ExcuteCommand<T>();
  428. }
  429. public static void SendCommand<T>(this ICanSendCommand self, T command) where T : ICommand
  430. {
  431. mCommandHandler.ExcuteCommand(command);
  432. }
  433. public static T SendCommand<T>(this ICanSendCommand self, ICommand<T> command)
  434. {
  435. return mCommandHandler.ExcuteCommand(command);
  436. }
  437. private static ICommandHandler mCommandHandler;
  438. public static void SetCommandHandler(ICommandHandler commandHandler)
  439. {
  440. mCommandHandler = commandHandler;
  441. }
  442. }
  443. /// <summary>
  444. /// 能够 SendEvent 的接口
  445. /// </summary>
  446. public interface ICanSendEvent
  447. {
  448. }
  449. /// <summary>
  450. /// ICanSendEvent 的静态扩展 SendEvent
  451. /// </summary>
  452. public static class CanSendEventExtension
  453. {
  454. public static void SendEvent<T>(this ICanSendEvent self) where T : IEvent, new()
  455. {
  456. typeEventSystem.Send<T>();
  457. }
  458. public static void SendEvent<T>(this ICanSendEvent self, T e) where T : IEvent
  459. {
  460. typeEventSystem.Send<T>(e);
  461. }
  462. private static ITypeEventSystem typeEventSystem;
  463. public static void SetTypeEventSystem(ITypeEventSystem mTypeEventSystem)
  464. {
  465. typeEventSystem = mTypeEventSystem;
  466. }
  467. }
  468. /// <summary>
  469. /// 能够 SendQuery 的接口
  470. /// </summary>
  471. public interface ICanSendQuery
  472. {
  473. }
  474. /// <summary>
  475. /// ICanSendQuery 的静态扩展 SendQuery
  476. /// </summary>
  477. public static class CanSendQueryExtension
  478. {
  479. public static T SendQuery<T>(this ICanSendQuery self, IQuery<T> queryInstance)
  480. {
  481. return _queryHandler.DoQuery<T>(queryInstance);
  482. }
  483. public static K SendQuery<T, K>(this ICanSendQuery self) where T : IQuery<K>, new()
  484. {
  485. return _queryHandler.DoQuery<T, K>();
  486. }
  487. public static IQueryResult<T> SendQueryAsync<T>(this ICanSendQuery self, IQuery<T> queryInstance)
  488. {
  489. return _queryHandler.DoQueryAsync<T>(queryInstance);
  490. }
  491. public static IQueryResult<K> SendQueryAsync<T, K>(this ICanSendQuery self) where T : IQuery<K>, new()
  492. {
  493. return _queryHandler.DoQueryAsync<T, K>();
  494. }
  495. private static IQueryHandler _queryHandler;
  496. public static void SetQueryHandler(IQueryHandler queryHandler)
  497. {
  498. _queryHandler = queryHandler;
  499. }
  500. }
  501. #endregion
  502. #region TypeEventSystem
  503. /// <summary>
  504. /// 取消监听接口
  505. /// </summary>
  506. public interface IUnRegister
  507. {
  508. void UnRegister();
  509. }
  510. /// <summary>
  511. /// 取消监听列表接口
  512. /// </summary>
  513. public interface IUnRegisterList
  514. {
  515. List<IUnRegister> UnRegistersList { get; }
  516. }
  517. /// <summary>
  518. /// 取消监听列表接口的静态扩展
  519. /// 【IUnRegister 添加进 IUnRegisterList】
  520. /// 【IUnRegisterList全部取消监听】
  521. /// </summary>
  522. public static class IUnRegisterListExtension
  523. {
  524. public static void AddToUnRegisterList(this IUnRegister self, IUnRegisterList unRegisterList)
  525. {
  526. unRegisterList.UnRegistersList.Add(self);
  527. }
  528. public static void UnRegisterAll(this IUnRegisterList self)
  529. {
  530. foreach (IUnRegister unRegister in self.UnRegistersList)
  531. {
  532. unRegister.UnRegister();
  533. }
  534. self.UnRegistersList.Clear();
  535. }
  536. }
  537. /// <summary>
  538. /// 取消监听的类
  539. /// </summary>
  540. public struct CustomUnRegister : IUnRegister
  541. {
  542. private Action mOnUnregister { get; set; }
  543. public CustomUnRegister(Action onUnRegsiter)
  544. {
  545. mOnUnregister = onUnRegsiter;
  546. }
  547. public void UnRegister()
  548. {
  549. mOnUnregister?.Invoke();
  550. mOnUnregister = null;
  551. }
  552. }
  553. /// <summary>
  554. /// 物体销毁时触发取消监听
  555. /// </summary>
  556. public class UnRegisterOnDestroyTrigger : MonoBehaviour
  557. {
  558. private readonly HashSet<IUnRegister> mUnRegisters = new HashSet<IUnRegister>();
  559. public void AddUnRegister(IUnRegister unRegister)
  560. {
  561. mUnRegisters.Add(unRegister);
  562. }
  563. public void RemoveUnRegister(IUnRegister unRegister)
  564. {
  565. mUnRegisters.Remove(unRegister);
  566. }
  567. private void OnDestroy()
  568. {
  569. foreach (IUnRegister unRegister in mUnRegisters)
  570. {
  571. unRegister.UnRegister();
  572. }
  573. mUnRegisters.Clear();
  574. }
  575. }
  576. /// <summary>
  577. /// 取消监听的静态扩展
  578. /// 【物体销毁时触发取消监听】
  579. /// 【组件所属物体销毁时触发取消监听】
  580. /// </summary>
  581. public static class UnRegisterExtension
  582. {
  583. /// <summary>
  584. /// 物体:物体销毁时触发取消监听
  585. /// </summary>
  586. /// <param name="unRegister">取消监听的接口</param>
  587. /// <param name="component">物体对象</param>
  588. /// <returns>取消监听的接口</returns>
  589. public static IUnRegister UnRegisterWhenGameObjectDestroyed(this IUnRegister unRegister, GameObject go)
  590. {
  591. UnRegisterOnDestroyTrigger trigger = go.GetComponent<UnRegisterOnDestroyTrigger>();
  592. if (!trigger)
  593. {
  594. trigger = go.AddComponent<UnRegisterOnDestroyTrigger>();
  595. }
  596. trigger.AddUnRegister(unRegister);
  597. return unRegister;
  598. }
  599. /// <summary>
  600. /// 组件:物体销毁时触发取消监听
  601. /// </summary>
  602. /// <param name="unRegister">取消监听的接口</param>
  603. /// <param name="component">组件对象</param>
  604. /// <returns>取消监听的接口</returns>
  605. public static IUnRegister UnRegisterWhenGameObjectDestroyed<T>(this IUnRegister unRegister, T component) where T : Component
  606. {
  607. return unRegister.UnRegisterWhenGameObjectDestroyed(component.gameObject);
  608. }
  609. }
  610. public interface ITypeEventSystem
  611. {
  612. IUnRegister Register<T>(Action<T> onEvent) where T : IEvent;
  613. void UnRegister<T>(Action<T> onEvent) where T : IEvent;
  614. void Send<T>() where T : IEvent, new();
  615. void Send<T>(T e) where T : IEvent;
  616. }
  617. public class TypeEventSystem : ITypeEventSystem
  618. {
  619. private readonly EasyEvents mEvents = new EasyEvents();
  620. public static readonly TypeEventSystem Global = new TypeEventSystem();
  621. public void Send<T>() where T : IEvent, new()
  622. {
  623. mEvents.GetEvent<EasyEvent<T>>()?.Trigger(new T());
  624. }
  625. public void Send<T>(T e) where T : IEvent
  626. {
  627. mEvents.GetEvent<EasyEvent<T>>()?.Trigger(e);
  628. }
  629. public IUnRegister Register<T>(Action<T> onEvent) where T : IEvent
  630. {
  631. var e = mEvents.GetOrAddEvent<EasyEvent<T>>();
  632. return e.Register(onEvent);
  633. }
  634. public void UnRegister<T>(Action<T> onEvent) where T : IEvent
  635. {
  636. var e = mEvents.GetEvent<EasyEvent<T>>();
  637. if (e != null)
  638. {
  639. e.UnRegister(onEvent);
  640. }
  641. }
  642. }
  643. #endregion
  644. #region IOC
  645. /// <summary>
  646. /// IOC 容器 ,将类型对应的对象存储进字典
  647. /// 【注册 + 获取】
  648. /// </summary>
  649. public class IOCContainer
  650. {
  651. private Dictionary<Type, object> mInstances = new Dictionary<Type, object>();
  652. /// <summary>
  653. /// IOC 容器注册方法
  654. /// </summary>
  655. /// <param name="instance">实例</param>
  656. /// <typeparam name="T">指定类型</typeparam>
  657. public void Register<T>(T instance)
  658. {
  659. Type key = typeof(T);
  660. if (mInstances.ContainsKey(key))
  661. {
  662. mInstances[key] = instance;
  663. }
  664. else
  665. {
  666. mInstances.Add(key, instance);
  667. }
  668. }
  669. /// <summary>
  670. /// IOC 容器获取方法
  671. /// </summary>
  672. /// <typeparam name="T">指定类型</typeparam>
  673. public T Get<T>() where T : class
  674. {
  675. Type key = typeof(T);
  676. if (mInstances.TryGetValue(key, out object retInstance))
  677. {
  678. return retInstance as T;
  679. }
  680. return null;
  681. }
  682. }
  683. #endregion
  684. #region BindableProperty
  685. public interface IReadonlyBindableProperty<T>
  686. {
  687. T Value { get; }
  688. IUnRegister Register(Action<T> onValueChanged);
  689. void UnRegister(Action<T> onValueChanged);
  690. IUnRegister RegisterWithInitValue(Action<T> action);
  691. }
  692. public interface IBindableProperty<T> : IReadonlyBindableProperty<T>
  693. {
  694. new T Value { get; set; }
  695. void SetValueWithoutEvent(T newValue);
  696. }
  697. public class BindableProperty<T> : IBindableProperty<T>
  698. {
  699. private Action<T> mOnValueChanged = (v) => { };
  700. public BindableProperty(T defaultValue = default)
  701. {
  702. mValue = defaultValue;
  703. }
  704. protected T mValue;
  705. public T Value
  706. {
  707. get => GetValue();
  708. set
  709. {
  710. if (value == null && mValue == null) return;
  711. if (value != null && value.Equals(mValue)) return;
  712. SetValue(value);
  713. mOnValueChanged?.Invoke(value);
  714. }
  715. }
  716. protected virtual void SetValue(T newValue)
  717. {
  718. mValue = newValue;
  719. }
  720. protected virtual T GetValue()
  721. {
  722. return mValue;
  723. }
  724. public void SetValueWithoutEvent(T newValue)
  725. {
  726. mValue = newValue;
  727. }
  728. public IUnRegister Register(Action<T> onValueChanged)
  729. {
  730. mOnValueChanged += onValueChanged;
  731. return new BindablePropertyUnRegister<T>()
  732. {
  733. BindableProperty = this,
  734. OnValueChanged = onValueChanged
  735. };
  736. }
  737. public IUnRegister RegisterWithInitValue(Action<T> onValueChanged)
  738. {
  739. onValueChanged(mValue);
  740. return Register(onValueChanged);
  741. }
  742. public static implicit operator T(BindableProperty<T> property)
  743. {
  744. return property.Value;
  745. }
  746. public override string ToString()
  747. {
  748. return Value.ToString();
  749. }
  750. public void UnRegister(Action<T> onValueChanged)
  751. {
  752. mOnValueChanged -= onValueChanged;
  753. }
  754. }
  755. public class BindablePropertyUnRegister<T> : IUnRegister
  756. {
  757. public BindableProperty<T> BindableProperty { get; set; }
  758. public Action<T> OnValueChanged { get; set; }
  759. public void UnRegister()
  760. {
  761. BindableProperty.UnRegister(OnValueChanged);
  762. BindableProperty = null;
  763. OnValueChanged = null;
  764. }
  765. }
  766. #endregion
  767. #region EasyEvent
  768. public interface IEasyEvent
  769. {
  770. }
  771. public class EasyEvent : IEasyEvent
  772. {
  773. private Action mOnEvent = () => { };
  774. public IUnRegister Register(Action onEvent)
  775. {
  776. mOnEvent += onEvent;
  777. return new CustomUnRegister(() =>
  778. {
  779. UnRegister(onEvent);
  780. });
  781. }
  782. public void UnRegister(Action onEvent)
  783. {
  784. mOnEvent -= onEvent;
  785. }
  786. public void Trigger()
  787. {
  788. mOnEvent?.Invoke();
  789. }
  790. }
  791. public class EasyEvent<T> : IEasyEvent
  792. {
  793. private Action<T> mOnEvent = e => { };
  794. public IUnRegister Register(Action<T> onEvent)
  795. {
  796. mOnEvent += onEvent;
  797. return new CustomUnRegister(() =>
  798. {
  799. UnRegister(onEvent);
  800. });
  801. }
  802. public void UnRegister(Action<T> onEvent)
  803. {
  804. mOnEvent -= onEvent;
  805. }
  806. public void Trigger(T t)
  807. {
  808. mOnEvent?.Invoke(t);
  809. }
  810. }
  811. public class EasyEvent<T, K> : IEasyEvent
  812. {
  813. private Action<T, K> mOnEvent = (t, k) => { };
  814. public IUnRegister Register(Action<T, K> onEvent)
  815. {
  816. mOnEvent += onEvent;
  817. return new CustomUnRegister(() =>
  818. {
  819. UnRegister(onEvent);
  820. });
  821. }
  822. public void UnRegister(Action<T, K> onEvent)
  823. {
  824. mOnEvent -= onEvent;
  825. }
  826. public void Trigger(T t, K k)
  827. {
  828. mOnEvent?.Invoke(t, k);
  829. }
  830. }
  831. public class EasyEvent<T, K, S> : IEasyEvent
  832. {
  833. private Action<T, K, S> mOnEvent = (t, k, s) => { };
  834. public IUnRegister Register(Action<T, K, S> onEvent)
  835. {
  836. mOnEvent += onEvent;
  837. return new CustomUnRegister(() =>
  838. {
  839. UnRegister(onEvent);
  840. });
  841. }
  842. public void UnRegister(Action<T, K, S> onEvent)
  843. {
  844. mOnEvent -= onEvent;
  845. }
  846. public void Trigger(T t, K k, S s)
  847. {
  848. mOnEvent?.Invoke(t, k, s);
  849. }
  850. }
  851. public class EasyEvents
  852. {
  853. private static EasyEvents mGlobalEvents = new EasyEvents();
  854. public static T Get<T>() where T : IEasyEvent
  855. {
  856. return mGlobalEvents.GetEvent<T>();
  857. }
  858. public static void Register<T>() where T : IEasyEvent, new()
  859. {
  860. mGlobalEvents.AddEvent<T>();
  861. }
  862. private Dictionary<Type, IEasyEvent> mTypeEvents = new Dictionary<Type, IEasyEvent>();
  863. public void AddEvent<T>() where T : IEasyEvent, new()
  864. {
  865. mTypeEvents.Add(typeof(T), new T());
  866. }
  867. public T GetEvent<T>() where T : IEasyEvent
  868. {
  869. if (mTypeEvents.TryGetValue(typeof(T), out IEasyEvent e))
  870. {
  871. return (T)e;
  872. }
  873. return default;
  874. }
  875. public T GetOrAddEvent<T>() where T : IEasyEvent, new()
  876. {
  877. var eType = typeof(T);
  878. if (mTypeEvents.TryGetValue(eType, out var e))
  879. {
  880. return (T)e;
  881. }
  882. var t = new T();
  883. mTypeEvents.Add(eType, t);
  884. return t;
  885. }
  886. }
  887. #endregion
  888. #region Event
  889. public interface IEvent
  890. {
  891. }
  892. #endregion
  893. }