diff --git a/examplemods/threadtesting.js b/examplemods/threadtesting.js new file mode 100644 index 0000000..1b0a633 --- /dev/null +++ b/examplemods/threadtesting.js @@ -0,0 +1,63 @@ +//SUCCESS - While there is no TeaVM thread actively running, I am able to run an asyncronous function, and get a result. +ModAPI.hooks._teavm.$rt_startThread(() => { + return ModAPI.hooks.methods.nlevi_PlatformRuntime_downloadRemoteURI(ModAPI.util.str("data:text/plain,hi")) +}, function (...args) { console.log(this, args) }) + + +//WIP - Pausing and resuming client thread +globalThis.suspendTest = function (...args) { + if (!ModAPI.util.isCritical()) { + var thread = ModAPI.hooks._teavm.$rt_nativeThread(); + var javaThread = ModAPI.hooks._teavm.$rt_getThread(); + globalThis.testThread = thread; + console.log("BeforeAnything: ", thread.stack); + thread.suspend(function () { + console.log("Pausing for 10 seconds.", thread.stack); + setTimeout(function () { + console.log("Resuming...", thread.stack); + ModAPI.hooks._teavm.$rt_setThread(javaThread); + thread.resume(); + console.log("After resume: ", thread.stack); + }, 10000); + }); + } + return suspendTest.apply(this, args); +} + + + + + +function jl_Thread_sleep$_asyncCall_$(millis) { + var thread = $rt_nativeThread(); + var javaThread = $rt_getThread(); + var callback = function () { }; + callback.$complete = function (val) { + thread.attribute = val; + $rt_setThread(javaThread); + thread.resume(); + }; + callback.$error = function (e) { + thread.attribute = $rt_exception(e); + $rt_setThread(javaThread); + thread.resume(); + }; + callback = otpp_AsyncCallbackWrapper_create(callback); + thread.suspend(function () { + try { + jl_Thread_sleep0(millis, callback); + } catch ($e) { + callback.$error($rt_exception($e)); + } + }); + return null; +} +function jl_Thread_sleep0($millis, $callback) { + var $current, $handler; + $current = jl_Thread_currentThread(); + $handler = new jl_Thread$SleepHandler; + $handler.$thread = $current; + $handler.$callback = $callback; + $handler.$scheduleId = otp_Platform_schedule($handler, Long_ge($millis, Long_fromInt(2147483647)) ? 2147483647 : Long_lo($millis)); + $current.$interruptHandler = $handler; +} \ No newline at end of file diff --git a/index.html b/index.html index a013e29..7dc79cf 100644 --- a/index.html +++ b/index.html @@ -169,7 +169,8 @@ - + + diff --git a/injector.js b/injector.js index cfb6243..c0c0c63 100644 --- a/injector.js +++ b/injector.js @@ -95,10 +95,13 @@ var main;(function(){` } ); + patchedFile = patchedFile.replaceAll("function TeaVMThread(", "globalThis.ModAPI.hooks.TeaVMThread = TeaVMThread;\nfunction TeaVMThread("); + patchedFile = patchedFile.replace( ` id="game_frame">`, ` id="game_frame"> \