File: data.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (2331 lines) | stat: -rw-r--r-- 47,868 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef MTROPOLIS_DATA_H
#define MTROPOLIS_DATA_H

#include "common/array.h"
#include "common/data-io.h"
#include "common/endian.h"
#include "common/error.h"
#include "common/hashmap.h"
#include "common/hash-str.h"
#include "common/ptr.h"
#include "common/stream.h"
#include "common/rect.h"
#include "common/xpfloat.h"

#include "mtropolis/core.h"

// This contains defs related to parsing of mTropolis stored data into structured data objects.
// This is separated from asset construction for a number of reasons, mainly that data parsing has
// several quirky parses, and there are a lot of fields where, due to platform-specific byte
// swaps, we know the size of the value but don't know what it means.

namespace MTropolis {

class PlugIn;

namespace Data {

enum DataFormat {
	kDataFormatUnknown,

	kDataFormatMacintosh,
	kDataFormatWindows,
};

} // End of namespace Data

} // End of namespace MTropolis

namespace Common {

template<>
struct DataFormatTraits<MTropolis::Data::DataFormat> {
	static inline bool isLittleEndian(MTropolis::Data::DataFormat dataFormat) {
		return dataFormat != MTropolis::Data::kDataFormatMacintosh;
	}
};

template<>
struct DataIO<MTropolis::Data::DataFormat, Common::XPFloat> {
	static const uint kMaxSize = 10;

	static inline uint computeSize(MTropolis::Data::DataFormat dataFormat) {
		if (dataFormat == MTropolis::Data::kDataFormatMacintosh)
			return 10;
		else if (dataFormat == MTropolis::Data::kDataFormatWindows)
			return 8;
		else
			return 0;
	}

	static inline void encode(MTropolis::Data::DataFormat dataFormat, byte *data, const Common::XPFloat &value) {
		if (dataFormat == MTropolis::Data::kDataFormatMacintosh) {
			DataIO<MTropolis::Data::DataFormat, uint16>::encode(dataFormat, data + 0, value.signAndExponent);
			DataIO<MTropolis::Data::DataFormat, uint64>::encode(dataFormat, data + 2, value.mantissa);
		} else if (dataFormat == MTropolis::Data::kDataFormatWindows) {
			uint64 doubleBits = 0;
			bool overflowed = false;
			value.toDoubleBitsSafe(doubleBits, overflowed);
			DataIO<MTropolis::Data::DataFormat, uint64>::encode(dataFormat, data, doubleBits);
		}
	}

	static inline void decode(MTropolis::Data::DataFormat dataFormat, const byte *data, Common::XPFloat &value) {
		if (dataFormat == MTropolis::Data::kDataFormatMacintosh) {
			DataIO<MTropolis::Data::DataFormat, uint16>::decode(dataFormat, data + 0, value.signAndExponent);
			DataIO<MTropolis::Data::DataFormat, uint64>::decode(dataFormat, data + 2, value.mantissa);
		} else if (dataFormat == MTropolis::Data::kDataFormatWindows) {
			uint64 doubleBits = 0;
			DataIO<MTropolis::Data::DataFormat, uint64>::decode(dataFormat, data, doubleBits);
			value = value.fromDoubleBits(doubleBits);
		}
	}
};

} // End of namespace Common


namespace MTropolis {

class PlugIn;

namespace Data {

struct PlugInModifier;
struct PlugInModifierData;

// Project format and data format are 2 separate things.
//
// A cross-platform project can be booted in either Mac or Win mode and contains
// separate scene streams for Mac and Win.  Which one is loaded depends on what the
// game platform is loaded as.
//
// The following table describes the behavior:
//
// Project type          | ProjectFormat | Catalog and asset data format | Scene data format |
// ------------------------------------------------------------------------------------------|
// Mac                   | Macintosh     | Macintosh                     | Macintosh         |
// Win                   | Windows       | Windows                       | Windows           |
// Cross-Platform as Mac | Neutral       | Windows                       | Macintosh         |
// Cross-Platform as Win | Neutral       | Windows                       | Windows           |

enum ProjectFormat {
	kProjectFormatUnknown,

	kProjectFormatMacintosh,
	kProjectFormatWindows,
	kProjectFormatNeutral,
};

enum DataReadErrorCode {
	kDataReadErrorNone = 0,

	kDataReadErrorUnsupportedRevision,
	kDataReadErrorReadFailed,
	kDataReadErrorUnrecognized,
	kDataReadErrorPlugInNotFound,
};

enum ModifierFlags {
	kModifierFlagLast = 0x2,
};

enum TextAlignmentCode {
	kTextAlignmentCodeLeft = 0,
	kTextAlignmentCodeCenter = 1,
	kTextAlignmentCodeRight = 0xffff,
};


namespace DataObjectTypes {

enum DataObjectType : uint {
	kUnknown								= 0,

	kProjectLabelMap						= 0x22,
	kProjectCatalog							= 0x3e8,
	kStreamHeader							= 0x3e9,
	kProjectHeader							= 0x3ea,
	kPresentationSettings					= 0x3ec,

	kAssetCatalog							= 0xd,
	kGlobalObjectInfo						= 0x17,
	kUnknown19								= 0x19,
	kUnknown2B								= 0x2b,

	kProjectStructuralDef					= 0x2,
	kSectionStructuralDef					= 0x3,
	kSubsectionStructuralDef				= 0x21,

	kGraphicElement							= 0x8,
	kMovieElement							= 0x5,
	kMToonElement							= 0x6,
	kImageElement							= 0x7,
	kSoundElement							= 0xa,
	kTextLabelElement						= 0x15,

	kAVIMovieElement						= 0x25,
	kAliasModifier							= 0x27,
	kChangeSceneModifier					= 0x136,
	kReturnModifier							= 0x140,
	kSoundEffectModifier					= 0x1a4,
	kSimpleMotionModifier					= 0x1fe,
	kDragMotionModifier						= 0x208,
	kPathMotionModifierV1					= 0x21c,
	kPathMotionModifierV2					= 0x21b,
	kVectorMotionModifier					= 0x226,
	kSceneTransitionModifier				= 0x26c,
	kElementTransitionModifier				= 0x276,
	kSharedSceneModifier					= 0x29a,
	kIfMessengerModifier					= 0x2bc,
	kBehaviorModifier						= 0x2c6,
	kMessengerModifier						= 0x2da,
	kSetModifier							= 0x2df,
	kTimerMessengerModifier					= 0x2e4,
	kCollisionDetectionMessengerModifier	= 0x2ee,
	kBoundaryDetectionMessengerModifier		= 0x2f8,
	kKeyboardMessengerModifier				= 0x302,
	kTextStyleModifier						= 0x32a,
	kGraphicModifier						= 0x334,
	kImageEffectModifier					= 0x384,
	kMiniscriptModifier						= 0x3c0,
	kCursorModifierV1						= 0x3ca,
	kGradientModifier						= 0x4b0,	// NYI
	kColorTableModifier						= 0x4c4,
	kSoundFadeModifier						= 0x4ce,
	kSaveAndRestoreModifier					= 0x4d8,

	kCompoundVariableModifier				= 0x2c7,
	kBooleanVariableModifier				= 0x321,
	kIntegerVariableModifier				= 0x322,
	kIntegerRangeVariableModifier			= 0x324,
	kVectorVariableModifier					= 0x327,
	kPointVariableModifier					= 0x326,
	kFloatingPointVariableModifier			= 0x328,
	kStringVariableModifier					= 0x329,
	kObjectReferenceVariableModifierV1		= 0x33e,
	kDebris									= 0xfffffffe,	// Deleted modifier in alias list
	kPlugInModifier							= 0xffffffff,

	kMovieAsset								= 0x10,
	kAudioAsset								= 0x11,
	kColorTableAsset						= 0x1e,
	kImageAsset								= 0xe,
	kMToonAsset								= 0xf,
	kTextAsset								= 0x1f,
	kAVIMovieAsset							= 0x24,

	kAssetDataChunk							= 0xffff,
};

bool isValidSceneRootElement(DataObjectType type);
bool isVisualElement(DataObjectType type);
bool isNonVisualElement(DataObjectType type);
bool isStructural(DataObjectType type);
bool isElement(DataObjectType type);
bool isModifier(DataObjectType type);
bool isAsset(DataObjectType type);

} // End of namespace DataObjectTypes

namespace StructuralFlags {
	enum StructuralFlags {
		kHasModifiers = 0x1,
		kHasChildren = 0x4,
		kNoMoreSiblings = 0x8,
	};
} // End of namespace StructuralFlags


class DataReader {
public:
	DataReader(int64 globalPosition, Common::SeekableReadStream &stream, DataFormat dataFormat, RuntimeVersion runtimeVersion, bool autoDetectVersion);

	bool readU8(uint8 &value);
	bool readU16(uint16 &value);
	bool readU32(uint32 &value);
	bool readU64(uint64 &value);
	bool readS8(int8 &value);
	bool readS16(int16 &value);
	bool readS32(int32 &value);
	bool readS64(int64 &value);
	bool readF32(float &value);
	bool readF64(double &value);
	bool readPlatformFloat(Common::XPFloat &value);

	template<class... T>
	bool readMultiple(T &...values);

	bool read(void *dest, size_t size);

	// Reads a terminated string where "length" is the number of characters including a null terminator
	bool readTerminatedStr(Common::String &value, size_t length);

	bool readNonTerminatedStr(Common::String &value, size_t length);

	template<size_t TSize>
	bool readChars(char (&arr)[TSize]);

	template<size_t TSize>
	bool readBytes(uint8 (&arr)[TSize]);

	bool seek(int64 pos);
	bool skip(size_t count);

	int64 tell() const;
	inline int64 tellGlobal() const { return _globalPosition + tell(); }

	DataFormat getDataFormat() const;

	void setPermitDamagedStrings(bool permit);

	bool isVersionAutoDetect() const;
	RuntimeVersion getRuntimeVersion() const;
	void setRuntimeVersion(RuntimeVersion runtimeVersion);

private:
	bool checkErrorAndReset();

	Common::SeekableReadStream &_stream;
	DataFormat _dataFormat;
	int64 _globalPosition;

	bool _permitDamagedStrings;

	RuntimeVersion _runtimeVersion;	// NOTE: May change during parsing if auto-detecting
	bool _autoDetect;
};

template<class... T>
bool DataReader::readMultiple(T &...values) {
	byte buffer[Common::DataMultipleIO<DataFormat, T...>::kMaxSize];
	const uint actualSize = Common::DataMultipleIO<DataFormat, T...>::computeSize(_dataFormat);

	if (!read(buffer, actualSize))
		return false;

	Common::DataMultipleIO<DataFormat, T...>::decode(_dataFormat, buffer, values...);
	return true;
}

struct Rect {
	Rect();

	bool load(DataReader &reader);

	bool toScummVMRect(Common::Rect &outRect) const;
	bool toScummVMRectUnchecked(Common::Rect &outRect) const;

	int16 top;
	int16 left;
	int16 bottom;
	int16 right;
};

struct Point {
	Point();

	bool load(DataReader &reader);
	bool toScummVMPoint(Common::Point &outPoint) const;

	int16 x;
	int16 y;
};

struct Event {
	Event();

	bool load(DataReader &reader);

	uint32 eventID;
	uint32 eventInfo;
};

struct ColorRGB16 {
	ColorRGB16();

	bool load(DataReader &reader);

	uint16 red;
	uint16 green;
	uint16 blue;
};

struct IntRange {
	IntRange();

	bool load(DataReader &reader);

	int32 min;
	int32 max;
};

struct XPFloatVector {
	bool load(DataReader &reader);

	Common::XPFloat angleRadians;
	Common::XPFloat magnitude;
};

struct XPFloatPOD {
	uint16 signAndExponent;
	uint64 mantissa;

	bool load(DataReader &reader);
	Common::XPFloat toXPFloat() const;
};

struct Label {
	bool load(DataReader &reader);

	uint32 superGroupID;
	uint32 labelID;
};

struct UniversalTime {
	bool load(DataReader &reader);

	int32 value;
	int32 scale;
	int32 base;
};


// mTropolis uses two separate type-tagged value formats.
//
// InternalTypeTaggedValue is used by internal modifiers for messenger payloads and set modifiers
// and seems to match Miniscript ops too.
// InternalTypeTaggedValue is always 46 bytes in size and stores string data elsewhere in the containing structure.
//
// PlugInTypeTaggedValue is used by plug-ins and is fully self-contained.
//
// If you change something here, remember to update DynamicValue::load
struct InternalTypeTaggedValue {
	enum TypeCode {
		kNull = 0x00,
		kInteger = 0x01,
		kString = 0x0d, // String data is stored externally from the value
		kPoint = 0x10,
		kIntegerRange = 0x11,
		kFloat = 0x15,
		kBool = 0x1a,
		kIncomingData = 0x1b,
		kVariableReference = 0x1c,
		kLabel = 0x1d,
	};

	struct VariableReference {
		uint32 unknown;
		uint32 guid;
	};

	union ValueUnion {
		ValueUnion();

		uint8 asBool;
		XPFloatPOD asFloat;
		int32 asInteger;
		IntRange asIntegerRange;
		VariableReference asVariableReference;
		Label asLabel;
		Point asPoint;

		template<class T>
		void constructField(T ValueUnion::*fieldPtr);
	};

	InternalTypeTaggedValue();
	~InternalTypeTaggedValue();

	uint16 type;
	ValueUnion value;

	bool load(DataReader &reader);
};

struct PlugInTypeTaggedValue : public Common::NonCopyable {
	enum TypeCode {
		kNull = 0x00,
		kInteger = 0x01,
		kUniversalTime = 0x09,
		kPoint = 0xa,
		kIntegerRange = 0xb,
		kFloat = 0xf,
		kBoolean = 0x14,
		kEvent = 0x17,
		kLabel = 0x64,
		kString = 0x66,
		kRGBColor = 0x6c,
		kIncomingData = 0x6e,
		kVariableReference = 0x73,	// Has extra data
	};

	union ValueUnion {
		ValueUnion();
		~ValueUnion();

		int32 asInt;
		Point asPoint;
		UniversalTime asUniversalTime;
		ColorRGB16 asColor;
		IntRange asIntRange;
		XPFloatPOD asFloat;
		uint16 asBoolean;
		Event asEvent;
		Label asLabel;
		uint32 asVarRefGUID;
		Common::String asString;

		template<class T>
		void constructField(T ValueUnion::*fieldPtr);

		template<class T>
		void destructField(T ValueUnion::*fieldPtr);
	};

	PlugInTypeTaggedValue();
	~PlugInTypeTaggedValue();

	uint16 type;
	ValueUnion value;

	Common::Array<uint8> extraData;

	bool load(DataReader &reader);
};

class DataObject : public Common::NonCopyable {

public:
	DataObject();
	virtual ~DataObject();
	DataReadErrorCode load(DataObjectTypes::DataObjectType type, uint16 revision, DataReader &reader);

	uint16 getRevision() const;
	DataObjectTypes::DataObjectType getType() const;

protected:
	virtual DataReadErrorCode load(DataReader &reader) = 0;

	DataObjectTypes::DataObjectType _type;
	uint16 _revision;
};

struct ProjectHeader : public DataObject {
	ProjectHeader();

	uint32 persistFlags;
	uint32 sizeIncludingTag;
	uint16 unknown1;
	uint32 catalogFilePosition;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct ProjectLabelMap : public DataObject {
	ProjectLabelMap();
	~ProjectLabelMap();

	struct LabelTree {
		LabelTree();
		~LabelTree();

		enum : uint {
			kExpandedInEditor = 0x80000000,
		};

		uint32 nameLength;
		uint32 isGroup;
		uint32 id;
		uint32 unknown1;
		uint32 flags;

		Common::String name;

		uint32 numChildren;
		LabelTree *children;
	};

	struct SuperGroup {
		SuperGroup();
		~SuperGroup();

		uint32 nameLength;
		uint32 id;
		uint32 unknown2;
		Common::String name;

		uint32 numChildren;
		LabelTree *tree;
	};

	uint32 persistFlags;
	uint32 unknown1; // Always 0x16
	uint32 numSuperGroups;
	uint32 nextAvailableID;

	SuperGroup *superGroups;

private:
	static DataReadErrorCode loadSuperGroup(SuperGroup &sg, DataReader &reader);
	static DataReadErrorCode loadLabelTree(LabelTree &lt, DataReader &reader);

	DataReadErrorCode load(DataReader &reader) override;
};

struct PresentationSettings : public DataObject {
	PresentationSettings();

	uint32 persistFlags;
	uint32 sizeIncludingTag;
	uint8 unknown1[2];
	Point dimensions;
	uint16 bitsPerPixel;
	uint16 unknown4;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct AssetCatalog : public DataObject {
	enum {
		kFlag1Deleted = 1,
		kFlag1LimitOnePerSegment = 2,
	};

	struct AssetInfoRev4Fields {
		AssetInfoRev4Fields();

		uint32 assetType;
		uint32 flags2;
	};

	struct AssetInfo {
		AssetInfo();

		uint32 flags1;
		uint16 nameLength;
		uint16 alwaysZero;
		uint32 streamID;
		uint32 filePosition; // Contains a static value in Obsidian

		AssetInfoRev4Fields rev4Fields;

		Common::String name;
	};

	AssetCatalog();

	uint32 persistFlags;
	uint32 totalNameSizePlus22;
	uint8 unknown1[4];
	uint32 numAssets;
	bool haveRev4Fields;

	Common::Array<AssetInfo> assets;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct Unknown19 : public DataObject {
	Unknown19();

	uint32 persistFlags;
	uint32 sizeIncludingTag;
	uint8 unknown1[2];

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct Unknown2B : public DataObject {
	Unknown2B();

	uint32 persistFlags;
	uint32 sizeIncludingTag;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct StructuralDef : public DataObject {
	StructuralDef();

	uint32 structuralFlags;
};

struct ProjectStructuralDef : public DataObject {
	ProjectStructuralDef();

	uint32 unknown1; // Seems to always be 0x16 or 0x9
	uint32 sizeIncludingTag;
	uint32 guid;
	uint32 otherFlags;
	uint16 lengthOfName;

	Common::String name;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct SectionStructuralDef : public StructuralDef {
	SectionStructuralDef();

	uint32 sizeIncludingTag;
	uint32 guid;
	uint16 lengthOfName;
	uint32 otherFlags;
	uint16 unknown4;
	uint16 sectionID;
	uint32 segmentID;

	Common::String name;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct SubsectionStructuralDef : public StructuralDef {
	SubsectionStructuralDef();

	uint32 structuralFlags;
	uint32 sizeIncludingTag;
	uint32 guid;
	uint16 lengthOfName;
	uint32 otherFlags;
	uint16 sectionID;

	Common::String name;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

namespace ElementFlags {

enum ElementFlags {
	kNotDirectToScreen	= 0x00001000,
	kHidden				= 0x00008000,
	kPaused				= 0x00010000,
	kExpandedInEditor	= 0x00800000,
	kCacheBitmap		= 0x02000000,
	kSelectedInEditor	= 0x10000000,
};

} // End of namespace ElementFlags

namespace AnimationFlags {

enum AnimationFlags {
	kAlternate			= 0x10000000,
	kLoop				= 0x08000000,
	kPlayEveryFrame		= 0x02000000,
};

} // End of namespace AnimationFlags

namespace SceneTransitionTypes {

enum SceneTransitionType {
	kNone = 0,
	kPatternDissolve = 0x0406,
	kRandomDissolve = 0x0410, // No steps
	kFade = 0x041a,
	kSlide = 0x03e8, // Directional
	kPush = 0x03f2,  // Directional
	kZoom = 0x03fc,
	kWipe = 0x0424, // Directional
};

} // End of namespace SceneTransitionTypes

namespace SceneTransitionDirections {

enum SceneTransitionDirection {
	kUp = 0x384,
	kDown = 0x385,
	kLeft = 0x386,
	kRight = 0x387,
};

} // End of namespace SceneTransitionDirections

struct GraphicElement : public StructuralDef {
	GraphicElement();

	// Possible element flags: NotDirectToScreen, CacheBitmap, Hidden
	uint32 sizeIncludingTag;
	uint32 guid;
	uint16 lengthOfName;
	uint32 elementFlags;
	uint16 layer;
	uint16 sectionID;
	Rect rect1;
	Rect rect2;
	uint32 streamLocator; // 1-based index, sometimes observed with 0x10000000 flag set, not sure of the meaning
	uint8 unknown11[4];

	Common::String name;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct ImageElement : public StructuralDef {
	ImageElement();

	// Possible element flags: NotDirectToScreen, CacheBitmap, Hidden
	uint32 sizeIncludingTag;
	uint32 guid;
	uint16 lengthOfName;
	uint32 elementFlags;
	uint16 layer;
	uint16 sectionID;
	Rect rect1;
	Rect rect2;
	uint32 imageAssetID;
	uint32 streamLocator;
	uint8 unknown7[4];

	Common::String name;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct TextLabelElement : public StructuralDef {
	TextLabelElement();

	// Possible element flags: NotDirectToScreen, CacheBitmap, Hidden
	uint32 sizeIncludingTag;
	uint32 guid;
	uint16 lengthOfName;
	uint32 elementFlags;
	uint16 layer;
	uint16 sectionID;
	Rect rect1;
	Rect rect2;
	uint32 assetID;

	struct MacPart {
		uint8 unknown2[30];
	};

	struct WinPart {
		uint8 unknown3[2];
		uint8 unknown4[8];
	};

	union PlatformPart {
		MacPart mac;
		WinPart win;
	};

	bool haveMacPart;
	bool haveWinPart;
	PlatformPart platform;

	Common::String name;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct SoundElement : public StructuralDef {
	enum SoundFlags : uint {
		kPaused = 0x40000000,
		kLoop = 0x80000000,
	};

	SoundElement();

	// Possible element flags: Loop, Paused
	uint32 sizeIncludingTag;
	uint32 guid;
	uint16 lengthOfName;
	uint32 elementFlags;
	uint32 soundFlags;
	uint16 unknown2;
	uint8 unknown3[2];
	uint16 rightVolume;
	uint16 leftVolume;
	int16 balance;
	uint32 assetID;
	uint8 unknown5[8];

	Common::String name;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct MovieElement : public StructuralDef {
	explicit MovieElement(bool avi);

	// Possible flags: NotDirectToScreen, CacheBitmap, Hidden, Loop, Loop + Alternate, Paused
	uint32 sizeIncludingTag;
	uint32 guid;
	uint16 lengthOfName;
	uint32 elementFlags;
	uint16 layer;
	uint8 unknown3[44];
	uint16 sectionID;
	uint8 unknown5[2];
	Rect rect1;
	Rect rect2;
	uint32 assetID;
	uint32 unknown7;
	uint16 volume;
	uint32 animationFlags;
	uint8 unknown10[4];
	uint8 unknown11[4];
	uint32 streamLocator;
	uint8 unknown13[4];

	Common::String name;

	bool isAVI;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct MToonElement : public StructuralDef {
	MToonElement();

	// Possible flags: NotDirectToScreen, CacheBitmap, Hidden, Loop, Paused, PlayEveryFrame (inverted as "Maintain Rate")
	uint32 sizeIncludingTag;
	uint32 guid;
	uint16 lengthOfName;
	uint32 elementFlags;
	uint16 layer;
	uint32 animationFlags;
	uint8 unknown4[4];
	uint16 sectionID;
	Rect rect1;
	Rect rect2;
	uint32 assetID;
	uint32 rateTimes100000;
	uint32 streamLocator;
	uint32 unknown6;

	Common::String name;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct GlobalObjectInfo : public DataObject {
	GlobalObjectInfo();

	uint32 persistFlags;
	uint32 sizeIncludingTag;
	uint16 numGlobalModifiers;
	uint8 unknown1[4];

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

class ProjectCatalog : public DataObject {
public:
	struct StreamDesc {
		StreamDesc();

		char streamType[25];
		uint16 segmentIndexPlusOne;
		uint32 winSize;
		uint32 winPos;
		uint32 macSize;
		uint32 macPos;
	};

	struct SegmentDesc {
		SegmentDesc();

		uint32 segmentID;
		Common::String label;
		Common::String exportedPath;
	};

	ProjectCatalog();

	uint32 persistFlags;
	uint32 sizeOfStreamAndSegmentDescs;
	uint16 unknown1;
	uint16 unknown2;
	uint32 unknown3;

	Common::Array<SegmentDesc> segments;
	Common::Array<StreamDesc> streams;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct StreamHeader : public DataObject {
	StreamHeader();

	uint32 marker;
	uint32 sizeIncludingTag;
	char name[17];
	uint8 projectID[2];
	uint8 unknown1[4]; // Seems to be consistent across builds
	uint16 unknown2;   // 0

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct BehaviorModifier : public DataObject {
	enum BehaviorFlags {
		kBehaviorFlagSwitchable = 1,
	};

	BehaviorModifier();

	uint32 modifierFlags;
	uint32 sizeIncludingTag;
	uint8 unknown2[2];
	uint32 guid;
	uint32 unknown4;
	uint16 unknown5;
	uint32 unknown6;
	uint32 unknown8; // revision 2 only.
	Point editorLayoutPosition;
	uint16 lengthOfName;
	uint16 numChildren;
	uint32 behaviorFlags;
	Event enableWhen;
	Event disableWhen;
	uint8 unknown7[2];

	Common::String name;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct MiniscriptProgram {
	struct LocalRef {
		LocalRef();

		uint32 guid;
		uint8 lengthOfName;
		uint8 unknown2;

		Common::String name;
	};

	struct Attribute {
		Attribute();

		uint8 lengthOfName;
		uint8 unknown3;

		Common::String name;
	};

	MiniscriptProgram();

	uint32 unknown1;
	uint32 sizeOfInstructions;
	uint32 numOfInstructions;
	uint32 numLocalRefs;
	uint32 numAttributes;

	Common::Array<uint8> bytecode;
	Common::Array<LocalRef> localRefs;
	Common::Array<Attribute> attributes;

	DataFormat dataFormat;

	bool load(DataReader &reader);
};

// Header used for most modifiers, but not all
struct TypicalModifierHeader {
	TypicalModifierHeader();

	uint32 modifierFlags;
	uint32 sizeIncludingTag;
	uint32 guid;
	uint8 unknown3[6];
	uint32 unknown4;
	uint32 unknown5;  // V2 header only
	Point editorLayoutPosition;
	uint16 lengthOfName;

	Common::String name;

	bool load(DataReader &reader, bool isV2);
};

struct MiniscriptModifier : public DataObject {
	MiniscriptModifier();

	TypicalModifierHeader modHeader;
	Event enableWhen;
	uint8 unknown6[11];
	uint8 unknown7;

	MiniscriptProgram program;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct ColorTableModifier : public DataObject {
	ColorTableModifier();

	TypicalModifierHeader modHeader;
	Event applyWhen;
	uint32 unknown1;
	uint8 unknown2[4];
	uint32 assetID;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct SoundFadeModifier : public DataObject {
	SoundFadeModifier();

	TypicalModifierHeader modHeader;
	uint8 unknown1[4];
	Event enableWhen;
	Event disableWhen;
	uint16 fadeToVolume;
	uint8 codedDuration[4];
	uint8 unknown2[18];

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct SaveAndRestoreModifier : public DataObject {
	SaveAndRestoreModifier();

	TypicalModifierHeader modHeader;
	uint8 unknown1[4];
	Event saveWhen;
	Event restoreWhen;
	InternalTypeTaggedValue saveOrRestoreValue;

	uint8 unknown5[8];

	uint8 lengthOfFilePath;
	uint8 lengthOfFileName;
	uint8 lengthOfVariableName;
	uint8 lengthOfVariableString;

	Common::String varName;
	Common::String varString;
	Common::String filePath;
	Common::String fileName;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

enum MessageFlags : uint {
	kMessageFlagNoRelay = 0x20000000,
	kMessageFlagNoCascade = 0x40000000,
	kMessageFlagNoImmediate = 0x80000000,
};


struct MessengerModifier : public DataObject {
	MessengerModifier();

	TypicalModifierHeader modHeader;

	uint32 messageFlags;
	Event send;
	Event when;
	uint16 unknown14;
	uint32 destination;
	uint8 unknown11[10];
	InternalTypeTaggedValue with;
	uint8 withSourceLength;
	uint8 withStringLength;

	Common::String withSource;
	Common::String withString;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct SetModifier : public DataObject {
	SetModifier();

	TypicalModifierHeader modHeader;

	uint8 unknown1[4];
	Event executeWhen;
	InternalTypeTaggedValue source;
	InternalTypeTaggedValue target;
	uint8 unknown3;
	uint8 sourceNameLength;
	uint8 targetNameLength;
	uint8 sourceStringLength;
	uint8 targetStringLength;
	uint8 unknown4;

	Common::String sourceName;
	Common::String targetName;
	Common::String sourceString;
	Common::String targetString;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct AliasModifier : public DataObject {
	AliasModifier();

	uint32 modifierFlags;
	uint32 sizeIncludingTag;
	uint16 aliasIndexPlusOne;
	uint32 unknown1;
	uint32 unknown2;
	uint32 unknown3;
	uint32 lengthOfName;
	uint32 guid;
	Point editorLayoutPosition;

	Common::String name;

	bool haveGUID;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct ChangeSceneModifier : public DataObject {
	enum ChangeSceneFlags : uint {
		kChangeSceneFlagNextScene			= 0x80000000,
		kChangeSceneFlagPrevScene			= 0x40000000,
		kChangeSceneFlagSpecificScene		= 0x20000000,
		kChangeSceneFlagAddToReturnList		= 0x10000000,
		kChangeSceneFlagAddToDestList		= 0x08000000,
		kChangeSceneFlagWrapAround			= 0x04000000,
	};

	ChangeSceneModifier();

	TypicalModifierHeader modHeader;
	uint32 changeSceneFlags;
	Event executeWhen;
	uint32 targetSectionGUID;
	uint32 targetSubsectionGUID;
	uint32 targetSceneGUID;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct SoundEffectModifier : public DataObject {
	SoundEffectModifier();

	static const uint32 kSpecialAssetIDSystemBeep = 0xffffffffu;

	TypicalModifierHeader modHeader;
	uint8 unknown1[4];
	Event executeWhen;
	Event terminateWhen;
	uint32 unknown2;
	uint8 unknown3[4];
	uint32 assetID;
	uint8 unknown5[4];

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct PathMotionModifier : public DataObject {
	struct PointDefMessageSpec {
		PointDefMessageSpec();

		uint32 messageFlags;
		Event send;
		uint16 unknown11;
		uint32 destination;
		uint8 unknown13[10];
		InternalTypeTaggedValue with;
		uint8 withSourceLength;
		uint8 withStringLength;

		Common::String withSource;
		Common::String withString;

		bool load(DataReader &reader);
	};

	struct PointDef {
		PointDef();

		enum FrameFlags {
			kFrameFlagPlaySequentially = 1,
		};

		Point point;
		uint32 frame;
		uint32 frameFlags;

		PointDefMessageSpec messageSpec;

		bool load(DataReader &reader, bool haveMessageSpec);
	};

	enum Flags {
		kFlagReverse = 0x00100000,
		kFlagLoop = 0x10000000,
		kFlagAlternate = 0x02000000,
		kFlagStartAtBeginning = 0x08000000,
	};

	explicit PathMotionModifier(uint version);

	TypicalModifierHeader modHeader;
	uint32 flags;

	Event executeWhen;
	Event terminateWhen;

	uint8 unknown2[2];
	uint16 numPoints;
	uint8 unknown3[4];
	uint32 frameDurationTimes10Million;
	uint8 unknown5[4];
	uint32 unknown6;

	bool havePointDefMessageSpecs;

	Common::Array<PointDef> points;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct SimpleMotionModifier : public DataObject {
	SimpleMotionModifier();

	TypicalModifierHeader modHeader;

	Event executeWhen;
	Event terminateWhen;
	uint16 motionType;
	uint16 directionFlags;
	uint16 steps;
	uint32 delayMSecTimes4800;
	uint8 unknown1[4];

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct DragMotionModifier : public DataObject {
	DragMotionModifier();

	TypicalModifierHeader modHeader;

	Event enableWhen;
	Event disableWhen;

	struct WinPart {
		uint8 unknown2;
		uint8 constrainHorizontal;
		uint8 constrainVertical;
		uint8 constrainToParent;
	};

	struct MacPart {
		uint8 flags;
		uint8 unknown3;

		enum Flags {
			kConstrainToParent = 0x10,
			kConstrainVertical = 0x20,
			kConstrainHorizontal = 0x40,
		};
	};

	union PlatformPart {
		WinPart win;
		MacPart mac;
	};

	PlatformPart platform;

	bool haveMacPart;
	bool haveWinPart;
	Rect constraintMargin;
	uint16 unknown1;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct VectorMotionModifier : public DataObject {
	VectorMotionModifier();

	TypicalModifierHeader modHeader;

	Event enableWhen;
	Event disableWhen;
	InternalTypeTaggedValue vec;
	uint16 unknown1;
	uint8 vecSourceLength;
	uint8 vecStringLength;

	Common::String vecSource;
	Common::String vecString;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct SceneTransitionModifier : public DataObject {
	SceneTransitionModifier();

	TypicalModifierHeader modHeader;

	Event enableWhen;
	Event disableWhen;
	uint16 transitionType;
	uint16 direction;
	uint16 unknown3;
	uint16 steps;
	uint32 duration;
	uint8 unknown5[2];

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct ElementTransitionModifier : public DataObject {
	ElementTransitionModifier();

	enum TransitionType {
		kTransitionTypeRectangularIris = 0x03e8,
		kTransitionTypeOvalIris = 0x03f2,
		kTransitionTypeZoom = 0x044c,
		kTransitionTypeFade = 0x2328,
	};

	enum RevealType {
		kRevealTypeReveal = 0,
		kRevealTypeConceal = 1,
	};

	TypicalModifierHeader modHeader;

	Event enableWhen;
	Event disableWhen;
	uint16 revealType;
	uint16 transitionType;
	uint16 unknown3;
	uint16 unknown4;
	uint16 steps;
	uint16 rate;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct SharedSceneModifier : public DataObject {
	SharedSceneModifier();

	TypicalModifierHeader modHeader;

	uint8 unknown1[4];
	Event executeWhen;
	uint32 sectionGUID;
	uint32 subsectionGUID;
	uint32 sceneGUID;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct IfMessengerModifier : public DataObject {
	IfMessengerModifier();

	TypicalModifierHeader modHeader;

	uint32 messageFlags;
	Event send;
	Event when;
	uint16 unknown6;
	uint32 destination;
	uint8 unknown7[10];
	InternalTypeTaggedValue with;
	uint8 unknown9[10];
	uint8 withSourceLength;
	uint8 withStringLength;
	MiniscriptProgram program;

	Common::String withSource;
	Common::String withString;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct TimerMessengerModifier : public DataObject {
	TimerMessengerModifier();

	TypicalModifierHeader modHeader;

	enum TimerFlags {
		kTimerFlagLooping = 0x10000000,
	};

	uint32 messageAndTimerFlags;
	Event executeWhen;
	Event send;
	Event terminateWhen;
	uint16 unknown2;
	uint32 destination;
	uint8 unknown4[10];
	InternalTypeTaggedValue with;
	uint8 unknown5;
	uint8 minutes;
	uint8 seconds;
	uint8 hundredthsOfSeconds;
	uint32 unknown6;
	uint32 unknown7;
	uint8 unknown8[10];
	uint8 withSourceLength;
	uint8 withStringLength;

	Common::String withSource;
	Common::String withString;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct BoundaryDetectionMessengerModifier : public DataObject {
	BoundaryDetectionMessengerModifier();

	enum Flags {
		kDetectTopEdge = 0x1000,
		kDetectBottomEdge = 0x0800,
		kDetectLeftEdge = 0x0400,
		kDetectRightEdge = 0x0200,
		kDetectExiting = 0x0100, // Off = once exited
		kWhileDetected = 0x0080, // Off = on first detected
	};

	TypicalModifierHeader modHeader;
	uint16 messageFlagsHigh;
	Event enableWhen;
	Event disableWhen;
	Event send;
	uint16 unknown2;
	uint32 destination;
	uint8 unknown3[10];
	InternalTypeTaggedValue with;
	uint8 withSourceLength;
	uint8 withStringLength;

	Common::String withSource;
	Common::String withString;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct CollisionDetectionMessengerModifier : public DataObject {
	CollisionDetectionMessengerModifier();

	enum ModifierFlags {
		kDetectLayerInFront = 0x10000000,
		kDetectLayerBehind = 0x08000000,
		kSendToCollidingElement = 0x02000000,
		kSendToOnlyFirstCollidingElement = 0x00200000,

		kDetectionModeMask = 0x01c00000,
		kDetectionModeFirstContact = 0x01400000,
		kDetectionModeWhileInContact = 0x01000000,
		kDetectionModeExiting = 0x00800000,

		kNoCollideWithParent = 0x00100000,
	};

	TypicalModifierHeader modHeader;
	uint32 messageAndModifierFlags;
	Event enableWhen;
	Event disableWhen;
	Event send;
	uint16 unknown2;
	uint32 destination;
	uint8 unknown3[10];
	InternalTypeTaggedValue with;
	uint8 withSourceLength;
	uint8 withStringLength;

	Common::String withSource;
	Common::String withString;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct KeyboardMessengerModifier : public DataObject {
	enum KeyStateFlags {
		kOnDown = 0x10000000,
		kOnUp = 0x4000000,
		kOnRepeat = 0x8000000,

		kKeyStateMask = (kOnDown | kOnUp | kOnRepeat),
	};

	enum KeyModifiers {
		kControl = 0x1000,
		kCommand = 0x0100,
		kOption = 0x0800,
	};

	enum KeyCodes {
		kAny = 0x00,
		kHome = 0x01,
		kEnter = 0x03,
		kEnd = 0x04,
		kHelp = 0x05,
		kBackspace = 0x08,
		kTab = 0x09,
		kPageUp = 0x0b,
		kPageDown = 0x0c,
		kReturn = 0x0d,
		kEscape = 0x1b,
		kArrowLeft = 0x1c,
		kArrowRight = 0x1d,
		kArrowUp = 0x1e,
		kArrowDown = 0x1f,
		kDelete = 0x7f,
	};

	KeyboardMessengerModifier();

	TypicalModifierHeader modHeader;
	uint32 messageFlagsAndKeyStates;
	uint16 unknown2;
	uint16 keyModifiers;
	uint8 keycode;
	uint8 unknown4[7];
	Event message;
	uint16 unknown7;
	uint32 destination;
	uint8 unknown9[10];
	InternalTypeTaggedValue with;
	uint8 withSourceLength;
	uint8 withStringLength;

	Common::String withSource;
	Common::String withString;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct TextStyleModifier : public DataObject {
	TextStyleModifier();

	TypicalModifierHeader modHeader;

	uint8 unknown1[4];
	uint16 macFontID;
	uint8 flags;
	uint8 unknown2;
	uint16 size;
	ColorRGB16 textColor;		// Appears to not actually be used
	ColorRGB16 backgroundColor; // Appears to not actually be used
	uint16 alignment;
	uint16 unknown3;
	Event applyWhen;
	Event removeWhen;
	uint16 lengthOfFontFamilyName;

	Common::String fontFamilyName;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct GraphicModifier : public DataObject {
	GraphicModifier();

	TypicalModifierHeader modHeader;

	uint16 unknown1;
	Event applyWhen;
	Event removeWhen;
	uint8 unknown2[2];
	uint16 inkMode;
	uint16 shape;

	struct MacPart {
		uint8 unknown4_1[6];
		uint8 unknown4_2[26];
	};

	struct WinPart {
		uint8 unknown5_1[4];
		uint8 unknown5_2[22];
	};

	union PlatformPart {
		MacPart mac;
		WinPart win;
	};

	bool haveMacPart;
	bool haveWinPart;
	PlatformPart platform;

	ColorRGB16 foreColor;
	ColorRGB16 backColor;
	uint16 borderSize;
	ColorRGB16 borderColor;
	uint16 shadowSize;
	ColorRGB16 shadowColor;

	uint16 numPolygonPoints;
	uint8 unknown6[8];

	Common::Array<Point> polyPoints;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct ImageEffectModifier : public DataObject {
	ImageEffectModifier();

	enum Types {
		kTypeInvert = 1,
		kTypeSelectedBevels = 2,
		kTypeDeselectedBevels = 3,
		kTypeToneDown = 4,
		kTypeToneUp = 5,
	};

	TypicalModifierHeader modHeader;

	uint32 flags;
	uint16 type;
	Event applyWhen;
	Event removeWhen;
	uint16 bevelWidth;
	uint16 toneAmount;
	uint8 unknown2[2];

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct ReturnModifier : public DataObject {
	ReturnModifier();

	TypicalModifierHeader modHeader;

	Event executeWhen;
	uint16 unknown1;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct CursorModifierV1 : public DataObject {
	CursorModifierV1();

	struct MacOnlyPart {
		MacOnlyPart();

		Event applyWhen;
		uint32 unknown1;
		uint16 unknown2;
		uint32 cursorIndex;
	};

	TypicalModifierHeader modHeader;

	bool hasMacOnlyPart;
	MacOnlyPart macOnlyPart;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct CompoundVariableModifier : public DataObject {
	CompoundVariableModifier();

	// This doesn't follow the usual modifier header layout
	uint32 modifierFlags;
	uint32 sizeIncludingTag;
	uint8 unknown1[2];	// Extra field
	uint32 guid;
	uint8 unknown4[6];
	uint32 unknown5;
	Point editorLayoutPosition;
	uint16 lengthOfName;
	uint16 numChildren;
	uint8 unknown7[4];

	Common::String name;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct BooleanVariableModifier : public DataObject {
	BooleanVariableModifier();

	TypicalModifierHeader modHeader;
	uint8 value;
	uint8 unknown5;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct IntegerVariableModifier : public DataObject {
	IntegerVariableModifier();

	TypicalModifierHeader modHeader;
	uint8 unknown1[4];
	int32 value;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct IntegerRangeVariableModifier : public DataObject {
	IntegerRangeVariableModifier();

	TypicalModifierHeader modHeader;
	uint8 unknown1[4];
	IntRange range;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct VectorVariableModifier : public DataObject {
	VectorVariableModifier();

	TypicalModifierHeader modHeader;
	uint8 unknown1[4];
	XPFloatVector vector;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct PointVariableModifier : public DataObject {
	PointVariableModifier();

	TypicalModifierHeader modHeader;

	uint8 unknown5[4];
	Point value;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct FloatingPointVariableModifier : public DataObject {
	FloatingPointVariableModifier();

	TypicalModifierHeader modHeader;
	uint8 unknown1[4];
	Common::XPFloat value;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct StringVariableModifier : public DataObject {
	StringVariableModifier();

	TypicalModifierHeader modHeader;
	uint32 lengthOfString;
	uint8 unknown1[4];
	Common::String value;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct ObjectReferenceVariableModifierV1 : public DataObject {
	ObjectReferenceVariableModifierV1();

	TypicalModifierHeader modHeader;
	uint32 unknown1;
	Event setToSourcesParentWhen;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct PlugInModifierData {
	virtual ~PlugInModifierData();
	virtual DataReadErrorCode load(PlugIn &plugIn, const PlugInModifier &prefix, DataReader &reader) = 0;
};

struct PlugInModifier : public DataObject {
	PlugInModifier();

	uint32 modifierFlags;
	uint32 codedSize;	// Total size on Mac but (size + (name length * 255)) on Windows for some reason
	char modifierName[17];
	uint32 guid;
	uint8 unknown2[6];
	uint16 plugInRevision;
	uint32 unknown4;
	Point editorLayoutPosition;
	uint16 lengthOfName;

	Common::String name;

	uint32 subObjectSize;

	Common::SharedPtr<PlugInModifierData> plugInData;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct Debris : public DataObject {
	Debris();

	uint32 persistFlags;
	uint32 sizeIncludingTag;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct ColorTableAsset : public DataObject {
	ColorTableAsset();

	uint32 persistFlags;
	uint32 sizeIncludingTag;
	uint8 unknown1[4];
	uint32 assetID;
	uint32 unknown2; // Usually zero-fill but sometimes contains 0xb

	ColorRGB16 colors[256];

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct MovieAsset : public DataObject {
	struct MacPart {
		uint8 unknown5_1[66];
		uint8 unknown6[12];
		uint8 unknown8[4];
	};

	struct WinPart {
		uint8 unknown3_1[32];
		uint8 unknown4[12];
		uint8 unknown7[12];
	};

	union PlatformPart {
		MacPart mac;
		WinPart win;
	};

	MovieAsset();

	uint32 persistFlags;
	uint32 assetAndDataCombinedSize;
	uint8 unknown1[4];
	uint32 assetID;
	uint8 unknown1_1[4];
	uint16 extFileNameLength;

	uint32 movieDataPos;
	uint32 moovAtomPos;
	uint32 movieDataSize;

	bool haveMacPart;
	bool haveWinPart;
	PlatformPart platform;

	Common::String extFileName;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct AVIMovieAsset : public DataObject {
	AVIMovieAsset();

	uint8 unknown1[12];
	uint32 assetID;
	uint8 unknown2[4];
	uint16 extFileNameLength;
	uint8 unknown3[60];

	Common::String extFileName;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct AudioAsset : public DataObject {
	struct MacPart {
		uint8 unknown4[4];
		uint8 unknown5[5];
		uint8 unknown6[3];
		uint8 unknown8[20];
	};

	struct WinPart {
		uint8 unknown9[3];
		uint8 unknown10[3];
		uint8 unknown11[18];
		uint8 unknown12_1[2];
	};

	union PlatformPart {
		MacPart mac;
		WinPart win;
	};

	struct CuePoint {
		uint8 unknown13[2];
		uint32 unknown14;
		uint32 position;
		uint32 cuePointID;
	};

	AudioAsset();

	uint32 persistFlags;
	uint32 assetAndDataCombinedSize;
	uint8 unknown2[4];
	uint32 assetID;
	uint8 unknown3[20];
	uint16 sampleRate1;
	uint8 bitsPerSample;
	uint8 encoding1;
	uint8 channels;
	uint8 codedDuration[4];
	uint16 sampleRate2;
	uint32 cuePointDataSize;
	uint16 numCuePoints;
	uint8 unknown14[4];
	uint32 filePosition;
	uint32 size;

	Common::Array<CuePoint> cuePoints;

	bool haveMacPart;
	bool haveWinPart;
	bool isBigEndian;
	PlatformPart platform;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct ImageAsset : public DataObject {
	struct MacPart {
		uint8 unknown7[44];
	};

	struct WinPart {
		uint8 unknown8[10];
	};

	union PlatformPart {
		WinPart win;
		MacPart mac;
	};

	ImageAsset();

	uint32 persistFlags;
	uint32 unknown1;
	uint8 unknown2[4];
	uint32 assetID;
	uint32 unknown3;

	Rect rect1;
	uint32 hdpiFixed;
	uint32 vdpiFixed;
	uint16 bitsPerPixel;
	uint8 unknown4[2];
	uint8 unknown5[4];
	uint8 unknown6[8];
	Rect rect2;
	uint32 filePosition;
	uint32 size;

	bool haveMacPart;
	bool haveWinPart;
	PlatformPart platform;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct MToonAsset : public DataObject {
	struct MacPart {
		uint8 unknown10[88];
	};

	struct WinPart {
		uint8 unknown11[54];
	};

	union PlatformUnion {
		MacPart mac;
		WinPart win;
	};

	struct FrameDef {
		struct MacPart {
			uint8 unknown17[4];
		};

		struct WinPart {
			uint8 unknown18[2];
		};

		union PlatformUnion {
			MacPart mac;
			WinPart win;
		};

		FrameDef();

		uint8 unknown12[4];
		Rect rect1;
		uint32 dataOffset;
		uint8 unknown13[2];
		uint32 compressedSize;
		uint8 unknown14;
		uint8 keyframeFlag;
		uint8 platformBit;
		uint8 unknown15;
		Rect rect2;
		uint32 hdpiFixed;
		uint32 vdpiFixed;
		uint16 bitsPerPixel;
		uint32 unknown16;
		uint16 decompressedBytesPerRow;
		uint32 decompressedSize;

		PlatformUnion platform;
	};

	struct FrameRangeDef {
		FrameRangeDef();

		uint32 startFrame;
		uint32 endFrame;
		uint8 lengthOfName;
		uint8 unknown14;

		Common::String name; // Null terminated
	};

	enum {
		kEncodingFlag_TemporalCompression = 0x80,
		kEncodingFlag_HasRanges = 0x20000000,
		kEncodingFlag_Trimming = 0x08,
	};

	struct FrameRangePart {
		FrameRangePart();

		uint32 tag;
		uint32 sizeIncludingTag;

		uint32 numFrameRanges;
		Common::Array<FrameRangeDef> frameRanges;
	};

	MToonAsset();

	uint32 marker;
	uint8 unknown1[8];
	uint32 assetID;

	bool haveMacPart;
	bool haveWinPart;
	PlatformUnion platform;

	uint32 frameDataPosition;
	uint32 sizeOfFrameData;

	// mToon data
	uint32 mtoonHeader[2];
	uint16 version;
	uint8 unknown2[4];
	uint32 encodingFlags;
	Rect rect;

	uint16 numFrames;
	uint8 unknown3[14];
	uint16 bitsPerPixel;
	uint32 codecID;
	uint8 unknown4_1[8];
	uint32 codecDataSize;
	Point registrationPoint;

	Common::Array<FrameDef> frames;

	// Codec data appears to be a 16-byte header followed by a QuickTime sample description
	// Note that the sample description is partly useless because frames can have different sizes
	// and the sample desc is only for the last frame!
	//
	// The 16-byte header is:
	// uint32be size of codec data
	// char[4] codec ID
	// byte[8] unknown (all 0?)
	Common::Array<uint8> codecData;

	FrameRangePart frameRangesPart;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct TextAsset : public DataObject {
	struct MacFormattingSpan {
		uint8 unknown9[2];
		uint16 spanStart;
		uint8 unknown10[4];
		uint16 fontID;
		uint8 fontFlags;
		uint8 unknown11[1];
		uint16 size;
		uint8 unknown12[6];
	};

	struct MacPart {
		uint8 unknown3[44];
	};

	struct WinPart {
		uint8 unknown4[10];
	};

	union PlatformPart {
		MacPart mac;
		WinPart win;
	};

	TextAsset();

	uint32 persistFlags;
	uint32 sizeIncludingTag;
	uint32 unknown1;
	uint32 assetID;
	uint32 unknown2;
	Rect bitmapRect;
	uint32 hdpi;
	uint32 vdpi;
	uint16 unknown5;
	uint8 pitchBigEndian[2];
	uint32 unknown6;

	uint32 bitmapSize;
	uint8 unknown7[20];
	uint32 textSize;
	uint8 unknown8[8];
	uint16 alignment;
	uint16 isBitmap;

	bool haveMacPart;
	bool haveWinPart;
	PlatformPart platform;

	Common::String text;
	Common::Array<uint8> bitmapData;

	bool isBottomUp;

	Common::Array<MacFormattingSpan> macFormattingSpans;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct AssetDataChunk : public DataObject {
	AssetDataChunk();

	uint32 unknown1;
	uint32 sizeIncludingTag;
	int64 filePosition;

protected:
	DataReadErrorCode load(DataReader &reader) override;
};

struct IPlugInModifierDataFactory : public IInterfaceBase {
	virtual Common::SharedPtr<Data::PlugInModifierData> createModifierData() const = 0;
	virtual PlugIn &getPlugIn() const = 0;
};

class PlugInModifierRegistry {
public:
	const IPlugInModifierDataFactory *findLoader(const char *modifierName) const;
	void registerLoader(const char *modifierName, const IPlugInModifierDataFactory *loader);

private:
	Common::HashMap<Common::String, const IPlugInModifierDataFactory *> _loaders;
};

DataReadErrorCode loadDataObject(const PlugInModifierRegistry &registry, DataReader &reader, Common::SharedPtr<DataObject> &outObject);

template<size_t TSize>
inline bool DataReader::readBytes(uint8(&arr)[TSize]) {
	return this->read(arr, TSize);
}

template<size_t TSize>
inline bool DataReader::readChars(char (&arr)[TSize]) {
	return this->read(arr, TSize);
}

} // End of namespace Data

} // End of namespace MTropolis

#endif /* MTROPOLIS_DATA_H */