Skip to content

Commit

Permalink
Fix memleak
Browse files Browse the repository at this point in the history
  • Loading branch information
ZXMushroom63 committed Aug 28, 2024
1 parent 58b8323 commit 2e70076
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 43 deletions.
9 changes: 0 additions & 9 deletions examplemods/minecraft_on_3x_speed.js

This file was deleted.

49 changes: 49 additions & 0 deletions examplemods/timescale_command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
(() => {
PluginAPI.addEventListener("sendchatmessage", (event) => {
if (event.message.toLowerCase().startsWith("/timescale")) {
var speed = parseFloat(event.message.split(" ")[1]);
if (!speed) {
PluginAPI.javaClient.$timer.$timerSpeed = 1;
} else {
if (speed < 1) {
speed = 1 / Math.round(1 / speed);
} else {
speed = Math.round(speed);
}
PluginAPI.javaClient.$timer.$timerSpeed = speed;
}
PluginAPI.displayToChat("[Timescale] Set world timescale to " + speed + ".");
}
});
PluginAPI.dedicatedServer.appendCode(function () {
globalThis.timeScale = 1n;
globalThis.timeScaleDividing = false;
PluginAPI.addEventListener("handleslashcommand", (event) => {
if (event.command.toLowerCase().startsWith("/timescale")) {
var speed = parseFloat(event.command.split(" ")[1]);
if (!speed) {
globalThis.timeScale = 1n;
globalThis.timeScaleDividing = false;
} else {
if (speed < 1) {
globalThis.timeScaleDividing = true;
globalThis.timeScale = BigInt(Math.round(1 / speed));
} else {
globalThis.timeScaleDividing = false;
globalThis.timeScale = BigInt(Math.round(speed));
}
}
event.preventDefault = true;
}
});
const original_getCurrentTime = ModAPI.hooks.methods.nms_MinecraftServer_getCurrentTimeMillis;
PluginAPI.hooks.methods.nms_MinecraftServer_getCurrentTimeMillis = function () {
if (globalThis.timeScaleDividing) {
return original_getCurrentTime() / globalThis.timeScale;
} else {
return original_getCurrentTime() * globalThis.timeScale;
}
};
});

})();
71 changes: 53 additions & 18 deletions injector.html
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,17 @@ <h4>
<script>
var modapi_preinit = `globalThis.ModAPI ||= {};
ModAPI.hooks ||= {};
ModAPI.hooks.freezeCallstack = false;
ModAPI.hooks._rippedData ||= [];
ModAPI.hooks._teavmMethods ||= {};
ModAPI.hooks._teavm ||= {};
ModAPI.hooks._rippedConstructors ||= {};
ModAPI.hooks.methods ||= {};
ModAPI.hooks._rippedMethodTypeMap ||= {};
ModAPI.hooks._postInit ||= ()=>{};
ModAPI.hooks._rippedStaticProperties ||= {};
ModAPI.hooks._rippedStaticIndexer ||= {};
`;
var freezeCallstack = `if(ModAPI.hooks.freezeCallstack){return false};`;
document.querySelector("#giveme").addEventListener("click", () => {
if (
!document.querySelector("input").files ||
Expand Down Expand Up @@ -246,6 +248,18 @@ <h4>
/*/EaglerForge Client Patch/*/`
);

patchedFile = patchedFile.replaceAll(`return thread != null && thread.isResuming()`, (match)=>{
return freezeCallstack + match;
});

patchedFile = patchedFile.replaceAll(`return thread != null && thread.isSuspending();`, (match)=>{
return freezeCallstack + match;
});

patchedFile = patchedFile.replaceAll(`return $rt_currentNativeThread;`, (match)=>{
return `if(ModAPI.hooks.freezeCallstack){return {push: (a)=>{console.log("Data pushed to stack: ") + a}, pop: ()=>{console.warn("Frozen stack was popped, context is now unstable.")}}};` + match;
});

patchedFile = patchedFile.replace(
` id="game_frame">`,
` id="game_frame">
Expand Down Expand Up @@ -349,7 +363,7 @@ <h4>
patchedFile = patchedFile.replaceAll(/function \$rt_\S+?\(/gm, (match)=>{
var name = match.replace("function ", "");
name = name.substring(0, name.length - 1);
return `ModAPI.hooks._teavmMethods[\`${name}\`]=${name};
return `ModAPI.hooks._teavm[\`${name}\`]=${name};
` + match;
});
patchedFile = patchedFile.replaceAll(/main\(\);\s*?}/gm, (match) => {
Expand All @@ -369,10 +383,19 @@ <h4>
globalThis.PluginAPI ||= ModAPI;
ModAPI.mcinstance ||= {};
ModAPI.javaClient ||= {};
ModAPI.server = ModAPI.serverInstance = null;
ModAPI.dedicatedServer ||= {};
ModAPI.dedicatedServer._data ||= [];
ModAPI.dedicatedServer._wasUsed = false;
ModAPI.dedicatedServer.appendCode = function (code) {
ModAPI.dedicatedServer._data.push(code);
if (ModAPI.dedicatedServer._wasUsed) {
return console.warn("The dedicated server has already launched, ModAPI.dedicatedServer.appendCode() is useless.");
}
if (typeof code === "function") {
ModAPI.dedicatedServer._data.push("(" + code.toString() + ")()");
} else if (typeof code === "string") {
ModAPI.dedicatedServer._data.push(code);
}
}
ModAPI.util ||= {};
ModAPI.util.getMethodFromPackage = function (classId, methodName) {
Expand Down Expand Up @@ -647,25 +670,21 @@ <h4>
return uint16Array;
}
var stringDefaultConstructor = ModAPI.hooks._classMap["java.lang.String"].constructors.filter(x => { return x.length === 0 })[0];
ModAPI.util.string = ModAPI.util.str = function (string) {
var jclString = stringDefaultConstructor();
jclString.$characters.data = ModAPI.util.stringToUint16Array(string);
return jclString;
//Overrides $rt_resuming, $rt_suspending, $rt_currentThread. Experimental, but should be used if call stack leaks occur as a result of running internal code.
ModAPI.freezeCallstack = function () {
ModAPI.hooks.freezeCallstack = true;
}
ModAPI.unfreezeCallstack = function () {
ModAPI.hooks.freezeCallstack = false;
}
ModAPI.util.string = ModAPI.util.str = ModAPI.hooks._teavm.$rt_str;
ModAPI.util.setStringContent = function (jclString) {
ModAPI.util.setStringContent = function (jclString, string) {
jclString.$characters.data = ModAPI.util.stringToUint16Array(string);
}
ModAPI.util.jclStrToJsStr = function (jclString) {
var uint16Array = jclString.$characters.data;
let str = '';
for (let i = 0; i < uint16Array.length; i++) {
str += String.fromCharCode(uint16Array[i]);
}
return str;
}
ModAPI.util.jclStrToJsStr = ModAPI.util.unstr = ModAPI.util.unstring = ModAPI.util.ustr = ModAPI.hooks._teavm.$rt_ustr;
ModAPI.displayToChat = function (param) {
var v = typeof param === "object" ? param.msg : (param + "");
Expand Down Expand Up @@ -697,6 +716,8 @@ <h4>
const integratedServerStartupMethod = ModAPI.hooks.methods[integratedServerStartup];
ModAPI.hooks.methods[integratedServerStartup] = function (worker, bootstrap) {
var x = integratedServerStartupMethod.apply(this, [worker, bootstrap + ";" + globalThis.modapi_postinit + ";" + ModAPI.dedicatedServer._data.join(";")]);
ModAPI.dedicatedServer._data = [];
ModAPI.dedicatedServer._wasUsed = true;
return x;
};
Expand All @@ -712,7 +733,10 @@ <h4>
if (data.preventDefault) {
return;
}
return sendChatMessage.apply(this, [$this, ModAPI.util.str(data.message) || $message]);
if (typeof data.message === "string") {
ModAPI.util.setStringContent($message, data.message)
}
return sendChatMessage.apply(this, [$this, $message]);
}
ModAPI.events.newEvent("tick");
Expand All @@ -733,9 +757,20 @@ <h4>
ModAPI.hooks.methods[serverStartMethodName] = function ($this) {
var x = serverStartMethod.apply(this, [$this]);
ModAPI.server = ModAPI.serverInstance = new Proxy($this, ModAPI.util.TeaVM_to_Recursive_BaseData_ProxyConf);
ModAPI.rawServer = $this;
ModAPI.events.callEvent("serverstart", {});
return x;
}
ModAPI.events.newEvent("serverstop");
const serverStopMethodName = ModAPI.util.getMethodFromPackage("net.minecraft.server.MinecraftServer", "stopServer");
const serverStopMethod = ModAPI.hooks.methods[serverStopMethodName];
ModAPI.hooks.methods[serverStopMethodName] = function ($this) {
var x = serverStopMethod.apply(this, [$this]);
ModAPI.server = ModAPI.serverInstance = null;
ModAPI.events.callEvent("serverstop", {});
return x;
}
})();`;
</script>

Expand Down
53 changes: 37 additions & 16 deletions postinit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@
globalThis.PluginAPI ||= ModAPI;
ModAPI.mcinstance ||= {};
ModAPI.javaClient ||= {};
ModAPI.server = ModAPI.serverInstance = null;
ModAPI.dedicatedServer ||= {};
ModAPI.dedicatedServer._data ||= [];
ModAPI.dedicatedServer._wasUsed = false;
ModAPI.dedicatedServer.appendCode = function (code) {
ModAPI.dedicatedServer._data.push(code);
if (ModAPI.dedicatedServer._wasUsed) {
return console.warn("The dedicated server has already launched, ModAPI.dedicatedServer.appendCode() is useless.");
}
if (typeof code === "function") {
ModAPI.dedicatedServer._data.push("(" + code.toString() + ")()");
} else if (typeof code === "string") {
ModAPI.dedicatedServer._data.push(code);
}
}
ModAPI.util ||= {};
ModAPI.util.getMethodFromPackage = function (classId, methodName) {
Expand Down Expand Up @@ -283,25 +292,21 @@
return uint16Array;
}

var stringDefaultConstructor = ModAPI.hooks._classMap["java.lang.String"].constructors.filter(x => { return x.length === 0 })[0];
ModAPI.util.string = ModAPI.util.str = function (string) {
var jclString = stringDefaultConstructor();
jclString.$characters.data = ModAPI.util.stringToUint16Array(string);
return jclString;
//Overrides $rt_resuming, $rt_suspending, $rt_currentThread. Experimental, but should be used if call stack leaks occur as a result of running internal code.
ModAPI.freezeCallstack = function () {
ModAPI.hooks.freezeCallstack = true;
}
ModAPI.unfreezeCallstack = function () {
ModAPI.hooks.freezeCallstack = false;
}

ModAPI.util.string = ModAPI.util.str = ModAPI.hooks._teavm.$rt_str;

ModAPI.util.setStringContent = function (jclString) {
ModAPI.util.setStringContent = function (jclString, string) {
jclString.$characters.data = ModAPI.util.stringToUint16Array(string);
}

ModAPI.util.jclStrToJsStr = function (jclString) {
var uint16Array = jclString.$characters.data;
let str = '';
for (let i = 0; i < uint16Array.length; i++) {
str += String.fromCharCode(uint16Array[i]);
}
return str;
}
ModAPI.util.jclStrToJsStr = ModAPI.util.unstr = ModAPI.util.unstring = ModAPI.util.ustr = ModAPI.hooks._teavm.$rt_ustr;

ModAPI.displayToChat = function (param) {
var v = typeof param === "object" ? param.msg : (param + "");
Expand Down Expand Up @@ -333,6 +338,8 @@
const integratedServerStartupMethod = ModAPI.hooks.methods[integratedServerStartup];
ModAPI.hooks.methods[integratedServerStartup] = function (worker, bootstrap) {
var x = integratedServerStartupMethod.apply(this, [worker, bootstrap + ";" + globalThis.modapi_postinit + ";" + ModAPI.dedicatedServer._data.join(";")]);
ModAPI.dedicatedServer._data = [];
ModAPI.dedicatedServer._wasUsed = true;
return x;
};

Expand All @@ -348,7 +355,10 @@
if (data.preventDefault) {
return;
}
return sendChatMessage.apply(this, [$this, ModAPI.util.str(data.message) || $message]);
if (typeof data.message === "string") {
ModAPI.util.setStringContent($message, data.message)
}
return sendChatMessage.apply(this, [$this, $message]);
}

ModAPI.events.newEvent("tick");
Expand All @@ -369,7 +379,18 @@
ModAPI.hooks.methods[serverStartMethodName] = function ($this) {
var x = serverStartMethod.apply(this, [$this]);
ModAPI.server = ModAPI.serverInstance = new Proxy($this, ModAPI.util.TeaVM_to_Recursive_BaseData_ProxyConf);
ModAPI.rawServer = $this;
ModAPI.events.callEvent("serverstart", {});
return x;
}

ModAPI.events.newEvent("serverstop");
const serverStopMethodName = ModAPI.util.getMethodFromPackage("net.minecraft.server.MinecraftServer", "stopServer");
const serverStopMethod = ModAPI.hooks.methods[serverStopMethodName];
ModAPI.hooks.methods[serverStopMethodName] = function ($this) {
var x = serverStopMethod.apply(this, [$this]);
ModAPI.server = ModAPI.serverInstance = null;
ModAPI.events.callEvent("serverstop", {});
return x;
}
})();

0 comments on commit 2e70076

Please sign in to comment.