diff --git a/postinit.js b/postinit.js index e93081c..30c4412 100644 --- a/postinit.js +++ b/postinit.js @@ -2,6 +2,9 @@ globalThis.modapi_postinit = "(" + (() => { //EaglerForge post initialization code. //This script cannot contain backticks, escape characters, or backslashes in order to inject into the dedicated server code. var startedModLoader = false; + var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; + var ARGUMENT_NAMES = /([^\s,]+)/g; + function startModLoader() { if (!startedModLoader) { @@ -34,6 +37,32 @@ globalThis.modapi_postinit = "(" + (() => { return x; } } + function arraysAreSame(arr1, arr2) { + if (!arr1 || !arr2) + return false; + if(arr1 === arr2) + return true; + if (arr1.length !== arr2.length) + return false; + + for (var i = 0, l=arr1.length; i < l; i++) { + if (arr1[i] instanceof Array && arr2[i] instanceof Array) { + if (!arr1[i].equals(arr2[i])) + return false; + } + else if (arr1[i] !== arr2[i]) { + return false; + } + } + return true; + } + function getParamNames(func) { + var fnStr = func.toString().replace(STRIP_COMMENTS, ''); + var result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(ARGUMENT_NAMES); + if (result === null) + result = []; + return result; + } function getEaglerConfigFlag(key) { var searchParams = new URLSearchParams(location.search); return (globalThis.eaglercraftXOpts?.[key] || searchParams.get(key)) ? true : false @@ -41,7 +70,7 @@ globalThis.modapi_postinit = "(" + (() => { function easyStaticMethod(classId, methodName, autoUnpack) { var method = ModAPI.reflect.getClassById(classId).staticMethods[methodName].method; return function easyImpl(...args) { - return method(...(autoUnpack ? args.map(x=>{ + return method(...(autoUnpack ? args.map(x => { if ((typeof x === "object") && (x.isModProxy === true)) { return x.getRef(); } @@ -277,6 +306,7 @@ globalThis.modapi_postinit = "(" + (() => { var classId = item?.$meta?.name || null; if (!ModAPI.hooks._classMap[compiledName]) { + var argumentCache = null; ModAPI.hooks._classMap[compiledName] = { "name": compiledName.split("_")[1], "id": classId, @@ -296,7 +326,21 @@ globalThis.modapi_postinit = "(" + (() => { return false; } }, - "compiledName": compiledName + "compiledName": compiledName, + "getConstructorByArgs": function (...argNames) { + if (!argumentCache) { + argumentCache = []; + this.internalConstructors.forEach(x=>{ + argumentCache.push(getParamNames(x).slice(1).map(y => y.substring(1))); + }); + } + for (let i = 0; i < argumentCache.length; i++) { + const args = argumentCache[i]; + if (arraysAreSame(args, argNames)) { + return this.constructors[i]; + } + } + } } } if (typeof item?.$meta?.superclass === "function" && item?.$meta?.superclass?.$meta) { @@ -346,8 +390,8 @@ globalThis.modapi_postinit = "(" + (() => { } } }); - ModAPI.hooks._classMap[compiledName].staticVariables = makeClinitProxy(ModAPI.hooks._rippedStaticProperties[compiledName] || {}, (()=>{ - (ModAPI.hooks._rippedStaticProperties[compiledName].$callClinit ?? (()=>{}))(); + ModAPI.hooks._classMap[compiledName].staticVariables = makeClinitProxy(ModAPI.hooks._rippedStaticProperties[compiledName] || {}, (() => { + (ModAPI.hooks._rippedStaticProperties[compiledName].$callClinit ?? (() => { }))(); })); ModAPI.hooks._classMap[compiledName].staticVariableNames = Object.keys(ModAPI.hooks._classMap[compiledName].staticVariables); }); @@ -406,7 +450,7 @@ globalThis.modapi_postinit = "(" + (() => { } const TeaVM_to_Recursive_BaseData_ProxyConf = { ownKeys(target) { - return Reflect.ownKeys(target).flatMap(x => x.substring(1)); + return Reflect.ownKeys(target).map(x => x.substring(1)); }, getOwnPropertyDescriptor(target, prop) { return Object.getOwnPropertyDescriptor(target, "$" + prop);