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
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
//! Game data structures for VPX tables.
//!
//! # VPinball Unit System (VPU)
//!
//! VPinball uses its own unit system called VPU (Visual Pinball Units).
//! The conversion factors are:
//!
//! - **50 VPU = 1.0625 inches** (based on standard pinball diameter of 27mm)
//! - **1 VPU ≈ 0.54 mm** (or about 0.021 inches)
//! - **CMTOVPU(x)** = x * (50 / (2.54 * 1.0625)) ≈ x * 18.53
//! - **VPUTOCM(x)** = x * (2.54 * 1.0625 / 50) ≈ x * 0.054
//! - **INCHESTOVPU(x)** = x * (50 / 1.0625) ≈ x * 47.06
//! - **VPUTOINCHES(x)** = x * (1.0625 / 50) ≈ x * 0.021
//!
//! # View Modes
//!
//! VPinball supports three view setups:
//! - **Desktop (BG_DESKTOP)**: Default view for playing on a computer monitor
//! - **Fullscreen (BG_FULLSCREEN)**: View for fullscreen/cabinet mode
//! - **FSS (BG_FSS)**: Full Single Screen mode for single-monitor cabinet setups
//!
//! Each view can use one of three layout modes:
//! - **Legacy**: Pre-10.8 mode with viewer position relative to fitted bounding vertices,
//!   uses perspective projection with layback skewing
//! - **Camera**: Viewer position relative to table bottom center, standard perspective projection,
//!   frustum offset instead of layback
//! - **Window**: Viewer position relative to screen bottom center, oblique projection for
//!   "magic window" effect on cabinet glass

#![allow(dead_code)]

use super::{
    biff::{self, BiffReader, BiffWriter},
    model::StringWithEncoding,
    version::Version,
};
use crate::vpx::biff::{BiffRead, BiffWrite};
use crate::vpx::color::Color;
use crate::vpx::json::F32WithNanInf;
use crate::vpx::material::{Material, SaveMaterial, SavePhysicsMaterial};
use crate::vpx::math::{dequantize_u8, dequantize_unsigned, quantize_u8, quantize_unsigned};
use crate::vpx::renderprobe::RenderProbeWithGarbage;
use bytes::{Buf, BufMut, BytesMut};
use log::warn;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[derive(Debug, PartialEq, Clone, Copy)]
#[cfg_attr(test, derive(fake::Dummy))]
pub enum ViewLayoutMode {
    /// All tables before 10.8 used a viewer position relative to a fitting of a set of bounding vertices (not all parts) with a standard perspective projection skewed by a layback angle
    Legacy = 0,
    /// Position viewer relative to the bottom center of the table, use a standard camera perspective projection, replace layback by a frustum offset
    Camera = 1,
    /// Position viewer relative to the bottom center of the screen, use an oblique surface (re)projection (would need some postprocess to limit distortion)
    Window = 2,
}

impl From<u32> for ViewLayoutMode {
    fn from(value: u32) -> Self {
        match value {
            0 => ViewLayoutMode::Legacy,
            1 => ViewLayoutMode::Camera,
            2 => ViewLayoutMode::Window,
            _ => panic!("Invalid ViewLayoutMode {value}"),
        }
    }
}

impl From<&ViewLayoutMode> for u32 {
    fn from(value: &ViewLayoutMode) -> Self {
        match value {
            ViewLayoutMode::Legacy => 0,
            ViewLayoutMode::Camera => 1,
            ViewLayoutMode::Window => 2,
        }
    }
}

// Serialize ViewLayoutMode as to lowercase string
impl Serialize for ViewLayoutMode {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            ViewLayoutMode::Legacy => serializer.serialize_str("legacy"),
            ViewLayoutMode::Camera => serializer.serialize_str("camera"),
            ViewLayoutMode::Window => serializer.serialize_str("window"),
        }
    }
}

// Deserialize ViewLayoutMode from lowercase string
// or number for backwards compatibility
impl<'de> Deserialize<'de> for ViewLayoutMode {
    fn deserialize<D>(deserializer: D) -> Result<ViewLayoutMode, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct ViewLayoutModeVisitor;

        impl serde::de::Visitor<'_> for ViewLayoutModeVisitor {
            type Value = ViewLayoutMode;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("a ViewLayoutMode as lowercase string or number")
            }

            fn visit_str<E>(self, value: &str) -> Result<ViewLayoutMode, E>
            where
                E: serde::de::Error,
            {
                match value {
                    "legacy" => Ok(ViewLayoutMode::Legacy),
                    "camera" => Ok(ViewLayoutMode::Camera),
                    "window" => Ok(ViewLayoutMode::Window),
                    _ => Err(serde::de::Error::unknown_variant(
                        value,
                        &["legacy", "camera", "window"],
                    )),
                }
            }

            fn visit_u64<E>(self, value: u64) -> Result<ViewLayoutMode, E>
            where
                E: serde::de::Error,
            {
                match value {
                    0 => Ok(ViewLayoutMode::Legacy),
                    1 => Ok(ViewLayoutMode::Camera),
                    2 => Ok(ViewLayoutMode::Window),
                    _ => Err(serde::de::Error::invalid_value(
                        serde::de::Unexpected::Unsigned(value),
                        &"0, 1, or 2",
                    )),
                }
            }
        }

        deserializer.deserialize_any(ViewLayoutModeVisitor)
    }
}

// TODO switch to a array of 3 view modes like in the original code
#[derive(Debug, PartialEq)]
pub struct ViewSetup {
    // ViewLayoutMode mMode = VLM_LEGACY;

    // // Overall scene scale
    // float mSceneScaleZ = 1.0f;

    // // View position (relative to table bounds for legacy mode, relative to the bottom center of the table for others)
    // float mViewX = 0.f;
    // float mViewY = CMTOVPU(20.f);
    // float mViewZ = CMTOVPU(70.f);
    // float mLookAt = 0.25f; // Look at expressed as a camera inclination for legacy, or a percent of the table height, starting from bottom (0.25 is around top of slingshots)

    // // Viewport adjustments
    // float mViewportRotation = 0.f;
    // float mSceneScaleX = 1.0f;
    // float mSceneScaleY = 1.0f;

    // // View properties
    // float mFOV = 45.0f; // Camera & Legacy: Field of view, in degrees
    // float mLayback = 0.0f; // Legacy: A skewing angle that deform the table to make it look 'good'
    // float mViewHOfs = 0.0f; // Camera & Window: horizontal frustrum offset
    // float mViewVOfs = 0.0f; // Camera & Window: vertical frustrum offset

    // // Magic Window mode properties
    // float mWindowTopXOfs = 0.0f; // Upper window border offset from left and right table bounds
    // float mWindowTopYOfs = 0.0f; // Upper window border Y coordinate, relative to table top
    // float mWindowTopZOfs = CMTOVPU(20.0f); // Upper window border Z coordinate, relative to table playfield Z
    // float mWindowBottomXOfs = 0.0f; // Lower window border offset from left and right table bounds
    // float mWindowBottomYOfs = 0.0f; // Lower window border Y coordinate, relative to table bottom
    // float mWindowBottomZOfs = CMTOVPU(7.5f); // Lower window border Z coordinate, relative to table playfield Z
    pub mode: ViewLayoutMode,
}

impl ViewSetup {
    pub fn new() -> Self {
        ViewSetup {
            mode: ViewLayoutMode::Legacy,
        }
    }
}

impl Default for ViewSetup {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
#[cfg_attr(test, derive(fake::Dummy))]
pub enum ToneMapper {
    /// Reinhard, used to be the default until 10.8
    Reinhard,
    /// AgX tonemapper, used in Blender, implementation derived from threeJs which derives its implementation from Filament
    AgX,
    /// Filmic tonemapper
    Filmic,
    /// Neutral tonemapper, designed for e-commerce, keeps sRGB colors kinda original
    Neutral,
    /// AgX tonemapper, punchy look curve (more contrast/saturation)
    AgXPunchy,
    Other(u32),
}

impl From<u32> for ToneMapper {
    fn from(value: u32) -> Self {
        match value {
            0 => ToneMapper::Reinhard,
            1 => ToneMapper::AgX,
            2 => ToneMapper::Filmic,
            3 => ToneMapper::Neutral,
            4 => ToneMapper::AgXPunchy,
            other => ToneMapper::Other(other),
        }
    }
}

impl From<&ToneMapper> for u32 {
    fn from(value: &ToneMapper) -> Self {
        match value {
            ToneMapper::Reinhard => 0,
            ToneMapper::AgX => 1,
            ToneMapper::Filmic => 2,
            ToneMapper::Neutral => 3,
            ToneMapper::AgXPunchy => 4,
            ToneMapper::Other(other) => *other,
        }
    }
}

/// Serializes ToneMapper to lowercase string
impl Serialize for ToneMapper {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            ToneMapper::Reinhard => serializer.serialize_str("reinhard"),
            ToneMapper::AgX => serializer.serialize_str("agx"),
            ToneMapper::Filmic => serializer.serialize_str("filmic"),
            ToneMapper::Neutral => serializer.serialize_str("neutral"),
            ToneMapper::AgXPunchy => serializer.serialize_str("agx_punchy"),
            ToneMapper::Other(other) => serializer.serialize_u32(*other),
        }
    }
}

/// Deserializes ToneMapper from lowercase string
/// or number for backwards compatibility
impl<'de> Deserialize<'de> for ToneMapper {
    fn deserialize<D>(deserializer: D) -> Result<ToneMapper, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct ToneMapperVisitor;

        impl serde::de::Visitor<'_> for ToneMapperVisitor {
            type Value = ToneMapper;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("a ToneMapper as lowercase string or number")
            }

            fn visit_u64<E>(self, value: u64) -> Result<ToneMapper, E>
            where
                E: serde::de::Error,
            {
                // try to convert u64 to u32
                let val: u32 = value.try_into().map_err(|_| {
                    serde::de::Error::invalid_value(
                        serde::de::Unexpected::Unsigned(value),
                        &"a ToneMapper number",
                    )
                })?;
                Ok(val.into())
            }

            fn visit_str<E>(self, value: &str) -> Result<ToneMapper, E>
            where
                E: serde::de::Error,
            {
                match value {
                    "reinhard" => Ok(ToneMapper::Reinhard),
                    "agx" => Ok(ToneMapper::AgX),
                    "filmic" => Ok(ToneMapper::Filmic),
                    "neutral" => Ok(ToneMapper::Neutral),
                    "agx_punchy" => Ok(ToneMapper::AgXPunchy),
                    // backwards compatibility, tony_mc_mapface was renamed to agx
                    // see https://github.com/vpinball/vpinball/pull/1999
                    "tony_mc_mapface" => Ok(ToneMapper::AgX),
                    _ => Err(serde::de::Error::unknown_variant(
                        value,
                        &["reinhard", "agx", "filmic", "neutral", "agx_punchy"],
                    )),
                }
            }
        }

        deserializer.deserialize_any(ToneMapperVisitor)
    }
}

#[derive(Debug, PartialEq)]
pub struct GameData {
    /// Table name
    /// BIFF tag: `NAME`
    pub name: String,

    pub left: f32,   // LEFT 1
    pub top: f32,    // TOPX 2
    pub right: f32,  // RGHT 3
    pub bottom: f32, // BOTM 4
    /// CLMO
    /// During the 10.8.0 development cycle, this field was added but later again removed
    /// Has meanwhile been replaced by the new [`GameData::bg_view_mode_desktop`],
    /// [`GameData::bg_view_mode_fullscreen`] and [`GameData::bg_view_mode_full_single_screen`] fields
    /// See [the related commit](https://github.com/vpinball/vpinball/commit/5087b3c51b99676f91b02ee4b0c0af4b89b6afda)
    /// CLM_RELATIVE = 0, // All tables before 10.8 used a camera position relative to a fitting of a set of bounding vertices (not all parts)
    /// CLM_ABSOLUTE = 1 // Position camera relative to the bottom center of the table
    pub camera_layout_mode: Option<u32>,

    // =====================================================================================
    // DESKTOP VIEW SETTINGS (BG_DESKTOP)
    //
    // These settings are used for the main desktop view when playing on a single computer monitor.
    // The table fills the screen from top to bottom and anything that would be shown on the
    // backglass is shown on the left or the right of the playfield.
    //
    // Default values (legacy VPX format, stored in VPU):
    // - Mode: Legacy (VLM_LEGACY) when not set
    // - Rotation: 0 degrees
    // - Inclination: 0 degrees (legacy) / 0.25 (camera mode, 25% from bottom)
    // - Layback: 0 degrees
    // - FOV: 45 degrees
    // - Offset X: 0 VPU
    // - Offset Y: 30 VPU (about 1.6 cm)
    // - Offset Z: -200 VPU (about -10.8 cm, negative moves camera back in legacy mode)
    // - Scale X/Y/Z: 1.0
    //
    // Note: VPinball 10.8+ ViewSetup.h uses defaults in centimeters (20cm Y, 70cm Z)
    // for new tables, but VPX files store values in VPU.
    // =====================================================================================
    /// View layout mode for desktop view (VSM0, added in 10.8.x)
    /// - Legacy: viewer position relative to fitted bounding vertices, perspective projection with layback
    /// - Camera: viewer position relative to table bottom center, standard perspective projection
    /// - Window: viewer position relative to screen bottom center, oblique projection
    pub bg_view_mode_desktop: Option<ViewLayoutMode>,
    /// Viewport rotation in degrees (ROTA)
    /// Rotates the entire viewport, useful for portrait/landscape orientations
    /// Default: 0 degrees
    pub bg_rotation_desktop: f32,
    /// Look-at / inclination setting (INCL)
    /// - Legacy mode: look-at as percent of table height from top 0% to table front 100%
    /// - Camera mode: look-at as percent of table height from bottom (0.25 = around slingshots)
    ///
    /// Default: 0 degrees (legacy mode)
    pub bg_inclination_desktop: f32,
    /// Layback angle in degrees (LAYB)
    /// Legacy mode only: skewing angle that deforms the table perspective
    /// Default: 0 degrees
    pub bg_layback_desktop: f32,
    /// Field of view in degrees (FOVX)
    /// Camera & Legacy modes: vertical field of view
    /// Default: 45 degrees
    pub bg_fov_desktop: f32,
    /// Camera X position in VPU (XLTX)
    /// - Legacy mode: offset from fitted camera position
    /// - Camera/Window mode: position relative to table bottom center
    ///
    /// Default: 0 VPU
    pub bg_offset_x_desktop: f32,
    /// Camera Y position in VPU (XLTY)
    /// - Legacy mode: offset from fitted camera position
    /// - Camera/Window mode: position relative to table bottom center
    ///
    /// Default: 30 VPU (about 1.6 cm)
    pub bg_offset_y_desktop: f32,
    /// Camera Z position in VPU (XLTZ)
    /// - Legacy mode: offset from fitted camera position (negative moves camera back)
    /// - Camera/Window mode: height relative to table playfield
    ///
    /// Default: -200 VPU (about -10.8 cm)
    pub bg_offset_z_desktop: f32,
    /// Scene scale X (SCLX) - multiplier
    /// Default: 1.0
    pub bg_scale_x_desktop: f32,
    /// Scene scale Y (SCLY) - multiplier
    /// Default: 1.0
    pub bg_scale_y_desktop: f32,
    /// Scene scale Z (SCLZ) - multiplier
    /// Default: 1.0
    pub bg_scale_z_desktop: f32,
    /// Enable Full Single Screen mode (EFSS)
    /// When true, FSS view settings are used when available
    pub bg_enable_fss: Option<bool>,
    /// Horizontal frustum offset (HOF0, added in 10.8.x)
    /// Camera & Window modes: shifts the view frustum horizontally
    /// Default: 0
    pub bg_view_horizontal_offset_desktop: Option<f32>,
    /// Vertical frustum offset (VOF0, added in 10.8.x)
    /// Camera & Window modes: shifts the view frustum vertically
    /// Default: 0
    pub bg_view_vertical_offset_desktop: Option<f32>,
    /// Window mode: top edge X offset in VPU (WTX0, added in 10.8.x)
    pub bg_window_top_x_offset_desktop: Option<f32>,
    /// Window mode: top edge Y offset in VPU (WTY0, added in 10.8.x)
    pub bg_window_top_y_offset_desktop: Option<f32>,
    /// Window mode: top edge Z offset in VPU (WTZ0, added in 10.8.x)
    /// Height of upper window/glass border above playfield
    /// Default: 20 cm → ~370.6 VPU (from ViewSetup.h)
    pub bg_window_top_z_offset_desktop: Option<f32>,
    /// Window mode: bottom edge X offset in VPU (WBX0, added in 10.8.x)
    pub bg_window_bottom_x_offset_desktop: Option<f32>,
    /// Window mode: bottom edge Y offset in VPU (WBY0, added in 10.8.x)
    pub bg_window_bottom_y_offset_desktop: Option<f32>,
    /// Window mode: bottom edge Z offset in VPU (WBZ0, added in 10.8.x)
    /// Height of lower window/glass border above playfield
    /// Default: 7.5 cm → ~139 VPU (from ViewSetup.h)
    pub bg_window_bottom_z_offset_desktop: Option<f32>,

    // =====================================================================================
    // FULLSCREEN VIEW SETTINGS (BG_FULLSCREEN)
    // This is for use with a pinball cabinet setup with a separate monitor for the backglass,
    // where the playfield is displayed on a monitor in landscape orientation,
    // and the backglass on a second monitor in portrait orientation above it.
    //
    // Default values (legacy VPX format, stored in VPU):
    // - Mode: Legacy (VLM_LEGACY) when not set
    // - Rotation: 0 degrees
    // - Inclination: 0 degrees
    // - Layback: 0 degrees
    // - FOV: 45 degrees
    // - Offset X: 110 VPU (about 5.9 cm)
    // - Offset Y: -86 VPU (about -4.6 cm)
    // - Offset Z: 400 VPU (about 21.6 cm)
    // - Scale X: 1.3, Scale Y: 1.41, Scale Z: 1.0
    // =====================================================================================
    /// View layout mode for fullscreen view (VSM1, added in 10.8.x)
    pub bg_view_mode_fullscreen: Option<ViewLayoutMode>,
    /// Viewport rotation in degrees (ROTF)
    /// Default: 0 degrees
    pub bg_rotation_fullscreen: f32,
    /// Look-at / inclination setting (INCF)
    /// - Legacy mode: look-at as percent of table height from top 0% to table front 100%
    /// - Camera mode: look-at as percent of table height from bottom
    ///
    /// Default: 0 degrees
    pub bg_inclination_fullscreen: f32,
    /// Layback angle in degrees (LAYF) - Legacy mode only
    /// Default: 0 degrees
    pub bg_layback_fullscreen: f32,
    /// Field of view in degrees (FOVF)
    /// Default: 45 degrees
    pub bg_fov_fullscreen: f32,
    /// Camera X position in VPU (XLFX)
    /// Default: 110 VPU (about 5.9 cm)
    pub bg_offset_x_fullscreen: f32,
    /// Camera Y position in VPU (XLFY)
    /// Default: -86 VPU (about -4.6 cm)
    pub bg_offset_y_fullscreen: f32,
    /// Camera Z position in VPU (XLFZ)
    /// Default: 400 VPU (about 21.6 cm)
    pub bg_offset_z_fullscreen: f32,
    /// Scene scale X (SCFX) - multiplier
    /// Default: 1.3
    pub bg_scale_x_fullscreen: f32,
    /// Scene scale Y (SCFY) - multiplier
    /// Default: 1.41
    pub bg_scale_y_fullscreen: f32,
    /// Scene scale Z (SCFZ) - multiplier
    /// Default: 1.0
    pub bg_scale_z_fullscreen: f32,
    /// Horizontal frustum offset (HOF1, added in 10.8.x)
    /// Default: 0
    pub bg_view_horizontal_offset_fullscreen: Option<f32>,
    /// Vertical frustum offset (VOF1, added in 10.8.x)
    /// Default: 0
    pub bg_view_vertical_offset_fullscreen: Option<f32>,
    /// Window mode: top edge X offset in VPU (WTX1, added in 10.8.x)
    pub bg_window_top_x_offset_fullscreen: Option<f32>,
    /// Window mode: top edge Y offset in VPU (WTY1, added in 10.8.x)
    pub bg_window_top_y_offset_fullscreen: Option<f32>,
    /// Window mode: top edge Z offset in VPU (WTZ1, added in 10.8.x)
    /// Default: 20 cm → ~370.6 VPU (from ViewSetup.h)
    pub bg_window_top_z_offset_fullscreen: Option<f32>,
    /// Window mode: bottom edge X offset in VPU (WBX1, added in 10.8.x)
    pub bg_window_bottom_x_offset_fullscreen: Option<f32>,
    /// Window mode: bottom edge Y offset in VPU (WBY1, added in 10.8.x)
    pub bg_window_bottom_y_offset_fullscreen: Option<f32>,
    /// Window mode: bottom edge Z offset in VPU (WBZ1, added in 10.8.x)
    /// Default: 7.5 cm → ~139 VPU (from ViewSetup.h)
    pub bg_window_bottom_z_offset_fullscreen: Option<f32>,

    // =====================================================================================
    // FULL SINGLE SCREEN (FSS) VIEW SETTINGS (BG_FSS)
    // This view is designed for single-screen setups where the playfield
    // and 3d backglass are displayed on one monitor in portrait orientation. (e.g. mobile phone)
    //
    // Default values (from ViewSetup.h):
    // Note: ViewSetup.h specifies defaults in centimeters, converted to VPU internally.
    // The values stored in VPX files are in VPU.
    //
    // - Mode: Legacy (VLM_LEGACY)
    // - Rotation: 0 degrees
    // - Inclination/LookAt: 0.25 (legacy: degrees, camera: 25% from bottom)
    // - Layback: 0 degrees
    // - FOV: 45 degrees
    // - Offset X: 0 cm → 0 VPU
    // - Offset Y: 20 cm → ~370.6 VPU (position relative to table bottom center)
    // - Offset Z: 70 cm → ~1297 VPU (camera height)
    // - Scale X/Y/Z: 1.0
    // - Horizontal/Vertical offset: 0
    // - Window Top Z: 20 cm → ~370.6 VPU (glass top height above playfield)
    // - Window Bottom Z: 7.5 cm → ~139 VPU (glass bottom height above playfield)
    //
    // Note: The commented defaults in Default::default() (52, 30, -50, 1.2, 1.1)
    // are legacy VPX file defaults, different from the ViewSetup struct defaults.
    // =====================================================================================
    /// View layout mode for FSS view (VSM2, added in 10.8.x)
    /// Default: Legacy (VLM_LEGACY)
    pub bg_view_mode_full_single_screen: Option<ViewLayoutMode>,
    /// Viewport rotation in degrees (ROFS, added in 10.?)
    /// Default: 0 degrees
    pub bg_rotation_full_single_screen: Option<f32>,
    /// Look-at / inclination setting (INFS, added in 10.?)
    /// - Legacy mode:look-at as percent of table height from top 0% to table front 100%
    /// - Camera mode: look-at as percent of table height from bottom
    ///
    /// Default: 0.25 (legacy file default was 52 percent)
    pub bg_inclination_full_single_screen: Option<f32>,
    /// Layback angle in degrees (LAFS, added in 10.?) - Legacy mode only
    /// Default: 0 degrees
    pub bg_layback_full_single_screen: Option<f32>,
    /// Field of view in degrees (FOFS, added in 10.?)
    /// Default: 45 degrees
    pub bg_fov_full_single_screen: Option<f32>,
    /// Camera X position in VPU (XLXS, added in 10.?)
    /// Default: 0 cm → 0 VPU
    pub bg_offset_x_full_single_screen: Option<f32>,
    /// Camera Y position in VPU (XLYS, added in 10.?)
    /// - Legacy mode: offset from fitted camera position
    /// - Camera/Window mode: position relative to table bottom center
    ///   Default: 20 cm → ~370.6 VPU (legacy file default was 30 VPU)
    pub bg_offset_y_full_single_screen: Option<f32>,
    /// Camera Z position in VPU (XLZS, added in 10.?)
    /// - Legacy mode: offset from fitted camera position (negative moves back)
    /// - Camera/Window mode: height relative to playfield
    ///
    /// Default: 70 cm → ~1297 VPU (legacy file default was -50 VPU)
    pub bg_offset_z_full_single_screen: Option<f32>,
    /// Scene scale X (SCXS, added in 10.?) - multiplier
    /// Default: 1.0 (legacy file default was 1.2)
    pub bg_scale_x_full_single_screen: Option<f32>,
    /// Scene scale Y (SCYS, added in 10.?) - multiplier
    /// Default: 1.0 (legacy file default was 1.1)
    pub bg_scale_y_full_single_screen: Option<f32>,
    /// Scene scale Z (SCZS, added in 10.?) - multiplier
    /// Default: 1.0
    pub bg_scale_z_full_single_screen: Option<f32>,
    /// Horizontal frustum offset as percentage (HOF2, added in 10.8.x)
    /// Camera & Window modes only
    /// Default: 0
    pub bg_view_horizontal_offset_full_single_screen: Option<f32>,
    /// Vertical frustum offset as percentage (VOF2, added in 10.8.x)
    /// Camera & Window modes only
    /// Default: 0
    pub bg_view_vertical_offset_full_single_screen: Option<f32>,
    /// Window mode: top edge X offset in VPU (WTX2, added in 10.8.x)
    pub bg_window_top_x_offset_full_single_screen: Option<f32>,
    /// Window mode: top edge Y offset in VPU (WTY2, added in 10.8.x)
    pub bg_window_top_y_offset_full_single_screen: Option<f32>,
    /// Window mode: top edge Z offset in VPU (WTZ2, added in 10.8.x)
    /// Height of upper window/glass border above playfield
    /// Default: 20 cm → ~370.6 VPU
    pub bg_window_top_z_offset_full_single_screen: Option<f32>,
    /// Window mode: bottom edge X offset in VPU (WBX2, added in 10.8.x)
    pub bg_window_bottom_x_offset_full_single_screen: Option<f32>,
    /// Window mode: bottom edge Y offset in VPU (WBY2, added in 10.8.x)
    pub bg_window_bottom_y_offset_full_single_screen: Option<f32>,
    /// Window mode: bottom edge Z offset in VPU (WBZ2, added in 10.8.x)
    /// Height of lower window/glass border above playfield
    /// Default: 7.5 cm → ~139 VPU
    pub bg_window_bottom_z_offset_full_single_screen: Option<f32>,

    // =====================================================================================
    // END OF VIEW SETTINGS
    // =====================================================================================
    pub override_physics: u32,                  // ORRP 36
    pub override_physics_flipper: Option<bool>, // ORPF 37 added in ?
    pub gravity: f32,                           // GAVT 38
    pub friction: f32,                          // FRCT 39
    pub elasticity: f32,                        // ELAS 40
    pub elastic_falloff: f32,                   // ELFA 41
    pub scatter: f32,                           // PFSC 42
    pub default_scatter: f32,                   // SCAT 43
    pub nudge_time: f32,                        // NDGT 44
    pub plunger_normalize: Option<u32>,         // MPGC 45
    pub plunger_filter: Option<bool>,           // MPDF 46
    pub physics_max_loops: u32,                 // PHML 47
    pub render_em_reels: bool,                  // REEL 48
    pub render_decals: bool,                    // DECL 49
    pub offset_x: f32,                          // OFFX 50
    pub offset_y: f32,                          // OFFY 51
    /// Editor zoom level — controls the scale factor for the 2D editor view.
    ///
    /// This is a display-only setting that determines how the table is rendered
    /// in VPinball's editor canvas. It converts between table coordinates (VPU)
    /// and screen pixels: `screen_pos = (table_pos - offset) * zoom`.
    ///
    /// - Default: `0.5`
    /// - Range: `0.126` (`MIN_ZOOM`) to `63.9` (`MAX_ZOOM`)
    /// - Mouse wheel zoom steps by 1.5x (zoom in) or 0.5x (zoom out)
    ///
    /// Has no effect on the 3D camera or gameplay rendering.
    ///
    /// BIFF tag `ZOOM`
    pub zoom: f32,
    pub angle_tilt_max: f32,                            // SLPX 53
    pub angle_tilt_min: f32,                            // SLOP 54
    pub stereo_max_separation: Option<f32>,             // MAXS 55
    pub stereo_zero_parallax_displacement: Option<f32>, // ZPD 56
    pub stereo_offset: Option<f32>,                     // STO 57 (was missing in  10.01)
    pub overwrite_global_stereo3d: Option<bool>,        // OGST 58
    /// Playfield image — the name of the texture used for the playfield surface.
    ///
    /// This references an image from the table's texture collection by name.
    /// At render time, VPinball assigns this to the playfield primitive's texture
    /// (`m_d.m_szImage = m_ptable->m_image` in `primitive.cpp`).
    /// It is also used as the fallback in `GetSurfaceImage()` when a surface/ramp
    /// name lookup fails.
    ///
    /// The playfield primitive is identified purely by name: any [`Primitive`] named
    /// `"playfield_mesh"` (case-insensitive) is treated as the playfield and has
    /// this image (and [`playfield_material`](Self::playfield_material)) applied
    /// automatically — there is no explicit flag to mark a primitive as the
    /// playfield. See [`Primitive::is_playfield()`].
    ///
    /// In the 2D editor, this image is drawn as the backdrop when `m_backdrop` is
    /// enabled (for the playfield view; the backglass view uses `BG_image` instead).
    ///
    /// HDR images (`.exr`/`.hdr`) are not allowed for this field.
    ///
    /// Default: `""` (empty string — no playfield image; the surface is rendered
    /// using only the [`playfield_material`](Self::playfield_material) color with
    /// no texture)
    ///
    /// BIFF tag `IMAG`
    pub image: String,
    pub backglass_image_full_desktop: String,    // BIMG 60
    pub backglass_image_full_fullscreen: String, // BIMF 61
    pub backglass_image_full_single_screen: Option<String>, // BIMS 62 (added in 10.?)
    pub image_backdrop_night_day: bool,          // BIMN 63
    /// Color grading LUT image — the name of a 256×16 texture used as a 3D color
    /// lookup table (LUT) for post-processing color grading.
    ///
    /// This references an image from the table's texture collection by name.
    /// The texture must be exactly **256×16 pixels**, representing a flattened
    /// 16×16×16 3D LUT (16 blue slices of 16×16 red×green tiles laid out
    /// horizontally).
    ///
    /// When set, the `FBColorGrade` shader function applies the LUT as the final
    /// step of the tonemapping pipeline (after gamma and dithering). Each pixel's
    /// RGB color is remapped through the LUT, enabling effects like color
    /// correction, film emulation, or stylized looks.
    ///
    /// Exposed as `ColorGradeImage` in VBScript. Configured in the editor under
    /// Backglass Visuals properties.
    ///
    /// Default: `""` (empty string, no color grading applied)
    ///
    /// BIFF tag `IMCG`
    pub image_color_grade: String,
    /// Default ball texture (typically HDR environment map for reflections).
    ///
    /// Used as fallback when `Ball::image` is empty. This is the base texture
    /// for all balls, usually an HDR environment map for realistic reflections.
    /// See [`crate::vpx::gameitem::ball::Ball`] for texture/decal handling details.
    ///
    /// BIFF tag: `BLIM`
    pub ball_image: String,
    /// Whether to use spherical UV mapping for ball textures.
    ///
    /// - `false`: Equirectangular mapping (standard HDR environment map layout)
    /// - `true`: Spherical UV mapping (direct sphere projection)
    ///
    /// BIFF tag: `BLSM` (added in 10.8)
    pub ball_spherical_mapping: Option<bool>,
    /// Default ball decal/overlay texture.
    ///
    /// Used as fallback when `Ball::image_decal` is empty. This texture is
    /// overlaid on top of `ball_image`. How it's blended depends on `ball_decal_mode`:
    /// - `false` (scratches mode): Additive blend using alpha for surface wear
    /// - `true` (decal mode): Screen blend for logos/artwork
    ///
    /// See [`crate::vpx::gameitem::ball::Ball`] for detailed blending behavior.
    ///
    /// BIFF tag: `BLIF`
    pub ball_image_front: String,
    /// Environment map image — the name of an equirectangular texture used for
    /// image-based lighting (IBL) on all table surfaces.
    ///
    /// This references an image from the table's texture collection by name.
    /// The texture must have a **2:1 aspect ratio** (width = 2× height), as it is
    /// interpreted as an equirectangular environment map. HDR formats (`.exr`/`.hdr`)
    /// are supported and recommended for realistic lighting.
    ///
    /// The environment map is used for:
    /// - **Diffuse IBL**: Pre-convolved irradiance lookup via `DoEnvmapDiffuse`
    ///   in `Material.fxh`, scaled by `fenvEmissionScale`
    /// - **Glossy reflections**: Mip-mapped lookup via `DoEnvmapGlossy`, where
    ///   the mip level is derived from the material's glossy power (roughness)
    /// - **Clearcoat/specular layer**: Fresnel-weighted blend via `DoEnvmap2ndLayer`
    ///
    /// When empty, VPinball falls back to the built-in `Assets/EnvMap.webp`.
    ///
    /// Exposed as `EnvironmentImage` in VBScript. Configured in the editor under
    /// Table Lights properties.
    ///
    /// Default: `""` (empty string, uses built-in environment map)
    ///
    /// BIFF tag: `EIMG` (was missing in 10.01)
    pub env_image: Option<String>,
    pub notes: Option<String>,            // NOTX 67.5 (added in 10.7)
    pub screen_shot: String,              // SSHT 68
    pub display_backdrop: bool,           // FBCK 69
    pub glass_top_height: f32,            // GLAS 70
    pub glass_bottom_height: Option<f32>, // GLAB 70.5 (added in 10.8)
    pub table_height: Option<f32>,        // TBLH 71 (optional in 10.8)
    /// Name of the material applied to the playfield surface.
    ///
    /// References a [`Material`] by name from the table's material list.
    /// Controls the appearance (color, glossiness, reflections, etc.) of the
    /// main playfield surface. When empty, VPinball uses its built-in default
    /// material.
    ///
    /// Like [`image`](Self::image), this is applied automatically to any
    /// [`Primitive`] named `"playfield_mesh"` (case-insensitive) — there is no
    /// explicit flag to mark a primitive as the playfield.
    /// See [`Primitive::is_playfield()`].
    ///
    /// Exposed as `PlayfieldMaterial` in VBScript.
    ///
    /// Default: `""` (empty string — built-in default material)
    ///
    /// BIFF tag: `PLMA`
    pub playfield_material: String,
    /// Background color rendered behind the table scene.
    ///
    /// This is used as the clear color for the render target — it fills any
    /// area not covered by the playfield, walls, or other geometry. Visible
    /// mainly around the edges of the table or through gaps in the playfield.
    ///
    /// Stored in BGR format in the BIFF data.
    ///
    /// Default: `#626E8E` (Waikawa Gray — a muted bluish gray)
    ///
    /// BIFF tag: `BCLR`
    pub backdrop_color: Color,
    pub global_difficulty: f32, // TDFT 74
    /// changes the ambient light contribution for each material, please always try to keep this at full Black
    pub light_ambient: Color, // LZAM 75 (color)
    /// changes the light contribution for each material (currently light0 emission is copied to light1, too)
    pub light0_emission: Color, // LZDI 76 (color)
    pub light_height: f32,      // LZHI 77
    pub light_range: f32,       // LZRA 78
    pub light_emission_scale: f32, // LIES 79
    pub env_emission_scale: f32, // ENES 80
    pub global_emission_scale: f32, // GLES 81
    /// Intensity scale for the screen-space ambient occlusion (SSAO) effect.
    ///
    /// Controls how strongly ambient occlusion darkens creases and contact
    /// areas between objects. Higher values produce more pronounced shadowing.
    /// A value of `0.0` effectively disables the AO effect (the renderer also
    /// checks [`use_ao`](Self::use_ao) as a master toggle).
    ///
    /// Passed to the shader as the first component of `AO_scale_timeblur`.
    /// Displayed as a percentage in the editor.
    ///
    /// No hard upper bound is enforced; typical values are in the `0.0`–`2.0`
    /// range.
    ///
    /// Exposed as `AOScale` in VBScript.
    ///
    /// Default: `1.75`
    ///
    /// BIFF tag: `AOSC`
    pub ao_scale: f32,
    /// Intensity scale for the screen-space reflections (SSR) effect.
    ///
    /// Controls how strongly screen-space reflections appear on surfaces.
    /// Higher values produce more visible reflections. A value of `0.0` or
    /// less effectively disables the SSR effect (the renderer also checks
    /// [`use_ssr`](Self::use_ssr) as a master toggle).
    ///
    /// Passed to the shader as the third component of
    /// `SSR_bumpHeight_fresnelRefl_scale_FS`.
    /// Displayed as a percentage in the editor.
    ///
    /// No hard upper bound is enforced; typical values are in the `0.0`–`1.0`
    /// range.
    ///
    /// Exposed as `SSRScale` in VBScript.
    ///
    /// Default: `None` — VPinball's `SetLoadDefaults` uses `0.5`
    ///
    /// BIFF tag: `SSSC` (added in 10.?)
    pub ssr_scale: Option<f32>,
    pub ground_to_lockbar_height: Option<f32>, // CLBH (added in 10.8.x)
    pub table_sound_volume: f32,               // SVOL 84
    pub table_music_volume: f32,               // MVOL 85
    pub table_adaptive_vsync: Option<i32>,     // AVSY 86 (became optional in 10.8)
    pub use_reflection_for_balls: Option<i32>, // BREF 87 (became optional in 10.8)
    pub brst: Option<i32>,                     // BRST (in use in 10.01)
    /// Default playfield reflection strength for objects.
    ///
    /// Controls how strongly objects reflect on the playfield surface.
    /// Range: 0.0 (no reflections) to 1.0 (full reflection strength).
    /// Default is 0.2 (20%).
    ///
    /// Note: This is used to initialize `Primitive::reflection_strength` for
    /// the playfield mesh. Individual objects can override with their own
    /// `reflection_strength` setting.
    ///
    /// BIFF tag: `PLST`
    pub playfield_reflection_strength: f32,
    pub use_trail_for_balls: Option<i32>, // BTRA 89 (became optional in 10.8)
    /// Default decal mode for all balls.
    ///
    /// Controls how `ball_image_front` is blended onto balls:
    /// - `false` (scratches mode): Decal is an alpha scratch texture, blended additively.
    ///   Scratches affect both diffuse and specular, creating surface wear effects.
    /// - `true` (decal mode): Decal is a proper logo/image, blended using screen blend.
    ///
    /// Individual balls can override this with `Ball::decal_mode`.
    /// See [`crate::vpx::gameitem::ball::Ball`] for detailed blending behavior.
    ///
    /// BIFF tag: `BDMO`
    pub ball_decal_mode: bool,
    /// Default strength of ball reflections on the playfield.
    ///
    /// Controls how visible balls are in the playfield reflection pass.
    /// Range: 0.0 (invisible) to 1.0 (full reflection). Default is 1.0.
    ///
    /// Individual balls can override with `Ball::playfield_reflection_strength`.
    ///
    /// BIFF tag: `BPRS` (was missing in 10.01)
    pub ball_playfield_reflection_strength: Option<f32>,
    /// Default bulb light intensity scale for balls.
    ///
    /// Controls how strongly point lights (bulbs) affect ball surfaces.
    /// Individual balls can override with `Ball::bulb_intensity_scale`.
    ///
    /// BIFF tag: `DBIS` (added in 10.?)
    pub default_bulb_intensity_scale_on_ball: Option<f32>,
    /// this has a special quantization,
    /// See [`Self::get_ball_trail_strength`] and [`Self::set_ball_trail_strength`]
    pub ball_trail_strength: Option<u32>, // BTST 93 (became optional in 10.8)
    pub user_detail_level: Option<u32>, // ARAC 94 (became optional in 10.8)
    pub overwrite_global_detail_level: Option<bool>, // OGAC 95 (became optional in 10.8)
    pub overwrite_global_day_night: Option<bool>, // OGDN 96 (became optional in 10.8)
    /// Whether to display the editor grid overlay in the 2D table editor.
    ///
    /// When `true`, VPinball draws a grid on top of the playfield in the editor
    /// view to help with aligning and positioning game items. This is an
    /// editor-only setting and has no effect during gameplay. The grid spacing
    /// is controlled by a separate editor preference (`m_gridSize`), not stored
    /// in the table file.
    ///
    /// Exposed as `DisplayGrid` in VBScript.
    ///
    /// Default: `true`
    ///
    /// BIFF tag: `GDAC`
    pub show_grid: bool,
    /// **Deprecated.** Formerly controlled whether game elements (flippers,
    /// bumpers, etc.) were rendered in the playfield reflection pass.
    ///
    /// In current VPinball, reflection is controlled per-item via each object's
    /// `m_reflectionEnabled` field (`ReflectionEnabled` in VBScript). The
    /// `REOP` tag is still read from older table files for backward
    /// compatibility but is no longer written. The VBScript property
    /// `ReflectElementsOnPlayfield` logs a deprecation error and always
    /// returns `true`.
    ///
    /// Default: `None` (not written; old files defaulted to `true`)
    ///
    /// BIFF tag: `REOP`
    pub reflect_elements_on_playfield: Option<bool>,
    /// **Deprecated (pre-10.8).** Formerly stored the table's anti-aliasing
    /// override setting.
    ///
    /// Before 10.8, per-table rendering overrides were stored in the table
    /// file. In 10.8+ these were moved to a per-user `.ini` file. When loading
    /// an old table without an `.ini`, VPinball imports this value into the
    /// user settings (`Player_AAFactor`): `0` → 1× (off), any other non-`-1`
    /// value → 2× supersampling. A value of `-1` meant "use global setting".
    ///
    /// This tag is no longer written by VPinball.
    ///
    /// The VBScript property `EnableAntiAliasing` is a no-op.
    ///
    /// Default: `None` (not written since 10.8; old files used `-1`)
    ///
    /// BIFF tag: `UAAL` (became optional in 10.8)
    pub use_aal: Option<i32>,
    /// **Deprecated (pre-10.8).** Formerly stored the table's FXAA override
    /// setting.
    ///
    /// Like [`use_aal`](Self::use_aal), this was moved to per-user settings in
    /// 10.8. When loading an old table without an `.ini`, VPinball imports this
    /// value into `Player_FXAA`. A value of `-1` meant "use global setting".
    ///
    /// This tag is no longer written by VPinball.
    ///
    /// The VBScript property `EnableFXAA` is a no-op.
    ///
    /// Default: `None` (not written since 10.8; old files used `-1`)
    ///
    /// BIFF tag: `UFXA` (became optional in 10.8)
    pub use_fxaa: Option<i32>,
    /// Whether screen-space ambient occlusion (SSAO) is enabled for this table.
    ///
    /// When enabled, VPinball darkens creases and contact areas between objects
    /// to simulate indirect light shadowing, adding depth to the scene.
    /// The effect strength is controlled by [`ao_scale`](Self::ao_scale).
    ///
    /// Before 10.8, a value of `-1` meant "use global video option". In 10.8+
    /// this is treated as a boolean (`0` = off, non-zero = on) and is still
    /// written to the file. Internally stored as `bool m_enableAO` in VPinball.
    ///
    /// Exposed as `EnableAO` in VBScript.
    ///
    /// Default: `None` (old files used `-1`; `SetLoadDefaults` uses `true`)
    ///
    /// BIFF tag: `UAOC` (became optional in 10.8)
    pub use_ao: Option<i32>,
    /// Whether screen-space reflections (SSR) are enabled for this table.
    ///
    /// When enabled, VPinball renders approximate reflections of objects on
    /// nearby surfaces using screen-space ray marching. The effect strength is
    /// controlled by [`ssr_scale`](Self::ssr_scale).
    ///
    /// Before 10.8, a value of `-1` meant "use global video option". In 10.8+
    /// this is treated as a boolean (`0` = off, non-zero = on) and is still
    /// written to the file. Internally stored as `bool m_enableSSR` in VPinball.
    ///
    /// Exposed as `EnableSSR` in VBScript.
    ///
    /// Default: `None` (old files used `-1`; `SetLoadDefaults` uses `true`)
    ///
    /// BIFF tag: `USSR` (added in 10.?)
    pub use_ssr: Option<i32>,
    pub tone_mapper: Option<ToneMapper>, // TMAP 102.5 (added in 10.8)
    /// Strength of the bloom post-processing effect.
    ///
    /// Controls how intensely bright areas of the scene bleed light into
    /// surrounding pixels. In the shader (`FBShader.hlsl`), this value is
    /// multiplied into the bloom cutoff result — higher values produce a
    /// stronger, more visible glow around bright spots (lights, reflections).
    ///
    /// A value of `0.0` effectively disables bloom. There is no hard upper
    /// bound enforced in VPinball; typical values range from `0.0` to `2.0`,
    /// but values above `2.0` are accepted.
    ///
    /// Exposed as `BloomStrength` in VBScript.
    ///
    /// Default: `1.8` — VPinball's `SetLoadDefaults()` uses `1.0` as the
    /// pre-load fallback when the `BLST` tag is missing from the file.
    ///
    /// BIFF tag: `BLST`
    pub bloom_strength: f32,
    pub materials_size: u32, // MASI 104
    /// Legacy material saving for backward compatibility
    pub materials_old: Vec<SaveMaterial>, // MATE 105 (only for <10.8)
    /// Legacy material saving for backward compatibility
    pub materials_physics_old: Option<Vec<SavePhysicsMaterial>>, // PHMA 106 (only for <10.8, added in 10.?)
    /// 10.8+ material saving (this format supports new properties, and can be extended in future versions, and does not perform quantizations)
    pub materials: Option<Vec<Material>>, // MATR (added in 10.8)
    pub render_probes: Option<Vec<RenderProbeWithGarbage>>, // RPRB (added in 10.8)
    pub gameitems_size: u32,                                // SEDT 107
    pub sounds_size: u32,                                   // SSND 108
    pub images_size: u32,                                   // SIMG 109
    pub fonts_size: u32,                                    // SFNT 110
    pub collections_size: u32,                              // SCOL 111
    pub custom_colors: [Color; 16],                         //[Color; 16], // CCUS 113
    pub protection_data: Option<Vec<u8>>,                   // SECB (removed in ?)
    pub code: StringWithEncoding,                           // CODE 114
    /// TLCK (added in 10.8 for tournament mode?)
    /// Flag that disables all table edition. Lock toggles are counted to identify
    /// version changes in a table (for example to guarantee untouched table for tournament)
    /// Used to be a boolean for a while in the 10.8 dev cycle but now is a lock counter.
    pub locked: Option<u32>, // TLCK (added in 10.8 for tournament mode?)

    /// Exposure value for the table (EXPO)
    /// Added in 10.8.1, defaults to 1.0
    pub exposure: Option<f32>,
    // This is a bit of a hack because we want reproducible builds.
    // 10.8.0 beta 1-4 had EFSS at the old location, but it was moved to the new location in beta 5
    // Some tables were released with these old betas, so we need to support both locations to be 100% reproducing the orignal table
    // and it's MAC hash.
    pub is_10_8_0_beta1_to_beta4: bool,
}

#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct GameDataJson {
    pub left: f32,
    pub top: f32,
    pub right: f32,
    pub bottom: f32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub camera_layout_mode: Option<u32>,
    pub bg_view_mode_desktop: Option<ViewLayoutMode>,
    pub bg_rotation_desktop: f32,
    pub bg_inclination_desktop: f32,
    pub bg_layback_desktop: f32,
    pub bg_fov_desktop: f32,
    pub bg_offset_x_desktop: f32,
    pub bg_offset_y_desktop: f32,
    pub bg_offset_z_desktop: f32,
    pub bg_scale_x_desktop: f32,
    pub bg_scale_y_desktop: f32,
    pub bg_scale_z_desktop: f32,
    pub bg_enable_fss: Option<bool>,
    pub bg_view_horizontal_offset_desktop: Option<f32>,
    pub bg_view_vertical_offset_desktop: Option<f32>,
    pub bg_window_top_x_offset_desktop: Option<f32>,
    pub bg_window_top_y_offset_desktop: Option<f32>,
    pub bg_window_top_z_offset_desktop: Option<f32>,
    pub bg_window_bottom_x_offset_desktop: Option<f32>,
    pub bg_window_bottom_y_offset_desktop: Option<f32>,
    pub bg_window_bottom_z_offset_desktop: Option<f32>,
    pub bg_view_mode_fullscreen: Option<ViewLayoutMode>,
    pub bg_rotation_fullscreen: f32,
    pub bg_inclination_fullscreen: f32,
    pub bg_layback_fullscreen: f32,
    pub bg_fov_fullscreen: f32,
    pub bg_offset_x_fullscreen: f32,
    pub bg_offset_y_fullscreen: f32,
    pub bg_offset_z_fullscreen: f32,
    pub bg_scale_x_fullscreen: f32,
    pub bg_scale_y_fullscreen: f32,
    pub bg_scale_z_fullscreen: f32,
    pub bg_view_horizontal_offset_fullscreen: Option<f32>,
    pub bg_view_vertical_offset_fullscreen: Option<f32>,
    pub bg_window_top_x_offset_fullscreen: Option<f32>,
    pub bg_window_top_y_offset_fullscreen: Option<f32>,
    pub bg_window_top_z_offset_fullscreen: Option<f32>,
    pub bg_window_bottom_x_offset_fullscreen: Option<f32>,
    pub bg_window_bottom_y_offset_fullscreen: Option<f32>,
    pub bg_window_bottom_z_offset_fullscreen: Option<f32>,
    pub bg_view_mode_full_single_screen: Option<ViewLayoutMode>,
    pub bg_rotation_full_single_screen: Option<f32>,
    pub bg_inclination_full_single_screen: Option<f32>,
    pub bg_layback_full_single_screen: Option<f32>,
    pub bg_fov_full_single_screen: Option<f32>,
    pub bg_offset_x_full_single_screen: Option<f32>,
    pub bg_offset_y_full_single_screen: Option<F32WithNanInf>,
    pub bg_offset_z_full_single_screen: Option<f32>,
    pub bg_scale_x_full_single_screen: Option<f32>,
    pub bg_scale_y_full_single_screen: Option<F32WithNanInf>,
    pub bg_scale_z_full_single_screen: Option<f32>,
    pub bg_view_horizontal_offset_full_single_screen: Option<f32>,
    pub bg_view_vertical_offset_full_single_screen: Option<f32>,
    pub bg_window_top_x_offset_full_single_screen: Option<f32>,
    pub bg_window_top_y_offset_full_single_screen: Option<f32>,
    pub bg_window_top_z_offset_full_single_screen: Option<f32>,
    pub bg_window_bottom_x_offset_full_single_screen: Option<f32>,
    pub bg_window_bottom_y_offset_full_single_screen: Option<f32>,
    pub bg_window_bottom_z_offset_full_single_screen: Option<f32>,
    pub override_physics: u32,
    pub override_physics_flipper: Option<bool>,
    pub gravity: f32,
    pub friction: f32,
    pub elasticity: f32,
    pub elastic_falloff: f32,
    pub scatter: f32,
    pub default_scatter: f32,
    pub nudge_time: f32,
    pub plunger_normalize: Option<u32>,
    pub plunger_filter: Option<bool>,
    pub physics_max_loops: u32,
    pub render_em_reels: bool,
    pub render_decals: bool,
    pub offset_x: f32,
    pub offset_y: f32,
    pub zoom: f32,
    pub angle_tilt_max: f32,
    pub angle_tilt_min: f32,
    pub stereo_max_separation: Option<f32>,
    pub stereo_zero_parallax_displacement: Option<f32>,
    pub stereo_offset: Option<f32>,
    pub overwrite_global_stereo3d: Option<bool>,
    pub image: String,
    pub backglass_image_full_desktop: String,
    pub backglass_image_full_fullscreen: String,
    pub backglass_image_full_single_screen: Option<String>,
    pub image_backdrop_night_day: bool,
    pub image_color_grade: String,
    pub ball_image: String,
    pub ball_spherical_mapping: Option<bool>,
    pub ball_image_front: String,
    pub env_image: Option<String>,
    pub notes: Option<String>,
    pub screen_shot: String,
    pub display_backdrop: bool,
    pub glass_top_height: f32,
    pub glass_bottom_height: Option<f32>,
    pub table_height: Option<f32>,
    pub playfield_material: String,
    pub backdrop_color: Color,
    pub global_difficulty: f32,
    pub light_ambient: Color,
    pub light0_emission: Color,
    pub light_height: f32,
    pub light_range: f32,
    pub light_emission_scale: f32,
    pub env_emission_scale: f32,
    pub global_emission_scale: f32,
    pub ao_scale: f32,
    pub ssr_scale: Option<f32>,
    pub ground_to_lockbar_height: Option<f32>,
    pub table_sound_volume: f32,
    pub table_music_volume: f32,
    pub table_adaptive_vsync: Option<i32>,
    pub use_reflection_for_balls: Option<i32>,
    pub brst: Option<i32>,
    pub playfield_reflection_strength: f32,
    pub use_trail_for_balls: Option<i32>,
    pub ball_decal_mode: bool,
    pub ball_playfield_reflection_strength: Option<f32>,
    pub default_bulb_intensity_scale_on_ball: Option<f32>,
    pub ball_trail_strength: Option<u32>,
    pub user_detail_level: Option<u32>,
    pub overwrite_global_detail_level: Option<bool>,
    pub overwrite_global_day_night: Option<bool>,
    pub show_grid: bool,
    pub reflect_elements_on_playfield: Option<bool>,
    pub use_aal: Option<i32>,
    pub use_fxaa: Option<i32>,
    pub use_ao: Option<i32>,
    pub use_ssr: Option<i32>,
    pub tone_mapper: Option<ToneMapper>,
    pub bloom_strength: f32,
    pub name: String,
    pub custom_colors: [Color; 16],
    pub protection_data: Option<Vec<u8>>,
    //pub code: StringWithEncoding,
    pub locked: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exposure: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_10_8_0_beta1_to_beta4: Option<bool>,
}

impl GameDataJson {
    pub fn to_game_data(&self) -> GameData {
        GameData {
            left: self.left,
            top: self.top,
            right: self.right,
            bottom: self.bottom,
            camera_layout_mode: self.camera_layout_mode,
            bg_view_mode_desktop: self.bg_view_mode_desktop,
            bg_rotation_desktop: self.bg_rotation_desktop,
            bg_inclination_desktop: self.bg_inclination_desktop,
            bg_layback_desktop: self.bg_layback_desktop,
            bg_fov_desktop: self.bg_fov_desktop,
            bg_offset_x_desktop: self.bg_offset_x_desktop,
            bg_offset_y_desktop: self.bg_offset_y_desktop,
            bg_offset_z_desktop: self.bg_offset_z_desktop,
            bg_scale_x_desktop: self.bg_scale_x_desktop,
            bg_scale_y_desktop: self.bg_scale_y_desktop,
            bg_scale_z_desktop: self.bg_scale_z_desktop,
            bg_enable_fss: self.bg_enable_fss,
            bg_view_horizontal_offset_desktop: self.bg_view_horizontal_offset_desktop,
            bg_view_vertical_offset_desktop: self.bg_view_vertical_offset_desktop,
            bg_window_top_x_offset_desktop: self.bg_window_top_x_offset_desktop,
            bg_window_top_y_offset_desktop: self.bg_window_top_y_offset_desktop,
            bg_window_top_z_offset_desktop: self.bg_window_top_z_offset_desktop,
            bg_window_bottom_x_offset_desktop: self.bg_window_bottom_x_offset_desktop,
            bg_window_bottom_y_offset_desktop: self.bg_window_bottom_y_offset_desktop,
            bg_window_bottom_z_offset_desktop: self.bg_window_bottom_z_offset_desktop,
            bg_view_mode_fullscreen: self.bg_view_mode_fullscreen,
            bg_rotation_fullscreen: self.bg_rotation_fullscreen,
            bg_inclination_fullscreen: self.bg_inclination_fullscreen,
            bg_layback_fullscreen: self.bg_layback_fullscreen,
            bg_fov_fullscreen: self.bg_fov_fullscreen,
            bg_offset_x_fullscreen: self.bg_offset_x_fullscreen,
            bg_offset_y_fullscreen: self.bg_offset_y_fullscreen,
            bg_offset_z_fullscreen: self.bg_offset_z_fullscreen,
            bg_scale_x_fullscreen: self.bg_scale_x_fullscreen,
            bg_scale_y_fullscreen: self.bg_scale_y_fullscreen,
            bg_scale_z_fullscreen: self.bg_scale_z_fullscreen,
            bg_view_horizontal_offset_fullscreen: self.bg_view_horizontal_offset_fullscreen,
            bg_view_vertical_offset_fullscreen: self.bg_view_vertical_offset_fullscreen,
            bg_window_top_x_offset_fullscreen: self.bg_window_top_x_offset_fullscreen,
            bg_window_top_y_offset_fullscreen: self.bg_window_top_y_offset_fullscreen,
            bg_window_top_z_offset_fullscreen: self.bg_window_top_z_offset_fullscreen,
            bg_window_bottom_x_offset_fullscreen: self.bg_window_bottom_x_offset_fullscreen,
            bg_window_bottom_y_offset_fullscreen: self.bg_window_bottom_y_offset_fullscreen,
            bg_window_bottom_z_offset_fullscreen: self.bg_window_bottom_z_offset_fullscreen,
            bg_view_mode_full_single_screen: self.bg_view_mode_full_single_screen,
            bg_rotation_full_single_screen: self.bg_rotation_full_single_screen,
            bg_inclination_full_single_screen: self.bg_inclination_full_single_screen,
            bg_layback_full_single_screen: self.bg_layback_full_single_screen,
            bg_fov_full_single_screen: self.bg_fov_full_single_screen,
            bg_offset_x_full_single_screen: self.bg_offset_x_full_single_screen,
            bg_offset_y_full_single_screen: self.bg_offset_y_full_single_screen.map(f32::from),
            bg_offset_z_full_single_screen: self.bg_offset_z_full_single_screen,
            bg_scale_x_full_single_screen: self.bg_scale_x_full_single_screen,
            bg_scale_y_full_single_screen: self.bg_scale_y_full_single_screen.map(f32::from),
            bg_scale_z_full_single_screen: self.bg_scale_z_full_single_screen,
            bg_view_horizontal_offset_full_single_screen: self
                .bg_view_horizontal_offset_full_single_screen,
            bg_view_vertical_offset_full_single_screen: self
                .bg_view_vertical_offset_full_single_screen,
            bg_window_top_x_offset_full_single_screen: self
                .bg_window_top_x_offset_full_single_screen,
            bg_window_top_y_offset_full_single_screen: self
                .bg_window_top_y_offset_full_single_screen,
            bg_window_top_z_offset_full_single_screen: self
                .bg_window_top_z_offset_full_single_screen,
            bg_window_bottom_x_offset_full_single_screen: self
                .bg_window_bottom_x_offset_full_single_screen,
            bg_window_bottom_y_offset_full_single_screen: self
                .bg_window_bottom_y_offset_full_single_screen,
            bg_window_bottom_z_offset_full_single_screen: self
                .bg_window_bottom_z_offset_full_single_screen,
            override_physics: self.override_physics,
            override_physics_flipper: self.override_physics_flipper,
            gravity: self.gravity,
            friction: self.friction,
            elasticity: self.elasticity,
            elastic_falloff: self.elastic_falloff,
            scatter: self.scatter,
            default_scatter: self.default_scatter,
            nudge_time: self.nudge_time,
            plunger_normalize: self.plunger_normalize,
            plunger_filter: self.plunger_filter,
            physics_max_loops: self.physics_max_loops,
            render_em_reels: self.render_em_reels,
            render_decals: self.render_decals,
            offset_x: self.offset_x,
            offset_y: self.offset_y,
            zoom: self.zoom,
            angle_tilt_max: self.angle_tilt_max,
            angle_tilt_min: self.angle_tilt_min,
            stereo_max_separation: self.stereo_max_separation,
            stereo_zero_parallax_displacement: self.stereo_zero_parallax_displacement,
            stereo_offset: self.stereo_offset,
            overwrite_global_stereo3d: self.overwrite_global_stereo3d,
            image: self.image.clone(),
            backglass_image_full_desktop: self.backglass_image_full_desktop.clone(),
            backglass_image_full_fullscreen: self.backglass_image_full_fullscreen.clone(),
            backglass_image_full_single_screen: self.backglass_image_full_single_screen.clone(),
            image_backdrop_night_day: self.image_backdrop_night_day,
            image_color_grade: self.image_color_grade.clone(),
            ball_image: self.ball_image.clone(),
            ball_spherical_mapping: self.ball_spherical_mapping,
            ball_image_front: self.ball_image_front.clone(),
            env_image: self.env_image.clone(),
            notes: self.notes.clone(),
            screen_shot: self.screen_shot.clone(),
            display_backdrop: self.display_backdrop,
            glass_top_height: self.glass_top_height,
            glass_bottom_height: self.glass_bottom_height,
            table_height: self.table_height,
            playfield_material: self.playfield_material.clone(),
            backdrop_color: self.backdrop_color,
            global_difficulty: self.global_difficulty,
            light_ambient: self.light_ambient,
            light0_emission: self.light0_emission,
            light_height: self.light_height,
            light_range: self.light_range,
            light_emission_scale: self.light_emission_scale,
            env_emission_scale: self.env_emission_scale,
            global_emission_scale: self.global_emission_scale,
            ao_scale: self.ao_scale,
            ssr_scale: self.ssr_scale,
            ground_to_lockbar_height: self.ground_to_lockbar_height,
            table_sound_volume: self.table_sound_volume,
            table_music_volume: self.table_music_volume,
            table_adaptive_vsync: self.table_adaptive_vsync,
            use_reflection_for_balls: self.use_reflection_for_balls,
            brst: self.brst,
            playfield_reflection_strength: self.playfield_reflection_strength,
            use_trail_for_balls: self.use_trail_for_balls,
            ball_decal_mode: self.ball_decal_mode,
            ball_playfield_reflection_strength: self.ball_playfield_reflection_strength,
            default_bulb_intensity_scale_on_ball: self.default_bulb_intensity_scale_on_ball,
            ball_trail_strength: self.ball_trail_strength,
            user_detail_level: self.user_detail_level,
            overwrite_global_detail_level: self.overwrite_global_detail_level,
            overwrite_global_day_night: self.overwrite_global_day_night,
            show_grid: self.show_grid,
            reflect_elements_on_playfield: self.reflect_elements_on_playfield,
            use_aal: self.use_aal,
            use_fxaa: self.use_fxaa,
            use_ao: self.use_ao,
            use_ssr: self.use_ssr,
            tone_mapper: self.tone_mapper,
            bloom_strength: self.bloom_strength,
            // this data is loaded from a separate file
            materials_size: 0,
            // this data is loaded from a separate file
            materials_old: vec![],
            // this data is loaded from a separate file
            materials_physics_old: None,
            // this data is loaded from a separate file
            materials: None,
            // this data is loaded from a separate file
            render_probes: None,
            // this data is loaded from a separate file
            gameitems_size: 0,
            // this data is loaded from a separate file
            sounds_size: 0,
            // this data is loaded from a separate file
            images_size: 0,
            // this data is loaded from a separate file
            fonts_size: 0,
            // this data is loaded from a separate file
            collections_size: 0,
            name: self.name.clone(),
            custom_colors: self.custom_colors,
            protection_data: self.protection_data.clone(),
            code: StringWithEncoding::empty(),
            locked: self.locked,
            exposure: self.exposure,
            is_10_8_0_beta1_to_beta4: self.is_10_8_0_beta1_to_beta4.unwrap_or(false),
        }
    }

    pub fn from_game_data(game_data: &GameData) -> GameDataJson {
        GameDataJson {
            left: game_data.left,
            top: game_data.top,
            right: game_data.right,
            bottom: game_data.bottom,
            camera_layout_mode: game_data.camera_layout_mode,
            bg_view_mode_desktop: game_data.bg_view_mode_desktop,
            bg_rotation_desktop: game_data.bg_rotation_desktop,
            bg_inclination_desktop: game_data.bg_inclination_desktop,
            bg_layback_desktop: game_data.bg_layback_desktop,
            bg_fov_desktop: game_data.bg_fov_desktop,
            bg_offset_x_desktop: game_data.bg_offset_x_desktop,
            bg_offset_y_desktop: game_data.bg_offset_y_desktop,
            bg_offset_z_desktop: game_data.bg_offset_z_desktop,
            bg_scale_x_desktop: game_data.bg_scale_x_desktop,
            bg_scale_y_desktop: game_data.bg_scale_y_desktop,
            bg_scale_z_desktop: game_data.bg_scale_z_desktop,
            bg_enable_fss: game_data.bg_enable_fss,
            bg_view_horizontal_offset_desktop: game_data.bg_view_horizontal_offset_desktop,
            bg_view_vertical_offset_desktop: game_data.bg_view_vertical_offset_desktop,
            bg_window_top_x_offset_desktop: game_data.bg_window_top_x_offset_desktop,
            bg_window_top_y_offset_desktop: game_data.bg_window_top_y_offset_desktop,
            bg_window_top_z_offset_desktop: game_data.bg_window_top_z_offset_desktop,
            bg_window_bottom_x_offset_desktop: game_data.bg_window_bottom_x_offset_desktop,
            bg_window_bottom_y_offset_desktop: game_data.bg_window_bottom_y_offset_desktop,
            bg_window_bottom_z_offset_desktop: game_data.bg_window_bottom_z_offset_desktop,
            bg_view_mode_fullscreen: game_data.bg_view_mode_fullscreen,
            bg_rotation_fullscreen: game_data.bg_rotation_fullscreen,
            bg_inclination_fullscreen: game_data.bg_inclination_fullscreen,
            bg_layback_fullscreen: game_data.bg_layback_fullscreen,
            bg_fov_fullscreen: game_data.bg_fov_fullscreen,
            bg_offset_x_fullscreen: game_data.bg_offset_x_fullscreen,
            bg_offset_y_fullscreen: game_data.bg_offset_y_fullscreen,
            bg_offset_z_fullscreen: game_data.bg_offset_z_fullscreen,
            bg_scale_x_fullscreen: game_data.bg_scale_x_fullscreen,
            bg_scale_y_fullscreen: game_data.bg_scale_y_fullscreen,
            bg_scale_z_fullscreen: game_data.bg_scale_z_fullscreen,
            bg_view_horizontal_offset_fullscreen: game_data.bg_view_horizontal_offset_fullscreen,
            bg_view_vertical_offset_fullscreen: game_data.bg_view_vertical_offset_fullscreen,
            bg_window_top_x_offset_fullscreen: game_data.bg_window_top_x_offset_fullscreen,
            bg_window_top_y_offset_fullscreen: game_data.bg_window_top_y_offset_fullscreen,
            bg_window_top_z_offset_fullscreen: game_data.bg_window_top_z_offset_fullscreen,
            bg_window_bottom_x_offset_fullscreen: game_data.bg_window_bottom_x_offset_fullscreen,
            bg_window_bottom_y_offset_fullscreen: game_data.bg_window_bottom_y_offset_fullscreen,
            bg_window_bottom_z_offset_fullscreen: game_data.bg_window_bottom_z_offset_fullscreen,
            bg_view_mode_full_single_screen: game_data.bg_view_mode_full_single_screen,
            bg_rotation_full_single_screen: game_data.bg_rotation_full_single_screen,
            bg_inclination_full_single_screen: game_data.bg_inclination_full_single_screen,
            bg_layback_full_single_screen: game_data.bg_layback_full_single_screen,
            bg_fov_full_single_screen: game_data.bg_fov_full_single_screen,
            bg_offset_x_full_single_screen: game_data.bg_offset_x_full_single_screen,
            bg_offset_y_full_single_screen: game_data
                .bg_offset_y_full_single_screen
                .map(F32WithNanInf::from),
            bg_offset_z_full_single_screen: game_data.bg_offset_z_full_single_screen,
            bg_scale_x_full_single_screen: game_data.bg_scale_x_full_single_screen,
            bg_scale_y_full_single_screen: game_data
                .bg_scale_y_full_single_screen
                .map(F32WithNanInf::from),
            bg_scale_z_full_single_screen: game_data.bg_scale_z_full_single_screen,
            bg_view_horizontal_offset_full_single_screen: game_data
                .bg_view_horizontal_offset_full_single_screen,
            bg_view_vertical_offset_full_single_screen: game_data
                .bg_view_vertical_offset_full_single_screen,
            bg_window_top_x_offset_full_single_screen: game_data
                .bg_window_top_x_offset_full_single_screen,
            bg_window_top_y_offset_full_single_screen: game_data
                .bg_window_top_y_offset_full_single_screen,
            bg_window_top_z_offset_full_single_screen: game_data
                .bg_window_top_z_offset_full_single_screen,
            bg_window_bottom_x_offset_full_single_screen: game_data
                .bg_window_bottom_x_offset_full_single_screen,
            bg_window_bottom_y_offset_full_single_screen: game_data
                .bg_window_bottom_y_offset_full_single_screen,
            bg_window_bottom_z_offset_full_single_screen: game_data
                .bg_window_bottom_z_offset_full_single_screen,
            override_physics: game_data.override_physics,
            override_physics_flipper: game_data.override_physics_flipper,
            gravity: game_data.gravity,
            friction: game_data.friction,
            elasticity: game_data.elasticity,
            elastic_falloff: game_data.elastic_falloff,
            scatter: game_data.scatter,
            default_scatter: game_data.default_scatter,
            nudge_time: game_data.nudge_time,
            plunger_normalize: game_data.plunger_normalize,
            plunger_filter: game_data.plunger_filter,
            physics_max_loops: game_data.physics_max_loops,
            render_em_reels: game_data.render_em_reels,
            render_decals: game_data.render_decals,
            offset_x: game_data.offset_x,
            offset_y: game_data.offset_y,
            zoom: game_data.zoom,
            angle_tilt_max: game_data.angle_tilt_max,
            angle_tilt_min: game_data.angle_tilt_min,
            stereo_max_separation: game_data.stereo_max_separation,
            stereo_zero_parallax_displacement: game_data.stereo_zero_parallax_displacement,
            stereo_offset: game_data.stereo_offset,
            overwrite_global_stereo3d: game_data.overwrite_global_stereo3d,
            image: game_data.image.clone(),
            backglass_image_full_desktop: game_data.backglass_image_full_desktop.clone(),
            backglass_image_full_fullscreen: game_data.backglass_image_full_fullscreen.clone(),
            backglass_image_full_single_screen: game_data
                .backglass_image_full_single_screen
                .clone(),
            image_backdrop_night_day: game_data.image_backdrop_night_day,
            image_color_grade: game_data.image_color_grade.clone(),
            ball_image: game_data.ball_image.clone(),
            ball_spherical_mapping: game_data.ball_spherical_mapping,
            ball_image_front: game_data.ball_image_front.clone(),
            env_image: game_data.env_image.clone(),
            notes: game_data.notes.clone(),
            screen_shot: game_data.screen_shot.clone(),
            display_backdrop: game_data.display_backdrop,
            glass_top_height: game_data.glass_top_height,
            glass_bottom_height: game_data.glass_bottom_height,
            table_height: game_data.table_height,
            playfield_material: game_data.playfield_material.clone(),
            backdrop_color: game_data.backdrop_color,
            global_difficulty: game_data.global_difficulty,
            light_ambient: game_data.light_ambient,
            light0_emission: game_data.light0_emission,
            light_height: game_data.light_height,
            light_range: game_data.light_range,
            light_emission_scale: game_data.light_emission_scale,
            env_emission_scale: game_data.env_emission_scale,
            global_emission_scale: game_data.global_emission_scale,
            ao_scale: game_data.ao_scale,
            ssr_scale: game_data.ssr_scale,
            ground_to_lockbar_height: game_data.ground_to_lockbar_height,
            table_sound_volume: game_data.table_sound_volume,
            table_music_volume: game_data.table_music_volume,
            table_adaptive_vsync: game_data.table_adaptive_vsync,
            use_reflection_for_balls: game_data.use_reflection_for_balls,
            brst: game_data.brst,
            playfield_reflection_strength: game_data.playfield_reflection_strength,
            use_trail_for_balls: game_data.use_trail_for_balls,
            ball_decal_mode: game_data.ball_decal_mode,
            ball_playfield_reflection_strength: game_data.ball_playfield_reflection_strength,
            default_bulb_intensity_scale_on_ball: game_data.default_bulb_intensity_scale_on_ball,
            ball_trail_strength: game_data.ball_trail_strength,
            user_detail_level: game_data.user_detail_level,
            overwrite_global_detail_level: game_data.overwrite_global_detail_level,
            overwrite_global_day_night: game_data.overwrite_global_day_night,
            show_grid: game_data.show_grid,
            reflect_elements_on_playfield: game_data.reflect_elements_on_playfield,
            use_aal: game_data.use_aal,
            use_fxaa: game_data.use_fxaa,
            use_ao: game_data.use_ao,
            use_ssr: game_data.use_ssr,
            tone_mapper: game_data.tone_mapper,
            bloom_strength: game_data.bloom_strength,
            name: game_data.name.clone(),
            custom_colors: game_data.custom_colors,
            protection_data: game_data.protection_data.clone(),
            // code: game_data.code.clone(),
            locked: game_data.locked,
            exposure: game_data.exposure,
            is_10_8_0_beta1_to_beta4: Some(game_data.is_10_8_0_beta1_to_beta4)
                .filter(|x| x == &true),
        }
    }
}

impl GameData {
    pub fn set_code(&mut self, script: String) {
        self.code = StringWithEncoding::new(script);
    }

    pub fn get_ball_trail_strength(&self) -> Option<f32> {
        self.ball_trail_strength.map(|v| dequantize_u8(8, v as u8))
    }

    pub fn set_ball_trail_strength(&mut self, value: f32) {
        self.ball_trail_strength = Some(quantize_u8(8, value) as u32);
    }
}

impl Default for GameData {
    fn default() -> Self {
        GameData {
            left: 0.0,
            top: 0.0,
            right: 952.0,
            bottom: 2162.0,
            camera_layout_mode: None,
            bg_view_mode_desktop: None,
            bg_rotation_desktop: 0.0,
            bg_inclination_desktop: 0.0,
            bg_layback_desktop: 0.0,
            bg_fov_desktop: 45.0,
            bg_offset_x_desktop: 0.0,
            bg_offset_y_desktop: 30.0,
            bg_offset_z_desktop: -200.0,
            bg_scale_x_desktop: 1.0,
            bg_scale_y_desktop: 1.0,
            bg_scale_z_desktop: 1.0,
            bg_enable_fss: None, //false,
            is_10_8_0_beta1_to_beta4: false,
            bg_rotation_fullscreen: 0.0,
            bg_inclination_fullscreen: 0.0,
            bg_layback_fullscreen: 0.0,
            bg_fov_fullscreen: 45.0,
            bg_offset_x_fullscreen: 110.0,
            bg_offset_y_fullscreen: -86.0,
            bg_offset_z_fullscreen: 400.0,
            bg_scale_x_fullscreen: 1.3,
            bg_scale_y_fullscreen: 1.41,
            bg_scale_z_fullscreen: 1.0,
            bg_rotation_full_single_screen: None,    //0.0,
            bg_inclination_full_single_screen: None, //52.0,
            bg_layback_full_single_screen: None,     //0.0,
            bg_fov_full_single_screen: None,         //45.0,
            bg_offset_x_full_single_screen: None,    //0.0,
            bg_offset_y_full_single_screen: None,    //30.0,
            bg_offset_z_full_single_screen: None,    //-50.0,
            bg_scale_x_full_single_screen: None,     //1.2,
            bg_scale_y_full_single_screen: None,     //1.1,
            bg_scale_z_full_single_screen: None,     //1.0,
            override_physics: 0,
            override_physics_flipper: None, //false,
            gravity: 1.762985,
            friction: 0.075,
            elasticity: 0.25,
            elastic_falloff: 0.0,
            scatter: 0.0,
            default_scatter: 0.0,
            nudge_time: 5.0,
            plunger_normalize: None, // 100
            plunger_filter: None,    // false
            physics_max_loops: 0,
            render_em_reels: false,
            render_decals: false,
            offset_x: 476.0,
            offset_y: 1081.0,
            zoom: 0.5,
            angle_tilt_max: 6.0,
            angle_tilt_min: 6.0,
            stereo_max_separation: None,             // 0.015,
            stereo_zero_parallax_displacement: None, // 0.1,
            stereo_offset: None,
            overwrite_global_stereo3d: None, // false,
            image: String::new(),
            backglass_image_full_desktop: String::new(),
            backglass_image_full_fullscreen: String::new(),
            backglass_image_full_single_screen: None,
            image_backdrop_night_day: false,
            image_color_grade: String::new(),
            ball_image: String::new(),
            ball_spherical_mapping: None,
            ball_image_front: String::new(),
            env_image: None,
            notes: None,
            screen_shot: String::new(),
            display_backdrop: false,
            glass_top_height: 400.0,   // new default 210 for both
            glass_bottom_height: None, // new default 210 for both
            table_height: None,        //0.0,
            playfield_material: "".to_string(),
            backdrop_color: Color::from_rgb(0x626E8E), // Waikawa/Bluish Gray
            global_difficulty: 0.2,
            light_ambient: Color::rgb((0.1 * 255.) as u8, (0.1 * 255.) as u8, (0.1 * 255.) as u8),
            light0_emission: Color::rgb((0.4 * 255.) as u8, (0.4 * 255.) as u8, (0.4 * 255.) as u8),
            light_height: 5000.0,
            light_range: 4000000.0,
            light_emission_scale: 4000000.0,
            env_emission_scale: 2.0,
            global_emission_scale: 0.52,
            ao_scale: 1.75,
            ssr_scale: None, //1.0,
            ground_to_lockbar_height: None,
            table_sound_volume: 1.0,
            table_music_volume: 1.0,
            table_adaptive_vsync: None,     //-1,
            use_reflection_for_balls: None, //-1,
            brst: None,
            playfield_reflection_strength: 1.0,
            use_trail_for_balls: None, //-1,
            ball_decal_mode: false,
            ball_playfield_reflection_strength: None,
            default_bulb_intensity_scale_on_ball: None, //1.0,
            ball_trail_strength: None,                  //quantize_unsigned(8, 0.4901961),
            user_detail_level: None,                    //5,
            overwrite_global_detail_level: None,        //false,
            overwrite_global_day_night: None,           //false,
            show_grid: true,
            reflect_elements_on_playfield: None, //true,
            use_aal: None,                       //-1,
            use_fxaa: None,                      //-1,
            use_ao: None,                        //-1,
            use_ssr: None,                       //-1,
            tone_mapper: None,                   // 0 = TM_REINHARD,
            exposure: None,                      // 1.0
            bloom_strength: 1.8,
            materials_size: 0,
            materials_old: Vec::new(),
            materials_physics_old: None,
            materials: None,
            render_probes: None,
            gameitems_size: 0,
            sounds_size: 0,
            images_size: 0,
            fonts_size: 0,
            collections_size: 0,
            name: "Table1".to_string(), // seems to be the default name
            custom_colors: [Color::BLACK; 16],
            protection_data: None,
            code: StringWithEncoding::empty(),
            bg_view_horizontal_offset_desktop: None,
            bg_view_vertical_offset_desktop: None,
            bg_window_top_x_offset_desktop: None,
            bg_window_top_y_offset_desktop: None,
            bg_window_top_z_offset_desktop: None,
            bg_window_bottom_x_offset_desktop: None,
            bg_window_bottom_y_offset_desktop: None,
            bg_window_bottom_z_offset_desktop: None,
            bg_view_mode_fullscreen: None,
            bg_view_horizontal_offset_fullscreen: None,
            bg_view_vertical_offset_fullscreen: None,
            bg_window_top_x_offset_fullscreen: None,
            bg_window_top_y_offset_fullscreen: None,
            bg_window_top_z_offset_fullscreen: None,
            bg_window_bottom_x_offset_fullscreen: None,
            bg_window_bottom_y_offset_fullscreen: None,
            bg_window_bottom_z_offset_fullscreen: None,
            bg_view_mode_full_single_screen: None,
            bg_view_horizontal_offset_full_single_screen: None,
            bg_view_vertical_offset_full_single_screen: None,
            bg_window_top_x_offset_full_single_screen: None,
            bg_window_top_y_offset_full_single_screen: None,
            bg_window_top_z_offset_full_single_screen: None,
            bg_window_bottom_x_offset_full_single_screen: None,
            bg_window_bottom_y_offset_full_single_screen: None,
            bg_window_bottom_z_offset_full_single_screen: None,
            locked: None,
        }
    }
}

#[derive(Debug, PartialEq)]
pub struct Record {
    name: String,
    data: Vec<u8>,
}

pub fn write_all_gamedata_records(gamedata: &GameData, version: &Version) -> Vec<u8> {
    let mut writer = BiffWriter::new();
    // order is important
    writer.write_tagged_f32("LEFT", gamedata.left);
    writer.write_tagged_f32("TOPX", gamedata.top);
    writer.write_tagged_f32("RGHT", gamedata.right);
    writer.write_tagged_f32("BOTM", gamedata.bottom);
    if let Some(clmo) = gamedata.camera_layout_mode {
        writer.write_tagged_u32("CLMO", clmo);
    }

    if version.u32() >= 1080
        && !gamedata.is_10_8_0_beta1_to_beta4
        && let Some(efss) = gamedata.bg_enable_fss
    {
        writer.write_tagged_bool("EFSS", efss);
    }
    if let Some(vsm0) = &gamedata.bg_view_mode_desktop {
        writer.write_tagged_u32("VSM0", vsm0.into());
    }
    writer.write_tagged_f32("ROTA", gamedata.bg_rotation_desktop);
    writer.write_tagged_f32("INCL", gamedata.bg_inclination_desktop);
    writer.write_tagged_f32("LAYB", gamedata.bg_layback_desktop);
    writer.write_tagged_f32("FOVX", gamedata.bg_fov_desktop);
    writer.write_tagged_f32("XLTX", gamedata.bg_offset_x_desktop);
    writer.write_tagged_f32("XLTY", gamedata.bg_offset_y_desktop);
    writer.write_tagged_f32("XLTZ", gamedata.bg_offset_z_desktop);
    writer.write_tagged_f32("SCLX", gamedata.bg_scale_x_desktop);
    writer.write_tagged_f32("SCLY", gamedata.bg_scale_y_desktop);
    writer.write_tagged_f32("SCLZ", gamedata.bg_scale_z_desktop);

    if let Some(hof0) = gamedata.bg_view_horizontal_offset_desktop {
        writer.write_tagged_f32("HOF0", hof0);
    }
    if let Some(vof0) = gamedata.bg_view_vertical_offset_desktop {
        writer.write_tagged_f32("VOF0", vof0);
    }
    if let Some(wtx0) = gamedata.bg_window_top_x_offset_desktop {
        writer.write_tagged_f32("WTX0", wtx0);
    }
    if let Some(wty0) = gamedata.bg_window_top_y_offset_desktop {
        writer.write_tagged_f32("WTY0", wty0);
    }
    if let Some(wtz0) = gamedata.bg_window_top_z_offset_desktop {
        writer.write_tagged_f32("WTZ0", wtz0);
    }
    if let Some(wbx0) = gamedata.bg_window_bottom_x_offset_desktop {
        writer.write_tagged_f32("WBX0", wbx0);
    }
    if let Some(wby0) = gamedata.bg_window_bottom_y_offset_desktop {
        writer.write_tagged_f32("WBY0", wby0);
    }
    if let Some(wbz0) = gamedata.bg_window_bottom_z_offset_desktop {
        writer.write_tagged_f32("WBZ0", wbz0);
    }

    if let Some(vsm1) = &gamedata.bg_view_mode_fullscreen {
        writer.write_tagged_u32("VSM1", vsm1.into());
    }

    if (version.u32() < 1080 || gamedata.is_10_8_0_beta1_to_beta4)
        && let Some(efss) = gamedata.bg_enable_fss
    {
        writer.write_tagged_bool("EFSS", efss);
    }
    writer.write_tagged_f32("ROTF", gamedata.bg_rotation_fullscreen);
    writer.write_tagged_f32("INCF", gamedata.bg_inclination_fullscreen);
    writer.write_tagged_f32("LAYF", gamedata.bg_layback_fullscreen);
    writer.write_tagged_f32("FOVF", gamedata.bg_fov_fullscreen);
    writer.write_tagged_f32("XLFX", gamedata.bg_offset_x_fullscreen);
    writer.write_tagged_f32("XLFY", gamedata.bg_offset_y_fullscreen);
    writer.write_tagged_f32("XLFZ", gamedata.bg_offset_z_fullscreen);
    writer.write_tagged_f32("SCFX", gamedata.bg_scale_x_fullscreen);
    writer.write_tagged_f32("SCFY", gamedata.bg_scale_y_fullscreen);
    writer.write_tagged_f32("SCFZ", gamedata.bg_scale_z_fullscreen);
    if let Some(hof1) = gamedata.bg_view_horizontal_offset_fullscreen {
        writer.write_tagged_f32("HOF1", hof1);
    }
    if let Some(vof1) = gamedata.bg_view_vertical_offset_fullscreen {
        writer.write_tagged_f32("VOF1", vof1);
    }
    if let Some(wtx1) = gamedata.bg_window_top_x_offset_fullscreen {
        writer.write_tagged_f32("WTX1", wtx1);
    }
    if let Some(wty1) = gamedata.bg_window_top_y_offset_fullscreen {
        writer.write_tagged_f32("WTY1", wty1);
    }
    if let Some(wtz1) = gamedata.bg_window_top_z_offset_fullscreen {
        writer.write_tagged_f32("WTZ1", wtz1);
    }
    if let Some(wbx1) = gamedata.bg_window_bottom_x_offset_fullscreen {
        writer.write_tagged_f32("WBX1", wbx1);
    }
    if let Some(wby1) = gamedata.bg_window_bottom_y_offset_fullscreen {
        writer.write_tagged_f32("WBY1", wby1);
    }
    if let Some(wbz1) = gamedata.bg_window_bottom_z_offset_fullscreen {
        writer.write_tagged_f32("WBZ1", wbz1);
    }

    if let Some(vsm2) = &gamedata.bg_view_mode_full_single_screen {
        writer.write_tagged_u32("VSM2", vsm2.into());
    }
    if let Some(rofs) = gamedata.bg_rotation_full_single_screen {
        writer.write_tagged_f32("ROFS", rofs);
    }
    if let Some(infs) = gamedata.bg_inclination_full_single_screen {
        writer.write_tagged_f32("INFS", infs);
    }
    if let Some(lafs) = gamedata.bg_layback_full_single_screen {
        writer.write_tagged_f32("LAFS", lafs);
    }
    if let Some(fofs) = gamedata.bg_fov_full_single_screen {
        writer.write_tagged_f32("FOFS", fofs);
    }
    if let Some(xlxs) = gamedata.bg_offset_x_full_single_screen {
        writer.write_tagged_f32("XLXS", xlxs);
    }
    if let Some(xlys) = gamedata.bg_offset_y_full_single_screen {
        writer.write_tagged_f32("XLYS", xlys);
    }
    if let Some(xlzs) = gamedata.bg_offset_z_full_single_screen {
        writer.write_tagged_f32("XLZS", xlzs);
    }
    if let Some(scxs) = gamedata.bg_scale_x_full_single_screen {
        writer.write_tagged_f32("SCXS", scxs);
    }
    if let Some(scys) = gamedata.bg_scale_y_full_single_screen {
        writer.write_tagged_f32("SCYS", scys);
    }
    if let Some(sczs) = gamedata.bg_scale_z_full_single_screen {
        writer.write_tagged_f32("SCZS", sczs);
    }
    if let Some(hof2) = gamedata.bg_view_horizontal_offset_full_single_screen {
        writer.write_tagged_f32("HOF2", hof2);
    }
    if let Some(vof2) = gamedata.bg_view_vertical_offset_full_single_screen {
        writer.write_tagged_f32("VOF2", vof2);
    }
    if let Some(wtx2) = gamedata.bg_window_top_x_offset_full_single_screen {
        writer.write_tagged_f32("WTX2", wtx2);
    }
    if let Some(wty2) = gamedata.bg_window_top_y_offset_full_single_screen {
        writer.write_tagged_f32("WTY2", wty2);
    }
    if let Some(wtz2) = gamedata.bg_window_top_z_offset_full_single_screen {
        writer.write_tagged_f32("WTZ2", wtz2);
    }
    if let Some(wbx2) = gamedata.bg_window_bottom_x_offset_full_single_screen {
        writer.write_tagged_f32("WBX2", wbx2);
    }
    if let Some(wby2) = gamedata.bg_window_bottom_y_offset_full_single_screen {
        writer.write_tagged_f32("WBY2", wby2);
    }
    if let Some(wbz2) = gamedata.bg_window_bottom_z_offset_full_single_screen {
        writer.write_tagged_f32("WBZ2", wbz2);
    }

    writer.write_tagged_u32("ORRP", gamedata.override_physics);
    if let Some(orpf) = gamedata.override_physics_flipper {
        writer.write_tagged_bool("ORPF", orpf);
    }
    writer.write_tagged_f32("GAVT", gamedata.gravity);
    writer.write_tagged_f32("FRCT", gamedata.friction);
    writer.write_tagged_f32("ELAS", gamedata.elasticity);
    writer.write_tagged_f32("ELFA", gamedata.elastic_falloff);
    writer.write_tagged_f32("PFSC", gamedata.scatter);
    writer.write_tagged_f32("SCAT", gamedata.default_scatter);
    writer.write_tagged_f32("NDGT", gamedata.nudge_time);
    if let Some(mpgc) = gamedata.plunger_normalize {
        writer.write_tagged_u32("MPGC", mpgc);
    }
    if let Some(mpdf) = gamedata.plunger_filter {
        writer.write_tagged_bool("MPDF", mpdf);
    }
    writer.write_tagged_u32("PHML", gamedata.physics_max_loops);
    writer.write_tagged_bool("REEL", gamedata.render_em_reels);
    writer.write_tagged_bool("DECL", gamedata.render_decals);
    writer.write_tagged_f32("OFFX", gamedata.offset_x);
    writer.write_tagged_f32("OFFY", gamedata.offset_y);
    writer.write_tagged_f32("ZOOM", gamedata.zoom);
    writer.write_tagged_f32("SLPX", gamedata.angle_tilt_max);
    writer.write_tagged_f32("SLOP", gamedata.angle_tilt_min);
    if let Some(maxs) = gamedata.stereo_max_separation {
        writer.write_tagged_f32("MAXS", maxs);
    }
    if let Some(zpd) = gamedata.stereo_zero_parallax_displacement {
        writer.write_tagged_f32("ZPD", zpd);
    }
    if let Some(sto) = gamedata.stereo_offset {
        writer.write_tagged_f32("STO", sto);
    }
    if let Some(ogst) = gamedata.overwrite_global_stereo3d {
        writer.write_tagged_bool("OGST", ogst);
    }
    writer.write_tagged_string("IMAG", &gamedata.image);
    writer.write_tagged_string("BIMG", &gamedata.backglass_image_full_desktop);
    writer.write_tagged_string("BIMF", &gamedata.backglass_image_full_fullscreen);

    if let Some(bims) = &gamedata.backglass_image_full_single_screen {
        writer.write_tagged_string("BIMS", bims);
    }
    writer.write_tagged_bool("BIMN", gamedata.image_backdrop_night_day);
    writer.write_tagged_string("IMCG", &gamedata.image_color_grade);
    writer.write_tagged_string("BLIM", &gamedata.ball_image);
    if let Some(ball_spherical_mapping) = gamedata.ball_spherical_mapping {
        writer.write_tagged_bool("BLSM", ball_spherical_mapping);
    }
    writer.write_tagged_string("BLIF", &gamedata.ball_image_front);
    if let Some(env_image) = &gamedata.env_image {
        writer.write_tagged_string("EIMG", env_image);
    }
    if let Some(notes) = &gamedata.notes {
        writer.write_tagged_string("NOTX", notes);
    }
    writer.write_tagged_string("SSHT", &gamedata.screen_shot);
    writer.write_tagged_bool("FBCK", gamedata.display_backdrop);
    writer.write_tagged_f32("GLAS", gamedata.glass_top_height);
    if let Some(glass_bottom_height) = gamedata.glass_bottom_height {
        writer.write_tagged_f32("GLAB", glass_bottom_height);
    }
    if let Some(table_height) = gamedata.table_height {
        writer.write_tagged_f32("TBLH", table_height);
    }
    writer.write_tagged_string("PLMA", &gamedata.playfield_material);
    writer.write_tagged_with("BCLR", &gamedata.backdrop_color, Color::biff_write);
    writer.write_tagged_f32("TDFT", gamedata.global_difficulty);
    writer.write_tagged_with("LZAM", &gamedata.light_ambient, Color::biff_write);
    writer.write_tagged_with("LZDI", &gamedata.light0_emission, Color::biff_write);
    writer.write_tagged_f32("LZHI", gamedata.light_height);
    writer.write_tagged_f32("LZRA", gamedata.light_range);
    writer.write_tagged_f32("LIES", gamedata.light_emission_scale);
    writer.write_tagged_f32("ENES", gamedata.env_emission_scale);
    writer.write_tagged_f32("GLES", gamedata.global_emission_scale);
    writer.write_tagged_f32("AOSC", gamedata.ao_scale);
    if let Some(sssc) = gamedata.ssr_scale {
        writer.write_tagged_f32("SSSC", sssc);
    }
    if let Some(clbh) = gamedata.ground_to_lockbar_height {
        writer.write_tagged_f32("CLBH", clbh);
    }
    writer.write_tagged_f32("SVOL", gamedata.table_sound_volume);
    writer.write_tagged_f32("MVOL", gamedata.table_music_volume);
    if let Some(avsy) = gamedata.table_adaptive_vsync {
        writer.write_tagged_i32("AVSY", avsy);
    }
    if let Some(bref) = gamedata.use_reflection_for_balls {
        writer.write_tagged_i32("BREF", bref);
    }
    if let Some(brst) = gamedata.brst {
        writer.write_tagged_i32("BRST", brst);
    }
    writer.write_tagged_u32(
        "PLST",
        quantize_unsigned::<8>(gamedata.playfield_reflection_strength),
    );
    if let Some(btst) = gamedata.use_trail_for_balls {
        writer.write_tagged_i32("BTRA", btst);
    }
    writer.write_tagged_bool("BDMO", gamedata.ball_decal_mode);
    if let Some(bprs) = gamedata.ball_playfield_reflection_strength {
        writer.write_tagged_f32("BPRS", bprs);
    }
    if let Some(dbis) = gamedata.default_bulb_intensity_scale_on_ball {
        writer.write_tagged_f32("DBIS", dbis);
    }
    if let Some(btst) = gamedata.ball_trail_strength {
        writer.write_tagged_u32("BTST", btst);
    }
    if let Some(arac) = gamedata.user_detail_level {
        writer.write_tagged_u32("ARAC", arac);
    }
    if let Some(ogac) = gamedata.overwrite_global_detail_level {
        writer.write_tagged_bool("OGAC", ogac);
    }
    if let Some(ogdn) = gamedata.overwrite_global_day_night {
        writer.write_tagged_bool("OGDN", ogdn);
    }
    writer.write_tagged_bool("GDAC", gamedata.show_grid);
    if let Some(reop) = gamedata.reflect_elements_on_playfield {
        writer.write_tagged_bool("REOP", reop);
    }
    if let Some(uaal) = gamedata.use_aal {
        writer.write_tagged_i32("UAAL", uaal);
    }
    if let Some(ufxa) = gamedata.use_fxaa {
        writer.write_tagged_i32("UFXA", ufxa);
    }
    if let Some(uaoc) = gamedata.use_ao {
        writer.write_tagged_i32("UAOC", uaoc);
    }
    if let Some(ussr) = gamedata.use_ssr {
        writer.write_tagged_i32("USSR", ussr);
    }
    if let Some(tmap) = &gamedata.tone_mapper {
        writer.write_tagged_u32("TMAP", tmap.into());
    }
    if let Some(expo) = gamedata.exposure {
        writer.write_tagged_f32("EXPO", expo);
    }
    writer.write_tagged_f32("BLST", gamedata.bloom_strength);
    writer.write_tagged_u32("MASI", gamedata.materials_size);
    let mut bytes = BytesMut::new();
    for mat in &gamedata.materials_old {
        mat.write(&mut bytes);
    }
    writer.write_tagged_data("MATE", &bytes);
    if let Some(phma) = &gamedata.materials_physics_old {
        let mut bytes = BytesMut::new();
        for mat in phma {
            mat.write(&mut bytes);
        }
        writer.write_tagged_data("PHMA", &bytes);
    }
    if let Some(materials_new) = &gamedata.materials {
        for mat in materials_new {
            let mut mat_writer = BiffWriter::new();
            mat.biff_write(&mut mat_writer);
            writer.write_tagged_data("MATR", mat_writer.get_data());
        }
    }
    // multiple RPRB // added in 10.8.x
    if let Some(render_probes) = &gamedata.render_probes {
        for render_probe in render_probes {
            let mut probe_writer = BiffWriter::new();
            render_probe.biff_write(&mut probe_writer);
            writer.write_tagged_data("RPRB", probe_writer.get_data());
        }
    }
    writer.write_tagged_u32("SEDT", gamedata.gameitems_size);
    writer.write_tagged_u32("SSND", gamedata.sounds_size);
    writer.write_tagged_u32("SIMG", gamedata.images_size);
    writer.write_tagged_u32("SFNT", gamedata.fonts_size);
    writer.write_tagged_u32("SCOL", gamedata.collections_size);
    writer.write_tagged_wide_string("NAME", &gamedata.name);

    let custom_color_bytes = write_colors(&gamedata.custom_colors);

    writer.write_tagged_data("CCUS", custom_color_bytes.as_slice());
    if let Some(protection_data) = &gamedata.protection_data {
        writer.write_tagged_data("SECB", protection_data);
    }
    writer.write_tagged_string_with_encoding_no_size("CODE", &gamedata.code);
    if let Some(is_locked) = gamedata.locked {
        writer.write_tagged_u32("TLCK", is_locked);
    }

    writer.close(true);
    // TODO how do we get rid of this extra copy?
    writer.get_data().to_vec()
}

pub fn read_all_gamedata_records(input: &[u8], version: &Version) -> GameData {
    let mut reader = BiffReader::new(input);
    let mut gamedata = GameData::default();
    let mut previous_tag = String::new();
    loop {
        reader.next(biff::WARN);
        if reader.is_eof() {
            break;
        }
        let tag = reader.tag();

        let reader: &mut BiffReader<'_> = &mut reader;

        match tag.as_str() {
            "LEFT" => gamedata.left = reader.get_f32(),
            "TOPX" => gamedata.top = reader.get_f32(),
            "RGHT" => gamedata.right = reader.get_f32(),
            "BOTM" => gamedata.bottom = reader.get_f32(),
            "CLMO" => gamedata.camera_layout_mode = Some(reader.get_u32()),
            "VSM0" => gamedata.bg_view_mode_desktop = Some(reader.get_u32().into()),
            "ROTA" => gamedata.bg_rotation_desktop = reader.get_f32(),
            "INCL" => gamedata.bg_inclination_desktop = reader.get_f32(),
            "LAYB" => gamedata.bg_layback_desktop = reader.get_f32(),
            "FOVX" => gamedata.bg_fov_desktop = reader.get_f32(),
            "XLTX" => gamedata.bg_offset_x_desktop = reader.get_f32(),
            "XLTY" => gamedata.bg_offset_y_desktop = reader.get_f32(),
            "XLTZ" => gamedata.bg_offset_z_desktop = reader.get_f32(),
            "SCLX" => gamedata.bg_scale_x_desktop = reader.get_f32(),
            "SCLY" => gamedata.bg_scale_y_desktop = reader.get_f32(),
            "SCLZ" => gamedata.bg_scale_z_desktop = reader.get_f32(),
            "EFSS" => {
                if version.u32() == 1080 && previous_tag != "BOTM" {
                    gamedata.is_10_8_0_beta1_to_beta4 = true;
                }
                gamedata.bg_enable_fss = Some(reader.get_bool())
            }
            "HOF0" => gamedata.bg_view_horizontal_offset_desktop = Some(reader.get_f32()),
            "VOF0" => gamedata.bg_view_vertical_offset_desktop = Some(reader.get_f32()),
            "WTX0" => gamedata.bg_window_top_x_offset_desktop = Some(reader.get_f32()),
            "WTY0" => gamedata.bg_window_top_y_offset_desktop = Some(reader.get_f32()),
            "WTZ0" => gamedata.bg_window_top_z_offset_desktop = Some(reader.get_f32()),
            "WBX0" => gamedata.bg_window_bottom_x_offset_desktop = Some(reader.get_f32()),
            "WBY0" => gamedata.bg_window_bottom_y_offset_desktop = Some(reader.get_f32()),
            "WBZ0" => gamedata.bg_window_bottom_z_offset_desktop = Some(reader.get_f32()),
            "VSM1" => gamedata.bg_view_mode_fullscreen = Some(reader.get_u32().into()),
            "ROTF" => gamedata.bg_rotation_fullscreen = reader.get_f32(),
            "INCF" => gamedata.bg_inclination_fullscreen = reader.get_f32(),
            "LAYF" => gamedata.bg_layback_fullscreen = reader.get_f32(),
            "FOVF" => gamedata.bg_fov_fullscreen = reader.get_f32(),
            "XLFX" => gamedata.bg_offset_x_fullscreen = reader.get_f32(),
            "XLFY" => gamedata.bg_offset_y_fullscreen = reader.get_f32(),
            "XLFZ" => gamedata.bg_offset_z_fullscreen = reader.get_f32(),
            "SCFX" => gamedata.bg_scale_x_fullscreen = reader.get_f32(),
            "SCFY" => gamedata.bg_scale_y_fullscreen = reader.get_f32(),
            "SCFZ" => gamedata.bg_scale_z_fullscreen = reader.get_f32(),
            "HOF1" => gamedata.bg_view_horizontal_offset_fullscreen = Some(reader.get_f32()),
            "VOF1" => gamedata.bg_view_vertical_offset_fullscreen = Some(reader.get_f32()),
            "WTX1" => gamedata.bg_window_top_x_offset_fullscreen = Some(reader.get_f32()),
            "WTY1" => gamedata.bg_window_top_y_offset_fullscreen = Some(reader.get_f32()),
            "WTZ1" => gamedata.bg_window_top_z_offset_fullscreen = Some(reader.get_f32()),
            "WBX1" => gamedata.bg_window_bottom_x_offset_fullscreen = Some(reader.get_f32()),
            "WBY1" => gamedata.bg_window_bottom_y_offset_fullscreen = Some(reader.get_f32()),
            "WBZ1" => gamedata.bg_window_bottom_z_offset_fullscreen = Some(reader.get_f32()),
            "VSM2" => gamedata.bg_view_mode_full_single_screen = Some(reader.get_u32().into()),
            "ROFS" => gamedata.bg_rotation_full_single_screen = Some(reader.get_f32()),
            "INFS" => gamedata.bg_inclination_full_single_screen = Some(reader.get_f32()),
            "LAFS" => gamedata.bg_layback_full_single_screen = Some(reader.get_f32()),
            "FOFS" => gamedata.bg_fov_full_single_screen = Some(reader.get_f32()),
            "XLXS" => gamedata.bg_offset_x_full_single_screen = Some(reader.get_f32()),
            "XLYS" => gamedata.bg_offset_y_full_single_screen = Some(reader.get_f32()),
            "XLZS" => gamedata.bg_offset_z_full_single_screen = Some(reader.get_f32()),
            "SCXS" => gamedata.bg_scale_x_full_single_screen = Some(reader.get_f32()),
            "SCYS" => gamedata.bg_scale_y_full_single_screen = Some(reader.get_f32()),
            "SCZS" => gamedata.bg_scale_z_full_single_screen = Some(reader.get_f32()),
            "HOF2" => {
                gamedata.bg_view_horizontal_offset_full_single_screen = Some(reader.get_f32())
            }
            "VOF2" => gamedata.bg_view_vertical_offset_full_single_screen = Some(reader.get_f32()),
            "WTX2" => gamedata.bg_window_top_x_offset_full_single_screen = Some(reader.get_f32()),
            "WTY2" => gamedata.bg_window_top_y_offset_full_single_screen = Some(reader.get_f32()),
            "WTZ2" => gamedata.bg_window_top_z_offset_full_single_screen = Some(reader.get_f32()),
            "WBX2" => {
                gamedata.bg_window_bottom_x_offset_full_single_screen = Some(reader.get_f32())
            }
            "WBY2" => {
                gamedata.bg_window_bottom_y_offset_full_single_screen = Some(reader.get_f32())
            }
            "WBZ2" => {
                gamedata.bg_window_bottom_z_offset_full_single_screen = Some(reader.get_f32())
            }
            "ORRP" => gamedata.override_physics = reader.get_u32(),
            "ORPF" => gamedata.override_physics_flipper = Some(reader.get_bool()),
            "GAVT" => gamedata.gravity = reader.get_f32(),
            "FRCT" => gamedata.friction = reader.get_f32(),
            "ELAS" => gamedata.elasticity = reader.get_f32(),
            "ELFA" => gamedata.elastic_falloff = reader.get_f32(),
            "PFSC" => gamedata.scatter = reader.get_f32(),
            "SCAT" => gamedata.default_scatter = reader.get_f32(),
            "NDGT" => gamedata.nudge_time = reader.get_f32(),
            "MPGC" => gamedata.plunger_normalize = Some(reader.get_u32()),
            "MPDF" => gamedata.plunger_filter = Some(reader.get_bool()),
            "PHML" => gamedata.physics_max_loops = reader.get_u32(),
            "REEL" => gamedata.render_em_reels = reader.get_bool(),
            "DECL" => gamedata.render_decals = reader.get_bool(),
            "OFFX" => gamedata.offset_x = reader.get_f32(),
            "OFFY" => gamedata.offset_y = reader.get_f32(),
            "ZOOM" => gamedata.zoom = reader.get_f32(),
            "SLPX" => gamedata.angle_tilt_max = reader.get_f32(),
            "SLOP" => gamedata.angle_tilt_min = reader.get_f32(),
            "MAXS" => gamedata.stereo_max_separation = Some(reader.get_f32()),
            "ZPD" => gamedata.stereo_zero_parallax_displacement = Some(reader.get_f32()),
            "STO" => gamedata.stereo_offset = Some(reader.get_f32()),
            "OGST" => gamedata.overwrite_global_stereo3d = Some(reader.get_bool()),
            "IMAG" => gamedata.image = reader.get_string(),
            "BIMG" => gamedata.backglass_image_full_desktop = reader.get_string(),
            "BIMF" => gamedata.backglass_image_full_fullscreen = reader.get_string(),
            "BIMS" => gamedata.backglass_image_full_single_screen = Some(reader.get_string()),
            "BIMN" => gamedata.image_backdrop_night_day = reader.get_bool(),
            "IMCG" => gamedata.image_color_grade = reader.get_string(),
            "BLIM" => gamedata.ball_image = reader.get_string(),
            "BLSM" => gamedata.ball_spherical_mapping = Some(reader.get_bool()),
            "BLIF" => gamedata.ball_image_front = reader.get_string(),
            "EIMG" => gamedata.env_image = Some(reader.get_string()),
            "NOTX" => gamedata.notes = Some(reader.get_string()),
            "SSHT" => gamedata.screen_shot = reader.get_string(),
            "FBCK" => gamedata.display_backdrop = reader.get_bool(),
            "GLAS" => gamedata.glass_top_height = reader.get_f32(),
            "GLAB" => gamedata.glass_bottom_height = Some(reader.get_f32()),
            "TBLH" => gamedata.table_height = Some(reader.get_f32()),
            "PLMA" => gamedata.playfield_material = reader.get_string(),
            "BCLR" => gamedata.backdrop_color = Color::biff_read(reader),
            "TDFT" => gamedata.global_difficulty = reader.get_f32(),
            "LZAM" => gamedata.light_ambient = Color::biff_read(reader),
            "LZDI" => gamedata.light0_emission = Color::biff_read(reader),
            "LZHI" => gamedata.light_height = reader.get_f32(),
            "LZRA" => gamedata.light_range = reader.get_f32(),
            "LIES" => gamedata.light_emission_scale = reader.get_f32(),
            "ENES" => gamedata.env_emission_scale = reader.get_f32(),
            "GLES" => gamedata.global_emission_scale = reader.get_f32(),
            "AOSC" => gamedata.ao_scale = reader.get_f32(),
            "SSSC" => gamedata.ssr_scale = Some(reader.get_f32()),
            "CLBH" => gamedata.ground_to_lockbar_height = Some(reader.get_f32()),
            "SVOL" => gamedata.table_sound_volume = reader.get_f32(),
            "MVOL" => gamedata.table_music_volume = reader.get_f32(),
            "AVSY" => gamedata.table_adaptive_vsync = Some(reader.get_i32()),
            "BREF" => gamedata.use_reflection_for_balls = Some(reader.get_i32()),
            "BRST" => gamedata.brst = Some(reader.get_i32()),
            "PLST" => {
                gamedata.playfield_reflection_strength = dequantize_unsigned::<8>(reader.get_u32())
            }
            "BTRA" => gamedata.use_trail_for_balls = Some(reader.get_i32()),
            "BDMO" => gamedata.ball_decal_mode = reader.get_bool(),
            "BPRS" => gamedata.ball_playfield_reflection_strength = Some(reader.get_f32()),
            "DBIS" => gamedata.default_bulb_intensity_scale_on_ball = Some(reader.get_f32()),
            "BTST" => {
                // TODO do we need this QuantizedUnsignedBits for some of the float fields?
                gamedata.ball_trail_strength = Some(reader.get_u32());
            }
            "ARAC" => gamedata.user_detail_level = Some(reader.get_u32()),
            "OGAC" => gamedata.overwrite_global_detail_level = Some(reader.get_bool()),
            "OGDN" => gamedata.overwrite_global_day_night = Some(reader.get_bool()),
            "GDAC" => gamedata.show_grid = reader.get_bool(),
            "REOP" => gamedata.reflect_elements_on_playfield = Some(reader.get_bool()),
            "UAAL" => gamedata.use_aal = Some(reader.get_i32()),
            "UFXA" => gamedata.use_fxaa = Some(reader.get_i32()),
            "UAOC" => gamedata.use_ao = Some(reader.get_i32()),
            "USSR" => gamedata.use_ssr = Some(reader.get_i32()),
            "TMAP" => gamedata.tone_mapper = Some(reader.get_u32().into()),
            "EXPO" => gamedata.exposure = Some(reader.get_f32()),
            "BLST" => gamedata.bloom_strength = reader.get_f32(),
            "MASI" => gamedata.materials_size = reader.get_u32(),
            "MATE" => {
                let data = reader.get_record_data(false).to_vec();
                let mut materials: Vec<SaveMaterial> = Vec::new();
                let mut buff = BytesMut::from(data.as_slice());
                for _ in 0..gamedata.materials_size {
                    let material = SaveMaterial::read(&mut buff);
                    materials.push(material);
                }
                gamedata.materials_old = materials;
            }
            "PHMA" => {
                let data = reader.get_record_data(false).to_vec();
                let mut materials: Vec<SavePhysicsMaterial> = Vec::new();
                let mut buff = BytesMut::from(data.as_slice());
                for _ in 0..gamedata.materials_size {
                    let material = SavePhysicsMaterial::read(&mut buff);
                    materials.push(material);
                }
                gamedata.materials_physics_old = Some(materials);
            }
            // see https://github.com/vpinball/vpinball/blob/1a994086a6092733272fda36a2f449753a1ca21a/pintable.cpp#L4429
            "MATR" => {
                let data = reader.get_record_data(false).to_vec();
                let mut reader = BiffReader::new(&data);
                let material = Material::biff_read(&mut reader);
                gamedata
                    .materials
                    .get_or_insert_with(Vec::new)
                    .push(material);
            }
            "RPRB" => {
                let data = reader.get_record_data(false).to_vec();
                let mut reader = BiffReader::new(&data);
                let render_probe = RenderProbeWithGarbage::biff_read(&mut reader);
                gamedata
                    .render_probes
                    .get_or_insert_with(Vec::new)
                    .push(render_probe);
            }
            "SEDT" => gamedata.gameitems_size = reader.get_u32(),
            "SSND" => gamedata.sounds_size = reader.get_u32(),
            "SIMG" => gamedata.images_size = reader.get_u32(),
            "SFNT" => gamedata.fonts_size = reader.get_u32(),
            "SCOL" => gamedata.collections_size = reader.get_u32(),
            "NAME" => gamedata.name = reader.get_wide_string(),
            "CCUS" => {
                let data = reader.get_record_data(false);
                let custom_colors = read_colors(data);
                gamedata.custom_colors = custom_colors;
            }
            "SECB" => gamedata.protection_data = Some(reader.get_record_data(false).to_vec()),
            "CODE" => {
                let len = reader.get_u32_no_remaining_update();
                // at least a the time of 1060, some code was still encoded in latin1
                gamedata.code = reader.get_str_with_encoding_no_remaining_update(len as usize);
            }
            "TLCK" => gamedata.locked = Some(reader.get_u32()),
            other => {
                let data = reader.get_record_data(false);
                warn!("unhandled gamedata tag {} {} bytes", other, data.len());
            }
        };
        previous_tag = tag;
    }
    gamedata
}

fn read_colors(data: Vec<u8>) -> [Color; 16] {
    // COLORREF: 0x00BBGGRR
    // sizeof(COLORREF) * 16
    let mut colors = Vec::new();
    let mut buff = BytesMut::from(data.as_slice());
    for _ in 0..16 {
        let color = Color::from_win_color(buff.get_u32_le());
        colors.push(color);
    }
    <[Color; 16]>::try_from(colors).unwrap()
}

fn write_colors(colors: &[Color; 16]) -> Vec<u8> {
    let mut bytes = BytesMut::new();
    for color in colors {
        bytes.put_u32_le(color.to_win_color());
    }
    bytes.to_vec()
}

#[cfg(test)]
mod tests {
    use super::*;
    use fake::{Fake, Faker};
    use pretty_assertions::assert_eq;

    #[test]
    fn read_write_empty() {
        let game_data = GameData::default();
        let version: Version = Version::new(1074);
        let bytes = write_all_gamedata_records(&game_data, &version);
        let read_game_data = read_all_gamedata_records(&bytes, &version);

        assert_eq!(game_data, read_game_data);
    }

    #[test]
    fn read_write() {
        let gamedata = GameData {
            left: 1.0,
            right: 2.0,
            top: 3.0,
            bottom: 4.0,
            camera_layout_mode: None,
            bg_view_mode_desktop: Faker.fake(),
            bg_rotation_desktop: 1.0,
            bg_inclination_desktop: 2.0,
            bg_layback_desktop: 3.0,
            bg_fov_desktop: 4.0,
            bg_offset_x_desktop: 1.0,
            bg_offset_y_desktop: 2.0,
            bg_offset_z_desktop: 3.0,
            bg_scale_x_desktop: 3.3,
            bg_scale_y_desktop: 2.2,
            bg_scale_z_desktop: 1.1,
            bg_enable_fss: Some(true),
            bg_rotation_fullscreen: 1.0,
            bg_inclination_fullscreen: 2.0,
            bg_layback_fullscreen: 3.0,
            bg_fov_fullscreen: 4.0,
            bg_offset_x_fullscreen: 1.0,
            bg_offset_y_fullscreen: 2.0,
            bg_offset_z_fullscreen: 3.0,
            bg_scale_x_fullscreen: 3.3,
            bg_scale_y_fullscreen: 2.2,
            bg_scale_z_fullscreen: 1.1,
            bg_rotation_full_single_screen: Some(1.0),
            bg_inclination_full_single_screen: Some(2.0),
            bg_layback_full_single_screen: Some(3.0),
            bg_fov_full_single_screen: Some(4.0),
            bg_offset_x_full_single_screen: Some(1.0),
            bg_offset_y_full_single_screen: Some(2.0),
            bg_offset_z_full_single_screen: Some(3.0),
            bg_scale_x_full_single_screen: Some(3.3),
            bg_scale_y_full_single_screen: Some(2.2),
            bg_scale_z_full_single_screen: Some(1.1),
            override_physics: 1,
            override_physics_flipper: Some(true),
            gravity: 1.0,
            friction: 0.1,
            elasticity: 0.2,
            elastic_falloff: 0.3,
            scatter: 0.2,
            default_scatter: 0.1,
            nudge_time: 3.0,
            plunger_normalize: Some(105),
            plunger_filter: Some(true),
            physics_max_loops: 30,
            render_em_reels: true,
            render_decals: true,
            offset_x: 50.0,
            offset_y: 60.0,
            zoom: 0.2,
            angle_tilt_max: 4.0,
            angle_tilt_min: 3.0,
            stereo_max_separation: Some(0.03),
            stereo_zero_parallax_displacement: Some(0.2),
            stereo_offset: Some(0.5),
            overwrite_global_stereo3d: Some(true),
            image: String::from("test image"),
            backglass_image_full_desktop: String::from("test desktop"),
            backglass_image_full_fullscreen: String::from("test fullscreen"),
            backglass_image_full_single_screen: Some(String::from("test single screen")),
            image_backdrop_night_day: true,
            image_color_grade: String::from("test color grade"),
            ball_image: String::from("test ball image"),
            ball_spherical_mapping: Some(true),
            ball_image_front: String::from("test ball image"),
            env_image: Some(String::from("test env image")),
            notes: Some(String::from("test notes")),
            screen_shot: String::from("test screenshot"),
            display_backdrop: true,
            glass_top_height: 234.0,
            glass_bottom_height: Some(123.0),
            table_height: Some(12.0),
            playfield_material: "material_pf".to_string(),
            backdrop_color: Color::rgb(0x11, 0x22, 0x33),
            global_difficulty: 0.3,
            light_ambient: Faker.fake(),
            light0_emission: Faker.fake(),
            light_height: 4000.0,
            light_range: 50000.0,
            light_emission_scale: 1.2,
            env_emission_scale: 1.23,
            global_emission_scale: 0.111,
            ao_scale: 0.9,
            ssr_scale: Some(0.5),
            ground_to_lockbar_height: Some(42.0),
            table_sound_volume: 0.6,
            table_music_volume: 0.5,
            table_adaptive_vsync: Some(1),
            use_reflection_for_balls: Some(1),
            brst: Some(123),
            playfield_reflection_strength: 0.019607844,
            use_trail_for_balls: Some(-3),
            ball_decal_mode: true,
            ball_playfield_reflection_strength: Some(2.0),
            default_bulb_intensity_scale_on_ball: Some(2.0),
            ball_trail_strength: Some(quantize_u8(8, 0.55) as u32),
            user_detail_level: Some(9),
            overwrite_global_detail_level: Some(true),
            overwrite_global_day_night: Some(false),
            show_grid: false,
            reflect_elements_on_playfield: Some(false),
            use_aal: Some(-10),
            use_fxaa: Some(-2),
            use_ao: Some(-3),
            use_ssr: Some(-4),
            tone_mapper: Faker.fake(),
            exposure: Some(0.42),
            bloom_strength: 0.3,
            materials_size: 0,
            gameitems_size: 0,
            sounds_size: 0,
            images_size: 0,
            fonts_size: 0,
            collections_size: 0,
            materials_old: vec![],
            materials_physics_old: Some(vec![]),
            materials: None,
            render_probes: Some(vec![Faker.fake(), Faker.fake()]),
            name: String::from("test name"),
            custom_colors: [Color::RED; 16],
            protection_data: None,
            code: StringWithEncoding::from("test code wit some unicode: Ǣ"),
            bg_view_horizontal_offset_desktop: None,
            bg_view_vertical_offset_desktop: None,
            bg_window_top_x_offset_desktop: None,
            bg_window_top_y_offset_desktop: None,
            bg_window_top_z_offset_desktop: None,
            bg_window_bottom_x_offset_desktop: None,
            bg_window_bottom_y_offset_desktop: None,
            bg_window_bottom_z_offset_desktop: None,
            bg_view_mode_fullscreen: Faker.fake(),
            bg_view_horizontal_offset_fullscreen: None,
            bg_view_vertical_offset_fullscreen: None,
            bg_window_top_x_offset_fullscreen: None,
            bg_window_top_y_offset_fullscreen: None,
            bg_window_top_z_offset_fullscreen: None,
            bg_window_bottom_x_offset_fullscreen: None,
            bg_window_bottom_y_offset_fullscreen: None,
            bg_window_bottom_z_offset_fullscreen: None,
            bg_view_mode_full_single_screen: Faker.fake(),
            bg_view_horizontal_offset_full_single_screen: None,
            bg_view_vertical_offset_full_single_screen: None,
            bg_window_top_x_offset_full_single_screen: None,
            bg_window_top_y_offset_full_single_screen: None,
            bg_window_top_z_offset_full_single_screen: None,
            bg_window_bottom_x_offset_full_single_screen: None,
            bg_window_bottom_y_offset_full_single_screen: None,
            bg_window_bottom_z_offset_full_single_screen: None,
            locked: Faker.fake(),
            is_10_8_0_beta1_to_beta4: false,
        };
        let version = Version::new(1074);
        let bytes = write_all_gamedata_records(&gamedata, &version);
        let read_game_data = read_all_gamedata_records(&bytes, &version);

        assert_eq!(gamedata, read_game_data);
    }

    #[test]
    fn test_write_read_colors() {
        let mut colors = [Color::RED; 16];
        for color in &mut colors {
            *color = Faker.fake();
        }
        let bytes = write_colors(&colors);
        let read_colors = read_colors(bytes);
        assert_eq!(colors, read_colors);
    }
}