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};
pub const DEFAULT_ONLINE_DB_URL: &str = "https://chdress.8101987.xyz/lib.db.zst";
pub const CACHE_TTL_SECONDS: u64 = 86_400;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OnlineDbEntry {
pub raw_title: String,
pub raw_serial: String,
pub normalized_serial: String,
pub clean_title: String,
pub comparison_title: String,
}
#[derive(Debug, Clone, Default)]
pub struct OnlineDatabase {
pub entries: Vec<OnlineDbEntry>,
by_normalized_serial: HashMap<String, Vec<usize>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OnlineMatch {
pub db_title: String,
pub db_serial: String,
pub clean_title: String,
pub suggested_filename: String,
pub score: u32,
}
impl OnlineDatabase {
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,
})
}
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())
}
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())
}
#[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,
})
}
}
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()
}
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)
}
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()
}
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
}
}
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
}
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
}