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
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
use super::common::*;
use super::config::*;
use super::database::*;
use super::import_roms::{UnattendedMode, import_other};
use super::mimetype::*;
use super::model::*;
use super::progress::*;
use super::prompt::*;
use super::sevenzip::*;
use super::util::*;
use anyhow::{Context, Result, bail};
use clap::builder::PossibleValuesParser;
use clap::{Arg, ArgAction, ArgMatches, Command};
use indicatif::ProgressBar;
use num_traits::FromPrimitive;
use sqlx::sqlite::SqliteConnection;
use std::collections::HashSet;
use std::path::PathBuf;

const MERGING_STRATEGIES: &[&str] = &["SPLIT", "NON_MERGED", "FULL_NON_MERGED"];

pub fn subcommand() -> Command {
    Command::new("rebuild-roms")
        .about("Rebuild arcade ROM sets according to the selected strategy")
        .arg(
            Arg::new("MERGING")
                .short('m')
                .long("merging")
                .help("Set the arcade merging strategy")
                .required(false)
                .num_args(1)
                .value_parser(PossibleValuesParser::new(MERGING_STRATEGIES)),
        )
        .arg(
            Arg::new("FORCE")
                .short('f')
                .long("force")
                .help("Force rebuild even if merging strategy is unchanged")
                .required(false)
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("ALL")
                .short('a')
                .long("all")
                .help("Rebuild all arcade systems")
                .required(false)
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("YES")
                .short('y')
                .long("yes")
                .help("Automatically say yes to prompts")
                .required(false)
                .action(ArgAction::SetTrue),
        )
}

pub async fn main(
    connection: &mut SqliteConnection,
    matches: &ArgMatches,
    progress_bar: &ProgressBar,
) -> Result<()> {
    let systems =
        prompt_for_systems(connection, None, true, false, matches.get_flag("ALL")).await?;

    let merging = match matches.get_one::<String>("MERGING").map(String::as_str) {
        Some("SPLIT") => Merging::Split,
        Some("NON_MERGED") => Merging::NonMerged,
        Some("FULL_NON_MERGED") => Merging::FullNonMerged,
        Some(&_) | None => FromPrimitive::from_usize(select(
            MERGING_STRATEGIES,
            "Please select a merge strategy",
            None,
            None,
        )?)
        .unwrap(),
    };

    for system in systems {
        print_header(progress_bar, &format!("Processing \"{}\"", system.name));
        rebuild_system(
            connection,
            progress_bar,
            &system,
            merging,
            matches.get_flag("FORCE"),
        )
        .await?;
        print_separator(progress_bar);
    }

    Ok(())
}

async fn rebuild_system(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system: &System,
    merging: Merging,
    force: bool,
) -> Result<()> {
    start_action(progress_bar, None);

    let tmp_directory = create_tmp_directory(connection).await?;
    let partial_games = find_partial_games_by_system_id(connection, system.id).await;
    let missing_roms = find_roms_without_romfile_by_game_ids(
        connection,
        &partial_games
            .iter()
            .map(|game| game.id)
            .collect::<Vec<i64>>(),
    )
    .await;
    for missing_rom in missing_roms {
        if missing_rom.size == 0 {
            continue;
        }
        let mut existing_rom: Option<Rom> = None;
        if let Some(crc) = missing_rom.crc {
            let mut roms = find_roms_with_romfile_by_size_and_crc_and_system_id(
                connection,
                missing_rom.size,
                &crc,
                system.id,
            )
            .await;
            existing_rom = roms.pop();
        }
        if existing_rom.is_none()
            && let Some(md5) = missing_rom.md5
        {
            let mut roms = find_roms_with_romfile_by_size_and_md5_and_system_id(
                connection,
                missing_rom.size,
                &md5,
                system.id,
            )
            .await;
            existing_rom = roms.pop();
        }
        if existing_rom.is_none()
            && let Some(sha1) = missing_rom.sha1
        {
            let mut roms = find_roms_with_romfile_by_size_and_sha1_and_system_id(
                connection,
                missing_rom.size,
                &sha1,
                system.id,
            )
            .await;
            existing_rom = roms.pop();
        }
        if let Some(existing_rom) = existing_rom {
            let romfile = find_romfile_by_id(connection, existing_rom.romfile_id.unwrap())
                .await
                .as_common(connection)
                .await?;
            let mimetype = get_mimetype(&romfile.path).await?;
            let new_romfile = if mimetype.is_some()
                && ARCHIVE_EXTENSIONS.contains(&mimetype.as_ref().unwrap().extension())
            {
                romfile
                    .as_archive(progress_bar, Some(&existing_rom))
                    .await?
                    .first()
                    .unwrap()
                    .to_common(progress_bar, &tmp_directory)
                    .await?
            } else {
                romfile.copy(progress_bar, &tmp_directory, false).await?
            };
            import_other(
                connection,
                progress_bar,
                &Some(system),
                &None,
                &HashSet::from_iter(vec![missing_rom.game_id]),
                new_romfile,
                false,
                false,
                UnattendedMode::Skip,
            )
            .await?;
        }
    }
    compute_system_completion(connection, progress_bar, system).await?;

    if system.merging == merging as i64 && !force {
        print_skip(progress_bar, "Nothing to do");
        return Ok(());
    }

    let games = find_full_games_by_system_id(connection, system.id).await;

    if (system.merging == Merging::Split as i64 || system.merging == Merging::NonMerged as i64)
        && (merging == Merging::NonMerged || merging == Merging::FullNonMerged)
    {
        let compression =
            get_archive_compression(connection, &ArchiveType::Zip, Some(system.id)).await;
        for game in games {
            expand_game(
                connection,
                progress_bar,
                system,
                &game,
                merging,
                &compression,
            )
            .await?;
        }
    } else if (system.merging == Merging::NonMerged as i64
        || system.merging == Merging::FullNonMerged as i64)
        && (merging == Merging::Split || merging == Merging::NonMerged)
    {
        for game in games {
            trim_game(connection, progress_bar, system, &game, merging).await?;
        }
    }

    update_system_merging(connection, system.id, merging).await;
    compute_system_completion(connection, progress_bar, system).await?;

    Ok(())
}

async fn expand_game(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system: &System,
    game: &Game,
    merging: Merging,
    compression: &ArchiveCompression,
) -> Result<()> {
    print_subheader(progress_bar, &format!("Processing \"{}\"", game.name));
    let rom_directory = get_rom_directory(connection).await;
    let game_directory = get_system_directory(connection, system)
        .await?
        .join(&game.name);
    let game_archive_path = get_system_directory(connection, system)
        .await?
        .join(format!("{}.{}", game.name, ZIP_EXTENSION));
    let relative_game_archive_path = game_archive_path
        .strip_prefix(rom_directory)
        .context("Failed to retrieve relative path")?;
    let archive_romfile = find_romfile_by_path(
        connection,
        relative_game_archive_path.as_os_str().to_str().unwrap(),
    )
    .await;
    let mut roms = match merging {
        Merging::NonMerged => {
            find_roms_by_game_id_parents_no_parent_bioses(connection, game.id).await
        }
        Merging::FullNonMerged => find_roms_by_game_id_parents(connection, game.id).await,
        _ => bail!("Not possible"),
    };
    // skip CHDs
    roms.retain(|rom| !rom.disk);
    let mut transaction = begin_transaction(connection).await;
    for rom in roms.iter().filter(|rom| rom.romfile_id.is_none()) {
        // find source rom in parent
        let mut source_rom: Option<Rom> = None;
        if let Some(parent_id) = rom.parent_id {
            let parent_rom = find_rom_by_id(&mut transaction, parent_id).await;
            if parent_rom.romfile_id.is_some() {
                source_rom = Some(parent_rom);
            }
        };
        if let Some(source_rom) = source_rom {
            add_rom(
                &mut transaction,
                progress_bar,
                game,
                rom,
                &source_rom,
                &game_directory,
                &archive_romfile,
                compression,
            )
            .await?;
        } else {
            print_warning(progress_bar, &format!("Missing \"{}\"", rom.name));
            return Ok(());
        }
    }
    if let Some(romfile) = archive_romfile {
        romfile
            .as_common(&mut transaction)
            .await?
            .update(&mut transaction, progress_bar, romfile.id)
            .await?;
    }
    commit_transaction(transaction).await;
    Ok(())
}

async fn trim_game(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system: &System,
    game: &Game,
    merging: Merging,
) -> Result<()> {
    print_subheader(progress_bar, &format!("Processing \"{}\"", game.name));
    let rom_directory = get_rom_directory(connection).await;
    let romfile_path = get_system_directory(connection, system)
        .await?
        .join(format!("{}.{}", game.name, ZIP_EXTENSION));
    let relative_path = romfile_path
        .strip_prefix(rom_directory)
        .context("Failed to retrieve relative path")?;
    let romfile =
        find_romfile_by_path(connection, relative_path.as_os_str().to_str().unwrap()).await;
    let roms = match merging {
        Merging::Split => find_roms_by_game_id_parents_only(connection, game.id).await,
        Merging::NonMerged => find_roms_by_game_id_parent_bioses_only(connection, game.id).await,
        _ => bail!("Not possible"),
    };
    let mut transaction = begin_transaction(connection).await;
    for rom in roms.iter().filter(|rom| rom.romfile_id.is_some()) {
        delete_rom(&mut transaction, progress_bar, rom, &romfile).await?;
    }
    if let Some(romfile) = romfile {
        romfile
            .as_common(&mut transaction)
            .await?
            .update(&mut transaction, progress_bar, romfile.id)
            .await?;
    }
    commit_transaction(transaction).await;
    Ok(())
}

#[allow(clippy::too_many_arguments)]
async fn add_rom(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    game: &Game,
    rom: &Rom,
    source_rom: &Rom,
    destination_directory: &PathBuf,
    destination_archive_romfile: &Option<Romfile>,
    compression: &ArchiveCompression,
) -> Result<()> {
    let source_romfile = find_romfile_by_id(connection, source_rom.romfile_id.unwrap())
        .await
        .as_common(connection)
        .await?;
    if let Some(destination_archive_romfile) = destination_archive_romfile {
        if source_romfile.path.extension().unwrap() == ZIP_EXTENSION {
            // both source and destination are archives
            copy_files_between_archives(
                progress_bar,
                &source_romfile.path,
                &destination_archive_romfile
                    .as_common(connection)
                    .await?
                    .path,
                &[&source_rom.name],
                &[&rom.name],
            )
            .await?;
        } else {
            // source is file and destination is archive
            let archive_romfile = source_romfile
                .to_archive(
                    progress_bar,
                    &source_romfile.path.parent().unwrap(),
                    &destination_archive_romfile
                        .as_common(connection)
                        .await?
                        .path
                        .parent()
                        .unwrap(),
                    &game.name,
                    &ArchiveType::Zip,
                    compression,
                    false,
                )
                .await?;
            if source_rom.name != rom.name {
                archive_romfile.rename_file(progress_bar, &rom.name).await?;
            }
        }
        update_rom_romfile(connection, rom.id, Some(destination_archive_romfile.id)).await;
    } else if source_romfile.path.extension().unwrap() == ZIP_EXTENSION {
        // source is archive and destination is file
        let romfile_id = source_romfile
            .as_archive(progress_bar, Some(source_rom))
            .await?
            .first()
            .unwrap()
            .to_common(progress_bar, destination_directory)
            .await?
            .rename(progress_bar, &destination_directory.join(&rom.name), true)
            .await?
            .create(connection, progress_bar, RomfileType::Romfile)
            .await?;
        update_rom_romfile(connection, rom.id, Some(romfile_id)).await;
    } else {
        // source and destination are files
        let romfile_id = source_romfile
            .copy(progress_bar, destination_directory, false)
            .await?
            .create(connection, progress_bar, RomfileType::Romfile)
            .await?;
        update_rom_romfile(connection, rom.id, Some(romfile_id)).await;
    }
    Ok(())
}

async fn delete_rom(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    rom: &Rom,
    archive_romfile: &Option<Romfile>,
) -> Result<()> {
    if let Some(archive_romfile) = archive_romfile {
        archive_romfile
            .as_common(connection)
            .await?
            .as_archive(progress_bar, Some(rom))
            .await?
            .first()
            .unwrap()
            .delete_file(progress_bar)
            .await?;
    } else {
        find_romfile_by_id(connection, rom.romfile_id.unwrap())
            .await
            .as_common(connection)
            .await?
            .delete(progress_bar, false)
            .await?;
    }
    update_rom_romfile(connection, rom.id, None).await;
    Ok(())
}

#[cfg(test)]
mod test_full_non_merged_to_non_merged;
#[cfg(test)]
mod test_non_merged_to_split;
#[cfg(test)]
mod test_nothing_to_do;
#[cfg(test)]
mod test_split_to_full_non_merged;
#[cfg(test)]
mod test_split_to_non_merged;