disclady 0.4.0

Pure Rust library and CLI utility for optical and storage disc image heuristics, inspection, and format detection (CD, DVD, GDI, Hard Disk, LaserDisc, CHD).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
use std::collections::HashMap;
use std::fs;
use std::io::{BufRead, BufReader, Read};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};

use crate::types::{DiscDescriptor, DiscError, Platform};

/// Default URL for the compressed disc title and serial database.
pub const DEFAULT_ONLINE_DB_URL: &str = "https://chdress.8101987.xyz/lib.db.zst";

/// Cache lifetime in seconds before attempting to refresh from the network.
pub const CACHE_TTL_SECONDS: u64 = 86_400;

/// An individual record in the online database.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OnlineDbEntry {
    /// Raw title or file name from column 1.
    pub raw_title: String,
    /// Raw serial ID from column 2.
    pub raw_serial: String,
    /// Alphanumeric uppercase serial for fast indexing.
    pub normalized_serial: String,
    /// Clean title stripped of track suffixes and file extension.
    pub clean_title: String,
    /// Lowercase alphanumeric comparison string precomputed for fast token matching.
    pub comparison_title: String,
}

/// In-memory indexed database of game titles and serial IDs.
#[derive(Debug, Clone, Default)]
pub struct OnlineDatabase {
    pub entries: Vec<OnlineDbEntry>,
    by_normalized_serial: HashMap<String, Vec<usize>>,
}

/// Result of matching disc metadata against the online database.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OnlineMatch {
    /// Game title as recorded in the database.
    pub db_title: String,
    /// Serial ID as recorded in the database.
    pub db_serial: String,
    /// Cleaned title suitable for user display.
    pub clean_title: String,
    /// Suggested filename incorporating appropriate extension.
    pub suggested_filename: String,
    /// Match confidence score (higher is better).
    pub score: u32,
}

impl OnlineDatabase {
    /// Parses an uncompressed TSV reader (two tab-separated columns, no header).
    pub fn parse_tsv<R: Read>(reader: R) -> Result<Self, DiscError> {
        let buf_reader = BufReader::new(reader);
        let mut entries = Vec::new();
        let mut by_normalized_serial: HashMap<String, Vec<usize>> = HashMap::new();

        for line_res in buf_reader.lines() {
            let line = line_res.map_err(DiscError::Io)?;
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }

            let mut cols = trimmed.split('\t');
            let raw_title = match cols.next() {
                Some(t) => t.trim().to_string(),
                None => continue,
            };
            let raw_serial = match cols.next() {
                Some(s) => s.trim().to_string(),
                None => continue,
            };

            let normalized_serial = normalize_serial_key(&raw_serial);
            let (clean_title, _) = sanitize_track_title(&raw_title);
            let comparison_title = clean_title_for_comparison(&clean_title);

            let idx = entries.len();
            if !normalized_serial.is_empty() {
                by_normalized_serial
                    .entry(normalized_serial.clone())
                    .or_default()
                    .push(idx);
            }

            entries.push(OnlineDbEntry {
                raw_title,
                raw_serial,
                normalized_serial,
                clean_title,
                comparison_title,
            });
        }

        Ok(Self {
            entries,
            by_normalized_serial,
        })
    }

    /// Decompresses and parses a zstd-compressed stream.
    pub fn from_zstd_reader<R: Read>(reader: R) -> Result<Self, DiscError> {
        let decompressed = zstd::decode_all(reader).map_err(|e| {
            DiscError::OnlineError(format!("Failed to decompress zstd database: {e}"))
        })?;
        Self::parse_tsv(decompressed.as_slice())
    }

    /// Fetches the remote database via HTTP or loads from local cache.
    pub fn fetch_or_load_cached(url: Option<&str>) -> Result<Self, DiscError> {
        let target_url = url.unwrap_or(DEFAULT_ONLINE_DB_URL);
        let cache_path = get_cache_file_path();

        if let Some(ref path) = cache_path {
            if is_cache_fresh(path, CACHE_TTL_SECONDS) {
                if let Ok(file) = fs::File::open(path) {
                    if let Ok(db) = Self::from_zstd_reader(file) {
                        return Ok(db);
                    }
                }
            }
        }

        let resp = ureq::get(target_url).call().map_err(|e| {
            DiscError::OnlineError(format!(
                "Failed to download database from {target_url}: {e}"
            ))
        })?;

        let mut body_reader = resp.into_body().into_reader();
        let mut compressed_bytes = Vec::new();
        body_reader
            .read_to_end(&mut compressed_bytes)
            .map_err(DiscError::Io)?;

        if let Some(ref path) = cache_path {
            let _ = fs::write(path, &compressed_bytes);
        }

        Self::from_zstd_reader(compressed_bytes.as_slice())
    }

    /// Searches for the best matching entry for an inspected disc.
    #[must_use]
    pub fn find_best_match(
        &self,
        desc: &DiscDescriptor,
        input_path: Option<&Path>,
    ) -> Option<OnlineMatch> {
        let disc_id = desc.disc_id();
        let title = desc.title();
        let platform = desc.volume_info.as_ref().and_then(|v| v.platform);
        let path_stem = input_path.and_then(|p| p.file_stem().and_then(|s| s.to_str()));

        let norm_disc_id = disc_id.map(normalize_serial_key);
        let query_title_clean = title.map(clean_title_for_comparison);
        let path_stem_clean = path_stem.map(clean_title_for_comparison);

        let query_tokens: Vec<&str> = query_title_clean
            .as_deref()
            .map(|qt| {
                qt.split_whitespace()
                    .filter(|t| t.len() > 1 && !is_stop_word(t))
                    .collect()
            })
            .unwrap_or_default();

        let path_stem_tokens: Vec<&str> = path_stem_clean
            .as_deref()
            .map(|ps| {
                ps.split_whitespace()
                    .filter(|t| t.len() > 1 && !is_stop_word(t))
                    .collect()
            })
            .unwrap_or_default();

        let path_has_usa = path_stem_clean
            .as_deref()
            .is_some_and(|ps| ps.split_whitespace().any(|w| w == "usa" || w == "us"));
        let path_has_eur = path_stem_clean
            .as_deref()
            .is_some_and(|ps| ps.split_whitespace().any(|w| w == "europe" || w == "eur"));
        let path_has_jap = path_stem_clean
            .as_deref()
            .is_some_and(|ps| ps.split_whitespace().any(|w| w == "japan" || w == "jap"));

        let mut best_entry_idx = None;
        let mut best_score = 0u32;

        if let Some(ref norm_id) = norm_disc_id {
            if !norm_id.is_empty() {
                if let Some(indices) = self.by_normalized_serial.get(norm_id) {
                    for &idx in indices {
                        let entry = &self.entries[idx];
                        let mut score = 1000;

                        if let Some(ref qt) = query_title_clean {
                            if entry.comparison_title == *qt {
                                score += 300;
                            } else if entry.comparison_title.contains(qt)
                                || qt.contains(&entry.comparison_title)
                            {
                                score += 150;
                            }
                        }

                        if let Some(ref ps) = path_stem_clean {
                            if entry.comparison_title == *ps {
                                score += 200;
                            } else if entry.comparison_title.contains(ps)
                                || ps.contains(&entry.comparison_title)
                            {
                                score += 100;
                            }
                        }

                        if score > best_score {
                            best_score = score;
                            best_entry_idx = Some(idx);
                        }
                    }
                }
            }
        }

        if best_score < 1000 {
            for (idx, entry) in self.entries.iter().enumerate() {
                let mut score = 0u32;

                if let Some(ref norm_id) = norm_disc_id {
                    if !norm_id.is_empty()
                        && !entry.normalized_serial.is_empty()
                        && norm_id.len() >= 5
                        && entry.normalized_serial.len() >= 5
                    {
                        let (shorter, longer) = if norm_id.len() <= entry.normalized_serial.len() {
                            (norm_id.as_str(), entry.normalized_serial.as_str())
                        } else {
                            (entry.normalized_serial.as_str(), norm_id.as_str())
                        };
                        if longer.starts_with(shorter) {
                            score += 750;
                        }
                    }
                }

                if let Some(ref qt) = query_title_clean {
                    if !qt.is_empty() {
                        if entry.comparison_title == *qt {
                            score += 500;
                        } else if entry.comparison_title.contains(qt)
                            || qt.contains(&entry.comparison_title)
                        {
                            score += 350;
                        } else {
                            let overlap =
                                token_overlap_score(&query_tokens, &entry.comparison_title);
                            if overlap >= 200 {
                                score += overlap;
                            }
                        }
                    }
                }

                if let Some(ref ps) = path_stem_clean {
                    if !ps.is_empty() {
                        if entry.comparison_title == *ps {
                            score += 400;
                        } else if entry.comparison_title.contains(ps)
                            || ps.contains(&entry.comparison_title)
                        {
                            score += 250;
                        } else {
                            let overlap =
                                token_overlap_score(&path_stem_tokens, &entry.comparison_title);
                            if overlap >= 150 {
                                score += overlap;
                            }
                        }
                    }
                }

                if score > 0 {
                    if let Some(plat) = platform {
                        if is_platform_serial_compatible(plat, &entry.raw_serial) {
                            score += 50;
                        }
                    }
                    if path_has_usa || path_has_eur || path_has_jap {
                        let mut matches_region = false;
                        for w in entry.comparison_title.split_whitespace() {
                            if path_has_usa && (w == "usa" || w == "us") {
                                matches_region = true;
                                break;
                            }
                            if path_has_eur && (w == "europe" || w == "eur") {
                                matches_region = true;
                                break;
                            }
                            if path_has_jap && (w == "japan" || w == "jap") {
                                matches_region = true;
                                break;
                            }
                        }
                        if matches_region {
                            score += 80;
                        }
                    }
                }

                if score > best_score {
                    best_score = score;
                    best_entry_idx = Some(idx);
                }
            }
        }

        let best_idx = best_entry_idx.filter(|_| best_score >= 200)?;
        let entry = &self.entries[best_idx];
        let suggested_filename = suggest_filename(&entry.raw_title, input_path);

        Some(OnlineMatch {
            db_title: entry.raw_title.clone(),
            db_serial: entry.raw_serial.clone(),
            clean_title: entry.clean_title.clone(),
            suggested_filename,
            score: best_score,
        })
    }
}

/// Normalizes a serial number string into a clean uppercase alphanumeric token.
pub fn normalize_serial_key(s: &str) -> String {
    let clean = s.trim_start_matches(['/', '\\']);
    clean
        .chars()
        .filter(|c| c.is_ascii_alphanumeric())
        .collect::<String>()
        .to_ascii_uppercase()
}

/// Splits a raw title into a clean title stem (without track notation) and optional file extension.
pub fn sanitize_track_title(raw_title: &str) -> (String, Option<String>) {
    let (stem, ext) = if let Some(dot_pos) = raw_title.rfind('.') {
        let (s, e) = raw_title.split_at(dot_pos);
        if e.len() <= 5 && e[1..].chars().all(|c| c.is_ascii_alphanumeric()) {
            (s, Some(e.to_string()))
        } else {
            (raw_title, None)
        }
    } else {
        (raw_title, None)
    };

    let clean_stem = strip_track_pattern(stem);
    (clean_stem, ext)
}

/// Strips track indicators like ` (Track 01)`, ` (Track 1)`, ` (Track 02)` from a title stem.
fn strip_track_pattern(stem: &str) -> String {
    if let Some(pos) = stem.rfind("(Track ") {
        if let Some(end_pos) = stem[pos..].find(')') {
            let before = stem[..pos].trim_end();
            let after = stem[pos + end_pos + 1..].trim_start();
            let combined = if after.is_empty() {
                before.to_string()
            } else {
                format!("{before} {after}")
            };
            return combined;
        }
    }
    stem.trim().to_string()
}

/// Generates a suggested filename based on the database title and target disc format.
pub fn suggest_filename(raw_title: &str, input_path: Option<&Path>) -> String {
    let (clean_stem, db_ext) = sanitize_track_title(raw_title);

    let target_ext = input_path.and_then(|p| p.extension().and_then(|e| e.to_str()));

    if let Some(ext) = target_ext {
        format!("{clean_stem}.{ext}")
    } else if let Some(ref ext) = db_ext {
        if ext.eq_ignore_ascii_case(".bin") {
            format!("{clean_stem}.cue")
        } else {
            format!("{clean_stem}{ext}")
        }
    } else {
        clean_stem
    }
}

/// Cleans a title for case-insensitive and punctuation-agnostic token comparison.
fn clean_title_for_comparison(title: &str) -> String {
    let mut out = String::with_capacity(title.len());
    let mut prev_is_space = true;
    for c in title.chars() {
        if c.is_ascii_alphanumeric() {
            out.push(c.to_ascii_lowercase());
            prev_is_space = false;
        } else if c.is_whitespace() && !prev_is_space {
            out.push(' ');
            prev_is_space = true;
        }
    }
    if prev_is_space && !out.is_empty() {
        out.pop();
    }
    out
}

/// Calculates token overlap score between precomputed query tokens and a cleaned candidate string.
fn token_overlap_score(query_tokens: &[&str], candidate_clean: &str) -> u32 {
    if query_tokens.is_empty() {
        return 0;
    }

    let mut cand_count = 0usize;
    let mut matches = 0usize;
    for cand_tok in candidate_clean
        .split_whitespace()
        .filter(|t| t.len() > 1 && !is_stop_word(t))
    {
        cand_count += 1;
        if query_tokens.contains(&cand_tok) {
            matches += 1;
        }
    }

    if matches == 0 || cand_count == 0 {
        return 0;
    }

    let max_len = query_tokens.len().max(cand_count);
    let score = (matches as u64)
        .saturating_mul(300)
        .checked_div(max_len as u64)
        .unwrap_or(0);
    u32::try_from(score).unwrap_or(u32::MAX)
}

fn is_stop_word(word: &str) -> bool {
    matches!(
        word,
        "the" | "a" | "an" | "and" | "or" | "of" | "in" | "on" | "at" | "to" | "for" | "is" | "by"
    )
}

fn starts_with_ignore_ascii_case(s: &str, prefix: &str) -> bool {
    s.get(..prefix.len())
        .is_some_and(|p| p.eq_ignore_ascii_case(prefix))
}

fn is_platform_serial_compatible(platform: Platform, raw_serial: &str) -> bool {
    let s = raw_serial;
    match platform {
        Platform::PlayStation1 => {
            starts_with_ignore_ascii_case(s, "SLUS")
                || starts_with_ignore_ascii_case(s, "SCUS")
                || starts_with_ignore_ascii_case(s, "SLES")
                || starts_with_ignore_ascii_case(s, "SCES")
                || starts_with_ignore_ascii_case(s, "SLPS")
                || starts_with_ignore_ascii_case(s, "SLPM")
                || starts_with_ignore_ascii_case(s, "SCPS")
                || starts_with_ignore_ascii_case(s, "SLED")
        }
        Platform::PlayStation2 => {
            starts_with_ignore_ascii_case(s, "SLUS")
                || starts_with_ignore_ascii_case(s, "SCUS")
                || starts_with_ignore_ascii_case(s, "SLES")
                || starts_with_ignore_ascii_case(s, "SCES")
                || starts_with_ignore_ascii_case(s, "SLPM")
                || starts_with_ignore_ascii_case(s, "SLPS")
                || starts_with_ignore_ascii_case(s, "SCPS")
        }
        Platform::PlayStationPortable => {
            starts_with_ignore_ascii_case(s, "UCUS")
                || starts_with_ignore_ascii_case(s, "UCES")
                || starts_with_ignore_ascii_case(s, "ULUS")
                || starts_with_ignore_ascii_case(s, "ULES")
                || starts_with_ignore_ascii_case(s, "ULJM")
                || starts_with_ignore_ascii_case(s, "UCJS")
                || starts_with_ignore_ascii_case(s, "NPUG")
        }
        Platform::Dreamcast => {
            starts_with_ignore_ascii_case(s, "T-")
                || starts_with_ignore_ascii_case(s, "HDR-")
                || starts_with_ignore_ascii_case(s, "MK-")
        }
        Platform::SegaSaturn => {
            starts_with_ignore_ascii_case(s, "T-")
                || starts_with_ignore_ascii_case(s, "GS-")
                || starts_with_ignore_ascii_case(s, "MK-")
        }
        Platform::MegaCd => {
            starts_with_ignore_ascii_case(s, "T-")
                || starts_with_ignore_ascii_case(s, "MK-")
                || starts_with_ignore_ascii_case(s, "G-")
        }
        Platform::ThreeDo => {
            starts_with_ignore_ascii_case(s, "FZ-")
                || starts_with_ignore_ascii_case(s, "E3D")
                || s.get(..4).is_some_and(|p| p.eq_ignore_ascii_case("ALON"))
        }
        Platform::NeoGeoCd => {
            starts_with_ignore_ascii_case(s, "NGCD") || starts_with_ignore_ascii_case(s, "NGH")
        }
        Platform::PcEngineCd => {
            starts_with_ignore_ascii_case(s, "TGXCD")
                || starts_with_ignore_ascii_case(s, "HCD")
                || starts_with_ignore_ascii_case(s, "HECD")
        }
        Platform::PcFx => starts_with_ignore_ascii_case(s, "FX"),
        Platform::AmigaCd32 => {
            starts_with_ignore_ascii_case(s, "CD32")
                || starts_with_ignore_ascii_case(s, "/DP")
                || starts_with_ignore_ascii_case(s, "SSI")
        }
        Platform::GameCube | Platform::Wii => {
            s.len() == 6 && s.chars().all(|c| c.is_ascii_graphic())
        }
        _ => true,
    }
}

fn get_cache_file_path() -> Option<PathBuf> {
    Some(std::env::temp_dir().join("disclady_lib.db.zst"))
}

fn is_cache_fresh(path: &Path, ttl_seconds: u64) -> bool {
    if let Ok(meta) = fs::metadata(path) {
        if meta.len() == 0 {
            return false;
        }
        if let Ok(modified) = meta.modified() {
            if let Ok(elapsed) = SystemTime::now().duration_since(modified) {
                return elapsed < Duration::from_secs(ttl_seconds);
            }
        }
    }
    false
}