Skip to content

Commit

Permalink
architecture docs and load event
Browse files Browse the repository at this point in the history
  • Loading branch information
ZXMushroom63 committed Sep 1, 2024
1 parent 21088fb commit 2bbef6d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
26 changes: 26 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# EaglerForge Injector Architecture
The Injector has several layers (or steps) in it's patching process. These layers can be put into two seperate categories: pre-processing and post-processing. The injector patches the EaglercraftX build with the pre-processor ahead of time, and then the post-processor is appended to the EaglercraftX build.

## Preprocessing
The preprocessor's job is to find and hook into as many internal TeaVM / Minecraft methods as possible.

The first thing it does is it adds a piece of pre-initialisation code at the start of the TeaVM module, to define global varaibles such as `ModAPI` and `ModAPI.hooks`.

Then it:

Hooks into the `$rt_metadata` function, intercepting its arguments and storing them in the array `ModAPI.hooks._rippedData`.

Hooks into the the teavm threading shim and adds controls to freeze the callstack.

Looks for the `#game_frame` element, and then adds the post-processing codes after it (`modapi_postinit`, `modapi_modloader` and `modapi_guikit`). It also stores a copy of the `modapi_postinit` script into a fourth post init script, for the post-processing scripts to inject into the dedicated server.

Searches for constructors and appends them to the `ModAPI.hooks._rippedConstructors` dictionary.

Extracts and hooks into all class methods, storing their type (`instance` or `static`) into `ModAPI.hooks._rippedMethodTypeMap`, and storing the method itself in `ModAPI.hooks.methods`

Finds static class variables, and build a proxy object for them, to allow for readwrite access even though there is no way to expose their context. The keys for the static properties of a specific class are stored under `ModAPI.hooks._rippedStaticIndexer` and the proxy objects are stored in `ModAPI.hooks._rippedStaticProperties`.

Finally, it adds references (not hooks, as they would tank performance) to all TeaVM internal methods, most importantly `$rt_str` and `$rt_ustr`, used for interaction with jcl strings.

## Postprocessing
The post processor is much simpler than the preprocessor, but it effectively uses the data in `ModAPI.hooks` to create a (mostly) backwards compatible `ModAPI` global.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ <h6>
`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.")}}};` +
`if(ModAPI.hooks.freezeCallstack){return {push: (a)=>{}, pop: ()=>{console.warn("Frozen stack was popped, context is now unstable.")}}};` +
match
);
}
Expand Down
2 changes: 2 additions & 0 deletions postinit.injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ globalThis.modapi_postinit = `(() => {
return x;
};
ModAPI.events.newEvent("load", "client");
var integratedServerStartup = ModAPI.util.getMethodFromPackage("net.lax1dude.eaglercraft.v1_8.sp.internal.ClientPlatformSingleplayer", "loadIntegratedServerSourceInline");
//Integrated server setup has a randomised suffix on the end
integratedServerStartup = ModAPI.hooks._rippedMethodKeys.filter(key => { return key.startsWith(integratedServerStartup); })[0];
Expand Down
2 changes: 2 additions & 0 deletions postinit.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@
return x;
};

ModAPI.events.newEvent("load", "client");

ModAPI.events.newEvent("sendchatmessage", "client");
const sendChatMessageMethodName = ModAPI.util.getMethodFromPackage("net.minecraft.client.entity.EntityPlayerSP", "sendChatMessage");
const sendChatMessage = ModAPI.hooks.methods[sendChatMessageMethodName];
Expand Down

0 comments on commit 2bbef6d

Please sign in to comment.