Skip to content

Commit

Permalink
add threadutil
Browse files Browse the repository at this point in the history
  • Loading branch information
KoloInDaCrib committed Jan 21, 2025
1 parent 92d94e2 commit e199f8c
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 39 deletions.
65 changes: 26 additions & 39 deletions source/funkin/play/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import haxe.Int64;
import funkin.api.discord.DiscordClient;
#end
#if sys
import sys.thread.Thread;
import funkin.util.ThreadUtil;
#end

/**
Expand Down Expand Up @@ -1776,54 +1776,41 @@ class PlayState extends MusicBeatSubState
}

#if sys
var destroyThread:Bool = false;

var isMainThreadFrozen:Bool = false;
var sideThread:Thread;

/**
* All of this to prevent one small bug.
* Setups the Thread to check if the game has been dragged by the window's title bar.
*/
function initThreads()
{
// Main thread sends the message via the boolean and side thread checks if it has been updated in the main thread
sideThread = Thread.create(function() {
Sys.sleep(1); // start the thing after a sec to let everything generate and such

while (true)
{
if (destroyThread) break;

var lostFocus:Bool = false;
@:privateAccess
lostFocus = FlxG.game._lostFocus;
ThreadUtil.createLoopingThread("playStateWindow", function() {
var lostFocus:Bool = false;
@:privateAccess
lostFocus = FlxG.game._lostFocus;

if (!initialized || health <= Constants.HEALTH_MIN || isGamePaused || !generatedMusic || criticalFailure || lostFocus || justUnpaused) continue;

if (isMainThreadFrozen)
{
trace("The game is frozen! Pausing.");

persistentUpdate = false;
persistentDraw = true;
if (!initialized || health <= Constants.HEALTH_MIN || isGamePaused || !generatedMusic || criticalFailure || lostFocus || justUnpaused) return;

var pauseSubState:FlxSubState = new PauseSubState({mode: isChartingMode ? Charting : Standard});
if (isMainThreadFrozen)
{
trace("The game is frozen! Pausing.");

FlxTransitionableState.skipNextTransIn = true;
FlxTransitionableState.skipNextTransOut = true;
pauseSubState.camera = camCutscene;
openSubState(pauseSubState);
}
else
{
// trace("Not frozen! We resetting and checking again!");
}
persistentUpdate = false;
persistentDraw = true;

isMainThreadFrozen = true;
var pauseSubState:FlxSubState = new PauseSubState({mode: isChartingMode ? Charting : Standard});

Sys.sleep(FlxG.maxElapsed); // we sleep so that we can let the damn thing update!
FlxTransitionableState.skipNextTransIn = true;
FlxTransitionableState.skipNextTransOut = true;
pauseSubState.camera = camCutscene;
openSubState(pauseSubState);
}
});
else
{
// trace("Not frozen! We resetting and checking again!");
}

isMainThreadFrozen = true;
}, 1, FlxG.maxElapsed);
}
#end

Expand Down Expand Up @@ -3172,8 +3159,8 @@ class PlayState extends MusicBeatSubState
function performCleanup():Void
{
#if sys
// If we have a thread running, kill it.
destroyThread = true;
// If we have a thread running, stop it.
ThreadUtil.stopThread("playStateWindow");
#end

// If the camera is being tweened, stop it.
Expand Down
126 changes: 126 additions & 0 deletions source/funkin/util/ThreadUtil.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package funkin.util;

#if sys
import sys.thread.Thread;
#end

/**
* Utilities for creating and managing Threads. Only available on desktop devices.
*/
class ThreadUtil
{
#if sys
/**
* Data regarding every looping Thread.
*/
public static var loopingThreads:Map<String, ThreadParams> = [];
#end

/**
* Creates a Thread that starts after a set time.
* @param job The function to execute after the delay.
* @param delay The delay itself.
*/
public static function createDelayedThread(job:Void->Void, delay:Float = 1.0)
{
#if sys
return Thread.create(function() {
Sys.sleep(delay);

job();
});
#else
throw "Threads aren't supported on this device.";
return null;
#end
}

/**
* Creates a Thread that loops infinitely until destroyed.
* @param id The ID of the Thread, used for pausing, resuming and stopping.
* @param job The function to execute.
* @param startDelay The delay before the loop begins.
* @param loopDelay The delay between every loop.
*/
public static function createLoopingThread(id:String, job:Void->Void, startDelay:Float = 1.0, loopDelay:Float = 0.0)
{
#if sys
var threadInfo:ThreadParams = {thread: null, isPaused: false, isDestroyed: false}
loopingThreads.set(id, threadInfo);

var thread:Thread = Thread.create(function() {
Sys.sleep(startDelay);

while (!threadInfo.isDestroyed)
{
if (threadInfo.isPaused) continue;

job();

Sys.sleep(loopDelay);
}

// Clean-up.
if (threadInfo.isDestroyed)
{
threadInfo.thread = null;
loopingThreads.remove(id);
}
});

threadInfo.thread = thread;
return thread;
#else
throw "Threads aren't supported on this device.";
return null;
#end
}

/**
* Pauses a looping Thread.
* @param id The ID of the Thread to pause.
*/
public static function pauseThread(id:String)
{
#if sys
if (loopingThreads.exists(id)) loopingThreads[id].isPaused = true;
#else
throw "Threads aren't supported on this device.";
#end
}

/**
* Resumes a looping Thread.
* @param id The ID of the Thread to resume.
*/
public static function resumeThread(id:String)
{
#if sys
if (loopingThreads.exists(id)) loopingThreads[id].isPaused = false;
#else
throw "Threads aren't supported on this device.";
#end
}

/**
* Stops a looping Thread and destroys it afterwards.
* @param id The ID of the Thread to stop and destroy.
*/
public static function stopThread(id:String)
{
#if sys
if (loopingThreads.exists(id)) loopingThreads[id].isDestroyed = true;
#else
throw "Threads aren't supported on this device.";
#end
}
}

#if sys
typedef ThreadParams =
{
var thread:Thread;
var isPaused:Bool;
var isDestroyed:Bool;
}
#end

0 comments on commit e199f8c

Please sign in to comment.