bump version + auto clinit

This commit is contained in:
ZXMushroom63 2024-12-24 17:42:00 +08:00
parent cdd427ed20
commit 3677335b10
2 changed files with 20 additions and 8 deletions

View File

@ -59,6 +59,8 @@ Each `ReflectClass` has the following methods:
- `instanceOf(object: Object)` - `instanceOf(object: Object)`
- Checks if the `object` is an instance of the class. - Checks if the `object` is an instance of the class.
- `init()`
- Initializes static variables. Not needed, as static variables already prepare themselves before being accessed, this is mostly used for debugging.
### ReflectMethod Definition ### ReflectMethod Definition

View File

@ -1,4 +1,4 @@
globalThis.ModAPIVersion = "v2.3.3"; globalThis.ModAPIVersion = "v2.3.4";
globalThis.modapi_postinit = "(" + (() => { globalThis.modapi_postinit = "(" + (() => {
//EaglerForge post initialization code. //EaglerForge post initialization code.
//This script cannot contain backticks, escape characters, or backslashes in order to inject into the dedicated server code. //This script cannot contain backticks, escape characters, or backslashes in order to inject into the dedicated server code.
@ -208,6 +208,16 @@ globalThis.modapi_postinit = "(" + (() => {
return ModAPI.hooks._teavm.$rt_createDoubleArray(size); return ModAPI.hooks._teavm.$rt_createDoubleArray(size);
} }
//Proxy to make sure static variables are initialized before access.
function makeClinitProxy(staticVariables, clinit) {
return new Proxy(staticVariables, {
get: function (a, b, c) {
clinit();
return Reflect.get(a, b, c);
}
});
}
ModAPI.hooks.regenerateClassMap = function () { ModAPI.hooks.regenerateClassMap = function () {
ModAPI.hooks._rippedConstructorKeys = Object.keys(ModAPI.hooks._rippedConstructors); ModAPI.hooks._rippedConstructorKeys = Object.keys(ModAPI.hooks._rippedConstructors);
ModAPI.hooks._rippedInternalConstructorKeys = Object.keys(ModAPI.hooks._rippedInternalConstructors); ModAPI.hooks._rippedInternalConstructorKeys = Object.keys(ModAPI.hooks._rippedInternalConstructors);
@ -282,9 +292,6 @@ globalThis.modapi_postinit = "(" + (() => {
ModAPI.hooks._classMap[compiledName].superclassName = null; ModAPI.hooks._classMap[compiledName].superclassName = null;
} }
ModAPI.hooks._classMap[compiledName].staticVariables = ModAPI.hooks._rippedStaticProperties[compiledName];
ModAPI.hooks._classMap[compiledName].staticVariableNames = Object.keys(ModAPI.hooks._classMap[compiledName].staticVariables || {});
if (item?.["$$constructor$$"]) { if (item?.["$$constructor$$"]) {
//Class does not have any hand written constructors //Class does not have any hand written constructors
//Eg: class MyClass {} //Eg: class MyClass {}
@ -324,6 +331,9 @@ globalThis.modapi_postinit = "(" + (() => {
} }
} }
}); });
ModAPI.hooks._classMap[compiledName].init = ModAPI.hooks._classMap[compiledName].staticMethods?.$callClinit?.method || (()=>{});
ModAPI.hooks._classMap[compiledName].staticVariables = makeClinitProxy(ModAPI.hooks._rippedStaticProperties[compiledName] || {}, ModAPI.hooks._classMap[compiledName].init);
ModAPI.hooks._classMap[compiledName].staticVariableNames = Object.keys(ModAPI.hooks._classMap[compiledName].staticVariables);
}); });
ModAPI.reflect.classes = Object.values(ModAPI.hooks._classMap); ModAPI.reflect.classes = Object.values(ModAPI.hooks._classMap);
console.log("[ModAPI] Regenerated hook classmap."); console.log("[ModAPI] Regenerated hook classmap.");
@ -340,7 +350,7 @@ globalThis.modapi_postinit = "(" + (() => {
//Magical function for making a subclass with a custom constructor that you can easily use super(...) on. //Magical function for making a subclass with a custom constructor that you can easily use super(...) on.
ModAPI.reflect.getSuper = function getSuper(reflectClass, filter) { ModAPI.reflect.getSuper = function getSuper(reflectClass, filter) {
filter ||= ()=>true; filter ||= () => true;
var initialiser = reflectClass.internalConstructors.find(filter); var initialiser = reflectClass.internalConstructors.find(filter);
return function superFunction(thisArg, ...extra_args) { return function superFunction(thisArg, ...extra_args) {
reflectClass.class.call(thisArg); reflectClass.class.call(thisArg);