softcoded multithreading

This commit is contained in:
Yoshman29
2023-01-12 00:46:25 +01:00
parent ef5bd52612
commit 114dcfcd70
6 changed files with 113 additions and 7 deletions
+1 -1
View File
@@ -1 +1 @@
2008
2010
+1 -1
View File
@@ -157,7 +157,7 @@ class PauseSubState extends MusicBeatSubstate
override function destroy()
{
FlxG.cameras.remove(camera, true);
pauseScript.call("destroy");
pauseScript.call("onDestroy");
if (pauseMusic != null) pauseMusic.destroy();
pauseScript.destroy();
+3 -3
View File
@@ -5,8 +5,8 @@ import openfl.Assets;
import hscript.*;
class HScript extends Script {
var interp:Interp;
var expr:Expr;
public var interp:Interp;
public var expr:Expr;
public override function onCreate(path:String) {
super.onCreate(path);
@@ -72,7 +72,7 @@ class HScript extends Script {
var oldParent = interp.scriptObject;
onCreate(path);
for(k=>e in Script.getDefaultVariables())
for(k=>e in Script.getDefaultVariables(this))
set(k, e);
load();
@@ -0,0 +1,89 @@
package funkin.scripting;
#if ALLOW_MULTITHREADING
import sys.thread.Thread;
import flixel.util.FlxDestroyUtil;
#end
import hscript.IHScriptCustomBehaviour;
class MultiThreadedScript implements IFlxDestroyable implements IHScriptCustomBehaviour {
var thread:#if ALLOW_MULTITHREADING Thread #else Dynamic #end;
/**
* Script being ran.
*/
public var script:Script;
private var __variables:Array<String>;
/**
* Return value of the last call.
*/
public var returnValue:Dynamic = null;
/**
* Whenever the current call has ended.
*/
public var callEnded:Bool = true;
public function new(path:String, ?parentScript:Script) {
script = Script.create(path);
if (parentScript != null) {
if (script is HScript && parentScript is HScript) {
var hscript = cast(script, HScript);
var parentHScript = cast(parentScript, HScript);
hscript.interp.variables = parentHScript.interp.variables;
hscript.interp.publicVariables = parentHScript.interp.publicVariables;
hscript.interp.staticVariables = parentHScript.interp.staticVariables;
script.setParent(parentHScript.interp.scriptObject);
}
}
script.load();
thread = Thread.createWithEventLoop(function() {
// Prevent the thread from being auto deleted
Thread.current().events.promise();
});
__variables = Type.getInstanceFields(Type.getClass(this));
}
public inline function hget(name:String):Dynamic
return __variables.contains(name) ? Reflect.getProperty(this, name) : script.get(name);
public inline function hset(name:String, val:Dynamic):Dynamic {
if (__variables.contains(name))
Reflect.setProperty(this, name, val);
else
script.set(name, val);
return val;
}
public function call(func:String, args:Array<Dynamic>) {
thread.events.run(function() {
callEnded = false;
returnValue = script.call(func, args);
callEnded = true;
});
}
public function destroy() {
if (script != null) {
script.call("onDestroy");
script.destroy();
}
if (thread != null) {
thread.events.runPromised(function() {
// close the thing
});
}
}
}
+4 -2
View File
@@ -17,7 +17,7 @@ class Script extends FlxBasic implements IFlxDestroyable {
public static var staticVariables:Map<String, Dynamic> = [];
public static function getDefaultVariables():Map<String, Dynamic> {
public static function getDefaultVariables(?script:Script):Map<String, Dynamic> {
return [
// Haxe related stuff
"Std" => Std,
@@ -78,11 +78,13 @@ class Script extends FlxBasic implements IFlxDestroyable {
"CustomShader" => funkin.shaders.CustomShader,
"FunkinText" => funkin.ui.FunkinText,
"Alphabet" => funkin.ui.Alphabet,
"CoolUtil" => funkin.utils.CoolUtil,
"IniUtil" => funkin.utils.IniUtil,
"XMLUtil" => funkin.utils.XMLUtil,
"ZipUtil" => funkin.utils.ZipUtil,
"MarkdownUtil" => funkin.utils.MarkdownUtil,
"EngineUtil" => funkin.utils.EngineUtil,
];
}
public static function getDefaultPreprocessors():Map<String, Dynamic> {
@@ -157,7 +159,7 @@ class Script extends FlxBasic implements IFlxDestroyable {
fileName = Path.withoutDirectory(path);
this.path = path;
onCreate(path);
for(k=>e in getDefaultVariables()) {
for(k=>e in getDefaultVariables(this)) {
set(k, e);
}
}
+15
View File
@@ -0,0 +1,15 @@
package funkin.utils;
import funkin.scripting.Script;
import funkin.scripting.MultiThreadedScript;
class EngineUtil {
/**
* Starts a new multithreaded script.
* This script will share all the variables with the current one, which means already existing callbacks will be replaced by new ones on conflict.
* @param path
*/
public static function startMultithreadedScript(path:String) {
return new MultiThreadedScript(path, Script.curScript);
}
}