Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions pomme-client/src/app/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,19 @@ impl Install {
let state = Arc::clone(&self.state);
let report = move |progress| *state.lock().unwrap() = State::Running(progress);
let job: Pin<Box<dyn Future<Output = State> + Send>> = match self.job {
Job::Update => Box::pin(async move {
match crate::update::run(&report).await {
Ok(Some(exe)) => State::Updated(exe),
Ok(None) => State::Done,
Err(reason) => {
tracing::warn!("Skipping the update: {reason}");
State::Done
Job::Update => {
let channel = core.menu.update_channel();
Box::pin(async move {
match crate::update::run(channel, &report).await {
Ok(Some(exe)) => State::Updated(exe),
Ok(None) => State::Done,
Err(reason) => {
tracing::warn!("Skipping the update: {reason}");
State::Done
}
}
}
}),
})
}
Job::Assets => {
let dirs = core.data_dirs.clone();
let version = core.version.clone();
Expand Down
5 changes: 5 additions & 0 deletions pomme-client/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ pub struct LaunchArgs {
#[arg(long)]
pub launch_token: Option<String>,

/// Starts this build as it is, without updating it first: the way to the
/// Nightly Builds option when the newest build won't start.
#[arg(long)]
pub no_update: bool,

#[arg(long)]
pub assets_dir: Option<String>,

Expand Down
27 changes: 27 additions & 0 deletions pomme-client/src/ui/menu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Instant;

use pomme_install::client::Channel;
use serde::{Deserialize, Serialize};
use winit::keyboard::KeyCode;

Expand Down Expand Up @@ -85,6 +86,8 @@ struct Settings {
vignette: bool,
#[serde(default = "default_true")]
entity_shadows: bool,
#[serde(default)]
nightly_builds: bool,
#[serde(default = "default_true")]
show_autosave_indicator: bool,
#[serde(default = "default_true")]
Expand Down Expand Up @@ -336,6 +339,7 @@ impl Default for Settings {
show_autosave_indicator: true,
vignette: true,
entity_shadows: true,
nightly_builds: false,
vsync: true,
max_framerate: 120,
inactivity_fps_limit: InactivityFpsLimit::default(),
Expand Down Expand Up @@ -633,6 +637,7 @@ enum Screen {
OptionsPomme,
OptionsPommePerformance,
OptionsPommeShaders,
OptionsPommeUpdates,
OptionsSkinCustomization,
OptionsMusicSounds,
OptionsControls,
Expand Down Expand Up @@ -661,6 +666,7 @@ impl Screen {
Self::OptionsPomme => Self::OptionsPomme,
Self::OptionsPommePerformance => Self::OptionsPommePerformance,
Self::OptionsPommeShaders => Self::OptionsPommeShaders,
Self::OptionsPommeUpdates => Self::OptionsPommeUpdates,
Self::OptionsSkinCustomization => Self::OptionsSkinCustomization,
Self::OptionsMusicSounds => Self::OptionsMusicSounds,
Self::OptionsControls => Self::OptionsControls,
Expand Down Expand Up @@ -857,6 +863,8 @@ pub struct MainMenu {
pub show_autosave_indicator: bool,
pub vignette: bool,
pub entity_shadows: bool,
/// Whether updates follow master's nightly builds (Pomme-only).
nightly_builds: bool,
pub vsync: bool,
pub max_framerate: u32,
pub inactivity_fps_limit: InactivityFpsLimit,
Expand Down Expand Up @@ -1033,6 +1041,7 @@ impl MainMenu {
show_autosave_indicator: settings.show_autosave_indicator,
vignette: settings.vignette,
entity_shadows: settings.entity_shadows,
nightly_builds: settings.nightly_builds,
vsync: settings.vsync,
max_framerate: settings.max_framerate,
inactivity_fps_limit: settings.inactivity_fps_limit,
Expand Down Expand Up @@ -1295,6 +1304,7 @@ impl MainMenu {
show_autosave_indicator: self.show_autosave_indicator,
vignette: self.vignette,
entity_shadows: self.entity_shadows,
nightly_builds: self.nightly_builds,
vsync: self.vsync,
max_framerate: self.max_framerate,
inactivity_fps_limit: self.inactivity_fps_limit,
Expand Down Expand Up @@ -1406,6 +1416,15 @@ impl MainMenu {
self.save_settings(key_bindings);
}

/// The builds this client updates to at its next start.
pub fn update_channel(&self) -> Channel {
if self.nightly_builds {
Channel::Nightly
} else {
Channel::Stable
}
}

pub fn open_options(&mut self, key_bindings: &KeyBindings) {
// Stale after a disconnect; the in-game path re-sets it every frame.
self.server_render_distance = 0;
Expand All @@ -1432,6 +1451,7 @@ impl MainMenu {
| Screen::OptionsPomme
| Screen::OptionsPommePerformance
| Screen::OptionsPommeShaders
| Screen::OptionsPommeUpdates
| Screen::OptionsSkinCustomization
| Screen::OptionsMusicSounds
| Screen::OptionsControls
Expand Down Expand Up @@ -1738,6 +1758,13 @@ impl MainMenu {
&text_width_fn,
key_bindings,
),
Screen::OptionsPommeUpdates => self.build_options_pomme_updates(
screen_w,
screen_h,
input,
&text_width_fn,
key_bindings,
),
Screen::OptionsSkinCustomization => {
self.build_options_skin(screen_w, screen_h, input, &text_width_fn, key_bindings)
}
Expand Down
84 changes: 73 additions & 11 deletions pomme-client/src/ui/menu/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ const MENU_BACKGROUND_BLUR: &str = "Menu Background Blur:";
const CHUNK_DETAIL: &str = "Chunk Detail:";
const DISTANT_TERRAIN: &str = "Distant Terrain:";
const SHADER_PACK: &str = "Shader Pack:";
const NIGHTLY_BUILDS: &str = "Nightly Builds:";
const CHUNK_FADE: &str = "Chunk Fade:";
const INACTIVITY_FPS_LIMIT: &str = "Reduce fps when:";

Expand Down Expand Up @@ -566,10 +567,15 @@ impl MainMenu {
text_width_fn: common::TextWidthFn,
key_bindings: &KeyBindings,
) -> MainMenuResult {
let rows = [OptRow::Pair("Performance...", "Shaders...")];
let mut rows = vec![OptRow::Pair("Performance...", "Shaders...")];
// Phones and tablets don't update themselves (`update::enabled`).
if cfg!(not(mobile)) {
rows.push(OptRow::PairLeft("Updates..."));
}
let nav: &[(&str, Screen)] = &[
("Performance...", Screen::OptionsPommePerformance),
("Shaders...", Screen::OptionsPommeShaders),
("Updates...", Screen::OptionsPommeUpdates),
];
self.build_options_grid(
sw,
Expand Down Expand Up @@ -662,6 +668,39 @@ impl MainMenu {
)
}

/// Pomme Settings' Updates category (Pomme-only).
pub(super) fn build_options_pomme_updates(
&mut self,
sw: f32,
sh: f32,
input: &MenuInput,
text_width_fn: common::TextWidthFn,
key_bindings: &KeyBindings,
) -> MainMenuResult {
let nightly_label = format!("{NIGHTLY_BUILDS} {}", on_off(self.nightly_builds));
let rows = [OptRow::PairLeft(&nightly_label)];
let tooltips: &[(&str, &str)] = &[(
NIGHTLY_BUILDS,
"Updates to each night's build when the game starts, not only to releases. Nightly \
builds are less tested.",
)];
self.build_options_grid(
sw,
sh,
input,
"Updates",
Screen::OptionsPomme,
&rows,
&[],
&[],
&[],
true,
tooltips,
text_width_fn,
key_bindings,
)
}

pub(super) fn build_options_controls(
&mut self,
sw: f32,
Expand Down Expand Up @@ -1739,6 +1778,10 @@ impl MainMenu {
self.cloud_mode = self.cloud_mode.cycle();
self.save_settings(key_bindings);
}
if label.starts_with(NIGHTLY_BUILDS) {
self.nightly_builds = !self.nightly_builds;
self.save_settings(key_bindings);
}
if label.starts_with(SHADER_PACK) {
self.shader_pack =
next_shader_pack(&self.shader_packs_dir, self.shader_pack.as_deref());
Expand Down Expand Up @@ -2808,16 +2851,26 @@ mod tests {
}
}

#[test]
fn entity_shadows_toggle_preserves_saved_key_bindings() {
let dir =
std::env::temp_dir().join(format!("pomme-entity-shadows-{}", uuid::Uuid::new_v4()));
/// A grid whose options save to a folder of its own, gone with the guard.
fn saving_grid(left: &'static str) -> (Grid, impl Drop) {
let dir = std::env::temp_dir().join(format!("pomme-options-{}", uuid::Uuid::new_v4()));
std::fs::create_dir(&dir).unwrap();
let _cleanup = scopeguard::guard((), |_| {
let mut grid = Grid::new(left, "Other");
grid.menu.settings_dir = dir.clone();
let cleanup = scopeguard::guard((), move |_| {
let _ = std::fs::remove_dir_all(&dir);
});
let mut grid = Grid::new("Entity Shadows: ON", "Other");
grid.menu.settings_dir = dir.clone();
(grid, cleanup)
}

fn saved_settings(grid: &Grid) -> Settings {
let saved = std::fs::read_to_string(grid.menu.settings_dir.join("options.json")).unwrap();
serde_json::from_str(&saved).unwrap()
}

#[test]
fn entity_shadows_toggle_preserves_saved_key_bindings() {
let (mut grid, _cleanup) = saving_grid("Entity Shadows: ON");
grid.menu.entity_shadows = true;
grid.key_bindings
.set(Action::Jump, vec![InputKey::Keyboard(KeyCode::KeyJ)]);
Expand All @@ -2826,9 +2879,7 @@ mod tests {
for enabled in [false, true] {
assert!(grid.frame(&click(300.0)).clicked_button);
assert_eq!(grid.menu.entity_shadows, enabled);
let saved: Settings =
serde_json::from_str(&std::fs::read_to_string(dir.join("options.json")).unwrap())
.unwrap();
let saved = saved_settings(&grid);
assert_eq!(saved.entity_shadows, enabled);
assert_eq!(
serde_json::to_value(&saved.key_bindings).unwrap(),
Expand All @@ -2837,6 +2888,16 @@ mod tests {
}
}

#[test]
fn the_nightly_builds_toggle_moves_the_update_channel_and_is_saved() {
let (mut grid, _cleanup) = saving_grid("Nightly Builds: OFF");
assert_eq!(grid.menu.update_channel(), Channel::Stable);

assert!(grid.frame(&click(300.0)).clicked_button);
assert_eq!(grid.menu.update_channel(), Channel::Nightly);
assert!(saved_settings(&grid).nightly_builds);
}

#[test]
fn mouse_focus_persists_until_another_enabled_option_is_clicked() {
let mut grid = Grid::new("First", "Second");
Expand Down Expand Up @@ -3004,6 +3065,7 @@ mod tests {
menu.build_options_pomme(sw, sh, &input, tw, &key_bindings);
menu.build_options_pomme_performance(sw, sh, &input, tw, &key_bindings);
menu.build_options_pomme_shaders(sw, sh, &input, tw, &key_bindings);
menu.build_options_pomme_updates(sw, sh, &input, tw, &key_bindings);
menu.build_options_controls(sw, sh, &input, tw, &key_bindings);
menu.build_options_chat(sw, sh, &input, tw, &key_bindings);
menu.build_options_accessibility(sw, sh, &input, tw, &key_bindings);
Expand Down
53 changes: 36 additions & 17 deletions pomme-client/src/update.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//! Keeps a release build current: before the menu, a newer `client-v*`
//! release replaces this build's files where they are, and the client
//! Keeps a published build current: before the menu, a newer build on its
//! channel replaces this build's files where they are, and the client
//! relaunches into it.

use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use std::time::Duration;

use pomme_install::client::Channel;
use pomme_install::{Progress, client};

/// The release this binary was built as (`release-client.yml` sets it); dev
/// and local builds have none and never update.
/// The build this binary was published as (`release-client.yml` sets it);
/// dev and local builds have none and never update.
const RELEASE: Option<&str> = option_env!("POMME_RELEASE");
/// Where a new release unpacks before its files move into place.
const STAGING: &str = ".pomme-update";
Expand All @@ -18,41 +20,49 @@ const REPLACED: &str = "old";
/// Offline, the check gives up this soon rather than hold the menu back.
const CHECK_TIMEOUT: Duration = Duration::from_secs(5);

/// This build's `x.y.z` for display, or "dev" for one that isn't a release.
/// This build's version for display, or "dev" for one that wasn't published.
pub fn version() -> &'static str {
RELEASE.and_then(client::release_version).unwrap_or("dev")
static VERSION: OnceLock<String> = OnceLock::new();
VERSION.get_or_init(|| {
RELEASE
.and_then(client::Build::parse)
.map_or_else(|| "dev".to_owned(), |build| build.to_string())
})
}

/// Whether this launch checks for a newer release. Not when the old launcher
/// started it: that keeps its own copies under `launcher/clients`.
/// Whether this launch checks for a newer build: only a published one's.
pub fn enabled(args: &crate::LaunchArgs) -> bool {
RELEASE.is_some() && cfg!(not(mobile)) && wanted(args)
}

/// Whether `args` leave the update on. Not when the old launcher started the
/// client: that keeps its own copies under `launcher/clients`.
fn wanted(args: &crate::LaunchArgs) -> bool {
#[cfg(feature = "test-harness")]
if args.harness_script.is_some() {
return false;
}
// A `--chunk-bench` run measures the binary it was started with.
RELEASE.is_some()
&& cfg!(not(mobile))
&& args.launch_token.is_none()
&& args.chunk_bench.is_none()
!args.no_update && args.launch_token.is_none() && args.chunk_bench.is_none()
}

/// Installs a newer release over this one, returning the binary to relaunch,
/// or `None` when this build is current.
/// Installs `channel`'s newest build over this one if it is newer, returning
/// the binary to relaunch, or `None` when this build is current.
pub async fn run(
channel: Channel,
on_progress: &(dyn Fn(Progress) + Send + Sync),
) -> Result<Option<PathBuf>, String> {
let Some(ours) = RELEASE else {
return Ok(None);
};
let http = crate::net::http::client();
let release = tokio::time::timeout(CHECK_TIMEOUT, client::latest_release(http))
let release = tokio::time::timeout(CHECK_TIMEOUT, client::latest_release(http, channel))
.await
.map_err(|_| "timed out".to_owned())??;
if !client::is_newer(ours, &release.tag_name) {
if !client::is_newer(ours, release.id()) {
return Ok(None);
}
tracing::info!("Updating from {ours} to {}", release.tag_name);
tracing::info!("Updating from {ours} to {}", release.id());
if let Some(appimage) = appimage() {
let file = client::download(http, &release, client::APPIMAGE, on_progress).await?;
replace_appimage(&file, &appimage).await?;
Expand Down Expand Up @@ -166,6 +176,15 @@ mod tests {

use super::*;

#[test]
fn a_launch_can_turn_the_update_off() {
let wanted = |words| wanted(&crate::LaunchArgs::from_words(words).unwrap());
assert!(wanted(""));
assert!(!wanted("--no-update"));
assert!(!wanted("--launch-token abc"));
assert!(!wanted("--chunk-bench 16"));
}

fn release_zip(files: &[(&str, &str)]) -> Vec<u8> {
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
for (name, body) in files {
Expand Down
Loading
Loading