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 30 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. /// <returns>返回操作前的值</returns>
  20. public long Start() => Interlocked.CompareExchange(ref _time, Environment.TickCount64, long.MaxValue);
  21. /// <returns>返回操作前的值</returns>
  22. public long Stop() => Interlocked.Exchange(ref _time, long.MaxValue);
  23. public void StopIfPassing(long passedTime)
  24. {
  25. if (Environment.TickCount64 - Interlocked.CompareExchange(ref _time, -2, -2) > passedTime)
  26. Interlocked.Exchange(ref _time, long.MaxValue);
  27. }
  28. }
  29. /// <summary>
  30. /// 根据时间推算Start后完成多少进度的进度条(long)。
  31. /// 只允许Start(清零状态的进度条才可以Start)时修改needTime(请确保大于0);
  32. /// 支持InterruptToSet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
  33. /// 通过原子操作实现。
  34. /// </summary>
  35. public class TimeBasedProgressOptimizedForInterrupting
  36. {
  37. private long endT = long.MaxValue;
  38. private long needT;
  39. public TimeBasedProgressOptimizedForInterrupting(long needTime)
  40. {
  41. if (needTime <= 0) Debugger.Output("Bug:TimeBasedProgressOptimizedForInterrupting.needProgress (" + needTime.ToString() + ") is less than 0.");
  42. this.needT = needTime;
  43. }
  44. public TimeBasedProgressOptimizedForInterrupting()
  45. {
  46. this.needT = 0;
  47. }
  48. public long GetEndTime() => Interlocked.CompareExchange(ref endT, -2, -2);
  49. public long GetNeedTime() => Interlocked.CompareExchange(ref needT, -2, -2);
  50. public override string ToString() => "EndTime:" + Interlocked.CompareExchange(ref endT, -2, -2).ToString() + " ms, NeedTime:" + Interlocked.CompareExchange(ref needT, -2, -2).ToString() + " ms";
  51. public bool IsFinished()
  52. {
  53. return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64;
  54. }
  55. public bool IsStarted() => Interlocked.Read(ref endT) != long.MaxValue;
  56. /// <summary>
  57. /// GetProgress<0则表明未开始
  58. /// </summary>
  59. public long GetProgress()
  60. {
  61. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  62. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  63. return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  64. }
  65. public long GetNonNegativeProgress()
  66. {
  67. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  68. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  69. long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  70. return progress < 0 ? 0 : progress;
  71. }
  72. /// <summary>
  73. /// GetProgress<0则表明未开始
  74. /// </summary>
  75. public long GetProgress(long time)
  76. {
  77. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time;
  78. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  79. return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  80. }
  81. public long GetNonNegativeProgress(long time)
  82. {
  83. long cutime = Interlocked.Read(ref endT) - time;
  84. if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
  85. long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
  86. return progress < 0 ? 0 : progress;
  87. }
  88. /// <summary>
  89. /// <0则表明未开始
  90. /// </summary>
  91. public static implicit operator long(TimeBasedProgressOptimizedForInterrupting pLong) => pLong.GetProgress();
  92. /// <summary>
  93. /// GetProgressDouble<0则表明未开始
  94. /// </summary>
  95. public double GetProgressDouble()
  96. {
  97. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  98. if (cutime <= 0) return 1;
  99. long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
  100. if (needTime == 0) return 0;
  101. return 1.0 - ((double)cutime / needTime);
  102. }
  103. public double GetNonNegativeProgressDouble(long time)
  104. {
  105. long cutime = Interlocked.Read(ref endT) - time;
  106. if (cutime <= 0) return 1;
  107. long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
  108. if (needTime <= cutime) return 0;
  109. return 1.0 - ((double)cutime / needTime);
  110. }
  111. public bool Start(long needTime)
  112. {
  113. if (needTime <= 0)
  114. {
  115. Debugger.Output("Warning:Start TimeBasedProgressOptimizedForInterrupting with the needProgress (" + needTime.ToString() + ") which is less than 0.");
  116. return false;
  117. }
  118. //规定只有Start可以修改needT,且需要先访问endTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
  119. if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
  120. if (needTime <= 2) Debugger.Output("Warning:the field of TimeBasedProgressOptimizedForInterrupting is " + needTime.ToString() + ",which is too small.");
  121. Interlocked.Exchange(ref needT, needTime);
  122. return true;
  123. }
  124. public bool Start()
  125. {
  126. long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
  127. if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
  128. return true;
  129. }
  130. /// <summary>
  131. /// 使进度条强制终止清零
  132. /// </summary>
  133. public void Set0() => Interlocked.Exchange(ref endT, long.MaxValue);
  134. /// <summary>
  135. /// 使未完成的进度条终止清零
  136. /// </summary>
  137. public bool InterruptToSet0()
  138. {
  139. if (Environment.TickCount64 < Interlocked.CompareExchange(ref endT, -2, -2))
  140. {
  141. Interlocked.Exchange(ref endT, long.MaxValue);
  142. return true;
  143. }
  144. return false;
  145. }
  146. //增加其他新的写操作可能导致不安全
  147. }
  148. /*
  149. public class TimeBasedProgressAtVariableSpeed
  150. {
  151. private long progress = 0;
  152. private long lastStartT = long.MaxValue;
  153. private readonly object progressLock = new();
  154. private long needProgress;
  155. public TimeBasedProgressAtVariableSpeed(long needProgress)
  156. {
  157. if (needProgress <= 0) Debugger.Output("Bug:TimeBasedProgressAtVariableSpeed.needProgress (" + needProgress.ToString() + ") is less than 0.");
  158. this.needProgress = needProgress;
  159. }
  160. public TimeBasedProgressAtVariableSpeed()
  161. {
  162. this.needProgress = 0;
  163. }
  164. public long GetNeedProgress()
  165. {
  166. lock (progressLock) return needProgress;
  167. }
  168. public override string ToString()
  169. {
  170. lock (progressLock)
  171. {
  172. return "NeedProgress:" + Interlocked.CompareExchange(ref needProgress, -2, -2).ToString() + " ms, "
  173. + "FinishedProgress:" + progress.ToString() +"ms, "
  174. + "lastStartTime:" +lastStartT.ToString() +"ms";
  175. }
  176. }
  177. public long GetProgressNow()
  178. {
  179. lock (progressLock)
  180. {
  181. if (lastStartT==long.MaxValue) return progress;
  182. long progressNow = progress + Environment.TickCount64 - lastStartT;
  183. if (progressNow>= needProgress)
  184. {
  185. lastStartT = long.MaxValue;
  186. return progress = needProgress;
  187. }
  188. return progressNow;
  189. }
  190. }
  191. public bool IsFinished()
  192. {
  193. lock (progressLock)
  194. {
  195. if (progress >= needProgress) return true;
  196. if (progress + Environment.TickCount64 - lastStartT >= needProgress)
  197. {
  198. lastStartT = long.MaxValue;
  199. progress = needProgress;
  200. return true;
  201. }
  202. return false;
  203. }
  204. }
  205. public bool IsProgressing()
  206. {
  207. lock (progressLock)
  208. {
  209. if (lastStartT==long.MaxValue) return false;
  210. if (progress + Environment.TickCount64 - lastStartT >= needProgress)
  211. {
  212. lastStartT = long.MaxValue;
  213. progress = needProgress;
  214. return false;
  215. }
  216. return true;
  217. }
  218. }
  219. /// <summary>
  220. /// GetProgress<0则表明未开始
  221. /// </summary>
  222. public long GetProgress()
  223. {
  224. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  225. if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
  226. return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
  227. }
  228. public long GetNonNegativeProgress()
  229. {
  230. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  231. if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
  232. long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
  233. return progress < 0 ? 0 : progress;
  234. }
  235. /// <summary>
  236. /// GetProgress<0则表明未开始
  237. /// </summary>
  238. public long GetProgress(long time)
  239. {
  240. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time;
  241. if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
  242. return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
  243. }
  244. public long GetNonNegativeProgress(long time)
  245. {
  246. long cutime = Interlocked.Read(ref endT) - time;
  247. if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
  248. long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
  249. return progress < 0 ? 0 : progress;
  250. }
  251. /// <summary>
  252. /// <0则表明未开始
  253. /// </summary>
  254. public static implicit operator long(TimeBasedProgressAtVariableSpeed pLong) => pLong.GetProgress();
  255. /// <summary>
  256. /// GetProgressDouble<0则表明未开始
  257. /// </summary>
  258. public double GetProgressDouble()
  259. {
  260. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  261. if (cutime <= 0) return 1;
  262. long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2);
  263. if (needTime == 0) return 0;
  264. return 1.0 - ((double)cutime / needTime);
  265. }
  266. public double GetNonNegativeProgressDouble(long time)
  267. {
  268. long cutime = Interlocked.Read(ref endT) - time;
  269. if (cutime <= 0) return 1;
  270. long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2);
  271. if (needTime <= cutime) return 0;
  272. return 1.0 - ((double)cutime / needTime);
  273. }
  274. public bool Start(long needTime)
  275. {
  276. if (needTime <= 2)
  277. {
  278. Debugger.Output("Warning:Start TimeBasedProgressAtVariableSpeed with the needProgress (" + needTime.ToString() + ") which is less than 0.");
  279. return false;
  280. }
  281. //规定只有Start可以修改needT,且需要先访问startTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
  282. if (Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) != long.MaxValue) return false;
  283. Interlocked.Exchange(ref needProgress, needTime);
  284. return true;
  285. }
  286. public bool Start()
  287. {
  288. return Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) == long.MaxValue;
  289. }
  290. /// <summary>
  291. /// 使进度条强制终止清零
  292. /// </summary>
  293. public void Set0()
  294. {
  295. Interlocked.Exchange(ref lastStartT, long.MaxValue);
  296. Interlocked.Exchange(ref progress, 0);
  297. }
  298. /// <summary>
  299. /// 使进度条暂停
  300. /// </summary>
  301. public bool Pause()
  302. {
  303. Interlocked.CompareExchange(ref lastStartT, -2, -2)
  304. if (Environment.TickCount64 < )
  305. {
  306. Interlocked.Exchange(ref endT, long.MaxValue);
  307. return true;
  308. }
  309. return false;
  310. }
  311. }*/
  312. /*
  313. public class TimeBasedProgressAtVariableSpeed
  314. {
  315. public LongInTheVariableRange progress;
  316. public StartTime startTime = new();
  317. private speed =1.0;
  318. public TimeBasedProgressAtVariableSpeed(long needProgress, double speed)
  319. {
  320. if (needProgress <= 0) Debugger.Output("Bug:TimeBasedProgressAtVariableSpeed.needProgress (" + needProgress.ToString() + ") is less than 0.");
  321. this.needProgress = needProgress;
  322. this.speed = speed;
  323. }
  324. public TimeBasedProgressAtVariableSpeed()
  325. {
  326. this.needProgress = 0;
  327. }
  328. public long GetNeedProgress()
  329. {
  330. lock (progressLock) return needProgress;
  331. }
  332. public override string ToString()
  333. {
  334. lock (progressLock)
  335. {
  336. return "NeedProgress:" + Interlocked.CompareExchange(ref needProgress, -2, -2).ToString() + " ms, "
  337. + "FinishedProgress:" + progress.ToString() + "ms, "
  338. + "lastStartTime:" + lastStartT.ToString() + "ms";
  339. }
  340. }
  341. public long GetProgressNow()
  342. {
  343. lock (progressLock)
  344. {
  345. if (lastStartT == long.MaxValue) return progress;
  346. long progressNow = progress + Environment.TickCount64 - lastStartT;
  347. if (progressNow >= needProgress)
  348. {
  349. lastStartT = long.MaxValue;
  350. return progress = needProgress;
  351. }
  352. return progressNow;
  353. }
  354. }
  355. public bool IsFinished()
  356. {
  357. lock (progressLock)
  358. {
  359. if (progress >= needProgress) return true;
  360. if (progress + Environment.TickCount64 - lastStartT >= needProgress)
  361. {
  362. lastStartT = long.MaxValue;
  363. progress = needProgress;
  364. return true;
  365. }
  366. return false;
  367. }
  368. }
  369. public bool IsProgressing()
  370. {
  371. lock (progressLock)
  372. {
  373. if (lastStartT == long.MaxValue) return false;
  374. if (progress + Environment.TickCount64 - lastStartT >= needProgress)
  375. {
  376. lastStartT = long.MaxValue;
  377. progress = needProgress;
  378. return false;
  379. }
  380. return true;
  381. }
  382. }
  383. /// <summary>
  384. /// GetProgress<0则表明未开始
  385. /// </summary>
  386. public long GetProgress()
  387. {
  388. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  389. if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
  390. return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
  391. }
  392. public long GetNonNegativeProgress()
  393. {
  394. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  395. if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
  396. long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
  397. return progress < 0 ? 0 : progress;
  398. }
  399. /// <summary>
  400. /// GetProgress<0则表明未开始
  401. /// </summary>
  402. public long GetProgress(long time)
  403. {
  404. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time;
  405. if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
  406. return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
  407. }
  408. public long GetNonNegativeProgress(long time)
  409. {
  410. long cutime = Interlocked.Read(ref endT) - time;
  411. if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
  412. long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
  413. return progress < 0 ? 0 : progress;
  414. }
  415. /// <summary>
  416. /// <0则表明未开始
  417. /// </summary>
  418. public static implicit operator long(TimeBasedProgressAtVariableSpeed pLong) => pLong.GetProgress();
  419. /// <summary>
  420. /// GetProgressDouble<0则表明未开始
  421. /// </summary>
  422. public double GetProgressDouble()
  423. {
  424. long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
  425. if (cutime <= 0) return 1;
  426. long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2);
  427. if (needTime == 0) return 0;
  428. return 1.0 - ((double)cutime / needTime);
  429. }
  430. public double GetNonNegativeProgressDouble(long time)
  431. {
  432. long cutime = Interlocked.Read(ref endT) - time;
  433. if (cutime <= 0) return 1;
  434. long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2);
  435. if (needTime <= cutime) return 0;
  436. return 1.0 - ((double)cutime / needTime);
  437. }
  438. public bool Start(long needTime)
  439. {
  440. if (needTime <= 2)
  441. {
  442. Debugger.Output("Warning:Start TimeBasedProgressAtVariableSpeed with the needProgress (" + needTime.ToString() + ") which is less than 0.");
  443. return false;
  444. }
  445. //规定只有Start可以修改needT,且需要先访问startTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
  446. if (Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) != long.MaxValue) return false;
  447. Interlocked.Exchange(ref needProgress, needTime);
  448. return true;
  449. }
  450. public bool Start()
  451. {
  452. return Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) == long.MaxValue;
  453. }
  454. /// <summary>
  455. /// 使进度条强制终止清零
  456. /// </summary>
  457. public void Set0()
  458. {
  459. Interlocked.Exchange(ref lastStartT, long.MaxValue);
  460. Interlocked.Exchange(ref progress, 0);
  461. }
  462. /// <summary>
  463. /// 使进度条暂停
  464. /// </summary>
  465. public bool Pause()
  466. {
  467. Interlocked.CompareExchange(ref lastStartT, -2, -2)
  468. if (Environment.TickCount64 < )
  469. {
  470. Interlocked.Exchange(ref endT, long.MaxValue);
  471. return true;
  472. }
  473. return false;
  474. }
  475. }
  476. */
  477. /// <summary>
  478. /// 冷却时间为可变的CDms的bool,不支持查看当前进度,初始为True
  479. /// </summary>
  480. public class BoolUpdateEachCD
  481. {
  482. private long cd;
  483. private long nextUpdateTime = 0;
  484. public BoolUpdateEachCD(int cd)
  485. {
  486. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  487. this.cd = cd;
  488. }
  489. public BoolUpdateEachCD(long cd)
  490. {
  491. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  492. this.cd = cd;
  493. }
  494. public BoolUpdateEachCD(long cd, long startTime)
  495. {
  496. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  497. this.cd = cd;
  498. this.nextUpdateTime = startTime;
  499. }
  500. public long GetCD() => Interlocked.Read(ref cd);
  501. public bool TryUse()
  502. {
  503. long needTime = Interlocked.Exchange(ref nextUpdateTime, long.MaxValue);
  504. if (needTime <= Environment.TickCount64)
  505. {
  506. Interlocked.Exchange(ref nextUpdateTime, Environment.TickCount64 + Interlocked.Read(ref cd));
  507. return true;
  508. }
  509. Interlocked.Exchange(ref nextUpdateTime, needTime);
  510. return false;
  511. }
  512. public void SetCD(int cd)
  513. {
  514. if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd to " + cd.ToString() + ".");
  515. Interlocked.Exchange(ref this.cd, cd);
  516. }
  517. }
  518. /// <summary>
  519. /// 冷却时间为可变的CDms的进度条,初始为满
  520. /// </summary>
  521. public class LongProgressUpdateEachCD
  522. {
  523. private int isusing = 0;
  524. private long cd;
  525. private long nextUpdateTime = 0;
  526. public LongProgressUpdateEachCD(int cd)
  527. {
  528. if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  529. this.cd = cd;
  530. }
  531. public LongProgressUpdateEachCD(long cd)
  532. {
  533. if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  534. this.cd = cd;
  535. }
  536. public LongProgressUpdateEachCD(long cd, long startTime)
  537. {
  538. if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
  539. this.cd = cd;
  540. this.nextUpdateTime = startTime;
  541. }
  542. public long GetRemainingTime()
  543. {
  544. long v = Interlocked.Read(ref nextUpdateTime) - Environment.TickCount64;
  545. return v < 0 ? 0 : v;
  546. }
  547. public long GetCD() => Interlocked.Read(ref cd);
  548. public bool TryUse()
  549. {
  550. if (Interlocked.Exchange(ref isusing, 1) == 1) return false;
  551. long needTime = Interlocked.Read(ref nextUpdateTime);
  552. if (needTime <= Environment.TickCount64)
  553. {
  554. Interlocked.Exchange(ref nextUpdateTime, Environment.TickCount64 + Interlocked.Read(ref cd));
  555. Interlocked.Exchange(ref isusing, 0);
  556. return true;
  557. }
  558. Interlocked.Exchange(ref isusing, 0);
  559. return false;
  560. }
  561. public void SetCD(int cd)
  562. {
  563. if (cd <= 1) Debugger.Output("Bug:Set LongProgressUpdateEachCD.cd to " + cd.ToString() + ".");
  564. Interlocked.Exchange(ref this.cd, cd);
  565. }
  566. }
  567. /// <summary>
  568. /// 一个保证在[0,maxNum],每CDms自动+1的int,支持可变的CD、maxNum(请确保大于0)
  569. /// </summary>
  570. public class IntNumUpdateEachCD
  571. {
  572. private int num;
  573. private int maxNum;
  574. private int cd;
  575. private long updateTime = 0;
  576. private readonly object numLock = new();
  577. public IntNumUpdateEachCD(int num, int maxNum, int cd)
  578. {
  579. if (num < 0) Debugger.Output("Bug:IntNumUpdateEachCD.num (" + num.ToString() + ") is less than 0.");
  580. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateEachCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  581. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateEachCD.cd (" + cd.ToString() + ") is less than 0.");
  582. this.num = (num < maxNum) ? num : maxNum;
  583. this.maxNum = maxNum;
  584. this.cd = cd;
  585. this.updateTime = Environment.TickCount64;
  586. }
  587. /// <summary>
  588. /// 默认使num=maxNum
  589. /// </summary>
  590. public IntNumUpdateEachCD(int maxNum, int cd)
  591. {
  592. if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateEachCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
  593. if (cd <= 0) Debugger.Output("Bug:IntNumUpdateEachCD.cd (" + cd.ToString() + ") is less than 0.");
  594. this.num = this.maxNum = maxNum;
  595. this.cd = cd;
  596. }
  597. public IntNumUpdateEachCD()
  598. {
  599. this.num = this.maxNum = 0;
  600. this.cd = int.MaxValue;
  601. }
  602. public int GetMaxNum() { lock (numLock) return maxNum; }
  603. public int GetCD() { lock (numLock) return cd; }
  604. public int GetNum(long time)
  605. {
  606. lock (numLock)
  607. {
  608. if (num < maxNum && time - updateTime >= cd)
  609. {
  610. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  611. updateTime += add * cd;
  612. return (num += add);
  613. }
  614. return num;
  615. }
  616. }
  617. public static implicit operator int(IntNumUpdateEachCD aint) => aint.GetNum(Environment.TickCount64);
  618. /// <summary>
  619. /// 应当保证该subV>=0
  620. /// </summary>
  621. public int TrySub(int subV)
  622. {
  623. if (subV < 0) Debugger.Output("Bug:IntNumUpdateEachCD Try to sub " + subV.ToString() + ", which is less than 0.");
  624. long time = Environment.TickCount64;
  625. lock (numLock)
  626. {
  627. if (num < maxNum && time - updateTime >= cd)
  628. {
  629. int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
  630. updateTime += add * cd;
  631. num += add;
  632. }
  633. if (num == maxNum) updateTime = time;
  634. num -= subV = Math.Min(subV, num);
  635. }
  636. return subV;
  637. }
  638. /// <summary>
  639. /// 应当保证该addV>=0
  640. /// </summary>
  641. public void TryAdd(int addV)
  642. {
  643. if (addV < 0) Debugger.Output("Bug:IntNumUpdateEachCD Try to add " + addV.ToString() + ", which is less than 0.");
  644. lock (numLock)
  645. {
  646. num += Math.Min(addV, maxNum - num);
  647. }
  648. }
  649. /// <summary>
  650. /// 若maxNum<=0则maxNum及Num设为0并返回False
  651. /// </summary>
  652. public bool SetMaxNumAndNum(int maxNum)
  653. {
  654. if (maxNum < 0) maxNum = 0;
  655. lock (numLock)
  656. {
  657. this.num = this.maxNum = maxNum;
  658. }
  659. return maxNum > 0;
  660. }
  661. /// <summary>
  662. /// 应当保证该maxnum>=0
  663. /// </summary>
  664. public void SetPositiveMaxNumAndNum(int maxNum)
  665. {
  666. lock (numLock)
  667. {
  668. this.num = this.maxNum = maxNum;
  669. }
  670. }
  671. /// <summary>
  672. /// 应当保证该maxnum>=0
  673. /// </summary>
  674. public void SetPositiveMaxNum(int maxNum)
  675. {
  676. lock (numLock)
  677. {
  678. if ((this.maxNum = maxNum) < num)
  679. num = maxNum;
  680. }
  681. }
  682. /// <summary>
  683. /// 若maxNum<=0则maxNum及Num设为0并返回False
  684. /// </summary>
  685. public bool SetMaxNum(int maxNum)
  686. {
  687. if (maxNum < 0) maxNum = 0;
  688. lock (numLock)
  689. {
  690. if ((this.maxNum = maxNum) < num)
  691. num = maxNum;
  692. }
  693. return maxNum > 0;
  694. }
  695. /// <summary>
  696. /// 若num<0则num设为0并返回False
  697. /// </summary>
  698. public bool SetNum(int num)
  699. {
  700. lock (numLock)
  701. {
  702. if (num < 0)
  703. {
  704. this.num = 0;
  705. updateTime = Environment.TickCount64;
  706. return false;
  707. }
  708. if (num < maxNum)
  709. {
  710. if (this.num == maxNum) updateTime = Environment.TickCount64;
  711. this.num = num;
  712. }
  713. else this.num = maxNum;
  714. return true;
  715. }
  716. }
  717. /// <summary>
  718. /// 应当保证该num>=0
  719. /// </summary>
  720. public void SetPositiveNum(int num)
  721. {
  722. lock (numLock)
  723. {
  724. if (num < maxNum)
  725. {
  726. if (this.num == maxNum) updateTime = Environment.TickCount64;
  727. this.num = num;
  728. }
  729. else this.num = maxNum;
  730. }
  731. }
  732. public void SetCD(int cd)
  733. {
  734. lock (numLock)
  735. {
  736. if (cd <= 0) Debugger.Output("Bug:Set IntNumUpdateEachCD.cd to " + cd.ToString() + ".");
  737. this.cd = cd;
  738. }
  739. }
  740. }
  741. }