oxyromon 0.23.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
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
use super::common::*;
use super::config::*;
use super::database::*;
use super::mimetype::*;
use super::model::*;
use super::progress::*;

use anyhow::{Context, Result, anyhow, bail};
use indicatif::ProgressBar;
use num_traits::FromPrimitive;
use rayon::prelude::*;
use regex::Regex;
use sqlx::sqlite::SqliteConnection;
use std::cmp::Ordering;
use std::ffi::OsStr;
use std::future::Future;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use tempfile::TempDir;
use tokio::fs;
use tokio::process::Command;
use tokio::fs::File;
use which::which;

static SYSTEM_NAME_REGEX: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^(Non-Redump - |Unofficial - )?([^()]+)( \(.*\))?$").unwrap());

pub async fn get_canonicalized_path<P: AsRef<Path>>(path: &P) -> Result<PathBuf> {
    let canonicalized_path = path.as_ref().canonicalize().with_context(|| {
        format!(
            "Failed to get canonicalized path for \"{}\"",
            path.as_ref().as_os_str().to_str().unwrap()
        )
    })?;
    Ok(canonicalized_path)
}

#[cfg(test)]
pub async fn open_file<P: AsRef<Path>>(path: &P) -> Result<File> {
    let file = File::open(path.as_ref()).await.with_context(|| {
        format!(
            "Failed to open \"{}\"",
            path.as_ref().as_os_str().to_str().unwrap()
        )
    })?;
    Ok(file)
}

pub fn open_file_sync<P: AsRef<Path>>(path: &P) -> Result<std::fs::File> {
    let file = std::fs::File::open(path.as_ref()).with_context(|| {
        format!(
            "Failed to open \"{}\"",
            path.as_ref().as_os_str().to_str().unwrap()
        )
    })?;
    Ok(file)
}

pub fn get_reader_sync<P: AsRef<Path>>(path: &P) -> Result<std::io::BufReader<std::fs::File>> {
    let f = open_file_sync(path)?;
    Ok(std::io::BufReader::new(f))
}

pub async fn create_file<P: AsRef<Path>>(
    progress_bar: &ProgressBar,
    path: &P,
    quiet: bool,
) -> Result<File> {
    if !quiet {
        print_action(
            progress_bar,
            &format!(
                "Creating \"{}\"",
                path.as_ref().as_os_str().to_str().unwrap()
            ),
        );
    }
    let directory = path.as_ref().parent().unwrap();
    if !directory.is_dir() {
        create_directory(progress_bar, &directory, quiet).await?;
    }
    let file = File::create(path).await.with_context(|| {
        format!(
            "Failed to create \"{}\"",
            path.as_ref().as_os_str().to_str().unwrap()
        )
    })?;
    Ok(file)
}

pub async fn copy_file<P: AsRef<Path>, Q: AsRef<Path>>(
    progress_bar: &ProgressBar,
    old_path: &P,
    new_path: &Q,
    quiet: bool,
) -> Result<()> {
    if old_path.as_ref() != new_path.as_ref() {
        let new_directory = new_path.as_ref().parent().unwrap();
        if !new_directory.is_dir() {
            create_directory(progress_bar, &new_directory, quiet).await?;
        }
        if !quiet {
            print_action(
                progress_bar,
                &format!(
                    "Copying to \"{}\"",
                    new_path.as_ref().as_os_str().to_str().unwrap()
                ),
            );
        }
        fs::copy(old_path, new_path).await.with_context(|| {
            format!(
                "Failed to copy \"{}\" to \"{}\"",
                old_path.as_ref().as_os_str().to_str().unwrap(),
                new_path.as_ref().as_os_str().to_str().unwrap()
            )
        })?;
    }
    Ok(())
}

pub async fn rename_file<P: AsRef<Path>, Q: AsRef<Path>>(
    progress_bar: &ProgressBar,
    old_path: &P,
    new_path: &Q,
    quiet: bool,
) -> Result<()> {
    if old_path.as_ref() != new_path.as_ref() {
        let new_directory = new_path.as_ref().parent().unwrap();
        if !new_directory.is_dir() {
            create_directory(progress_bar, &new_directory, quiet).await?;
        }
        if !quiet {
            print_action(
                progress_bar,
                &format!(
                    "Moving to \"{}\"",
                    new_path.as_ref().as_os_str().to_str().unwrap()
                ),
            );
        }
        let result = fs::rename(old_path, new_path).await;
        // rename doesn't work across filesystems, use copy/remove as fallback
        if result.is_err() {
            copy_file(progress_bar, old_path, new_path, quiet).await?;
            remove_file(progress_bar, old_path, quiet).await?;
        }
    }
    Ok(())
}

pub async fn remove_file<P: AsRef<Path>>(
    progress_bar: &ProgressBar,
    path: &P,
    quiet: bool,
) -> Result<()> {
    if !quiet {
        print_action(
            progress_bar,
            &format!(
                "Deleting \"{}\"",
                path.as_ref().as_os_str().to_str().unwrap()
            ),
        );
    }
    fs::remove_file(path).await.with_context(|| {
        format!(
            "Failed to delete \"{}\"",
            path.as_ref().as_os_str().to_str().unwrap()
        )
    })?;
    Ok(())
}

pub async fn create_directory<P: AsRef<Path>>(
    progress_bar: &ProgressBar,
    path: &P,
    quiet: bool,
) -> Result<()> {
    if !quiet {
        print_action(
            progress_bar,
            &format!(
                "Creating \"{}\"",
                path.as_ref().as_os_str().to_str().unwrap()
            ),
        );
    }
    if !path.as_ref().is_dir() {
        fs::create_dir_all(path).await.with_context(|| {
            format!(
                "Failed to create \"{}\"",
                path.as_ref().as_os_str().to_str().unwrap()
            )
        })?;
    }
    Ok(())
}

pub async fn create_tmp_directory(connection: &mut SqliteConnection) -> Result<TempDir> {
    let tmp_directory = TempDir::new_in(get_tmp_directory(connection).await)
        .context("Failed to create temp directory")?;
    Ok(tmp_directory)
}

pub async fn remove_directory<P: AsRef<Path>>(
    progress_bar: &ProgressBar,
    path: &P,
    quiet: bool,
) -> Result<()> {
    if !quiet {
        print_action(
            progress_bar,
            &format!(
                "Deleting \"{}\"",
                path.as_ref().as_os_str().to_str().unwrap()
            ),
        );
    }
    fs::remove_dir_all(path).await.with_context(|| {
        format!(
            "Failed to delete \"{}\"",
            path.as_ref().as_os_str().to_str().unwrap()
        )
    })?;
    Ok(())
}

pub async fn get_system_directory(
    connection: &mut SqliteConnection,
    system: &System,
) -> Result<PathBuf> {
    let system_name = match &system.custom_name {
        Some(custom_name) => {
            if get_bool(connection, "GROUP_SUBSYSTEMS", Some(system.id)).await {
                SYSTEM_NAME_REGEX
                    .captures(custom_name)
                    .unwrap()
                    .get(2)
                    .unwrap()
                    .as_str()
                    .to_owned()
            } else {
                custom_name.to_owned()
            }
        }
        None => {
            if get_bool(connection, "GROUP_SUBSYSTEMS", Some(system.id)).await {
                SYSTEM_NAME_REGEX
                    .captures(&system.name)
                    .unwrap()
                    .get(2)
                    .unwrap()
                    .as_str()
                    .to_owned()
            } else {
                system.name.trim().to_owned()
            }
        }
    };
    let rom_directory = match get_directory(connection, "ROM_DIRECTORY", Some(system.id)).await {
        Some(dir) => dir,
        None => get_rom_directory(connection).await,
    };
    let system_directory = rom_directory.join(system_name);
    Ok(system_directory)
}

pub async fn get_one_region_directory(
    connection: &mut SqliteConnection,
    system: &System,
) -> Result<PathBuf> {
    let one_region_directory = get_system_directory(connection, system).await?.join("1G1R");
    Ok(one_region_directory)
}

pub async fn get_trash_directory(
    connection: &mut SqliteConnection,
    system: Option<&System>,
) -> Result<PathBuf> {
    let trash_directory = match system {
        Some(system) => get_system_directory(connection, system)
            .await?
            .join("Trash"),
        None => get_rom_directory(connection).await.join("Trash"),
    };
    Ok(trash_directory)
}

/// Runs an external tool command, logging it and bailing with the tool's
/// stderr when it exits unsuccessfully.
pub async fn run_tool(command: &mut tokio::process::Command) -> Result<std::process::Output> {
    log::debug!("{:?}", command);
    let program = command.as_std().get_program().to_string_lossy().to_string();
    let output = command
        .output()
        .await
        .with_context(|| format!("Failed to spawn {}", program))?;
    if !output.status.success() {
        bail!("{}", String::from_utf8_lossy(&output.stderr));
    }
    Ok(output)
}

pub async fn tool_available<F, Fut>(check: F, name: &str, progress_bar: &ProgressBar) -> bool
where
    F: FnOnce() -> Fut,
    Fut: Future<Output = Result<String>>,
{
    match check().await {
        Ok(_) => true,
        Err(_) => {
            print_error(progress_bar, &format!("Required tool not found: {}", name));
            false
        }
    }
}

pub async fn tool_version(
    command: impl AsRef<OsStr>,
    name: &str,
    args: &[&str],
    stdout: bool,
    line: usize,
    regex: Option<&Regex>,
) -> Result<String> {
    let output = Command::new(command.as_ref())
        .args(args)
        .output()
        .await
        .context(format!("Failed to spawn {}", name))?;
    let text = if stdout {
        String::from_utf8(output.stdout).unwrap()
    } else {
        String::from_utf8(output.stderr).unwrap()
    };
    Ok(regex
        .and_then(|regex| text.lines().nth(line).and_then(|l| regex.find(l)))
        .map(|version| version.as_str().to_string())
        .unwrap_or(String::from("unknown")))
}

pub fn get_executable_path(executables: &[&str]) -> Result<PathBuf> {
    let path = executables
        .iter()
        .find_map(|executable| which(executable).ok());
    if let Some(path) = path {
        Ok(path)
    } else {
        Err(anyhow!("No executable in path"))
    }
}

pub fn is_update(progress_bar: &ProgressBar, old_version: &str, new_version: &str) -> bool {
    match new_version.cmp(old_version) {
        Ordering::Less => {
            print_skip(
                progress_bar,
                &format!(
                    "Skipping older version \"{}\" (current: \"{}\")",
                    new_version, old_version
                ),
            );
            false
        }
        Ordering::Equal => {
            print_skip(
                progress_bar,
                &format!("Already up to date (version \"{}\")", new_version),
            );
            false
        }
        Ordering::Greater => {
            print_info(
                progress_bar,
                &format!("Updating from \"{}\" to \"{}\"", old_version, new_version),
            );
            true
        }
    }
}

async fn create_missing_empty_files(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system: &System,
) -> Result<()> {
    let partial_games = find_partial_games_by_system_id(connection, system.id).await;

    for game in partial_games {
        let missing_roms = find_roms_without_romfile_by_game_ids(connection, &[game.id]).await;

        for rom in missing_roms {
            // Only create files for empty ROMs (size = 0)
            if rom.size == 0 && rom.crc.is_none() {
                // Create a temporary romfile to get the proper sorted path
                let tmp_path = create_tmp_directory(connection)
                    .await?
                    .path()
                    .join(&rom.name);
                create_file(progress_bar, &tmp_path, false).await?;
                let common_romfile = CommonRomfile::from_path(&tmp_path)?;
                common_romfile
                    .rename(
                        progress_bar,
                        &common_romfile
                            .get_sorted_path(connection, system, &game, &rom, &None, &None)
                            .await?,
                        false,
                    )
                    .await?
                    .create(connection, progress_bar, RomfileType::Romfile)
                    .await?;
            }
        }
    }
    Ok(())
}

pub async fn compute_system_completion(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system: &System,
) -> Result<()> {
    start_action(progress_bar, Some("Computing completion"));

    // Create missing empty files for partial games
    create_missing_empty_files(connection, progress_bar, system).await?;

    if system.arcade {
        let merging = Merging::from_i64(system.merging).unwrap();
        match merging {
            Merging::Split => {
                update_split_games_completion_by_system_id(connection, system.id).await;
            }
            Merging::NonMerged | Merging::Merged => {
                update_non_merged_and_merged_games_completion_by_system_id(connection, system.id)
                    .await;
            }
            Merging::FullNonMerged | Merging::FullMerged => {
                update_games_completion_by_system_id(connection, system.id).await;
            }
        }
    } else {
        update_games_completion_by_system_id(connection, system.id).await;
        update_jbfolder_games_completion_by_system_id(connection, system.id).await;
    }
    update_system_completion(connection, system.id).await;

    stop_action(progress_bar);

    Ok(())
}

pub async fn find_parent_chd_romfile_by_game(
    connection: &mut SqliteConnection,
    game: &Game,
) -> Option<Romfile> {
    let parent_game = match game.playlist_id {
        Some(playlist_id) => {
            let parent_game = find_first_game_by_playlist_id(connection, playlist_id).await;
            if parent_game.id != game.id {
                Some(parent_game)
            } else {
                None
            }
        }
        None => None,
    };
    match parent_game {
        Some(parent_game) => {
            let roms = find_roms_with_romfile_by_game_ids(connection, &[parent_game.id]).await;
            let mut romfile_ids = roms
                .into_par_iter()
                .map(|rom| rom.romfile_id.unwrap())
                .collect::<Vec<i64>>();
            romfile_ids.dedup();
            find_romfiles_by_ids(connection, &romfile_ids)
                .await
                .into_par_iter()
                .find_first(|romfile| romfile.path.ends_with(CHD_EXTENSION))
        }
        None => None,
    }
}

pub fn compute_alpha_subfolder(name: &str) -> String {
    let first_char = name.chars().next().unwrap();
    if first_char.is_ascii_alphabetic() {
        first_char.to_ascii_uppercase().to_string()
    } else {
        String::from("#")
    }
}

#[cfg(test)]
mod test_compute_alpha_subfolder;
#[cfg(test)]
mod test_system_directory_custom_name;
#[cfg(test)]
mod test_system_directory_group_non_redump;
#[cfg(test)]
mod test_system_directory_group_subsystems;
#[cfg(test)]
mod test_system_directory_no_group_subsystems;