File: ws_load.cpp

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 (1847 lines) | stat: -rw-r--r-- 57,792 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
/* 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/>.
 *
 */

#include "m4/wscript/ws_load.h"
#include "m4/wscript/ws_machine.h"
#include "m4/wscript/wst_regs.h"
#include "m4/core/errors.h"
#include "m4/core/imath.h"
#include "m4/graphics/graphics.h"
#include "m4/mem/mem.h"
#include "m4/vars.h"

namespace M4 {

#define CHUNK_MACH	0x4D414348	//'MACH'
#define CHUNK_SEQU	0x53455155	//'SEQU'
#define CHUNK_DATA	0x44415441	//'DATA'
#define CHUNK_CELS	0x43454C53	//'CELS'

#define CHUNK_NECS	0x4E454353	//INTEL 'SCEN'
#define CHUNK_HCAM	0x4843414D	//INTEL 'MACH'
#define CHUNK_UQES	0x55514553	//INTEL 'SEQU'
#define CHUNK_SLEC	0x534C4543	//INTEL 'CELS'
#define CHUNK_ATAD	0x41544144	//INTEL 'DATA'

#define MACH_NUM_STATES		0
#define MACH_OFFSETS		1

#define SEQU_NUM_VARS		0
#define SEQU_SEQU_START		1

#define DATA_REC_COUNT		0
#define DATA_REC_SIZE		1
#define DATA_REC_START		2

#define MAX_ASSET_HASH		255

/**
 * This tries to encapsulate an uint32 * that also does endian conversion automatically.
 * GetNextInt32 can't just return values directly, because in the case of some chunk
 * types, the underlying data being pointed to gets byte swapped.
 */
class IntPointer {
private:
	int32 *_ptr = nullptr;

public:
	void set(int32 *ptr) { _ptr = ptr; }
	void swap() { *_ptr = SWAP_INT32(*_ptr); }
	int32 *ptr() const { return _ptr; }
	int32 operator*() const { return READ_LE_INT32(_ptr); }
};

static int32 ProcessCELS(const char * /*assetName*/, char **parseAssetPtr, char * /*mainAssetPtr*/, char *endOfAssetBlock,
	int32 **dataOffset, int32 **palDataOffset, RGB8 *myPalette);
static void RestoreSSPaletteInfo(RGB8 *myPalette, int32 *palPtr);

static bool GetNextint32(char **assetPtr, char *endOfAssetBlock, IntPointer &returnVal) {
	// Check to see if we still have an int32 available
	if ((endOfAssetBlock - *assetPtr) < 4) {
		return false;
	}

	// Get the next int32
	returnVal.set((int32 *)*assetPtr);
	*assetPtr += 4;

	return true;
}

bool InitWSAssets() {
	int32 i;

	// Make sure this is only called once.
	if (_GWS(wsloaderInitialized)) {
		error_show(FL, 'WSSN');
	}

	// Allocate space for the tables used by the loader and the resource io MACHine tables
	if ((_GWS(globalMACHnames) = (char **)mem_alloc(sizeof(char *) * 256, "MACH resource table")) == nullptr) {
		return false;
	}
	if ((_GWS(globalMACHHandles) = (MemHandle *)mem_alloc(sizeof(Handle) * 256, "CELS Handles table")) == nullptr) {
		return false;
	}
	if ((_GWS(globalMACHoffsets) = (int32 *)mem_alloc(sizeof(int32 *) * 256, "MACH offsets table")) == nullptr) {
		return false;
	}
	for (i = 0; i < 256; i++) {
		_GWS(globalMACHnames)[i] = nullptr;
		_GWS(globalMACHHandles)[i] = nullptr;
		_GWS(globalMACHoffsets)[i] = -1;
	}

	// SEQUence tables
	if ((_GWS(globalSEQUnames) = (char **)mem_alloc(sizeof(char *) * 256, "SEQU resource table")) == nullptr) {
		return false;
	}
	if ((_GWS(globalSEQUHandles) = (MemHandle *)mem_alloc(sizeof(Handle) * 256, "CELS Handles table")) == nullptr) {
		return false;
	}
	if ((_GWS(globalSEQUoffsets) = (int32 *)mem_alloc(sizeof(int32 *) * 256, "SEQU offsets table")) == nullptr) {
		return false;
	}
	for (i = 0; i < 256; i++) {
		_GWS(globalSEQUnames)[i] = nullptr;
		_GWS(globalSEQUHandles)[i] = nullptr;
		_GWS(globalSEQUoffsets)[i] = -1;
	}

	// DATA tables
	if ((_GWS(globalDATAnames) = (char **)mem_alloc(sizeof(char *) * 256, "DATA resource table")) == nullptr) {
		return false;
	}
	if ((_GWS(globalDATAHandles) = (MemHandle *)mem_alloc(sizeof(Handle) * 256, "CELS Handles table")) == nullptr) {
		return false;
	}
	if ((_GWS(globalDATAoffsets) = (int32 *)mem_alloc(sizeof(int32 *) * 256, "DATA offsets table")) == nullptr) {
		return false;
	}
	for (i = 0; i < 256; i++) {
		_GWS(globalDATAnames)[i] = nullptr;
		_GWS(globalDATAHandles)[i] = nullptr;
		_GWS(globalDATAoffsets)[i] = -1;
	}

	// CELS tables
	if ((_GWS(globalCELSnames) = (char **)mem_alloc(sizeof(char *) * 256, "CELS resource table")) == nullptr) {
		return false;
	}
	if ((_GWS(globalCELSHandles) = (MemHandle *)mem_alloc(sizeof(MemHandle *) * 256, "CELS Handles table")) == nullptr) {
		return false;
	}
	if ((_GWS(globalCELSoffsets) = (int32 *)mem_alloc(sizeof(int32 *) * 256, "CELS offsets table")) == nullptr) {
		return false;
	}
	if ((_GWS(globalCELSPaloffsets) = (int32 *)mem_alloc(sizeof(int32 *) * 256, "CELS pal offsets table")) == nullptr) {
		return false;
	}
	for (i = 0; i < 256; i++) {
		_GWS(globalCELSnames)[i] = nullptr;
		_GWS(globalCELSHandles)[i] = nullptr;
		_GWS(globalCELSoffsets)[i] = -1;
		_GWS(globalCELSPaloffsets)[i] = -1;
	}

	// Set the global to indicate the loader is active
	_GWS(wsloaderInitialized) = true;

	return true;
}

bool ClearWSAssets(uint32 assetType, int32 minHash, int32 maxHash) {
	int32 i;
	assert(maxHash >= minHash);

	if (!_GWS(wsloaderInitialized)) {
		return false;
	}

	// Bounds checking
	if (minHash < 0)
		minHash = 0;
	if (maxHash > MAX_ASSET_HASH)
		maxHash = MAX_ASSET_HASH;

	switch (assetType) {
	case _WS_ASSET_MACH:
		// Clear the machines table for entries [minHash, maxHash]
		for (i = minHash; i <= maxHash; i++) {
			terminateMachinesByHash(i);
			if (_GWS(globalMACHnames)[i]) {
				rtoss(_GWS(globalMACHnames)[i]);
				mem_free(_GWS(globalMACHnames)[i]);
				_GWS(globalMACHnames)[i] = nullptr;
				_GWS(globalMACHHandles)[i] = nullptr;
				_GWS(globalMACHoffsets)[i] = -1;
			}
		}
		break;

	case _WS_ASSET_SEQU:
		// Clear the sequences table for entries [minHash, maxHash]
		for (i = minHash; i <= maxHash; i++) {
			if (_GWS(globalSEQUnames)[i]) {
				rtoss(_GWS(globalSEQUnames)[i]);
				mem_free(_GWS(globalSEQUnames)[i]);
				_GWS(globalSEQUnames)[i] = nullptr;
				_GWS(globalSEQUHandles)[i] = nullptr;
				_GWS(globalSEQUoffsets)[i] = -1;
			}
		}
		break;

	case _WS_ASSET_DATA:
		// Clear the data table for entries [minHash, maxHash]
		for (i = minHash; i <= maxHash; i++) {
			if (_GWS(globalDATAnames)[i]) {
				rtoss(_GWS(globalDATAnames)[i]);
				mem_free(_GWS(globalDATAnames)[i]);
				_GWS(globalDATAnames)[i] = nullptr;
				_GWS(globalDATAHandles)[i] = nullptr;
				_GWS(globalDATAoffsets)[i] = -1;
			}
		}
		break;

	case _WS_ASSET_CELS:
		// Clear the cels tables for entries [minHash, maxHash]
		for (i = minHash; i <= maxHash; i++) {
			if (_GWS(globalCELSnames)[i]) {
				rtoss(_GWS(globalCELSnames)[i]);
				mem_free(_GWS(globalCELSnames)[i]);
				_GWS(globalCELSnames)[i] = nullptr;
				_GWS(globalCELSHandles)[i] = nullptr;
				_GWS(globalCELSoffsets)[i] = -1;
				_GWS(globalCELSPaloffsets)[i] = -1;
			}
		}
		break;

	default:
		return false;
	}
	return true;
}

void ShutdownWSAssets(void) {
	if (!_GWS(wsloaderInitialized))
		return;

	// For each asset type, clear the entire table
	ClearWSAssets(_WS_ASSET_MACH, 0, MAX_ASSET_HASH);
	ClearWSAssets(_WS_ASSET_SEQU, 0, MAX_ASSET_HASH);
	ClearWSAssets(_WS_ASSET_CELS, 0, MAX_ASSET_HASH);
	ClearWSAssets(_WS_ASSET_DATA, 0, MAX_ASSET_HASH);

	// Deallocate all tables
	if (_GWS(globalMACHnames)) mem_free(_GWS(globalMACHnames));
	if (_GWS(globalSEQUnames)) mem_free(_GWS(globalSEQUnames));
	if (_GWS(globalDATAnames)) mem_free(_GWS(globalDATAnames));
	if (_GWS(globalCELSnames)) mem_free(_GWS(globalCELSnames));

	if (_GWS(globalMACHHandles)) mem_free(_GWS(globalMACHHandles));
	if (_GWS(globalMACHoffsets)) mem_free(_GWS(globalMACHoffsets));
	if (_GWS(globalSEQUHandles)) mem_free(_GWS(globalSEQUHandles));
	if (_GWS(globalSEQUoffsets)) mem_free(_GWS(globalSEQUoffsets));
	if (_GWS(globalDATAHandles)) mem_free(_GWS(globalDATAHandles));
	if (_GWS(globalDATAoffsets)) mem_free(_GWS(globalDATAoffsets));
	if (_GWS(globalCELSHandles)) mem_free(_GWS(globalCELSHandles));
	if (_GWS(globalCELSoffsets)) mem_free(_GWS(globalCELSoffsets));
	if (_GWS(globalCELSPaloffsets)) mem_free(_GWS(globalCELSPaloffsets));

	_GWS(wsloaderInitialized) = false;
}

bool LoadWSAssets(const char *wsAssetName) {
	return LoadWSAssets(wsAssetName, _G(master_palette));
}

bool LoadWSAssets(const char *wsAssetName, RGB8 *myPalette) {
	MemHandle workHandle;
	char *mainAssetPtr, *parseAssetPtr, *endOfAssetBlock;
	uint32 *tempPtr;
	IntPointer chunkType, chunkSize, chunkHash;
	bool finished, chunkSwap;
	int32 *celsPtr, *palPtr;
	int32 i;
	int32 assetSize;

	// Check that the loader has been initialized
	if (!_GWS(wsloaderInitialized)) {
		error_show(FL, 'WSLI');
	}

	// Use the resource io manager to read in the entire block
	if ((workHandle = rget(wsAssetName, &assetSize)) == nullptr) {
		error_show(FL, 'FNF!', "Asset Name: %s", wsAssetName);
	}

	// Lock the handle so we can step through the chunk
	HLock(workHandle);
	mainAssetPtr = (char *)(*workHandle);

	endOfAssetBlock = (char *)((intptr)mainAssetPtr + assetSize);

	parseAssetPtr = mainAssetPtr;

	// Set the finished flag
	finished = false;

	// Get the first chunkType
	if (!GetNextint32(&parseAssetPtr, endOfAssetBlock, chunkType)) {
		finished = true;
	}

	// Process each chunk according to type
	while (!finished) {
		// Read in the chunk size and hash number
		if (!GetNextint32(&parseAssetPtr, endOfAssetBlock, chunkSize)) {
			error_show(FL, 'WSLE', "Asset Name: %s", wsAssetName);
		}
		if (!GetNextint32(&parseAssetPtr, endOfAssetBlock, chunkHash)) {
			error_show(FL, 'WSLE', "Asset Name: %s", wsAssetName);
		}

		// Process the chunk according to type
		chunkSwap = false;
		switch (*chunkType) {
		// Chunk is a machine chunk
		case CHUNK_HCAM:
			// Byte swap the type, size and hash and continue through case CHUNK_MACH.
			chunkType.swap();
			chunkSize.swap();
			chunkHash.swap();
			chunkSwap = true;
			// Fall through

		case CHUNK_MACH:
			// Check the validity of the machine hash number, and clear it
			if (*chunkHash > MAX_ASSET_HASH) {
				error_show(FL, 'WSLA', "Asset Name: %s, MACH hash was: %d", wsAssetName, *chunkHash);
			}
			ClearWSAssets(_WS_ASSET_MACH, *chunkHash, *chunkHash);

			// Store the resource name, and the offset into the resource block
			_GWS(globalMACHnames)[*chunkHash] = mem_strdup(wsAssetName);
			_GWS(globalMACHHandles)[*chunkHash] = workHandle;
			_GWS(globalMACHoffsets)[*chunkHash] = parseAssetPtr - mainAssetPtr;

			// Check that the assetblocksize is big enough that the chunk body was read in...
			if ((endOfAssetBlock - parseAssetPtr) < (int)(*chunkSize - 12)) {
				error_show(FL, 'WSLE', "Asset Name: %s, MACH hash was: %d", wsAssetName, *chunkHash);
			}

			// Byteswap the entire machine if necessary
			if (chunkSwap) {
				tempPtr = (uint32 *)parseAssetPtr;
				for (i = 0; i < (*chunkSize - 12) >> 2; i++) {	//>>2 - chunkSize is bytes, not int32s
					*tempPtr = SWAP_INT32(*tempPtr);
					tempPtr++;
				}
			}

			// Update the assetPtr to the beginning of the next chunk
			parseAssetPtr += *chunkSize - 12;
			break;

		case CHUNK_UQES:
			// Chunk is a machine chunk
			// Byte swap the type, size and hash and continue through case CHUNK_SEQU.
			chunkType.swap();
			chunkSize.swap();
			chunkHash.swap();
			chunkSwap = true;
			// Fall through

		case CHUNK_SEQU:
			// Check the validity of the sequence hash number, and clear it
			if (*chunkHash > MAX_ASSET_HASH) {
				error_show(FL, 'WSLA', "Asset Name: %s, SEQU hash was: %d", wsAssetName, *chunkHash);
			}
			ClearWSAssets(_WS_ASSET_SEQU, *chunkHash, *chunkHash);

			// Store the resource name, and the offset into the resource block
			_GWS(globalSEQUnames)[*chunkHash] = mem_strdup(wsAssetName);
			_GWS(globalSEQUHandles)[*chunkHash] = workHandle;
			_GWS(globalSEQUoffsets)[*chunkHash] = (intptr)parseAssetPtr - (intptr)mainAssetPtr;

			// Check that the assetblocksize is big enough that the chunk body was read in...
			if ((endOfAssetBlock - parseAssetPtr) < (int)(*chunkSize - 12)) {
				error_show(FL, 'WSLE', "Asset Name: %s, SEQU hash was: %d", wsAssetName, *chunkHash);
			}

			// Byteswap the entire sequence if necessary
			if (chunkSwap) {
				tempPtr = (uint32 *)parseAssetPtr;
				for (i = 0; i < (*chunkSize - 12) >> 2; i++) {	//>>2 - chunkSize is bytes, not int32s
					*tempPtr = SWAP_INT32(*tempPtr);
					tempPtr++;
				}
			}

			// Update the assetPtr to the beginning of the next chunk
			parseAssetPtr += *chunkSize - 12;
			break;

		case CHUNK_ATAD:
			// Chunk is a data chunk
			// Byte swap the type, size and hash and continue through case CHUNK_DATA.
			chunkType.swap();
			chunkSize.swap();
			chunkHash.swap();
			chunkSwap = true;
			// Fall through

		case CHUNK_DATA:
			// Check the validity of the data block hash number, and clear it
			if (*chunkHash > MAX_ASSET_HASH) {
				error_show(FL, 'WSLA', "Asset Name: %s, DATA hash was: %d", wsAssetName, *chunkHash);
			}
			ClearWSAssets(_WS_ASSET_DATA, *chunkHash, *chunkHash);

			// Store the resource name, and the offset into the resource block
			_GWS(globalDATAnames)[*chunkHash] = mem_strdup(wsAssetName);
			_GWS(globalDATAHandles)[*chunkHash] = workHandle;
			_GWS(globalDATAoffsets)[*chunkHash] = (intptr)parseAssetPtr - (intptr)mainAssetPtr;

			// Check that the assetblocksize is big enough that the chunk body was read in...
			if ((endOfAssetBlock - parseAssetPtr) < (int)(*chunkSize - 12)) {
				error_show(FL, 'WSLE', "Asset Name: %s, DATA hash was: %d", wsAssetName, *chunkHash);
			}

			// Byteswap the entire data block if necessary
			if (chunkSwap) {
				tempPtr = (uint32 *)parseAssetPtr;
				for (i = 0; i < (*chunkSize - 12) >> 2; i++) {	//>>2 - chunkSize is bytes, not int32s
					*tempPtr = SWAP_INT32(*tempPtr);
					tempPtr++;
				}
			}

			// Update the assetPtr to the beginning of the next chunk
			parseAssetPtr += *chunkSize - 12;
			break;

		case CHUNK_SLEC:
			// Byte swap the type, size and hash and continue through case CHUNK_CELS.
			chunkType.swap();
			chunkSize.swap();
			chunkHash.swap();
			chunkSwap = true;
			// Fall through

		case CHUNK_CELS: {
			// Check the validity of the cels hash number, and clear it
			if (*chunkHash > MAX_ASSET_HASH) {
				error_show(FL, 'WSLA', "Asset Name: %s, CELS hash was: %d", wsAssetName, *chunkHash);
			}

			ClearWSAssets(_WS_ASSET_CELS, *chunkHash, *chunkHash);

			// Store the resource name
			_GWS(globalCELSnames)[*chunkHash] = mem_strdup(wsAssetName);

			// Process the SS from the stream file
			if (ProcessCELS(wsAssetName, &parseAssetPtr, mainAssetPtr, endOfAssetBlock, &celsPtr, &palPtr, myPalette) < 0) {
				error_show(FL, 'WSLP', "Asset Name: %s, CELS hash was: %d", wsAssetName, *chunkHash);
			}

			// At this point, celsPtr points to the beginning of the cels data, palPtr to the pal data
			// Store the Handle, and calculate the offsets
			_GWS(globalCELSHandles)[*chunkHash] = workHandle;
			if (celsPtr) {
				_GWS(globalCELSoffsets)[*chunkHash] = (intptr)celsPtr - (intptr)mainAssetPtr;
			} else {
				_GWS(globalCELSoffsets)[*chunkHash] = -1;
			}
			if (palPtr) {
				_GWS(globalCELSPaloffsets)[*chunkHash] = (intptr)palPtr - (intptr)mainAssetPtr;
			} else {
				_GWS(globalCELSPaloffsets)[*chunkHash] = -1;
			}
			break;
		}

		default:
			error_show(FL, 'WSLT', "Asset Name: %s, %d bytes into the file.", wsAssetName,
				(intptr)parseAssetPtr - 12 - (intptr)mainAssetPtr);
			break;
		}

		// Read the next chunkType, or signal we are finished
		if (!GetNextint32(&parseAssetPtr, endOfAssetBlock, chunkType)) {
			finished = true;
		}
	}

	// Unlock the handle now
	HUnLock(workHandle);
	return true;
}

M4sprite *CreateSprite(MemHandle resourceHandle, int32 handleOffset, int32 index, M4sprite *mySprite, bool *streamSeries) {
	uint32 *myCelSource, *data, *offsets, numCels;
	uint32 *celsPtr;

	// Parameter verification
	if ((!resourceHandle) || (!*resourceHandle)) {
		ws_LogErrorMsg(FL, "No sprite source in memory.");
		return nullptr;
	}

	if (!mySprite) {
		mySprite = (M4sprite *)mem_alloc(sizeof(M4sprite), "Sprite");
		if (!mySprite) {
			ws_LogErrorMsg(FL, "Out of memory - mem requested: %d.", sizeof(M4sprite));
			return nullptr;
		}
	}

	// Find the cels source  from the asset block
	HLock(resourceHandle);
	celsPtr = (uint32 *)((intptr)*resourceHandle + handleOffset);

	// Check that the index into the series requested is within a valid range
	numCels = FROM_LE_32(celsPtr[CELS_COUNT]);
	if (index >= (int)numCels) {
		ws_LogErrorMsg(FL, "CreateSprite: Sprite index out of range - max index: %d, requested index: %d", numCels - 1, index);
		return nullptr;
	}

	// Find the offset table, and the beginning of the data for all sprites
	offsets = &celsPtr[CELS_OFFSETS];
	data = &celsPtr[CELS_OFFSETS + numCels];

	// Find the sprite data for the specific sprite in the series
	myCelSource = (uint32 *)((intptr)data + FROM_LE_32(offsets[index]));

	// Set the stream boolean
	if (streamSeries) {
		if (FROM_LE_32(myCelSource[CELS_STREAM]))
			*streamSeries = true;
		else
			*streamSeries = false;
	}

	// Initialize the sprite struct and return it
	mySprite->next = mySprite->prev = nullptr;
	mySprite->sourceHandle = resourceHandle;
	mySprite->xOffset = FROM_LE_32(myCelSource[CELS_X]);
	mySprite->yOffset = FROM_LE_32(myCelSource[CELS_Y]);
	mySprite->w = FROM_LE_32(myCelSource[CELS_W]);
	mySprite->h = FROM_LE_32(myCelSource[CELS_H]);
	mySprite->encoding = (uint8)FROM_LE_32(myCelSource[CELS_COMP]);
	mySprite->data = (uint8 *)&myCelSource[CELS_DATA];

	if ((mySprite->w > 0) && (mySprite->h > 0)) {
		mySprite->sourceOffset = (int32)((intptr)mySprite->data - (intptr)*resourceHandle);
	} else {
		mySprite->sourceOffset = 0;
	}

	// This value MUST be set before sprite draws are called after the block has been locked
	mySprite->data = nullptr;

	// Unlock the handle
	HUnLock(resourceHandle);

	return mySprite;
}

int32 LoadSpriteSeries(const char *assetName, MemHandle *seriesHandle, int32 *celsOffset, int32 *palOffset, RGB8 *myPalette) {
	MemHandle workHandle;
	int32 celsSize, *celsPtr, *palPtr;
	char *mainAssetPtr, *endOfAssetBlock, *parseAssetPtr;
	int32 assetSize;

	// This loads a sprite series into the provided vars, rather than the WS tables.
	// The WS loader is not involved with this procedure.

	// Load in the sprite series
	if ((workHandle = rget(assetName, &assetSize)) == nullptr)
		error_show(FL, 'FNF!', "Sprite series: %s", assetName);

	HLock(workHandle);

	mainAssetPtr = (char *)*workHandle;
	endOfAssetBlock = (char *)((intptr)mainAssetPtr + assetSize);
	parseAssetPtr = mainAssetPtr;

	// Process the SS from the stream file
	if ((celsSize = ProcessCELS(assetName, &parseAssetPtr, mainAssetPtr, endOfAssetBlock, &celsPtr, &palPtr, myPalette)) < 0) {
		error_show(FL, 'WSLP', "series: %s", assetName);
	}

	// Store the handle and offsets
	*seriesHandle = workHandle;
	*celsOffset = (intptr)celsPtr - (intptr)mainAssetPtr;
	*palOffset = (intptr)palPtr - (intptr)mainAssetPtr;

	HUnLock(workHandle);

	return celsSize;
}

int32 LoadSpriteSeriesDirect(const char *assetName, MemHandle *seriesHandle, int32 *celsOffset, int32 *palOffset, RGB8 *myPalette) {
	MemHandle workHandle;
	Common::File f;
	int32 celsSize, *celsPtr, *palPtr;
	char *mainAssetPtr, *endOfAssetBlock, *parseAssetPtr;
	uint32 assetSize;

	// This loads a sprite series into the provided vars, rather than the WS tables.
	// The WS loader is not involved with this procedure.

	// First open the file
	if (!f.open(assetName))
		return -1;

	// Get the file size
	assetSize = f.size();

	// Create a handle big enough to hold the contents of the file
	if ((workHandle = NewHandle(assetSize, "ss file")) == nullptr)
		return -1;

	// Lock the handle and read the contents of the file intoit
	HLock(workHandle);
	mainAssetPtr = (char *)*workHandle;
	if (f.read(mainAssetPtr, assetSize) < assetSize) {
		mem_free(workHandle);
		return -1;
	}

	// Close the file
	f.close();

	// Set up some pointers
	endOfAssetBlock = (char *)((intptr)mainAssetPtr + assetSize);
	parseAssetPtr = mainAssetPtr;

	// Process the SS from the stream file
	if ((celsSize = ProcessCELS(assetName, &parseAssetPtr, mainAssetPtr, endOfAssetBlock, &celsPtr, &palPtr, myPalette)) < 0) {
		error_show(FL, 'WSLP', "series: %s", assetName);
	}

	// Store the handle and offsets
	*seriesHandle = workHandle;
	*celsOffset = (intptr)celsPtr - (intptr)mainAssetPtr;
	*palOffset = (intptr)palPtr - (intptr)mainAssetPtr;
	HUnLock(workHandle);

	return celsSize;
}

bool ws_GetSSMaxWH(MemHandle ssHandle, int32 ssOffset, int32 *maxW, int32 *maxH) {
	int32 *celsPtr;

	// Parameter verification
	if ((!ssHandle) || (!*ssHandle)) {
		ws_LogErrorMsg(FL, "nullptr Handle given.");
		return false;
	}

	// Lock the handle, and get the cels source
	HLock(ssHandle);
	celsPtr = (int32 *)((intptr)*ssHandle + ssOffset);

	// Return the values
	if (maxW) {
		*maxW = FROM_LE_32(celsPtr[CELS_SS_MAX_W]);
	}

	if (maxH) {
		*maxH = FROM_LE_32(celsPtr[CELS_SS_MAX_H]);
	}

	// unlock the handle
	HUnLock(ssHandle);

	return true;
}

int32 AddWSAssetCELS(const char *wsAssetName, int32 hash, RGB8 *myPalette) {
	MemHandle workHandle;
	char *parseAssetPtr, *mainAssetPtr, *endOfAssetBlock;
	int32 emptySlot, i, assetSize, *celsPtr, *palPtr;

	// Check that the loader has been initialized
	if (!_GWS(wsloaderInitialized)) {
		error_show(FL, 'WSLI', "Asset Name: %s", wsAssetName);
	}

	emptySlot = -1;

	// If hash is < 0, find the first available slot
	if (hash < 0) {
		// Search through the SS names table
		for (i = 0; i <= MAX_ASSET_HASH; i++) {
			// See if there is something loaded in this slot
			if (_GWS(globalCELSnames)[i]) {
				if (!strcmp(_GWS(globalCELSnames)[i], wsAssetName)) {
					break;
				}
			} else if (emptySlot < 0) {
				// Else we found an empty slot
				emptySlot = i;
			}
		}
	} else {
		// Else the SS must be stored in the given hash, replacing any previous contents.
		// Index checking
		if (hash > MAX_ASSET_HASH) {
			error_show(FL, 'WSLA', "Asset Name: %s, hash given was %d", wsAssetName, hash);
		}

		// Check to see if the SS is already loaded in the given hash slot
		if (_GWS(globalCELSnames)[hash] && (!strcmp(_GWS(globalCELSnames)[hash], wsAssetName))) {
			if (_GWS(globalCELSPaloffsets)[hash] >= 0) {
				// Get the pointer to the pal data
				workHandle = _GWS(globalCELSHandles)[hash];
				palPtr = (int32 *)((intptr)*workHandle + _GWS(globalCELSPaloffsets)[hash]);

				// Restore the palette and unlock the handle
				RestoreSSPaletteInfo(myPalette, palPtr);
				HUnLock(workHandle);
			}

			// Since the SS is already loaded, return the slot
			return hash;
		} else {
			// The series is not already loaded, set up values for the next if statement
			i = MAX_ASSET_HASH + 1;
			emptySlot = hash;
		}
	}

	// If we've searched the entire table and not found the series, but
	// we found an empty slot to load the SS into
	if ((i > MAX_ASSET_HASH) && (emptySlot >= 0)) {

		if ((workHandle = rget(wsAssetName, &assetSize)) == nullptr) {
			error_show(FL, 'FNF!', wsAssetName);
		}

		// Lock the handle so we can step through the chunk
		HLock(workHandle);
		mainAssetPtr = (char *)(*workHandle);

		parseAssetPtr = mainAssetPtr;
		endOfAssetBlock = (char *)((intptr)mainAssetPtr + assetSize);

		ClearWSAssets(_WS_ASSET_CELS, emptySlot, emptySlot);

		// Store the resource name
		_GWS(globalCELSnames)[emptySlot] = mem_strdup(wsAssetName);

		// Process the SS from the stream file
		if (ProcessCELS(wsAssetName, &parseAssetPtr, mainAssetPtr, endOfAssetBlock, &celsPtr, &palPtr, myPalette) < 0) {
			error_show(FL, 'WSLP', "Asset Name: %s", wsAssetName);
		}

		// At this point, celsPtr points to the beginning of the cels data, palPtr to the pal data
		// Store the Handle, and calculate the offsets
		_GWS(globalCELSHandles)[emptySlot] = workHandle;
		if (celsPtr) {
			_GWS(globalCELSoffsets)[emptySlot] = (intptr)celsPtr - (intptr)mainAssetPtr;
		} else {
			_GWS(globalCELSoffsets)[emptySlot] = -1;
		}
		if (palPtr) {
			_GWS(globalCELSPaloffsets)[emptySlot] = (intptr)palPtr - (intptr)mainAssetPtr;
		} else {
			_GWS(globalCELSPaloffsets)[emptySlot] = -1;
		}

		// Unlock the handle
		HUnLock(workHandle);

		return emptySlot;
	} else if (i < MAX_ASSET_HASH) {
		// Else if we found the SS already loaded
		if (_GWS(globalCELSPaloffsets)[i] >= 0) {
			// Get the pointer to the pal data
			workHandle = _GWS(globalCELSHandles)[i];
			HLock(workHandle);
			palPtr = (int32 *)((intptr)*workHandle + _GWS(globalCELSPaloffsets)[i]);

			// Restore the palette and unlock the handle
			RestoreSSPaletteInfo(myPalette, palPtr);
			HUnLock(workHandle);
		}

		// Return the hash number for the series
		return i;
	} else {
		// Else we searched the entire table, it was not already loaded, and there are no empty slots
		error_show(FL, 'WSLF', "Asset Name: %s", wsAssetName);
	}

	return -1;
}

static int32 ProcessCELS(const char * /*assetName*/, char **parseAssetPtr, char * /*mainAssetPtr*/, char *endOfAssetBlock,
		int32 **dataOffset, int32 **palDataOffset, RGB8 *myPalette) {
	IntPointer celsType, numColors, celsSize;
	int32 *tempPtr, *data, *dataPtr, *offsetPtr, *palData, i, j;
	IntPointer header, format;
	bool byteSwap;

	if (!_GWS(wsloaderInitialized))
		return -1;

	*dataOffset = nullptr;
	*palDataOffset = nullptr;

	// Get the header and the format
	if (!GetNextint32(parseAssetPtr, endOfAssetBlock, header)) {
		ws_LogErrorMsg(FL, "Unable to get the SS header");
		return -1;
	}
	if (!GetNextint32(parseAssetPtr, endOfAssetBlock, format)) {
		ws_LogErrorMsg(FL, "Unable to get the SS format");
		return -1;
	}

	// Make sure the file is tagged "M4SS" (or "SS4M")
	if (*header == HEAD_SS4M) {
		header.swap();
		format.swap();
	} else if (*header != HEAD_M4SS) {
		ws_LogErrorMsg(FL, "SS chunk is not a valid M4SS chunk.");
		return -1;
	}

	// Verify the format is recent. This is a version control
	if (*format < SS_FORMAT) {
		ws_LogErrorMsg(FL, "Format is antique and cannot be read - rebuild series.");
		return -1;
	}

	// Get the CELS chunk type - either PAL information, or the actual SS info
	if (!GetNextint32(parseAssetPtr, endOfAssetBlock, celsType)) {
		ws_LogErrorMsg(FL, "Unable to read the SS chunk type.");
		return -1;
	}

	byteSwap = false;
	// If the chunk is PAL info - celsType == CELS_LAP_ indicates the chunk needs to be byte-swapped.
	if ((*celsType == CELS__PAL) || (*celsType == CELS_LAP_)) {
		// Read the chunk size, and the number of palette colors, and byte-swap if necessary
		if (!GetNextint32(parseAssetPtr, endOfAssetBlock, celsSize)) {
			ws_LogErrorMsg(FL, "Unable to read the SS PAL chunk size.");
			return -1;
		}
		if (!GetNextint32(parseAssetPtr, endOfAssetBlock, numColors)) {
			ws_LogErrorMsg(FL, "Unable to read the SS PAL number of colors.");
			return -1;
		}

		if (*celsType == CELS_LAP_) {
			celsType.swap();
			celsSize.swap();
			numColors.swap();
			byteSwap = true;
		}

		// Verify that we actually got legitimate values
		if (((int32)(*celsSize) <= 0) || ((int32)(*numColors) <= 0)) {
			ws_LogErrorMsg(FL, "Pal info has been corrupted");
			return -1;
		}

		// The asset block offset for palData should begin with the number of colors
		*palDataOffset = numColors.ptr();
		palData = numColors.ptr();

		if (((intptr)endOfAssetBlock - (intptr)(*parseAssetPtr)) < ((int32)(*celsSize) - 8)) {
			ws_LogErrorMsg(FL, "Pal info is larger than asset block.");
			return -1;
		}

		// If the chunk is in the wrong format, byte-swap the entire chunk
		// Note: we do this because we want the data stored in nrgb format
		// The data is always read in low byte first, but we need it high byte first
		// regardless of the endianness of the machine.
		if (byteSwap) {
			tempPtr = numColors.ptr() + 1;
			for (i = 0; i < *numColors; i++) {
				*tempPtr = SWAP_INT32(*tempPtr);
				tempPtr++;
			}
		}

		*parseAssetPtr += *numColors << 2;

		// The palette info has been processed, now it can be stored
		if (myPalette) {
			tempPtr = (int32 *)(&palData[1]);
			for (i = 0; i < *numColors; i++) {
				uint rgb = READ_LE_UINT32(tempPtr);
				j = (rgb & 0xff000000) >> 24;
				myPalette[j].r = (rgb & 0x00ff0000) >> 14;
				myPalette[j].g = (rgb & 0x0000ff00) >> 6;
				myPalette[j].b = (rgb & 0x000000ff) << 2;
				tempPtr++;
			}
		}

		byteSwap = false;
		// Pal chunk has been processed, get the next chunk type
		if (!GetNextint32(parseAssetPtr, endOfAssetBlock, celsType)) {
			ws_LogErrorMsg(FL, "Unable to read the SS chunk type.");
			return -1;
		}
	}

	// The chunk header must now be the SS information (possibly byte-swapped)
	if ((*celsType != CELS___SS) && (*celsType != CELS_SS__)) {
		ws_LogErrorMsg(FL, "SS chunk type is invalid.");
		return -1;
	}

	// Read in the chunk size
	if (!GetNextint32(parseAssetPtr, endOfAssetBlock, celsSize)) {
		ws_LogErrorMsg(FL, "Unable to read the SS chunk size.");
		return -1;
	}

	// Byteswap if necessary
	if (*celsType == CELS_SS__) {
		celsType.swap();
		celsSize.swap();
		byteSwap = true;
	}

	// The asset block offset for the cel should begin with the celsType
	*dataOffset = (int32 *)celsType.ptr();
	data = (int32 *)celsType.ptr();

	// Verify that we actually got legitimate values
	if ((int32)(*celsSize) <= 0) {
		ws_LogErrorMsg(FL, "SS info has been corrupted");
		return -1;
	}

	if (((intptr)endOfAssetBlock - (intptr)data) < (int32)*celsSize) {
		ws_LogErrorMsg(FL, "SS info is larger than asset block.");
		return -1;
	}

	// Check to see if we need to byte-swap the header information.
	if (byteSwap) {
		// The chunk header begins at (*data)[2]
		// byte-swap the entire chunk header
		tempPtr = &(data[2]);
		for (i = 0; i < SS_HEAD_SIZE - 2; i++) {
			*tempPtr = SWAP_INT32(*tempPtr);
			tempPtr++;
		}

		if (FROM_LE_32(data[CELS_COUNT]) <= 0) {
			ws_LogErrorMsg(FL, "SS info has been corrupted");
			return -1;
		}

		// The chunk header has been byteswapped, now we must byte-swap the table of
		// offsets into the chunk, which indicate where each individual sprite can be found.
		offsetPtr = &(data[CELS_OFFSETS]);
		tempPtr = offsetPtr;
		for (i = 0; i < (int32)FROM_LE_32(data[CELS_COUNT]); i++) {
			*tempPtr = SWAP_INT32(*tempPtr);
			tempPtr++;
		}

		// dataPtr points to the beginning of the block which is a concatenation of
		// all the sprites.  Loop through and byteswap each individual sprite header.
		dataPtr = tempPtr;
		for (i = 0; i < (int32)FROM_LE_32(data[CELS_COUNT]); i++) {
			// The beginning of sprite i is the dataPtr + the number of bytes in the offset table
			tempPtr = (int32 *)((intptr)dataPtr + offsetPtr[i]);

			// Byteswap the individual sprite's header
			for (j = 0; j < SS_INDV_HEAD; j++) {
				*tempPtr = SWAP_INT32(*tempPtr);
				tempPtr++;
			}
		}
	}

	// Return the size of the chunk containing the sprite series info
	return *celsSize;
}

static void RestoreSSPaletteInfo(RGB8 *myPalette, int32 *palPtr) {
	uint32 *tempPtr, i, j;

	// Parameter verification
	if ((!myPalette) || (!palPtr))
		return;

	// Set up a pointer that can step through the pal info for the SS, and restore each color
	if (myPalette) {
		tempPtr = (uint32 *)(&palPtr[1]);
		for (i = 0; i < FROM_LE_32(palPtr[0]); i++) {
			uint32 rgb = READ_LE_UINT32(tempPtr);
			j = (rgb & 0xff000000) >> 24;
			myPalette[j].r = (rgb & 0x00ff0000) >> 14;
			myPalette[j].g = (rgb & 0x0000ff00) >> 6;
			myPalette[j].b = (rgb & 0x000000ff) << 2;
			tempPtr++;
		}
	}
}

M4sprite *GetWSAssetSprite(char *spriteName, uint32 hash, uint32 index, M4sprite *mySprite, bool *streamSeries) {
	MemHandle workHandle;
	int32 i;

	// Ensure wsloader has been initialized
	if (!_GWS(wsloaderInitialized)) {
		ws_LogErrorMsg(FL, "WS loader has not been initialized.");
		return nullptr;
	}

	// If spriteName is specified, we search by name, otherwise hash is assumed to be correct
	if (spriteName) {
		if (!_GWS(globalCELSnames)) return nullptr;
		for (i = 0; i <= MAX_ASSET_HASH; i++) {
			if (!strcmp(spriteName, _GWS(globalCELSnames)[i])) {
				break;
			}
		}
		hash = i;
	}

	// Check for valid index, and sprite loaded at that index
	if (hash > MAX_ASSET_HASH) {
		if (spriteName) {
			ws_LogErrorMsg(FL, "Sprite series is not in memory: %s.", spriteName);
		} else {
			ws_LogErrorMsg(FL, "Series number out of range: requested num: %d.", hash);
		}
	}

	// Get the resource handle
	workHandle = _GWS(globalCELSHandles)[hash];

	// Create the sprite
	mySprite = CreateSprite(workHandle, _GWS(globalCELSoffsets)[hash], index, mySprite, streamSeries);

	// Check the sprite
	if (!mySprite) {
		ws_LogErrorMsg(FL, "Series: %s, Hash: %d, index: %d", _GWS(globalCELSnames)[hash], hash, index);
	}

	return mySprite;
}


int32 LoadSpriteSeries(const char *assetName, Handle *seriesHandle, int32 *celsOffset, int32 *palOffset, RGB8 *myPalette) {
	MemHandle workHandle;
	int32 celsSize, *celsPtr, *palPtr;
	char *mainAssetPtr, *endOfAssetBlock, *parseAssetPtr;
	int32 assetSize;

	//This loads a sprite series into the provided vars, rather than the WS tables.
	//The WS loader is not involved with this procedure.

	// Load in the sprite series
	if ((workHandle = rget(assetName, &assetSize)) == nullptr)
		error_show(FL, 'FNF!', "Sprite series: %s", assetName);

	HLock(workHandle);

	mainAssetPtr = (char *)*workHandle;
	endOfAssetBlock = (char *)((intptr)mainAssetPtr + assetSize);
	parseAssetPtr = mainAssetPtr;

	// Process the SS from the stream file
	if ((celsSize = ProcessCELS(assetName, &parseAssetPtr, mainAssetPtr, endOfAssetBlock, &celsPtr, &palPtr, myPalette)) < 0) {
		error_show(FL, 'WSLP', "series: %s", assetName);
	}

	// Store the handle and offsets
	*seriesHandle = workHandle;
	*celsOffset = (intptr)celsPtr - (intptr)mainAssetPtr;
	*palOffset = (intptr)palPtr - (intptr)mainAssetPtr;

	HUnLock(workHandle);

	return celsSize;
}

int32 LoadSpriteSeriesDirect(const char *assetName, Handle *seriesHandle, int32 *celsOffset, int32 *palOffset, RGB8 *myPalette) {
	MemHandle workHandle;
	Common::File f;
	int32 celsSize, *celsPtr, *palPtr;
	char *mainAssetPtr, *endOfAssetBlock, *parseAssetPtr;
	uint32 assetSize;

	// This loads a sprite series into the provided vars, rather than the WS tables.
	// The WS loader is not involved with this procedure.

	// First open the file
	if (!f.open(assetName))
		return -1;

	// Get the size
	assetSize = f.size();

	// Create a handle big enough to hold the contents of the file
	if ((workHandle = NewHandle(assetSize, "ss file")) == nullptr) {
		f.close();
		return -1;
	}

	// Lock the handle and read the contents of the file intoit
	HLock(workHandle);
	mainAssetPtr = (char *)*workHandle;
	if (f.read(mainAssetPtr, assetSize) < assetSize) {
		f.close();
		mem_free(workHandle);
		return -1;
	}

	// Close the file
	f.close();

	// Set up some pointers
	endOfAssetBlock = (char *)((intptr)mainAssetPtr + assetSize);
	parseAssetPtr = mainAssetPtr;

	// Process the SS from the stream file
	if ((celsSize = ProcessCELS(assetName, &parseAssetPtr, mainAssetPtr, endOfAssetBlock, &celsPtr, &palPtr, myPalette)) < 0) {
		error_show(FL, 'WSLP', "series: %s", assetName);
	}

	// Store the handle and offsets
	*seriesHandle = workHandle;
	*celsOffset = (intptr)celsPtr - (intptr)mainAssetPtr;
	*palOffset = (intptr)palPtr - (intptr)mainAssetPtr;
	HUnLock(workHandle);

	return celsSize;
}

CCB *GetWSAssetCEL(uint32 hash, uint32 index, CCB *myCCB) {
	bool streamSeries;
	M4sprite *mySprite;

	// Ensure the WS loader has been initialized.
	if (!_GWS(wsloaderInitialized)) {
		ws_LogErrorMsg(FL, "WS loader has not been initialized.");
		return nullptr;
	}

	//If a memory location to store the CCB has not been provided...
	if (!myCCB) {
		// Create the CCB struct
		if ((myCCB = (CCB *)mem_alloc(sizeof(CCB), "CCB")) == nullptr) {
			ws_LogErrorMsg(FL, "Out of memory - mem requested: %d bytes", sizeof(CCB));
			return nullptr;
		}

		// Create the CCB current location and new location rectangles
		if ((myCCB->currLocation = (M4Rect *)mem_alloc(sizeof(M4Rect), "M4Rect")) == nullptr) {
			ws_LogErrorMsg(FL, "Out of memory - mem requested: %d bytes", sizeof(M4Rect));
			return nullptr;
		}
		myCCB->currLocation->x1 = 0;
		myCCB->currLocation->y1 = 0;
		myCCB->currLocation->x2 = 0;
		myCCB->currLocation->y2 = 0;
		if ((myCCB->newLocation = (M4Rect *)mem_alloc(sizeof(M4Rect), "M4Rect")) == nullptr) {
			ws_LogErrorMsg(FL, "Out of memory - mem requested: %d bytes", sizeof(M4Rect));
			return nullptr;
		}
		myCCB->maxArea = nullptr;
		myCCB->source = nullptr;
	}

	//The source for a CCB is a sprite.  create the sprite from the WS tables hash, index
	mySprite = myCCB->source;
	if ((mySprite = GetWSAssetSprite(nullptr, hash, index, mySprite, &streamSeries)) == nullptr) {
		// Term messages for whatever went wrong are printed from within GetWSAssetSprite()
		return nullptr;
	}
	myCCB->source = mySprite;
	if (streamSeries) {
		myCCB->flags |= CCB_STREAM;
	}

	//Initialize the CCB and return
	myCCB->newLocation->x1 = 0;
	myCCB->newLocation->y1 = 0;
	myCCB->newLocation->x2 = 0;
	myCCB->newLocation->y2 = 0;
	myCCB->scaleX = 0;
	myCCB->scaleY = 0;
	myCCB->seriesName = _GWS(globalCELSnames)[hash];
	return myCCB;
}

int32 GetWSAssetCELCount(uint32 hash) {
	uint32 *celsPtr;

	// Ensure the WS loader has been initialized.
	if (!_GWS(wsloaderInitialized)) {
		ws_LogErrorMsg(FL, "WS loader has not been initialized.");
		return -1;
	}

	// Verify the hash is valid, and a SS for that hash has been loaded
	if (hash > MAX_ASSET_HASH) {
		ws_LogErrorMsg(FL, "Series number out of range: requested num: %d", hash);
		return -1;
	}

	// Make sure the series is still in memory
	if ((!_GWS(globalCELSHandles)[hash]) || (!*_GWS(globalCELSHandles)[hash])) {
		ws_LogErrorMsg(FL, "Series not in memory series num: %d", hash);
		return -1;
	}

	// Find and return the number of sprites in the SS
	celsPtr = (uint32 *)((intptr)*(_GWS(globalCELSHandles)[hash]) + (uint32)(_GWS(globalCELSoffsets)[hash]));
	return FROM_LE_32(celsPtr[CELS_COUNT]);
}


int32 GetWSAssetCELFrameRate(uint32 hash) {
	uint32 *celsPtr;

	// Ensure the WS loader has been initialized.
	if (!_GWS(wsloaderInitialized)) {
		ws_LogErrorMsg(FL, "WS loader has not been initialized.");
		return -1;
	}

	// Verify the hash is valid, and a SS for that hash has been loaded
	if (hash > MAX_ASSET_HASH) {
		ws_LogErrorMsg(FL, "Series number out of range: requested num: %d", hash);
		return -1;
	}

	// Make sure the series is still in memory
	if ((!_GWS(globalCELSHandles)[hash]) || (!*_GWS(globalCELSHandles)[hash])) {
		ws_LogErrorMsg(FL, "Series not in memory series num: %d", hash);
		return -1;
	}

	// Find and return the frame rate for the SS
	celsPtr = (uint32 *)((intptr)*(_GWS(globalCELSHandles)[hash]) + (uint32)(_GWS(globalCELSoffsets)[hash]));
	return FROM_LE_32(celsPtr[CELS_FRAME_RATE]);
}


int32 GetWSAssetCELPixSpeed(uint32 hash) {
	uint32 *celsPtr;

	// Ensure the WS loader has been initialized.
	if (!_GWS(wsloaderInitialized)) {
		ws_LogErrorMsg(FL, "WS loader has not been initialized.");
		return -1;
	}

	// Verify the hash is valid, and a SS for that hash has been loaded
	if (hash > MAX_ASSET_HASH) {
		ws_LogErrorMsg(FL, "Series number out of range: requested num: %d", hash);
		return -1;
	}

	// Make sure the series is still in memory
	if ((!_GWS(globalCELSHandles)[hash]) || (!*_GWS(globalCELSHandles)[hash])) {
		ws_LogErrorMsg(FL, "Series not in memory series num: %d", hash);
		return -1;
	}

	// Find and return the pix speed for the SS
	celsPtr = (uint32 *)((intptr)*(_GWS(globalCELSHandles)[hash]) + (uint32)(_GWS(globalCELSoffsets)[hash]));
	return FROM_LE_32(celsPtr[CELS_PIX_SPEED]);
}

int32 ws_get_sprite_width(uint32 hash, int32 index) {
	uint32 *celsPtr, *offsets, *data, *myCelSource;
	int32 numCels;

	// Ensure the WS loader has been initialized.
	if (!_GWS(wsloaderInitialized)) {
		ws_LogErrorMsg(FL, "WS loader has not been initialized.");
		return -1;
	}

	// Verify the hash is valid, and a SS for that hash has been loaded
	if (hash > MAX_ASSET_HASH) {
		ws_LogErrorMsg(FL, "Series number out of range: requested num: %d", hash);
		return -1;
	}

	// Make sure the series is still in memory
	if ((!_GWS(globalCELSHandles)[hash]) || (!*_GWS(globalCELSHandles)[hash])) {
		ws_LogErrorMsg(FL, "Series not in memory series num: %d", hash);
		return -1;
	}

	// Find the source for the SS
	celsPtr = (uint32 *)((intptr)*(_GWS(globalCELSHandles)[hash]) + (uint32)(_GWS(globalCELSoffsets)[hash]));

	// Check that the index into the series requested is within a valid range
	numCels = FROM_LE_32(celsPtr[CELS_COUNT]);
	if (index >= numCels) {
		ws_LogErrorMsg(FL, "ws_get_sprite_width: Sprite index out of range - max index: %d, requested index: %d, hash: %d",
			numCels - 1, index, hash);
		return -1;
	}

	// Find the offset table in the SS header
	offsets = &celsPtr[CELS_OFFSETS];

	// Find the beginning of the data for all sprites in the SS
	data = &celsPtr[CELS_OFFSETS + numCels];

	// Find the sprite data for the specific sprite in the series
	myCelSource = (uint32 *)((intptr)data + FROM_LE_32(offsets[index]));

	return FROM_LE_32(myCelSource[CELS_W]);
}

int32 ws_get_sprite_height(uint32 hash, int32 index) {
	uint32 *celsPtr, *offsets, *data, *myCelSource;
	int32 numCels;

	// Ensure the WS loader has been initialized.
	if (!_GWS(wsloaderInitialized)) {
		ws_LogErrorMsg(FL, "WS loader has not been initialized.");
		return -1;
	}

	// Verify the hash is valid, and a SS for that hash has been loaded
	if (hash > MAX_ASSET_HASH) {
		ws_LogErrorMsg(FL, "Series number out of range: requested num: %d", hash);
		return -1;
	}

	// Make sure the series is still in memory
	if ((!_GWS(globalCELSHandles)[hash]) || (!*_GWS(globalCELSHandles)[hash])) {
		ws_LogErrorMsg(FL, "Series not in memory series num: %d", hash);
		return -1;
	}

	// Find the source for the SS
	celsPtr = (uint32 *)((intptr)*(_GWS(globalCELSHandles)[hash]) + (uint32)(_GWS(globalCELSoffsets)[hash]));

	// Check that the index into the series requested is within a valid range
	numCels = FROM_LE_32(celsPtr[CELS_COUNT]);
	if (index >= numCels) {
		ws_LogErrorMsg(FL, "ws_get_sprite_height: Sprite index out of range - max index: %d, requested index: %d, hash: %d",
			numCels - 1, index, hash);
		return -1;
	}

	// Find the offset table in the SS header
	offsets = &celsPtr[CELS_OFFSETS];

	// Find the beginning of the data for all sprites in the SS
	data = &celsPtr[CELS_OFFSETS + numCels];

	// Find the sprite data for the specific sprite in the series
	myCelSource = (uint32 *)((intptr)data + FROM_LE_32(offsets[index]));

	return FROM_LE_32(myCelSource[CELS_H]);
}

MemHandle ws_GetSEQU(uint32 hash, int32 *numLocalVars, int32 *offset) {
	uint32 *sequPtr;

	// Ensure the WS loader has been initialized.
	if (!_GWS(wsloaderInitialized)) {
		ws_LogErrorMsg(FL, "WS loader has not been initialized.");
		return nullptr;
	}

	// Verify the hash is valid, and a SEQU for that hash has been loaded
	if (hash > MAX_ASSET_HASH) {
		ws_LogErrorMsg(FL, "SEQU number out of range: requested num: %d", hash);
		return nullptr;
	}

	// Make sure the SEQU is still in memory
	if ((!_GWS(globalSEQUHandles)[hash]) || (!*_GWS(globalSEQUHandles)[hash])) {
		ws_LogErrorMsg(FL, "SEQU not in memory: sequence num: %d", hash);
		return nullptr;
	}

	// Find the sequence chunk
	sequPtr = (uint32 *)((intptr)*(_GWS(globalSEQUHandles)[hash]) + (uint32)(_GWS(globalSEQUoffsets)[hash]));

	// Return the offset into the resource chunk, and the number of local vars used by the sequence
	*offset = (intptr)(&sequPtr[SEQU_SEQU_START]) - (intptr)*(_GWS(globalSEQUHandles)[hash]);
	*numLocalVars = FROM_LE_32(sequPtr[SEQU_NUM_VARS]);

	// Return the resource handle
	return _GWS(globalSEQUHandles)[hash];
}

MemHandle ws_GetMACH(uint32 hash, int32 *numStates, int32 *stateTableOffset, int32 *machInstrOffset) {
	uint32 *machPtr;

	// Ensure the WS loader has been initialized.
	if (!_GWS(wsloaderInitialized)) {
		ws_LogErrorMsg(FL, "WS loader has not been initialized.");
		return nullptr;
	}

	// Verify the hash is valid, and a MACH for that hash has been loaded
	if (hash > MAX_ASSET_HASH) {
		ws_LogErrorMsg(FL, "MACH number out of range: requested num: %d", hash);
		return nullptr;
	}

	// Make sure the MACH is still in memory
	if ((!_GWS(globalMACHHandles)[hash]) || (!*_GWS(globalMACHHandles)[hash])) {
		ws_LogErrorMsg(FL, "MACH not in memory: machine num: %d", hash);
		return nullptr;
	}

	// Lock the handle
	HLock(_GWS(globalMACHHandles)[hash]);

	// Find the machine chunk
	machPtr = (uint32 *)((intptr)*(_GWS(globalMACHHandles)[hash]) + (uint32)(_GWS(globalMACHoffsets)[hash]));

	// Set the number of states, the state offset table, the start of the mach instructions
	*numStates = FROM_LE_32(machPtr[MACH_NUM_STATES]);
	*stateTableOffset = (intptr)(&machPtr[MACH_OFFSETS]) - (intptr)(*_GWS(globalMACHHandles)[hash]);
	*machInstrOffset = ((intptr)machPtr + ((*numStates + 1) << 2)) - (intptr)(*_GWS(globalMACHHandles)[hash]);

	// Unlock and return the handle
	HUnLock(_GWS(globalMACHHandles)[hash]);
	return _GWS(globalMACHHandles)[hash];
}

MemHandle ws_GetDATA(uint32 hash, uint32 index, int32 *rowOffset) {
	uint32 *dataPtr;

	// Ensure the WS loader has been initialized.
	if (!_GWS(wsloaderInitialized)) {
		ws_LogErrorMsg(FL, "WS loader has not been initialized.");
		return nullptr;
	}

	// Verify the hash is valid, and a SS for that hash has been loaded
	if (hash > MAX_ASSET_HASH) {
		ws_LogErrorMsg(FL, "DATA number out of range: requested num: %d", hash);
		return nullptr;
	}

	// Make sure the DATA is still in memory
	if ((!_GWS(globalDATAHandles)[hash]) || (!*_GWS(globalDATAHandles)[hash])) {
		ws_LogErrorMsg(FL, "DATA not in memory: data num: %d", hash);
		return nullptr;
	}

	// Find the data block chunk
	dataPtr = (uint32 *)((intptr)*(_GWS(globalDATAHandles)[hash]) + (uint32)(_GWS(globalDATAoffsets)[hash]));

	// Verify the row index of the data block is valid
	if (index > FROM_LE_32(dataPtr[DATA_REC_COUNT])) {
		term_message("File: %s, line: %d, ws_GetDATA() failed:", FL);
		term_message("Data block num: %d", hash);
		term_message("Data row out of range - max row index: %d, requested row index: %d",
			FROM_LE_32(dataPtr[DATA_REC_COUNT]), index);
		return nullptr;
	}

	*rowOffset = (int32)((intptr)(&dataPtr[DATA_REC_START]) +
		((index * FROM_LE_32(dataPtr[DATA_REC_SIZE])) << 2)
		- (intptr)(*_GWS(globalDATAHandles)[hash]));
	// Return the data handle
	return _GWS(globalDATAHandles)[hash];
}

int32 ws_GetDATACount(uint32 hash) {
	uint32 *dataPtr;

	// Ensure the WS loader has been initialized.
	if (!_GWS(wsloaderInitialized)) {
		ws_LogErrorMsg(FL, "WS loader has not been initialized.");
		return -1;
	}

	// Verify the hash is valid, and a SS for that hash has been loaded
	if (hash > MAX_ASSET_HASH) {
		ws_LogErrorMsg(FL, "DATA number out of range: requested num: %d", hash);
		return -1;
	}

	// Make sure the DATA is still in memory
	if ((!_GWS(globalDATAHandles)[hash]) || (!*_GWS(globalDATAHandles)[hash])) {
		ws_LogErrorMsg(FL, "DATA not in memory: data num: %d", hash);
		return -1;
	}

	// Find the data block chunk
	dataPtr = (uint32 *)((intptr)*(_GWS(globalDATAHandles)[hash]) + (uint32)(_GWS(globalDATAoffsets)[hash]));

	// Return the number of rows in the data block
	return FROM_LE_32(dataPtr[DATA_REC_COUNT]);
}

static int32 GetSSHeaderInfo(SysFile *sysFile, uint32 **data, RGB8 *myPalette) {
	uint32 celsType, celsSize, numColors, *myColors;
	uint32 *tempPtr, i, j, header, format;
	int32 numCels, dataOffset;
	bool byteSwap;

	if (!sysFile) {
		ws_LogErrorMsg(FL, "nullptr FILE POINTER given.");
		return -1;
	}

	// Read in the series header and the format number
	header = sysFile->readUint32LE();
	format = sysFile->readUint32LE();

	// Make sure the header is "M4SS", and that the format is not antique
	if (header == HEAD_SS4M) {
		format = SWAP_INT32(format);
	} else if (header != HEAD_M4SS) {
		ws_LogErrorMsg(FL, "Series is not a valid M4SS series.");
		return -1;
	}
	if (format < SS_FORMAT) {
		ws_LogErrorMsg(FL, "Format is antique and cannot be read - rebuild series.");
		return -1;
	}

	// Read in the SS chunk type - either PAL or SS info
	celsType = sysFile->readUint32LE();

	if ((celsType == CELS__PAL) || (celsType == CELS_LAP_)) {
		// PAL info, read in the size of the PAL chunk
		celsSize = sysFile->readUint32LE();

		// Now read in the number of colors to be inserted into the PAL
		numColors = sysFile->readUint32LE();

		// Make sure the info is in the correct format (swap between Motorola and Intel formats)
		if (celsType == CELS_LAP_) {
			celsSize = SWAP_INT32(celsSize);
			numColors = SWAP_INT32(numColors);
			byteSwap = true;
		} else {
			byteSwap = false;
		}

		// If there is at least one color specified in this block
		if (numColors > 0) {
			if ((myColors = (uint32 *)mem_alloc(celsSize - 12, "ss pal info")) == nullptr) {
				ws_LogErrorMsg(FL, "Failed to mem_alloc() %d bytes.", celsSize - 12);
				return -1;
			}

			// Read in the color info into a temp buffer
			for (i = 0; i < numColors; ++i)
				myColors[i] = sysFile->readUint32LE();


			// If the chunk is in the wrong format, byte-swap the entire chunk
			// note: we do this because we want the data stored in nrgb format
			// The data is always read in low byte first, but we need it high byte first
			// regardless of the endianness of the machine.
			if (byteSwap) {
				tempPtr = (uint32 *)&myColors[0];
				for (i = 0; i < numColors; i++) {
					*tempPtr = SWAP_INT32(*tempPtr);
					tempPtr++;
				}
			}

			// If we have a place to store the color info
			if (myPalette) {
				tempPtr = (uint32 *)(&myColors[0]);
				for (i = 0; i < numColors; i++) {
					j = (*tempPtr & 0xff000000) >> 24;
					myPalette[j].r = (*tempPtr & 0x00ff0000) >> 14;
					myPalette[j].g = (*tempPtr & 0x0000ff00) >> 6;
					myPalette[j].b = (*tempPtr & 0x000000ff) << 2;
					tempPtr++;
				}
			}

			// Turf the temp buffer
			mem_free((void *)myColors);
		}

		// Read in the next chunk type
		celsType = sysFile->readUint32LE();
	}

	// Make sure the chunk type is Sprite Series info
	if ((celsType != CELS___SS) && (celsType != CELS_SS__)) {
		ws_LogErrorMsg(FL, "Series chunk type is not labelled as SS info.");
		return -1;
	}

	// Read in the size of the entire chunk
	celsSize = sysFile->readUint32LE();

	// If the chunk is the wrong format, byte-swap (between motorola and intel formats)
	if (celsType == CELS_SS__) {
		celsSize = SWAP_INT32(celsSize);
	}

	// *data contains header + offsets, therefore, we must scan ahead
	// and find out how many cels are here...
	if (!(*sysFile).seek_ahead((CELS_COUNT - CELS_SRC_SIZE - 1) << 2)) {
		ws_LogErrorMsg(FL, "Failed to seek ahead in the stream.");
		return -1;
	}

	// Read how many sprites are in the series
	numCels = sysFile->readUint32LE();

	// Again, byte-swap if the chunk is in the wrong format
	if (celsType == CELS_SS__) {
		numCels = SWAP_INT32(numCels);
	}

	// Now, seek backwards to where we left off
	if (!(*sysFile).seek_ahead((CELS_SRC_SIZE - CELS_COUNT) * 4)) {
		ws_LogErrorMsg(FL, "Failed to seek backwards in the stream.");
		return -1;
	}

	// Allocate a block to hold both the series header, and the sprite offset table
	if ((*data = (uint32 *)mem_alloc((SS_HEAD_SIZE + numCels) * 4, "ss header")) == nullptr) {
		ws_LogErrorMsg(FL, "Failed to mem_alloc() %d bytes.", (SS_HEAD_SIZE + numCels) << 2);
		return -1;
	}

	// Read in the series header and the sprite offset table
	// Since we already read in celsType and celsSize, SS_HEAD_SIZE-2
	for (i = 0; i < SS_HEAD_SIZE + (uint)numCels - 2; ++i)
		(*data)[2 + i] = sysFile->readUint32LE();

	// Set the celsType and the celsSize
	(*data)[0] = celsType;
	(*data)[1] = celsSize;

	// If the chunk is in the wrong format, byte-swap the series header
	if (celsType == CELS_SS__) {
		tempPtr = &((*data)[2]);
		for (i = 0; i < (uint)(SS_HEAD_SIZE + numCels - 2); i++) {
			*tempPtr = SWAP_INT32(*tempPtr);
			tempPtr++;
		}
	}

	// Find out how far into the stream we are, and return that value
	dataOffset = (*sysFile).get_pos();
	return dataOffset;
}

bool ws_OpenSSstream(SysFile *sysFile, Anim8 *anim8) {
	CCB *myCCB;
	frac16 *myRegs;
	uint32 *celsPtr, *offsets;
	int32 ssDataOffset, i, numSprites;
	int32 obesest_frame = 0;
	uint32 maxFrameSize;

	// Verify the parameters
	if (!sysFile || !anim8 || !anim8->myCCB) {
		ws_LogErrorMsg(FL, "SysFile* streamFile invalid.");
		return false;
	}

	myCCB = anim8->myCCB;
	myRegs = anim8->myRegs;
	ssDataOffset = 0;

	// Read in the SS stream header
	if ((ssDataOffset = GetSSHeaderInfo(sysFile, &(myCCB->streamSSHeader), &_G(master_palette)[0])) <= 0) {
		return false;
	}

	// Automatically set some of the sequence registers
	celsPtr = myCCB->streamSSHeader;
	numSprites = celsPtr[CELS_COUNT];
	myRegs[IDX_CELS_INDEX] = -(1 << 16);	// First frame inc will make it 0
	myRegs[IDX_CELS_COUNT] = numSprites << 16;
	myRegs[IDX_CELS_FRAME_RATE] = celsPtr[CELS_FRAME_RATE] << 16;

	// Here we convert the offset table to become the actual size of the data for each sprite
	// This is so the stream can be optimized to always read in on sprite boundaries
	// Get the beginning of the offset table
	offsets = &celsPtr[CELS_OFFSETS];

	maxFrameSize = 0;
	// For all but the last frame, the frame size is the difference in offset values
	for (i = 0; i < numSprites - 1; i++) {
		offsets[i] = offsets[i + 1] - offsets[i];

		if (offsets[i] > maxFrameSize) {
			maxFrameSize = offsets[i];
			obesest_frame = i;
		}
	}

	// For the last sprite we take the entire chunk size - the chunk header - the offset for that sprite
	offsets[numSprites - 1] = celsPtr[CELS_SRC_SIZE] -
		((SS_HEAD_SIZE + celsPtr[CELS_COUNT]) << 2) -
		offsets[numSprites - 1];

	if (offsets[numSprites - 1] > maxFrameSize) {
		maxFrameSize = offsets[numSprites - 1];
		obesest_frame = numSprites - 1;
	}

	// Calculate the maximum size a sprite could be
	maxFrameSize += SS_INDV_HEAD << 2;

	if (!myCCB->source) {
		myCCB->source = (M4sprite *)mem_alloc(sizeof(M4sprite), "Sprite");
		if (!myCCB->source) {
			ws_LogErrorMsg(FL, "Failed to mem_alloc() %d bytes.", sizeof(M4sprite));
			return false;
		}
	}

	term_message("Biggest frame was: %d, size: %d bytes (compressed)", obesest_frame, maxFrameSize);

	// Access the streamer to recognize the new client
	if ((myCCB->myStream = (void *)f_stream_Open(sysFile, ssDataOffset, maxFrameSize, maxFrameSize << 4, numSprites, (int32 *)offsets, 4, false)) == nullptr) {
		ws_LogErrorMsg(FL, "Failed to open a stream.");
		return false;
	}

	// Tag the CCB as being streamed
	myCCB->flags |= CCB_DISC_STREAM;
	myCCB->seriesName = nullptr;

	// Get the first frame
	if (!ws_GetNextSSstreamCel(anim8)) {
		ws_LogErrorMsg(FL, "Failed to get the first stream frame.");
		return false;
	}

	return true;
}

bool ws_GetNextSSstreamCel(Anim8 *anim8) {
	CCB *myCCB;
	M4sprite *mySprite;
	uint32 *celsPtr, *offsets, *myCelSource;
	uint32 frameNum;

	// Verify the parameters
	if (!anim8) {
		ws_LogErrorMsg(FL, "nullptr Anim8* given");
		return false;
	}

	myCCB = anim8->myCCB;
	if ((!anim8->myCCB) || (!myCCB->streamSSHeader) || (!myCCB->myStream)) {
		ws_LogErrorMsg(FL, "Invalid Anim8* given.");
		return false;
	}
	if (!(myCCB->flags & CCB_DISC_STREAM)) {
		ws_LogErrorMsg(FL, "Anim8* given has not previously opened a stream");
		return false;
	}

	// Find the SS source and the offset table into the source
	celsPtr = myCCB->streamSSHeader;
	offsets = &celsPtr[CELS_OFFSETS];

	// Automatically increment the sequence register
	anim8->myRegs[IDX_CELS_INDEX] += 0x10000;

	// Check whether the end of the SS has been streamed
	frameNum = anim8->myRegs[IDX_CELS_INDEX] >> 16;
	if (frameNum >= celsPtr[CELS_COUNT]) {
		ws_LogErrorMsg(FL, "No more frames available to stream");
		return false;
	}

	// Read the next sprite from the stream.  Note the offset table was converted to absolute size when the stream was opened.
	if (f_stream_Read((strmRequest *)myCCB->myStream, (uint8 **)(&myCCB->streamSpriteSource), offsets[frameNum])
			< (int32)offsets[frameNum]) {
		ws_LogErrorMsg(FL, "Unable to read the next stream frame");
		return false;
	}

	// Flag the CCB if the sprite series is a delta-streaming sprite series
	if (myCCB->streamSpriteSource[CELS_STREAM]) {
		myCCB->flags |= CCB_STREAM;
	}

	// Initialize the sprite structure
	myCelSource = myCCB->streamSpriteSource;
	mySprite = myCCB->source;

	mySprite->xOffset = FROM_LE_32(myCelSource[CELS_X]);
	mySprite->yOffset = FROM_LE_32(myCelSource[CELS_Y]);
	mySprite->w = FROM_LE_32(myCelSource[CELS_W]);
	mySprite->h = FROM_LE_32(myCelSource[CELS_H]);

	mySprite->encoding = (uint8)FROM_LE_32(myCelSource[CELS_COMP]);

	mySprite->data = (uint8 *)&myCelSource[CELS_DATA];

	// Initialize the CCB structure
	myCCB->newLocation->x1 = 0;
	myCCB->newLocation->y1 = 0;
	myCCB->newLocation->x2 = 0;
	myCCB->newLocation->y2 = 0;
	myCCB->scaleX = 0;
	myCCB->scaleY = 0;

	return true;
}

void ws_CloseSSstream(CCB *myCCB) {
	// Verify the parameters
	if ((!myCCB) || (!(myCCB->flags & CCB_DISC_STREAM))) {
		ws_LogErrorMsg(FL, "Invalid CCB* given.");
		return;
	}

	// Remove the CCB_DISC_STREAM flag
	myCCB->flags &= ~CCB_DISC_STREAM;

	// Free up the CCB pointers which store streaming information
	if (myCCB->streamSSHeader) {
		mem_free((char *)myCCB->streamSSHeader);
	}

	// Close the stream
	if (myCCB->myStream) {
		f_stream_Close((strmRequest *)myCCB->myStream);
		myCCB->myStream = nullptr;
	}
}

} // End of namespace M4