Merge pull request #1562 from ludovic111/feat/server-pack-status
This commit is contained in:
@@ -608,6 +608,7 @@ impl Game<'_> {
|
||||
lang: Arc::new(horizon_client::net::load_language(&self.options.resources)),
|
||||
log_ips: props.log_ips,
|
||||
}),
|
||||
resource_pack: props.resource_pack.clone(),
|
||||
};
|
||||
let config = HubConfig {
|
||||
default_game_mode: game_mode,
|
||||
|
||||
@@ -119,6 +119,88 @@ fn component_value(tag: &horizon_world::nbt::Tag) -> crate::server::json::Json {
|
||||
}
|
||||
}
|
||||
|
||||
/// The inverse of [`component_value`]: a JSON component (a server.properties
|
||||
/// prompt) as ComponentSerialization.CODEC writes it with NbtOps. An array is
|
||||
/// its first component with the rest appended (`createFromList`); a list of
|
||||
/// components mixing plain strings and objects has the strings as `{text}`.
|
||||
pub fn component_tag(v: &crate::server::json::Json) -> horizon_world::nbt::Tag {
|
||||
use crate::server::json::Json;
|
||||
use horizon_world::nbt::{JavaString, Tag};
|
||||
fn list(items: &[Json]) -> Tag {
|
||||
same_kind(items.iter().map(component_tag).collect())
|
||||
}
|
||||
fn same_kind(mut tags: Vec<Tag>) -> Tag {
|
||||
if tags.iter().any(|t| matches!(t, Tag::Compound(_))) {
|
||||
for t in &mut tags {
|
||||
if let Tag::String(s) = t {
|
||||
*t = Tag::compound([("text", Tag::String(s.clone()))]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Tag::List(tags)
|
||||
}
|
||||
fn value(v: &Json) -> Tag {
|
||||
match v {
|
||||
Json::Bool(b) => Tag::Byte(i8::from(*b)),
|
||||
Json::Number(n) => n.parse().map_or_else(
|
||||
|_| Tag::Double(n.parse::<f64>().unwrap_or(0.0).to_bits()),
|
||||
Tag::Int,
|
||||
),
|
||||
Json::String(s) => Tag::String(JavaString::from(s.as_str())),
|
||||
Json::Array(items) => list(items),
|
||||
Json::Object(members) => Tag::compound(
|
||||
members
|
||||
.iter()
|
||||
.map(|(k, v)| (k.as_str(), value(v)))
|
||||
.collect::<Vec<_>>(),
|
||||
),
|
||||
Json::Null => Tag::String(JavaString::from("")),
|
||||
}
|
||||
}
|
||||
match v {
|
||||
Json::Array(items) if !items.is_empty() => {
|
||||
let mut first = component_tag(&items[0]);
|
||||
if let Tag::String(s) = &first {
|
||||
first = Tag::compound([("text", Tag::String(s.clone()))]);
|
||||
}
|
||||
if let Tag::Compound(map) = &mut first {
|
||||
let mut extra = match map.remove(&JavaString::from("extra")) {
|
||||
Some(Tag::List(e)) => e,
|
||||
_ => Vec::new(),
|
||||
};
|
||||
extra.extend(items[1..].iter().map(component_tag));
|
||||
map.insert(JavaString::from("extra"), same_kind(extra));
|
||||
}
|
||||
first
|
||||
}
|
||||
Json::Object(members) => {
|
||||
if let [(key, Json::String(text))] = members.as_slice()
|
||||
&& key == "text"
|
||||
{
|
||||
return Tag::String(JavaString::from(text.as_str()));
|
||||
}
|
||||
Tag::compound(
|
||||
members
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
let tag = match k.as_str() {
|
||||
"extra" | "with" => match v {
|
||||
Json::Array(items) => list(items),
|
||||
other => value(other),
|
||||
},
|
||||
_ => value(v),
|
||||
};
|
||||
(k.as_str(), tag)
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
}
|
||||
Json::String(s) | Json::Number(s) => Tag::String(JavaString::from(s.as_str())),
|
||||
Json::Bool(b) => Tag::String(JavaString::from(b.to_string().as_str())),
|
||||
_ => Tag::String(JavaString::from("")),
|
||||
}
|
||||
}
|
||||
|
||||
/// StringUtil.isValidPlayerName.
|
||||
pub fn valid_name(name: &str) -> bool {
|
||||
(1..=16).contains(&name.len()) && name.chars().all(|c| c as u32 > 32 && (c as u32) < 127)
|
||||
@@ -745,6 +827,14 @@ fn login_and_configure(
|
||||
send(c, cfg::Clientbound::RegistryData(packet))?;
|
||||
}
|
||||
send(c, cfg::Clientbound::UpdateTags(sync.tags.clone()))?;
|
||||
if let Some(pack) = &shared.config.resource_pack
|
||||
&& let Some(rejected) = offer_pack(c, pack, &mut info)?
|
||||
{
|
||||
let (reason, text) = pack_rejected(shared, name, rejected);
|
||||
let _ = send(c, cfg::Clientbound::Disconnect(cfg::Disconnect { reason }));
|
||||
lost(shared, &format!("{name} ({})", uuid_string(uuid)), &text);
|
||||
return Ok(None);
|
||||
}
|
||||
send(c, cfg::Clientbound::FinishConfiguration(Empty))?;
|
||||
loop {
|
||||
let (id, payload) = c.recv().map_err(e)?;
|
||||
@@ -763,6 +853,79 @@ fn login_and_configure(
|
||||
}))
|
||||
}
|
||||
|
||||
/// ServerboundResourcePackPacket.Action.DECLINED.
|
||||
const PACK_DECLINED: i32 = 1;
|
||||
|
||||
/// ServerCommonPacketListenerImpl.handleResourcePackResponse: a player who
|
||||
/// declined the required pack is disconnected. The reason and its text.
|
||||
fn pack_rejected(shared: &Shared, name: &str, pack: u128) -> (horizon_world::nbt::Tag, String) {
|
||||
let reason = translatable("multiplayer.requiredTexturePrompt.disconnect", &[]);
|
||||
let Some(log) = &shared.config.log else {
|
||||
return (reason, String::new());
|
||||
};
|
||||
let line = format!(
|
||||
"Disconnecting {name} due to resource pack {} rejection",
|
||||
uuid_string(pack)
|
||||
);
|
||||
(log.write)("Server thread", "INFO", &line);
|
||||
let text = horizon_protocol::text::plain(&reason, &log.lang);
|
||||
(reason, text)
|
||||
}
|
||||
|
||||
/// ServerResourcePackConfigurationTask: offer the server's pack, then wait
|
||||
/// for the client's final answer (`Action.isTerminal`: anything but
|
||||
/// ACCEPTED or DOWNLOADED). The player may take a while to answer the
|
||||
/// prompt, so the connection is kept alive meanwhile, as
|
||||
/// ServerConfigurationPacketListenerImpl.tick does. The id of a required
|
||||
/// pack the client declined, which disconnects it.
|
||||
fn offer_pack(
|
||||
c: &mut Connection,
|
||||
pack: &crate::server::properties::ResourcePackInfo,
|
||||
info: &mut ClientInformation,
|
||||
) -> Result<Option<u128>, String> {
|
||||
let e = |e: horizon_protocol::Error| e.to_string();
|
||||
let push = cfg::ResourcePackPush {
|
||||
id: pack.id,
|
||||
url: pack.url.clone(),
|
||||
hash: pack.hash.clone(),
|
||||
required: pack.required,
|
||||
prompt: pack.prompt.as_ref().map(component_tag),
|
||||
};
|
||||
c.send(&cfg::Clientbound::ResourcePackPush(push).encode())
|
||||
.map_err(e)?;
|
||||
std::thread::scope(|scope| {
|
||||
let (stop, stopped) = channel::<()>();
|
||||
let writer = &mut c.writer;
|
||||
scope.spawn(move || {
|
||||
let start = Instant::now();
|
||||
while stopped.recv_timeout(KEEP_ALIVE_EVERY) == Err(RecvTimeoutError::Timeout) {
|
||||
let id = i64::try_from(start.elapsed().as_millis()).unwrap_or(0);
|
||||
let keep_alive = cfg::Clientbound::KeepAlive(cfg::KeepAlive { id });
|
||||
if writer.send(&keep_alive.encode()).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
let answer = loop {
|
||||
let (id, payload) = match c.reader.recv() {
|
||||
Ok(packet) => packet,
|
||||
Err(err) => break Err(err.to_string()),
|
||||
};
|
||||
match cfg::Serverbound::decode(id, &payload) {
|
||||
Ok(cfg::Serverbound::ResourcePack(r)) if !matches!(r.action, 3 | 4) => {
|
||||
let rejected = r.action == PACK_DECLINED && pack.required;
|
||||
break Ok(rejected.then_some(r.id));
|
||||
}
|
||||
Ok(cfg::Serverbound::ClientInformation(i)) => *info = i,
|
||||
Ok(_) => {}
|
||||
Err(err) => break Err(err.to_string()),
|
||||
}
|
||||
};
|
||||
drop(stop);
|
||||
answer
|
||||
})
|
||||
}
|
||||
|
||||
fn play(c: Connection, shared: &Arc<Shared>, parts: Parts, address: std::net::SocketAddr) {
|
||||
let conn_id = shared.next_id();
|
||||
let events = shared.events.clone();
|
||||
@@ -784,6 +947,7 @@ fn play(c: Connection, shared: &Arc<Shared>, parts: Parts, address: std::net::So
|
||||
})
|
||||
.ok()
|
||||
};
|
||||
let name = parts.name.clone();
|
||||
let joined = Joined {
|
||||
conn: conn_id,
|
||||
uuid: parts.uuid,
|
||||
@@ -795,8 +959,20 @@ fn play(c: Connection, shared: &Arc<Shared>, parts: Parts, address: std::net::So
|
||||
latency,
|
||||
address,
|
||||
};
|
||||
let required_pack = shared
|
||||
.config
|
||||
.resource_pack
|
||||
.as_ref()
|
||||
.is_some_and(|p| p.required);
|
||||
let reason = if events.send(NetEvent::Joined(Box::new(joined))).is_ok() {
|
||||
read_loop(&mut reader, conn_id, &events, &alive)
|
||||
match read_loop(&mut reader, conn_id, &events, &alive, required_pack) {
|
||||
Ok(reason) => reason,
|
||||
Err(rejected) => {
|
||||
let (reason, text) = pack_rejected(shared, &name, rejected);
|
||||
let _ = tx.send(Out::Disconnect(reason));
|
||||
text
|
||||
}
|
||||
}
|
||||
} else {
|
||||
"server closed".to_owned()
|
||||
};
|
||||
@@ -808,12 +984,16 @@ fn play(c: Connection, shared: &Arc<Shared>, parts: Parts, address: std::net::So
|
||||
shared.release(parts.uuid);
|
||||
}
|
||||
|
||||
/// Forward the player's packets until the connection ends: why it did, or
|
||||
/// the id of a required resource pack the player declined (with
|
||||
/// `required_pack`), which disconnects them.
|
||||
fn read_loop(
|
||||
reader: &mut ConnReader,
|
||||
conn: u32,
|
||||
events: &Sender<NetEvent>,
|
||||
alive: &AtomicI64,
|
||||
) -> String {
|
||||
required_pack: bool,
|
||||
) -> Result<String, u128> {
|
||||
loop {
|
||||
let (id, payload) = match reader.recv() {
|
||||
Ok(p) => p,
|
||||
@@ -824,19 +1004,24 @@ fn read_loop(
|
||||
|| e.0.contains("reset by peer")
|
||||
|| e.0.contains("aborted") =>
|
||||
{
|
||||
return "Disconnected".to_owned();
|
||||
return Ok("Disconnected".to_owned());
|
||||
}
|
||||
Err(e) => return e.to_string(),
|
||||
Err(e) => return Ok(e.to_string()),
|
||||
};
|
||||
match play::Serverbound::decode(id, &payload) {
|
||||
Ok(play::Serverbound::KeepAlive(k)) => alive.store(k.id, Ordering::Relaxed),
|
||||
Ok(play::Serverbound::ResourcePack(r))
|
||||
if r.action == PACK_DECLINED && required_pack =>
|
||||
{
|
||||
return Err(r.id);
|
||||
}
|
||||
Ok(p) => {
|
||||
if events.send(NetEvent::Packet(conn, Box::new(p))).is_err() {
|
||||
return "server closed".to_owned();
|
||||
return Ok("server closed".to_owned());
|
||||
}
|
||||
}
|
||||
// A malformed packet disconnects like vanilla's decoder exception.
|
||||
Err(e) => return format!("Invalid packet {id}: {e}"),
|
||||
Err(e) => return Ok(format!("Invalid packet {id}: {e}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -988,6 +1173,24 @@ fn write_loop(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn prompts_become_the_components_vanilla_sends() {
|
||||
let tag = |json: &str| {
|
||||
let tag = component_tag(&crate::server::json::parse(json, true).unwrap());
|
||||
component_json(&tag)
|
||||
};
|
||||
assert_eq!(tag(r#"{"text":"Hi"}"#), r#""Hi""#);
|
||||
assert_eq!(tag(r#""Hi""#), r#""Hi""#);
|
||||
assert_eq!(
|
||||
tag(r#"["Hi ",{"text":"there","bold":true}]"#),
|
||||
r#"{"text":"Hi ","extra":[{"text":"there","bold":true}]}"#
|
||||
);
|
||||
assert_eq!(
|
||||
tag(r#"{"translate":"a","with":["b",{"text":"c","color":"red"}]}"#),
|
||||
r#"{"translate":"a","with":["b",{"text":"c","color":"red"}]}"#
|
||||
);
|
||||
}
|
||||
|
||||
fn unpack(out: Out) -> Vec<Vec<u8>> {
|
||||
match out {
|
||||
Out::Packet(body) => vec![body],
|
||||
|
||||
@@ -139,6 +139,9 @@ pub struct NetConfig {
|
||||
/// Online mode: players are authenticated (none on a LAN world).
|
||||
pub online: Option<Arc<OnlineMode>>,
|
||||
pub log: Option<NetLog>,
|
||||
/// server.properties `resource-pack`: offered to every player during
|
||||
/// configuration (a dedicated server's; MinecraftServer.getServerResourcePack).
|
||||
pub resource_pack: Option<crate::server::properties::ResourcePackInfo>,
|
||||
}
|
||||
impl NetConfig {
|
||||
/// MinecraftServer.enforceSecureProfile.
|
||||
@@ -164,6 +167,7 @@ impl NetConfig {
|
||||
gate: None,
|
||||
online: None,
|
||||
log: None,
|
||||
resource_pack: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,6 +165,8 @@ hardcore = false
|
||||
# Any other server.properties key, spelled as vanilla spells it:
|
||||
[properties]
|
||||
# spawn-protection = 16
|
||||
# resource-pack = "https://example.com/pack.zip" # offered to players as they join; with
|
||||
# resource-pack-sha1, require-resource-pack and resource-pack-prompt, as on vanilla
|
||||
|
||||
[engine]
|
||||
path = "" # the game program; empty: the one beside horizon-server
|
||||
|
||||
@@ -0,0 +1,378 @@
|
||||
package dev.horizonlab.parity;
|
||||
|
||||
import com.mojang.serialization.JsonOps;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.Inflater;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.core.UUIDUtil;
|
||||
import net.minecraft.network.ProtocolInfo;
|
||||
import net.minecraft.network.RegistryFriendlyByteBuf;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.ComponentSerialization;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.common.ClientboundDisconnectPacket;
|
||||
import net.minecraft.network.protocol.common.ClientboundKeepAlivePacket;
|
||||
import net.minecraft.network.protocol.common.ClientboundPingPacket;
|
||||
import net.minecraft.network.protocol.common.ClientboundResourcePackPushPacket;
|
||||
import net.minecraft.network.protocol.common.ServerboundClientInformationPacket;
|
||||
import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
|
||||
import net.minecraft.network.protocol.common.ServerboundKeepAlivePacket;
|
||||
import net.minecraft.network.protocol.common.ServerboundPongPacket;
|
||||
import net.minecraft.network.protocol.common.ServerboundResourcePackPacket;
|
||||
import net.minecraft.network.protocol.common.custom.BrandPayload;
|
||||
import net.minecraft.network.protocol.configuration.ClientboundFinishConfigurationPacket;
|
||||
import net.minecraft.network.protocol.configuration.ClientboundSelectKnownPacks;
|
||||
import net.minecraft.network.protocol.configuration.ConfigurationProtocols;
|
||||
import net.minecraft.network.protocol.configuration.ServerboundFinishConfigurationPacket;
|
||||
import net.minecraft.network.protocol.configuration.ServerboundSelectKnownPacks;
|
||||
import net.minecraft.network.protocol.game.ClientboundLoginPacket;
|
||||
import net.minecraft.network.protocol.game.GameProtocols;
|
||||
import net.minecraft.network.protocol.handshake.ClientIntent;
|
||||
import net.minecraft.network.protocol.handshake.ClientIntentionPacket;
|
||||
import net.minecraft.network.protocol.handshake.HandshakeProtocols;
|
||||
import net.minecraft.network.protocol.login.ClientboundLoginCompressionPacket;
|
||||
import net.minecraft.network.protocol.login.ClientboundLoginDisconnectPacket;
|
||||
import net.minecraft.network.protocol.login.ClientboundLoginFinishedPacket;
|
||||
import net.minecraft.network.protocol.login.LoginProtocols;
|
||||
import net.minecraft.network.protocol.login.ServerboundHelloPacket;
|
||||
import net.minecraft.network.protocol.login.ServerboundLoginAcknowledgedPacket;
|
||||
import net.minecraft.server.level.ClientInformation;
|
||||
|
||||
/**
|
||||
* Players made of vanilla's own protocol code that answer a server's resource pack offer
|
||||
* (tools/test_server_resource_pack_parity.py). Script on stdin: {@code join NAME DELAY ACTIONS} joins and, once the
|
||||
* pack is offered, waits DELAY seconds (answering keep-alives) then sends ACTIONS, a comma-separated list of
|
||||
* ServerboundResourcePackPacket.Action names (or {@code none}); it returns once the player is in play or
|
||||
* disconnected. {@code play NAME ACTION} sends an answer to the same pack in play and waits up to 10 s for a disconnect;
|
||||
* {@code end} writes the transcript. Each player records the configuration packets it was sent (runs of one packet
|
||||
* counted), the offer, whether it was kept alive while it waited, and how the connection ended.
|
||||
*/
|
||||
public final class ServerPackClient {
|
||||
public static void main(String[] args) throws Exception {
|
||||
ParityData.init();
|
||||
String host = args[0];
|
||||
int port = Integer.parseInt(args[1]);
|
||||
Path output = Path.of(args[2]);
|
||||
Map<String, Player> players = new LinkedHashMap<>();
|
||||
BufferedReader script = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
|
||||
String line;
|
||||
while ((line = script.readLine()) != null) {
|
||||
String[] w = line.trim().split(" ");
|
||||
try {
|
||||
switch (w[0]) {
|
||||
case "join" -> {
|
||||
Player p = new Player(w[1]);
|
||||
players.put(w[1], p);
|
||||
p.join(host, port, Double.parseDouble(w[2]), w[3]);
|
||||
}
|
||||
case "play" -> players.get(w[1]).answerInPlay(action(w[2]));
|
||||
case "end" -> {
|
||||
try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(output, StandardCharsets.UTF_8))) {
|
||||
for (Player p : players.values()) {
|
||||
synchronized (p.lines) {
|
||||
for (String l : p.lines) out.println(p.name + " " + l);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Player p : players.values()) p.close();
|
||||
System.exit(0);
|
||||
}
|
||||
default -> throw new IllegalArgumentException("unknown script line " + line);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Player p = w.length > 1 ? players.get(w[1]) : null;
|
||||
if (p != null) p.record("error " + w[0] + " " + e);
|
||||
else System.err.println("error " + line + ": " + e);
|
||||
}
|
||||
}
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
private static ServerboundResourcePackPacket.Action action(String name) {
|
||||
return ServerboundResourcePackPacket.Action.valueOf(name.toUpperCase(Locale.ROOT));
|
||||
}
|
||||
|
||||
private static String json(Component c) {
|
||||
return ComponentSerialization.CODEC.encodeStart(ParityData.access.createSerializationContext(JsonOps.INSTANCE), c)
|
||||
.getOrThrow().toString();
|
||||
}
|
||||
|
||||
private static final class Player {
|
||||
final String name;
|
||||
final List<String> lines = new ArrayList<>();
|
||||
private Socket socket;
|
||||
private DataInputStream in;
|
||||
private OutputStream out;
|
||||
private int threshold = -1;
|
||||
private ProtocolInfo<?> inbound;
|
||||
private ProtocolInfo<?> outbound;
|
||||
private final Object ended = new Object();
|
||||
private boolean disconnected;
|
||||
/** The pack offered during configuration (the one answered in play). */
|
||||
private java.util.UUID offered;
|
||||
private String lastConfig;
|
||||
private int run;
|
||||
|
||||
Player(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
void record(String line) {
|
||||
synchronized (lines) {
|
||||
lines.add(line);
|
||||
}
|
||||
}
|
||||
|
||||
/** A configuration packet: consecutive ones of the same type are one line with their count. */
|
||||
private void config(Packet<?> p) {
|
||||
String type = p.type().id().getPath();
|
||||
if (type.equals(lastConfig)) {
|
||||
run++;
|
||||
return;
|
||||
}
|
||||
flushConfig();
|
||||
lastConfig = type;
|
||||
run = 1;
|
||||
}
|
||||
|
||||
private void flushConfig() {
|
||||
if (lastConfig != null) record("config " + lastConfig + (run > 1 ? " x" + run : ""));
|
||||
lastConfig = null;
|
||||
}
|
||||
|
||||
void close() throws Exception {
|
||||
if (socket != null) socket.close();
|
||||
}
|
||||
|
||||
void answerInPlay(ServerboundResourcePackPacket.Action action) throws Exception {
|
||||
synchronized (ended) {
|
||||
send(new ServerboundResourcePackPacket(offered, action));
|
||||
long until = System.nanoTime() + 10_000_000_000L;
|
||||
while (!disconnected) {
|
||||
long left = (until - System.nanoTime()) / 1_000_000L;
|
||||
if (left <= 0) {
|
||||
record("still connected after " + action);
|
||||
return;
|
||||
}
|
||||
ended.wait(left);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void join(String host, int port, double delay, String actions) throws Exception {
|
||||
socket = new Socket();
|
||||
socket.connect(new InetSocketAddress(host, port), 10_000);
|
||||
socket.setTcpNoDelay(true);
|
||||
in = new DataInputStream(socket.getInputStream());
|
||||
out = socket.getOutputStream();
|
||||
outbound = HandshakeProtocols.SERVERBOUND;
|
||||
send(new ClientIntentionPacket(SharedConstants.getProtocolVersion(), host, port, ClientIntent.LOGIN));
|
||||
outbound = LoginProtocols.SERVERBOUND;
|
||||
inbound = LoginProtocols.CLIENTBOUND;
|
||||
send(new ServerboundHelloPacket(name, UUIDUtil.createOfflinePlayerUUID(name)));
|
||||
while (true) {
|
||||
Packet<?> p = read();
|
||||
if (p instanceof ClientboundLoginCompressionPacket c) {
|
||||
threshold = c.getCompressionThreshold();
|
||||
} else if (p instanceof ClientboundLoginFinishedPacket) {
|
||||
send(ServerboundLoginAcknowledgedPacket.INSTANCE);
|
||||
break;
|
||||
} else if (p instanceof ClientboundLoginDisconnectPacket d) {
|
||||
record("login refused " + d.reason().getString());
|
||||
return;
|
||||
}
|
||||
}
|
||||
outbound = ConfigurationProtocols.SERVERBOUND;
|
||||
inbound = ConfigurationProtocols.CLIENTBOUND;
|
||||
send(new ServerboundCustomPayloadPacket(new BrandPayload("vanilla")));
|
||||
send(new ServerboundClientInformationPacket(ClientInformation.createDefault()));
|
||||
boolean keptAlive = false;
|
||||
while (true) {
|
||||
Packet<?> p;
|
||||
try {
|
||||
p = read();
|
||||
} catch (java.io.EOFException | java.net.SocketException e) {
|
||||
flushConfig();
|
||||
record("connection closed in configuration");
|
||||
return;
|
||||
}
|
||||
if (!(p instanceof ClientboundKeepAlivePacket)) config(p);
|
||||
if (p instanceof ClientboundSelectKnownPacks k) {
|
||||
send(new ServerboundSelectKnownPacks(k.knownPacks()));
|
||||
} else if (p instanceof ClientboundKeepAlivePacket k) {
|
||||
// Kept alive while the player looks at the prompt (once is enough to know).
|
||||
if (offered != null && !keptAlive) {
|
||||
flushConfig();
|
||||
record("kept alive while the offer was open");
|
||||
keptAlive = true;
|
||||
}
|
||||
send(new ServerboundKeepAlivePacket(k.getId()));
|
||||
} else if (p instanceof ClientboundPingPacket k) {
|
||||
send(new ServerboundPongPacket(k.getId()));
|
||||
} else if (p instanceof ClientboundResourcePackPushPacket push) {
|
||||
flushConfig();
|
||||
record("offer id=" + push.id() + " url=" + push.url() + " hash=" + push.hash() + " required="
|
||||
+ push.required() + " prompt=" + push.prompt().map(ServerPackClient::json).orElse("none"));
|
||||
offered = push.id();
|
||||
Thread answer = new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep((long) (delay * 1000));
|
||||
if (actions.equals("none")) return;
|
||||
for (String a : actions.split(",")) send(new ServerboundResourcePackPacket(push.id(), action(a)));
|
||||
} catch (Exception e) {
|
||||
record("error answering " + e);
|
||||
}
|
||||
}, name + "-answer");
|
||||
answer.setDaemon(true);
|
||||
answer.start();
|
||||
} else if (p instanceof ClientboundFinishConfigurationPacket) {
|
||||
flushConfig();
|
||||
send(ServerboundFinishConfigurationPacket.INSTANCE);
|
||||
break;
|
||||
} else if (p instanceof ClientboundDisconnectPacket d) {
|
||||
flushConfig();
|
||||
record("disconnected in configuration: " + d.reason().getString());
|
||||
return;
|
||||
}
|
||||
}
|
||||
outbound = GameProtocols.SERVERBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access),
|
||||
new GameProtocols.Context() {
|
||||
@Override
|
||||
public boolean hasInfiniteMaterials() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canUseCommandBlocks() {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
inbound = GameProtocols.CLIENTBOUND_TEMPLATE.bind(RegistryFriendlyByteBuf.decorator(ParityData.access));
|
||||
while (!(read() instanceof ClientboundLoginPacket)) {
|
||||
}
|
||||
record("joined");
|
||||
Thread reader = new Thread(this::play, name);
|
||||
reader.setDaemon(true);
|
||||
reader.start();
|
||||
}
|
||||
|
||||
private void play() {
|
||||
try {
|
||||
while (true) {
|
||||
Packet<?> p = read();
|
||||
if (p instanceof ClientboundKeepAlivePacket k) {
|
||||
send(new ServerboundKeepAlivePacket(k.getId()));
|
||||
} else if (p instanceof ClientboundPingPacket k) {
|
||||
send(new ServerboundPongPacket(k.getId()));
|
||||
} else if (p instanceof ClientboundDisconnectPacket d) {
|
||||
record("disconnected in play: " + d.reason().getString());
|
||||
synchronized (ended) {
|
||||
disconnected = true;
|
||||
ended.notifyAll();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (!socket.isClosed()) record("error play " + e);
|
||||
}
|
||||
}
|
||||
|
||||
private Packet<?> read() throws Exception {
|
||||
byte[] payload = readFrame();
|
||||
ByteBuf buf = Unpooled.wrappedBuffer(payload);
|
||||
Packet<?> packet = inbound.codec().decode(buf);
|
||||
if (buf.readableBytes() > 0) {
|
||||
throw new IllegalStateException(packet.type().id() + " was larger than expected: " + buf.readableBytes() + " bytes left");
|
||||
}
|
||||
return packet;
|
||||
}
|
||||
|
||||
synchronized void send(Packet<?> packet) throws Exception {
|
||||
ByteBuf body = Unpooled.buffer();
|
||||
((ProtocolInfo) outbound).codec().encode(body, packet);
|
||||
byte[] raw = new byte[body.readableBytes()];
|
||||
body.readBytes(raw);
|
||||
ByteBuf frame = Unpooled.buffer();
|
||||
if (threshold >= 0) {
|
||||
ByteBuf inner = Unpooled.buffer();
|
||||
if (raw.length >= threshold) {
|
||||
writeVarInt(inner, raw.length);
|
||||
Deflater d = new Deflater();
|
||||
d.setInput(raw);
|
||||
d.finish();
|
||||
byte[] tmp = new byte[raw.length + 64];
|
||||
while (!d.finished()) inner.writeBytes(tmp, 0, d.deflate(tmp));
|
||||
d.end();
|
||||
} else {
|
||||
writeVarInt(inner, 0);
|
||||
inner.writeBytes(raw);
|
||||
}
|
||||
writeVarInt(frame, inner.readableBytes());
|
||||
frame.writeBytes(inner);
|
||||
} else {
|
||||
writeVarInt(frame, raw.length);
|
||||
frame.writeBytes(raw);
|
||||
}
|
||||
byte[] bytes = new byte[frame.readableBytes()];
|
||||
frame.readBytes(bytes);
|
||||
out.write(bytes);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
private byte[] readFrame() throws Exception {
|
||||
byte[] frame = new byte[readVarInt(in)];
|
||||
in.readFully(frame);
|
||||
if (threshold < 0) return frame;
|
||||
ByteArrayInputStream header = new ByteArrayInputStream(frame);
|
||||
int dataLength = readVarInt(header);
|
||||
int skip = frame.length - header.available();
|
||||
if (dataLength == 0) return java.util.Arrays.copyOfRange(frame, skip, frame.length);
|
||||
Inflater inflater = new Inflater();
|
||||
inflater.setInput(frame, skip, frame.length - skip);
|
||||
byte[] raw = new byte[dataLength];
|
||||
int n = inflater.inflate(raw);
|
||||
inflater.end();
|
||||
if (n != dataLength) throw new IllegalStateException("decompressed " + n + " of " + dataLength + " bytes");
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
|
||||
private static int readVarInt(InputStream in) throws Exception {
|
||||
int value = 0;
|
||||
for (int shift = 0; shift < 35; shift += 7) {
|
||||
int b = in.read();
|
||||
if (b < 0) throw new java.io.EOFException();
|
||||
value |= (b & 0x7F) << shift;
|
||||
if ((b & 0x80) == 0) return value;
|
||||
}
|
||||
throw new IllegalStateException("VarInt too big");
|
||||
}
|
||||
|
||||
private static void writeVarInt(ByteBuf buf, int value) {
|
||||
while ((value & ~0x7F) != 0) {
|
||||
buf.writeByte((value & 0x7F) | 0x80);
|
||||
value >>>= 7;
|
||||
}
|
||||
buf.writeByte(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
#!/usr/bin/env python3
|
||||
"""A dedicated server's resource pack (server.properties `resource-pack`), against vanilla's, as vanilla clients see it.
|
||||
|
||||
A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`) each serve a copy of one flat world
|
||||
vanilla generated first, twice: once with a required pack (an id, a SHA-1 and a prompt), once with an optional one
|
||||
(none of those: the id is derived from the URL). ServerPackClient, players made of vanilla's protocol code, join each
|
||||
and answer the offer the way vanilla's client can:
|
||||
|
||||
- required: declined (disconnected with vanilla's reason), accepted then downloaded then loaded (joins, then a
|
||||
declined answer in play disconnects it too), accepted then failed to download (joins: only declining is refused),
|
||||
and loaded after 20 seconds (the server keeps the connection alive while the player reads the prompt);
|
||||
- optional: declined (joins).
|
||||
|
||||
Compared exactly, per player: the configuration packets in order (runs counted), the offer (id, URL, hash,
|
||||
required, the prompt as a component), whether a keep-alive came while the offer was open, and how the connection
|
||||
ended. The server logs' "due to resource pack ... rejection" lines are compared too. No pack is downloaded: the
|
||||
servers only offer it.
|
||||
"""
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
try:
|
||||
from . import lab, test_rust_parity as base
|
||||
from .test_dedicated_server_parity import Server, free_port
|
||||
from .test_locator_bar_parity import generate_world
|
||||
except ImportError:
|
||||
import lab
|
||||
import test_rust_parity as base
|
||||
from test_dedicated_server_parity import Server, free_port
|
||||
from test_locator_bar_parity import generate_world
|
||||
|
||||
NAME = 'server-resource-pack-parity'
|
||||
PROPERTIES = {
|
||||
'online-mode': 'false',
|
||||
'white-list': 'false',
|
||||
'level-seed': '12345',
|
||||
'level-type': 'minecraft\\:flat',
|
||||
'generate-structures': 'false',
|
||||
'view-distance': '2',
|
||||
'simulation-distance': '2',
|
||||
'max-players': '5',
|
||||
'pause-when-empty-seconds': '-1',
|
||||
}
|
||||
# The URL is never fetched: the servers only offer it.
|
||||
URL = 'http://127.0.0.1:9/horizon-test-pack.zip'
|
||||
SERVERS = {
|
||||
'required': {
|
||||
'resource-pack': URL,
|
||||
'resource-pack-id': '5b2c7a4e-1f3d-4c8b-9a6e-2d4f8b1c3e5a',
|
||||
'resource-pack-sha1': '0123456789abcdef0123456789abcdef01234567',
|
||||
'require-resource-pack': 'true',
|
||||
'resource-pack-prompt': '["Our pack: ",{"text":"blue dirt","color":"aqua","bold":true}]',
|
||||
},
|
||||
'optional': {
|
||||
'resource-pack': URL,
|
||||
'require-resource-pack': 'false',
|
||||
},
|
||||
}
|
||||
# (server, player, seconds before answering, the answers, an answer in play or None).
|
||||
PLAYERS = [
|
||||
('required', 'Alice', 0, 'declined', None),
|
||||
('required', 'Bob', 0, 'accepted,downloaded,successfully_loaded', 'declined'),
|
||||
('required', 'Carol', 0, 'accepted,failed_download', None),
|
||||
('required', 'Dave', 20, 'accepted,downloaded,successfully_loaded', None),
|
||||
('optional', 'Erin', 0, 'declined', None),
|
||||
]
|
||||
REJECTION = re.compile(r'Disconnecting (\S+) due to resource pack (\S+) rejection')
|
||||
|
||||
|
||||
def server_dir(directory, world, pack):
|
||||
if directory.exists():
|
||||
shutil.rmtree(directory)
|
||||
directory.mkdir(parents=True)
|
||||
port = free_port()
|
||||
props = dict(PROPERTIES, **pack, **{'server-port': str(port)})
|
||||
(directory / 'server.properties').write_text(''.join(f'{k}={v}\n' for k, v in props.items()),
|
||||
encoding='iso-8859-1')
|
||||
(directory / 'eula.txt').write_text('eula=true\n')
|
||||
shutil.copytree(world, directory / 'world')
|
||||
(directory / 'world' / 'session.lock').unlink(missing_ok=True)
|
||||
return port
|
||||
|
||||
|
||||
def run_side(label, kind, server_command, client_command, build, world):
|
||||
"""One server with this pack: every player's transcript, and the server's rejection log lines."""
|
||||
directory = build / f'{label}-{kind}'
|
||||
port = server_dir(directory, world, SERVERS[kind])
|
||||
out = build / f'{label}-{kind}.txt'
|
||||
out.unlink(missing_ok=True)
|
||||
server = Server(server_command, directory, f'{label}-{kind}')
|
||||
try:
|
||||
server.wait_for(r'Done \(')
|
||||
with (build / f'{label}-{kind}-client.log').open('w') as log:
|
||||
client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)],
|
||||
cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT,
|
||||
text=True)
|
||||
try:
|
||||
for server_kind, name, delay, answers, in_play in PLAYERS:
|
||||
if server_kind != kind:
|
||||
continue
|
||||
client.stdin.write(f'join {name} {delay} {answers}\n')
|
||||
if in_play:
|
||||
client.stdin.write(f'play {name} {in_play}\n')
|
||||
client.stdin.write('end\n')
|
||||
client.stdin.flush()
|
||||
client.wait(timeout=180)
|
||||
finally:
|
||||
if client.poll() is None:
|
||||
client.kill()
|
||||
finally:
|
||||
server.stop()
|
||||
if not out.exists():
|
||||
raise AssertionError(f'{label}: the client wrote no transcript (see {build}/{label}-{kind}-client.log)')
|
||||
rejections = [' '.join(m.groups()) for m in map(REJECTION.search, server.lines) if m]
|
||||
return out.read_text(encoding='utf-8').splitlines() + [f'log rejected {r}' for r in rejections]
|
||||
|
||||
|
||||
def main():
|
||||
lock, jars, libraries = base.verify_artifacts()
|
||||
build = lab.LOCAL / NAME
|
||||
build.mkdir(parents=True, exist_ok=True)
|
||||
report = build / 'report.json'
|
||||
report.unlink(missing_ok=True)
|
||||
oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/ServerPackClient.java'
|
||||
classes = build / 'classes'
|
||||
shutil.rmtree(classes, ignore_errors=True)
|
||||
classes.mkdir()
|
||||
dependencies = os.pathsep.join(map(str, [jars['server'], *libraries]))
|
||||
subprocess.run(['javac', '--release', str(lock['minecraft']['java_major']), '-proc:none', '-nowarn', '-sourcepath',
|
||||
lab.ROOT / 'mods/horizon/src/test/java', '-cp', dependencies, '-d', classes, oracle],
|
||||
cwd=lab.ROOT, check=True, capture_output=True)
|
||||
subprocess.run(['cargo', 'build', '--release', '-p', 'horizon-client', '--bin', 'horizon-client'], cwd=lab.ROOT,
|
||||
check=True, capture_output=True)
|
||||
binary = lab.cargo_target() / 'release' / ('horizon-client.exe' if os.name == 'nt' else 'horizon-client')
|
||||
vanilla_server = ['java', '-Xmx2g', '-Duser.timezone=UTC', '-cp', dependencies, 'net.minecraft.server.Main', 'nogui']
|
||||
horizon_server = [binary, '--resources', lab.LOCAL / 'horizon-client/26.3', '--dedicated', '--nogui']
|
||||
client = ['java', '-Xmx1g', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.ServerPackClient']
|
||||
|
||||
world = generate_world(vanilla_server, build)
|
||||
differ = []
|
||||
for kind in SERVERS:
|
||||
vanilla = run_side('vanilla', kind, vanilla_server, client, build, world)
|
||||
horizon = run_side('horizon', kind, horizon_server, client, build, world)
|
||||
if not any('offer ' in l for l in vanilla) or any(' error ' in f' {l} ' for l in vanilla):
|
||||
raise AssertionError(f'{kind}: vanilla offered no pack or the client failed: {vanilla}')
|
||||
if vanilla != horizon:
|
||||
import difflib
|
||||
differ.append(f'{kind}:\n' + '\n'.join(difflib.unified_diff(vanilla, horizon, 'vanilla', 'horizon',
|
||||
n=2, lineterm='')))
|
||||
if differ:
|
||||
raise AssertionError(f'the servers differ (see {build}):\n' + '\n'.join(differ)[:8000])
|
||||
lab.write_json(report, dict(
|
||||
schema=1, minecraft=lock['minecraft']['version'], platform=platform.platform(),
|
||||
java=subprocess.check_output(['java', '--version'], text=True).splitlines()[0],
|
||||
players=len(PLAYERS),
|
||||
scope='A dedicated server\'s resource pack as vanilla-codec players see it: the offer during configuration '
|
||||
'(id, URL, hash, required, prompt), its place among the configuration packets, the keep-alives while '
|
||||
'the player decides, and each answer\'s outcome (declined, loaded, failed download, declined in '
|
||||
'play) for a required and an optional pack, with the server\'s rejection log lines.'))
|
||||
print(f'server resource pack: {len(PLAYERS)} players identical')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except AssertionError as error:
|
||||
sys.exit(f'{NAME}: {error}')
|
||||
Reference in New Issue
Block a user