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 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.2.1"
version = "0.3.0"
edition = "2021"
license = "MIT"

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

> 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.
> game events (ticks, HUD/world rendering, keys and mouse) and packets work. Features come next.

## Using it

Expand Down Expand Up @@ -39,8 +39,12 @@ public class ExamplePlugin implements Plugin {
```

Events: `TickEvent`, `RenderOverlayEvent`, `RenderWorldEvent`, `KeyEvent`, `MouseButtonEvent`,
`PacketReceiveEvent`, `PacketSendEvent`, `ConnectEvent`, `DisconnectEvent`,
`PluginEnableEvent`, `PluginDisableEvent`, `ShutdownEvent`.

Packets: cancel or `replace()` them in the packet events, or use `ctx.network().send(packet)` /
`receive(packet)` with the game's own packet classes.

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

## Building
Expand Down
14 changes: 13 additions & 1 deletion docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Prism patches a few game methods to raise its events:
| `TickEvent` | `Minecraft.runTick` / `func_71407_l` | start |
| `RenderOverlayEvent` | `GuiIngame.renderGameOverlay` / `func_175180_a` | every return |
| `RenderWorldEvent` | `EntityRenderer.renderWorldPass` / `func_175068_a` | before `renderHand` |
| (packets) | `NetworkManager.channelActive` | start: adds the packet handler |
| `KeyEvent`, `MouseButtonEvent` | `KeyBinding.setKeyBindState` / `func_74510_a` | start, cancelable |

- The agent retransforms a class (JVMTI `RetransformClasses`) with its ClassFileLoadHook switched on
Expand All @@ -70,7 +71,18 @@ Prism patches a few game methods to raise its events:
- 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.

## Packets (`prism.core.net`)

- A Netty handler named `prism_packets` goes into each client connection's pipeline, right before
Minecraft's `packet_handler`, where packets are decoded objects both ways.
- New connections are caught by the `NetworkManager.channelActive` hook; the connection already
open when Prism starts is found through `Minecraft → NetHandlerPlayClient → NetworkManager`.
Singleplayer's server-side NetworkManager (direction SERVERBOUND) is skipped.
- `PacketReceiveEvent` / `PacketSendEvent` can be cancelled or replaced. `Network.send()` writes from
the handler's own context (straight to the encoder) and `receive()` fires from it (straight to the
game), so neither raises an event. Events run on the Netty thread.
- On unload the handler is removed from every pipeline before the core's class loader closes.

## Next steps (not built yet)

- Packets: a Netty handler in the game's pipeline for packet events.
- In-game UI, updates, Forge and vanilla testing.
19 changes: 19 additions & 0 deletions java/api/src/main/java/prism/api/Network.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package prism.api;

/**
* The connection to the server. Packets are the game's own packet objects
* (e.g. {@code net.minecraft.network.play.client.C01PacketChatMessage}); build them with the
* game's class loader ({@link Game#classLoader()}).
*/
public interface Network {
boolean connected();

/** "host:port" of the current server, or null. */
String serverAddress();

/** Sends a packet to the server. Plugins don't see it as a PacketSendEvent. */
void send(Object packet);

/** Hands a packet to the game as if the server sent it. Plugins don't see it as a PacketReceiveEvent. */
void receive(Object packet);
}
3 changes: 3 additions & 0 deletions java/api/src/main/java/prism/api/PluginContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public interface PluginContext {
/** The game Prism is injected into. */
Game game();

/** The connection to the server: send and receive packets. */
Network network();

/** A folder for this plugin's own files (created on first call). */
Path dataFolder();
}
10 changes: 10 additions & 0 deletions java/api/src/main/java/prism/api/event/ConnectEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package prism.api.event;

/** The game opened a connection to a server ("host:port"). Network thread. */
public final class ConnectEvent {
private final String address;

public ConnectEvent(String address) { this.address = address; }

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

/** The connection to the server closed. Network thread. */
public final class DisconnectEvent {
private final String address;

public DisconnectEvent(String address) { this.address = address; }

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

/** Base of the packet events. Runs on the network thread: use the scheduler for game state. */
public abstract class PacketEvent extends Cancellable {
private Object packet;

protected PacketEvent(Object packet) { this.packet = packet; }

/** The game's packet object. */
public Object packet() { return packet; }

/** Class name without the package, e.g. "S02PacketChat" (Lunar and Forge use the same names). */
public String name() { return packet.getClass().getSimpleName(); }

/** Replaces the packet with another one (of any type). */
public void replace(Object packet) {
if (packet == null) throw new IllegalArgumentException("use cancel() to drop a packet");
this.packet = packet;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package prism.api.event;

/** A packet from the server, before the game handles it. Cancel to hide it from the game. */
public final class PacketReceiveEvent extends PacketEvent {
public PacketReceiveEvent(Object packet) { super(packet); }
}
6 changes: 6 additions & 0 deletions java/api/src/main/java/prism/api/event/PacketSendEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package prism.api.event;

/** A packet the game is sending to the server. Cancel to stop it. */
public final class PacketSendEvent extends PacketEvent {
public PacketSendEvent(Object packet) { super(packet); }
}
2 changes: 2 additions & 0 deletions java/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ base { archivesName = 'prism-core' }
dependencies {
implementation project(':api')
implementation 'org.ow2.asm:asm-tree:9.7.1'
// the game's own Netty (Minecraft 1.8.9 ships 4.0.23); not bundled
compileOnly 'io.netty:netty-all:4.0.23.Final'
}
jar { enabled = false }
shadowJar {
Expand Down
6 changes: 5 additions & 1 deletion java/core/src/main/java/prism/core/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import prism.api.EventBus;
import prism.api.Game;
import prism.api.Logger;
import prism.api.Network;
import prism.api.Plugin;
import prism.api.PluginContext;
import prism.api.PluginInfo;
Expand All @@ -39,6 +40,7 @@ final class PluginManager {
private final ClassLoader parent;
private final Bus bus;
private final Game game;
private final Network network;
private final Logger log = CoreLog.forSource("plugins");

/** file name -> loaded plugin */
Expand All @@ -50,7 +52,8 @@ final class PluginManager {
private int cacheCounter;
private boolean firstScan = true;

PluginManager(Path home, ClassLoader parent, Bus bus, Game game) {
PluginManager(Path home, ClassLoader parent, Bus bus, Game game, Network network) {
this.network = network;
this.pluginsDir = home.resolve("plugins");
this.cacheDir = home.resolve("cache").resolve("plugins");
this.dataDir = home.resolve("data");
Expand Down Expand Up @@ -270,6 +273,7 @@ private final class Context implements PluginContext {
@Override public EventBus events() { return events; }
@Override public Scheduler scheduler() { return tasks; }
@Override public Game game() { return game; }
@Override public Network network() { return network; }

@Override
public Path dataFolder() {
Expand Down
19 changes: 16 additions & 3 deletions java/core/src/main/java/prism/core/Prism.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
import prism.api.event.ShutdownEvent;
import prism.core.hook.GameHooks;
import prism.core.hook.Hooks;
import prism.core.net.Connections;

/**
* Started by the native agent once the game's classes are loaded.
* Layout of the Prism folder (home): bin/ (agent + core), plugins/, data/, logs/, cache/.
*/
public final class Prism {
public static final String VERSION = "0.2.1";
public static final String VERSION = "0.3.0";

private static final AtomicBoolean running = new AtomicBoolean();
private static Bus bus;
private static PluginManager plugins;
private static Hooks hooks;
private static Connections connections;
private static Thread shutdownHook;

private Prism() {}
Expand Down Expand Up @@ -47,11 +49,16 @@ public static String start(String home, String mappings, Class<?> minecraft) {
GameImpl game = new GameImpl(mappings, minecraft.getClassLoader());
hooks = new Hooks(minecraft.getClassLoader(), mappings, CoreLog.forSource("hooks"));
Bus b = bus;
connections = new Connections(new Connections.Poster() {
public <E> E post(E event) { return b.post(event); }
}, CoreLog.forSource("network"));
Connections conns = connections;
GameHooks.register(hooks, new GameHooks.Poster() {
public <E> E post(E event) { return b.post(event); }
}, pt -> game.drawImpl().screenSize());
}, pt -> game.drawImpl().screenSize(), conns::onChannelActive);
log.info("hooks placed: " + hooks.install());
plugins = new PluginManager(dir, Prism.class.getClassLoader(), bus, game);
conns.attachCurrent(ClientThread.minecraft());
plugins = new PluginManager(dir, Prism.class.getClassLoader(), bus, game, connections);
plugins.start();
shutdownHook = new Thread(Prism::stop, "Prism Shutdown");
Runtime.getRuntime().addShutdownHook(shutdownHook);
Expand All @@ -75,6 +82,12 @@ public static void stop() {
} catch (Throwable t) {
log.error("stop failed", t);
}
try {
if (connections != null) connections.stop();
} catch (Throwable t) {
log.error("removing packet handlers failed", t);
}
connections = null;
try {
if (hooks != null) hooks.uninstall();
} catch (Throwable t) {
Expand Down
10 changes: 9 additions & 1 deletion java/core/src/main/java/prism/core/hook/GameHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ public interface Poster {
<E> E post(E event);
}

public static void register(Hooks hooks, Poster bus, Function<Float, int[]> screenSize) {
public static void register(Hooks hooks, Poster bus, Function<Float, int[]> screenSize,
java.util.function.BiConsumer<Object, Object> channelActive) {
// every new connection: NetworkManager.channelActive (a Netty method, same name everywhere)
hooks.add(Hook.head("connect", "net/minecraft/network/NetworkManager", "channelActive", "channelActive",
"(Lio/netty/channel/ChannelHandlerContext;)V", new int[] {0, 1}, false, args -> {
channelActive.accept(args[0], args[1]);
return false;
}));

hooks.add(Hook.head("tick", "net/minecraft/client/Minecraft", "runTick", "func_71407_l", "()V",
new int[0], false, args -> {
bus.post(TickEvent.INSTANCE);
Expand Down
Loading
Loading