vpxtool 0.28.1

Terminal based frontend and utilities for Visual Pinball
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
use chrono::{DateTime, Utc};
use log::info;
use rayon::prelude::*;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt::Debug;
use std::fs::Metadata;
use std::io::{BufReader, BufWriter, Read};
use std::sync::LazyLock;
use std::time::SystemTime;
use std::{
    ffi::OsStr,
    fs::{self, File},
    io,
    path::{Path, PathBuf},
};
use vpin::vpx;
use vpin::vpx::jsonmodel::json_to_info;
use vpin::vpx::tableinfo::TableInfo;
use walkdir::{DirEntry, FilterEntry, IntoIter, WalkDir};

use vpx::gamedata::GameData;

use crate::atomicwrite::atomic_write;

pub const DEFAULT_INDEX_FILE_NAME: &str = "vpxtool_index.json";

// Compile regexes once to avoid per-table startup cost while indexing large collections.
static LINE_WITH_CGAMENAME_REGEX: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(r#"(?i)cgamename\s*=\s*\"([^"\\]*(?:\\.[^"\\]*)*)\""#).unwrap()
});
static LINE_WITH_DOT_GAMENAME_REGEX: LazyLock<regex::Regex> = LazyLock::new(|| {
    regex::Regex::new(r#"(?i)\.gamename\s*=\s*\"([^"\\]*(?:\\.[^"\\]*)*)\""#).unwrap()
});
static LOADVPM_SUB_REGEX: LazyLock<regex::Regex> =
    LazyLock::new(|| regex::Regex::new(r#"sub\s*loadvpm"#).unwrap());

/// Introduced because we want full control over serialization
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct IndexedTableInfo {
    pub table_name: Option<String>,
    pub author_name: Option<String>,
    //pub screenshot: Option<Vec<u8>>,
    pub table_blurb: Option<String>,
    pub table_rules: Option<String>,
    pub author_email: Option<String>,
    pub release_date: Option<String>,
    pub table_save_rev: Option<String>,
    pub table_version: Option<String>,
    pub author_website: Option<String>,
    pub table_save_date: Option<String>,
    pub table_description: Option<String>,
    // serialized in alphabetical key order for stable diffs; the VPX insertion order
    // (defined in "GameStg/CustomInfoTags") is not preserved by the source HashMap
    pub properties: BTreeMap<String, String>,
}
impl From<TableInfo> for IndexedTableInfo {
    fn from(table_info: TableInfo) -> Self {
        IndexedTableInfo {
            table_name: table_info.table_name,
            author_name: table_info.author_name,
            //screenshot: table_info.screenshot, // TODO we might want to write this to a file next to the table?
            table_blurb: table_info.table_blurb,
            table_rules: table_info.table_rules,
            author_email: table_info.author_email,
            release_date: table_info.release_date,
            table_save_rev: table_info.table_save_rev,
            table_version: table_info.table_version,
            author_website: table_info.author_website,
            table_save_date: table_info.table_save_date,
            table_description: table_info.table_description,
            properties: table_info.properties.into_iter().collect(),
        }
    }
}

pub struct PathWithMetadata {
    pub path: PathBuf,
    pub last_modified: SystemTime,
}

#[derive(Clone, Copy, PartialEq, Debug, Eq, Ord, PartialOrd)]
pub struct IsoSystemTime(SystemTime);
impl From<SystemTime> for IsoSystemTime {
    fn from(system_time: SystemTime) -> Self {
        IsoSystemTime(system_time)
    }
}
impl From<IsoSystemTime> for SystemTime {
    fn from(iso_system_time: IsoSystemTime) -> Self {
        iso_system_time.0
    }
}
impl Serialize for IsoSystemTime {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let now: DateTime<Utc> = self.0.into();
        now.to_rfc3339().serialize(serializer)
    }
}
impl<'de> Deserialize<'de> for IsoSystemTime {
    fn deserialize<D>(deserializer: D) -> Result<IsoSystemTime, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        let dt = DateTime::parse_from_rfc3339(&s).map_err(serde::de::Error::custom)?;
        Ok(IsoSystemTime(dt.into()))
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct IndexedTable {
    pub path: PathBuf,
    pub table_info: IndexedTableInfo,
    pub game_name: Option<String>,
    pub b2s_path: Option<PathBuf>,
    /// The rom path, in the table folder or in the global pinmame roms folder
    rom_path: Option<PathBuf>,
    /// deprecated: only used for reading the old index format
    #[serde(skip_serializing_if = "Option::is_none")]
    local_rom_path: Option<PathBuf>,
    pub wheel_path: Option<PathBuf>,
    pub requires_pinmame: bool,
    pub last_modified: IsoSystemTime,
}

impl IndexedTable {
    pub fn rom_path(&self) -> Option<&PathBuf> {
        self.rom_path.as_ref().or(self.local_rom_path.as_ref())
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct TablesIndex {
    tables: HashMap<PathBuf, IndexedTable>,
}

impl TablesIndex {
    pub(crate) fn empty() -> TablesIndex {
        TablesIndex {
            tables: HashMap::new(),
        }
    }

    pub fn len(&self) -> usize {
        self.tables.len()
    }

    pub fn is_empty(&self) -> bool {
        self.tables.is_empty()
    }

    pub(crate) fn insert(&mut self, table: IndexedTable) {
        self.tables.insert(table.path.clone(), table);
    }

    pub fn insert_all(&mut self, new_tables: Vec<IndexedTable>) {
        for table in new_tables {
            self.insert(table);
        }
    }

    pub fn merge(&mut self, other: TablesIndex) {
        self.tables.extend(other.tables);
    }

    pub fn tables(&self) -> Vec<IndexedTable> {
        self.tables.values().cloned().collect()
    }

    pub(crate) fn should_index(&self, path_with_metadata: &PathWithMetadata) -> bool {
        // if exists with different last modified or missing
        match self.tables.get(&path_with_metadata.path) {
            Some(existing) => {
                let existing_last_modified: SystemTime = existing.last_modified.into();
                existing_last_modified != path_with_metadata.last_modified
            }
            None => true,
        }
    }

    pub(crate) fn remove_missing(&mut self, paths: &[PathWithMetadata]) -> usize {
        // create a hashset with the paths
        let len = self.tables.len();
        let paths_set: HashSet<PathBuf> = paths.iter().map(|p| p.path.clone()).collect();
        self.tables.retain(|path, _| paths_set.contains(path));
        len - self.tables.len()
    }
}

/// We prefer keeping a flat index instead of an object
#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct TablesIndexJson {
    tables: Vec<IndexedTable>,
}

impl From<TablesIndex> for TablesIndexJson {
    fn from(index: TablesIndex) -> Self {
        TablesIndexJson {
            tables: sort_tables(index.tables()),
        }
    }
}

impl From<&TablesIndex> for TablesIndexJson {
    fn from(table: &TablesIndex) -> Self {
        TablesIndexJson {
            tables: sort_tables(table.tables()),
        }
    }
}

fn sort_tables(mut tables: Vec<IndexedTable>) -> Vec<IndexedTable> {
    tables.sort_by(|a, b| {
        a.path
            .to_string_lossy()
            .to_lowercase()
            .cmp(&b.path.to_string_lossy().to_lowercase())
    });
    tables
}

impl From<TablesIndexJson> for TablesIndex {
    fn from(index: TablesIndexJson) -> Self {
        let mut tables = HashMap::new();
        for table in index.tables {
            tables.insert(table.path.clone(), table);
        }
        TablesIndex { tables }
    }
}

/// Returns all roms names lower case for the roms in the given folder
pub fn find_roms(rom_path: &Path) -> io::Result<HashMap<String, PathBuf>> {
    if !rom_path.exists() {
        return Ok(HashMap::new());
    }
    // TODO
    // TODO if there is an ini file for the table we might have to check locally for the rom
    //   currently only a standalone feature
    let mut roms = HashMap::new();
    // TODO is there a cleaner version like try_filter_map?
    let mut entries = fs::read_dir(rom_path)?;
    entries.try_for_each(|entry| {
        let dir_entry = entry?;
        let path = dir_entry.path();
        if path.is_file()
            && let Some("zip") = path.extension().and_then(OsStr::to_str)
        {
            let rom_name = path
                .file_stem()
                .unwrap()
                .to_str()
                .unwrap()
                .to_string()
                .to_lowercase();
            roms.insert(rom_name, path);
        }
        Ok::<(), io::Error>(())
    })?;
    Ok(roms)
}

pub fn find_vpx_files(
    recursive: bool,
    max_depth: Option<usize>,
    tables_path: &Path,
) -> io::Result<Vec<PathWithMetadata>> {
    if recursive {
        let mut vpx_paths = Vec::new();
        let mut entries = walk_dir_filtered(tables_path, max_depth);
        entries.try_for_each(|entry| {
            let dir_entry = entry?;
            if dir_entry.file_type().is_file() {
                let path = dir_entry.path();
                if let Some("vpx") = path.extension().and_then(OsStr::to_str) {
                    vpx_paths.push(path.to_path_buf());
                }
            }
            Ok::<(), io::Error>(())
        })?;
        vpx_paths
            .par_iter()
            .map(|path| {
                let last_modified = last_modified(path)?;
                Ok(PathWithMetadata {
                    path: path.clone(),
                    last_modified,
                })
            })
            .collect()
    } else {
        let mut vpx_files = Vec::new();
        // TODO is there a cleaner version like try_filter_map?
        let mut dirs = fs::read_dir(tables_path)?;
        dirs.try_for_each(|entry| {
            let dir_entry = entry?;
            if dir_entry.file_type()?.is_file() {
                let path = dir_entry.path();
                if let Some("vpx") = path.extension().and_then(OsStr::to_str) {
                    let last_modified = last_modified(&path)?;
                    vpx_files.push(PathWithMetadata {
                        path: path.to_path_buf(),
                        last_modified,
                    });
                }
            }
            Ok::<(), io::Error>(())
        })?;
        Ok(vpx_files)
    }
}

/// Walks the directory and filters out .git and __MACOSX folders
fn walk_dir_filtered(
    tables_path: &Path,
    max_depth: Option<usize>,
) -> FilterEntry<IntoIter, fn(&DirEntry) -> bool> {
    let mut walkdir = WalkDir::new(tables_path);
    if let Some(max_depth) = max_depth {
        walkdir = walkdir.max_depth(max_depth);
    }
    walkdir.into_iter().filter_entry(|entry| {
        if !entry.file_type().is_dir() {
            return true;
        }
        !matches!(entry.file_name().to_str(), Some(".git" | "__MACOSX"))
    })
}

pub trait Progress {
    fn set_length(&self, len: u64);
    fn set_position(&self, i: u64);
    fn finish_and_clear(&self);
}

pub struct VoidProgress;
impl Progress for VoidProgress {
    fn set_length(&self, _len: u64) {}
    fn set_position(&self, _i: u64) {}
    fn finish_and_clear(&self) {}
}

pub enum IndexError {
    FolderDoesNotExist(PathBuf),
    IoError(io::Error),
}
impl Debug for IndexError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IndexError::FolderDoesNotExist(path) => {
                write!(f, "Folder does not exist: {}", path.display())
            }
            IndexError::IoError(e) => write!(f, "IO error: {e}"),
        }
    }
}
impl From<IndexError> for io::Error {
    fn from(e: IndexError) -> io::Error {
        io::Error::other(format!("{e:?}"))
    }
}

impl From<io::Error> for IndexError {
    fn from(e: io::Error) -> Self {
        IndexError::IoError(e)
    }
}

/// Indexes all vpx files in the given folder and writes the index to a file.
/// Returns the index.
/// If the index file already exists, it will be read and updated.
/// If the index file does not exist, it will be created.
///
/// Arguments:
/// * `recursive`: if true, all subdirectories will be searched for vpx files.
/// * `tables_folder`: the folder to search for vpx files.
/// * `tables_index_path`: the path to the index file.
/// * `global_pinmame_path`: the path to the global pinmame folder. Eg ~/.pinmame/roms on *nix systems.
/// * `configured_pinmame_path`: the path to the local pinmame folder configured in the vpinball config.
/// * `progress`: lister for progress updates.
/// * `force_reindex`: a list of vpx files to reindex, even if they are not modified.
#[allow(clippy::too_many_arguments)]
pub fn index_folder(
    recursive: bool,
    max_depth: Option<usize>,
    tables_folder: &Path,
    tables_index_path: &Path,
    global_pinmame_path: Option<&Path>,
    configured_pinmame_path: Option<&Path>,
    progress: &impl Progress,
    force_reindex: Vec<PathBuf>,
) -> Result<TablesIndex, IndexError> {
    info!("Indexing {}", tables_folder.display());

    if !tables_folder.exists() {
        return Err(IndexError::FolderDoesNotExist(tables_folder.to_path_buf()));
    }

    let existing_index = read_index_json(tables_index_path)?;
    if let Some(index) = &existing_index {
        info!(
            "  Found existing index with {} tables at {}",
            index.tables.len(),
            tables_index_path.display()
        );
    }
    let mut index = existing_index.unwrap_or(TablesIndex::empty());

    let vpx_files = find_vpx_files(recursive, max_depth, tables_folder)?;
    info!("  Found {} tables", vpx_files.len());
    // remove files that are missing
    let removed_len = index.remove_missing(&vpx_files);
    info!("  {removed_len} missing tables have been removed");

    let tables_with_missing_rom = index
        .tables()
        .iter()
        .filter_map(|table| {
            table
                .rom_path()
                .filter(|rom_path| !rom_path.exists())
                .map(|_| table.path.clone())
        })
        .collect::<HashSet<PathBuf>>();
    info!(
        "  {} tables will be re-indexed because their rom is missing",
        tables_with_missing_rom.len()
    );

    // find files that are missing or have been modified
    let mut vpx_files_to_index = Vec::new();
    for vpx_file in vpx_files {
        if tables_with_missing_rom.contains(&vpx_file.path)
            || force_reindex.contains(&vpx_file.path)
            || index.should_index(&vpx_file)
        {
            vpx_files_to_index.push(vpx_file);
        }
    }

    info!("  {} tables need (re)indexing.", vpx_files_to_index.len());
    let vpx_files_with_table_info = index_vpx_files(
        vpx_files_to_index,
        global_pinmame_path,
        configured_pinmame_path,
        progress,
    )?;

    // add new files to index
    index.merge(vpx_files_with_table_info);

    // write the index to a file
    write_index_json(&index, tables_index_path)?;

    Ok(index)
}

/// Indexes all vpx files in the given folder and returns the index.
/// note: The index is unordered, so the order of the tables is not guaranteed.
///
/// Arguments:
/// * `vpx_files`: the vpx files to index.
/// * `global_roms_path`: the path to the global roms folder. Eg ~/.pinmame/roms on *nix systems.
/// * `pinmame_roms_path`: the path to the local pinmame roms folder configured in the vpinball config
/// * `progress`: lister for progress updates.
///
/// see https://github.com/francisdb/vpxtool/issues/526
pub fn index_vpx_files(
    vpx_files: Vec<PathWithMetadata>,
    global_pinmame_path: Option<&Path>,
    configured_pinmame_path: Option<&Path>,
    progress: &impl Progress,
) -> io::Result<TablesIndex> {
    let global_roms = global_pinmame_path
        .map(|pinmame_path| {
            let roms_path = pinmame_path.join("roms");
            find_roms(&roms_path)
        })
        .unwrap_or_else(|| Ok(HashMap::new()))?;

    let pinmame_roms_path = configured_pinmame_path.map(|p| p.join("roms").to_path_buf());

    let (progress_tx, progress_rx) = std::sync::mpsc::channel();

    progress.set_length(vpx_files.len() as u64);
    let index_thread = std::thread::spawn(move || {
        vpx_files
            .par_iter()
            .flat_map(|vpx_file| {
                let res = match index_vpx_file(vpx_file, pinmame_roms_path.as_deref(), &global_roms)
                {
                    Ok(indexed_table) => Some(indexed_table),
                    Err(e) => {
                        // TODO we want to return any failures instead of printing here
                        let warning =
                            format!("Not a valid vpx file {}: {}", vpx_file.path.display(), e);
                        println!("{warning}");
                        None
                    }
                };
                // We don't care if something fails, it's just progress reporting.
                let _ = progress_tx.send(1);
                res
            })
            .collect()
    });

    let mut finished = 0;
    // The sender is automatically closed when it goes out of scope, we can be sure
    // that this does not block forever.
    for i in progress_rx {
        finished += i;
        progress.set_position(finished);
    }

    let vpx_files_with_table_info = index_thread
        .join()
        .map_err(|e| io::Error::other(format!("{e:?}")))?;

    Ok(TablesIndex {
        tables: vpx_files_with_table_info,
    })
}

fn index_vpx_file(
    vpx_file_path: &PathWithMetadata,
    configured_roms_path: Option<&Path>,
    global_roms: &HashMap<String, PathBuf>,
) -> io::Result<(PathBuf, IndexedTable)> {
    let path = &vpx_file_path.path;
    let mut vpx_file = vpx::open(path)?;
    // if there's an .info.json file, we should use that instead of the info in the vpx file
    let info_file_path = path.with_extension("info.json");
    let table_info = if info_file_path.exists() {
        read_table_info_json(&info_file_path)
    } else {
        vpx_file.read_tableinfo()
    }?;
    let game_data = vpx_file.read_gamedata()?;
    let code = consider_sidecar_vbs(path, game_data)?;
    //  also this sidecar should be part of the cache key
    let game_name = extract_game_name(&code);
    let requires_pinmame = requires_pinmame(&code);
    let rom_path = find_local_rom_path(path, &game_name, configured_roms_path)?.or_else(|| {
        game_name
            .as_ref()
            .and_then(|game_name| global_roms.get(&game_name.to_lowercase()).cloned())
    });
    let b2s_path = find_b2s_path(path);
    let wheel_path = find_wheel_path(path);
    let last_modified = last_modified(path)?;
    let indexed_table_info = IndexedTableInfo::from(table_info);

    let indexed = IndexedTable {
        path: path.clone(),
        table_info: indexed_table_info,
        game_name,
        b2s_path,
        rom_path,
        local_rom_path: None,
        wheel_path,
        requires_pinmame,
        last_modified: IsoSystemTime(last_modified),
    };
    Ok((indexed.path.clone(), indexed))
}

pub fn get_romname_from_vpx(vpx_path: &Path) -> io::Result<Option<String>> {
    let mut vpx_file = vpx::open(vpx_path)?;
    let game_data = vpx_file.read_gamedata()?;
    let code = consider_sidecar_vbs(vpx_path, game_data)?;
    let game_name = extract_game_name(&code);
    let requires_pinmame = requires_pinmame(&code);
    if requires_pinmame {
        Ok(game_name)
    } else {
        Ok(None)
    }
}

fn read_table_info_json(info_file_path: &Path) -> io::Result<TableInfo> {
    let info_file = File::open(info_file_path)?;
    let reader = BufReader::new(info_file);
    let json = serde_json::from_reader(reader).map_err(|e| {
        io::Error::other(format!(
            "Failed to parse/read json {}: {}",
            info_file_path.display(),
            e
        ))
    })?;
    let (table_info, _custom_info_tags) = json_to_info(json, None)?;
    Ok(table_info)
}

/// Visual pinball always falls back to the [vpx_folder]/pinmame/roms folder,
/// even if the PinMAMEPath folder is configured in the vpinball config.
fn find_local_rom_path(
    vpx_file_path: &Path,
    game_name: &Option<String>,
    configured_roms_path: Option<&Path>,
) -> io::Result<Option<PathBuf>> {
    if let Some(game_name) = game_name {
        let rom_file_name = format!("{}.zip", game_name.to_lowercase());

        let pinmame_roms_path = if let Some(configured_roms_path) = configured_roms_path {
            let configured_roms_path = if configured_roms_path.is_relative() {
                vpx_file_path.parent().unwrap().join(configured_roms_path)
            } else {
                configured_roms_path.to_owned()
            };
            if configured_roms_path.exists() {
                configured_roms_path
            } else {
                vpx_file_path.parent().unwrap().join("pinmame").join("roms")
            }
        } else {
            vpx_file_path.parent().unwrap().join("pinmame").join("roms")
        };

        let rom_path = pinmame_roms_path.join(rom_file_name);
        return if rom_path.exists() {
            Ok(Some(rom_path.canonicalize()?))
        } else {
            Ok(None)
        };
    };
    Ok(None)
}

fn find_b2s_path(vpx_file_path: &Path) -> Option<PathBuf> {
    let b2s_file_name = format!(
        "{}.directb2s",
        vpx_file_path.file_stem().unwrap().to_string_lossy()
    );
    let b2s_path = vpx_file_path.parent().unwrap().join(b2s_file_name);
    if b2s_path.exists() {
        Some(b2s_path)
    } else {
        None
    }
}

/// Tries to find a wheel image for the given vpx file.
/// 2 locations are tried:
/// * ../wheels/<vpx_file_name>.png
/// * <vpx_file_name>.wheel.png
fn find_wheel_path(vpx_file_path: &Path) -> Option<PathBuf> {
    let wheel_file_name = format!(
        "wheels/{}.png",
        vpx_file_path.file_stem().unwrap().to_string_lossy()
    );
    let wheel_path = vpx_file_path.parent().unwrap().join(wheel_file_name);
    if wheel_path.exists() {
        return Some(wheel_path);
    }
    let wheel_path = vpx_file_path.with_extension("wheel.png");
    if wheel_path.exists() {
        return Some(wheel_path);
    }
    None
}

/// If there is a file with the same name and extension .vbs we pick that code
/// instead of the code in the vpx file.
///
/// TODO if this file changes the index entry is currently not invalidated
fn consider_sidecar_vbs(path: &Path, game_data: GameData) -> io::Result<String> {
    let vbs_path = path.with_extension("vbs");
    let code = if vbs_path.exists() {
        let vbs_file = File::open(vbs_path)?;
        let mut reader = BufReader::new(vbs_file);
        let mut code = String::new();
        reader.read_to_string(&mut code)?;
        code
    } else {
        game_data.code.string
    };
    Ok(code)
}

fn last_modified(path: &Path) -> io::Result<SystemTime> {
    let metadata: Metadata = path.metadata()?;
    metadata.modified()
}

pub fn write_index_json(indexed_tables: &TablesIndex, json_path: &Path) -> io::Result<()> {
    let indexed_tables_json: TablesIndexJson = indexed_tables.into();
    atomic_write(json_path, |file| {
        let mut writer = BufWriter::new(file);
        serde_json::to_writer_pretty(&mut writer, &indexed_tables_json)
            .map_err(io::Error::other)?;
        writer.into_inner().map_err(|e| e.into_error())?;
        Ok(())
    })
}

pub fn read_index_json(json_path: &Path) -> io::Result<Option<TablesIndex>> {
    if !json_path.exists() {
        return Ok(None);
    }
    let json_file = File::open(json_path)?;
    let reader = BufReader::new(json_file);
    match serde_json::from_reader::<_, TablesIndexJson>(reader) {
        Ok(indexed_tables_json) => {
            let indexed_tables: TablesIndex = indexed_tables_json.into();
            Ok(Some(indexed_tables))
        }
        Err(e) => {
            println!("Failed to parse index file, ignoring existing index. ({e})");
            Ok(None)
        }
    }
}

fn extract_game_name<S: AsRef<str>>(code: S) -> Option<String> {
    let unified = unify_line_endings(code.as_ref());
    unified
        .lines()
        // skip rows that start with ' or whitespace followed by '
        .filter(|line| !line.trim().starts_with('\''))
        .filter(|line| {
            let lower: String = line.to_owned().to_lowercase().trim().to_string();
            lower.contains("cgamename") || lower.contains(".gamename")
        })
        .flat_map(|line| {
            let caps = LINE_WITH_CGAMENAME_REGEX
                .captures(line)
                .or(LINE_WITH_DOT_GAMENAME_REGEX.captures(line))?;
            let first = caps.get(1)?;
            Some(first.as_str().to_string())
        })
        .next()
}

fn requires_pinmame<S: AsRef<str>>(code: S) -> bool {
    let unified = unify_line_endings(code.as_ref());
    let lower = unified.to_lowercase();
    lower
        .lines()
        .filter(|line| !line.trim().starts_with('\''))
        .any(|line| line.contains("loadvpm") && !LOADVPM_SUB_REGEX.is_match(line))
}

/// Some scripts contain only CR as line separator. Eg "Monte Carlo (Premier 1987) (10.7) 1.6.vpx"
/// Therefore we replace first all CRLF and then all leftover CR with LF
fn unify_line_endings(code: &str) -> String {
    code.replace("\r\n", "\n").replace('\r', "\n")
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use serde_json::json;
    use std::io::Write;
    use testdir::testdir;
    use vpin::vpx;

    #[test]
    fn test_index_vpx_files() -> io::Result<()> {
        // Test setup looks like this:
        // test_dir/
        // ├── test.vpx
        // ├── test2.vpx
        // ├── subdir
        // │   └── test3.vpx
        // ├── test3.vpx
        // ├── __MACOSX/
        // │   └── ignored.vpx
        // ├── .git/
        // │   └── ignored2.vpx
        // ├── pinmame/
        // │   └── roms/
        // │       └── testgamename.zip
        // global_pinmame/
        // ├── roms/
        // │   └── testgamename2.zip
        let global_pinmame_dir = testdir!().join("global_pinmame");
        fs::create_dir(&global_pinmame_dir)?;
        let global_roms_dir = global_pinmame_dir.join("roms");
        fs::create_dir(&global_roms_dir)?;
        let tables_dir = testdir!().join("tables");
        fs::create_dir(&tables_dir)?;
        let temp_dir = testdir!().join("temp");
        fs::create_dir(&temp_dir)?;
        // the next two folders should be ignored
        let macosx = tables_dir.join("__MACOSX");
        fs::create_dir(&macosx)?;
        File::create(macosx.join("ignored.vpx"))?;
        let git = tables_dir.join(".git");
        fs::create_dir(&git)?;
        File::create(git.join("ignored2.vpx"))?;
        fs::create_dir(tables_dir.join("subdir"))?;
        // actual vpx files to index
        let vpx_1_path = tables_dir.join("test.vpx");
        let vpx_2_path = tables_dir.join("test2.vpx");
        let vpx_3_path = tables_dir.join("subdir").join("test3.vpx");

        vpx::new_minimal_vpx(&vpx_1_path)?;
        let script1 = test_script(&temp_dir, "testgamename")?;
        vpx::importvbs(&vpx_1_path, Some(script1))?;
        // local rom
        let mut rom1_path_local = tables_dir
            .join("pinmame")
            .join("roms")
            .join("testgamename.zip");
        // recursively create dir
        fs::create_dir_all(rom1_path_local.parent().unwrap())?;
        File::create(&rom1_path_local)?;
        // this canonicalize makes a strange dir starting with //?/
        rom1_path_local = rom1_path_local.canonicalize()?;

        vpx::new_minimal_vpx(&vpx_2_path)?;
        let script2 = test_script(&temp_dir, "testgamename2")?;
        vpx::importvbs(&vpx_2_path, Some(script2))?;
        // global rom
        let rom2_path_global = global_roms_dir.join("testgamename2.zip");
        File::create(&rom2_path_global)?;

        vpx::new_minimal_vpx(&vpx_3_path)?;
        // no rom

        // let output = std::process::Command::new("tree")
        //     .arg(&tables_dir)
        //     .output()
        //     .expect("failed to execute process");
        // let output_str = String::from_utf8_lossy(&output.stdout);
        // println!("test_dir:\n{}", output_str);
        //
        // let output = std::process::Command::new("tree")
        //     .arg(&global_pinmame_dir)
        //     .output()
        //     .expect("failed to execute process");
        // let output_str = String::from_utf8_lossy(&output.stdout);
        // println!("global_pinmame_dir:\n{}", output_str);

        let vpx_files = find_vpx_files(true, None, &tables_dir)?;
        assert_eq!(vpx_files.len(), 3);
        let global_roms = find_roms(&global_roms_dir)?;
        assert_eq!(global_roms.len(), 1);
        let configured_roms_path = Some(PathBuf::from("./"));
        let indexed_tables = index_vpx_files(
            vpx_files,
            Some(&global_pinmame_dir),
            configured_roms_path.as_deref(),
            &VoidProgress,
        )?;
        assert_eq!(indexed_tables.tables.len(), 3);
        let table1 = indexed_tables
            .tables
            .get(&vpx_1_path)
            .expect("table1 not found");
        let table2 = indexed_tables
            .tables
            .get(&vpx_2_path)
            .expect("table2 not found");
        let table3 = indexed_tables
            .tables
            .get(&vpx_3_path)
            .expect("table3 not found");
        assert_eq!(table1.path, vpx_1_path);
        assert_eq!(table2.path, vpx_2_path);
        assert_eq!(table3.path, vpx_3_path);
        assert_eq!(table1.rom_path, Some(rom1_path_local.clone()));
        assert_eq!(table2.rom_path, Some(rom2_path_global.clone()));
        assert_eq!(table3.rom_path, None);
        Ok(())
    }

    #[test]
    fn test_index_to_json_is_sorted() -> io::Result<()> {
        let tables_dir = testdir!().join("tables");
        fs::create_dir(&tables_dir)?;
        let temp_dir = testdir!().join("temp");
        fs::create_dir(&temp_dir)?;

        // Create vpx files whose paths are not in alphabetical order to verify sort
        let vpx_z_path = tables_dir.join("z_table.vpx");
        let vpx_a_path = tables_dir.join("a_table.vpx");
        let vpx_m_path = tables_dir.join("m_table.vpx");

        vpx::new_minimal_vpx(&vpx_z_path)?;
        vpx::new_minimal_vpx(&vpx_a_path)?;
        vpx::new_minimal_vpx(&vpx_m_path)?;

        let vpx_files = find_vpx_files(true, None, &tables_dir)?;
        let indexed = index_vpx_files(vpx_files, None, None, &VoidProgress)?;

        let json: TablesIndexJson = indexed.into();
        let filenames: Vec<_> = json
            .tables
            .iter()
            .map(|t| t.path.file_name().unwrap().to_str().unwrap().to_string())
            .collect();
        assert_eq!(filenames, vec!["a_table.vpx", "m_table.vpx", "z_table.vpx"]);
        Ok(())
    }

    #[test]
    fn test_find_vpx_files_max_depth() -> io::Result<()> {
        let tables_dir = testdir!().join("tables");
        fs::create_dir(&tables_dir)?;
        let sub1 = tables_dir.join("sub1");
        fs::create_dir(&sub1)?;
        let sub2 = sub1.join("sub2");
        fs::create_dir(&sub2)?;

        let top_vpx = tables_dir.join("top.vpx");
        let mid_vpx = sub1.join("mid.vpx");
        let deep_vpx = sub2.join("deep.vpx");
        vpx::new_minimal_vpx(&top_vpx)?;
        vpx::new_minimal_vpx(&mid_vpx)?;
        vpx::new_minimal_vpx(&deep_vpx)?;

        fn names(files: &[PathWithMetadata]) -> Vec<String> {
            let mut v: Vec<_> = files
                .iter()
                .map(|f| f.path.file_name().unwrap().to_string_lossy().into_owned())
                .collect();
            v.sort();
            v
        }

        // None means unlimited - all three files found.
        assert_eq!(
            names(&find_vpx_files(true, None, &tables_dir)?),
            vec!["deep.vpx", "mid.vpx", "top.vpx"]
        );

        // Depth 1: only the top-level file.
        assert_eq!(
            names(&find_vpx_files(true, Some(1), &tables_dir)?),
            vec!["top.vpx"]
        );

        // Depth 2: top-level + one subdir level.
        assert_eq!(
            names(&find_vpx_files(true, Some(2), &tables_dir)?),
            vec!["mid.vpx", "top.vpx"]
        );

        // Depth 3: everything.
        assert_eq!(
            names(&find_vpx_files(true, Some(3), &tables_dir)?),
            vec!["deep.vpx", "mid.vpx", "top.vpx"]
        );

        // Depth 0 yields only the root dir entry, which is not a .vpx file.
        assert!(find_vpx_files(true, Some(0), &tables_dir)?.is_empty());

        Ok(())
    }

    fn test_script(temp_dir: &Path, game_name: &str) -> io::Result<PathBuf> {
        // write simple script in tempdir
        let script = format!(
            r#"
    Const cGameName = "{game_name}"
    Sub LoadVPM
    "#
        );
        let script_path = temp_dir.join(game_name).with_extension("vbs");
        let mut script_file = File::create(&script_path)?;
        script_file.write_all(script.as_bytes())?;
        Ok(script_path)
    }

    #[test]
    fn test_write_read_empty_array() -> io::Result<()> {
        let index = TablesIndex::empty();
        let test_dir = testdir!();
        let index_path = test_dir.join("test.json");
        // write empty json array using serde_json
        let json_file = File::create(&index_path)?;
        let json_object = json!({
            "tables": []
        });
        serde_json::to_writer_pretty(json_file, &json_object)?;
        let read = read_index_json(&index_path)?;
        assert_eq!(read, Some(index));
        Ok(())
    }

    #[test]
    fn test_write_read_invalid_file() -> io::Result<()> {
        let test_dir = testdir!();
        let index_path = test_dir.join("test.json");
        // write empty json array using serde_json
        let json_file = File::create(&index_path)?;
        // write garbage to file
        serde_json::to_writer_pretty(json_file, &"garbage")?;
        let read = read_index_json(&index_path)?;
        assert_eq!(read, None);
        Ok(())
    }

    #[test]
    fn test_write_read_empty_index() -> io::Result<()> {
        let index = TablesIndex::empty();
        let test_dir = testdir!();
        let index_path = test_dir.join("test.json");
        write_index_json(&index, &index_path)?;
        let read = read_index_json(&index_path)?;
        assert_eq!(read, Some(index));
        Ok(())
    }

    #[test]
    fn test_write_read_single_item_index() -> io::Result<()> {
        let mut index = TablesIndex::empty();
        index.insert(IndexedTable {
            path: PathBuf::from("test.vpx"),
            table_info: IndexedTableInfo {
                table_name: Some("test".to_string()),
                author_name: Some("test".to_string()),
                table_blurb: None,
                table_rules: None,
                author_email: None,
                release_date: None,
                table_save_rev: None,
                table_version: None,
                author_website: None,
                table_save_date: None,
                table_description: None,
                properties: BTreeMap::new(),
            },
            game_name: Some("testrom".to_string()),
            b2s_path: Some(PathBuf::from("test.b2s")),
            rom_path: Some(PathBuf::from("testrom.zip")),
            local_rom_path: None,
            wheel_path: Some(PathBuf::from("test.png")),
            requires_pinmame: true,
            last_modified: IsoSystemTime(SystemTime::UNIX_EPOCH),
        });
        let test_dir = testdir!();
        let index_path = test_dir.join("test.json");
        write_index_json(&index, &index_path)?;
        let read = read_index_json(&index_path)?;
        assert_eq!(read, Some(index));
        Ok(())
    }

    #[test]
    fn test_properties_serialized_in_alphabetical_order() -> io::Result<()> {
        let mut properties = BTreeMap::new();
        // Insert in reverse-alphabetical order to prove ordering is not insertion-driven.
        properties.insert("zebra".to_string(), "z".to_string());
        properties.insert("mango".to_string(), "m".to_string());
        properties.insert("apple".to_string(), "a".to_string());

        let mut index = TablesIndex::empty();
        index.insert(IndexedTable {
            path: PathBuf::from("test.vpx"),
            table_info: IndexedTableInfo {
                table_name: None,
                author_name: None,
                table_blurb: None,
                table_rules: None,
                author_email: None,
                release_date: None,
                table_save_rev: None,
                table_version: None,
                author_website: None,
                table_save_date: None,
                table_description: None,
                properties,
            },
            game_name: None,
            b2s_path: None,
            rom_path: None,
            local_rom_path: None,
            wheel_path: None,
            requires_pinmame: false,
            last_modified: IsoSystemTime::from(SystemTime::UNIX_EPOCH),
        });
        let test_dir = testdir!();
        let index_path = test_dir.join("test.json");
        write_index_json(&index, &index_path)?;

        // Check raw JSON key order is alphabetical.
        let json_str = std::fs::read_to_string(&index_path)?;
        let apple_pos = json_str.find("\"apple\"").expect("apple not found");
        let mango_pos = json_str.find("\"mango\"").expect("mango not found");
        let zebra_pos = json_str.find("\"zebra\"").expect("zebra not found");
        assert!(apple_pos < mango_pos, "apple should come before mango");
        assert!(mango_pos < zebra_pos, "mango should come before zebra");

        // Round-trip preserves all entries.
        let read = read_index_json(&index_path)?.expect("index missing after write");
        let keys: Vec<_> = read
            .tables()
            .into_iter()
            .next()
            .unwrap()
            .table_info
            .properties
            .keys()
            .cloned()
            .collect();
        assert_eq!(keys, vec!["apple", "mango", "zebra"]);
        Ok(())
    }

    #[test]
    fn test_read_index_missing() -> io::Result<()> {
        let index_path = PathBuf::from("missing_index_file.json");
        let read = read_index_json(&index_path)?;
        assert_eq!(read, None);
        Ok(())
    }

    #[test]
    fn test_extract_game_name() {
        let code = r#"
  Dim tableheight: tableheight = Table1.height

  Const cGameName="godzilla",UseSolenoids=2,UseLamps=1,UseGI=0, SCoin=""
  Const UseVPMModSol = True

"#
        .to_string();
        let game_name = extract_game_name(code);
        assert_eq!(game_name, Some("godzilla".to_string()));
    }

    #[test]
    fn test_extract_game_name_commented() {
        let code = r#"
  'Const cGameName = "commented"
  Const cGameName = "actual"
"#
        .to_string();
        let game_name = extract_game_name(code);
        assert_eq!(game_name, Some("actual".to_string()));
    }

    #[test]
    fn test_extract_game_name_spaced() {
        let code = r#"
  Const cGameName = "gg"
"#
        .to_string();
        let game_name = extract_game_name(code);
        assert_eq!(game_name, Some("gg".to_string()));
    }

    #[test]
    fn test_extract_game_name_casing() {
        let code = r#"
  const cgamenamE = "othercase"
"#
        .to_string();
        let game_name = extract_game_name(code);
        assert_eq!(game_name, Some("othercase".to_string()));
    }

    #[test]
    fn test_extract_game_name_uppercase_name() {
        let code = r#"
Const cGameName = "BOOM"
"#
        .to_string();
        let game_name = extract_game_name(code);
        assert_eq!(game_name, Some("BOOM".to_string()));
    }

    #[test]
    fn test_extract_game_name_with_underscore() {
        let code = r#"
Const cGameName="simp_a27",UseSolenoids=1,UseLamps=0,UseGI=0,SSolenoidOn="SolOn",SSolenoidOff="SolOff", SCoin="coin"

LoadVPM "01000200", "DE.VBS", 3.36
"#
            .to_string();
        let game_name = extract_game_name(code);
        assert_eq!(game_name, Some("simp_a27".to_string()));
    }

    #[test]
    fn test_extract_game_name_multidef_end() {
        let code = r#"
Const UseSolenoids=2,UseLamps=0,UseSync=1,UseGI=0,SCoin="coin",cGameName="barbwire"
"#
        .to_string();
        let game_name = extract_game_name(code);
        assert_eq!(game_name, Some("barbwire".to_string()));
    }

    /// https://github.com/francisdb/vpxtool/issues/203
    #[test]
    fn test_extract_game_name_in_controller() {
        let code = r#"
        Sub Gorgar_Init
    LoadLUT
	On Error Resume Next
	With Controller
	.GameName="grgar_l1"
        "#;
        let game_name = extract_game_name(code);
        assert_eq!(game_name, Some("grgar_l1".to_string()));
    }

    #[test]
    fn test_extract_game_name_dot_gamename_inline() {
        let code = r#"
    With Controller : .GameName = "inline_l1" : End With
"#;
        let game_name = extract_game_name(code);
        assert_eq!(game_name, Some("inline_l1".to_string()));
    }

    #[test]
    fn test_extract_game_name_2_line_dim() {
        let code = r#"
Dim cGameName
cGameName = "abv106"
"#
        .to_string();
        let game_name = extract_game_name(code);
        assert_eq!(game_name, Some("abv106".to_string()));
    }

    #[test]
    fn test_requires_pinmame() {
        let code = r#"#
  LoadVPM "01210000", "sys80.VBS", 3.1
"#
        .to_string();
        assert!(requires_pinmame(code));
    }

    #[test]
    fn test_requires_pinmame_other_casing() {
        let code = r#"
  loadVpm "01210000", \"sys80.VBS\", 3.1
"#
        .to_string();
        assert!(requires_pinmame(code));
    }

    #[test]
    fn test_requires_pinmame_not() {
        let code = r#"
Const cGameName = "GTB_4Square_1971"
"#
        .to_string();
        assert!(!requires_pinmame(code));
    }

    #[test]
    fn test_requires_pinmame_with_same_sub() {
        // got this from blood machines
        let code = r#"
Sub LoadVPM(VPMver, VBSfile, VBSver)
	LoadVBSFiles VPMver, VBSfile, VBSver
	LoadController("VPM")
End Sub
"#
        .to_string();
        assert!(!requires_pinmame(code));
    }

    #[test]
    fn test_requires_pinmame_comment() {
        // got this from blood machines
        let code = r#"
' VRRoom set based on RenderingMode
' Internal DMD in Desktop Mode, using a textbox (must be called before LoadVPM)
Dim UseVPMDMD, VRRoom, DesktopMode
If RenderingMode = 2 Then VRRoom = VRRoomChoice Else VRRoom = 0
"#
        .to_string();
        assert!(!requires_pinmame(code));
    }

    #[test]
    fn test_requires_pinmame_comment_and_used() {
        // got this from blood machines
        let code = r#"
Const SCoin="coin3",cCredits=""

LoadVPM "01210000","sys80.vbs",3.10

'Sub LoadVPM(VPMver, VBSfile, VBSver)
'	On Error Resume Next
"#
        .to_string();
        assert!(requires_pinmame(code));
    }

    #[test]
    fn test_requires_pinmame_cr_only_lines_and_commented_sub_loadvpm() {
        // This code was taken from "Monte Carlo (Premier 1987) (10.7) 1.6.vpx"
        let code =
            "LoadVPM \"01210000\", \"sys80.VBS\", 3.1\r\r'Sub LoadVPM(VPMver, VBSfile, VBSver)\r";
        assert!(requires_pinmame(code));
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_find_local_rom_path_relative_linux() {
        // On Batocera the PinMAMEPath is configured as ./
        // That gives us a roms path of ./roms
        let test_table_dir = testdir!();
        let vpx_path = test_table_dir.join("test.vpx");
        let expected_rom_path = test_table_dir.join("roms").join("testgamename.zip");
        fs::create_dir_all(expected_rom_path.parent().unwrap()).unwrap();
        File::create(&expected_rom_path).unwrap();

        let local_rom = find_local_rom_path(
            &vpx_path,
            &Some("testgamename".to_string()),
            Some(&PathBuf::from("./roms")),
        )
        .unwrap();
        assert_eq!(local_rom, Some(expected_rom_path));
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_find_local_rom_path_relative_not_found_linux() {
        // On Batocera the PinMAMEPath is configured as ./
        // That gives us a roms path of ./roms
        let test_table_dir = testdir!();
        let vpx_path = test_table_dir.join("test.vpx");
        let expected_rom_path = test_table_dir.join("roms").join("testgamename.zip");
        fs::create_dir_all(expected_rom_path.parent().unwrap()).unwrap();

        let local_rom = find_local_rom_path(
            &vpx_path,
            &Some("testgamename".to_string()),
            Some(&PathBuf::from("./roms")),
        )
        .unwrap();
        assert_eq!(local_rom, None);
    }

    fn make_indexed_table(path: &str, table_name: Option<&str>) -> IndexedTable {
        IndexedTable {
            path: PathBuf::from(path),
            table_info: IndexedTableInfo {
                table_name: table_name.map(|s| s.to_string()),
                author_name: None,
                table_blurb: None,
                table_rules: None,
                author_email: None,
                release_date: None,
                table_save_rev: None,
                table_version: None,
                author_website: None,
                table_save_date: None,
                table_description: None,
                properties: BTreeMap::new(),
            },
            game_name: None,
            b2s_path: None,
            rom_path: None,
            local_rom_path: None,
            wheel_path: None,
            requires_pinmame: false,
            last_modified: IsoSystemTime::from(SystemTime::UNIX_EPOCH),
        }
    }

    #[test]
    fn test_sort_tables_by_path_case_insensitive() {
        let tables = vec![
            make_indexed_table("dir/Zebra.vpx", Some("Alpha")),
            make_indexed_table("dir/apple.vpx", Some("Zeta")),
            make_indexed_table("dir/Mango.vpx", Some("Beta")),
        ];
        let sorted = sort_tables(tables);
        let paths: Vec<_> = sorted.iter().map(|t| t.path.to_str().unwrap()).collect();
        assert_eq!(
            paths,
            vec!["dir/apple.vpx", "dir/Mango.vpx", "dir/Zebra.vpx"]
        );
    }

    #[test]
    fn test_sort_tables_by_full_path_not_just_filename() {
        let tables = vec![
            make_indexed_table("z_dir/apple.vpx", None),
            make_indexed_table("a_dir/zebra.vpx", None),
        ];
        let sorted = sort_tables(tables);
        let paths: Vec<_> = sorted.iter().map(|t| t.path.to_str().unwrap()).collect();
        // a_dir sorts before z_dir even though apple < zebra doesn't matter
        assert_eq!(paths, vec!["a_dir/zebra.vpx", "z_dir/apple.vpx"]);
    }
}