Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ jobs:
cd java; gradle -q build; cd ..
cargo build --release -p prism-agent
$env:PRISM_AGENT_DLL = "$PWD\target\release\prism_agent.dll"
$env:PRISM_CORE_JAR = "$PWD\java\core\build\libs\prism-core-0.1.0.jar"
$env:PRISM_CORE_JAR = "$PWD\java\core\build\libs\prism-core.jar"
cargo build --release -p prism-app
New-Item -ItemType Directory -Force dist | Out-Null
Copy-Item target\release\Prism.exe dist\
Copy-Item java\api\build\libs\prism-api-0.1.0.jar dist\prism-api.jar
Copy-Item java\example-plugin\build\libs\example-plugin-0.1.0.jar dist\example-plugin.jar
Copy-Item java\api\build\libs\prism-api.jar dist\prism-api.jar
Copy-Item java\example-plugin\build\libs\example-plugin.jar dist\example-plugin.jar
- uses: actions/upload-artifact@v4
with:
name: prism-windows
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "2"
members = ["native/agent", "native/inject", "native/app"]

[workspace.package]
version = "0.1.0"
version = "0.2.0"
edition = "2021"
license = "MIT"

Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
An open-source plugin platform for Minecraft 1.8.9. Prism loads into a running game
(Lunar Client first; Forge and vanilla next) and runs Java plugins inside it.

> Status: **base only**. Injection, the Java core, plugin loading with hot reload and the
> desktop app work. Features come next, one at a time.
> Status: early. Injection, the Java core, plugin loading with hot reload, the desktop app and
> game events (ticks, HUD/world rendering, keys and mouse) work. Features come next.

## Using it

Expand Down Expand Up @@ -32,11 +32,15 @@ public class ExamplePlugin implements Plugin {
@Override
public void onEnable(PluginContext ctx) {
ctx.game().chat("§dHello!");
ctx.scheduler().every(1000, () -> { /* runs on the game thread */ });
ctx.events().on(RenderOverlayEvent.class, e -> ctx.game().draw().text("§dHi", 4, 4, -1));
ctx.events().on(KeyEvent.class, e -> { if (e.key() == 30) e.cancel(); }); // block A
}
}
```

Events: `TickEvent`, `RenderOverlayEvent`, `RenderWorldEvent`, `KeyEvent`, `MouseButtonEvent`,
`PluginEnableEvent`, `PluginDisableEvent`, `ShutdownEvent`.

See [`java/example-plugin`](java/example-plugin) and [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md).

## Building
Expand Down
6 changes: 3 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ if [[ "$(uname -s)" != MINGW* && "$(uname -s)" != MSYS* ]]; then
fi
(cd java && gradle -q build)
cargo build --release "${TARGET_ARGS[@]}" -p prism-agent
PRISM_AGENT_DLL="$PWD/$OUT/prism_agent.dll" PRISM_CORE_JAR="$PWD/java/core/build/libs/prism-core-0.1.0.jar" \
PRISM_AGENT_DLL="$PWD/$OUT/prism_agent.dll" PRISM_CORE_JAR="$PWD/java/core/build/libs/prism-core.jar" \
cargo build --release "${TARGET_ARGS[@]}" -p prism-app
mkdir -p dist
cp "$OUT/Prism.exe" dist/
cp java/api/build/libs/prism-api-0.1.0.jar dist/prism-api.jar
cp java/example-plugin/build/libs/example-plugin-0.1.0.jar dist/example-plugin.jar
cp java/api/build/libs/prism-api.jar dist/prism-api.jar
cp java/example-plugin/build/libs/example-plugin.jar dist/example-plugin.jar
ls -la dist
25 changes: 22 additions & 3 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Prism.exe (Rust) the game (javaw.exe)
- **inject**: lists visible windows whose process has `jvm.dll` loaded, and loads the agent with
`VirtualAllocEx` + `WriteProcessMemory` + `CreateRemoteThread(LoadLibraryW)`.
- **agent**: `DllMain` starts a thread that attaches to the JVM as a daemon thread, gets JVMTI
(with `can_redefine_classes` for the hook system later) and waits until Minecraft is loaded.
(with `can_retransform_classes` for the hook system) and waits until Minecraft is loaded.
It finds the game's class loader from the threads' context class loaders, falling back to
JVMTI `GetLoadedClasses`, and detects the names the client uses:

Expand All @@ -32,7 +32,7 @@ Prism.exe (Rust) the game (javaw.exe)

The core jar is loaded by a `URLClassLoader` whose parent is the game's loader, so core and
plugins can use Minecraft classes. The agent registers the natives of `prism.core.Native`
(log, unload, redefine class).
(log, unload, retransform class).
- **Unload**: the app creates `bin\unload-<pid>`. The agent calls `Prism.stop()`, unregisters its
natives, closes the core's class loader, detaches and frees the DLL. The game keeps running.
- Files are named per build (`-<build>`), so a new version never needs to overwrite a DLL or jar
Expand All @@ -50,8 +50,27 @@ Prism.exe (Rust) the game (javaw.exe)
- Game state is only touched on the client thread. `Scheduler` runs work there through
`Minecraft#addScheduledTask`, found by its shape, so it works with every naming scheme.

## Hooks (`prism.core.hook`)

Prism patches a few game methods to raise its events:

| event | method (MCP / SRG) | where |
|---|---|---|
| `TickEvent` | `Minecraft.runTick` / `func_71407_l` | start |
| `RenderOverlayEvent` | `GuiIngame.renderGameOverlay` / `func_175180_a` | every return |
| `RenderWorldEvent` | `EntityRenderer.renderWorldPass` / `func_175068_a` | before `renderHand` |
| `KeyEvent`, `MouseButtonEvent` | `KeyBinding.setKeyBindState` / `func_74510_a` | start, cancelable |

- The agent retransforms a class (JVMTI `RetransformClasses`) with its ClassFileLoadHook switched on
only for that call. The hook passes the class's current bytes to a Java transformer, so Prism
patches what the game is really running (a client's own patches stay).
- The patch is ASM (bundled, moved to `prism.shadow.asm`). The added code only uses JDK classes:
`((Predicate) System.getProperties().get("prism.hook.<id>")).test(args)`, so the game's class
loader never needs to see Prism. A cancelable hook returns early when the handler says so.
- On unload every patched class is retransformed without the hook, which puts back the original.
The property is set before patching and only removed once the class is restored.

## Next steps (not built yet)

- Hooks: patch game methods (JVMTI redefine + ASM) for ticks, rendering and input events.
- Packets: a Netty handler in the game's pipeline for packet events.
- In-game UI, updates, Forge and vanilla testing.
1 change: 1 addition & 0 deletions java/api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// prism-api.jar: what plugins compile against.
base { archivesName = 'prism-api' }
java { withSourcesJar() }
tasks.withType(Jar).configureEach { archiveVersion = '' }
15 changes: 15 additions & 0 deletions java/api/src/main/java/prism/api/Draw.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package prism.api;

/** Simple 2D drawing. Only call it inside a RenderOverlayEvent handler. Colors are 0xAARRGGBB. */
public interface Draw {
/** Draws text with a shadow; supports § colour codes. Returns the text width. */
int text(String text, float x, float y, int color);

int textWidth(String text);

/** Height of one line of text. */
int fontHeight();

/** Filled rectangle from (x1, y1) to (x2, y2). */
void rect(int x1, int y1, int x2, int y2, int color);
}
3 changes: 3 additions & 0 deletions java/api/src/main/java/prism/api/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ public interface Game {
* Supports {@code §} colour codes. Safe to call from any thread.
*/
void chat(String message);

/** 2D drawing for RenderOverlayEvent handlers. */
Draw draw();
}
10 changes: 10 additions & 0 deletions java/api/src/main/java/prism/api/event/Cancellable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package prism.api.event;

/** An event a handler can cancel, stopping what the game was about to do. */
public abstract class Cancellable {
private boolean cancelled;

public void cancel() { cancelled = true; }

public boolean isCancelled() { return cancelled; }
}
18 changes: 18 additions & 0 deletions java/api/src/main/java/prism/api/event/KeyEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package prism.api.event;

/**
* A key went down or up while playing (not while a menu or chat is open).
* Key codes are LWJGL codes (e.g. 30 = A). Cancel to hide it from the game's keybinds.
*/
public final class KeyEvent extends Cancellable {
private final int key;
private final boolean pressed;

public KeyEvent(int key, boolean pressed) {
this.key = key;
this.pressed = pressed;
}

public int key() { return key; }
public boolean pressed() { return pressed; }
}
18 changes: 18 additions & 0 deletions java/api/src/main/java/prism/api/event/MouseButtonEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package prism.api.event;

/**
* A mouse button went down or up while playing (0 = left, 1 = right, 2 = middle).
* Cancel to hide it from the game's keybinds (e.g. stop an attack or use).
*/
public final class MouseButtonEvent extends Cancellable {
private final int button;
private final boolean pressed;

public MouseButtonEvent(int button, boolean pressed) {
this.button = button;
this.pressed = pressed;
}

public int button() { return button; }
public boolean pressed() { return pressed; }
}
20 changes: 20 additions & 0 deletions java/api/src/main/java/prism/api/event/RenderOverlayEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package prism.api.event;

/**
* After the game drew its HUD, every frame. Draw with {@code ctx.game().draw()}.
* Width and height are in GUI-scaled pixels.
*/
public final class RenderOverlayEvent {
private final float partialTicks;
private final int width, height;

public RenderOverlayEvent(float partialTicks, int width, int height) {
this.partialTicks = partialTicks;
this.width = width;
this.height = height;
}

public float partialTicks() { return partialTicks; }
public int width() { return width; }
public int height() { return height; }
}
10 changes: 10 additions & 0 deletions java/api/src/main/java/prism/api/event/RenderWorldEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package prism.api.event;

/** After the world was drawn, before the player's hand, every frame (camera-space GL state). */
public final class RenderWorldEvent {
private final float partialTicks;

public RenderWorldEvent(float partialTicks) { this.partialTicks = partialTicks; }

public float partialTicks() { return partialTicks; }
}
8 changes: 8 additions & 0 deletions java/api/src/main/java/prism/api/event/TickEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package prism.api.event;

/** Start of every client tick (20 per second), on the client thread. */
public final class TickEvent {
public static final TickEvent INSTANCE = new TickEvent();

private TickEvent() {}
}
2 changes: 1 addition & 1 deletion java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
subprojects {
apply plugin: 'java'
group = 'prism'
version = '0.1.0'
version = '0.2.0'
repositories { mavenCentral() }
tasks.withType(JavaCompile).configureEach {
options.release = 8
Expand Down
19 changes: 14 additions & 5 deletions java/core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
// prism-core.jar: loaded into the game by the agent. Contains the API classes too.
// prism-core.jar: loaded into the game by the agent. Contains the API classes and a private
// copy of ASM (moved to prism.shadow.asm so it never clashes with a client's own ASM).
plugins { id 'com.gradleup.shadow' version '8.3.5' }
base { archivesName = 'prism-core' }
dependencies { implementation project(':api') }
jar {
dependsOn ':api:jar'
from { project(':api').sourceSets.main.output }
dependencies {
implementation project(':api')
implementation 'org.ow2.asm:asm-tree:9.7.1'
}
jar { enabled = false }
shadowJar {
archiveClassifier = ''
archiveVersion = ''
relocate 'org.objectweb.asm', 'prism.shadow.asm'
exclude 'module-info.class', 'META-INF/versions/**', 'META-INF/maven/**'
}
assemble.dependsOn shadowJar
106 changes: 106 additions & 0 deletions java/core/src/main/java/prism/core/DrawImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package prism.core;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import prism.api.Draw;
import prism.api.Logger;

/** Draw through the game's FontRenderer and Gui.drawRect, found by MCP or SRG names. */
final class DrawImpl implements Draw {
private final String mappings;
private final ClassLoader loader;
private final Logger log = CoreLog.forSource("draw");

private Field fontField, fontHeight;
private Method drawText, textWidth, drawRect, scaledW, scaledH;
private Constructor<?> scaled;
private boolean resolved, broken;

DrawImpl(String mappings, ClassLoader loader) {
this.mappings = mappings;
this.loader = loader;
}

private boolean srg() { return "srg".equals(mappings); }

private synchronized boolean resolve() {
if (resolved || broken) return resolved;
try {
if ("notch".equals(mappings)) throw new UnsupportedOperationException("vanilla names not supported yet");
Class<?> mc = ClientThread.minecraftClass();
fontField = mc.getDeclaredField(srg() ? "field_71466_p" : "fontRendererObj");
fontField.setAccessible(true);
Class<?> fr = fontField.getType();
drawText = fr.getMethod(srg() ? "func_175063_a" : "drawStringWithShadow", String.class, float.class, float.class, int.class);
textWidth = fr.getMethod(srg() ? "func_78256_a" : "getStringWidth", String.class);
fontHeight = fr.getField(srg() ? "field_78288_b" : "FONT_HEIGHT");
Class<?> gui = Class.forName("net.minecraft.client.gui.Gui", false, loader);
drawRect = gui.getMethod(srg() ? "func_73734_a" : "drawRect", int.class, int.class, int.class, int.class, int.class);
Class<?> sr = Class.forName("net.minecraft.client.gui.ScaledResolution", false, loader);
scaled = sr.getConstructor(mc);
scaledW = sr.getMethod(srg() ? "func_78326_a" : "getScaledWidth");
scaledH = sr.getMethod(srg() ? "func_78328_b" : "getScaledHeight");
resolved = true;
} catch (Throwable t) {
broken = true;
log.error("drawing not available", t);
}
return resolved;
}

private Object font() throws IllegalAccessException {
return fontField.get(ClientThread.minecraft());
}

@Override
public int text(String text, float x, float y, int color) {
if (!resolve()) return 0;
try {
return (Integer) drawText.invoke(font(), text, x, y, color);
} catch (Exception e) {
return 0;
}
}

@Override
public int textWidth(String text) {
if (!resolve()) return 0;
try {
return (Integer) textWidth.invoke(font(), text);
} catch (Exception e) {
return 0;
}
}

@Override
public int fontHeight() {
if (!resolve()) return 9;
try {
return fontHeight.getInt(font());
} catch (Exception e) {
return 9;
}
}

@Override
public void rect(int x1, int y1, int x2, int y2, int color) {
if (!resolve()) return;
try {
drawRect.invoke(null, x1, y1, x2, y2, color);
} catch (Exception ignored) {
}
}

/** GUI-scaled screen size {width, height}. */
int[] screenSize() {
if (!resolve()) return new int[] {0, 0};
try {
Object sr = scaled.newInstance(ClientThread.minecraft());
return new int[] {(Integer) scaledW.invoke(sr), (Integer) scaledH.invoke(sr)};
} catch (Exception e) {
return new int[] {0, 0};
}
}
}
Loading
Loading