|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767 |
- using System;
- using System.Threading;
-
- namespace Preparation.Utility
- {
- //其对应属性不应当有set访问器,避免不安全的=赋值
-
- /// <summary>
- /// 记录上次Start的时间,尚未Start则为long.MaxValue
- /// 当前不为long.MaxValue则不能Start
- /// </summary>
- public class StartTime
- {
- private long _time;
- public StartTime(long time)
- {
- _time = time;
- }
- public StartTime() { _time = long.MaxValue; }
- public long Get() => Interlocked.CompareExchange(ref _time, -2, -2);
- /// <returns>返回操作前的值</returns>
- public long Start() => Interlocked.CompareExchange(ref _time, Environment.TickCount64, long.MaxValue);
- /// <returns>返回操作前的值</returns>
- public long Stop() => Interlocked.Exchange(ref _time, long.MaxValue);
- public void StopIfPassing(long passedTime)
- {
- if (Environment.TickCount64 - Interlocked.CompareExchange(ref _time, -2, -2) > passedTime)
- Interlocked.Exchange(ref _time, long.MaxValue);
- }
- }
-
- /// <summary>
- /// 根据时间推算Start后完成多少进度的进度条(long)。
- /// 只允许Start(清零状态的进度条才可以Start)时修改needTime(请确保大于0);
- /// 支持InterruptToSet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
- /// 通过原子操作实现。
- /// </summary>
- public class TimeBasedProgressOptimizedForInterrupting
- {
- private long endT = long.MaxValue;
- private long needT;
-
- public TimeBasedProgressOptimizedForInterrupting(long needTime)
- {
- if (needTime <= 0) Debugger.Output("Bug:TimeBasedProgressOptimizedForInterrupting.needProgress (" + needTime.ToString() + ") is less than 0.");
- this.needT = needTime;
- }
- public TimeBasedProgressOptimizedForInterrupting()
- {
- this.needT = 0;
- }
- public long GetEndTime() => Interlocked.CompareExchange(ref endT, -2, -2);
- public long GetNeedTime() => Interlocked.CompareExchange(ref needT, -2, -2);
- public override string ToString() => "EndTime:" + Interlocked.CompareExchange(ref endT, -2, -2).ToString() + " ms, NeedTime:" + Interlocked.CompareExchange(ref needT, -2, -2).ToString() + " ms";
- public bool IsFinished()
- {
- return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64;
- }
- public bool IsStarted() => Interlocked.Read(ref endT) != long.MaxValue;
- /// <summary>
- /// GetProgress<0则表明未开始
- /// </summary>
- public long GetProgress()
- {
- long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
- if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
- return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
- }
- public long GetNonNegativeProgress()
- {
- long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
- if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
- long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
- return progress < 0 ? 0 : progress;
- }
- /// <summary>
- /// GetProgress<0则表明未开始
- /// </summary>
- public long GetProgress(long time)
- {
- long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time;
- if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
- return Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
- }
- public long GetNonNegativeProgress(long time)
- {
- long cutime = Interlocked.Read(ref endT) - time;
- if (cutime <= 0) return Interlocked.CompareExchange(ref needT, -2, -2);
- long progress = Interlocked.CompareExchange(ref needT, -2, -2) - cutime;
- return progress < 0 ? 0 : progress;
- }
- /// <summary>
- /// <0则表明未开始
- /// </summary>
- public static implicit operator long(TimeBasedProgressOptimizedForInterrupting pLong) => pLong.GetProgress();
-
- /// <summary>
- /// GetProgressDouble<0则表明未开始
- /// </summary>
- public double GetProgressDouble()
- {
- long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
- if (cutime <= 0) return 1;
- long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
- if (needTime == 0) return 0;
- return 1.0 - ((double)cutime / needTime);
- }
- public double GetNonNegativeProgressDouble(long time)
- {
- long cutime = Interlocked.Read(ref endT) - time;
- if (cutime <= 0) return 1;
- long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
- if (needTime <= cutime) return 0;
- return 1.0 - ((double)cutime / needTime);
- }
-
- public bool Start(long needTime)
- {
- if (needTime <= 0)
- {
- Debugger.Output("Warning:Start TimeBasedProgressOptimizedForInterrupting with the needProgress (" + needTime.ToString() + ") which is less than 0.");
- return false;
- }
- //规定只有Start可以修改needT,且需要先访问endTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
- if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
- if (needTime <= 2) Debugger.Output("Warning:the field of TimeBasedProgressOptimizedForInterrupting is " + needTime.ToString() + ",which is too small.");
- Interlocked.Exchange(ref needT, needTime);
- return true;
- }
- public bool Start()
- {
- long needTime = Interlocked.CompareExchange(ref needT, -2, -2);
- if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
- return true;
- }
- /// <summary>
- /// 使进度条强制终止清零
- /// </summary>
- public void Set0() => Interlocked.Exchange(ref endT, long.MaxValue);
- /// <summary>
- /// 使未完成的进度条终止清零
- /// </summary>
- public bool InterruptToSet0()
- {
- if (Environment.TickCount64 < Interlocked.CompareExchange(ref endT, -2, -2))
- {
- Interlocked.Exchange(ref endT, long.MaxValue);
- return true;
- }
- return false;
- }
- //增加其他新的写操作可能导致不安全
- }
- /*
- public class TimeBasedProgressAtVariableSpeed
- {
- private long progress = 0;
- private long lastStartT = long.MaxValue;
- private readonly object progressLock = new();
- private long needProgress;
-
- public TimeBasedProgressAtVariableSpeed(long needProgress)
- {
- if (needProgress <= 0) Debugger.Output("Bug:TimeBasedProgressAtVariableSpeed.needProgress (" + needProgress.ToString() + ") is less than 0.");
- this.needProgress = needProgress;
- }
- public TimeBasedProgressAtVariableSpeed()
- {
- this.needProgress = 0;
- }
- public long GetNeedProgress()
- {
- lock (progressLock) return needProgress;
- }
- public override string ToString()
- {
- lock (progressLock)
- {
- return "NeedProgress:" + Interlocked.CompareExchange(ref needProgress, -2, -2).ToString() + " ms, "
- + "FinishedProgress:" + progress.ToString() +"ms, "
- + "lastStartTime:" +lastStartT.ToString() +"ms";
- }
- }
- public long GetProgressNow()
- {
- lock (progressLock)
- {
- if (lastStartT==long.MaxValue) return progress;
- long progressNow = progress + Environment.TickCount64 - lastStartT;
- if (progressNow>= needProgress)
- {
- lastStartT = long.MaxValue;
- return progress = needProgress;
- }
- return progressNow;
- }
- }
-
- public bool IsFinished()
- {
- lock (progressLock)
- {
- if (progress >= needProgress) return true;
- if (progress + Environment.TickCount64 - lastStartT >= needProgress)
- {
- lastStartT = long.MaxValue;
- progress = needProgress;
- return true;
- }
- return false;
- }
- }
- public bool IsProgressing()
- {
- lock (progressLock)
- {
- if (lastStartT==long.MaxValue) return false;
- if (progress + Environment.TickCount64 - lastStartT >= needProgress)
- {
- lastStartT = long.MaxValue;
- progress = needProgress;
- return false;
- }
- return true;
- }
- }
-
- /// <summary>
- /// GetProgress<0则表明未开始
- /// </summary>
- public long GetProgress()
- {
- long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
- if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
- return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
- }
- public long GetNonNegativeProgress()
- {
- long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
- if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
- long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
- return progress < 0 ? 0 : progress;
- }
- /// <summary>
- /// GetProgress<0则表明未开始
- /// </summary>
- public long GetProgress(long time)
- {
- long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time;
- if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
- return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
- }
- public long GetNonNegativeProgress(long time)
- {
- long cutime = Interlocked.Read(ref endT) - time;
- if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
- long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
- return progress < 0 ? 0 : progress;
- }
- /// <summary>
- /// <0则表明未开始
- /// </summary>
- public static implicit operator long(TimeBasedProgressAtVariableSpeed pLong) => pLong.GetProgress();
-
- /// <summary>
- /// GetProgressDouble<0则表明未开始
- /// </summary>
- public double GetProgressDouble()
- {
- long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
- if (cutime <= 0) return 1;
- long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2);
- if (needTime == 0) return 0;
- return 1.0 - ((double)cutime / needTime);
- }
- public double GetNonNegativeProgressDouble(long time)
- {
- long cutime = Interlocked.Read(ref endT) - time;
- if (cutime <= 0) return 1;
- long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2);
- if (needTime <= cutime) return 0;
- return 1.0 - ((double)cutime / needTime);
- }
-
- public bool Start(long needTime)
- {
- if (needTime <= 2)
- {
- Debugger.Output("Warning:Start TimeBasedProgressAtVariableSpeed with the needProgress (" + needTime.ToString() + ") which is less than 0.");
- return false;
- }
- //规定只有Start可以修改needT,且需要先访问startTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
- if (Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) != long.MaxValue) return false;
- Interlocked.Exchange(ref needProgress, needTime);
- return true;
- }
- public bool Start()
- {
- return Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) == long.MaxValue;
- }
- /// <summary>
- /// 使进度条强制终止清零
- /// </summary>
- public void Set0()
- {
- Interlocked.Exchange(ref lastStartT, long.MaxValue);
- Interlocked.Exchange(ref progress, 0);
- }
- /// <summary>
- /// 使进度条暂停
- /// </summary>
- public bool Pause()
- {
- Interlocked.CompareExchange(ref lastStartT, -2, -2)
- if (Environment.TickCount64 < )
- {
- Interlocked.Exchange(ref endT, long.MaxValue);
- return true;
- }
- return false;
- }
- }*/
-
- /*
- public class TimeBasedProgressAtVariableSpeed
- {
- public LongInTheVariableRange progress;
- public StartTime startTime = new();
- private speed =1.0;
-
- public TimeBasedProgressAtVariableSpeed(long needProgress, double speed)
- {
- if (needProgress <= 0) Debugger.Output("Bug:TimeBasedProgressAtVariableSpeed.needProgress (" + needProgress.ToString() + ") is less than 0.");
- this.needProgress = needProgress;
- this.speed = speed;
- }
- public TimeBasedProgressAtVariableSpeed()
- {
- this.needProgress = 0;
- }
- public long GetNeedProgress()
- {
- lock (progressLock) return needProgress;
- }
- public override string ToString()
- {
- lock (progressLock)
- {
- return "NeedProgress:" + Interlocked.CompareExchange(ref needProgress, -2, -2).ToString() + " ms, "
- + "FinishedProgress:" + progress.ToString() + "ms, "
- + "lastStartTime:" + lastStartT.ToString() + "ms";
- }
- }
- public long GetProgressNow()
- {
- lock (progressLock)
- {
- if (lastStartT == long.MaxValue) return progress;
- long progressNow = progress + Environment.TickCount64 - lastStartT;
- if (progressNow >= needProgress)
- {
- lastStartT = long.MaxValue;
- return progress = needProgress;
- }
- return progressNow;
- }
- }
-
- public bool IsFinished()
- {
- lock (progressLock)
- {
- if (progress >= needProgress) return true;
- if (progress + Environment.TickCount64 - lastStartT >= needProgress)
- {
- lastStartT = long.MaxValue;
- progress = needProgress;
- return true;
- }
- return false;
- }
- }
- public bool IsProgressing()
- {
- lock (progressLock)
- {
- if (lastStartT == long.MaxValue) return false;
- if (progress + Environment.TickCount64 - lastStartT >= needProgress)
- {
- lastStartT = long.MaxValue;
- progress = needProgress;
- return false;
- }
- return true;
- }
- }
-
- /// <summary>
- /// GetProgress<0则表明未开始
- /// </summary>
- public long GetProgress()
- {
- long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
- if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
- return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
- }
- public long GetNonNegativeProgress()
- {
- long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
- if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
- long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
- return progress < 0 ? 0 : progress;
- }
- /// <summary>
- /// GetProgress<0则表明未开始
- /// </summary>
- public long GetProgress(long time)
- {
- long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - time;
- if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
- return Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
- }
- public long GetNonNegativeProgress(long time)
- {
- long cutime = Interlocked.Read(ref endT) - time;
- if (cutime <= 0) return Interlocked.CompareExchange(ref needProgress, -2, -2);
- long progress = Interlocked.CompareExchange(ref needProgress, -2, -2) - cutime;
- return progress < 0 ? 0 : progress;
- }
- /// <summary>
- /// <0则表明未开始
- /// </summary>
- public static implicit operator long(TimeBasedProgressAtVariableSpeed pLong) => pLong.GetProgress();
-
- /// <summary>
- /// GetProgressDouble<0则表明未开始
- /// </summary>
- public double GetProgressDouble()
- {
- long cutime = Interlocked.CompareExchange(ref endT, -2, -2) - Environment.TickCount64;
- if (cutime <= 0) return 1;
- long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2);
- if (needTime == 0) return 0;
- return 1.0 - ((double)cutime / needTime);
- }
- public double GetNonNegativeProgressDouble(long time)
- {
- long cutime = Interlocked.Read(ref endT) - time;
- if (cutime <= 0) return 1;
- long needTime = Interlocked.CompareExchange(ref needProgress, -2, -2);
- if (needTime <= cutime) return 0;
- return 1.0 - ((double)cutime / needTime);
- }
-
- public bool Start(long needTime)
- {
- if (needTime <= 2)
- {
- Debugger.Output("Warning:Start TimeBasedProgressAtVariableSpeed with the needProgress (" + needTime.ToString() + ") which is less than 0.");
- return false;
- }
- //规定只有Start可以修改needT,且需要先访问startTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
- if (Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) != long.MaxValue) return false;
- Interlocked.Exchange(ref needProgress, needTime);
- return true;
- }
- public bool Start()
- {
- return Interlocked.CompareExchange(ref lastStartT, Environment.TickCount64, long.MaxValue) == long.MaxValue;
- }
- /// <summary>
- /// 使进度条强制终止清零
- /// </summary>
- public void Set0()
- {
- Interlocked.Exchange(ref lastStartT, long.MaxValue);
- Interlocked.Exchange(ref progress, 0);
- }
- /// <summary>
- /// 使进度条暂停
- /// </summary>
- public bool Pause()
- {
- Interlocked.CompareExchange(ref lastStartT, -2, -2)
- if (Environment.TickCount64 < )
- {
- Interlocked.Exchange(ref endT, long.MaxValue);
- return true;
- }
- return false;
- }
- }
- */
-
- /// <summary>
- /// 冷却时间为可变的CDms的bool,不支持查看当前进度,初始为True
- /// </summary>
- public class BoolUpdateEachCD
- {
- private long cd;
- private long nextUpdateTime = 0;
- public BoolUpdateEachCD(int cd)
- {
- if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
- this.cd = cd;
- }
- public BoolUpdateEachCD(long cd)
- {
- if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
- this.cd = cd;
- }
- public BoolUpdateEachCD(long cd, long startTime)
- {
- if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
- this.cd = cd;
- this.nextUpdateTime = startTime;
- }
-
- public long GetCD() => Interlocked.Read(ref cd);
-
- public bool TryUse()
- {
- long needTime = Interlocked.Exchange(ref nextUpdateTime, long.MaxValue);
- if (needTime <= Environment.TickCount64)
- {
- Interlocked.Exchange(ref nextUpdateTime, Environment.TickCount64 + Interlocked.Read(ref cd));
- return true;
- }
- Interlocked.Exchange(ref nextUpdateTime, needTime);
- return false;
- }
- public void SetCD(int cd)
- {
- if (cd <= 1) Debugger.Output("Bug:BoolUpdateEachCD.cd to " + cd.ToString() + ".");
- Interlocked.Exchange(ref this.cd, cd);
- }
- }
-
- /// <summary>
- /// 冷却时间为可变的CDms的进度条,初始为满
- /// </summary>
- public class LongProgressUpdateEachCD
- {
- private int isusing = 0;
- private long cd;
- private long nextUpdateTime = 0;
- public LongProgressUpdateEachCD(int cd)
- {
- if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
- this.cd = cd;
- }
- public LongProgressUpdateEachCD(long cd)
- {
- if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
- this.cd = cd;
- }
- public LongProgressUpdateEachCD(long cd, long startTime)
- {
- if (cd <= 1) Debugger.Output("Bug:LongProgressUpdateEachCD.cd (" + cd.ToString() + ") is less than 1.");
- this.cd = cd;
- this.nextUpdateTime = startTime;
- }
-
- public long GetRemainingTime()
- {
- long v = Interlocked.Read(ref nextUpdateTime) - Environment.TickCount64;
- return v < 0 ? 0 : v;
- }
- public long GetCD() => Interlocked.Read(ref cd);
-
- public bool TryUse()
- {
- if (Interlocked.Exchange(ref isusing, 1) == 1) return false;
- long needTime = Interlocked.Read(ref nextUpdateTime);
- if (needTime <= Environment.TickCount64)
- {
- Interlocked.Exchange(ref nextUpdateTime, Environment.TickCount64 + Interlocked.Read(ref cd));
- Interlocked.Exchange(ref isusing, 0);
- return true;
- }
- Interlocked.Exchange(ref isusing, 0);
- return false;
- }
- public void SetCD(int cd)
- {
- if (cd <= 1) Debugger.Output("Bug:Set LongProgressUpdateEachCD.cd to " + cd.ToString() + ".");
- Interlocked.Exchange(ref this.cd, cd);
- }
- }
-
- /// <summary>
- /// 一个保证在[0,maxNum],每CDms自动+1的int,支持可变的CD、maxNum(请确保大于0)
- /// </summary>
- public class IntNumUpdateEachCD
- {
- private int num;
- private int maxNum;
- private int cd;
- private long updateTime = 0;
- private readonly object numLock = new();
- public IntNumUpdateEachCD(int num, int maxNum, int cd)
- {
- if (num < 0) Debugger.Output("Bug:IntNumUpdateEachCD.num (" + num.ToString() + ") is less than 0.");
- if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateEachCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
- if (cd <= 0) Debugger.Output("Bug:IntNumUpdateEachCD.cd (" + cd.ToString() + ") is less than 0.");
- this.num = (num < maxNum) ? num : maxNum;
- this.maxNum = maxNum;
- this.cd = cd;
- this.updateTime = Environment.TickCount64;
- }
- /// <summary>
- /// 默认使num=maxNum
- /// </summary>
- public IntNumUpdateEachCD(int maxNum, int cd)
- {
- if (maxNum < 0) Debugger.Output("Bug:IntNumUpdateEachCD.maxNum (" + maxNum.ToString() + ") is less than 0.");
- if (cd <= 0) Debugger.Output("Bug:IntNumUpdateEachCD.cd (" + cd.ToString() + ") is less than 0.");
- this.num = this.maxNum = maxNum;
- this.cd = cd;
- }
- public IntNumUpdateEachCD()
- {
- this.num = this.maxNum = 0;
- this.cd = int.MaxValue;
- }
-
- public int GetMaxNum() { lock (numLock) return maxNum; }
- public int GetCD() { lock (numLock) return cd; }
- public int GetNum(long time)
- {
- lock (numLock)
- {
- if (num < maxNum && time - updateTime >= cd)
- {
- int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
- updateTime += add * cd;
- return (num += add);
- }
- return num;
- }
- }
- public static implicit operator int(IntNumUpdateEachCD aint) => aint.GetNum(Environment.TickCount64);
-
- /// <summary>
- /// 应当保证该subV>=0
- /// </summary>
- public int TrySub(int subV)
- {
- if (subV < 0) Debugger.Output("Bug:IntNumUpdateEachCD Try to sub " + subV.ToString() + ", which is less than 0.");
- long time = Environment.TickCount64;
- lock (numLock)
- {
- if (num < maxNum && time - updateTime >= cd)
- {
- int add = (int)Math.Min(maxNum - num, (time - updateTime) / cd);
- updateTime += add * cd;
- num += add;
- }
- if (num == maxNum) updateTime = time;
- num -= subV = Math.Min(subV, num);
- }
- return subV;
- }
- /// <summary>
- /// 应当保证该addV>=0
- /// </summary>
- public void TryAdd(int addV)
- {
- if (addV < 0) Debugger.Output("Bug:IntNumUpdateEachCD Try to add " + addV.ToString() + ", which is less than 0.");
- lock (numLock)
- {
- num += Math.Min(addV, maxNum - num);
- }
- }
- /// <summary>
- /// 若maxNum<=0则maxNum及Num设为0并返回False
- /// </summary>
- public bool SetMaxNumAndNum(int maxNum)
- {
- if (maxNum < 0) maxNum = 0;
- lock (numLock)
- {
- this.num = this.maxNum = maxNum;
- }
- return maxNum > 0;
- }
- /// <summary>
- /// 应当保证该maxnum>=0
- /// </summary>
- public void SetPositiveMaxNumAndNum(int maxNum)
- {
- lock (numLock)
- {
- this.num = this.maxNum = maxNum;
- }
- }
- /// <summary>
- /// 应当保证该maxnum>=0
- /// </summary>
- public void SetPositiveMaxNum(int maxNum)
- {
- lock (numLock)
- {
- if ((this.maxNum = maxNum) < num)
- num = maxNum;
- }
- }
- /// <summary>
- /// 若maxNum<=0则maxNum及Num设为0并返回False
- /// </summary>
- public bool SetMaxNum(int maxNum)
- {
- if (maxNum < 0) maxNum = 0;
- lock (numLock)
- {
- if ((this.maxNum = maxNum) < num)
- num = maxNum;
- }
- return maxNum > 0;
- }
- /// <summary>
- /// 若num<0则num设为0并返回False
- /// </summary>
- public bool SetNum(int num)
- {
- lock (numLock)
- {
- if (num < 0)
- {
- this.num = 0;
- updateTime = Environment.TickCount64;
- return false;
- }
- if (num < maxNum)
- {
- if (this.num == maxNum) updateTime = Environment.TickCount64;
- this.num = num;
- }
- else this.num = maxNum;
- return true;
- }
- }
- /// <summary>
- /// 应当保证该num>=0
- /// </summary>
- public void SetPositiveNum(int num)
- {
- lock (numLock)
- {
- if (num < maxNum)
- {
- if (this.num == maxNum) updateTime = Environment.TickCount64;
- this.num = num;
- }
- else this.num = maxNum;
- }
- }
- public void SetCD(int cd)
- {
- lock (numLock)
- {
- if (cd <= 0) Debugger.Output("Bug:Set IntNumUpdateEachCD.cd to " + cd.ToString() + ".");
- this.cd = cd;
- }
- }
- }
- }
|