df14adbfba
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
418 lines
15 KiB
Rust
418 lines
15 KiB
Rust
//! Multiplayer session: a network thread that connects to a server, logs in
|
|
//! (offline mode only), runs the configuration phase and then forwards decoded
|
|
//! play packets to the game thread. Keep-alives and pings are answered on the
|
|
//! network thread like vanilla's connection handlers so a busy frame never
|
|
//! times the client out. Online-mode (authenticated) servers are refused:
|
|
//! Horizon never handles Microsoft credentials.
|
|
use horizon_protocol::{
|
|
PROTOCOL_VERSION,
|
|
connection::{ConnReader, ConnWriter, Connection, Counters},
|
|
crypto::offline_uuid,
|
|
packets::{Empty, configuration as cfg, handshake, login, play},
|
|
registry::Dynamic,
|
|
servers::parse_address,
|
|
text::{Language, plain},
|
|
};
|
|
use std::{
|
|
sync::{
|
|
Arc, Mutex,
|
|
mpsc::{Receiver, Sender, channel},
|
|
},
|
|
thread::JoinHandle,
|
|
time::Duration,
|
|
};
|
|
|
|
/// What the network thread reports to the game.
|
|
pub enum Event {
|
|
/// Progress text for the connecting screen.
|
|
Status(String),
|
|
/// Configuration finished: the play state begins with these registries.
|
|
Joined {
|
|
registries: Arc<Dynamic>,
|
|
uuid: u128,
|
|
name: String,
|
|
},
|
|
Packet(Box<play::Clientbound>),
|
|
/// A packet that failed to decode (skipped, like vanilla's skippable packets).
|
|
DecodeError(String),
|
|
/// The server sent us back to configuration and finished it again.
|
|
Reconfigured(Arc<Dynamic>),
|
|
Disconnected(String),
|
|
}
|
|
|
|
pub struct Session {
|
|
events: Receiver<Event>,
|
|
writer: Arc<Mutex<Option<ConnWriter>>>,
|
|
thread: Option<JoinHandle<()>>,
|
|
pub address: String,
|
|
pub name: String,
|
|
pub counters: Arc<Mutex<Option<Arc<Counters>>>>,
|
|
/// HORIZON_NET_DEBUG=1 logs every packet sent from the game thread.
|
|
debug: bool,
|
|
}
|
|
impl Session {
|
|
/// Start connecting in the background.
|
|
pub fn connect(address: &str, name: &str, view_distance: i8, lang: Arc<Language>) -> Self {
|
|
let (send, events) = channel();
|
|
let writer = Arc::new(Mutex::new(None));
|
|
let counters = Arc::new(Mutex::new(None));
|
|
let (thread_writer, thread_counters) = (writer.clone(), counters.clone());
|
|
let (address_owned, name_owned) = (address.to_owned(), name.to_owned());
|
|
let thread = std::thread::Builder::new()
|
|
.name("horizon-net".into())
|
|
.spawn(move || {
|
|
let reason = match run(
|
|
&address_owned,
|
|
&name_owned,
|
|
view_distance,
|
|
&lang,
|
|
&send,
|
|
&thread_writer,
|
|
&thread_counters,
|
|
) {
|
|
Ok(reason) => reason,
|
|
Err(e) => e,
|
|
};
|
|
if let Some(w) = thread_writer.lock().unwrap().take() {
|
|
w.shutdown();
|
|
}
|
|
let _ = send.send(Event::Disconnected(reason));
|
|
})
|
|
.ok();
|
|
let debug = std::env::var_os("HORIZON_NET_DEBUG").is_some();
|
|
Self {
|
|
events,
|
|
writer,
|
|
thread,
|
|
address: address.to_owned(),
|
|
name: name.to_owned(),
|
|
counters,
|
|
debug,
|
|
}
|
|
}
|
|
/// Everything received since the last poll.
|
|
pub fn poll(&self) -> Vec<Event> {
|
|
self.events.try_iter().collect()
|
|
}
|
|
/// Send a play packet; silently dropped once the connection is gone.
|
|
pub fn send(&self, packet: play::Serverbound) {
|
|
if self.debug
|
|
&& !matches!(
|
|
packet,
|
|
play::Serverbound::ClientTickEnd(_)
|
|
| play::Serverbound::MovePlayerPos(_)
|
|
| play::Serverbound::MovePlayerPosRot(_)
|
|
)
|
|
{
|
|
eprintln!("[net] -> {packet:?}");
|
|
}
|
|
if let Some(w) = self.writer.lock().unwrap().as_mut()
|
|
&& w.send(&packet.encode()).is_err()
|
|
{
|
|
w.shutdown();
|
|
}
|
|
}
|
|
pub fn disconnect(&mut self) {
|
|
if let Some(w) = self.writer.lock().unwrap().take() {
|
|
w.shutdown();
|
|
}
|
|
if let Some(t) = self.thread.take() {
|
|
let _ = t.join();
|
|
}
|
|
}
|
|
}
|
|
impl Drop for Session {
|
|
fn drop(&mut self) {
|
|
self.disconnect();
|
|
}
|
|
}
|
|
|
|
fn status(send: &Sender<Event>, text: &str) {
|
|
let _ = send.send(Event::Status(text.to_owned()));
|
|
}
|
|
|
|
/// The whole connection; returns the disconnect reason.
|
|
fn run(
|
|
address: &str,
|
|
name: &str,
|
|
view_distance: i8,
|
|
lang: &Language,
|
|
send: &Sender<Event>,
|
|
shared_writer: &Mutex<Option<ConnWriter>>,
|
|
shared_counters: &Mutex<Option<Arc<Counters>>>,
|
|
) -> Result<String, String> {
|
|
let (host, port) =
|
|
parse_address(address).ok_or_else(|| format!("Invalid server address: {address}"))?;
|
|
status(send, "Connecting to the server...");
|
|
let mut c =
|
|
Connection::connect(&host, port, Duration::from_secs(10)).map_err(|e| e.to_string())?;
|
|
*shared_counters.lock().unwrap() = Some(c.reader.counters.clone());
|
|
let e = |e: horizon_protocol::Error| e.to_string();
|
|
c.send(
|
|
&handshake::Serverbound::Intention(handshake::Intention {
|
|
protocol: PROTOCOL_VERSION,
|
|
host: host.clone(),
|
|
port,
|
|
intent: handshake::LOGIN,
|
|
})
|
|
.encode(),
|
|
)
|
|
.map_err(e)?;
|
|
status(send, "Logging in...");
|
|
let uuid = offline_uuid(name);
|
|
c.send(
|
|
&login::Serverbound::Hello(login::ServerHello {
|
|
name: name.to_owned(),
|
|
uuid,
|
|
})
|
|
.encode(),
|
|
)
|
|
.map_err(e)?;
|
|
let profile = loop {
|
|
let (id, payload) = c.recv().map_err(e)?;
|
|
match login::Clientbound::decode(id, &payload).map_err(e)? {
|
|
login::Clientbound::Compression(p) => c.set_compression(p.threshold),
|
|
login::Clientbound::LoginFinished(p) => {
|
|
c.send(&login::Serverbound::LoginAcknowledged(Empty).encode())
|
|
.map_err(e)?;
|
|
break p.profile;
|
|
}
|
|
login::Clientbound::Disconnect(p) => {
|
|
let reason = serde_json::from_str::<serde_json::Value>(&p.reason_json)
|
|
.map(|v| json_text(&v, lang))
|
|
.unwrap_or(p.reason_json);
|
|
return Err(reason);
|
|
}
|
|
login::Clientbound::Hello(_) => {
|
|
return Err("This server is in online mode. Horizon only joins offline-mode servers (online-mode=false).".into());
|
|
}
|
|
login::Clientbound::CustomQuery(q) => {
|
|
c.send(
|
|
&login::Serverbound::CustomQueryAnswer(login::CustomQueryAnswer {
|
|
transaction: q.transaction,
|
|
payload: None,
|
|
})
|
|
.encode(),
|
|
)
|
|
.map_err(e)?;
|
|
}
|
|
login::Clientbound::CookieRequest(q) => {
|
|
c.send(
|
|
&login::Serverbound::CookieResponse(login::CookieResponse {
|
|
key: q.key,
|
|
payload: None,
|
|
})
|
|
.encode(),
|
|
)
|
|
.map_err(e)?;
|
|
}
|
|
login::Clientbound::Unknown(..) => {}
|
|
}
|
|
};
|
|
status(send, "Joining world...");
|
|
c.send(
|
|
&cfg::Serverbound::ClientInformation(cfg::ClientInformation::new(view_distance)).encode(),
|
|
)
|
|
.map_err(e)?;
|
|
c.send(
|
|
&cfg::Serverbound::CustomPayload(cfg::CustomPayload {
|
|
channel: "minecraft:brand".into(),
|
|
data: brand(),
|
|
})
|
|
.encode(),
|
|
)
|
|
.map_err(e)?;
|
|
let (mut reader, writer) = c.split();
|
|
*shared_writer.lock().unwrap() = Some(writer);
|
|
let registries = configure(&mut reader, shared_writer, lang)?;
|
|
let _ = send.send(Event::Joined {
|
|
registries: Arc::new(registries),
|
|
uuid: profile.uuid,
|
|
name: profile.name,
|
|
});
|
|
loop {
|
|
let (id, payload) = reader.recv().map_err(e)?;
|
|
let packet = match play::Clientbound::decode(id, &payload) {
|
|
Ok(p) => p,
|
|
Err(err) => {
|
|
let name = horizon_protocol::ids::name(
|
|
horizon_protocol::State::Play,
|
|
horizon_protocol::Direction::Clientbound,
|
|
id,
|
|
)
|
|
.unwrap_or("?");
|
|
let _ = send.send(Event::DecodeError(format!("{name}: {err}")));
|
|
continue;
|
|
}
|
|
};
|
|
let reply = |p: play::Serverbound| {
|
|
if let Some(w) = shared_writer.lock().unwrap().as_mut() {
|
|
let _ = w.send(&p.encode());
|
|
}
|
|
};
|
|
match packet {
|
|
play::Clientbound::KeepAlive(k) => reply(play::Serverbound::KeepAlive(k)),
|
|
play::Clientbound::Ping(p) => reply(play::Serverbound::Pong(p)),
|
|
play::Clientbound::BundleDelimiter(_) => {}
|
|
play::Clientbound::ResourcePackPush(p) => {
|
|
// Declined: Horizon has no server resource packs.
|
|
reply(play::Serverbound::ResourcePack(cfg::ResourcePackResponse {
|
|
id: p.id,
|
|
action: 1,
|
|
}));
|
|
}
|
|
play::Clientbound::StartConfiguration(_) => {
|
|
reply(play::Serverbound::ConfigurationAcknowledged(Empty));
|
|
let registries = configure(&mut reader, shared_writer, lang)?;
|
|
let _ = send.send(Event::Reconfigured(Arc::new(registries)));
|
|
}
|
|
play::Clientbound::Disconnect(d) => return Ok(plain(&d.reason, lang)),
|
|
other => {
|
|
if send.send(Event::Packet(Box::new(other))).is_err() {
|
|
return Ok("Disconnected".into());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The configuration phase: registries, tags and known packs until the
|
|
/// server finishes it (ClientConfigurationPacketListenerImpl).
|
|
fn configure(
|
|
reader: &mut ConnReader,
|
|
writer: &Mutex<Option<ConnWriter>>,
|
|
lang: &Language,
|
|
) -> Result<Dynamic, String> {
|
|
let mut registries = Dynamic::default();
|
|
let reply = |p: cfg::Serverbound| -> Result<(), String> {
|
|
match writer.lock().unwrap().as_mut() {
|
|
Some(w) => w.send(&p.encode()).map_err(|e| e.to_string()),
|
|
None => Err("Disconnected".into()),
|
|
}
|
|
};
|
|
loop {
|
|
let (id, payload) = reader.recv().map_err(|e| e.to_string())?;
|
|
match cfg::Clientbound::decode(id, &payload).map_err(|e| e.to_string())? {
|
|
cfg::Clientbound::KeepAlive(k) => reply(cfg::Serverbound::KeepAlive(k))?,
|
|
cfg::Clientbound::Ping(p) => reply(cfg::Serverbound::Pong(p))?,
|
|
// Know no packs: the server then sends every registry entry in full.
|
|
cfg::Clientbound::SelectKnownPacks(_) => {
|
|
reply(cfg::Serverbound::SelectKnownPacks(cfg::SelectKnownPacks {
|
|
packs: vec![],
|
|
}))?
|
|
}
|
|
cfg::Clientbound::RegistryData(r) => registries.add(r),
|
|
cfg::Clientbound::CodeOfConduct(_) => {
|
|
reply(cfg::Serverbound::AcceptCodeOfConduct(Empty))?
|
|
}
|
|
cfg::Clientbound::ResourcePackPush(p) => {
|
|
reply(cfg::Serverbound::ResourcePack(cfg::ResourcePackResponse {
|
|
id: p.id,
|
|
action: 1,
|
|
}))?
|
|
}
|
|
cfg::Clientbound::CookieRequest(q) => {
|
|
reply(cfg::Serverbound::CookieResponse(cfg::CookieResponse {
|
|
key: q.key,
|
|
payload: None,
|
|
}))?
|
|
}
|
|
cfg::Clientbound::Disconnect(d) => return Err(plain(&d.reason, lang)),
|
|
cfg::Clientbound::FinishConfiguration(_) => {
|
|
reply(cfg::Serverbound::FinishConfiguration(Empty))?;
|
|
return Ok(registries);
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The minecraft:brand payload: a string naming the client.
|
|
fn brand() -> Vec<u8> {
|
|
let mut w = horizon_protocol::buf::Writer::new();
|
|
w.string("horizon");
|
|
w.data
|
|
}
|
|
|
|
/// Flatten a JSON chat component (login disconnects are JSON).
|
|
pub fn json_text(v: &serde_json::Value, lang: &Language) -> String {
|
|
match v {
|
|
serde_json::Value::String(s) => s.clone(),
|
|
serde_json::Value::Array(a) => a.iter().map(|e| json_text(e, lang)).collect(),
|
|
serde_json::Value::Object(o) => {
|
|
let mut out = if let Some(t) = o.get("text").and_then(|t| t.as_str()) {
|
|
t.to_owned()
|
|
} else if let Some(key) = o.get("translate").and_then(|t| t.as_str()) {
|
|
let args: Vec<String> = o
|
|
.get("with")
|
|
.and_then(|w| w.as_array())
|
|
.map(|a| a.iter().map(|e| json_text(e, lang)).collect())
|
|
.unwrap_or_default();
|
|
let pattern = lang
|
|
.get(key)
|
|
.cloned()
|
|
.or_else(|| {
|
|
o.get("fallback")
|
|
.and_then(|f| f.as_str())
|
|
.map(str::to_owned)
|
|
})
|
|
.unwrap_or_else(|| key.to_owned());
|
|
horizon_protocol::text::format_java(&pattern, &args)
|
|
} else {
|
|
String::new()
|
|
};
|
|
if let Some(extra) = o.get("extra").and_then(|e| e.as_array()) {
|
|
for e in extra {
|
|
out.push_str(&json_text(e, lang));
|
|
}
|
|
}
|
|
out
|
|
}
|
|
other => other.to_string(),
|
|
}
|
|
}
|
|
|
|
/// en_us.json from the prepared resources.
|
|
pub fn load_language(resources: &std::path::Path) -> Language {
|
|
std::fs::read(resources.join("assets/minecraft/lang/en_us.json"))
|
|
.ok()
|
|
.and_then(|b| serde_json::from_slice(&b).ok())
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
/// Ask a server for its status (ServerStatusPinger): MOTD, players and the
|
|
/// round trip time in milliseconds.
|
|
pub fn ping(address: &str, timeout: Duration) -> Result<(serde_json::Value, u128), String> {
|
|
use horizon_protocol::packets::status;
|
|
let (host, port) = parse_address(address).ok_or("Invalid address")?;
|
|
let mut c = Connection::connect(&host, port, timeout).map_err(|e| e.to_string())?;
|
|
c.reader
|
|
.set_read_timeout(Some(timeout))
|
|
.map_err(|e| e.to_string())?;
|
|
let e = |e: horizon_protocol::Error| e.to_string();
|
|
c.send(
|
|
&handshake::Serverbound::Intention(handshake::Intention {
|
|
protocol: PROTOCOL_VERSION,
|
|
host: host.clone(),
|
|
port,
|
|
intent: handshake::STATUS,
|
|
})
|
|
.encode(),
|
|
)
|
|
.map_err(e)?;
|
|
c.send(&status::Serverbound::StatusRequest(Empty).encode())
|
|
.map_err(e)?;
|
|
let (id, payload) = c.recv().map_err(e)?;
|
|
let json = match status::Clientbound::decode(id, &payload).map_err(e)? {
|
|
status::Clientbound::StatusResponse(s) => {
|
|
serde_json::from_str(&s.json).map_err(|e| e.to_string())?
|
|
}
|
|
_ => return Err("unexpected status reply".into()),
|
|
};
|
|
let start = std::time::Instant::now();
|
|
c.send(&status::Serverbound::PingRequest(status::Ping { time: 1 }).encode())
|
|
.map_err(e)?;
|
|
let _ = c.recv().map_err(e)?;
|
|
Ok((json, start.elapsed().as_millis()))
|
|
}
|