Skip to content

Commit 10c42c8

Browse files
fix(gui): select the g512 render and key metadata in the g513 depot (#2405)
* fix(gui): select the G512 photo and matching key metadata * fix(gui): fetch the manifest-selected keyboard metadata * refactor(gui): file the g512 manifest base in the depot sibling table candidate_manifest_bases hard-coded the g513 depot and the g512 base, a second copy of the fact DEPOT_SIBLINGS already owns. Give DepotSibling its manifest base and read it from the table, so the name correction and the variant lookup share one entry and one catalog-name match. --------- Co-authored-by: AprilNEA <37977109+AprilNEA@users.noreply.github.com>
1 parent 83fa1d7 commit 10c42c8

4 files changed

Lines changed: 150 additions & 30 deletions

File tree

‎crates/openlogi-desktop/src/services/assets.rs‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,9 @@ struct DepotSibling {
665665
/// The sibling's leading firmware-name word, which is also its display
666666
/// name.
667667
model: &'static str,
668+
/// The base the depot manifest files the sibling's variants under
669+
/// (`{manifest_base}_ext{N}`).
670+
manifest_base: &'static str,
668671
}
669672

670673
/// The `g513` depot (model id `c33c`) also serves the G512: its manifest
@@ -673,6 +676,7 @@ struct DepotSibling {
673676
const DEPOT_SIBLINGS: [DepotSibling; 1] = [DepotSibling {
674677
catalog_name: "G513",
675678
model: "G512",
679+
manifest_base: "g512",
676680
}];
677681

678682
/// Correct a shared depot's display name from the firmware name: either a
@@ -698,21 +702,30 @@ fn codename_model_words(codename: &str) -> impl Iterator<Item = &str> {
698702
})
699703
}
700704

705+
/// The [`DEPOT_SIBLINGS`] filed under the depot whose catalog `displayName`
706+
/// is `catalog_name`.
707+
fn depot_siblings(catalog_name: &str) -> impl Iterator<Item = &'static DepotSibling> {
708+
let catalog_name = catalog_name.trim();
709+
DEPOT_SIBLINGS
710+
.iter()
711+
.filter(move |sibling| catalog_name.eq_ignore_ascii_case(sibling.catalog_name))
712+
}
713+
701714
/// The [`DEPOT_SIBLINGS`] model whose depot is `catalog_name` and whose
702715
/// model word leads the firmware name.
703716
fn depot_sibling_name(catalog_name: &str, codename: &str) -> Option<&'static str> {
704717
let model = codename_model_words(codename).next()?;
705-
DEPOT_SIBLINGS
706-
.iter()
707-
.find(|sibling| {
708-
catalog_name
709-
.trim()
710-
.eq_ignore_ascii_case(sibling.catalog_name)
711-
&& model.eq_ignore_ascii_case(sibling.model)
712-
})
718+
depot_siblings(catalog_name)
719+
.find(|sibling| model.eq_ignore_ascii_case(sibling.model))
713720
.map(|sibling| sibling.model)
714721
}
715722

723+
/// The manifest bases of the [`DEPOT_SIBLINGS`] filed under the depot whose
724+
/// catalog `displayName` is `catalog_name`.
725+
fn depot_sibling_manifest_bases(catalog_name: &str) -> impl Iterator<Item = &'static str> {
726+
depot_siblings(catalog_name).map(|sibling| sibling.manifest_base)
727+
}
728+
716729
/// The catalog name without its qualifiers, for a firmware name that matches
717730
/// a nonempty catalog prefix where every remaining word is a recognized
718731
/// qualifier. Keeps catalog spelling, joining matched words with single

‎crates/openlogi-desktop/src/services/assets/images.rs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ where
9191
None
9292
}
9393

94-
/// Enumerate candidate manifest bases: model IDs, the depot name, and any stripped `_ext\d+` stem.
94+
/// Enumerate candidate manifest bases: model IDs, the depot name, any stripped `_ext\d+` stem,
95+
/// and the bases of products the catalog files under the same depot (`DEPOT_SIBLINGS`).
9596
pub(super) fn candidate_manifest_bases(
9697
entry: &openlogi_assets::DeviceEntry,
9798
depot: &str,
@@ -120,6 +121,16 @@ pub(super) fn candidate_manifest_bases(
120121
{
121122
candidates.push(stem.to_string());
122123
}
124+
// A sibling's variants keep their own base (`g512_ext6` in the G513
125+
// depot), tried after the depot's own bases.
126+
for base in super::depot_sibling_manifest_bases(&entry.display_name) {
127+
if !candidates
128+
.iter()
129+
.any(|c: &String| c.eq_ignore_ascii_case(base))
130+
{
131+
candidates.push(base.to_string());
132+
}
133+
}
123134
candidates
124135
}
125136

‎crates/openlogi-desktop/src/services/assets/sync.rs‎

Lines changed: 103 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,20 @@ fn sync_depot(
155155
// Baseline: hotspot metadata + manifest + hero render, in whichever
156156
// schema this depot ships (`*_core` or the bare names). Manifest is
157157
// fetched here so the variant lookup below has something to consult.
158-
for name in entry.baseline_files() {
158+
let mut fetched = entry.baseline_files();
159+
for name in &fetched {
159160
fetch_to_cache(client, &entry.asset_path, &dir, entry, name)?;
160161
}
161162

162163
// Dedicated buttons render — only present on devices whose manifest
163164
// points `device_buttons_image` at a distinct side view. Fetch it only
164165
// when the registry lists it so front-only devices don't 404; failure
165166
// is non-fatal (the GUI falls back to the hero render).
166-
if let Some(side) = entry.preferred_file(&BUTTONS_RENDER_FILES)
167-
&& let Err(e) = fetch_to_cache(client, &entry.asset_path, &dir, entry, side)
168-
{
169-
warn!(depot, error = %e, "buttons render fetch failed");
167+
if let Some(side) = entry.preferred_file(&BUTTONS_RENDER_FILES) {
168+
match fetch_to_cache(client, &entry.asset_path, &dir, entry, side) {
169+
Ok(()) => fetched.push(side),
170+
Err(e) => warn!(depot, error = %e, "buttons render fetch failed"),
171+
}
170172
}
171173

172174
// Optional second pass: download the manifest-mapped resources — the
@@ -189,16 +191,7 @@ fn sync_depot(
189191
else {
190192
continue;
191193
};
192-
if matches!(
193-
variant.as_str(),
194-
"front_core.png"
195-
| "front.png"
196-
| "side_core.png"
197-
| "side.png"
198-
| "core_metadata.json"
199-
| "metadata_full.json"
200-
| "metadata.json"
201-
) {
194+
if fetched.contains(&variant.as_str()) {
202195
continue;
203196
}
204197
if let Err(e) = fetch_to_cache(client, &entry.asset_path, &dir, entry, &variant) {
@@ -322,6 +315,101 @@ mod tests {
322315
use openlogi_assets::AssetSource;
323316
use openlogi_core::config::AssetSourcePreference;
324317

318+
#[test]
319+
fn g512_sync_fetches_metadata_not_in_the_baseline() {
320+
use std::io::{Read, Write};
321+
use std::net::{TcpListener, TcpStream};
322+
323+
let root = tempfile::tempdir().expect("asset root");
324+
let dir = root.path().join("g513");
325+
std::fs::create_dir(&dir).unwrap();
326+
let metadata = br#"{"images":[]}"#;
327+
let mut entry = openlogi_assets::DeviceEntry {
328+
model_id: "c33c".into(),
329+
model_ids: Vec::new(),
330+
display_name: "G513".into(),
331+
kind: "keyboard".into(),
332+
asset_path: "assets/g513/".into(),
333+
files: Vec::new(),
334+
};
335+
for (name, bytes) in [
336+
("manifest.json", br#"{"devices":[{"modelId":"g512_ext6","resources":[{"key":"image_metadata","src":"metadata.json"}]}]}"#.as_slice()),
337+
("metadata_full.json", br#"{"images":[]}"#.as_slice()),
338+
("metadata.json", metadata.as_slice()),
339+
] {
340+
entry.files.push(openlogi_assets::FileEntry {
341+
name: name.into(),
342+
sha256: openlogi_assets::http::sha256_hex(bytes),
343+
bytes: bytes.len() as u64,
344+
});
345+
if name != "metadata.json" {
346+
std::fs::write(dir.join(name), bytes).unwrap();
347+
}
348+
}
349+
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
350+
let address = listener.local_addr().unwrap();
351+
let server = std::thread::spawn(move || {
352+
let (mut stream, _) = listener.accept().unwrap();
353+
let mut request = Vec::new();
354+
let mut byte = [0];
355+
while stream.read(&mut byte).unwrap() != 0 {
356+
request.push(byte[0]);
357+
if request.ends_with(b"\r\n\r\n") {
358+
break;
359+
}
360+
}
361+
if !request.is_empty() {
362+
write!(
363+
stream,
364+
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
365+
metadata.len()
366+
)
367+
.unwrap();
368+
stream.write_all(metadata).unwrap();
369+
}
370+
request
371+
});
372+
let client = openlogi_assets::http::AssetClient::new(&format!("http://{address}"));
373+
super::sync_depot(&client, root.path(), "g513", &entry, 6).unwrap();
374+
// Release the server even when a regression skips the HTTP request.
375+
drop(TcpStream::connect(address));
376+
let request = server.join().unwrap();
377+
assert!(request.starts_with(b"GET /assets/g513/metadata.json "));
378+
assert_eq!(std::fs::read(dir.join("metadata.json")).unwrap(), metadata);
379+
}
380+
381+
#[test]
382+
fn g512_photo_and_metadata_are_selected_for_download() {
383+
let root = tempfile::tempdir().expect("asset root");
384+
let manifest = root.path().join("manifest.json");
385+
std::fs::write(
386+
&manifest,
387+
r#"{"devices":[{"modelId":"g512_ext6","resources":[{"key":"device_image","src":"front_ext6.png"},{"key":"image_metadata","src":"metadata.json"}]}]}"#,
388+
)
389+
.unwrap();
390+
let entry = openlogi_assets::DeviceEntry {
391+
model_id: "c33c".into(),
392+
model_ids: Vec::new(),
393+
display_name: "G513".into(),
394+
kind: "keyboard".into(),
395+
asset_path: "assets/g513/".into(),
396+
files: Vec::new(),
397+
};
398+
for (key, expected) in [
399+
("device_image", "front_ext6.png"),
400+
("image_metadata", "metadata.json"),
401+
] {
402+
assert_eq!(
403+
super::pick_variant_filename(&manifest, &entry, "g513", 6, key).as_deref(),
404+
Some(expected)
405+
);
406+
assert_eq!(
407+
super::pick_variant_filename(&manifest, &entry, "g513", 5, key),
408+
None
409+
);
410+
}
411+
}
412+
325413
#[test]
326414
fn automatic_preference_races_the_built_in_sources() {
327415
assert_eq!(

‎crates/openlogi-desktop/src/services/assets/tests.rs‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -621,17 +621,22 @@ fn g512_in_the_shared_g513_depot_shows_its_own_name() {
621621
let root = tempfile::tempdir().expect("create asset root");
622622
let depot = root.path().join("g513");
623623
std::fs::create_dir_all(&depot).unwrap();
624+
std::fs::write(depot.join("front.png"), png_header(100, 200)).unwrap();
625+
std::fs::write(depot.join("front_ext6.png"), png_header(300, 100)).unwrap();
624626
std::fs::write(
625-
depot.join("metadata.json"),
626-
r#"{"images":[{"key":"device_image","origin":{"width":100,"height":40}}]}"#,
627+
depot.join("manifest.json"),
628+
r#"{"devices":[{"modelId":"g512_ext6","resources":[{"key":"device_image","src":"front_ext6.png"},{"key":"image_metadata","src":"metadata_g512.json"}]}]}"#,
629+
)
630+
.unwrap();
631+
std::fs::write(
632+
depot.join("metadata_g512.json"),
633+
r#"{"images":[{"key":"device_image","origin":{"width":300,"height":100}}]}"#,
627634
)
628635
.unwrap();
629-
std::fs::write(depot.join("front.png"), png_header(100, 40)).unwrap();
630636
let resolver = resolver_over(&[root.path()], Some(g513_index()));
631637
let model = g512_model();
632638

633-
// One cached G513 depot serves both keyboards; each device's firmware
634-
// name decides only its own display name, never the artwork.
639+
// The same cached extended-model render serves both firmware names.
635640
for (codename, expected) in [
636641
(None, "G513"),
637642
(Some("G512 RGB MECHANICAL GAMING KEYBOARD"), "G512"),
@@ -641,7 +646,10 @@ fn g512_in_the_shared_g513_depot_shows_its_own_name() {
641646
let asset = resolver.resolve(&model, codename).expect("resolve c33c");
642647
assert_eq!(asset.display_name, expected, "codename={codename:?}");
643648
assert_eq!(asset.depot, "g513");
644-
assert_eq!(asset.image_path, depot.join("front.png"));
649+
assert_eq!(asset.image_path, depot.join("front_ext6.png"));
650+
assert_eq!(asset.hero_image_path, Some(depot.join("front_ext6.png")));
651+
assert_eq!((asset.png_width, asset.png_height), (300, 100));
652+
assert_eq!(asset.metadata.origin().expect("G512 metadata").width, 300);
645653
}
646654
assert_eq!(resolver.resolved.borrow().len(), 1, "artwork stays shared");
647655
}

0 commit comments

Comments
 (0)