BlueFramework.cs 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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<T>(T service) where T : IService
  78. {
  79. mContainer.Register<T>(service);
  80. mService.Add(service);
  81. }
  82. public void RegisterModel<T>(T model) where T : IModel
  83. {
  84. mContainer.Register<T>(model);
  85. mModels.Add(model);
  86. }
  87. public void RegisterUtility<T>(T utility) where T : IUtility
  88. {
  89. mContainer.Register<T>(utility);
  90. }
  91. public T GetService<T>() where T : class, IService
  92. {
  93. return mContainer.Get<T>();
  94. }
  95. public T GetModel<T>() where T : class, IModel
  96. {
  97. return mContainer.Get<T>();
  98. }
  99. public T GetUtility<T>() where T : class, IUtility
  100. {
  101. return mContainer.Get<T>();
  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. return typeEventSystem.Register<T>(onEvent);
  402. }
  403. public static void UnRegisterEvent<T>(this ICanRegisterEvent self, Action<T> onEvent) where T : IEvent
  404. {
  405. typeEventSystem.UnRegister<T>(onEvent);
  406. }
  407. private static ITypeEventSystem typeEventSystem;
  408. public static void SetTypeEventSystem(ITypeEventSystem mTypeEventSystem)
  409. {
  410. typeEventSystem = mTypeEventSystem;
  411. }
  412. }
  413. /// <summary>
  414. /// 能够 SendCommand 的接口
  415. /// </summary>
  416. public interface ICanSendCommand
  417. {
  418. }
  419. /// <summary>
  420. /// ICanSendCommand 的静态扩展 SendCommand
  421. /// </summary>
  422. public static class CanSendCommandExtension
  423. {
  424. public static void SendCommand<T>(this ICanSendCommand self) where T : ICommand, new()
  425. {
  426. mCommandHandler.ExcuteCommand<T>();
  427. }
  428. public static void SendCommand<T>(this ICanSendCommand self, T command) where T : ICommand
  429. {
  430. mCommandHandler.ExcuteCommand(command);
  431. }
  432. public static T SendCommand<T>(this ICanSendCommand self, ICommand<T> command)
  433. {
  434. return mCommandHandler.ExcuteCommand(command);
  435. }
  436. private static ICommandHandler mCommandHandler;
  437. public static void SetCommandHandler(ICommandHandler commandHandler)
  438. {
  439. mCommandHandler = commandHandler;
  440. }
  441. }
  442. /// <summary>
  443. /// 能够 SendEvent 的接口
  444. /// </summary>
  445. public interface ICanSendEvent
  446. {
  447. }
  448. /// <summary>
  449. /// ICanSendEvent 的静态扩展 SendEvent
  450. /// </summary>
  451. public static class CanSendEventExtension
  452. {
  453. public static void SendEvent<T>(this ICanSendEvent self) where T : IEvent, new()
  454. {
  455. typeEventSystem.Send<T>();
  456. }
  457. public static void SendEvent<T>(this ICanSendEvent self, T e) where T : IEvent
  458. {
  459. typeEventSystem.Send<T>(e);
  460. }
  461. private static ITypeEventSystem typeEventSystem;
  462. public static void SetTypeEventSystem(ITypeEventSystem mTypeEventSystem)
  463. {
  464. typeEventSystem = mTypeEventSystem;
  465. }
  466. }
  467. /// <summary>
  468. /// 能够 SendQuery 的接口
  469. /// </summary>
  470. public interface ICanSendQuery
  471. {
  472. }
  473. /// <summary>
  474. /// ICanSendQuery 的静态扩展 SendQuery
  475. /// </summary>
  476. public static class CanSendQueryExtension
  477. {
  478. public static T SendQuery<T>(this ICanSendQuery self, IQuery<T> queryInstance)
  479. {
  480. return _queryHandler.DoQuery<T>(queryInstance);
  481. }
  482. public static K SendQuery<T, K>(this ICanSendQuery self) where T : IQuery<K>, new()
  483. {
  484. return _queryHandler.DoQuery<T, K>();
  485. }
  486. public static IQueryResult<T> SendQueryAsync<T>(this ICanSendQuery self, IQuery<T> queryInstance)
  487. {
  488. return _queryHandler.DoQueryAsync<T>(queryInstance);
  489. }
  490. public static IQueryResult<K> SendQueryAsync<T, K>(this ICanSendQuery self) where T : IQuery<K>, new()
  491. {
  492. return _queryHandler.DoQueryAsync<T, K>();
  493. }
  494. private static IQueryHandler _queryHandler;
  495. public static void SetQueryHandler(IQueryHandler queryHandler)
  496. {
  497. _queryHandler = queryHandler;
  498. }
  499. }
  500. #endregion
  501. #region TypeEventSystem
  502. /// <summary>
  503. /// 取消监听接口
  504. /// </summary>
  505. public interface IUnRegister
  506. {
  507. void UnRegister();
  508. }
  509. /// <summary>
  510. /// 取消监听列表接口
  511. /// </summary>
  512. public interface IUnRegisterList
  513. {
  514. List<IUnRegister> UnRegistersList { get; }
  515. }
  516. /// <summary>
  517. /// 取消监听列表接口的静态扩展
  518. /// 【IUnRegister 添加进 IUnRegisterList】
  519. /// 【IUnRegisterList全部取消监听】
  520. /// </summary>
  521. public static class IUnRegisterListExtension
  522. {
  523. public static void AddToUnRegisterList(this IUnRegister self, IUnRegisterList unRegisterList)
  524. {
  525. unRegisterList.UnRegistersList.Add(self);
  526. }
  527. public static void UnRegisterAll(this IUnRegisterList self)
  528. {
  529. foreach (IUnRegister unRegister in self.UnRegistersList)
  530. {
  531. unRegister.UnRegister();
  532. }
  533. self.UnRegistersList.Clear();
  534. }
  535. }
  536. /// <summary>
  537. /// 取消监听的类
  538. /// </summary>
  539. public struct CustomUnRegister : IUnRegister
  540. {
  541. private Action mOnUnregister { get; set; }
  542. public CustomUnRegister(Action onUnRegsiter)
  543. {
  544. mOnUnregister = onUnRegsiter;
  545. }
  546. public void UnRegister()
  547. {
  548. mOnUnregister?.Invoke();
  549. mOnUnregister = null;
  550. }
  551. }
  552. /// <summary>
  553. /// 物体销毁时触发取消监听
  554. /// </summary>
  555. public class UnRegisterOnDestroyTrigger : MonoBehaviour
  556. {
  557. private readonly HashSet<IUnRegister> mUnRegisters = new HashSet<IUnRegister>();
  558. public void AddUnRegister(IUnRegister unRegister)
  559. {
  560. mUnRegisters.Add(unRegister);
  561. }
  562. public void RemoveUnRegister(IUnRegister unRegister)
  563. {
  564. mUnRegisters.Remove(unRegister);
  565. }
  566. private void OnDestroy()
  567. {
  568. foreach (IUnRegister unRegister in mUnRegisters)
  569. {
  570. unRegister.UnRegister();
  571. }
  572. mUnRegisters.Clear();
  573. }
  574. }
  575. /// <summary>
  576. /// 取消监听的静态扩展
  577. /// 【物体销毁时触发取消监听】
  578. /// 【组件所属物体销毁时触发取消监听】
  579. /// </summary>
  580. public static class UnRegisterExtension
  581. {
  582. /// <summary>
  583. /// 物体:物体销毁时触发取消监听
  584. /// </summary>
  585. /// <param name="unRegister">取消监听的接口</param>
  586. /// <param name="component">物体对象</param>
  587. /// <returns>取消监听的接口</returns>
  588. public static IUnRegister UnRegisterWhenGameObjectDestroyed(this IUnRegister unRegister, GameObject go)
  589. {
  590. UnRegisterOnDestroyTrigger trigger = go.GetComponent<UnRegisterOnDestroyTrigger>();
  591. if (!trigger)
  592. {
  593. trigger = go.AddComponent<UnRegisterOnDestroyTrigger>();
  594. }
  595. trigger.AddUnRegister(unRegister);
  596. return unRegister;
  597. }
  598. /// <summary>
  599. /// 组件:物体销毁时触发取消监听
  600. /// </summary>
  601. /// <param name="unRegister">取消监听的接口</param>
  602. /// <param name="component">组件对象</param>
  603. /// <returns>取消监听的接口</returns>
  604. public static IUnRegister UnRegisterWhenGameObjectDestroyed<T>(this IUnRegister unRegister, T component) where T : Component
  605. {
  606. return unRegister.UnRegisterWhenGameObjectDestroyed(component.gameObject);
  607. }
  608. }
  609. public interface ITypeEventSystem
  610. {
  611. IUnRegister Register<T>(Action<T> onEvent) where T: IEvent;
  612. void UnRegister<T>(Action<T> onEvent) where T: IEvent;
  613. void Send<T>() where T: IEvent, new();
  614. void Send<T>(T e) where T: IEvent;
  615. }
  616. public class TypeEventSystem:ITypeEventSystem
  617. {
  618. private readonly EasyEvents mEvents = new EasyEvents();
  619. public static readonly TypeEventSystem Global = new TypeEventSystem();
  620. public void Send<T>() where T : IEvent,new()
  621. {
  622. mEvents.GetEvent<EasyEvent<T>>()?.Trigger(new T());
  623. }
  624. public void Send<T>(T e) where T: IEvent
  625. {
  626. mEvents.GetEvent<EasyEvent<T>>()?.Trigger(e);
  627. }
  628. public IUnRegister Register<T>(Action<T> onEvent)where T: IEvent
  629. {
  630. var e = mEvents.GetOrAddEvent<EasyEvent<T>>();
  631. return e.Register(onEvent);
  632. }
  633. public void UnRegister<T>(Action<T> onEvent)where T: IEvent
  634. {
  635. var e = mEvents.GetEvent<EasyEvent<T>>();
  636. if (e != null)
  637. {
  638. e.UnRegister(onEvent);
  639. }
  640. }
  641. }
  642. #endregion
  643. #region IOC
  644. /// <summary>
  645. /// IOC 容器 ,将类型对应的对象存储进字典
  646. /// 【注册 + 获取】
  647. /// </summary>
  648. public class IOCContainer
  649. {
  650. private Dictionary<Type, object> mInstances = new Dictionary<Type, object>();
  651. /// <summary>
  652. /// IOC 容器注册方法
  653. /// </summary>
  654. /// <param name="instance">实例</param>
  655. /// <typeparam name="T">指定类型</typeparam>
  656. public void Register<T>(T instance)
  657. {
  658. Type key = typeof(T);
  659. if (mInstances.ContainsKey(key))
  660. {
  661. mInstances[key] = instance;
  662. }
  663. else
  664. {
  665. mInstances.Add(key, instance);
  666. }
  667. }
  668. /// <summary>
  669. /// IOC 容器获取方法
  670. /// </summary>
  671. /// <typeparam name="T">指定类型</typeparam>
  672. public T Get<T>() where T : class
  673. {
  674. Type key = typeof(T);
  675. if (mInstances.TryGetValue(key, out object retInstance))
  676. {
  677. return retInstance as T;
  678. }
  679. return null;
  680. }
  681. }
  682. #endregion
  683. #region BindableProperty
  684. public interface IReadonlyBindableProperty<T>
  685. {
  686. T Value { get; }
  687. IUnRegister Register(Action<T> onValueChanged);
  688. void UnRegister(Action<T> onValueChanged);
  689. IUnRegister RegisterWithInitValue(Action<T> action);
  690. }
  691. public interface IBindableProperty<T> : IReadonlyBindableProperty<T>
  692. {
  693. new T Value { get; set; }
  694. void SetValueWithoutEvent(T newValue);
  695. }
  696. public class BindableProperty<T> : IBindableProperty<T>
  697. {
  698. private Action<T> mOnValueChanged = (v) => { };
  699. public BindableProperty(T defaultValue = default)
  700. {
  701. mValue = defaultValue;
  702. }
  703. protected T mValue;
  704. public T Value
  705. {
  706. get => GetValue();
  707. set
  708. {
  709. if (value == null && mValue == null) return;
  710. if (value != null && value.Equals(mValue)) return;
  711. SetValue(value);
  712. mOnValueChanged?.Invoke(value);
  713. }
  714. }
  715. protected virtual void SetValue(T newValue)
  716. {
  717. mValue = newValue;
  718. }
  719. protected virtual T GetValue()
  720. {
  721. return mValue;
  722. }
  723. public void SetValueWithoutEvent(T newValue)
  724. {
  725. mValue = newValue;
  726. }
  727. public IUnRegister Register(Action<T> onValueChanged)
  728. {
  729. mOnValueChanged += onValueChanged;
  730. return new BindablePropertyUnRegister<T>()
  731. {
  732. BindableProperty = this,
  733. OnValueChanged = onValueChanged
  734. };
  735. }
  736. public IUnRegister RegisterWithInitValue(Action<T> onValueChanged)
  737. {
  738. onValueChanged(mValue);
  739. return Register(onValueChanged);
  740. }
  741. public static implicit operator T(BindableProperty<T> property)
  742. {
  743. return property.Value;
  744. }
  745. public override string ToString()
  746. {
  747. return Value.ToString();
  748. }
  749. public void UnRegister(Action<T> onValueChanged)
  750. {
  751. mOnValueChanged -= onValueChanged;
  752. }
  753. }
  754. public class BindablePropertyUnRegister<T> : IUnRegister
  755. {
  756. public BindableProperty<T> BindableProperty { get; set; }
  757. public Action<T> OnValueChanged { get; set; }
  758. public void UnRegister()
  759. {
  760. BindableProperty.UnRegister(OnValueChanged);
  761. BindableProperty = null;
  762. OnValueChanged = null;
  763. }
  764. }
  765. #endregion
  766. #region EasyEvent
  767. public interface IEasyEvent
  768. {
  769. }
  770. public class EasyEvent : IEasyEvent
  771. {
  772. private Action mOnEvent = () => { };
  773. public IUnRegister Register(Action onEvent)
  774. {
  775. mOnEvent += onEvent;
  776. return new CustomUnRegister(() =>
  777. {
  778. UnRegister(onEvent);
  779. });
  780. }
  781. public void UnRegister(Action onEvent)
  782. {
  783. mOnEvent -= onEvent;
  784. }
  785. public void Trigger()
  786. {
  787. mOnEvent?.Invoke();
  788. }
  789. }
  790. public class EasyEvent<T> : IEasyEvent
  791. {
  792. private Action<T> mOnEvent = e => { };
  793. public IUnRegister Register(Action<T> onEvent)
  794. {
  795. mOnEvent += onEvent;
  796. return new CustomUnRegister(() =>
  797. {
  798. UnRegister(onEvent);
  799. });
  800. }
  801. public void UnRegister(Action<T> onEvent)
  802. {
  803. mOnEvent -= onEvent;
  804. }
  805. public void Trigger(T t)
  806. {
  807. mOnEvent?.Invoke(t);
  808. }
  809. }
  810. public class EasyEvent<T, K> : IEasyEvent
  811. {
  812. private Action<T, K> mOnEvent = (t, k) => { };
  813. public IUnRegister Register(Action<T, K> onEvent)
  814. {
  815. mOnEvent += onEvent;
  816. return new CustomUnRegister(() =>
  817. {
  818. UnRegister(onEvent);
  819. });
  820. }
  821. public void UnRegister(Action<T, K> onEvent)
  822. {
  823. mOnEvent -= onEvent;
  824. }
  825. public void Trigger(T t, K k)
  826. {
  827. mOnEvent?.Invoke(t, k);
  828. }
  829. }
  830. public class EasyEvent<T, K, S> : IEasyEvent
  831. {
  832. private Action<T, K, S> mOnEvent = (t, k, s) => { };
  833. public IUnRegister Register(Action<T, K, S> onEvent)
  834. {
  835. mOnEvent += onEvent;
  836. return new CustomUnRegister(() =>
  837. {
  838. UnRegister(onEvent);
  839. });
  840. }
  841. public void UnRegister(Action<T, K, S> onEvent)
  842. {
  843. mOnEvent -= onEvent;
  844. }
  845. public void Trigger(T t, K k, S s)
  846. {
  847. mOnEvent?.Invoke(t, k, s);
  848. }
  849. }
  850. public class EasyEvents
  851. {
  852. private static EasyEvents mGlobalEvents = new EasyEvents();
  853. public static T Get<T>() where T : IEasyEvent
  854. {
  855. return mGlobalEvents.GetEvent<T>();
  856. }
  857. public static void Register<T>() where T : IEasyEvent, new()
  858. {
  859. mGlobalEvents.AddEvent<T>();
  860. }
  861. private Dictionary<Type, IEasyEvent> mTypeEvents = new Dictionary<Type, IEasyEvent>();
  862. public void AddEvent<T>() where T : IEasyEvent, new()
  863. {
  864. mTypeEvents.Add(typeof(T), new T());
  865. }
  866. public T GetEvent<T>() where T : IEasyEvent
  867. {
  868. if (mTypeEvents.TryGetValue(typeof(T), out IEasyEvent e))
  869. {
  870. return (T)e;
  871. }
  872. return default;
  873. }
  874. public T GetOrAddEvent<T>() where T : IEasyEvent, new()
  875. {
  876. var eType = typeof(T);
  877. if (mTypeEvents.TryGetValue(eType, out var e))
  878. {
  879. return (T)e;
  880. }
  881. var t = new T();
  882. mTypeEvents.Add(eType, t);
  883. return t;
  884. }
  885. }
  886. #endregion
  887. #region Event
  888. public interface IEvent
  889. {
  890. }
  891. #endregion
  892. }