From 76b5449bad1e2ecfa4d9ba469db01fe1bdfeebb4 Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Fri, 5 Dec 2025 13:03:11 +0100 Subject: [PATCH] installer: Write checksum of modified update script to manifest This prevent having to perform a redundant update on first startup. --- CHANGELOG.md | 6 +++++- src/installer.rs | 30 ++++++++++++++++++++++++++++-- src/util.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a6bc21..3b7abf5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] - ReleaseDate +### Fixed + +- Avoid redundant update after initial download + ## [0.2.9] - 2025-12-03 ### Added @@ -133,8 +137,8 @@ - First release -[Unreleased]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.9...HEAD +[Unreleased]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.9...HEAD [0.2.9]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.8...v0.2.9 [0.2.8]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.7...v0.2.8 [0.2.7]: https://github.com/meehl/rusty-path-of-building/compare/v0.2.6...v0.2.7 diff --git a/src/installer.rs b/src/installer.rs index 5b0df74..cfa5ccb 100644 --- a/src/installer.rs +++ b/src/installer.rs @@ -6,6 +6,7 @@ use crate::{ fonts::{Alignment, FontStyle, LayoutJob}, mode::{AppEvent, ModeFrameOutput, ModeTransition}, renderer::primitives::{ClippedPrimitive, DrawPrimitive, TextPrimitive}, + util::replace_in_matching_lines, }; use flate2::read::GzDecoder; use parley::{FontFamily, GenericFamily}; @@ -335,9 +336,9 @@ fn download_path_of_building>( Ok(()) } -/// Replaces UpdateCheck with rusty-path-of-building's custom version of it. +/// Replaces UpdateCheck with rusty-path-of-building's modified version fn replace_updatecheck>(target_dir: P) -> anyhow::Result<()> { - log::info!("Downloading custom UpdateCheck.lua script..."); + log::info!("Downloading modified UpdateCheck.lua script..."); download_file( &format!( @@ -347,6 +348,31 @@ fn replace_updatecheck>(target_dir: P) -> anyhow::Result<()> { target_dir.as_ref().join("UpdateCheck.lua"), )?; + // Replace original checksum with checksum of modified update script + let new_checksum = download_file_contents(&format!( + "https://raw.githubusercontent.com/{REPO_NAME}/main/{}", + "UpdateCheck.lua.sha1" + ))?; + + // file contains checksum followed by filename (separated by whitespace) + // we only need the checksum + let new_checksum = new_checksum + .split_whitespace() + .next() + .ok_or_else(|| anyhow::anyhow!("Invalid checksum file"))?; + + let filename = target_dir.as_ref().join("manifest.xml"); + let manifest = fs::read_to_string(&filename)?; + + let new_manifest = replace_in_matching_lines( + &manifest, + r#"name="UpdateCheck.lua""#, + r#"sha1="([0-9A-Za-z]+)""#, + &format!(r#"sha1="{}""#, new_checksum), + ); + + fs::write(&filename, &new_manifest)?; + Ok(()) } diff --git a/src/util.rs b/src/util.rs index 0a469e2..b5526f9 100644 --- a/src/util.rs +++ b/src/util.rs @@ -27,3 +27,29 @@ pub fn calculate_hash(t: &T) -> u64 { t.hash(&mut state); state.finish() } + +/// Performs replacement only in lines that match a given pattern +pub fn replace_in_matching_lines( + input: &str, + match_pattern: &str, + replace_pattern: &str, + replacement_text: &str, +) -> String { + let match_re = regex::Regex::new(match_pattern).expect("Invalid match regex"); + let replace_re = regex::Regex::new(replace_pattern).expect("Invalid replace regex"); + + let mut output = String::new(); + for line in input.lines() { + dbg!(&line); + if match_re.is_match(line) { + // if line matches the pattern, replace + let replaced_line = replace_re.replace_all(line, replacement_text); + output.push_str(&replaced_line); + } else { + // otherwise, keep original line + output.push_str(line); + } + output.push('\n'); + } + output +}