BlueVersion3.cs 29 KB

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