Skip to content

Commit 3960258

Browse files
committed
rewrite networking api
1 parent f07d6c7 commit 3960258

File tree

435 files changed

+16502
-14914
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

435 files changed

+16502
-14914
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
setUpLibrary(project)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version = 0.1.0
2+
archives_base_name = networking-impl
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
setUpModule(project,
2+
'entrypoints-mc13w16a-04192037-mc1.14.4',
3+
'lifecycle-events-mc13w36a-09051446-mc1.13',
4+
'networking-mc13w41a-mc18w30b'
5+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
environment = *
2+
min_mc_version = 1.13-pre3
3+
max_mc_version = 1.13-pre3
4+
mc_version_range = >=1.13-rc.3 <=1.13-rc.3
5+
6+
minecraft_version = 1.13-pre3
7+
feather_build = 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package net.ornithemc.osl.networking.impl;
2+
3+
import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket;
4+
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket;
5+
6+
import net.ornithemc.osl.entrypoints.api.ModInitializer;
7+
import net.ornithemc.osl.entrypoints.api.client.ClientModInitializer;
8+
import net.ornithemc.osl.entrypoints.api.server.ServerModInitializer;
9+
import net.ornithemc.osl.lifecycle.api.client.MinecraftClientEvents;
10+
import net.ornithemc.osl.lifecycle.api.server.MinecraftServerEvents;
11+
import net.ornithemc.osl.networking.api.IdentifierChannelIdentifierParser;
12+
import net.ornithemc.osl.networking.api.PacketBuffers;
13+
import net.ornithemc.osl.networking.api.StringChannelIdentifierParser;
14+
import net.ornithemc.osl.networking.api.client.ClientConnectionEvents;
15+
import net.ornithemc.osl.networking.api.server.ServerConnectionEvents;
16+
import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess;
17+
import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl;
18+
import net.ornithemc.osl.networking.impl.server.ServerPlayNetworkingImpl;
19+
20+
public class Networking implements ModInitializer, ClientModInitializer, ServerModInitializer {
21+
22+
@Override
23+
public void init() {
24+
MinecraftServerEvents.START.register(ServerPlayNetworkingImpl::setUp);
25+
MinecraftServerEvents.STOP.register(ServerPlayNetworkingImpl::destroy);
26+
ServerPlayNetworkingImpl.setUpPacketFactory((channel, data) ->
27+
new CustomPayloadS2CPacket(IdentifierChannelIdentifierParser.toIdentifier(channel), PacketBuffers.unwrapped(data)));
28+
ServerPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (server, handler, player, payload) -> {
29+
// send channel registration data as a response to receiving client channel registration data
30+
ServerPlayNetworkingImpl.sendNoCheck(player, HandshakePayload.CHANNEL, HandshakePayload.server());
31+
32+
((NetworkHandlerAccess)handler).osl$networking$registerChannels(payload.channels);
33+
ServerConnectionEvents.PLAY_READY.invoker().accept(server, player);
34+
35+
return true;
36+
});
37+
}
38+
39+
@Override
40+
public void initClient() {
41+
MinecraftClientEvents.START.register(ClientPlayNetworkingImpl::setUp);
42+
MinecraftClientEvents.STOP.register(ClientPlayNetworkingImpl::destroy);
43+
ClientPlayNetworkingImpl.setUpPacketFactory((channel, data) ->
44+
new CustomPayloadC2SPacket(StringChannelIdentifierParser.toString(channel), PacketBuffers.unwrapped(data)));
45+
ClientPlayNetworkingImpl.registerListener(HandshakePayload.CHANNEL, HandshakePayload::new, (minecraft, handler, payload) -> {
46+
((NetworkHandlerAccess)handler).osl$networking$registerChannels(payload.channels);
47+
ClientConnectionEvents.PLAY_READY.invoker().accept(minecraft);
48+
49+
return true;
50+
});
51+
}
52+
53+
@Override
54+
public void initServer() {
55+
// no-op
56+
}
57+
}

libraries/networking/networking-mc14w21a-mc14w30c/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java renamed to libraries/networking-impl/networking-impl-mc1.13-pre3-mc1.13-pre3/src/main/java/net/ornithemc/osl/networking/impl/mixin/client/ClientPlayNetworkHandlerMixin.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@
1515
import net.minecraft.client.network.handler.ClientPlayNetworkHandler;
1616
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket;
1717

18+
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
1819
import net.ornithemc.osl.networking.api.client.ClientConnectionEvents;
1920
import net.ornithemc.osl.networking.impl.HandshakePayload;
21+
import net.ornithemc.osl.networking.impl.access.NetworkHandlerAccess;
2022
import net.ornithemc.osl.networking.impl.client.ClientPlayNetworkingImpl;
21-
import net.ornithemc.osl.networking.impl.interfaces.mixin.INetworkHandler;
2223

2324
@Mixin(ClientPlayNetworkHandler.class)
24-
public class ClientPlayNetworkHandlerMixin implements INetworkHandler {
25+
public class ClientPlayNetworkHandlerMixin implements NetworkHandlerAccess {
2526

2627
@Shadow @Final private Minecraft minecraft;
2728

2829
/**
2930
* Channels that the server is listening to.
3031
*/
31-
@Unique private Set<String> serverChannels;
32+
@Unique private Set<NamespacedIdentifier> serverChannels;
3233

3334
@Inject(
3435
method = "handleLogin",
@@ -38,7 +39,7 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler {
3839
)
3940
private void osl$networking$handleLogin(CallbackInfo ci) {
4041
// send channel registration data as soon as login occurs
41-
ClientPlayNetworkingImpl.doSend(HandshakePayload.CHANNEL, HandshakePayload.client());
42+
ClientPlayNetworkingImpl.sendNoCheck(HandshakePayload.CHANNEL, HandshakePayload.client());
4243

4344
ClientConnectionEvents.LOGIN.invoker().accept(minecraft);
4445
}
@@ -62,7 +63,7 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler {
6263
)
6364
)
6465
private void osl$networking$handleCustomPayload(CustomPayloadS2CPacket packet, CallbackInfo ci) {
65-
if (ClientPlayNetworkingImpl.handle(minecraft, (ClientPlayNetworkHandler)(Object)this, packet)) {
66+
if (ClientPlayNetworkingImpl.handlePacket(minecraft, (ClientPlayNetworkHandler)(Object)this, packet)) {
6667
ci.cancel();
6768
}
6869
}
@@ -73,12 +74,12 @@ public class ClientPlayNetworkHandlerMixin implements INetworkHandler {
7374
}
7475

7576
@Override
76-
public void osl$networking$registerChannels(Set<String> channels) {
77-
serverChannels = new LinkedHashSet<>(channels);
77+
public boolean osl$networking$isPlayReady(NamespacedIdentifier channel) {
78+
return serverChannels != null && serverChannels.contains(channel);
7879
}
7980

8081
@Override
81-
public boolean osl$networking$isRegisteredChannel(String channel) {
82-
return serverChannels != null && serverChannels.contains(channel);
82+
public void osl$networking$registerChannels(Set<NamespacedIdentifier> channels) {
83+
serverChannels = new LinkedHashSet<>(channels);
8384
}
8485
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.ornithemc.osl.networking.impl.mixin.client;
2+
3+
import org.spongepowered.asm.mixin.Mixin;
4+
5+
import net.minecraft.client.Minecraft;
6+
import net.minecraft.util.BlockableEventLoop;
7+
8+
import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess;
9+
10+
@Mixin(Minecraft.class)
11+
public abstract class MinecraftMixin implements BlockableEventLoop, TaskRunnerAccess {
12+
13+
@Override
14+
public boolean osl$networking$submit(Runnable task) {
15+
this.submit(task);
16+
return true;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package net.ornithemc.osl.networking.impl.mixin.common;
2+
3+
import org.spongepowered.asm.mixin.Mixin;
4+
import org.spongepowered.asm.mixin.Shadow;
5+
import org.spongepowered.asm.mixin.injection.At;
6+
import org.spongepowered.asm.mixin.injection.At.Shift;
7+
import org.spongepowered.asm.mixin.injection.Constant;
8+
import org.spongepowered.asm.mixin.injection.Inject;
9+
import org.spongepowered.asm.mixin.injection.ModifyConstant;
10+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
11+
12+
import net.minecraft.network.PacketByteBuf;
13+
import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket;
14+
15+
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
16+
import net.ornithemc.osl.networking.api.PacketBuffer;
17+
import net.ornithemc.osl.networking.api.PacketBuffers;
18+
import net.ornithemc.osl.networking.api.StringChannelIdentifierParser;
19+
import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess;
20+
21+
@Mixin(CustomPayloadC2SPacket.class)
22+
public class CustomPayloadC2SPacketMixin implements CustomPayloadPacketAccess {
23+
24+
@Shadow private String channel;
25+
@Shadow private PacketByteBuf data;
26+
27+
@ModifyConstant(
28+
method = "read",
29+
constant = @Constant(
30+
intValue = 20
31+
)
32+
)
33+
private int osl$networking$modifyMaxChannelLength(int maxLength) {
34+
return StringChannelIdentifierParser.MAX_LENGTH;
35+
}
36+
37+
@Inject(
38+
method = "m_9429910",
39+
cancellable = true,
40+
at = @At(
41+
value = "INVOKE",
42+
shift = Shift.AFTER,
43+
target = "Lnet/minecraft/server/network/handler/ServerPlayPacketHandler;handleCustomPayload(Lnet/minecraft/network/packet/c2s/play/CustomPayloadC2SPacket;)V"
44+
)
45+
)
46+
private void osl$networking$skipBufferRelease(CallbackInfo ci) {
47+
// there's a call to ByteBuf.release() that we want to skip
48+
// so that we can queue packet handling to the main thread
49+
// Vanilla does this by throwing an exception but for the sake
50+
// of version compat we cannot use the same approach
51+
ci.cancel();
52+
}
53+
54+
@Override
55+
public NamespacedIdentifier osl$networking$getChannel() {
56+
return StringChannelIdentifierParser.fromString(channel);
57+
}
58+
59+
@Override
60+
public PacketBuffer osl$networking$getData() {
61+
return PacketBuffers.wrapped(data.copy());
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.ornithemc.osl.networking.impl.mixin.common;
2+
3+
import org.spongepowered.asm.mixin.Mixin;
4+
import org.spongepowered.asm.mixin.Shadow;
5+
6+
import net.minecraft.network.PacketByteBuf;
7+
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket;
8+
import net.minecraft.resource.Identifier;
9+
10+
import net.ornithemc.osl.core.api.util.NamespacedIdentifier;
11+
import net.ornithemc.osl.networking.api.IdentifierChannelIdentifierParser;
12+
import net.ornithemc.osl.networking.api.PacketBuffer;
13+
import net.ornithemc.osl.networking.api.PacketBuffers;
14+
import net.ornithemc.osl.networking.impl.access.CustomPayloadPacketAccess;
15+
16+
@Mixin(CustomPayloadS2CPacket.class)
17+
public class CustomPayloadS2CPacketMixin implements CustomPayloadPacketAccess {
18+
19+
@Shadow private Identifier channel;
20+
@Shadow private PacketByteBuf data;
21+
22+
@Override
23+
public NamespacedIdentifier osl$networking$getChannel() {
24+
return IdentifierChannelIdentifierParser.fromIdentifier(channel);
25+
}
26+
27+
@Override
28+
public PacketBuffer osl$networking$getData() {
29+
return PacketBuffers.wrapped(data.copy());
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.ornithemc.osl.networking.impl.mixin.common;
2+
3+
import org.spongepowered.asm.mixin.Mixin;
4+
5+
import net.minecraft.server.MinecraftServer;
6+
import net.minecraft.util.BlockableEventLoop;
7+
8+
import net.ornithemc.osl.networking.impl.access.TaskRunnerAccess;
9+
10+
@Mixin(MinecraftServer.class)
11+
public abstract class MinecraftServerMixin implements BlockableEventLoop, TaskRunnerAccess {
12+
13+
@Override
14+
public boolean osl$networking$submit(Runnable task) {
15+
this.submit(task);
16+
return true;
17+
}
18+
}

0 commit comments

Comments
 (0)