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

TimeBased.cs 20 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. using System;
  2. using System.Threading;
  3. namespace Preparation.Utility
  4. {
  5. //其对应属性不应当有set访问器,避免不安全的=赋值
  6. /// <summary>
  7. /// 记录上次Start的时间,尚未Start则为long.MaxValue
  8. /// 当前不为long.MaxValue则不能Start
  9. /// </summary>
  10. public class StartTime
  11. {
  12. private long _time;
  13. public StartTime(long time)
  14. {
  15. _time = time;
  16. }
  17. public StartTime() { _time = long.MaxValue; }
  18. public long Get() => Interlocked.CompareExchange(ref _time, -2, -2);
  19. public override string ToString() => Get().ToString();
  20. /// <returns>返回操作前的值</returns>
  21. public long Start() => Interlocked.CompareExchange(ref _time, Environment.TickCount64, long.MaxValue);
  22. /// <returns>返回操作前的值</returns>
  23. public long Stop() => Interlocked.Exchange(ref _time, long.MaxValue);
  24. /// <returns>返回时间差,<0意味着未开始</returns>
  25. public long StopIfPassing(long passedTime)
  26. {
  27. long ans = Environment.TickCount64 - Interlocked.CompareExchange(ref _time, -2, -2);
  28. if (ans > passedTime)
  29. {
  30. Interlocked.Exchange(ref _time, long.MaxValue);
  31. }
  32. return ans;
  33. }
  34. }
  35. /// <summary>
  36. /// 根据时间推算Start后完成多少进度的进度条(long)。
  37. /// 只允许Start(清零状态的进度条才可以Start)时修改needTime(请确保大于0);
  38. /// 支持InterruptToSet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
  39. /// 通过原子操作实现。
  40. /// </summary>
  41. public class TimeBasedProgressOptimizedForInterrupting
  42. {
  43. private long endT = long.MaxValue;
  44. private long needT;
  45. public TimeBasedProgressOptimizedForInterrupting(long needTime)
  46. {
  47. if (needTime <= 0) Debugger.Output("Bug:TimeBasedProgressOptimizedForInterrupting.needProgress (" + needTime.ToString() + ") is less than 0.");
  48. this.needT = needTime;
  49. }
  50. public TimeBasedProgressOptimizedForInterrupting()
  51. {
  52. this.needT = 0;
  53. }
  54. public long GetEndTime() => Interlocked.CompareExchange(ref endT, -2, -2);
  55. public long GetNeedTime() => Interlocked.CompareExchange(ref needT, -2, -2);
  56. public override string ToString() => "EndTime:" + Interlocked.CompareExchange(ref endT, -2, -2).ToString() + " ms, NeedTime:" + Interlocked.CompareExchange(ref needT, -2, -2).ToString() + " ms";
  57. public bool IsFinished()
  58. {
  59. return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64;
  60. }
  61. public bool IsStarted() => Interlocked.Read(ref endT) != long.MaxValue;
  62. /// <summary>
  63. /// GetProgress<0则表明未开始
  64. /// </summary>
  65. public long GetProgress()
  66. {
  67. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  68. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  69. return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  70. }
  71. public long GetNonNegativeProgress()
  72. {
  73. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  74. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  75. long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  76. return progress < 0 ? 0 : progress;
  77. }
  78. /// <summary>
  79. /// GetProgress<0则表明未开始
  80. /// </summary>
  81. public long GetProgress(long time)
  82. {
  83. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time;
  84. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  85. return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  86. }
  87. public long GetNonNegativeProgress(long time)
  88. {
  89. long cutime = Interlocked.Read(ref endT) - time;
  90. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  91. long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  92. return progress < 0 ? 0 : progress;
  93. }
  94. /// <summary>
  95. /// <0则表明未开始
  96. /// </summary>
  97. public static implicit operator long(TimeBasedProgressOptimizedForInterrupting pLong) => pLong.GetProgress();
  98. /// <summary>
  99. /// GetProgressDouble<0则表明未开始
  100. /// </summary>
  101. public double GetProgressDouble()
  102. {
  103. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  104. if (cutime <= 0) return 1;
  105. long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
  106. if (needTime == 0) return 0;
  107. return 1.0 - ((double)cutime / needTime);
  108. }
  109. public double GetNonNegativeProgressDouble(long time)
  110. {
  111. long cutime = Interlocked.Read(ref endT) - time;
  112. if (cutime <= 0) return 1;
  113. long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
  114. if (needTime <= cutime) return 0;
  115. return 1.0 - ((double)cutime / needTime);
  116. }
  117. public bool Start(long needTime)
  118. {
  119. if (needTime <= 0)
  120. {
  121. Debugger.Output("Warning:Start TimeBasedProgressOptimizedForInterrupting with the needProgress (" + needTime.ToString() + ") which is less than 0.");
  122. return false;
  123. }
  124. //规定只有Start可以修改needT,且需要先访问endTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
  125. if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
  126. if (needTime <= 2) Debugger.Output("Warning:the field of TimeBasedProgressOptimizedForInterrupting is " + needTime.ToString() + ",which is too small.");
  127. Interlocked.Exchange(ref needT, needTime);
  128. return true;
  129. }
  130. public bool Start()
  131. {
  132. long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
  133. if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
  134. return true;
  135. }
  136. /// <summary>
  137. /// 使进度条强制终止清零
  138. /// </summary>
  139. public void Set0() => Interlocked.Exchange(ref endT, long.MaxValue);
  140. /// <summary>
  141. /// 使未完成的进度条终止清零
  142. /// </summary>
  143. public bool InterruptToSet0()
  144. {
  145. if (Environment.TickCount64 < Interlocked.CompareExchange(ref endT, -2, -2))
  146. {
  147. Interlocked.Exchange(ref endT, long.MaxValue);
  148. return true;
  149. }
  150. return false;
  151. }
  152. //增加其他新的写操作可能导致不安全
  153. }
  154. public class TimeBasedProgressAtVariableSpeed
  155. {
  156. public LongInTheVariableRange progress;
  157. public StartTime startTime = new();
  158. public AtomicDouble speed;
  159. #region 构造
  160. public TimeBasedProgressAtVariableSpeed(long needProgress, double speed)
  161. {
  162. if (needProgress <= 0) Debugger.Output("Bug:TimeBasedProgressAtVariableSpeed.needProgress (" + needProgress.ToString() + ") is less than 0.");
  163. this.progress = new(0, needProgress);
  164. this.speed = new(speed);
  165. }
  166. public TimeBasedProgressAtVariableSpeed()
  167. {
  168. this.progress = new(0, 0);
  169. this.speed = new(1.0);
  170. }
  171. #endregion
  172. #region 读取
  173. public override string ToString()
  174. {
  175. return "ProgressStored: " + progress.ToString()
  176. + " ; LastStartTime: " + startTime.ToString() + "ms"
  177. + " ; Speed: " + speed.ToString();
  178. }
  179. public long GetProgressNow() => progress.TryAddToMaxV(startTime, (double)speed).Item1;
  180. public (long, long, long) GetProgressNowAndNeedTimeAndLastStartTime() => progress.TryAddToMaxV(startTime, (double)speed);
  181. public long GetProgressStored() => progress.GetValue();
  182. public (long, long) GetProgressStoredAndNeedTime() => progress.GetValueAndMaxV();
  183. public (long, long, long) GetProgressStoredAndNeedTimeAndLastStartTime() => progress.GetValueAndMaxV(startTime);
  184. public bool IsFinished()
  185. {
  186. long progressNow, needTime;
  187. (progressNow, needTime, _) = progress.TryAddToMaxV(startTime);
  188. return progressNow == needTime;
  189. }
  190. public bool IsProgressing()
  191. {
  192. long progressNow, needTime, startT;
  193. (progressNow, needTime, startT) = progress.TryAddToMaxV(startTime);
  194. return (startT != long.MaxValue && progressNow != needTime);
  195. }
  196. #endregion
  197. public bool Start(long needTime)
  198. {
  199. if (needTime <= 2)
  200. {
  201. Debugger.Output("Warning:Start TimeBasedProgressAtVariableSpeed with the needProgress (" + needTime.ToString() + ") which is less than 0.");
  202. return false;
  203. }
  204. if (startTime.Start() != long.MaxValue) return false;
  205. progress.SetMaxV(needTime);
  206. return true;
  207. }
  208. public bool Start()
  209. {
  210. return startTime.Start() == long.MaxValue;
  211. }
  212. /// <summary>
  213. /// 使进度条强制终止清零
  214. /// </summary>
  215. public void Set0()
  216. {
  217. startTime.Stop();
  218. progress.SetPositiveV(0);
  219. }
  220. /// <summary>
  221. /// 使进度条暂停
  222. /// </summary>
  223. public bool Pause()
  224. {
  225. return progress.AddV(startTime, (double)speed) != 0;
  226. }
  227. }
  228. /// <summary>
  229. /// 冷却时间为可变的CDms的bool,不支持查看当前进度,初始为True
  230. /// </summary>
  231. public class BoolUpdateEachCD
  232. {
  233. private long cd;
  234. private long nextUpdateTime = 0;
  235. public BoolUpdateEachCD(int cd)
  236. {
  237. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  238. this.cd = cd;
  239. }
  240. public BoolUpdateEachCD(long cd)
  241. {
  242. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  243. this.cd = cd;
  244. }
  245. public BoolUpdateEachCD(long cd, long startTime)
  246. {
  247. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  248. this.cd = cd;
  249. this.nextUpdateTime = startTime;
  250. }
  251. public long GetCD() => Interlocked.Read(ref cd);
  252. public bool TryUse()
  253. {
  254. long needTime = Interlocked.Exchange(ref nextUpdateTime, long.MaxValue);
  255. if (needTime <= Environment.TickCount64)
  256. {
  257. Interlocked.Exchange(ref nextUpdateTime, Environment.TickCount64 + Interlocked.Read(ref cd));
  258. return true;
  259. }
  260. Interlocked.Exchange(ref nextUpdateTime, needTime);
  261. return false;
  262. }
  263. public void SetCD(int cd)
  264. {
  265. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd to " + cd.ToString() + ".");
  266. Interlocked.Exchange(ref this.cd, cd);
  267. }
  268. }
  269. /// <summary>
  270. /// 冷却时间为可变的CDms的进度条,初始为满
  271. /// </summary>
  272. public class LongProgressUpdateEachCD
  273. {
  274. private int isusing = 0;
  275. private long cd;
  276. private long nextUpdateTime = 0;
  277. public LongProgressUpdateEachCD(int cd)
  278. {
  279. if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  280. this.cd = cd;
  281. }
  282. public LongProgressUpdateEachCD(long cd)
  283. {
  284. if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  285. this.cd = cd;
  286. }
  287. public LongProgressUpdateEachCD(long cd, long startTime)
  288. {
  289. if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  290. this.cd = cd;
  291. this.nextUpdateTime = startTime;
  292. }
  293. public long GetRemainingTime()
  294. {
  295. long v = Interlocked.Read(ref nextUpdateTime) - Environment.TickCount64;
  296. return v < 0 ? 0 : v;
  297. }
  298. public long GetCD() => Interlocked.Read(ref cd);
  299. public bool TryUse()
  300. {
  301. if (Interlocked.Exchange(ref isusing, 1) == 1) return false;
  302. long needTime = Interlocked.Read(ref nextUpdateTime);
  303. if (needTime <= Environment.TickCount64)
  304. {
  305. Interlocked.Exchange(ref nextUpdateTime, Environment.TickCount64 + Interlocked.Read(ref cd));
  306. Interlocked.Exchange(ref isusing, 0);
  307. return true;
  308. }
  309. Interlocked.Exchange(ref isusing, 0);
  310. return false;
  311. }
  312. public void SetCD(int cd)
  313. {
  314. if (cd <= 1) Debugger.Output("Bug:Set LongProgressUpdateEachCD.cd to " + cd.ToString() + ".");
  315. Interlocked.Exchange(ref this.cd, cd);
  316. }
  317. }
  318. /// <summary>
  319. /// 一个保证在[0,maxNum],每CDms自动+1的int,支持可变的CD、maxNum(请确保大于0)
  320. /// </summary>
  321. public class IntNumUpdateEachCD
  322. {
  323. private int num;
  324. private int maxNum;
  325. private int cd;
  326. private long updateTime = 0;
  327. private readonly object numLock = new();
  328. public IntNumUpdateEachCD(int num, int maxNum, int cd)
  329. {
  330. if (num < 0) Debugger.Output("Bug:IntNumUpdateEachCD.num (" + num.ToString() + ") is less than 0.");
  331. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateEachCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  332. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateEachCD.cd (" + cd.ToString() + ") is less than 0.");
  333. this.num = (num < maxNum) ? num : maxNum;
  334. this.maxNum = maxNum;
  335. this.cd = cd;
  336. this.updateTime = Environment.TickCount64;
  337. }
  338. /// <summary>
  339. /// 默认使num=maxNum
  340. /// </summary>
  341. public IntNumUpdateEachCD(int maxNum, int cd)
  342. {
  343. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateEachCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  344. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateEachCD.cd (" + cd.ToString() + ") is less than 0.");
  345. this.num = this.maxNum = maxNum;
  346. this.cd = cd;
  347. }
  348. public IntNumUpdateEachCD()
  349. {
  350. this.num = this.maxNum = 0;
  351. this.cd = int.MaxValue;
  352. }
  353. public int GetMaxNum() { lock (numLock) return maxNum; }
  354. public int GetCD() { lock (numLock) return cd; }
  355. public int GetNum(long time)
  356. {
  357. lock (numLock)
  358. {
  359. if (num < maxNum && time - updateTime >= cd)
  360. {
  361. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  362. updateTime += add * cd;
  363. return (num += add);
  364. }
  365. return num;
  366. }
  367. }
  368. public static implicit operator int(IntNumUpdateEachCD aint) => aint.GetNum(Environment.TickCount64);
  369. /// <summary>
  370. /// 应当保证该subV>=0
  371. /// </summary>
  372. public int TrySub(int subV)
  373. {
  374. if (subV < 0) Debugger.Output("Bug:IntNumUpdateEachCD Try to sub " + subV.ToString() + ", which is less than 0.");
  375. long time = Environment.TickCount64;
  376. lock (numLock)
  377. {
  378. if (num < maxNum && time - updateTime >= cd)
  379. {
  380. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  381. updateTime += add * cd;
  382. num += add;
  383. }
  384. if (num == maxNum) updateTime = time;
  385. num -= subV = Math.Min(subV, num);
  386. }
  387. return subV;
  388. }
  389. /// <summary>
  390. /// 应当保证该addV>=0
  391. /// </summary>
  392. public void TryAdd(int addV)
  393. {
  394. if (addV < 0) Debugger.Output("Bug:IntNumUpdateEachCD Try to add " + addV.ToString() + ", which is less than 0.");
  395. lock (numLock)
  396. {
  397. num += Math.Min(addV, maxNum - num);
  398. }
  399. }
  400. /// <summary>
  401. /// 若maxNum<=0则maxNum及Num设为0并返回False
  402. /// </summary>
  403. public bool SetMaxNumAndNum(int maxNum)
  404. {
  405. if (maxNum < 0) maxNum = 0;
  406. lock (numLock)
  407. {
  408. this.num = this.maxNum = maxNum;
  409. }
  410. return maxNum > 0;
  411. }
  412. /// <summary>
  413. /// 应当保证该maxnum>=0
  414. /// </summary>
  415. public void SetPositiveMaxNumAndNum(int maxNum)
  416. {
  417. lock (numLock)
  418. {
  419. this.num = this.maxNum = maxNum;
  420. }
  421. }
  422. /// <summary>
  423. /// 应当保证该maxnum>=0
  424. /// </summary>
  425. public void SetPositiveMaxNum(int maxNum)
  426. {
  427. lock (numLock)
  428. {
  429. if ((this.maxNum = maxNum) < num)
  430. num = maxNum;
  431. }
  432. }
  433. /// <summary>
  434. /// 若maxNum<=0则maxNum及Num设为0并返回False
  435. /// </summary>
  436. public bool SetMaxNum(int maxNum)
  437. {
  438. if (maxNum < 0) maxNum = 0;
  439. lock (numLock)
  440. {
  441. if ((this.maxNum = maxNum) < num)
  442. num = maxNum;
  443. }
  444. return maxNum > 0;
  445. }
  446. /// <summary>
  447. /// 若num<0则num设为0并返回False
  448. /// </summary>
  449. public bool SetNum(int num)
  450. {
  451. lock (numLock)
  452. {
  453. if (num < 0)
  454. {
  455. this.num = 0;
  456. updateTime = Environment.TickCount64;
  457. return false;
  458. }
  459. if (num < maxNum)
  460. {
  461. if (this.num == maxNum) updateTime = Environment.TickCount64;
  462. this.num = num;
  463. }
  464. else this.num = maxNum;
  465. return true;
  466. }
  467. }
  468. /// <summary>
  469. /// 应当保证该num>=0
  470. /// </summary>
  471. public void SetPositiveNum(int num)
  472. {
  473. lock (numLock)
  474. {
  475. if (num < maxNum)
  476. {
  477. if (this.num == maxNum) updateTime = Environment.TickCount64;
  478. this.num = num;
  479. }
  480. else this.num = maxNum;
  481. }
  482. }
  483. public void SetCD(int cd)
  484. {
  485. lock (numLock)
  486. {
  487. if (cd <= 0) Debugger.Output("Bug:Set IntNumUpdateEachCD.cd to " + cd.ToString() + ".");
  488. this.cd = cd;
  489. }
  490. }
  491. }
  492. }