Skip to content

Commit

Permalink
Merge pull request #6 from Delsin-Yu/4.x
Browse files Browse the repository at this point in the history
Fixes issue with headless mode
  • Loading branch information
Atlinx authored Dec 30, 2023
2 parents 1e55741 + 54a5820 commit 8bb9680
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
27 changes: 18 additions & 9 deletions addons/GDTask/GDTask.Delay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ static NextFramePromise()
TaskPool.RegisterSizeGetter(typeof(NextFramePromise), () => pool.Size);
}

int frameCount;
bool isMainThread;
ulong frameCount;
CancellationToken cancellationToken;
GDTaskCompletionSourceCore<AsyncUnit> core;

Expand All @@ -273,7 +274,9 @@ public static IGDTaskSource Create(PlayerLoopTiming timing, CancellationToken ca
result = new NextFramePromise();
}

result.frameCount = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetFramesDrawn() : -1;
result.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread;
if (result.isMainThread)
result.frameCount = Engine.GetProcessFrames();
result.cancellationToken = cancellationToken;

TaskTracker.TrackActiveTask(result, 3);
Expand Down Expand Up @@ -319,7 +322,7 @@ public bool MoveNext()
return false;
}

if (frameCount == Engine.GetFramesDrawn())
if (isMainThread && frameCount == Engine.GetProcessFrames())
{
return true;
}
Expand Down Expand Up @@ -348,7 +351,8 @@ static DelayFramePromise()
TaskPool.RegisterSizeGetter(typeof(DelayFramePromise), () => pool.Size);
}

int initialFrame;
bool isMainThread;
ulong initialFrame;
int delayFrameCount;
CancellationToken cancellationToken;

Expand All @@ -373,7 +377,9 @@ public static IGDTaskSource Create(int delayFrameCount, PlayerLoopTiming timing,

result.delayFrameCount = delayFrameCount;
result.cancellationToken = cancellationToken;
result.initialFrame = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetFramesDrawn() : -1;
result.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread;
if (result.isMainThread)
result.initialFrame = Engine.GetProcessFrames();

TaskTracker.TrackActiveTask(result, 3);

Expand Down Expand Up @@ -427,7 +433,7 @@ public bool MoveNext()
}

// skip in initial frame.
if (initialFrame == Engine.GetFramesDrawn())
if (isMainThread && initialFrame == Engine.GetProcessFrames())
{
#if DEBUG
// force use Realtime.
Expand Down Expand Up @@ -476,7 +482,8 @@ static DelayPromise()
TaskPool.RegisterSizeGetter(typeof(DelayPromise), () => pool.Size);
}

int initialFrame;
bool isMainThread;
ulong initialFrame;
double delayTimeSpan;
double elapsed;
CancellationToken cancellationToken;
Expand All @@ -502,7 +509,9 @@ public static IGDTaskSource Create(TimeSpan delayTimeSpan, PlayerLoopTiming timi
result.elapsed = 0.0f;
result.delayTimeSpan = (float)delayTimeSpan.TotalSeconds;
result.cancellationToken = cancellationToken;
result.initialFrame = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetFramesDrawn() : -1;
result.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread;
if (result.isMainThread)
result.initialFrame = Engine.GetProcessFrames();

TaskTracker.TrackActiveTask(result, 3);

Expand Down Expand Up @@ -549,7 +558,7 @@ public bool MoveNext()

if (elapsed == 0.0f)
{
if (initialFrame == Engine.GetFramesDrawn())
if (isMainThread && initialFrame == Engine.GetProcessFrames())
{
return true;
}
Expand Down
9 changes: 6 additions & 3 deletions addons/GDTask/PlayerLoopTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ bool IPlayerLoopItem.MoveNext()

sealed class DeltaTimePlayerLoopTimer : PlayerLoopTimer
{
int initialFrame;
bool isMainThread;
ulong initialFrame;
double elapsed;
double interval;

Expand All @@ -156,7 +157,7 @@ protected override bool MoveNextCore()
{
if (elapsed == 0.0)
{
if (initialFrame == Engine.GetFramesDrawn())
if (isMainThread && initialFrame == Engine.GetProcessFrames())
{
return true;
}
Expand All @@ -174,7 +175,9 @@ protected override bool MoveNextCore()
protected override void ResetCore(TimeSpan? interval)
{
this.elapsed = 0.0;
this.initialFrame = GDTaskPlayerLoopAutoload.IsMainThread ? Engine.GetFramesDrawn() : -1;
this.isMainThread = GDTaskPlayerLoopAutoload.IsMainThread;
if (this.isMainThread)
this.initialFrame = Engine.GetProcessFrames();
if (interval != null)
{
this.interval = (float)interval.Value.TotalSeconds;
Expand Down

0 comments on commit 8bb9680

Please sign in to comment.