Skip to content

Commit

Permalink
0.6.0.25 - Prevent CCounter from halting due to very large BPM (#759)
Browse files Browse the repository at this point in the history
- Prevent CCounter from halting due to very large BPM
  • Loading branch information
IepIweidieng authored Dec 6, 2024
1 parent 744370d commit 7e73a83
Show file tree
Hide file tree
Showing 20 changed files with 159 additions and 140 deletions.
144 changes: 94 additions & 50 deletions FDK/src/00.Common/CCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
///
/// double値を使う場合、t進行db、t進行LoopDbを使うこと。
/// また、double版では間隔の値はミリ秒単位ではなく、通常の秒単位になります。
/// Note: For the double version, the given interval is in second only for new CCounter(), not for Start().
/// </remarks>
public class CCounter {
public bool IsStarted {
Expand All @@ -33,23 +34,23 @@ public int CurrentValue {
set;
}

public double _Interval {
public double _msInterval {
get {
return this.Interval;
return this.msInterval;
}
set {
this.Interval = value >= 0 ? value : value * -1;
this.msInterval = Math.Max(1e-6, Math.Abs(value));
}
}

public double NowTime {
public double msNowTime {
get;
set;
}
// 状態プロパティ

public bool IsTicked {
get { return (this.NowTime != -1); }
get { return (this.msNowTime != -1); }
}
public bool IsStoped {
get { return !this.IsTicked; }
Expand All @@ -69,19 +70,19 @@ public CCounter() {
this.EndValue = 0;
this.CurrentValue = 0;
this.CurrentValue = 0;
this.NowTime = CSoundTimer.UnusedNum;
this.msNowTime = CSoundTimer.UnusedNum;
}

/// <summary>生成と同時に開始する。</summary>
public CCounter(double begin, double end, double interval, CTimer timer)
public CCounter(double begin, double end, double msInterval, CTimer timer)
: this() {
this.Start(begin, end, interval, timer);
this.Start(begin, end, msInterval, timer);
}

/// <summary>生成と同時に開始する。(double版)</summary>
public CCounter(double begin, double end, double interval, CSoundTimer timer)
public CCounter(double begin, double end, double secInterval, CSoundTimer timer)
: this() {
this.Start(begin, end, interval * 1000.0f, timer);
this.Start(begin, end, secInterval * 1000.0f, timer);
}


Expand All @@ -92,14 +93,14 @@ public CCounter(double begin, double end, double interval, CSoundTimer timer)
/// </summary>
/// <param name="begin">最初のカウント値。</param>
/// <param name="end">最後のカウント値。</param>
/// <param name="interval">カウント値を1増加させるのにかける時間(ミリ秒単位)。</param>
/// <param name="msInterval">カウント値を1増加させるのにかける時間(ミリ秒単位)。</param>
/// <param name="timer">カウントに使用するタイマ。</param>
public void Start(double begin, double end, double interval, CTimer timer) {
public void Start(double begin, double end, double msInterval, CTimer timer) {
this.BeginValue = begin;
this.EndValue = end;
this._Interval = interval;
this._msInterval = msInterval;
this.NormalTimer = timer;
this.NowTime = this.NormalTimer.NowTime;
this.msNowTime = this.NormalTimer.NowTimeMs;
this.CurrentValue = (int)begin;
this.IsStarted = true;
}
Expand All @@ -109,14 +110,14 @@ public void Start(double begin, double end, double interval, CTimer timer) {
/// </summary>
/// <param name="begin">最初のカウント値。</param>
/// <param name="end">最後のカウント値。</param>
/// <param name="interval">カウント値を1増加させるのにかける時間(秒単位)。</param>
/// <param name="msInterval">カウント値を1増加させるのにかける時間(ミリ秒単位)。</param>
/// <param name="timer">カウントに使用するタイマ。</param>
public void Start(double begin, double end, double interval, CSoundTimer timer) {
public void Start(double begin, double end, double msInterval, CSoundTimer timer) {
this.BeginValue = begin;
this.EndValue = end;
this._Interval = interval;
this._msInterval = msInterval;
this.TimerDB = timer;
this.NowTime = this.TimerDB.SystemTime_Double;
this.msNowTime = this.TimerDB.SystemTimeMs_Double;
this.CurrentValue = (int)begin;
this.IsStarted = true;
}
Expand All @@ -126,17 +127,21 @@ public void Start(double begin, double end, double interval, CSoundTimer timer)
/// カウント値が終了値に達している場合は、それ以上増加しない(終了値を維持する)。
/// </summary>
public void Tick() {
if ((this.NormalTimer != null) && (this.NowTime != CTimer.UnusedNum)) {
long num = this.NormalTimer.NowTime;
if (num < this.NowTime)
this.NowTime = num;
if ((this.NormalTimer != null) && (this.msNowTime != CTimer.UnusedNum)) {
long msNow = this.NormalTimer.NowTimeMs;
if (msNow < this.msNowTime)
this.msNowTime = msNow;

while ((num - this.NowTime) >= this.Interval) {
for (int i = 0; i < 8; ++i) {
if ((msNow - this.msNowTime) < this.msInterval)
return;
if (++this.CurrentValue > this.EndValue)
this.CurrentValue = (int)this.EndValue;

this.NowTime += this.Interval;
this.msNowTime += this.msInterval;
}

this.TickJump(msNow);
}
}

Expand All @@ -145,36 +150,58 @@ public void Tick() {
/// カウント値が終了値に達している場合は、それ以上増加しない(終了値を維持する)。
/// </summary>
public void TickDB() {
if ((this.TimerDB != null) && (this.NowTime != CSoundTimer.UnusedNum)) {
double num = this.TimerDB.NowTime;
if (num < this.NowTime)
this.NowTime = num;
if ((this.TimerDB != null) && (this.msNowTime != CSoundTimer.UnusedNum)) {
double msNow = this.TimerDB.NowTimeMs;
if (msNow < this.msNowTime)
this.msNowTime = msNow;

while ((num - this.NowTime) >= this.Interval) {
for (int i = 0; i < 8; ++i) {
if ((msNow - this.msNowTime) < this.msInterval)
return;
if (++this.CurrentValue > this.EndValue)
this.CurrentValue = (int)this.EndValue;

this.NowTime += this.Interval;
this.msNowTime += this.msInterval;
}

this.TickJump(msNow);
}
}

/// Jump over precalculated steps. Might be slower due to float division.
private void TickJump(double msNow) {
if ((msNow - this.msNowTime) < this.msInterval)
return;
int nStepsMax = (int)this.EndValue + 1 - (int)this.BeginValue;
long nSteps = (long)((msNow - this.msNowTime) / this.msInterval);
if (nSteps >= nStepsMax // attempt to prevent overflow
|| (this.CurrentValue += (int)nSteps) > this.EndValue
) {
this.CurrentValue = (int)this.EndValue;
}
this.msNowTime += nSteps * this.msInterval;
}

/// <summary>
/// 前回の t進行Loop() の呼び出しからの経過時間をもとに、必要なだけカウント値を増加させる。
/// カウント値が終了値に達している場合は、次の増加タイミングで開始値に戻る(値がループする)。
/// </summary>
public void TickLoop() {
if ((this.NormalTimer != null) && (this.NowTime != CTimer.UnusedNum)) {
long num = this.NormalTimer.NowTime;
if (num < this.NowTime)
this.NowTime = num;
if ((this.NormalTimer != null) && (this.msNowTime != CTimer.UnusedNum)) {
long msNow = this.NormalTimer.NowTimeMs;
if (msNow < this.msNowTime)
this.msNowTime = msNow;

while ((num - this.NowTime) >= this.Interval) {
for (int i = 0; i < 8; ++i) {
if ((msNow - this.msNowTime) < this.msInterval)
return;
if (++this.CurrentValue > this.EndValue)
this.CurrentValue = (int)this.BeginValue;

this.NowTime += this.Interval;
this.msNowTime += this.msInterval;
}

this.TickLoopJump(msNow);
}
}

Expand All @@ -183,30 +210,47 @@ public void TickLoop() {
/// カウント値が終了値に達している場合は、次の増加タイミングで開始値に戻る(値がループする)。
/// </summary>
public void TickLoopDB() {
if ((this.TimerDB != null) && (this.NowTime != CSoundTimer.UnusedNum)) {
double num = this.TimerDB.NowTime;
if (num < this.NowTime)
this.NowTime = num;
if ((this.TimerDB != null) && (this.msNowTime != CSoundTimer.UnusedNum)) {
double msNow = this.TimerDB.NowTimeMs;
if (msNow < this.msNowTime)
this.msNowTime = msNow;

while ((num - this.NowTime) >= this.Interval) {
for (int i = 0; i < 8; ++i) {
if ((msNow - this.msNowTime) < this.msInterval)
return;
if (++this.CurrentValue > this.EndValue)
this.CurrentValue = (int)this.BeginValue;

this.NowTime += this.Interval;
this.msNowTime += this.msInterval;
}

this.TickLoopJump(msNow);
}
}

/// Jump over precalculated steps. Might be slower due to float division.
private void TickLoopJump(double msNow) {
if ((msNow - this.msNowTime) < this.msInterval)
return;
int nStepsMax = (int)this.EndValue + 1 - (int)this.BeginValue;
long nSteps = (long)((msNow - this.msNowTime) / this.msInterval);
int dVal = (int)(nSteps % nStepsMax); // attempt to prevent overflow
if ((this.CurrentValue += dVal) > this.EndValue) {
this.CurrentValue = (int)this.BeginValue;
}
this.msNowTime += nSteps * this.msInterval;
}

/// <summary>
/// カウントを停止する。
/// これ以降に t進行() や t進行Loop() を呼び出しても何も処理されない。
/// </summary>
public void Stop() {
this.NowTime = CTimer.UnusedNum;
this.msNowTime = CTimer.UnusedNum;
}

public void ChangeInterval(double Value) {
this._Interval = Value;
this._msInterval = Value;
}

// その他
Expand All @@ -232,23 +276,23 @@ public void KeyIntervalFunc(bool pressFlag, KeyProcess keyProcess) {

keyProcess();
this.CurrentValue = second;
this.NowTime = this.NormalTimer.NowTime;
this.msNowTime = this.NormalTimer.NowTimeMs;
return;

case second:

if ((this.NormalTimer.NowTime - this.NowTime) > 200) {
if ((this.NormalTimer.NowTimeMs - this.msNowTime) > 200) {
keyProcess();
this.NowTime = this.NormalTimer.NowTime;
this.msNowTime = this.NormalTimer.NowTimeMs;
this.CurrentValue = later;
}
return;

case later:

if ((this.NormalTimer.NowTime - this.NowTime) > 30) {
if ((this.NormalTimer.NowTimeMs - this.msNowTime) > 30) {
keyProcess();
this.NowTime = this.NormalTimer.NowTime;
this.msNowTime = this.NormalTimer.NowTimeMs;
}
return;
}
Expand All @@ -265,7 +309,7 @@ public void KeyIntervalFunc(bool pressFlag, KeyProcess keyProcess) {
//-----------------
private CTimer NormalTimer;
private CSoundTimer TimerDB;
private double Interval;
private double msInterval;
//-----------------
#endregion
}
8 changes: 4 additions & 4 deletions FDK/src/00.Common/CFPS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public CFPS() {
this.NowFPS = 0;
this.DeltaTime = 0;
this.FPSTimer = new CTimer(CTimer.TimerType.MultiMedia);
this.BeginTime = this.FPSTimer.NowTime;
this.BeginTime = this.FPSTimer.NowTimeMs;
this.CoreFPS = 0;
this.ChangedFPS = false;
}
Expand All @@ -36,9 +36,9 @@ public void Update() {
this.ChangedFPS = false;

const long INTERVAL = 1000;
this.DeltaTime = (this.FPSTimer.NowTime - this.PrevFrameTime) / 1000.0;
PrevFrameTime = this.FPSTimer.NowTime;
while ((this.FPSTimer.NowTime - this.BeginTime) >= INTERVAL) {
this.DeltaTime = (this.FPSTimer.NowTimeMs - this.PrevFrameTime) / 1000.0;
PrevFrameTime = this.FPSTimer.NowTimeMs;
while ((this.FPSTimer.NowTimeMs - this.BeginTime) >= INTERVAL) {
this.NowFPS = this.CoreFPS;
this.CoreFPS = 0;
this.ChangedFPS = true;
Expand Down
25 changes: 0 additions & 25 deletions FDK/src/00.Common/CTimerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,6 @@ public double SystemTimeMs_Double {
}
public abstract void Dispose();

#region [ DTXMania用に、語尾にmsのつかない宣言を追加 ]
public long SystemTime {
get { return SystemTimeMs; }
}
public long NowTime {
get { return NowTimeMs; }
set { NowTimeMs = value; }
}
public long PrevResetTime {
get { return PrevResetTimeMs; }
}

//double
public double SystemTime_Double {
get { return SystemTimeMs_Double; }
}
public double NowTime_Double {
get { return NowTimeMs_Double; }
set { NowTimeMs_Double = value; }
}
public double PrevResetTime_Double {
get { return PrevResetTimeMs_Double; }
}
#endregion

public long NowTimeMs {
get {
if (this.StopCount > 0)
Expand Down
4 changes: 2 additions & 2 deletions OpenTaiko/src/Stages/04.Config/CActConfigList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ public int t進行描画(bool b項目リスト側にフォーカスがある) {
#region [ 初めての進行描画 ]
//-----------------
if (base.IsFirstDraw) {
this.nスクロール用タイマ値 = (long)(SoundManager.PlayTimer.NowTime * OpenTaiko.ConfigIni.SongPlaybackSpeed);
this.nスクロール用タイマ値 = (long)(SoundManager.PlayTimer.NowTimeMs * OpenTaiko.ConfigIni.SongPlaybackSpeed);
this.ct三角矢印アニメ.Start(0, 9, 50, OpenTaiko.Timer);
base.IsFirstDraw = false;
}
Expand All @@ -1094,7 +1094,7 @@ public int t進行描画(bool b項目リスト側にフォーカスがある) {

#region [ 項目スクロールの進行 ]
//-----------------
long n現在時刻 = OpenTaiko.Timer.NowTime;
long n現在時刻 = OpenTaiko.Timer.NowTimeMs;
if (n現在時刻 < this.nスクロール用タイマ値) this.nスクロール用タイマ値 = n現在時刻;

const int INTERVAL = 2; // [ms]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override int Draw() {
if ((this.ctDelayedDisplay.CurrentValue >= 0) && this.bNewPreimageStillLoading) {
this.tUpdatePreimage(OpenTaiko.stageSongSelect.r現在選択中のスコア);
OpenTaiko.Timer.Update();
this.ctDelayedDisplay.NowTime = OpenTaiko.Timer.NowTime;
this.ctDelayedDisplay.msNowTime = OpenTaiko.Timer.NowTimeMs;
this.bNewPreimageLoaded = true;
} else if (this.ctDelayedDisplay.IsEnded && this.ctDelayedDisplay.IsTicked) {
this.ctDelayedDisplay.Stop();
Expand Down
4 changes: 2 additions & 2 deletions OpenTaiko/src/Stages/05.SongSelect/CStageSongSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ public void Add(EInstrumentPad _eInst, EPadFlag _ePad) {
STCommandTime _stct = new STCommandTime {
eInst = _eInst,
ePad = _ePad,
time = OpenTaiko.Timer.NowTime
time = OpenTaiko.Timer.NowTimeMs
};

if (stct.Count >= buffersize) {
Expand All @@ -1400,7 +1400,7 @@ public bool CheckCommand(EPadFlag[] _ePad, EInstrumentPad _eInst) {
return false;
}

long curTime = OpenTaiko.Timer.NowTime;
long curTime = OpenTaiko.Timer.NowTimeMs;
//Debug.WriteLine("Start checking...targetCount=" + targetCount);
for (int i = targetCount - 1, j = stciCount - 1; i >= 0; i--, j--) {
if (_ePad[i] != stct[j].ePad) {
Expand Down
Loading

0 comments on commit 7e73a83

Please sign in to comment.