vpin 0.23.5

Rust library for working with Visual Pinball VPX files
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
//! Library for reading and writing [Visual Pinball](https://github.com/vpinball/vpinball/) `vpx` files and working with exploded vpx directories.
//!
//! # Example
//!
//! ```no_run
//! use std::io;
//! use std::path::PathBuf;
//! use vpin::vpx::{read, version};
//!
//! let path = PathBuf::from("testdata/completely_blank_table_10_7_4.vpx");
//! let vpx = read(&path).unwrap();
//! println!("version: {}", vpx.version);
//! println!("table name: {}", vpx.info.table_name.unwrap_or("unknown".to_string()));
//! ```
//!

use ::image::ImageFormat;
use std::fs::OpenOptions;
use std::io::{self, Error, Read, Seek, Write};
use std::path::MAIN_SEPARATOR_STR;
use std::{
    fs::File,
    path::{Path, PathBuf},
};

use crate::vpx::biff::BiffReader;
use cfb::CompoundFile;
use log::{debug, info, warn};
use md2::{Digest, Md2};
use tracing::{info_span, instrument};

use crate::vpx::image::{ImageDataJpeg, vpx_image_to_dynamic_image};
use crate::vpx::tableinfo::read_tableinfo;
use tableinfo::{TableInfo, write_tableinfo};
use version::Version;

use self::biff::{BiffRead, BiffWrite, BiffWriter};
use self::collection::Collection;
use self::custominfotags::CustomInfoTags;
use self::font::FontData;
use self::gamedata::GameData;
use self::gameitem::GameItemEnum;
use self::image::ImageData;
use self::sound::SoundData;
use self::version::{read_version, write_version};

pub mod biff;
pub mod collection;
pub mod color;
pub mod custominfotags;
pub mod expanded;
pub mod font;
pub mod gamedata;
pub mod gameitem;
pub mod image;
pub mod jsonmodel;
pub mod math;
pub mod model;
pub mod sound;
pub mod tableinfo;
pub mod units;
pub mod version;

pub(crate) mod utf16;

pub mod material;

pub mod renderprobe;

pub(crate) mod json;

// we have to make this public for the integration tests
pub mod export;
mod gltf;
pub mod lzw;
mod mesh;
mod obj;
pub(crate) mod wav;

/// In-memory representation of a VPX file
///
/// *We guarantee an exact copy when reading and writing this. Exact as in the same structure and data, the underlying compound file will be a bit different on the binary level.*
///
/// # Example
///
/// ```no_run
/// use std::io;
/// use std::path::PathBuf;
/// use vpin::vpx::{read, version};
///
/// let path = PathBuf::from("testdata/completely_blank_table_10_7_4.vpx");
/// let vpx = read(&path).unwrap();
/// println!("version: {}", vpx.version);
/// println!("table name: {}", vpx.info.table_name.unwrap_or("unknown".to_string()));
/// ```

#[derive(Debug, PartialEq, Default)]
pub struct VPX {
    /// This is mainly here to have an ordering for custom info tags
    pub custominfotags: CustomInfoTags, // this is a bit redundant
    pub info: TableInfo,
    pub version: Version,
    pub gamedata: GameData,
    pub gameitems: Vec<GameItemEnum>,
    pub images: Vec<ImageData>,
    pub sounds: Vec<SoundData>,
    pub fonts: Vec<FontData>,
    pub collections: Vec<Collection>,
}

pub enum AddImageResult {
    Added,
    Replaced(Box<ImageData>),
}

impl VPX {
    pub fn add_game_item(&mut self, item: GameItemEnum) -> &Self {
        self.gameitems.push(item);
        self.gamedata.gameitems_size = self.gameitems.len() as u32;
        self
    }

    pub fn set_script(&mut self, script: String) -> &Self {
        self.gamedata.set_code(script);
        self
    }

    pub fn add_or_replace_image(&mut self, image: ImageData) -> AddImageResult {
        // make sure there is a unique name
        let existing_pos = self
            .images
            .iter()
            .position(|i| i.name.eq_ignore_ascii_case(&image.name));
        match existing_pos {
            Some(pos) => {
                let existing = self.images[pos].clone();
                self.images[pos] = image;
                AddImageResult::Replaced(Box::new(existing))
            }
            None => {
                self.gamedata.images_size += 1;
                self.images.push(image);
                AddImageResult::Added
            }
        }
    }
}

#[derive(Debug)]
pub enum ExtractResult {
    Extracted(PathBuf),
    Existed(PathBuf),
}

#[derive(Eq, PartialEq, Debug)]
pub enum VerifyResult {
    Ok(PathBuf),
    Failed(PathBuf, String),
}

/// Handle to an underlying VPX file
///
/// # Example
///
/// ```no_run
/// use std::io;
/// use std::path::PathBuf;
/// use vpin::vpx::{open, read, version};
///
/// let path = PathBuf::from("testdata/completely_blank_table_10_7_4.vpx");
/// let mut vpx = open(&path).unwrap();
/// let version = vpx.read_version().unwrap();
/// println!("version: {}", version);
/// let images = vpx.read_images().unwrap();
/// for image in images {
///    println!("image: {}", image.name);
/// }
/// ```
///
pub struct VpxFile<F> {
    // keep this private
    compound_file: CompoundFile<F>,
}

impl<F: Read + Seek + Write> VpxFile<F> {
    /// Opens an existing compound file, using the underlying reader.  If the
    /// underlying reader also supports the `Write` trait, then the
    /// `CompoundFile` object will be writable as well.
    pub fn open(inner: F) -> io::Result<VpxFile<F>> {
        // TODO the fact that this is read only should be reflected in the VpxFile type
        let compound_file = CompoundFile::open_strict(inner)?;
        Ok(VpxFile { compound_file })
    }

    pub fn open_rw(inner: F) -> io::Result<VpxFile<F>> {
        let compound_file = CompoundFile::open_strict(inner)?;
        Ok(VpxFile { compound_file })
    }

    pub fn read_version(&mut self) -> io::Result<Version> {
        read_version(&mut self.compound_file)
    }

    pub fn read_tableinfo(&mut self) -> io::Result<TableInfo> {
        read_tableinfo(&mut self.compound_file)
    }

    pub fn read_gamedata(&mut self) -> io::Result<GameData> {
        let version = self.read_version()?;
        read_gamedata(&mut self.compound_file, &version)
    }

    pub fn read_gameitems(&mut self) -> io::Result<Vec<GameItemEnum>> {
        let gamedata = self.read_gamedata()?;
        read_gameitems(&mut self.compound_file, &gamedata)
    }

    pub fn read_images(&mut self) -> io::Result<Vec<ImageData>> {
        let gamedata = self.read_gamedata()?;
        read_images(&mut self.compound_file, &gamedata)
    }

    pub fn read_sounds(&mut self) -> io::Result<Vec<SoundData>> {
        let version = self.read_version()?;
        let gamedata = self.read_gamedata()?;
        read_sounds(&mut self.compound_file, &gamedata, &version)
    }

    pub fn read_fonts(&mut self) -> io::Result<Vec<FontData>> {
        let gamedata = self.read_gamedata()?;
        read_fonts(&mut self.compound_file, &gamedata)
    }

    pub fn read_collections(&mut self) -> io::Result<Vec<Collection>> {
        let gamedata = self.read_gamedata()?;
        read_collections(&mut self.compound_file, &gamedata)
    }

    pub fn read_custominfotags(&mut self) -> io::Result<CustomInfoTags> {
        read_custominfotags(&mut self.compound_file)
    }

    /// Convert all PNG and BMP images to WebP format and write them back to the VPX file.
    /// This will overwrite the existing images.
    /// The images will be converted to lossless WebP.
    ///
    /// Note: this will not shrink the vpx file, that requires compacting the file.
    ///
    /// Returns a list of conversions that were made.
    pub fn images_to_webp(&mut self) -> io::Result<Vec<ImageToWebpConversion>> {
        // We need to make sure we have read access, or we will get a: Bad file descriptor (os error 9)
        let gamedata = self.read_gamedata()?;
        let results = images_to_webp(&mut self.compound_file, &gamedata)?;
        self.compound_file.flush()?;
        Ok(results)
    }
}

/// Tries to reduce the size of the VPX file by rewriting it.
/// Useful after removing or replacing data in the vpx file
pub fn compact<P: AsRef<Path>>(path: P) -> io::Result<()> {
    compact_cfb(path)
}

/// Rewrites the whole compound file with the same data causing the file to be compacted.
fn compact_cfb<P: AsRef<Path>>(in_path: P) -> io::Result<()> {
    // requested to be added in https://github.com/mdsteele/rust-cfb/issues/55
    let out_path: PathBuf = in_path.as_ref().with_extension("compacting");
    let mut original = cfb::open(&in_path)?;
    let version = original.version();
    let out_file = std::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(true)
        .open(&out_path)?;
    let mut duplicate = CompoundFile::create_with_version(version, out_file)?;
    let mut stream_paths = Vec::<PathBuf>::new();
    for entry in original.walk() {
        if entry.is_storage() {
            if !entry.is_root() {
                duplicate.create_storage(entry.path())?;
            }
            duplicate.set_storage_clsid(entry.path(), *entry.clsid())?;
        } else {
            stream_paths.push(entry.path().to_path_buf());
        }
    }
    for path in stream_paths.iter() {
        std::io::copy(
            &mut original.open_stream(path)?,
            &mut duplicate.create_new_stream(path)?,
        )?;
    }
    duplicate.flush()?;
    std::fs::remove_file(&in_path)?;
    std::fs::rename(&out_path, &in_path)
}

/// Opens a handle to an existing VPX file
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<VpxFile<File>> {
    VpxFile::open(File::open(path)?)
}

pub fn open_rw<P: AsRef<Path>>(path: P) -> io::Result<VpxFile<File>> {
    let file = OpenOptions::new().read(true).write(true).open(path)?;
    VpxFile::open_rw(file)
}

/// Reads a VPX file from disk to memory
///
/// see also [`write()`]
///
/// **Note:** This might take up a lot of memory depending on the size of the VPX file.
#[instrument]
pub fn read(path: &Path) -> io::Result<VPX> {
    if !path.exists() {
        return Err(io::Error::new(
            io::ErrorKind::NotFound,
            format!("File not found: {}", path.display()),
        ));
    }
    let file = File::open(path)?;
    let mut comp = CompoundFile::open_strict(file)?;
    read_vpx(&mut comp)
}

/// Reads a VPX file from bytes to memory
///
/// see also [`write()`]
///
/// **Note:** This might take up a lot of memory depending on the size of the VPX file.
#[instrument(skip(slice))]
pub fn from_bytes(slice: &[u8]) -> io::Result<VPX> {
    let mut comp = CompoundFile::open_strict(std::io::Cursor::new(slice))?;
    read_vpx(&mut comp)
}

#[instrument(skip(vpx))]
pub fn to_bytes(vpx: &VPX) -> io::Result<Vec<u8>> {
    let buffer = std::io::Cursor::new(Vec::new());
    let mut comp = CompoundFile::create(buffer)?;
    write_vpx(&mut comp, vpx)?;
    comp.flush()?;
    Ok(comp.into_inner().into_inner())
}

/// Writes a VPX file from memory to disk
///
/// see also [`read()`]/// ///
#[instrument(skip(vpx))]
pub fn write(path: &Path, vpx: &VPX) -> io::Result<()> {
    let file = File::options()
        .read(true)
        .write(true)
        .create(true)
        .truncate(true)
        .open(path)?;
    let mut comp = CompoundFile::create(file)?;
    let result = write_vpx(&mut comp, vpx);
    info!("Wrote {}", path.file_name().unwrap().to_string_lossy());
    result
}

fn read_vpx<F: Read + Seek>(comp: &mut CompoundFile<F>) -> io::Result<VPX> {
    let custominfotags = read_custominfotags(comp)?;
    let info = read_tableinfo(comp)?;
    let version = read_version(comp)?;
    let gamedata = read_gamedata(comp, &version)?;
    let gameitems = read_gameitems(comp, &gamedata)?;
    let images = read_images(comp, &gamedata)?;
    let sounds = read_sounds(comp, &gamedata, &version)?;
    let fonts = read_fonts(comp, &gamedata)?;
    let collections = read_collections(comp, &gamedata)?;
    info!("Loaded VPX");
    Ok(VPX {
        custominfotags,
        info,
        version,
        gamedata,
        gameitems,
        images,
        sounds,
        fonts,
        collections,
    })
}

fn write_vpx<F: Read + Write + Seek>(comp: &mut CompoundFile<F>, vpx: &VPX) -> io::Result<()> {
    create_game_storage(comp)?;
    write_custominfotags(comp, &vpx.custominfotags)?;
    write_tableinfo(comp, &vpx.info)?;
    write_version(comp, &vpx.version)?;
    write_game_data(comp, &vpx.gamedata, &vpx.version)?;
    debug!("Wrote gamedata");
    // Validate part group ordering before writing
    for warning in gameitem::validate_part_group_order(&vpx.gameitems) {
        warn!("{}", warning);
    }
    write_game_items(comp, &vpx.gameitems)?;
    debug!("Wrote {} gameitems", vpx.gameitems.len());
    write_images(comp, &vpx.images)?;
    debug!("Wrote {} images", vpx.images.len());
    write_sounds(comp, &vpx.sounds, &vpx.version)?;
    debug!("Wrote {} sounds", vpx.sounds.len());
    write_fonts(comp, &vpx.fonts)?;
    debug!("Wrote {} fonts", vpx.fonts.len());
    write_collections(comp, &vpx.collections)?;
    debug!("Wrote {} collections", vpx.collections.len());
    let mac = generate_mac(comp)?;
    write_mac(comp, &mac)?;
    info!("Wrote VPX");
    Ok(())
}

/// Writes a minimal `vpx` file
pub fn new_minimal_vpx<P: AsRef<Path>>(vpx_file_path: P) -> io::Result<()> {
    let file = File::options()
        .read(true)
        .write(true)
        .create(true)
        .truncate(true)
        .open(&vpx_file_path)?;
    let mut comp = CompoundFile::create(file)?;
    write_minimal_vpx(&mut comp)
}

fn write_minimal_vpx<F: Read + Write + Seek>(comp: &mut CompoundFile<F>) -> io::Result<()> {
    let table_info = TableInfo::new();
    write_tableinfo(comp, &table_info)?;
    create_game_storage(comp)?;
    let version = Version::new(1072);
    write_version(comp, &version)?;
    write_game_data(comp, &GameData::default(), &version)?;
    // to be more efficient we could generate the mac while writing the different parts
    let mac = generate_mac(comp)?;
    write_mac(comp, &mac)
}

fn create_game_storage<F: Read + Write + Seek>(comp: &mut CompoundFile<F>) -> io::Result<()> {
    let game_stg_path = Path::new(MAIN_SEPARATOR_STR).join("GameStg");
    comp.create_storage(&game_stg_path)
}

/// Extracts the script from an existing `vpx` file.
///
/// # Arguments
/// * `vpx_file_path` Path to the VPX file
/// * `vbs_file_path` Optional path to the script file to write. Defaults to the VPX sidecar script location.
/// * `overwrite` If true, the script will be extracted even if it already exists
pub fn extractvbs(
    vpx_file_path: &Path,
    vbs_file_path: Option<PathBuf>,
    overwrite: bool,
) -> io::Result<ExtractResult> {
    let script_path = match vbs_file_path {
        Some(vbs_file_path) => vbs_file_path,
        None => vbs_path_for(vpx_file_path),
    };

    if !script_path.exists() || (script_path.exists() && overwrite) {
        let mut comp = cfb::open(vpx_file_path)?;
        let version = read_version(&mut comp)?;
        let gamedata = read_gamedata(&mut comp, &version)?;
        extract_script(&gamedata, &script_path)?;
        Ok(ExtractResult::Extracted(script_path))
    } else {
        Ok(ExtractResult::Existed(script_path))
    }
}

/// Imports a script into the provided `vpx` file.
///
/// # Arguments
/// * `vpx_file_path` Path to the VPX file
/// * `vbs_file_path` Optional path to the script file to import. Defaults to the VPX sidecar script location.
///
/// see also [extractvbs]
pub fn importvbs(vpx_file_path: &Path, vbs_file_path: Option<PathBuf>) -> io::Result<PathBuf> {
    let script_path = match vbs_file_path {
        Some(vbs_file_path) => vbs_file_path,
        None => vbs_path_for(vpx_file_path),
    };
    if !script_path.exists() {
        return Err(io::Error::new(
            io::ErrorKind::NotFound,
            format!("Script file not found: {}", script_path.display()),
        ));
    }
    let mut comp = cfb::open_rw(vpx_file_path)?;
    let version = read_version(&mut comp)?;
    let mut gamedata = read_gamedata(&mut comp, &version)?;
    let script = std::fs::read_to_string(&script_path)?;
    gamedata.set_code(script);
    write_game_data(&mut comp, &gamedata, &version)?;
    let mac = generate_mac(&mut comp)?;
    write_mac(&mut comp, &mac)?;
    comp.flush()?;
    Ok(script_path)
}

/// Verifies the MAC signature of a VPX file
pub fn verify(vpx_file_path: &Path) -> VerifyResult {
    let result = move || -> io::Result<_> {
        let mut comp = cfb::open(vpx_file_path)?;
        let mac = read_mac(&mut comp)?;
        let generated_mac = generate_mac(&mut comp)?;
        Ok((mac, generated_mac))
    }();
    match result {
        Ok((mac, generated_mac)) => {
            if mac == generated_mac {
                VerifyResult::Ok(vpx_file_path.to_path_buf())
            } else {
                VerifyResult::Failed(
                    vpx_file_path.to_path_buf(),
                    format!("MAC mismatch: {mac:?} != {generated_mac:?}"),
                )
            }
        }
        Err(e) => VerifyResult::Failed(
            vpx_file_path.to_path_buf(),
            format!("Failed to read VPX file {}: {}", vpx_file_path.display(), e),
        ),
    }
}

/// Returns the path to the sidecar script for a given `vpx` file
pub fn vbs_path_for(vpx_file_path: &Path) -> PathBuf {
    path_for(vpx_file_path, "vbs")
}

/// Returns the path to table `ini` file
pub fn ini_path_for(vpx_file_path: &Path) -> PathBuf {
    path_for(vpx_file_path, "ini")
}

fn path_for(vpx_file_path: &Path, extension: &str) -> PathBuf {
    PathBuf::from(vpx_file_path).with_extension(extension)
}

fn read_mac<F: Read + Write + Seek>(comp: &mut CompoundFile<F>) -> io::Result<Vec<u8>> {
    let mac_path = Path::new(MAIN_SEPARATOR_STR).join("GameStg").join("MAC");
    if !comp.exists(&mac_path) {
        // fail
        return Err(io::Error::new(
            io::ErrorKind::NotFound,
            "MAC stream not found",
        ));
    }
    let mut mac_stream = comp.open_stream(mac_path)?;
    let mut mac = Vec::new();
    mac_stream.read_to_end(&mut mac)?;
    Ok(mac)
}

fn write_mac<F: Read + Write + Seek>(comp: &mut CompoundFile<F>, mac: &[u8]) -> io::Result<()> {
    let mac_path = Path::new(MAIN_SEPARATOR_STR).join("GameStg").join("MAC");
    let mut mac_stream = comp.create_stream(mac_path)?;
    mac_stream.write_all(mac)
}

#[derive(Clone, Debug)]
enum FileType {
    UnstructuredBytes,
    Biff,
}

#[derive(Debug)]
struct FileStructureItem {
    path: PathBuf,
    file_type: FileType,
    hashed: bool,
}
// contructor with default values
impl FileStructureItem {
    fn new(path: &str, file_type: FileType, hashed: bool) -> Self {
        FileStructureItem {
            path: PathBuf::from(path),
            file_type,
            hashed,
        }
    }
}

fn generate_mac<F: Read + Seek>(comp: &mut CompoundFile<F>) -> io::Result<Vec<u8>> {
    // Regarding mac generation, see
    //  https://github.com/freezy/VisualPinball.Engine/blob/ec1e9765cd4832c134e889d6e6d03320bc404bd5/VisualPinball.Engine/VPT/Table/TableWriter.cs#L42
    //  https://github.com/vbousquet/vpx_lightmapper/blob/ca5fddd4c2a0fbe817fd546c5f4db609f9d0da9f/addons/vpx_lightmapper/vlm_export.py#L906-L913
    //  https://github.com/vpinball/vpinball/blob/d9d22a5923ad5a9902a27fae296bc6b2e9ed95ca/pintable.cpp#L2634-L2667
    //  ordering of writes is important co come up with the correct hash

    fn item_path(path: &Path, index: i32) -> PathBuf {
        path.with_file_name(format!(
            "{}{}",
            path.file_name().unwrap().to_string_lossy(),
            index
        ))
    }

    fn append_structure<F: Seek + Read>(
        file_structure: &mut Vec<FileStructureItem>,
        comp: &mut CompoundFile<F>,
        src_path: &str,
        file_type: FileType,
        hashed: bool,
    ) {
        let mut index = 0;
        let path = PathBuf::from(src_path);
        while comp.exists(item_path(&path, index)) {
            file_structure.push(FileStructureItem {
                path: item_path(&path, index),
                file_type: file_type.clone(),
                hashed,
            });
            index += 1;
        }
    }

    use FileType::*;

    // above pythin code converted to rust
    let mut file_structure: Vec<FileStructureItem> = vec![
        FileStructureItem::new("GameStg/Version", UnstructuredBytes, true),
        FileStructureItem::new("TableInfo/TableName", UnstructuredBytes, true),
        FileStructureItem::new("TableInfo/AuthorName", UnstructuredBytes, true),
        FileStructureItem::new("TableInfo/TableVersion", UnstructuredBytes, true),
        FileStructureItem::new("TableInfo/ReleaseDate", UnstructuredBytes, true),
        FileStructureItem::new("TableInfo/AuthorEmail", UnstructuredBytes, true),
        FileStructureItem::new("TableInfo/AuthorWebSite", UnstructuredBytes, true),
        FileStructureItem::new("TableInfo/TableBlurb", UnstructuredBytes, true),
        FileStructureItem::new("TableInfo/TableDescription", UnstructuredBytes, true),
        FileStructureItem::new("TableInfo/TableRules", UnstructuredBytes, true),
        FileStructureItem::new("TableInfo/TableSaveDate", UnstructuredBytes, false),
        FileStructureItem::new("TableInfo/TableSaveRev", UnstructuredBytes, false),
        FileStructureItem::new("TableInfo/Screenshot", UnstructuredBytes, true),
        FileStructureItem::new("GameStg/CustomInfoTags", Biff, true), // custom info tags must be hashed just after this stream
        FileStructureItem::new("GameStg/GameData", Biff, true),
    ];
    // //append_structure(&mut file_structure, comp, "GameStg/GameItem", Biff, false);
    //append_structure(&mut file_structure, comp, "GameStg/Sound", Biff, false);
    // //append_structure(&mut file_structure, comp, "GameStg/Image", Biff, false);
    //append_structure(&mut file_structure, comp, "GameStg/Font", Biff, false);
    append_structure(&mut file_structure, comp, "GameStg/Collection", Biff, true);

    let mut hasher = Md2::new();

    // header is always there.
    hasher.update(b"Visual Pinball");

    for item in file_structure {
        if !item.hashed {
            continue;
        }
        if !comp.exists(&item.path) {
            continue;
        }
        match item.file_type {
            UnstructuredBytes => {
                let bytes = read_bytes_at(&item.path, comp)?;
                hasher.update(&bytes);
            }
            Biff => {
                // println!("reading biff: {:?}", item.path);
                let bytes = read_bytes_at(&item.path, comp)?;
                let mut biff = BiffReader::new(&bytes);

                loop {
                    if biff.is_eof() {
                        break;
                    }
                    biff.next(biff::WARN);
                    // println!("reading biff: {:?} {}", item.path, biff.tag());
                    let tag = biff.tag();
                    let tag_str = tag.as_str();
                    match tag_str {
                        "CODE" => {
                            //  For some reason, the code length info is not hashed, just the tag and code string
                            hasher.update(b"CODE");
                            // code is a special case, it indicates a length of 4 (only the tag)
                            // so already 0 bytes remaining
                            let code_length = biff.get_u32_no_remaining_update();
                            let code = biff.get_no_remaining_update(code_length as usize);
                            hasher.update(code);
                        }
                        _other => {
                            // Biff tags and data are hashed but not their size
                            hasher.update(biff.get_record_data(true));
                        }
                    }
                }
            }
        }

        if item.path.ends_with("CustomInfoTags") {
            let bytes = read_bytes_at(&item.path, comp)?;
            let mut biff = BiffReader::new(&bytes);

            loop {
                if biff.is_eof() {
                    break;
                }
                biff.next(biff::WARN);
                if biff.tag() == "CUST" {
                    let cust_name = biff.get_string();
                    //println!("Hashing custom information block {}", cust_name);
                    let path = format!("TableInfo/{cust_name}");
                    if comp.exists(&path) {
                        let data = read_bytes_at(&path, comp)?;
                        hasher.update(&data);
                    }
                } else {
                    biff.skip_tag();
                }
            }
        }
    }
    let result = hasher.finalize();
    Ok(result.to_vec())
}

// TODO this is not very efficient as we copy the bytes around a lot
fn read_bytes_at<F: Read + Seek, P: AsRef<Path>>(
    path: P,
    comp: &mut CompoundFile<F>,
) -> Result<Vec<u8>, io::Error> {
    // println!("reading bytes at: {:?}", path.as_ref());
    let mut bytes = Vec::new();
    let mut stream = comp.open_stream(&path)?;
    stream.read_to_end(&mut bytes).map_err(|e| {
        io::Error::other(
            format!("Failed to read bytes at {:?}, this might be because the file is open in write only mode. {}", path.as_ref(), e),
        )
    })?;
    Ok(bytes)
}

/// Write the script to file in utf8 encoding
pub fn extract_script<P: AsRef<Path>>(gamedata: &GameData, vbs_path: &P) -> Result<(), io::Error> {
    let script = &gamedata.code;
    std::fs::write(vbs_path, &script.string)
}

fn read_gamedata<F: Seek + Read>(
    comp: &mut CompoundFile<F>,
    version: &Version,
) -> io::Result<GameData> {
    let mut game_data_vec = Vec::new();
    let game_data_path = Path::new(MAIN_SEPARATOR_STR)
        .join("GameStg")
        .join("GameData");
    let mut stream = comp.open_stream(game_data_path)?;
    stream.read_to_end(&mut game_data_vec)?;
    let gamedata = gamedata::read_all_gamedata_records(&game_data_vec[..], version);
    Ok(gamedata)
}

fn write_game_data<F: Read + Write + Seek>(
    comp: &mut CompoundFile<F>,
    gamedata: &GameData,
    version: &Version,
) -> Result<(), io::Error> {
    let game_data_path = Path::new(MAIN_SEPARATOR_STR)
        .join("GameStg")
        .join("GameData");
    // we expect GameStg to exist
    let mut game_data_stream = comp.create_stream(&game_data_path)?;
    let data = gamedata::write_all_gamedata_records(gamedata, version);
    game_data_stream.write_all(&data)
    // this flush was required before but now it's working without
    // game_data_stream.flush()
}

#[instrument(skip(comp, gamedata), fields(gameitem_count = gamedata.gameitems_size))]
fn read_gameitems<F: Read + Seek>(
    comp: &mut CompoundFile<F>,
    gamedata: &GameData,
) -> io::Result<Vec<GameItemEnum>> {
    let gamestg = Path::new(MAIN_SEPARATOR_STR).join("GameStg");
    (0..gamedata.gameitems_size)
        .map(|index| {
            let path = gamestg.join(format!("GameItem{index}"));
            let mut input = Vec::new();
            let mut stream = comp.open_stream(&path)?;
            stream.read_to_end(&mut input)?;
            let game_item = gameitem::read(&input);
            Ok(game_item)
        })
        .collect()
}

#[instrument(skip(comp, gameitems), fields(gameitem_count = gameitems.len()))]
fn write_game_items<F: Read + Write + Seek>(
    comp: &mut CompoundFile<F>,
    gameitems: &[GameItemEnum],
) -> io::Result<()> {
    let game_storage_path = Path::new(MAIN_SEPARATOR_STR).join("GameStg");
    for (index, gameitem) in gameitems.iter().enumerate() {
        let path = game_storage_path.join(format!("GameItem{index}"));
        write_gameitem(comp, &path, gameitem)?;
    }
    Ok(())
}

#[instrument(skip(comp, path, gameitem), fields(name = ?gameitem.name()))]
fn write_gameitem<F: Read + Write + Seek>(
    comp: &mut CompoundFile<F>,
    path: &Path,
    gameitem: &GameItemEnum,
) -> Result<(), Error> {
    let mut stream = comp.create_stream(path)?;
    let data = gameitem::write(gameitem);
    stream.write_all(&data)
}

#[instrument(skip(comp, gamedata), fields(sound_count = gamedata.sounds_size))]
fn read_sounds<F: Read + Seek>(
    comp: &mut CompoundFile<F>,
    gamedata: &GameData,
    file_version: &Version,
) -> io::Result<Vec<SoundData>> {
    (0..gamedata.sounds_size)
        .map(|index| read_sound(comp, file_version, index)?)
        .collect()
}

#[instrument(skip(comp, file_version), fields(index))]
fn read_sound<F: Read + Seek>(
    comp: &mut CompoundFile<F>,
    file_version: &Version,
    index: u32,
) -> Result<Result<SoundData, Error>, Error> {
    let path = Path::new(MAIN_SEPARATOR_STR)
        .join("GameStg")
        .join(format!("Sound{index}"));
    let mut input = Vec::new();
    let span = info_span!("read_sound_stream", ?path);
    let mut stream = comp.open_stream(&path)?;
    stream.read_to_end(&mut input)?;
    drop(span);
    let mut reader = BiffReader::new(&input);
    let sound = sound::read(file_version, &mut reader);
    Ok(Ok(sound))
}

#[instrument(skip(comp, sounds), fields(sound_count = sounds.len()))]
fn write_sounds<F: Read + Write + Seek>(
    comp: &mut CompoundFile<F>,
    sounds: &[SoundData],
    file_version: &Version,
) -> io::Result<()> {
    for (index, sound) in sounds.iter().enumerate() {
        let path = Path::new(MAIN_SEPARATOR_STR)
            .join("GameStg")
            .join(format!("Sound{index}"));
        let mut stream = comp.create_stream(&path)?;
        let mut writer = BiffWriter::new();
        sound::write(file_version, sound, &mut writer);
        stream.write_all(writer.get_data())?;
    }
    Ok(())
}

fn read_collections<F: Read + Seek>(
    comp: &mut CompoundFile<F>,
    gamedata: &GameData,
) -> io::Result<Vec<Collection>> {
    (0..gamedata.collections_size)
        .map(|index| {
            let path = Path::new(MAIN_SEPARATOR_STR)
                .join("GameStg")
                .join(format!("Collection{index}"));
            let mut input = Vec::new();
            let mut stream = comp.open_stream(&path)?;
            stream.read_to_end(&mut input)?;
            Ok(collection::read(&input))
        })
        .collect()
}

fn write_collections<F: Read + Write + Seek>(
    comp: &mut CompoundFile<F>,
    collections: &[Collection],
) -> io::Result<()> {
    for (index, collection) in collections.iter().enumerate() {
        let path = Path::new(MAIN_SEPARATOR_STR)
            .join("GameStg")
            .join(format!("Collection{index}"));
        let mut stream = comp.create_stream(&path)?;
        let data = collection::write(collection);
        stream.write_all(&data)?;
    }
    Ok(())
}

#[instrument(skip(comp, gamedata), fields(image_count = gamedata.images_size))]
fn read_images<F: Read + Seek>(
    comp: &mut CompoundFile<F>,
    gamedata: &GameData,
) -> io::Result<Vec<ImageData>> {
    (0..gamedata.images_size)
        .map(|index| read_image(comp, index))
        .collect()
}

fn read_image<F: Read + Seek>(comp: &mut CompoundFile<F>, index: u32) -> Result<ImageData, Error> {
    let path = format!("GameStg/Image{index}");
    let mut input = Vec::new();
    let mut stream = comp.open_stream(&path)?;
    stream.read_to_end(&mut input)?;
    let mut reader = BiffReader::new(&input);
    Ok(ImageData::biff_read(&mut reader))
}

#[instrument(skip(comp, images), fields(image_count = images.len()))]
fn write_images<F: Read + Write + Seek>(
    comp: &mut CompoundFile<F>,
    images: &[ImageData],
) -> io::Result<()> {
    for (index, image) in images.iter().enumerate() {
        write_image(comp, index, image, false)?;
    }
    Ok(())
}

fn write_image<F: Read + Write + Seek>(
    comp: &mut CompoundFile<F>,
    index: usize,
    image: &ImageData,
    overwrite: bool,
) -> Result<(), Error> {
    let path = format!("GameStg/Image{index}");

    let mut stream = if overwrite {
        comp.create_stream(&path)?
    } else {
        comp.create_new_stream(&path)?
    };
    let mut writer = BiffWriter::new();
    image.biff_write(&mut writer);
    stream.write_all(writer.get_data())?;
    Ok(())
}

#[derive(Debug, PartialEq, Clone)]
pub struct ImageToWebpConversion {
    pub name: String,
    pub old_extension: String,
    pub new_extension: String,
}

fn images_to_webp<F: Read + Write + Seek>(
    comp: &mut CompoundFile<F>,
    gamedata: &GameData,
) -> io::Result<Vec<ImageToWebpConversion>> {
    let mut conversions = Vec::new();
    for index in 0..gamedata.images_size {
        let mut image_data = read_image(comp, index)?;
        match image_data.ext().to_lowercase().as_str() {
            "png" => {
                // convert the image to webp
                image_data.change_extension("webp");
                if let Some(jpeg) = &mut image_data.jpeg {
                    // read the image bytes using the rust image library
                    let dynamic_image =
                        match ::image::load_from_memory_with_format(&jpeg.data, ImageFormat::Png) {
                            Ok(image) => image,
                            Err(e) => {
                                // see https://github.com/image-rs/image/issues/2260
                                warn!("Skipping image {}: {}", image_data.name, e);
                                continue;
                            }
                        };

                    // write as webp back to the image
                    let mut webp = Vec::new();
                    let mut cursor = io::Cursor::new(&mut webp);
                    // should be lossless according to the docs
                    dynamic_image
                        .write_to(&mut cursor, ImageFormat::WebP)
                        .map_err(|e| io::Error::other(e.to_string()))?;
                    jpeg.data = webp;
                    write_image(comp, index as usize, &image_data, true)?;
                    conversions.push(ImageToWebpConversion {
                        name: image_data.name.clone(),
                        old_extension: "png".to_string(),
                        new_extension: "webp".to_string(),
                    });
                }
            }
            "bmp" => {
                // convert the image to webp
                image_data.change_extension("webp");
                if let Some(bits) = &mut image_data.bits {
                    // read the image bytes using the rust image library

                    let dynamic_image = vpx_image_to_dynamic_image(
                        &bits.lzw_compressed_data,
                        image_data.width,
                        image_data.height,
                    );

                    // write as webp back to the image
                    let mut webp = Vec::new();
                    let mut cursor = io::Cursor::new(&mut webp);
                    // should be lossless according to the docs
                    dynamic_image
                        .write_to(&mut cursor, ImageFormat::WebP)
                        .map_err(|e| io::Error::other(e.to_string()))?;
                    let jpg = ImageDataJpeg {
                        path: image_data.path.clone(),
                        name: image_data.name.clone(),
                        internal_name: None,
                        data: webp,
                    };
                    image_data.bits = None;
                    image_data.jpeg = Some(jpg);
                    write_image(comp, index as usize, &image_data, true)?;
                }
                conversions.push(ImageToWebpConversion {
                    name: image_data.name.clone(),
                    old_extension: "bmp".to_string(),
                    new_extension: "webp".to_string(),
                });
            }
            _ => {}
        }
    }
    Ok(conversions)
}

fn read_fonts<F: Read + Seek>(
    comp: &mut CompoundFile<F>,
    gamedata: &GameData,
) -> io::Result<Vec<FontData>> {
    (0..gamedata.fonts_size)
        .map(|index| {
            let path = format!("GameStg/Font{index}");
            let mut input = Vec::new();
            let mut stream = comp.open_stream(&path)?;
            stream.read_to_end(&mut input)?;

            let font = font::read(&input);
            Ok(font)
        })
        .collect()
}

fn write_fonts<F: Read + Write + Seek>(
    comp: &mut CompoundFile<F>,
    fonts: &[FontData],
) -> io::Result<()> {
    for (index, font) in fonts.iter().enumerate() {
        let path = format!("GameStg/Font{index}");
        let mut stream = comp.create_stream(&path)?;
        let data = font::write(font);
        stream.write_all(&data)?;
    }
    Ok(())
}

fn read_custominfotags<F: Read + Seek>(comp: &mut CompoundFile<F>) -> io::Result<CustomInfoTags> {
    let path = Path::new(MAIN_SEPARATOR_STR)
        .join("GameStg")
        .join("CustomInfoTags");
    let mut tags_data = Vec::new();
    let tags = if comp.is_stream(&path) {
        let mut stream = comp.open_stream(path)?;
        stream.read_to_end(&mut tags_data)?;

        custominfotags::read_custominfotags(&tags_data)
    } else {
        CustomInfoTags::default()
    };
    Ok(tags)
}

fn write_custominfotags<F: Read + Write + Seek>(
    comp: &mut CompoundFile<F>,
    tags: &CustomInfoTags,
) -> io::Result<()> {
    let path = Path::new(MAIN_SEPARATOR_STR)
        .join("GameStg")
        .join("CustomInfoTags");

    let data = custominfotags::write_custominfotags(tags);
    let mut stream = comp.create_stream(path)?;
    stream.write_all(&data)
}

/// Table dimensions for UV coordinate calculation
/// Used for world-space texture mapping in walls, ramps, and flashers
#[derive(Debug, Clone, Copy)]
pub struct TableDimensions {
    pub left: f32,
    pub top: f32,
    pub right: f32,
    pub bottom: f32,
}

impl TableDimensions {
    pub fn new(left: f32, top: f32, right: f32, bottom: f32) -> Self {
        Self {
            left,
            top,
            right,
            bottom,
        }
    }
}

#[cfg(test)]
mod tests {
    #[cfg(not(target_family = "wasm"))]
    use crate::vpx::image::ImageDataBits;
    use pretty_assertions::assert_eq;
    use std::io::Cursor;
    #[cfg(not(target_family = "wasm"))]
    use testdir::testdir;

    use super::*;

    #[test]
    fn test_write_read() -> io::Result<()> {
        let buff = Cursor::new(vec![0; 15]);
        let mut comp = CompoundFile::create(buff)?;
        write_minimal_vpx(&mut comp)?;

        let version = read_version(&mut comp)?;
        let tableinfo = read_tableinfo(&mut comp)?;
        let game_data = read_gamedata(&mut comp, &version)?;

        assert_eq!(tableinfo, TableInfo::new());
        assert_eq!(version, Version::new(1072));
        let expected = GameData::default();
        assert_eq!(game_data, expected);
        Ok(())
    }

    const TEST_TABLE_BYTES: &[u8] =
        include_bytes!("../../testdata/completely_blank_table_10_7_4.vpx");

    #[test]
    fn test_mac_generation() -> io::Result<()> {
        let cursor = Cursor::new(TEST_TABLE_BYTES.to_vec());
        let mut comp = CompoundFile::open_strict(cursor)?;

        let expected = [
            231, 121, 242, 251, 174, 227, 247, 90, 58, 105, 13, 92, 13, 73, 151, 86,
        ];

        let mac = read_mac(&mut comp)?;
        assert_eq!(mac, expected);

        let generated_mac = generate_mac(&mut comp)?;
        assert_eq!(mac, generated_mac);
        Ok(())
    }

    #[test]
    fn test_minimal_mac() -> io::Result<()> {
        let buff = Cursor::new(vec![0; 15]);
        let mut comp = CompoundFile::create(buff)?;
        write_minimal_vpx(&mut comp)?;

        let mac = read_mac(&mut comp)?;
        let expected = [
            6, 114, 79, 74, 132, 67, 3, 171, 234, 201, 25, 188, 149, 226, 239, 4,
        ];
        assert_eq!(mac, expected);
        Ok(())
    }

    #[test]
    fn read_write_gamedata() -> io::Result<()> {
        let cursor = Cursor::new(TEST_TABLE_BYTES.to_vec());
        let mut comp = CompoundFile::open_strict(cursor)?;
        let version = read_version(&mut comp)?;
        let original = read_gamedata(&mut comp, &version)?;

        let buff = Cursor::new(vec![0; 15]);
        let mut comp2 = CompoundFile::create(buff)?;
        create_game_storage(&mut comp2)?;
        write_version(&mut comp2, &version)?;
        write_game_data(&mut comp2, &original, &version)?;

        let read = read_gamedata(&mut comp2, &version)?;

        assert_eq!(original, read);
        Ok(())
    }

    #[test]
    fn read_write_gameitems() -> io::Result<()> {
        let cursor = Cursor::new(TEST_TABLE_BYTES.to_vec());
        let mut comp = CompoundFile::open_strict(cursor)?;
        let version = read_version(&mut comp)?;
        let gamedata = read_gamedata(&mut comp, &version)?;
        let original = read_gameitems(&mut comp, &gamedata)?;

        let buff = Cursor::new(vec![0; 15]);
        let mut comp = CompoundFile::create(buff)?;
        create_game_storage(&mut comp)?;
        write_game_items(&mut comp, &original)?;

        let read = read_gameitems(&mut comp, &gamedata)?;

        assert_eq!(original.len(), read.len());
        assert_eq!(original, read);
        // TODO match original bytes and written bytes for each item
        Ok(())
    }

    #[test]
    #[cfg(not(target_family = "wasm"))]
    fn read() -> io::Result<()> {
        let path = PathBuf::from("testdata/completely_blank_table_10_7_4.vpx");
        let mut comp = cfb::open(path)?;
        let original = read_vpx(&mut comp)?;

        let mut expected_info = TableInfo::new();
        expected_info.table_name = Some(String::from("Visual Pinball Demo Table"));
        expected_info.table_save_rev = Some(String::from("10"));
        expected_info.table_version = Some(String::from("1.2"));

        expected_info.author_website = Some(String::from("http://www.vpforums.org/"));
        expected_info.table_save_date = Some(String::from("Tue Jul 11 15:48:49 2023"));
        expected_info.table_description = Some(String::from(
            "Press C to enable manual Ball Control via the arrow keys and B",
        ));

        assert_eq!(original.version, Version::new(1072));
        assert_eq!(original.info, expected_info);
        assert_eq!(original.gamedata.collections_size, 9);
        assert_eq!(original.gamedata.images_size, 1);
        assert_eq!(original.gamedata.sounds_size, 0);
        assert_eq!(original.gamedata.fonts_size, 0);
        assert_eq!(original.gamedata.gameitems_size, 73);
        assert_eq!(original.gameitems.len(), 73);
        assert_eq!(original.images.len(), 1);
        assert_eq!(original.sounds.len(), 0);
        assert_eq!(original.fonts.len(), 0);
        assert_eq!(original.collections.len(), 9);
        Ok(())
    }

    #[test]
    #[cfg(not(target_family = "wasm"))]
    fn create_minimal_vpx_and_read() -> io::Result<()> {
        let dir: PathBuf = testdir!();
        let test_vpx_path = dir.join("test.vpx");
        let mut comp = cfb::create(test_vpx_path)?;
        write_minimal_vpx(&mut comp)?;
        comp.flush()?;
        let vpx = read_vpx(&mut comp)?;
        assert_eq!(vpx.info.table_name, None);
        assert_eq!(vpx.info.table_version, None);
        Ok(())
    }

    #[test]
    #[cfg(not(target_family = "wasm"))]
    fn images_to_webp_and_compact() -> io::Result<()> {
        let dir: PathBuf = testdir!();
        let test_vpx_path = dir.join("test.vpx");
        let mut vpx = VPX::default();
        // generate random values for the pixels
        let random_pixels = (0..1000 * 1000 * 4)
            .map(|_| rand::random::<u8>())
            .collect::<Vec<u8>>();
        let bmp_image = ImageData {
            name: "bpmimage".to_string(),
            path: "test.bmp".to_string(),
            width: 1000,
            height: 1000,
            bits: Some(ImageDataBits {
                lzw_compressed_data: lzw::to_lzw_blocks(&random_pixels),
            }),
            ..Default::default()
        };
        let dynamic_image = ::image::RgbaImage::from_raw(1000, 1000, random_pixels).unwrap();
        // write the image to a png file in memory
        let mut png_data = Vec::new();
        let mut cursor = io::Cursor::new(&mut png_data);
        dynamic_image
            .write_to(&mut cursor, ImageFormat::Png)
            .unwrap();
        let png_image = ImageData {
            name: "pngimage".to_string(),
            path: "test.png".to_string(),
            width: 1000,
            height: 1000,
            jpeg: Some(ImageDataJpeg {
                path: "pngimage".to_string(),
                name: "test.png".to_string(),
                internal_name: None,
                data: png_data,
            }),
            ..Default::default()
        };
        vpx.add_or_replace_image(bmp_image);
        vpx.add_or_replace_image(png_image);
        write(&test_vpx_path, &vpx)?;

        let initial_size = test_vpx_path.metadata()?.len();

        let mut vpx = open_rw(&test_vpx_path)?;
        let updates = vpx.images_to_webp()?;

        compact(&test_vpx_path)?;

        let final_size = test_vpx_path.metadata()?.len();
        assert_eq!(
            updates,
            vec!(
                ImageToWebpConversion {
                    name: "bpmimage".to_string(),
                    old_extension: "bmp".to_string(),
                    new_extension: "webp".to_string(),
                },
                ImageToWebpConversion {
                    name: "pngimage".to_string(),
                    old_extension: "png".to_string(),
                    new_extension: "webp".to_string(),
                },
            )
        );
        println!("Initial size: {initial_size}, Final size: {final_size}");
        assert!(
            final_size < initial_size,
            "Final size: {final_size} >= Initial size: {initial_size}!"
        );

        Ok(())
    }

    #[test]
    #[cfg(not(target_family = "wasm"))]
    fn test_extractvbs_empty_file() {
        let dir: PathBuf = testdir!();
        let test_vpx_path = dir.join("test.vpx");
        // make an empty file
        File::create(&test_vpx_path).unwrap();
        let result = extractvbs(&test_vpx_path, None, false);
        let script_path = vbs_path_for(&test_vpx_path);
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            "Invalid CFB file (0 bytes is too small)",
        );
        assert!(!script_path.exists());
    }

    #[test]
    #[cfg(not(target_family = "wasm"))]
    fn test_verify_empty_file() {
        let dir: PathBuf = testdir!();
        let test_vpx_path = dir.join("test.vpx");
        // make an empty file
        File::create(&test_vpx_path).unwrap();
        let result = verify(&test_vpx_path);
        let script_path = vbs_path_for(&test_vpx_path);
        assert_eq!(
            result,
            VerifyResult::Failed(
                test_vpx_path.clone(),
                format!(
                    "Failed to read VPX file {}: Invalid CFB file (0 bytes is too small)",
                    test_vpx_path.display()
                )
            ),
        );
        assert!(!script_path.exists());
    }
}