Save splashes when loading art is missing and restore art notifications (#83)
CI / core (pull_request) Successful in 1m31s
CI / core (push) Successful in 1m40s

This commit was merged in pull request #93.
This commit is contained in:
nessar committed 2026-11-21 14:26:59 +01:00
1 parent e440d155fc
commit cee9ff2989
5 files changed
+190 -86

No files matched your search

+7 -4
View File
@@ -281,11 +281,11 @@ impl Tracker {
self.download_cards(on_attempt, |_, _| {}).map(|_| ())
}
/// Shares the uncropped loading art with the splash pass. False means the network went away.
/// Shares decoded loading art or a confirmed 404 with the splash pass. False stops both passes offline.
pub(crate) fn download_cards(
&self,
on_attempt: &mut dyn FnMut(bool),
mut on_loading: impl FnMut(&str, &image::DynamicImage),
mut on_loading: impl FnMut(&str, Option<&image::DynamicImage>),
) -> Result<bool, Error> {
let Some((source, portraits_dir)) = &self.champions else {
return Ok(true);
@@ -304,7 +304,10 @@ impl Tracker {
on_attempt(false);
return Ok(false);
}
Err(_) => {
Err(error) => {
if error == SourceError::NotFound {
on_loading(&champion.key, None);
}
on_attempt(false);
continue;
}
@@ -313,7 +316,7 @@ impl Tracker {
on_attempt(false);
continue;
};
on_loading(&champion.key, &art);
on_loading(&champion.key, Some(&art));
let Ok(card) = process_card_portrait(art.into_rgb8()) else {
on_attempt(false);
continue;
+67 -27
View File
@@ -12,6 +12,10 @@ pub struct Splash {
pub focus: SplashFocus,
}
pub(crate) fn splashes_dir(portraits: &Path) -> PathBuf {
portraits.join("splashes")
}
/// A single rename publishes both the original JPEG and its focus: key.x.y.jpg.
pub(crate) fn cached_splashes(dir: &Path) -> HashMap<String, Splash> {
file_names(dir)
@@ -36,19 +40,36 @@ pub(crate) fn cached_splashes(dir: &Path) -> HashMap<String, Splash> {
.collect()
}
enum LoadingArt {
Template(GrayImage),
NotFound,
}
impl Tracker {
/// Runs after the catalog refresh on the image worker, sharing one stream of save notifications.
pub fn refresh_champion_art(&self, on_attempt: &mut dyn FnMut(bool)) -> Result<(), Error> {
/// Notifies the worker when the catalog refresh ends (cached data is usable offline),
/// then reports each card and splash attempt while online.
pub fn refresh_champion_art(
&self,
catalog_ready: impl FnOnce(),
on_attempt: &mut dyn FnMut(bool),
) -> Result<(), Error> {
let catalog = self.refresh_champions();
catalog_ready();
catalog?;
let Some((source, portraits_dir)) = &self.champions else {
return Ok(());
};
let mut loading = HashMap::new();
if !self.download_cards(on_attempt, |key, image| {
loading.insert(key.to_owned(), loading_template(image));
let art = match image {
Some(image) => LoadingArt::Template(loading_template(image)),
None => LoadingArt::NotFound,
};
loading.insert(key.to_owned(), art);
})? {
return Ok(());
}
let dir = portraits_dir.join("splashes");
let dir = splashes_dir(portraits_dir);
std::fs::create_dir_all(&dir)
.map_err(|e| Error::Storage(format!("{} : {e}", dir.display())))?;
let cached = cached_splashes(&dir);
@@ -77,25 +98,30 @@ fn download_splash(
source: &dyn ChampionSource,
dir: &Path,
key: &str,
template: Option<GrayImage>,
loading: Option<LoadingArt>,
card: &Path,
) -> Result<bool, SourceError> {
let template = match template {
Some(template) => template,
let template = match loading {
Some(LoadingArt::Template(template)) => Some(template),
Some(LoadingArt::NotFound) => None,
// Cards from earlier launches only contain the cropped art.
None if card.is_file() => {
let Some(art) = decode_jpeg(&source.card_portrait(key)?) else {
return Ok(false);
};
loading_template(&art)
}
None if card.is_file() => match source.card_portrait(key) {
Ok(bytes) => {
let Some(art) = decode_jpeg(&bytes) else {
return Ok(false);
};
Some(loading_template(&art))
}
Err(SourceError::NotFound) => None,
Err(error) => return Err(error),
},
None => return Ok(false),
};
let jpeg = source.splash(key)?;
let Some(image) = decode_jpeg(&jpeg) else {
return Ok(false);
};
let focus = locate(&image, &template);
let focus = template.map_or(FALLBACK, |art| locate(&image, &art));
let path = dir.join(format!("{key}.{}.{}.jpg", focus.x, focus.y));
Ok(save_image(&path, &jpeg))
}
@@ -120,9 +146,13 @@ pub struct BannerFrame {
impl SplashFocus {
pub fn banner_frame(self, area_width: f32, area_height: f32) -> BannerFrame {
let scale = (area_width / (1215.0 * 0.8)).max(area_height / 717.0);
let width = 1215.0 * scale;
let height = 717.0 * scale;
const SPLASH_WIDTH: f32 = 1215.0;
const SPLASH_HEIGHT: f32 = 717.0;
const VISIBLE_WIDTH_FRACTION: f32 = 0.8;
let scale =
(area_width / (SPLASH_WIDTH * VISIBLE_WIDTH_FRACTION)).max(area_height / SPLASH_HEIGHT);
let width = SPLASH_WIDTH * scale;
let height = SPLASH_HEIGHT * scale;
let offset = |area: f32, size: f32, percent: u8| {
(area * 0.5 - size * f32::from(percent) / 100.0)
.max(area - size)
@@ -242,17 +272,27 @@ mod tests {
}
#[test]
fn locates_the_loading_art_even_with_different_brightness() {
let art = GrayImage::from_fn(35, 64, |x, y| {
Luma([((((x / 4) * 71) ^ ((y / 4) * 59)) % 150) as u8])
fn locates_full_size_loading_art_at_both_ends_of_the_scale_range() {
let art = image::RgbImage::from_fn(308, 560, |x, y| {
let value = ((((x / 32) * 71) ^ ((y / 32) * 59)) % 150) as u8;
image::Rgb([value, 150 - value, value / 2])
});
let mut splash = GrayImage::new(145, 80);
for (x, y, pixel) in art.enumerate_pixels() {
splash.put_pixel(x + 90, y + 9, Luma([pixel[0] + 70]));
let template = loading_template(&DynamicImage::ImageRgb8(art.clone()));
for (w, h, x, y, expected) in [
(276, 502, 735, 108, SplashFocus { x: 72, y: 36 }),
(394, 717, 180, 0, SplashFocus { x: 31, y: 30 }),
] {
let mut patch = imageops::resize(&art, w, h, FilterType::Triangle);
for pixel in patch.as_mut() {
*pixel += 60;
}
let mut splash = image::RgbImage::new(1215, 717);
imageops::replace(&mut splash, &patch, x, y);
let focus = locate(&DynamicImage::ImageRgb8(splash), &template);
assert!(
focus.x.abs_diff(expected.x) <= 1 && focus.y.abs_diff(expected.y) <= 1,
"{focus:?}, expected {expected:?}"
);
}
assert_eq!(
locate(&DynamicImage::ImageLuma8(splash), &art),
SplashFocus { x: 74, y: 35 }
);
}
}
+3 -2
View File
@@ -10,6 +10,7 @@ use crate::date::Date;
use crate::error::Error;
use crate::model::{Format, Match};
use crate::seasons::{Season, period};
use crate::splash::{Splash, cached_splashes, splashes_dir};
use crate::tracker::Tracker;
#[derive(Clone, Debug, PartialEq)]
@@ -62,7 +63,7 @@ pub struct ChampionRow {
pub portrait: Option<PathBuf>,
/// Only when the file exists in the portraits dir's `cards` folder.
pub card_portrait: Option<PathBuf>,
pub splash: Option<crate::Splash>,
pub splash: Option<Splash>,
// In scope (Season and Format filter):
pub matches: usize,
pub best_placement: Option<u8>,
@@ -452,7 +453,7 @@ impl Tracker {
// One read of each folder rather than a file check per champion and per portrait.
let squares: HashSet<_> = file_names(dir).collect();
let cards: HashSet<_> = file_names(&cards_dir(dir)).collect();
let mut splashes = crate::splash::cached_splashes(&dir.join("splashes"));
let mut splashes = cached_splashes(&splashes_dir(dir));
for row in &mut stats.rows {
row.splash = splashes.remove(&row.champion.key);
let name = format!("{}.png", row.champion.key);