6d735c26d3
What the toast showed differed from vanilla 26.3 in several ways:
- the title and the failure line had no arguments: download.pack.title takes
the request number and the batch size, download.pack.failed the failures
and the batch size;
- sizes read "1.3 KiB" style; com.mojang.realmsclient.Unit.humanReadable gives
"1.3 KB" ("512 B" below 1 KiB, half-up rounding), checked against the jar;
- each update didn't restart SystemToast's 5 s display time, so a download
longer than 5 s lost its toast, and the Notification Time option was ignored;
- a finished batch removed the toast at once instead of forceHide's slide out;
- the failure title sat at y 7 in white; SystemToast draws titles yellow, and a
title without a message at y 12;
- a second batch replaced the first one's channel and lost its events.
The download thread now reports HttpUtil's listener calls (requestStart,
downloadStart with the Content-Length, downloadedBytes, requestFinished), and
each batch gets a PackNotifier (createDownloadNotifier) with its own toast. The
toast is a shared toasts::SystemToast; the system background moved to
toasts.rs with a y, so the pack toasts stack under an advancement toast.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
560 lines
21 KiB
Rust
560 lines
21 KiB
Rust
//! A server's resource packs bound to the game (`ClientCommonPacketListenerImpl.
|
|
//! handleResourcePackPush` and `handleResourcePackPop`, its `PackConfirmScreen`,
|
|
//! and `DownloadedPackSource` as `Minecraft` drives it): the prompt, the
|
|
//! server list's setting, downloads into `downloads/`, and a resource reload
|
|
//! with the server's packs on top of the player's own.
|
|
use super::Game;
|
|
use horizon_client::{
|
|
Result,
|
|
confirm_screen::Confirm,
|
|
hud::{HudAtlas, HudBatch},
|
|
java_text, multiplayer, packs,
|
|
scoreboard::{comp_text, tag_to_json},
|
|
server_packs::{Prompt, ServerPacks, action, download, parse_sha1, valid_url},
|
|
text::{Text, named_color},
|
|
toasts::SystemToast,
|
|
ui,
|
|
};
|
|
use horizon_core::jmath;
|
|
use horizon_protocol::{packets::configuration::ResourcePackPush, servers};
|
|
use std::{
|
|
path::PathBuf,
|
|
sync::mpsc::{Receiver, TryRecvError},
|
|
};
|
|
|
|
/// `ClientCommonPacketListenerImpl.PackConfirmScreen`: the packs it asks
|
|
/// about, whether the last one is required, and the server's message.
|
|
pub(super) struct PackPrompt {
|
|
requests: Vec<(u128, String, String)>,
|
|
required: bool,
|
|
prompt: Option<Text>,
|
|
}
|
|
impl PackPrompt {
|
|
fn texts(&self) -> (Text, Text, [Text; 2]) {
|
|
let t = |key: &str| Text::translate(key, &[]);
|
|
let (title, line2) = if self.required {
|
|
let yellow = named_color("yellow").unwrap_or(0xFFFF55);
|
|
(
|
|
t("multiplayer.requiredTexturePrompt.line1"),
|
|
t("multiplayer.requiredTexturePrompt.line2")
|
|
.with_color(yellow)
|
|
.bold(),
|
|
)
|
|
} else {
|
|
(
|
|
t("multiplayer.texturePrompt.line1"),
|
|
t("multiplayer.texturePrompt.line2"),
|
|
)
|
|
};
|
|
// preparePackPrompt.
|
|
let message = match &self.prompt {
|
|
Some(prompt) => Text::translate(
|
|
"multiplayer.texturePrompt.serverPrompt",
|
|
&[line2, prompt.clone()],
|
|
),
|
|
None => line2,
|
|
};
|
|
let buttons = if self.required {
|
|
[t("gui.proceed"), t("menu.disconnect")]
|
|
} else {
|
|
[t("gui.yes"), t("gui.no")]
|
|
};
|
|
(title, message, buttons)
|
|
}
|
|
fn layout(&self, atlas: &HudAtlas, size: (f32, f32)) -> Confirm {
|
|
let (title, message, _) = self.texts();
|
|
Confirm::new(&atlas.font, size, &title, &message)
|
|
}
|
|
}
|
|
|
|
/// The game's `DownloadedPackSource`.
|
|
#[derive(Default)]
|
|
pub(super) struct ServerPackState {
|
|
packs: ServerPacks,
|
|
/// `downloads/` in the game folder, opened at the first download.
|
|
queue: Option<download::Queue>,
|
|
pub(super) prompt: Option<PackPrompt>,
|
|
/// The server list entry the setting is saved to (none: a direct join).
|
|
address: Option<String>,
|
|
/// The account the downloads are made for (the download headers).
|
|
user: (String, u128),
|
|
/// The packs loaded on top of the player's own, bottom first.
|
|
pub(super) active: Vec<packs::Pack>,
|
|
/// The download batches' notifiers, oldest first.
|
|
notifiers: Vec<PackNotifier>,
|
|
}
|
|
/// `DownloadedPackSource.createDownloadNotifier`: one batch's progress in its
|
|
/// own `SystemToast`.
|
|
struct PackNotifier {
|
|
progress: Receiver<download::Progress>,
|
|
total: usize,
|
|
/// `count`: the requests started.
|
|
count: usize,
|
|
/// `failCount`.
|
|
failed: usize,
|
|
/// `totalBytes`: the Content-Length of the request in flight.
|
|
size: Option<u64>,
|
|
/// The bytes it has written so far: none before the first response.
|
|
bytes: Option<u64>,
|
|
/// The last request has finished.
|
|
done: bool,
|
|
toast: Option<SystemToast>,
|
|
}
|
|
impl PackNotifier {
|
|
fn new(progress: Receiver<download::Progress>, total: usize) -> Self {
|
|
Self {
|
|
progress,
|
|
total,
|
|
count: 0,
|
|
failed: 0,
|
|
size: None,
|
|
bytes: None,
|
|
done: false,
|
|
toast: None,
|
|
}
|
|
}
|
|
/// The batch's events since the last call, with the toast updated once
|
|
/// for all of them. False once the batch has ended and its toast is gone.
|
|
fn poll(&mut self) -> bool {
|
|
let (mut changed, mut hide) = (false, false);
|
|
let ended = loop {
|
|
let event = match self.progress.try_recv() {
|
|
Ok(event) => event,
|
|
Err(e) => break e == TryRecvError::Disconnected,
|
|
};
|
|
changed |= match event {
|
|
download::Progress::RequestStart => {
|
|
self.count += 1;
|
|
true
|
|
}
|
|
download::Progress::DownloadStart(size) => {
|
|
self.size = size;
|
|
self.bytes = Some(0);
|
|
true
|
|
}
|
|
download::Progress::Bytes(bytes) => {
|
|
self.bytes = Some(bytes);
|
|
true
|
|
}
|
|
download::Progress::RequestFinished(ok) => {
|
|
self.failed += usize::from(!ok);
|
|
self.done = self.count == self.total;
|
|
// Every pack came: forceHide; else the failure title.
|
|
hide |= self.done && self.failed == 0;
|
|
self.done && self.failed > 0
|
|
}
|
|
};
|
|
};
|
|
if changed {
|
|
let (title, message) = self.text();
|
|
let (title, message) = (title.plain(), message.map(|m| m.plain()));
|
|
// SystemToast.addOrUpdate.
|
|
match &mut self.toast {
|
|
Some(toast) => toast.reset(title, message),
|
|
None => self.toast = Some(SystemToast::new(title, message)),
|
|
}
|
|
}
|
|
if hide && let Some(toast) = &mut self.toast {
|
|
toast.force_hide();
|
|
}
|
|
!ended || self.toast.is_some()
|
|
}
|
|
/// The toast's title and message.
|
|
fn text(&self) -> (Text, Option<Text>) {
|
|
if self.done && self.failed > 0 {
|
|
let failed = Text::tr("download.pack.failed", &[&self.failed, &self.total]);
|
|
return (failed, None);
|
|
}
|
|
let title = Text::tr("download.pack.title", &[&self.count, &self.total]);
|
|
let message = self.bytes.map(|bytes| match self.size {
|
|
Some(size) if size > 0 => {
|
|
Text::tr("download.pack.progress.percent", &[&(bytes * 100 / size)])
|
|
}
|
|
_ => Text::tr("download.pack.progress.bytes", &[&human_readable(bytes)]),
|
|
});
|
|
(title, message)
|
|
}
|
|
}
|
|
/// `com.mojang.realmsclient.Unit.humanReadable`, for
|
|
/// `download.pack.progress.bytes`.
|
|
fn human_readable(bytes: u64) -> String {
|
|
if bytes < 1024 {
|
|
return format!("{bytes} B");
|
|
}
|
|
let exp = (jmath::log(bytes as f64) / jmath::log(1024.0)) as i32;
|
|
let prefix = &"KMGTPE"[exp as usize - 1..exp as usize];
|
|
let value = bytes as f64 / 1024f64.powi(exp);
|
|
format!("{} {prefix}B", java_text::format_fixed(value, 1))
|
|
}
|
|
|
|
fn saves() -> PathBuf {
|
|
std::env::current_dir().unwrap_or_default().join("saves")
|
|
}
|
|
|
|
impl Game<'_> {
|
|
/// `ConnectScreen`: the server's setting decides what its packs do
|
|
/// (`configureForServerControl`); single player's own server takes them
|
|
/// all, quietly (`configureForLocalWorld`).
|
|
pub(super) fn server_packs_connect(&mut self, address: Option<&str>, user: (String, u128)) {
|
|
let state = &mut self.server_packs;
|
|
state.user = user;
|
|
state.prompt = None;
|
|
state.notifiers.clear();
|
|
let Some(address) = address else {
|
|
state.address = None;
|
|
state.packs.local_world();
|
|
return;
|
|
};
|
|
let file = multiplayer::servers_file(&saves());
|
|
let entry = servers::load(&file).into_iter().find(|s| s.ip == address);
|
|
let setting = entry.as_ref().and_then(|e| e.accept_textures);
|
|
state.address = entry.map(|e| e.ip);
|
|
state.packs.connect(Prompt::for_server(setting));
|
|
}
|
|
/// `Minecraft.clearDownloadedResourcePacks`: every server pack goes at once.
|
|
pub(super) fn server_packs_disconnect(&mut self) {
|
|
self.server_packs.prompt = None;
|
|
self.server_packs.notifiers.clear();
|
|
self.server_packs.packs.disconnect();
|
|
self.tick_server_packs();
|
|
}
|
|
|
|
/// `handleResourcePackPush`.
|
|
pub(super) fn handle_pack_push(&mut self, p: ResourcePackPush) {
|
|
if !valid_url(&p.url) {
|
|
self.send_pack_status(p.id, action::INVALID_URL);
|
|
return;
|
|
}
|
|
let setting = self.server_setting();
|
|
let prompt = Prompt::for_server(setting);
|
|
if prompt != Prompt::Pending && (!p.required || prompt != Prompt::Declined) {
|
|
self.server_packs
|
|
.packs
|
|
.push(p.id, &p.url, parse_sha1(&p.hash));
|
|
return;
|
|
}
|
|
// addOrUpdatePackPrompt: one prompt for every pack pushed before it is answered.
|
|
let text = p.prompt.as_ref().map(|t| comp_text(&tag_to_json(t)));
|
|
let state = &mut self.server_packs;
|
|
let open = state.prompt.get_or_insert(PackPrompt {
|
|
requests: Vec::new(),
|
|
required: p.required,
|
|
prompt: None,
|
|
});
|
|
open.requests.push((p.id, p.url, p.hash));
|
|
open.required = p.required;
|
|
open.prompt = text;
|
|
self.capture(false);
|
|
}
|
|
/// `handleResourcePackPop`: one pack, or all of them.
|
|
pub(super) fn handle_pack_pop(&mut self, id: Option<u128>) {
|
|
match id {
|
|
Some(id) => self.server_packs.packs.pop(id),
|
|
None => self.server_packs.packs.pop_all(),
|
|
}
|
|
}
|
|
/// The joined server's entry in the server list: its `acceptTextures`.
|
|
fn server_setting(&self) -> Option<bool> {
|
|
let address = self.server_packs.address.as_deref()?;
|
|
servers::load(&multiplayer::servers_file(&saves()))
|
|
.into_iter()
|
|
.find(|s| s.ip == address)?
|
|
.accept_textures
|
|
}
|
|
fn send_pack_status(&self, id: u128, status: i32) {
|
|
if let Some(r) = &self.remote {
|
|
r.session.send_pack_status(id, status);
|
|
}
|
|
}
|
|
|
|
/// The prompt's answer (`PackConfirmScreen`'s callback): the server's
|
|
/// setting follows it, declining a required pack disconnects, and every
|
|
/// pack it asked about is pushed.
|
|
pub(super) fn answer_pack_prompt(&mut self, yes: bool) -> Result<()> {
|
|
let Some(prompt) = self.server_packs.prompt.take() else {
|
|
return Ok(());
|
|
};
|
|
if self.player.is_some() && self.screen == super::Screen::Playing {
|
|
self.capture(true);
|
|
}
|
|
let setting = if yes {
|
|
self.server_packs.packs.allow();
|
|
Some(true)
|
|
} else {
|
|
self.server_packs.packs.reject();
|
|
if prompt.required {
|
|
let reason = Text::translate("multiplayer.requiredTexturePrompt.disconnect", &[]);
|
|
return self.leave_server(Some(reason.plain()));
|
|
}
|
|
Some(false)
|
|
};
|
|
for (id, url, hash) in prompt.requests {
|
|
self.server_packs.packs.push(id, &url, parse_sha1(&hash));
|
|
}
|
|
// ServerList.saveSingleServer.
|
|
if let Some(address) = &self.server_packs.address {
|
|
let file = multiplayer::servers_file(&saves());
|
|
let mut list = servers::load(&file);
|
|
for entry in list.iter_mut().filter(|s| &s.ip == address) {
|
|
entry.accept_textures = setting;
|
|
}
|
|
if let Err(e) = servers::save(&file, &list) {
|
|
log::warn!("Horizon: could not save {}: {e}", file.display());
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Runs what the packs need (`createUpdateScheduler`): the downloads
|
|
/// that finished, the ones to start, a resource reload when the active
|
|
/// packs change, and the statuses for the server.
|
|
pub(super) fn tick_server_packs(&mut self) {
|
|
self.server_packs.notifiers.retain_mut(PackNotifier::poll);
|
|
// A reload waits for the world's resources (`reload_packs`).
|
|
if self.rest_loader.is_some() || self.world_loader.is_some() {
|
|
return;
|
|
}
|
|
let finished = self
|
|
.server_packs
|
|
.queue
|
|
.as_ref()
|
|
.map(download::Queue::finished)
|
|
.unwrap_or_default();
|
|
for batch in finished {
|
|
self.server_packs.packs.downloaded(batch);
|
|
}
|
|
while self.server_packs.packs.wants_tick() {
|
|
let tick = self.server_packs.packs.tick();
|
|
if !tick.download.is_empty() {
|
|
self.start_pack_downloads(tick.download);
|
|
}
|
|
if let Some(mut load) = tick.reload {
|
|
while let Some(recovery) = {
|
|
let ok = self.reload_server_packs(&load);
|
|
self.server_packs.packs.reloaded(ok)
|
|
} {
|
|
load = recovery;
|
|
}
|
|
}
|
|
}
|
|
for (id, status) in self.server_packs.packs.take_replies() {
|
|
self.send_pack_status(id, status);
|
|
}
|
|
}
|
|
fn start_pack_downloads(&mut self, requests: Vec<horizon_client::server_packs::Request>) {
|
|
let state = &mut self.server_packs;
|
|
if state.queue.is_none() {
|
|
let dir = std::env::current_dir()
|
|
.unwrap_or_default()
|
|
.join("downloads");
|
|
match download::Queue::open(dir) {
|
|
Ok(queue) => state.queue = Some(queue),
|
|
Err(e) => {
|
|
log::error!("Horizon: failed to open the download queue: {e}");
|
|
// Every request failed, as the notifier would hear it.
|
|
let (progress, events) = std::sync::mpsc::channel();
|
|
for _ in &requests {
|
|
let _ = progress.send(download::Progress::RequestStart);
|
|
let _ = progress.send(download::Progress::RequestFinished(false));
|
|
}
|
|
state
|
|
.notifiers
|
|
.push(PackNotifier::new(events, requests.len()));
|
|
let failed = requests.iter().map(|r| r.id).collect();
|
|
state.packs.downloaded(horizon_client::server_packs::Batch {
|
|
downloaded: Vec::new(),
|
|
failed,
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
let headers = download::headers(&state.user.0, state.user.1);
|
|
let (progress, events) = std::sync::mpsc::channel();
|
|
state
|
|
.notifiers
|
|
.push(PackNotifier::new(events, requests.len()));
|
|
if let Some(queue) = &state.queue {
|
|
queue.download(requests, headers, progress);
|
|
}
|
|
}
|
|
/// `DownloadedPackSource.startReload`: the packs on top of the player's
|
|
/// own, then `Minecraft.reloadResourcePacks`. False when a pack has no
|
|
/// readable `pack.mcmeta` or the reload failed.
|
|
fn reload_server_packs(&mut self, load: &[(u128, PathBuf)]) -> bool {
|
|
let packs: Option<Vec<_>> = load
|
|
.iter()
|
|
.map(|(id, path)| packs::server_pack(*id, path))
|
|
.collect();
|
|
let Some(packs) = packs else {
|
|
log::warn!("Horizon: invalid pack metadata in a server pack, ignoring all");
|
|
return false;
|
|
};
|
|
self.server_packs.active = packs;
|
|
match self.reload_selected_packs() {
|
|
Ok(()) => true,
|
|
Err(e) => {
|
|
log::warn!("Horizon: server resource pack reload failed: {e}");
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(super) fn draw_pack_prompt(&self, batch: &mut HudBatch) {
|
|
let Some(prompt) = &self.server_packs.prompt else {
|
|
return;
|
|
};
|
|
let atlas = &self.gui.atlas;
|
|
let (w, h) = (batch.width, batch.height);
|
|
if self.player.is_none() {
|
|
// In place of the connecting screen, on its background.
|
|
batch.fill(
|
|
atlas,
|
|
0.0,
|
|
0.0,
|
|
w,
|
|
h,
|
|
horizon_client::hud::rgba(20, 20, 20, 255),
|
|
);
|
|
}
|
|
ui::menu_background(batch, atlas, 0.0, 0.0, w, h, self.player.is_some());
|
|
let (_, _, [yes, no]) = prompt.texts();
|
|
prompt
|
|
.layout(atlas, (w, h))
|
|
.draw(batch, atlas, self.cursor, [&yes, &no]);
|
|
}
|
|
/// The download notifiers' toasts, each in the next 32-pixel slot
|
|
/// below an advancement or recipe toast (`ToastManager`).
|
|
pub(super) fn draw_pack_toasts(&mut self, batch: &mut HudBatch) {
|
|
let multiplier = f64::from(self.settings.notification_display_time);
|
|
let mut slot = usize::from(!self.adv.toasts.is_empty());
|
|
for notifier in &mut self.server_packs.notifiers {
|
|
let Some(toast) = &mut notifier.toast else {
|
|
continue;
|
|
};
|
|
let (sound, finished) = toast.update(multiplier);
|
|
if let Some(show) = sound {
|
|
let event = if show { "ui.toast.in" } else { "ui.toast.out" };
|
|
self.audio
|
|
.play(&format!("minecraft:{event}"), None, 1.0, 1.0);
|
|
}
|
|
if finished {
|
|
notifier.toast = None;
|
|
continue;
|
|
}
|
|
toast.draw(&self.gui.atlas, batch, 32.0 * slot as f32);
|
|
slot += 1;
|
|
}
|
|
}
|
|
/// A left click on the prompt: its answer when it hit a button.
|
|
pub(super) fn pack_prompt_click(&mut self) -> Result<()> {
|
|
let Some(prompt) = &self.server_packs.prompt else {
|
|
return Ok(());
|
|
};
|
|
let answer = prompt
|
|
.layout(&self.gui.atlas, self.gui_size())
|
|
.answer_at(self.cursor);
|
|
match answer {
|
|
Some(yes) => {
|
|
self.audio.play_button_click();
|
|
self.answer_pack_prompt(yes)
|
|
}
|
|
None => Ok(()),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use download::Progress::*;
|
|
|
|
fn notifier(total: usize, events: &[download::Progress]) -> PackNotifier {
|
|
let (send, receive) = std::sync::mpsc::channel();
|
|
for event in events {
|
|
send.send(*event).unwrap();
|
|
}
|
|
let mut notifier = PackNotifier::new(receive, total);
|
|
assert!(notifier.poll());
|
|
notifier
|
|
}
|
|
fn texts(n: &PackNotifier) -> (serde_json::Value, Option<serde_json::Value>) {
|
|
let (title, message) = n.text();
|
|
(title.component(), message.map(|m| m.component()))
|
|
}
|
|
|
|
#[test]
|
|
fn the_toast_counts_requests_and_shows_progress() {
|
|
let n = notifier(2, &[RequestStart]);
|
|
let (title, message) = texts(&n);
|
|
assert_eq!(title["translate"], "download.pack.title");
|
|
assert_eq!(title["with"], serde_json::json!([1, 2]));
|
|
assert_eq!(message, None);
|
|
let n = notifier(2, &[RequestStart, DownloadStart(Some(400)), Bytes(100)]);
|
|
assert_eq!(
|
|
texts(&n).1.unwrap(),
|
|
serde_json::json!({"translate": "download.pack.progress.percent", "with": [25]})
|
|
);
|
|
let n = notifier(1, &[RequestStart, DownloadStart(None), Bytes(1280)]);
|
|
assert_eq!(
|
|
texts(&n).1.unwrap()["with"],
|
|
serde_json::json!([{"text": "1.3 KB"}])
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_failed_pack_turns_the_toast_into_the_failure_count() {
|
|
let events = [
|
|
RequestStart,
|
|
RequestFinished(false),
|
|
RequestStart,
|
|
DownloadStart(Some(3)),
|
|
Bytes(3),
|
|
];
|
|
let (send, receive) = std::sync::mpsc::channel();
|
|
for event in events {
|
|
send.send(event).unwrap();
|
|
}
|
|
let mut n = PackNotifier::new(receive, 2);
|
|
assert!(n.poll());
|
|
// Not the last request yet: still downloading.
|
|
assert_eq!(texts(&n).0["translate"], "download.pack.title");
|
|
send.send(RequestFinished(true)).unwrap();
|
|
drop(send);
|
|
assert!(n.poll(), "the failure toast stays until it slides out");
|
|
let (title, message) = texts(&n);
|
|
assert_eq!(
|
|
title,
|
|
serde_json::json!({"translate": "download.pack.failed", "with": [1, 2]})
|
|
);
|
|
assert_eq!(message, None);
|
|
}
|
|
|
|
#[test]
|
|
fn a_finished_batch_without_a_toast_is_dropped() {
|
|
let (send, receive) = std::sync::mpsc::channel::<download::Progress>();
|
|
drop(send);
|
|
assert!(!PackNotifier::new(receive, 1).poll());
|
|
}
|
|
|
|
#[test]
|
|
fn sizes_read_as_realms_unit_human_readable() {
|
|
// com.mojang.realmsclient.Unit.humanReadable, run on the 26.3 jar.
|
|
for (bytes, text) in [
|
|
(0, "0 B"),
|
|
(1023, "1023 B"),
|
|
(1024, "1.0 KB"),
|
|
(1280, "1.3 KB"),
|
|
(1535, "1.5 KB"),
|
|
(10_000, "9.8 KB"),
|
|
(1_048_575, "1024.0 KB"),
|
|
(1_048_576, "1.0 MB"),
|
|
(52_428_800, "50.0 MB"),
|
|
(262_144_000, "250.0 MB"),
|
|
(1_073_741_824, "1.0 GB"),
|
|
] {
|
|
assert_eq!(human_readable(bytes), text, "{bytes}");
|
|
}
|
|
}
|
|
}
|