Skip to content

Commit

Permalink
Protocol updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Mar 27, 2024
1 parent 706af6c commit 7b94e9b
Show file tree
Hide file tree
Showing 28 changed files with 349 additions and 26 deletions.
8 changes: 7 additions & 1 deletion src/main/java/dev/hilligans/ourcraft/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import dev.hilligans.ourcraft.mod.handler.events.client.OpenScreenEvent;
import dev.hilligans.ourcraft.network.ClientNetwork;
import dev.hilligans.ourcraft.network.IClientPacketHandler;
import dev.hilligans.ourcraft.network.Network;
import dev.hilligans.ourcraft.network.PacketBase;
import dev.hilligans.ourcraft.network.packet.client.CCloseScreen;
import dev.hilligans.ourcraft.network.packet.client.CDropItem;
Expand Down Expand Up @@ -178,7 +179,7 @@ public void tick(ThreadContext threadContext) {
}
if(transition) {
Thread.startVirtualThread(() -> {
network = new ClientNetwork(gameInstance.PROTOCOLS.get("ourcraft:Play")).debug(argumentContainer.getBoolean("--packetTrace", false));
network = new ClientNetwork(gameInstance, gameInstance.PROTOCOLS.get("ourcraft:Play")).debug(argumentContainer.getBoolean("--packetTrace", false));
});
client.gameInstance.build(client.graphicsEngine, null);
transition = false;
Expand Down Expand Up @@ -441,4 +442,9 @@ public IWorld getWorld() {
public GameInstance getGameInstance() {
return gameInstance;
}

@Override
public Network getNetwork() {
return network;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.hilligans.ourcraft.entity.living.entities.PlayerEntity;
import dev.hilligans.ourcraft.item.ItemStack;
import dev.hilligans.ourcraft.network.IServerPacketHandler;
import dev.hilligans.ourcraft.network.Network;
import dev.hilligans.ourcraft.network.ServerNetworkHandler;
import dev.hilligans.ourcraft.save.WorldLoader;
import dev.hilligans.ourcraft.server.IServer;
Expand Down Expand Up @@ -236,4 +237,9 @@ public ServerNetworkHandler getServerNetworkHandler() {
public void disconnect(String reason) {

}

@Override
public Network getNetwork() {
return serverNetworkHandler.network;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import dev.hilligans.ourcraft.mod.handler.content.CoreExtensionView;
import dev.hilligans.ourcraft.mod.handler.content.ModContainer;
import dev.hilligans.ourcraft.mod.handler.content.RegistryView;
import dev.hilligans.ourcraft.mod.handler.pipeline.InstanceLoaderPipeline;

public abstract class ModClass {

public abstract String getModID();

public void registerHooks(InstanceLoaderPipeline<?> pipeline) {
}

public void registerRegistries(RegistryView view) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public static InstanceLoaderPipeline get(GameInstance gameInstance) {
//load all mods
pipeline.addStage("Locate Mods", (pipeline1, s) -> pipeline1.setModList(gameInstance.MOD_LIST.load()));

//register hooks
pipeline.addStage("Register Hooks", ((pipeline1, section1) -> pipeline1.getModList().foreach(modContainer -> modContainer.modClass.registerHooks(pipeline))));

//load all the registries defined in each mod
pipeline.addStage("Register Registries", (pipeline16, section) -> {
pipeline16.getModList().foreach(container -> container.modClass.registerRegistries(registryView));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public class ClientNetwork extends Network {
public GameInstance gameInstance;
public ArrayList<PacketBase<?>> packets = new ArrayList<>();

public ClientNetwork(Protocol protocol) {
super(protocol);
public ClientNetwork(GameInstance gameInstance, Protocol protocol) {
super(gameInstance, protocol);
}

public ClientNetwork(Protocol sendProtocol, Protocol receiveProtocol, int packetIdWidth) {
super(sendProtocol, receiveProtocol, packetIdWidth, false);
public ClientNetwork(GameInstance gameInstance, Protocol sendProtocol, Protocol receiveProtocol, int packetIdWidth) {
super(gameInstance, sendProtocol, receiveProtocol, packetIdWidth, false);
}

public void joinServer(String ip, String port, Client client) throws Exception {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/network/IPacketHandler.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
package dev.hilligans.ourcraft.network;

import dev.hilligans.ourcraft.network.debug.PacketTraceByteArray;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;

public interface IPacketHandler {

default ChannelFuture sendPacket(PacketBase<?> packetBase, ChannelHandlerContext ctx) {
packetBase.packetId = getNetwork().sendProtocol.packetMap.get(packetBase.getClass());
return ctx.channel().writeAndFlush(new PacketByteArray(packetBase));
}

Network getNetwork();
}
22 changes: 16 additions & 6 deletions src/main/java/dev/hilligans/ourcraft/network/Network.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hilligans.ourcraft.network;

import dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.network.debug.PacketTrace;
import dev.hilligans.ourcraft.network.debug.PacketTracePacketDecoder;
import dev.hilligans.ourcraft.network.debug.PacketTracePacketEncoder;
Expand All @@ -18,23 +19,32 @@ public class Network extends ChannelInitializer<SocketChannel> {
public int packetIdWidth;
public boolean compressed;
public boolean debug = false;
public GameInstance gameInstance;


public Network(Protocol protocol) {
this(protocol,protocol,2);
public Network(GameInstance gameInstance, Protocol protocol) {
this(gameInstance, protocol,protocol,2);
}

public Network(Protocol sendProtocol, Protocol receiveProtocol, int packetIdWidth) {
this(sendProtocol,receiveProtocol,packetIdWidth,false);
public Network(GameInstance gameInstance, Protocol sendProtocol, Protocol receiveProtocol, int packetIdWidth) {
this(gameInstance, sendProtocol,receiveProtocol,packetIdWidth,false);
}

public Network(Protocol sendProtocol, Protocol receiveProtocol, int packetIdWidth, boolean compressed) {
public Network(GameInstance gameInstance, Protocol sendProtocol, Protocol receiveProtocol, int packetIdWidth, boolean compressed) {
this.gameInstance = gameInstance;
this.sendProtocol = sendProtocol;
this.receiveProtocol = receiveProtocol;
this.packetIdWidth = packetIdWidth;
this.compressed = compressed;
}

public void setSendProtocol(String name) {
sendProtocol = gameInstance.PROTOCOLS.getExcept(name);
}

public void setReceiveProtocol(String name) {
receiveProtocol = gameInstance.PROTOCOLS.getExcept(name);
}

@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/network/PacketByteArray.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hilligans.ourcraft.network;

import dev.hilligans.ourcraft.util.IByteArray;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
Expand Down Expand Up @@ -41,6 +42,15 @@ public PacketByteArray(byte[] bytes, int packetWidth) {
index = bytes.length - packetWidth;
}

public PacketByteArray(byte[] bytes) {
this.byteBuf = Unpooled.buffer();
this.byteBuf.writeBytes(bytes);

this.packetID = readVarInt();

this.index = bytes.length - IByteArray.varIntLength(packetID);
}

public PacketByteArray() {
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/dev/hilligans/ourcraft/network/ServerNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class ServerNetwork extends Network {
public GameInstance gameInstance;
public IServer server;

EventLoopGroup bossGroup;
EventLoopGroup workerGroup;
ChannelFuture channelFuture;
public EventLoopGroup bossGroup;
public EventLoopGroup workerGroup;
public ChannelFuture channelFuture;

public ServerNetwork(Protocol protocol, IServer server) {
super(protocol);
public ServerNetwork(GameInstance gameInstance, Protocol protocol, IServer server) {
super(gameInstance, protocol);
this.server = server;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.HashMap;

@ChannelHandler.Sharable
public class ServerNetworkHandler extends SimpleChannelInboundHandler<IPacketByteArray> implements IServerPacketHandler {

final ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
Expand All @@ -28,6 +29,7 @@ public class ServerNetworkHandler extends SimpleChannelInboundHandler<IPacketByt

public ServerNetwork network;
public static boolean debug = false;
public boolean ssl = true;

public IServer server;

Expand All @@ -38,15 +40,17 @@ public ServerNetworkHandler(ServerNetwork network, IServer server) {

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.pipeline().get(SslHandler.class).handshakeFuture().addListener(
new GenericFutureListener<Future<Channel>>() {
@Override
public void operationComplete(Future<Channel> future) throws Exception {
// ctx.writeAndFlush(new PacketData(21));
channels.add(ctx.channel());
channelIds.add(ctx.channel().id());
}
});
if(ssl) {
ctx.pipeline().get(SslHandler.class).handshakeFuture().addListener(
new GenericFutureListener<Future<Channel>>() {
@Override
public void operationComplete(Future<Channel> future) throws Exception {
// ctx.writeAndFlush(new PacketData(21));
channels.add(ctx.channel());
channelIds.add(ctx.channel().id());
}
});
}
super.channelActive(ctx);
}

Expand Down Expand Up @@ -115,6 +119,11 @@ public ChannelFuture sendPacket(PacketBase<?> packetBase, ChannelHandlerContext
return sendPacket(packetBase, ctx.channel());
}

@Override
public Network getNetwork() {
return network;
}

public static ChannelFuture sendPacket1(PacketBase<?> packetBase, ChannelHandlerContext ctx) {
return sendPacket(packetBase, ctx.channel());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void startServer(String port) {
playerHandler.scheduleAtFixedRate(new PlayerHandler(this), 0, 10, TimeUnit.MILLISECONDS);
// ConsoleReader consoleReader = new ConsoleReader(this::executeCommand);

serverNetwork = new ServerNetwork(gameInstance.PROTOCOLS.get("ourcraft:Play"), this).debug(Ourcraft.getArgumentContainer().getBoolean("--packetTrace", false));
serverNetwork = new ServerNetwork(gameInstance, gameInstance.PROTOCOLS.get("ourcraft:Play"), this).debug(Ourcraft.getArgumentContainer().getBoolean("--packetTrace", false));
try {
serverNetwork.startServer(port);
} catch (Exception e) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/tag/ByteArrayNBTTag.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.hilligans.ourcraft.tag;

import dev.hilligans.ourcraft.util.IByteArray;

import java.nio.ByteBuffer;
import java.util.Arrays;

Expand Down Expand Up @@ -40,6 +42,23 @@ public void write(ByteBuffer byteBuf) {
}
}

@Override
public void read(IByteArray byteArray) {
int length = byteArray.readInt();
val = new byte[length];
for(int x = 0; x < length; x++) {
val[x] = byteArray.readByte();
}
}

@Override
public void write(IByteArray byteArray) {
byteArray.writeInt(val.length);
for (byte b : val) {
byteArray.writeByte(b);
}
}

@Override
public NBTTag duplicate() {
byte[] newBytes = new byte[val.length];
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/tag/ByteNBTTag.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.hilligans.ourcraft.tag;

import dev.hilligans.ourcraft.util.IByteArray;

import java.nio.ByteBuffer;

public class ByteNBTTag extends NBTTag {
Expand Down Expand Up @@ -32,6 +34,16 @@ public void write(ByteBuffer byteBuf) {
byteBuf.put(val);
}

@Override
public void read(IByteArray byteArray) {
val = byteArray.readByte();
}

@Override
public void write(IByteArray byteArray) {
byteArray.writeByte(val);
}

@Override
public NBTTag duplicate() {
return new ByteNBTTag(val);
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/tag/CompoundNBTTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import dev.hilligans.ourcraft.Ourcraft;
import dev.hilligans.ourcraft.item.ItemStack;
import dev.hilligans.ourcraft.util.IByteArray;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.HashMap;

Expand All @@ -16,6 +19,8 @@ int getSize() {
return 0;
}

public CompoundNBTTag() {}


public CompoundNBTTag putTag(String id, NBTTag NBTTag) {
tags.put(id, NBTTag);
Expand Down Expand Up @@ -174,6 +179,29 @@ public void write(ByteBuffer byteBuf) {
byteBuf.put((byte)0);
}

@Override
public void read(IByteArray byteArray) {
byte tagId;
while((tagId = byteArray.readByte()) != 0) {
String string = new String(byteArray.readBytes(byteArray.readShort()), StandardCharsets.UTF_8);
NBTTag nbtTag = NBTTag.tags.get(tagId).get();
nbtTag.read(byteArray);
tags.put(string, nbtTag);
}
}

@Override
public void write(IByteArray byteArray) {
Collection<String> tagCollection = tags.keySet();
for(String string : tagCollection) {
byteArray.writeByte(tags.get(string).getId());
byteArray.writeShort((short) string.length());
byteArray.writeBytesN(string.getBytes(StandardCharsets.UTF_8));
tags.get(string).write(byteArray);
}
byteArray.writeByte((byte)0);
}

@Override
public NBTTag duplicate() {
CompoundNBTTag compoundNBTTag = new CompoundNBTTag();
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/tag/DoubleNBTTag.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.hilligans.ourcraft.tag;

import dev.hilligans.ourcraft.util.IByteArray;

import java.nio.ByteBuffer;

public class DoubleNBTTag extends NBTTag {
Expand Down Expand Up @@ -32,6 +34,16 @@ public void write(ByteBuffer byteBuf) {
byteBuf.putDouble(val);
}

@Override
public void read(IByteArray byteArray) {
val = byteArray.readDouble();
}

@Override
public void write(IByteArray byteArray) {
byteArray.writeDouble(val);
}

@Override
public NBTTag duplicate() {
return new DoubleNBTTag(val);
Expand Down
Loading

0 comments on commit 7b94e9b

Please sign in to comment.