You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

401 lines
9.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using SUISS.Core;
  4. using UnityEngine;
  5. namespace SUISSEngine
  6. {
  7. public sealed class TimerManager : SingletonMonobehaviour<TimerManager>
  8. {
  9. protected override void Awake()
  10. {
  11. base.Awake();
  12. if (!this._isValidNewInstance)
  13. {
  14. return;
  15. }
  16. base.transform.parent = null;
  17. UnityEngine.Object.DontDestroyOnLoad(this);
  18. }
  19. private void Update()
  20. {
  21. this.FireGameTimers(ref this._gameTimersDirty, this._gameTimers);
  22. this.FireClockTimers(ref this._utcClockTimersDirty, this._utcClockTimers, DateTime.UtcNow);
  23. this.FireClockTimers(ref this._localClockTimersDirty, this._localClockTimers, DateTime.Now);
  24. }
  25. protected override void OnDestroy()
  26. {
  27. base.OnDestroy();
  28. this.CancelTimers<TimerManager.GameTimer>(this._gameTimers);
  29. this.CancelTimers<TimerManager.ClockTimer>(this._utcClockTimers);
  30. this.CancelTimers<TimerManager.ClockTimer>(this._localClockTimers);
  31. }
  32. public Timer CreateGameTimer(float interval, Action action)
  33. {
  34. return this.CreateGameTimer(interval, interval, action);
  35. }
  36. [Obsolete("Use CreateGameTimer(float delay, float interval, Action action) with delay 0.0f")]
  37. public Timer CreateGameTimer(float interval, bool fireNow, Action action)
  38. {
  39. return this.CreateGameTimer(0f, interval, action);
  40. }
  41. public Timer CreateGameTimer(float delay, float interval, Action action)
  42. {
  43. TimerManager.GameTimer gameTimer = new TimerManager.GameTimer(Time.time + delay, interval, action);
  44. this._gameTimers.Add(gameTimer);
  45. this._gameTimersDirty = true;
  46. return gameTimer;
  47. }
  48. public Timer CreateOnceGameTimer(float delay, Action action)
  49. {
  50. TimerManager.GameTimer gameTimer = new TimerManager.GameTimer(Time.time + delay, action);
  51. this._gameTimers.Add(gameTimer);
  52. this._gameTimersDirty = true;
  53. return gameTimer;
  54. }
  55. public Timer CreateClockTimer(DateTimeKind kind, TimeSpan interval, Action action)
  56. {
  57. return this.CreateClockTimer(kind, TimeSpan.Zero, interval, action);
  58. }
  59. public Timer CreateClockTimer(DateTimeKind kind, TimeSpan offset, TimeSpan interval, Action action)
  60. {
  61. if (kind == DateTimeKind.Unspecified)
  62. {
  63. kind = DateTimeKind.Local;
  64. }
  65. DateTime value;
  66. if (kind != DateTimeKind.Utc)
  67. {
  68. if (kind != DateTimeKind.Local)
  69. {
  70. return null;
  71. }
  72. value = DateTime.Now;
  73. }
  74. else
  75. {
  76. value = DateTime.UtcNow;
  77. }
  78. DateTime fireTime = value.Date.Add(offset);
  79. while (fireTime.CompareTo(value) < 0)
  80. {
  81. fireTime = fireTime.Add(interval);
  82. }
  83. return this.CreateClockTimer(fireTime, interval, action);
  84. }
  85. public Timer CreateClockTimer(DateTime fireTime, TimeSpan interval, Action action)
  86. {
  87. TimerManager.ClockTimer clockTimer = new TimerManager.ClockTimer(fireTime, interval, action);
  88. this.AddClockTimerToSchedule(clockTimer);
  89. return clockTimer;
  90. }
  91. public Timer CreateClockTimer(DateTime fireTime, Action action)
  92. {
  93. TimerManager.ClockTimer clockTimer = new TimerManager.ClockTimer(fireTime, action);
  94. this.AddClockTimerToSchedule(clockTimer);
  95. return clockTimer;
  96. }
  97. private void AddClockTimerToSchedule(TimerManager.ClockTimer timer)
  98. {
  99. DateTime fireTime = timer.FireTime;
  100. if (fireTime.Kind == DateTimeKind.Unspecified)
  101. {
  102. fireTime = new DateTime(fireTime.Ticks, DateTimeKind.Local);
  103. }
  104. DateTimeKind kind = fireTime.Kind;
  105. if (kind != DateTimeKind.Utc)
  106. {
  107. if (kind == DateTimeKind.Local)
  108. {
  109. this._localClockTimers.Add(timer);
  110. this._localClockTimersDirty = true;
  111. }
  112. }
  113. else
  114. {
  115. this._utcClockTimers.Add(timer);
  116. this._utcClockTimersDirty = true;
  117. }
  118. }
  119. private void FireGameTimers(ref bool dirty, List<TimerManager.GameTimer> timers)
  120. {
  121. if (timers == null)
  122. {
  123. return;
  124. }
  125. if (dirty)
  126. {
  127. timers.Sort();
  128. dirty = false;
  129. }
  130. int num = 0;
  131. while (num < timers.Count && timers[num].FireTime <= Time.time)
  132. {
  133. TimerManager.GameTimer gameTimer = timers[num];
  134. if (gameTimer.Action != null)
  135. {
  136. gameTimer.Action();
  137. }
  138. if (gameTimer.Repeating)
  139. {
  140. gameTimer.FireTime += gameTimer.Interval;
  141. if (gameTimer.FireTime < Time.time)
  142. {
  143. gameTimer.FireTime = Time.time;
  144. }
  145. }
  146. else
  147. {
  148. gameTimer.Cancel();
  149. }
  150. num++;
  151. }
  152. }
  153. private void FireClockTimers(ref bool dirty, List<TimerManager.ClockTimer> timers, DateTime now)
  154. {
  155. if (timers == null)
  156. {
  157. return;
  158. }
  159. if (dirty)
  160. {
  161. timers.Sort();
  162. dirty = false;
  163. }
  164. int num = 0;
  165. while (num < timers.Count && timers[num].FireTime.CompareTo(now) <= 0)
  166. {
  167. TimerManager.ClockTimer clockTimer = timers[num];
  168. if (clockTimer.Action != null)
  169. {
  170. clockTimer.Action();
  171. }
  172. if (clockTimer.Repeating)
  173. {
  174. do
  175. {
  176. clockTimer.FireTime = clockTimer.FireTime.Add(clockTimer.Interval);
  177. }
  178. while (clockTimer.FireTime.CompareTo(now) <= 0);
  179. }
  180. else
  181. {
  182. clockTimer.Cancel();
  183. }
  184. num++;
  185. }
  186. }
  187. private void CancelTimers<T>(List<T> timers) where T : Timer
  188. {
  189. if (timers == null)
  190. {
  191. return;
  192. }
  193. List<Timer> list = new List<Timer>(timers.Count);
  194. foreach (T t in timers)
  195. {
  196. list.Add(t);
  197. }
  198. foreach (Timer timer in list)
  199. {
  200. timer.Cancel();
  201. }
  202. timers.Clear();
  203. }
  204. private List<TimerManager.GameTimer> _gameTimers = new List<TimerManager.GameTimer>();
  205. private bool _gameTimersDirty;
  206. private List<TimerManager.ClockTimer> _utcClockTimers = new List<TimerManager.ClockTimer>();
  207. private List<TimerManager.ClockTimer> _localClockTimers = new List<TimerManager.ClockTimer>();
  208. private bool _utcClockTimersDirty;
  209. private bool _localClockTimersDirty;
  210. private class GameTimer : Timer, IComparable<TimerManager.GameTimer>
  211. {
  212. public GameTimer(float fireTime, float interval, Action action) : base(action)
  213. {
  214. this._fireTime = fireTime;
  215. this._interval = interval;
  216. this._repeating = true;
  217. }
  218. public GameTimer(float fireTime, Action action) : base(action)
  219. {
  220. this._fireTime = fireTime;
  221. this._repeating = false;
  222. }
  223. public float FireTime
  224. {
  225. get
  226. {
  227. return this._fireTime;
  228. }
  229. set
  230. {
  231. this._fireTime = value;
  232. SingletonMonobehaviour<TimerManager>.Instance._gameTimersDirty = true;
  233. }
  234. }
  235. public float Interval
  236. {
  237. get
  238. {
  239. return this._interval;
  240. }
  241. set
  242. {
  243. this._interval = value;
  244. }
  245. }
  246. public bool Repeating
  247. {
  248. get
  249. {
  250. return this._repeating;
  251. }
  252. }
  253. public int CompareTo(TimerManager.GameTimer other)
  254. {
  255. if (this._fireTime < other._fireTime)
  256. {
  257. return -1;
  258. }
  259. if (this._fireTime > other._fireTime)
  260. {
  261. return 1;
  262. }
  263. return 0;
  264. }
  265. public override void Cancel()
  266. {
  267. if (!this._cancelled && SingletonMonobehaviour<TimerManager>.IsAvailable && SingletonMonobehaviour<TimerManager>.Instance._gameTimers != null)
  268. {
  269. SingletonMonobehaviour<TimerManager>.Instance._gameTimers.Remove(this);
  270. }
  271. base.Cancel();
  272. }
  273. private float _fireTime;
  274. private float _interval;
  275. private bool _repeating;
  276. }
  277. private class ClockTimer : Timer, IComparable<TimerManager.ClockTimer>
  278. {
  279. public ClockTimer(DateTime fireTime, TimeSpan interval, Action action) : base(action)
  280. {
  281. this._fireTime = fireTime;
  282. this._interval = interval;
  283. this._repeating = true;
  284. }
  285. public ClockTimer(DateTime fireTime, Action action) : base(action)
  286. {
  287. this._fireTime = fireTime;
  288. this._repeating = false;
  289. }
  290. public DateTime FireTime
  291. {
  292. get
  293. {
  294. return this._fireTime;
  295. }
  296. set
  297. {
  298. this._fireTime = value;
  299. DateTimeKind kind = this._fireTime.Kind;
  300. if (kind != DateTimeKind.Utc)
  301. {
  302. if (kind == DateTimeKind.Local)
  303. {
  304. SingletonMonobehaviour<TimerManager>.Instance._localClockTimersDirty = true;
  305. }
  306. }
  307. else
  308. {
  309. SingletonMonobehaviour<TimerManager>.Instance._utcClockTimersDirty = true;
  310. }
  311. }
  312. }
  313. public TimeSpan Interval
  314. {
  315. get
  316. {
  317. return this._interval;
  318. }
  319. set
  320. {
  321. this._interval = value;
  322. }
  323. }
  324. public bool Repeating
  325. {
  326. get
  327. {
  328. return this._repeating;
  329. }
  330. }
  331. public int CompareTo(TimerManager.ClockTimer other)
  332. {
  333. return this._fireTime.CompareTo(other._fireTime);
  334. }
  335. public override void Cancel()
  336. {
  337. if (!this._cancelled && SingletonMonobehaviour<TimerManager>.IsAvailable && SingletonMonobehaviour<TimerManager>.Instance._localClockTimers != null && SingletonMonobehaviour<TimerManager>.Instance._utcClockTimers != null)
  338. {
  339. DateTimeKind kind = this._fireTime.Kind;
  340. if (kind != DateTimeKind.Utc)
  341. {
  342. if (kind == DateTimeKind.Local)
  343. {
  344. SingletonMonobehaviour<TimerManager>.Instance._localClockTimers.Remove(this);
  345. }
  346. }
  347. else
  348. {
  349. SingletonMonobehaviour<TimerManager>.Instance._utcClockTimers.Remove(this);
  350. }
  351. }
  352. base.Cancel();
  353. }
  354. private DateTime _fireTime;
  355. private TimeSpan _interval;
  356. private bool _repeating;
  357. }
  358. }
  359. }