oxyromon 0.24.0

Rusty ROM OrgaNizer
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
use super::database::*;
use super::import_dats::{import_dat, parse_dat};
use super::model::*;
use super::progress::*;
use super::prompt::*;
use super::util::*;
use anyhow::{Context, Result};
use cfg_if::cfg_if;
use clap::{Arg, ArgAction, ArgMatches, Command};
use indicatif::ProgressBar;
use phf::phf_map;
use quick_xml::de;
use rayon::prelude::*;
use sqlx::sqlite::SqliteConnection;
use std::collections::HashSet;
use std::io::Cursor;
use std::path::PathBuf;
use tokio::time::{Duration, sleep};
use zip::read::ZipArchive;

const NOINTRO_BASE_URL: &str = "https://datomatic.no-intro.org";
const NOINTRO_PROFILE_URL: &str = "/profile.xml";
const REDUMP_BASE_URL: &str = "http://redump.org";

pub const NOINTRO_SYSTEM_URL: &str = "www.no-intro.org";
pub const REDUMP_SYSTEM_URL: &str = "http://redump.org/";

cfg_if! {
    if #[cfg(test)] {
        static REDUMP_SYSTEMS_CODES: phf::Map<&str, &str> = phf_map! {
            "Test System" => "ts"
        };
    } else {
        static REDUMP_SYSTEMS_CODES: phf::Map<&str, &str> = phf_map! {
            "Acorn - Archimedes" => "arch",
            "Apple - Macintosh" => "mac",
            "Arcade - Konami - e-Amusement" => "kea",
            "Arcade - Konami - FireBeat" => "kfb",
            "Arcade - Konami - System GV" => "ksgv",
            "Arcade - Namco - Sega - Nintendo - Triforce" => "trf",
            "Arcade - Sega - Chihiro" => "chihiro",
            "Arcade - Sega - Lindbergh" => "lindbergh",
            "Arcade - Sega - Naomi" => "naomi",
            "Arcade - Sega - Naomi 2" => "naomi2",
            "Arcade - Sega - RingEdge" => "sre",
            "Arcade - Sega - RingEdge 2" => "sre2",
            "Atari - Jaguar CD Interactive Multimedia System" => "ajcd",
            "Bandai - Pippin" => "pippin",
            "Bandai - Playdia Quick Interactive System" => "qis",
            "Commodore - Amiga CD" => "acd",
            "Commodore - Amiga CD32" => "cd32",
            "Commodore - Amiga CDTV" => "cdtv",
            "Fujitsu - FM-Towns" => "fmt",
            "funworld - Photo Play" => "fpp",
            "IBM - PC compatible" => "pc",
            "Incredible Technologies - Eagle" => "ite",
            "Mattel - Fisher-Price iXL" => "ixl",
            "Mattel - HyperScan" => "hs",
            "Memorex - Visual Information System" => "vis",
            "Microsoft - Xbox" => "xbox",
            "Microsoft - Xbox 360" => "xbox360",
            "NEC - PC Engine CD & TurboGrafx CD" => "pce",
            "NEC - PC-88 series" => "pc-88",
            "NEC - PC-98 series" => "pc-98",
            "NEC - PC-FX & PC-FXGA" => "pc-fx",
            "Nintendo - GameCube" => "gc",
            "Nintendo - Wii" => "wii",
            "Palm" => "palm",
            "Panasonic - 3DO Interactive Multiplayer" => "3do",
            "Philips - CD-i" => "cdi",
            "Photo CD" => "photo-cd",
            "PlayStation GameShark Updates" => "psxgs",
            "Sega - Dreamcast" => "dc",
            "Sega - Mega CD & Sega CD" => "mcd",
            "Sega - Prologue 21" => "sp21",
            "Sega - Saturn" => "ss",
            "SNK - Neo Geo CD" => "ngcd",
            "Sony - PlayStation" => "psx",
            "Sony - PlayStation 2" => "ps2",
            "Sony - PlayStation 3" => "ps3",
            "Sony - PlayStation Portable" => "psp",
            "TAB-Austria - Quizard" => "quizard",
            "Tomy - Kiss-Site" => "ksite",
            "VM Labs - NUON" => "nuon",
            "VTech - V.Flash & V.Smile Pro" => "vflash",
            "ZAPiT Games - Game Wave Family Entertainment System" => "gamewave",
        };
    }
}

/// The Redump systems oxyromon knows how to fetch, sorted by name.
///
/// `update` picks which half of the catalogue is wanted: the systems already in
/// the database, whose DATs can be refreshed, or the ones that are not there
/// yet. Either way the answer is drawn from the catalogue, so a system imported
/// from a hand-downloaded Redump DAT under some other name is never offered for
/// an update oxyromon has no code for.
///
/// This is what `download-dats -r` offers interactively, with and without `-u`;
/// the server exposes the same lists so the web UI can offer them as a picker.
#[cfg(feature = "server")]
pub async fn find_redump_systems(connection: &mut SqliteConnection, update: bool) -> Vec<String> {
    let existing: HashSet<String> = find_systems_by_url(connection, REDUMP_SYSTEM_URL)
        .await
        .into_par_iter()
        .map(|system| system.name)
        .collect();
    let mut names: Vec<String> = REDUMP_SYSTEMS_CODES
        .keys()
        .filter(|name| existing.contains(**name) == update)
        .map(|name| name.to_string())
        .collect();
    names.sort_unstable();
    names
}

/// Fetch one Redump system's DAT and import it.
///
/// The name is checked against the catalogue first: unlike the CLI, which can
/// only offer names it already holds, callers here may pass anything.
#[cfg(feature = "server")]
pub async fn download_redump_system(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system_name: &str,
    force: bool,
) -> Result<()> {
    if !REDUMP_SYSTEMS_CODES.contains_key(system_name) {
        anyhow::bail!("Unknown Redump system \"{}\"", system_name);
    }
    download_redump_dat(
        connection,
        progress_bar,
        REDUMP_BASE_URL,
        system_name,
        force,
        None,
    )
    .await
}

pub fn subcommand() -> Command {
    Command::new("download-dats")
        .about("Download No-Intro and Redump DAT files and import them into oxyromon")
        .arg(
            Arg::new("NOINTRO")
                .short('n')
                .long("nointro")
                .help("Download No-Intro DAT files")
                .required(false)
                .action(ArgAction::SetTrue)
                .conflicts_with("REDUMP")
                .required_unless_present("REDUMP"),
        )
        .arg(
            Arg::new("REDUMP")
                .short('r')
                .long("redump")
                .help("Download Redump DAT files")
                .required(false)
                .action(ArgAction::SetTrue)
                .conflicts_with("NOINTRO")
                .required_unless_present("NOINTRO"),
        )
        .arg(
            Arg::new("UPDATE")
                .short('u')
                .long("update")
                .help("Check for system updates")
                .required(false)
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("ALL")
                .short('a')
                .long("all")
                .help("Import all systems")
                .required(false)
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("FORCE")
                .short('f')
                .long("force")
                .help("Force import of outdated DAT files")
                .required(false)
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("SAVE_DIRECTORY")
                .short('s')
                .long("save")
                .help("Save downloaded DAT files to the specified directory")
                .required(false)
                .value_name("DIRECTORY"),
        )
}

pub async fn main(
    connection: &mut SqliteConnection,
    matches: &ArgMatches,
    progress_bar: &ProgressBar,
) -> Result<()> {
    let save_directory = matches.get_one::<String>("SAVE_DIRECTORY");

    if matches.get_flag("NOINTRO") {
        if matches.get_flag("UPDATE") {
            update_nointro_dats(
                connection,
                progress_bar,
                NOINTRO_BASE_URL,
                matches.get_flag("ALL"),
            )
            .await?
        } else {
            print_warning(progress_bar, "Not supported");
        }
    } else if matches.get_flag("REDUMP") {
        if matches.get_flag("UPDATE") {
            update_redump_dats(
                connection,
                progress_bar,
                REDUMP_BASE_URL,
                matches.get_flag("ALL"),
                matches.get_flag("FORCE"),
                save_directory.map(|s| s.as_str()),
            )
            .await?
        } else {
            download_redump_dats(
                connection,
                progress_bar,
                REDUMP_BASE_URL,
                matches.get_flag("ALL"),
                save_directory.map(|s| s.as_str()),
            )
            .await?
        }
    }
    Ok(())
}

async fn update_nointro_dats(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    base_url: &str,
    all: bool,
) -> Result<()> {
    if let Ok(response) = reqwest::get(format!("{}{}", base_url, NOINTRO_PROFILE_URL))
        .await
        .expect("Failed to download No-Intro profiles")
        .text()
        .await
    {
        let profile: ProfileXml =
            de::from_str(&response).context("Failed to parse No-Intro profiles")?;
        let systems =
            prompt_for_systems(connection, Some(NOINTRO_SYSTEM_URL), false, false, all).await?;
        for system in systems {
            print_header(progress_bar, &format!("Processing \"{}\"", system.name));
            let system_xml = profile
                .systems
                .par_iter()
                .find_first(|system_xml| system_xml.name == system.name);
            match system_xml {
                Some(system_xml) => {
                    is_update(progress_bar, &system.version, &system_xml.version);
                }
                None => print_warning(progress_bar, "System is no longer available"),
            }
            print_separator(progress_bar);
        }
    }
    Ok(())
}

async fn download_redump_dats(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    base_url: &str,
    all: bool,
    save_directory: Option<&str>,
) -> Result<()> {
    let system_names: HashSet<String> = find_systems_by_url(connection, REDUMP_SYSTEM_URL)
        .await
        .into_par_iter()
        .map(|system| system.name)
        .collect();
    let mut items: Vec<&str> = REDUMP_SYSTEMS_CODES
        .keys()
        .copied()
        .filter(|&system_name| !system_names.contains(system_name))
        .collect();
    items.sort_unstable();
    let indices: Vec<usize> = if all {
        (0..items.len()).collect()
    } else {
        multiselect(&items, "Please select systems", None, None)?
    };
    for i in indices {
        download_redump_dat(
            connection,
            progress_bar,
            base_url,
            items.get(i).unwrap(),
            false,
            save_directory,
        )
        .await?;
    }
    Ok(())
}

async fn update_redump_dats(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    base_url: &str,
    all: bool,
    force: bool,
    save_directory: Option<&str>,
) -> Result<()> {
    let systems =
        prompt_for_systems(connection, Some(REDUMP_SYSTEM_URL), false, false, all).await?;
    for system in systems {
        download_redump_dat(
            connection,
            progress_bar,
            base_url,
            &system.name,
            force,
            save_directory,
        )
        .await?;
    }
    Ok(())
}

async fn download_redump_dat(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    base_url: &str,
    system_name: &str,
    force: bool,
    save_directory: Option<&str>,
) -> Result<()> {
    print_header(progress_bar, &format!("Processing \"{}\"", system_name));
    let code = *REDUMP_SYSTEMS_CODES.get(system_name).unwrap();
    let zip_url = format!("{}/datfile/{}/", base_url, code);
    match reqwest::get(zip_url)
        .await
        .expect("Failed to download ZIP")
        .bytes()
        .await
    {
        Ok(response) => {
            let tmp_directory = create_tmp_directory(connection).await?;
            let mut zip_archive =
                ZipArchive::new(Cursor::new(response)).context("Failed to read Redump ZIP")?;
            match zip_archive.len() {
                0 => print_warning(progress_bar, "ZIP is empty"),
                1 => {
                    zip_archive
                        .extract(&tmp_directory)
                        .context("Failed to extract ZIP")?;
                    let dat_file_name = zip_archive.file_names().next().unwrap();
                    let dat_file_path = tmp_directory.path().join(dat_file_name);

                    // Save DAT file to specified directory if requested
                    if let Some(save_directory) = save_directory {
                        let save_directory = PathBuf::from(save_directory);
                        create_directory(progress_bar, &save_directory, false).await?;
                        copy_file(
                            progress_bar,
                            &dat_file_path,
                            &save_directory.join(dat_file_name),
                            false,
                        )
                        .await?;
                    }

                    let (datfile_xml, detector_xml) =
                        parse_dat(progress_bar, &dat_file_path, true).await?;
                    import_dat(
                        connection,
                        progress_bar,
                        &datfile_xml,
                        &detector_xml,
                        None,
                        None,
                        force,
                    )
                    .await?;
                }
                _ => print_warning(progress_bar, "ZIP contains too many files"),
            }
        }
        _ => print_error(progress_bar, "Failed to download ZIP"),
    }
    // rate limit
    sleep(Duration::from_secs(1)).await;
    print_separator(progress_bar);
    Ok(())
}

#[cfg(test)]
mod test_nointro;
#[cfg(test)]
mod test_redump;
#[cfg(test)]
mod test_redump_failures;