File: OsirisLoadandBind.cpp

package info (click to toggle)
descent3 1.5.0%2Bds-1
  • links: PTS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 35,204 kB
  • sloc: cpp: 416,147; ansic: 3,233; sh: 10; makefile: 8
file content (4038 lines) | stat: -rw-r--r-- 132,507 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
/*
* Descent 3 
* Copyright (C) 2024 Parallax Software
*
* 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/>.

--- HISTORICAL COMMENTS FOLLOW ---

 * $Logfile: /DescentIII/main/OsirisLoadandBind.cpp $
 * $Revision: 116 $
 * $Date: 4/04/00 12:40p $
 * $Author: Matt $
 *
 * OSIRIS functions to load a script DLL and bind to objects
 *
 * $Log: /DescentIII/main/OsirisLoadandBind.cpp $
 *
 * 116   4/04/00 12:40p Matt
 * Fixed mem leak in debugging code
 *
 * 115   10/25/99 11:57a Jeff
 * ifdef'd some Macintosh pragmas
 *
 * 114   10/22/99 11:33a Matt
 * Mac merge
 *
 * 113   9/29/99 8:04a Kevin
 * Demo playback fixes
 *
 * 112   5/19/99 3:54p Jeff
 * error is scripts are being bound to an object more than once
 *
 * 111   5/19/99 11:26a Matt
 * Added multisafe functions & Dallas actions for showing a timer on the
 * screen and adding custom HUD messages.
 *
 * 110   5/12/99 2:24p Jeff
 * Descent3 now has a setable temp directory for all temp files
 *
 * 109   5/10/99 11:55a 3dsmax
 * removed an int3
 *
 * 108   5/08/99 6:45p Chris
 * safety precausion to unload modules if they are not already
 *
 * 107   5/08/99 4:31a Jeff
 * don't int3 on multiplayer modules
 *
 * 106   5/07/99 8:27p Jeff
 * trying to hunt down why some modules hang around after they are done
 *
 * 105   5/02/99 6:41a Jeff
 * created 'test/temp' function to force unload all osiris modules
 *
 * 104   5/01/99 1:40a Jeff
 * put in hack to keep levelscripts from even being attempted to be called
 * by a client during disconnect
 *
 * 103   4/24/99 12:09a Jeff
 * added path value functions
 *
 * 102   4/20/99 8:56p Chris
 * Fixed problem with robots not being able to open locked doors that a
 * player has the key for.
 *
 * 101   4/20/99 12:59p Jeff
 * fixed loading script bug
 *
 * 100   4/20/99 12:02a Jeff
 * made linux filename friendly
 *
 * 99    4/16/99 12:19a Jeff
 * linux wants stdcall modifiers before parens, unlike windows
 *
 * 98    4/14/99 4:19a Jeff
 * more case mismatch fixes in #includes
 *
 * 97    4/14/99 3:57a Jeff
 * fixed case mismatch in #includes
 *
 * 96    3/17/99 11:48a Jeff
 * exported function to get what language we are using
 *
 * 95    3/11/99 9:10p Jeff
 * int3 only when restore system state if not playing back demo (because
 * scripts are not really used in demos)
 *
 * 94    3/10/99 6:44p Jeff
 * fixed exported game cinematic function for canned cinematics, so it's
 * prototype doesn't change (oem patch friendly)
 *
 * 93    3/10/99 6:21p Jeff
 * many fixes to demo system.  Fixed IGC so cameras move...fixed osiris to
 * be restored correctly, and it handles errors on restore
 *
 * 92    3/09/99 12:34p Jeff
 * extract scripts to custom\cache
 *
 * 91    3/05/99 5:25p Jeff
 * fixed saving demos in multiplayer...required adding a parameter to
 * Osiris_LoadMissionModule() to pass in the name of the d3m file (for
 * storage)
 *
 * 90    3/03/99 5:13a Jeff
 * fix bug loading dlls from hog files (wouldn't load..comparing something
 * like aigame to aigame.dll)
 *
 * 89    3/03/99 3:01a Chris
 * Exported the difficulty stuff to OSIRIS
 *
 * 88    3/03/99 12:12a Chris
 *
 * 87    3/03/99 12:07a Chris
 * Added the two new OSIRIS predefs for AI path finding
 *
 * 86    3/01/99 3:45p Jeff
 * fixed up save and restore a little bit
 *
 * 85    3/01/99 11:50a Jeff
 * call EVT_SAVESTATE and EVT_RESTORESTATE for levels
 *
 * 84    2/28/99 11:30p Chris
 * FindObjOfType and OSIRIS controllable deaths
 *
 * 83    2/28/99 8:52p Jeff
 * added functions to enable/disable creation events
 *
 * 82    2/23/99 12:54p Matt
 * Removed Int3() (Jeff on MattT's machine)
 *
 * 81    2/22/99 8:17p Chris
 * Added the LGoal_Value() function to OSIRIS
 *
 * 80    2/21/99 8:36p Jeff
 * misc changes to handle new matcen and path types of dallas
 *
 * 79    2/21/99 8:04p Jeff
 * better handling of out-of-sync scripts
 *
 * 78    2/21/99 5:51p Chris
 * FIxed a bug with OBJV_C_TYPE (It should have been OBJV_I_TYPE).  Added
 * Obj_FindType()
 *
 * 77    2/18/99 4:28p Jeff
 * added lookup functions for matcen, paths, level goals
 *
 * 76    2/18/99 11:06a Jeff
 * added event masks (so you can enable/disable object/trigger/level
 * events)
 *
 * 75    2/17/99 3:27a Jeff
 * added game checksum function to handle out-of-sync dlls
 *
 * 74    2/15/99 11:38a Jeff
 * debug mprintfs when extracting dlls
 *
 * 73    2/14/99 1:16a Jeff
 * exported canned cinematic
 *
 * 72    2/13/99 6:11p Jeff
 * fixed bug when extracting scripts for the second+ time
 *
 * 71    2/12/99 6:24p Kevin
 *
 * 70    2/12/99 6:22p Kevin
 * Fixed a thingy for Jeffy
 *
 * 69    2/11/99 2:56a Jeff
 * improvements to introcam
 *
 * 68    2/10/99 7:12p Jeff
 * oops...OpenLibrary returns 0 not -1
 *
 * 67    2/10/99 6:56p Jeff
 * handle invalid libraries when extracting scripts
 *
 * 66    2/10/99 3:41p Jeff
 * removed unneeded assert
 *
 * 65    2/10/99 3:29p Jeff
 * extracted dll manager knows the difference between game hogs and
 * mission hogs
 *
 * 64    2/10/99 1:47p Matt
 * Changed object handle symbolic constants
 *
 * 63    2/08/99 2:51p Jeff
 * removed dead evt_ain events that were commented out
 *
 * 62    2/06/99 7:05p Jeff
 * turned off osiris debug messages by default
 *
 * 61    2/05/99 9:06p Jeff
 * added aux events for a couple new ainotify's
 *
 * 60    2/03/99 1:51a Jeff
 * added predefs for ship permissions
 *
 * 59    2/03/99 12:43a Chris
 * Added Obj_GetGroundPos
 *
 * 58    2/02/99 12:46p Jeff
 * handy mprintfs on save/restore state
 *
 * 57    2/01/99 5:29p Jeff
 * obj_dummy's can be assigned scripts
 *
 * 56    1/31/99 10:40p Jeff
 * exported Osiris_CancelTimerID to DLLs
 *
 * 55    1/31/99 10:16p Matt
 * Added Osiris_CancelTimerID()
 *
 * 54    1/31/99 8:52p Jeff
 * new in game cinematics system finished
 *
 * 51    1/25/99 8:18a Chris
 * Made the AUX Notify remap more obvious
 *
 * 50    1/25/99 8:13a Chris
 * Made the AUX goal remap more obvious...
 *
 * 49    1/24/99 8:18p Chris
 * Updated AI and OSIRIS.  Externalized fireball constants for spew and
 * sparks.  Added CreateRandomSparks to OSIRIS, renamed a bunch of AI
 * Notify names to use underscores.  Fixed a memory access leak in the
 * matcen effect code.
 *
 * 48    1/24/99 6:36p Jeff
 * fixed trigger call event bug (wasn't using the correct DLLid)
 *
 * 47    1/22/99 8:54p Jeff
 * added custom-default script overrides
 *
 * 46    1/22/99 6:53p Chris
 * Fixed LoadandBind Aux notify problems, fixed static path problems,
 * fixed AIF_FORCE_AWARE Problems, improved path code
 *
 * 45    1/22/99 5:15p Jeff
 * added a key to toggle osiris debug messages
 *
 * 44    1/22/99 4:47p Jeff
 *
 * 43    1/21/99 11:15p Jeff
 * pulled out some structs and defines from header files and moved them
 * into seperate header files so that multiplayer dlls don't require major
 * game headers, just those new headers.  Side effect is a shorter build
 * time.  Also cleaned up some header file #includes that weren't needed.
 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
 * manage.h and multi.h
 *
 * 42    1/20/99 4:17p Jeff
 * added ai notify child events and predefs to get ids of sound, room,
 * trigger, object names
 *
 * 41    1/20/99 3:46a Jeff
 * exported boss introduction functions
 *
 * 39    1/16/99 2:19p Jeff
 * only save and use the base filename of a dll (to keep it platform
 * independent)
 *
 * 38    1/16/99 1:29p Jeff
 * updated the OMMS system to be faster.  Removed the event for restoring
 * it, since it is require to surround code in attach/detach blocks.
 *
 * 37    1/16/99 10:39a Jeff
 * added mission memory management to Osiris...only slightly tested.  Need
 * to solve game save/restore problem still
 *
 * 36    1/15/99 5:59p Chris
 *
 * 35    1/13/99 5:20p Jeff
 * added 4 new file operation predefs
 *
 * 34    1/13/99 4:05p Jeff
 * when loading a script dll, a string table is looked for and
 * automatically loaded and passed to the newly loaded dll on init for
 * use.
 *
 * 33    1/13/99 3:25a Chris
 * Added Obj_Burning and Obj_IsEffect to OSIRIS
 *
 * 32    1/13/99 2:29a Chris
 * Massive AI, OSIRIS update
 *
 * 31    1/11/99 2:14p Chris
 * Massive work on OSIRIS and AI
 *
 * 30    1/11/99 12:53p Jeff
 * added a function that given a module name it will make sure it has an
 * extension.  Made Osiris friendly with modules with no extension
 *
 * 29    1/08/99 2:02p Jeff
 * handle the case of extracting too many scripts
 *
 * 28    1/08/99 2:00p Jeff
 * fixed minor bug with hog extraction (mem leak) and made it so you can
 * keep calling ExtractScripts for multiple hogs
 *
 * 27    1/08/99 1:50p Jeff
 * finished support on loading scripts from hogs
 *
 * 26    1/07/99 10:52p Jeff
 * wrote skeleton of hog script file extraction
 *
 * 25    1/04/99 6:43p Jeff
 * added support to get/set mission flag values
 *
 * 24    1/04/99 12:42p Chris
 * Updated OSIRIS with "xxxx_external.h" files and Obj_Value, and
 * Matcen_XXX functions
 *
 * 23    1/04/99 12:23p Jeff
 * added to evt_use and support for mission module scripts
 *
 * 22    12/31/98 7:33p Jeff
 * improved Osiris timers to have unique IDs, along with adding an
 * EVT_TIMERCANCEL and a new flag to autodestruct the timer if a given
 * object handle dies.
 *
 * 21    12/31/98 2:37p Jeff
 * added SaveRestoreState import function
 *
 * 20    12/30/98 5:17p Jeff
 * changes made to handle script name override, to override the name of
 * the script to use in a module.
 *
 * 19    12/30/98 3:46p Chris
 * Incremental AI changes
 *
 * 18    12/30/98 12:31p Jeff
 * when freeing object scripts, the type is changed before it's freed in
 * ObjDelete.  Also, when destroyed, OBJ_ROBOT becomes OBJ_DEBRIS and
 * Osiris was not handling this.
 *
 * 17    12/23/98 12:10p Chris
 * Added Gametime and Frametime to OSIRIS
 *
 * 16    12/22/98 4:51p Chris
 * Added a ObjCreate predefined function
 *
 * 15    12/22/98 4:19p Jeff
 * exported msafe_DoPowerup
 *
 * 14    12/22/98 2:46p Chris
 * Added the AIValue predefined function
 *
 * 13    12/18/98 6:00p Jeff
 * added support for door scripts in new osiris
 *
 * 12    12/18/98 3:10p Mark
 * removed int3
 *
 * 11    12/18/98 11:07a Jeff
 * triggers are implemented
 *
 * 10    12/18/98 10:42a Jeff
 * added timer support and auto-save memory manager for new osiris.  New
 * events evt_memrestore state added also
 *
 * 9     12/17/98 7:26p Jeff
 * added memory manager for autosaving script data
 *
 * 8     12/17/98 5:44p Jeff
 * created timer system for osiris and save restore events
 *
 * 7     12/17/98 2:27p Keneta
 * Commented out an Int3(), as per Jeff's directions.  (MattT)
 *
 * 6     12/17/98 12:08p Jeff
 * first checkin of new implementation of OSIRIS (old OSIRIS no longer
 * works)
 *
 * 5     12/16/98 10:21p Jeff
 * added level and trigger event call functions.  added trigger script
 * functionality (still primative)
 *
 * 4     12/16/98 5:37p Jason
 * added new multisafe architecture
 *
 * 3     12/16/98 10:58a Jeff
 * checked in so Jason can use
 *
 * 2     12/14/98 11:32a Jeff
 * started work on osiris load and bind functions
 *
 * $NoKeywords: $
 */

#include "osiris_dll.h"
#include "pserror.h"
#include "mono.h"
#include "cfile.h"
#include "ddio.h"
#include "manage.h"
#include <stdlib.h>
#include "mem.h"
#include "DllWrappers.h"
#include "objinfo.h"
#include "multisafe.h"
#include "osiris_predefs.h"
#include "trigger.h"
#include "game.h"
#include "multi.h"
#include "door.h"
#include "localization.h"
#include "player.h"
#include "gamecinematics.h"
#include "demofile.h"

#ifdef _DEBUG
#define OSIRISDEBUG
#endif


bool Show_osiris_debug = false;

#define MAX_LOADED_MODULES 96 // maximum number of dlls that can be loaded at a time

#define OSIMF_INUSE 0x1        // slot in use
#define OSIMF_LEVEL 0x2        // level module
#define OSIMF_DLLELSEWHERE 0x4 // mission module
#define OSIMF_INTEMPDIR 0x8    // the dll was extracted from a hog and it is in a temp file
#define OSIMF_NOUNLOAD                                                                                                 \
  0x10 // the dll should not be unloaded if the reference count is
       // 0, only when the level ends

// The exported DLL function call prototypes
#if defined(POSIX)
typedef char DLLFUNCCALL (*InitializeDLL_fp)(tOSIRISModuleInit *function_list);
typedef void DLLFUNCCALL (*ShutdownDLL_fp)(void);
typedef int DLLFUNCCALL (*GetGOScriptID_fp)(const char *name, uint8_t isdoor);
typedef void DLLFUNCCALL *(*CreateInstance_fp)(int id);
typedef void DLLFUNCCALL (*DestroyInstance_fp)(int id, void *ptr);
typedef int16_t DLLFUNCCALL (*CallInstanceEvent_fp)(int id, void *ptr, int event, tOSIRISEventInfo *data);
typedef int DLLFUNCCALL (*GetTriggerScriptID_fp)(int trigger_room, int trigger_face);
typedef int DLLFUNCCALL (*GetCOScriptList_fp)(int **list, int **id_list);
typedef int DLLFUNCCALL (*SaveRestoreState_fp)(void *file_ptr, uint8_t saving_state);
#else
typedef char(DLLFUNCCALL *InitializeDLL_fp)(tOSIRISModuleInit *function_list);
typedef void(DLLFUNCCALL *ShutdownDLL_fp)(void);
typedef int(DLLFUNCCALL *GetGOScriptID_fp)(const char *name, uint8_t isdoor);
typedef void *(DLLFUNCCALL *CreateInstance_fp)(int id);
typedef void(DLLFUNCCALL *DestroyInstance_fp)(int id, void *ptr);
typedef int16_t(DLLFUNCCALL *CallInstanceEvent_fp)(int id, void *ptr, int event, tOSIRISEventInfo *data);
typedef int(DLLFUNCCALL *GetTriggerScriptID_fp)(int trigger_room, int trigger_face);
typedef int(DLLFUNCCALL *GetCOScriptList_fp)(int **list, int **id_list);
typedef int(DLLFUNCCALL *SaveRestoreState_fp)(void *file_ptr, uint8_t saving_state);
#endif

struct tRefObj {
  int objnum;
  int type, id;
  bool dummy;
  tRefObj *next;
};

struct tOSIRISModule {
  uint8_t flags;
  uint8_t extracted_id;
  uint16_t reference_count;
  InitializeDLL_fp InitializeDLL;
  ShutdownDLL_fp ShutdownDLL;
  GetGOScriptID_fp GetGOScriptID;
  CreateInstance_fp CreateInstance;
  DestroyInstance_fp DestroyInstance;
  CallInstanceEvent_fp CallInstanceEvent;
  GetTriggerScriptID_fp GetTriggerScriptID;
  GetCOScriptList_fp GetCOScriptList;
  SaveRestoreState_fp SaveRestoreState;
  module mod;
  char *module_name;
  char **string_table;
  int strings_loaded;

#ifdef OSIRISDEBUG
  tRefObj *RefRoot;
#endif
};

static tOSIRISModule OSIRIS_loaded_modules[MAX_LOADED_MODULES];
tOSIRISModuleInit Osiris_module_init;

struct {
  bool level_loaded;
  uint16_t num_customs;
  uint16_t dll_id;
  int *custom_ids;
  int *custom_handles;
  void *instance;
} tOSIRISCurrentLevel;

struct {
  bool mission_loaded;
  uint16_t dll_id;
} tOSIRISCurrentMission;

#define OESF_USED 0x0001
#define OESF_MISSION 0x0002 // mission dlls
struct tExtractedScriptInfo {
  uint8_t flags;
  char *temp_filename;
  char *real_filename;
};
static tExtractedScriptInfo OSIRIS_Extracted_scripts[MAX_LOADED_MODULES];
static char *OSIRIS_Extracted_script_dir = NULL;

//	Osiris_CreateModuleInitStruct
//	Purpose:
//		This function initializes a Module Init Struct with all the needed data to get sent
//	to the module during initialization.
extern void Osiris_CreateModuleInitStruct(tOSIRISModuleInit *mi);

static void Osiris_RestoreOMMS(CFILE *file);
static void Osiris_SaveOMMS(CFILE *file);

//	Osiris_CreateGameChecksum
//	Purpose:
//		Generates a checksum of the game's structures, to give to the modules
//	so they can use to compare to the time when they were compiled, to see
//	if they are compatible.
uint32_t Osiris_CreateGameChecksum(void);
//	Osiris_IsEventEnabled
//	Purpose:
//		Returns true if the event is allowed to be called
static bool Osiris_IsEventEnabled(int event);

extern void Cinematic_StartCannedScript(tCannedCinematicInfo *info);

//	Osiris_DumpLoadedObjects
//
//	Debug.  Dumps all loaded objects to given file
static void Osiris_DumpLoadedObjects(char *file);
static void Osiris_ForceUnloadModules(void);

uint32_t Osiris_game_checksum;

static uint8_t Osiris_event_mask = OEM_OBJECTS | OEM_TRIGGERS | OEM_LEVELS;
static bool Osiris_create_events_disabled = false;
static bool Osiris_level_script_loaded = false;

#define HAVECUSTOMONLY(type) (type == OBJ_CAMERA)
#define CANBEASSIGNEDSCRIPT(obj)                                                                                       \
  ((obj) &&                                                                                                            \
   ((obj->type == OBJ_ROBOT) || (obj->type == OBJ_BUILDING) || (obj->type == OBJ_POWERUP) ||                           \
    (obj->type == OBJ_CLUTTER) || (obj->type == OBJ_DOOR) || (obj->type == OBJ_CAMERA) || (obj->type == OBJ_DUMMY)))
#define CANHAVEANYSCRIPT(obj)                                                                                          \
  (CANBEASSIGNEDSCRIPT(obj) || (obj && ((obj->type == OBJ_DEBRIS) || (obj->type == OBJ_GHOST))))
#define CANTYPEBEASSIGNEDSCRIPT(type)                                                                                  \
  ((type == OBJ_ROBOT) || (type == OBJ_BUILDING) || (type == OBJ_POWERUP) || (type == OBJ_CLUTTER) ||                  \
   (type == OBJ_DOOR) || (type == OBJ_CAMERA))
//========================================================================

//	Osiris_ShutdownModuleLoader
//	Purpose:
//		Closes down the OSIRIS module loader and handling system
void Osiris_ShutdownModuleLoader(void) {
  mprintf(0, "OSIRIS: Shutting down module manager\n");
  int i;
  for (i = 0; i < MAX_LOADED_MODULES; i++) {
    Osiris_FreeModule(i);
  }
}

//	Osiris_InitModuleLoader
//	Purpose:
//		Initializes the OSIRIS module loader and handling system
void Osiris_InitModuleLoader(void) {
  mprintf(0, "OSIRIS: Initializing module manager\n");
  int i;
  for (i = 0; i < MAX_LOADED_MODULES; i++) {
    OSIRIS_loaded_modules[i].flags = 0;
    OSIRIS_loaded_modules[i].reference_count = 0;
    OSIRIS_loaded_modules[i].module_name = NULL;
    OSIRIS_loaded_modules[i].CallInstanceEvent = NULL;
    OSIRIS_loaded_modules[i].CreateInstance = NULL;
    OSIRIS_loaded_modules[i].DestroyInstance = NULL;
    OSIRIS_loaded_modules[i].GetCOScriptList = NULL;
    OSIRIS_loaded_modules[i].GetGOScriptID = NULL;
    OSIRIS_loaded_modules[i].GetTriggerScriptID = NULL;
    OSIRIS_loaded_modules[i].InitializeDLL = NULL;
    OSIRIS_loaded_modules[i].SaveRestoreState = NULL;
    OSIRIS_loaded_modules[i].string_table = NULL;
    OSIRIS_loaded_modules[i].strings_loaded = 0;

#ifdef OSIRISDEBUG
    ASSERT(OSIRIS_loaded_modules[i].RefRoot == NULL);
    OSIRIS_loaded_modules[i].RefRoot = NULL;
#endif
  }

  Osiris_CreateModuleInitStruct(&Osiris_module_init);
  Osiris_game_checksum = Osiris_CreateGameChecksum();
  Osiris_module_init.game_checksum = Osiris_game_checksum;

  tOSIRISCurrentLevel.level_loaded = false;
  tOSIRISCurrentLevel.num_customs = 0;
  tOSIRISCurrentLevel.dll_id = 0;
  tOSIRISCurrentLevel.custom_ids = NULL;
  tOSIRISCurrentLevel.custom_handles = NULL;
  tOSIRISCurrentLevel.instance = NULL;

  tOSIRISCurrentMission.mission_loaded = false;
  tOSIRISCurrentMission.dll_id = 0;

  Osiris_InitMemoryManager();
  Osiris_InitOMMS();

  OSIRIS_Extracted_script_dir = NULL;

  atexit(Osiris_ShutdownModuleLoader);
}

//	Osiris_CreateGameChecksum
//	Purpose:
//		Generates a checksum of the game's structures, to give to the modules
//	so they can use to compare to the time when they were compiled, to see
//	if they are compatible.
uint32_t Osiris_CreateGameChecksum(void) {
  uint32_t value = 0xe1e1b0b0;

  value += sizeof(object);
  value += sizeof(player) * 2;
  value += sizeof(tOSIRISModuleInit) * 3;
  value += sizeof(tOSIRISEventInfo) * 5;
  value += sizeof(tOSIRISTIMER) * 7;
  value += sizeof(tOSIRISSCRIPTID) * 11;
  value += sizeof(tOSIRISMEMCHUNK) * 13;
  value += sizeof(msafe_struct) * 17;
  value += sizeof(ray_info) * 19;
  value += sizeof(vector) * 23;
  value += sizeof(matrix) * 29;
  value += sizeof(tGameCinematic) * 31;
  value += sizeof(tCannedCinematicInfo) * 37;

  value = value << 2;

  int index = 0;
  while (Osiris_module_init.fp[index]) {
    value++;
    index++;
  }

  return value;
}

//	Osiris_FreeModule
//	Purpose:
//		Removes a currently loaded module from the OSIRIS system
void Osiris_FreeModule(int id) {
  if (id < 0 || id >= MAX_LOADED_MODULES)
    return;

  if (OSIRIS_loaded_modules[id].flags & OSIMF_INUSE) {

#ifdef OSIRISDEBUG
    ASSERT(OSIRIS_loaded_modules[id].RefRoot == NULL); // not all objects freed
#endif

    if (OSIRIS_loaded_modules[id].flags & OSIMF_LEVEL) {
      // we're freeing the level dll
      tOSIRISCurrentLevel.level_loaded = false;
      tOSIRISCurrentLevel.num_customs = 0;
      tOSIRISCurrentLevel.dll_id = 0;
      tOSIRISCurrentLevel.custom_ids = NULL;
      tOSIRISCurrentLevel.custom_handles = NULL;
      tOSIRISCurrentLevel.instance = NULL;
    }

    if (!(OSIRIS_loaded_modules[id].flags & OSIMF_DLLELSEWHERE)) {

      // this is a DLL belonging to us
      if (OSIRIS_loaded_modules[id].ShutdownDLL) {
        // call the shutdown function so the DLL can perform what it needs
        OSIRIS_loaded_modules[id].ShutdownDLL();
      }

      if (OSIRIS_loaded_modules[id].string_table != NULL) {
        DestroyStringTable(OSIRIS_loaded_modules[id].string_table, OSIRIS_loaded_modules[id].strings_loaded);
      }
      mod_FreeModule(&OSIRIS_loaded_modules[id].mod);
    }

    if (tOSIRISCurrentMission.mission_loaded && id == tOSIRISCurrentMission.dll_id) {
      // we're freeing the mission dll
      tOSIRISCurrentMission.mission_loaded = false;
      tOSIRISCurrentMission.dll_id = 0;
    }

    OSIRIS_loaded_modules[id].CallInstanceEvent = NULL;
    OSIRIS_loaded_modules[id].CreateInstance = NULL;
    OSIRIS_loaded_modules[id].DestroyInstance = NULL;
    OSIRIS_loaded_modules[id].GetCOScriptList = NULL;
    OSIRIS_loaded_modules[id].GetGOScriptID = NULL;
    OSIRIS_loaded_modules[id].GetTriggerScriptID = NULL;
    OSIRIS_loaded_modules[id].InitializeDLL = NULL;
    OSIRIS_loaded_modules[id].SaveRestoreState = NULL;
    OSIRIS_loaded_modules[id].string_table = NULL;
    OSIRIS_loaded_modules[id].strings_loaded = 0;
    OSIRIS_loaded_modules[id].flags = 0;
    OSIRIS_loaded_modules[id].reference_count = 0;

#ifdef OSIRISDEBUG
    ASSERT(OSIRIS_loaded_modules[id].RefRoot == NULL);
    OSIRIS_loaded_modules[id].RefRoot = NULL;
#endif

    if (OSIRIS_loaded_modules[id].module_name) {
      mem_free(OSIRIS_loaded_modules[id].module_name);
      OSIRIS_loaded_modules[id].module_name = NULL;
    }
  }
}

void Osiris_ForceUnloadModules(void) {
  for (int j = 0; j < MAX_LOADED_MODULES; j++) {
    if (OSIRIS_loaded_modules[j].flags & OSIMF_INUSE) {
      Osiris_FreeModule(j);
    }
  }
}

//	Osiris_DumpLoadedObjects
//
//	Debug.  Dumps all loaded objects to given file
void Osiris_DumpLoadedObjects(char *file) {
#ifndef OSIRISDEBUG
  return;
#else
  CFILE *f = cfopen(file, "wt");
  if (!f) {
    Int3();
    return;
  }

  char buffy[1024];

  for (int j = 0; j < MAX_LOADED_MODULES; j++) {
    if (OSIRIS_loaded_modules[j].flags & OSIMF_INUSE && OSIRIS_loaded_modules[j].RefRoot != NULL) {
      // uh oh! scripts were not completely decremented
      tRefObj *node = OSIRIS_loaded_modules[j].RefRoot;
      while (node) {
        snprintf(buffy, sizeof(buffy), "Module: \"%s\" Objnum: %d Type: %d%s Id:%d\n",
                 OSIRIS_loaded_modules[j].module_name, node->objnum, node->type, (node->dummy) ? "(Dummy)" : "",
                 node->id);
        cf_WriteString(f, buffy);
        node = node->next;
      }
    }
  }

  cfclose(f);
#endif
}

//	Osiris_FindLoadedModule
//	Purpose:
//		Given the name of a module, it returns the id of a loaded OSIRIS module.  -1 if it isn't loaded.
int Osiris_FindLoadedModule(char *module_name) {
  // search through the list of loaded modules and see if we can find a match
  // strip off the extension
  char real_name[_MAX_PATH];
  ddio_SplitPath(module_name, NULL, real_name, NULL);

  int i;
  for (i = 0; i < MAX_LOADED_MODULES; i++) {
    if (OSIRIS_loaded_modules[i].flags & OSIMF_INUSE) {
      if (OSIRIS_loaded_modules[i].module_name && (!stricmp(OSIRIS_loaded_modules[i].module_name, real_name))) {
        // we found a match
        return i;
      }
    }
  }

  return -1;
}

//	Osiris_UnloadModule
//	Purpose:
//		Given a module id, it will decrement the reference count to that module by one.  If the reference
//	count becomes zero, the module will be unloaded from memory.
void Osiris_UnloadModule(int module_id) {
  if (module_id < 0 || module_id >= MAX_LOADED_MODULES)
    return;
  if (OSIRIS_loaded_modules[module_id].flags & OSIMF_INUSE) {
    // the module is in use
    if (Show_osiris_debug) {
      mprintf(0, "OSIRIS: Decrementing reference count for module (%s)\n", OSIRIS_loaded_modules[module_id].module_name);
    }
    OSIRIS_loaded_modules[module_id].reference_count--;

    ASSERT(OSIRIS_loaded_modules[module_id].reference_count >= 0);

    if (OSIRIS_loaded_modules[module_id].reference_count <= 0) {

      if (OSIRIS_loaded_modules[module_id].flags & OSIMF_NOUNLOAD) {

        ASSERT(!(OSIRIS_loaded_modules[module_id].flags & OSIMF_LEVEL));        // level modules cannot be set static
        ASSERT(!(OSIRIS_loaded_modules[module_id].flags & OSIMF_DLLELSEWHERE)); // mission modules cannot be set static

        // do not unload this module, due to the forced stay in memory
        mprintf(0, "OSIRIS: Module (%s) staying in memory due to static flag\n",
                 OSIRIS_loaded_modules[module_id].module_name);
      } else {
        // time to unload this module
        if (Show_osiris_debug) {
          mprintf(0, "OSIRIS: Module (%s) reference count is at 0, unloading\n",
                   OSIRIS_loaded_modules[module_id].module_name);
        }
        Osiris_FreeModule(module_id);
      }
    }
  } else {
    mprintf(0, "OSIRIS: Trying to unload a module (%d) that is not in use!\n", module_id);
    Int3();
  }
}

//	Osiris_UnloadLevelModule
//	Purpose:
//		Instead of saving the handle returned from Osiris_LoadLevelModule() and calling
//	Osiris_UnloadModule() with that handle, you can just call this, instead of the call
//	to Osiris_UnloadModule().
void Osiris_UnloadLevelModule(void) {
  Osiris_level_script_loaded = false;

  if ((Game_mode & GM_MULTI) && (Netgame.local_role != LR_SERVER)) {
    // no scripts for a client!
    return;
  }

  // free allocated scripts (we can do this, because the root node will just become
  // NULL and once the first call to allocate memory is used, it'll automatically work)
  Osiris_CloseMemoryManager();

  if (tOSIRISCurrentLevel.level_loaded) {
    int dll_id = tOSIRISCurrentLevel.dll_id;

    // free up all instances of trigger scripts still around
    for (int i = 0; i < Num_triggers; i++) {
      if (Triggers[i].osiris_script.script_instance) {
        OSIRIS_loaded_modules[dll_id].DestroyInstance(Triggers[i].osiris_script.script_id,
                                                      Triggers[i].osiris_script.script_instance);
      }
      Triggers[i].osiris_script.script_id = -1;
      Triggers[i].osiris_script.script_instance = NULL;
    }

    Osiris_UnloadModule(dll_id);
  }

  // go through the remaining modules and unload those with the OSIMF_NOUNLOAD set
  for (int j = 0; j < MAX_LOADED_MODULES; j++) {

    if (OSIRIS_loaded_modules[j].flags & OSIMF_INUSE && OSIRIS_loaded_modules[j].flags & OSIMF_NOUNLOAD) {
      // unload this module
      mprintf(0, "OSIRIS: Unloading static module (%s) due to level end\n", OSIRIS_loaded_modules[j].module_name);
      Osiris_FreeModule(j);
    }

    if (OSIRIS_loaded_modules[j].flags & OSIMF_INUSE && (!(OSIRIS_loaded_modules[j].flags & OSIMF_DLLELSEWHERE))) {
      // Please get Jeff
      // We are done with the level and all modules should be unloaded at this point,
      // however, this module is still in use....
      Osiris_FreeModule(j);
    }
  }
}

//	returns:	-2 if not found
//				-1 if it is in data\scripts
//				0-x which extracted script id it is
int _get_full_path_to_module(char *module_name, char *fullpath, char *basename) {
  char modfilename[_MAX_PATH];
  char ppath[_MAX_PATH], pext[256];
  char adjusted_name[_MAX_PATH], adjusted_fname[_MAX_PATH];
  char *p;

  ddio_SplitPath(module_name, ppath, basename, pext);

  // make sure filename/ext is all lowercase, requirement for Linux, doesn't hurt Windows
  p = basename;
  while (p && *p) {
    *p = tolower(*p);
    p++;
  }
  p = pext;
  while (p && *p) {
    *p = tolower(*p);
    p++;
  }
  strcpy(adjusted_fname, basename);
  strcat(adjusted_fname, pext);

  if (strlen(ppath) > 0) {
    ddio_MakePath(adjusted_name, ppath, adjusted_fname, NULL);
  } else {
    strcpy(adjusted_name, adjusted_fname);
  }

  // determine real name of script
  mod_GetRealModuleName(adjusted_name, modfilename);

  int exist = cfexist(modfilename);
  switch (exist) {
  case CFES_ON_DISK:
    ddio_MakePath(fullpath, LocalScriptDir, modfilename, NULL);
    return -1;
    break;
  case CFES_IN_LIBRARY: {
    ASSERT(OSIRIS_Extracted_script_dir);
    if (!OSIRIS_Extracted_script_dir)
      return -2;

    // search through our list of extracted files to find it...
    for (int i = 0; i < MAX_LOADED_MODULES; i++) {
      if (OSIRIS_Extracted_scripts[i].flags & OESF_USED) {
        if (!stricmp(basename, OSIRIS_Extracted_scripts[i].real_filename)) {
          // this is it
          ddio_MakePath(fullpath, OSIRIS_Extracted_script_dir, OSIRIS_Extracted_scripts[i].temp_filename, NULL);
          return i;
        }
      }
    }
    Int3(); // this file was supposed to exist
  } break;
  default:
    *fullpath = '\0';
    return -2;
  };
  return -2;
}

//	Osiris_LoadLevelModule
//	Purpose:
//		Given a module name, it will attempt to load the module as a level module.  If it succeeds
//	it will return the id of the module where it has been loaded.  If the module was already loaded
//	before calling this function, it will return the id to where the module is, and will not reload
//	the module.  Returns -1 if the module does not exist.  Returns -2 if the module couldn't initialize.
//	Returns -3 if the module is not a level module. Returns -4 if no module slots are available.
int Osiris_LoadLevelModule(char *module_name) {
  if ((Game_mode & GM_MULTI) && (Netgame.local_role != LR_SERVER)) {
    // no scripts for a client!
    return -2;
  }

  ASSERT(!tOSIRISCurrentLevel.level_loaded);
  if (tOSIRISCurrentLevel.level_loaded) {
    mprintf(0, "OSIRIS: Trying to load a level dll, when one has already been loaded\n");
    return tOSIRISCurrentLevel.dll_id;
  }

  int loaded_id;
  loaded_id = Osiris_FindLoadedModule(module_name);
  if (loaded_id != -1) {
    // the module is already loaded
    OSIRIS_loaded_modules[loaded_id].reference_count++;
    if (Show_osiris_debug) {
      mprintf(0, "OSIRIS: Level Module (%s) reference count increased to %d\n", module_name,
               OSIRIS_loaded_modules[loaded_id].reference_count);
    }
    return loaded_id;
  }

  // the module hasn't been loaded yet, find an available slot.
  for (loaded_id = 0; loaded_id < MAX_LOADED_MODULES; loaded_id++) {
    if (!(OSIRIS_loaded_modules[loaded_id].flags & OSIMF_INUSE)) {
      // we found a free slot
      break;
    }
  }

  if (loaded_id >= MAX_LOADED_MODULES) {
    // no slots available
    mprintf(0, "OSIRIS: Osiris_LoadLevelModule(%s): No available slots\n", module_name);
    Int3();
    return -4;
  }

  OSIRIS_loaded_modules[loaded_id].flags = 0; // set this to 0 as we fill in the data

  char fullpath[_MAX_PATH], basename[_MAX_PATH];
  int ret_val = _get_full_path_to_module(module_name, fullpath, basename);
  switch (ret_val) {
  case -2:
    // the module does not exist
    mprintf(0, "OSIRIS: Osiris_LoadLevelModule(%s): Module doesn't exist\n", module_name);
    return -1;
    break;
  case -1:
    // the module is in data\scripts
    break;
  default:
    // the module was an extracted file
    mprintf(0, "OSIRIS: Found module (%s) in a temp file\n", basename);
    OSIRIS_loaded_modules[loaded_id].flags |= OSIMF_INTEMPDIR;
    OSIRIS_loaded_modules[loaded_id].extracted_id = ret_val;
    break;
  }

  // the module exists, now attempt to load it
  if (!mod_LoadModule(&OSIRIS_loaded_modules[loaded_id].mod, fullpath)) {
    // there was an error trying to load the module
    mprintf(0, "OSIRIS: Osiris_LoadLevelModule(%s): Unable to load module\n", module_name);
    Int3();
    return -3;
  }

  // the module has loaded, attempt to import all the level functions
  tOSIRISModule *osm = &OSIRIS_loaded_modules[loaded_id];
  module *mod = &osm->mod;

  // there are 9 functions we need to import
  // InitializeDLL@4
  // ShutdownDLL@0
  // GetGOScriptID@4
  // GetTriggerScriptID@4
  // GetCOScriptList@8
  // CreateInstance@4
  // DestroyInstance@8
  // CallInstanceEvent@16
  // SaveRestoreState@8

  osm->InitializeDLL = (InitializeDLL_fp)mod_GetSymbol(mod, "InitializeDLL", 4);
  osm->ShutdownDLL = (ShutdownDLL_fp)mod_GetSymbol(mod, "ShutdownDLL", 0);
  osm->GetGOScriptID = (GetGOScriptID_fp)mod_GetSymbol(mod, "GetGOScriptID", 8);
  osm->GetTriggerScriptID = (GetTriggerScriptID_fp)mod_GetSymbol(mod, "GetTriggerScriptID", 8);
  osm->GetCOScriptList = (GetCOScriptList_fp)mod_GetSymbol(mod, "GetCOScriptList", 8);
  osm->CreateInstance = (CreateInstance_fp)mod_GetSymbol(mod, "CreateInstance", 4);
  osm->DestroyInstance = (DestroyInstance_fp)mod_GetSymbol(mod, "DestroyInstance", 8);
  osm->CallInstanceEvent = (CallInstanceEvent_fp)mod_GetSymbol(mod, "CallInstanceEvent", 16);
  osm->SaveRestoreState = (SaveRestoreState_fp)mod_GetSymbol(mod, "SaveRestoreState", 8);

  osm->flags |= OSIMF_INUSE | OSIMF_LEVEL;
  osm->module_name = mem_strdup(basename);
  osm->reference_count = 1;

#ifdef OSIRISDEBUG
  ASSERT(osm->RefRoot == NULL);
  osm->RefRoot = NULL;
#endif

  // make sure all of the functions imported ok
  if (!osm->InitializeDLL || !osm->ShutdownDLL || !osm->GetGOScriptID || !osm->GetTriggerScriptID ||
      !osm->GetCOScriptList || !osm->CreateInstance || !osm->DestroyInstance || !osm->SaveRestoreState ||
      !osm->CallInstanceEvent) {
    // there was an error importing a function
    mprintf(0, "OSIRIS: Osiris_LoadLevelModule(%s) couldn't import function.\n", module_name);
    Int3();
    osm->flags = 0;
    if (osm->module_name)
      mem_free(osm->module_name);
    osm->module_name = NULL;
    mod_FreeModule(mod);
    return -3;
  }

  // check to see if there is a corresponding string table to load
  char stringtablename[_MAX_PATH];
  strcpy(stringtablename, basename);
  strcat(stringtablename, ".str");

  if (cfexist(stringtablename)) {
    // there is a string table, load it up
    bool ret = CreateStringTable(stringtablename, &osm->string_table, &osm->strings_loaded);
    if (!ret) {
      mprintf(0, "OSIRIS: Unable to load string table (%s) for (%s)\n", stringtablename, basename);
      Int3();
      osm->string_table = NULL;
      osm->strings_loaded = 0;
    }
  } else {
    osm->string_table = NULL;
    osm->strings_loaded = 0;
  }

  Osiris_module_init.string_count = osm->strings_loaded;
  Osiris_module_init.string_table = osm->string_table;
  Osiris_module_init.module_is_static = false;
  Osiris_module_init.script_identifier = osm->module_name;
  Osiris_module_init.module_identifier = loaded_id;

  // when we get to this point we nearly have a loaded module, we just need to initialize it
  if (!osm->InitializeDLL(&Osiris_module_init)) {
    // there was an error initializing the module
    mprintf(0, "OSIRIS: Osiris_LoadLevelModule(%s) error initializing module.\n", basename);
    if (osm->string_table) {
      DestroyStringTable(osm->string_table, osm->strings_loaded);
    }
    osm->flags = 0;
    if (osm->module_name)
      mem_free(osm->module_name);
    osm->module_name = NULL;
    osm->string_table = NULL;
    osm->strings_loaded = 0;
    mod_FreeModule(mod);
    return -2;
  }

  // intialize trigger scripts
  for (int i = 0; i < Num_triggers; i++) {
    int script_id;
    void *instance;

    Triggers[i].osiris_script.script_id = -1;
    Triggers[i].osiris_script.script_instance = NULL;

    script_id = osm->GetTriggerScriptID(Triggers[i].roomnum, Triggers[i].facenum);
    if (script_id != -1) {
      // the trigger was found
      instance = osm->CreateInstance(script_id);
      if (!instance) {
        mprintf(0, "OSIRIS: Unable to create instance for trigger script (%d)\n", i);
      } else {
        Triggers[i].osiris_script.script_id = script_id;
        Triggers[i].osiris_script.script_instance = instance;
      }
    }
  }

  // we have a successful module load
  tOSIRISCurrentLevel.level_loaded = true;
  tOSIRISCurrentLevel.dll_id = loaded_id;
  tOSIRISCurrentLevel.num_customs = OSIRIS_loaded_modules[loaded_id].GetCOScriptList(
      &tOSIRISCurrentLevel.custom_handles, &tOSIRISCurrentLevel.custom_ids);
  tOSIRISCurrentLevel.instance =
      OSIRIS_loaded_modules[loaded_id].CreateInstance(0); // level scripts always have id of 0 in a level dll

  mprintf(0, "OSIRIS: Level Module (%s) loaded successfully (%d custom handles)\n", basename,
           tOSIRISCurrentLevel.num_customs);
  Osiris_level_script_loaded = true;
  return loaded_id;
}

//	Osiris_LoadGameModule
//	Purpose:
//		Given a module name, it will attempt to load the module as a game module.  If it succeeds
//	it will return the id of the module where it has been loaded.  If the module was already loaded
//	before calling this function, it will return the id to where the module is, and will not reload
//	the module.  Returns -1 if the module does not exist.  Returns -2 if the module couldn't initialize.
//	Returns -3 if the module is not a game module. Returns -4 if no module slots are available.
int Osiris_LoadGameModule(char *module_name) {
  if (module_name[0] == '\0') {
    return -1;
  }

  int loaded_id;
  loaded_id = Osiris_FindLoadedModule(module_name);
  if (loaded_id != -1) {
    // the module is already loaded
    OSIRIS_loaded_modules[loaded_id].reference_count++;
    if (Show_osiris_debug) {
      mprintf(0, "OSIRIS: Game Module (%s) reference count increased to %d\n", module_name,
               OSIRIS_loaded_modules[loaded_id].reference_count);
    }
    return loaded_id;
  }

  // the module hasn't been loaded yet, find an available slot.
  for (loaded_id = 0; loaded_id < MAX_LOADED_MODULES; loaded_id++) {
    if (!(OSIRIS_loaded_modules[loaded_id].flags & OSIMF_INUSE)) {
      // we found a free slot
      break;
    }
  }

  if (loaded_id >= MAX_LOADED_MODULES) {
    // no slots available
    mprintf(0, "OSIRIS: Osiris_LoadGameModule(%s): No available slots\n", module_name);
    Int3();
    return -4;
  }

  OSIRIS_loaded_modules[loaded_id].flags = 0; // set this to 0 as we fill in the data

  char fullpath[_MAX_PATH], basename[_MAX_PATH];
  int ret_val = _get_full_path_to_module(module_name, fullpath, basename);
  switch (ret_val) {
  case -2:
    // the module does not exist
    mprintf(0, "OSIRIS: Osiris_LoadLevelModule(%s): Module doesn't exist\n", module_name);
    return -1;
    break;
  case -1:
    // the module is in data\scripts
    break;
  default:
    // the module was an extracted file
    mprintf(0, "OSIRIS: Found module (%s) in a temp file\n", basename);
    OSIRIS_loaded_modules[loaded_id].flags |= OSIMF_INTEMPDIR;
    OSIRIS_loaded_modules[loaded_id].extracted_id = ret_val;
    break;
  }

  // the module exists, now attempt to load it
  if (!mod_LoadModule(&OSIRIS_loaded_modules[loaded_id].mod, fullpath)) {
    // there was an error trying to load the module
    mprintf(0, "OSIRIS: Osiris_LoadGameModule(%s): Unable to load module\n", module_name);
    Int3();
    return -3;
  }

  // the module has loaded, attempt to import all the level functions
  tOSIRISModule *osm = &OSIRIS_loaded_modules[loaded_id];
  module *mod = &osm->mod;

  // there are 7 functions we need to import
  // InitializeDLL@4
  // ShutdownDLL@0
  // GetGOScriptID@4
  // CreateInstance@4
  // DestroyInstance@8
  // CallInstanceEvent@16
  // SaveRestoreState@8

  osm->InitializeDLL = (InitializeDLL_fp)mod_GetSymbol(mod, "InitializeDLL", 4);
  osm->ShutdownDLL = (ShutdownDLL_fp)mod_GetSymbol(mod, "ShutdownDLL", 0);
  osm->GetGOScriptID = (GetGOScriptID_fp)mod_GetSymbol(mod, "GetGOScriptID", 8);
  osm->GetTriggerScriptID = NULL;
  osm->GetCOScriptList = NULL;
  osm->CreateInstance = (CreateInstance_fp)mod_GetSymbol(mod, "CreateInstance", 4);
  osm->DestroyInstance = (DestroyInstance_fp)mod_GetSymbol(mod, "DestroyInstance", 8);
  osm->CallInstanceEvent = (CallInstanceEvent_fp)mod_GetSymbol(mod, "CallInstanceEvent", 16);
  osm->SaveRestoreState = (SaveRestoreState_fp)mod_GetSymbol(mod, "SaveRestoreState", 8);

  osm->flags |= OSIMF_INUSE;
  osm->module_name = mem_strdup(basename);
  osm->reference_count = 1;

#ifdef OSIRISDEBUG
  ASSERT(osm->RefRoot == NULL);
  osm->RefRoot = NULL;
#endif

  // make sure all of the functions imported ok
  if (!osm->InitializeDLL || !osm->ShutdownDLL || !osm->GetGOScriptID || !osm->CreateInstance ||
      !osm->DestroyInstance || !osm->SaveRestoreState || !osm->CallInstanceEvent) {
    // there was an error importing a function
    mprintf(0, "OSIRIS: Osiris_LoadGameModule(%s) couldn't import function.\n", basename);
    Int3();
    osm->flags = 0;
    if (osm->module_name)
      mem_free(osm->module_name);
    osm->module_name = NULL;
    mod_FreeModule(mod);
    return -3;
  }

  // check to see if there is a corresponding string table to load
  char stringtablename[_MAX_PATH];
  strcpy(stringtablename, basename);
  strcat(stringtablename, ".str");

  if (cfexist(stringtablename)) {
    // there is a string table, load it up
    bool ret = CreateStringTable(stringtablename, &osm->string_table, &osm->strings_loaded);
    if (!ret) {
      mprintf(0, "OSIRIS: Unable to load string table (%s) for (%s)\n", stringtablename, basename);
      Int3();
      osm->string_table = NULL;
      osm->strings_loaded = 0;
    }
  } else {
    osm->string_table = NULL;
    osm->strings_loaded = 0;
  }
  Osiris_module_init.string_count = osm->strings_loaded;
  Osiris_module_init.string_table = osm->string_table;
  Osiris_module_init.module_is_static = false; // set this to false to avoid a fake true
  Osiris_module_init.script_identifier = osm->module_name;
  Osiris_module_init.module_identifier = loaded_id;

  // when we get to this point we nearly have a loaded module, we just need to initialize it
  if (!osm->InitializeDLL(&Osiris_module_init)) {
    // there was an error initializing the module
    mprintf(0, "OSIRIS: Osiris_LoadGameModule(%s) error initializing module.\n", basename);
    if (osm->string_table) {
      DestroyStringTable(osm->string_table, osm->strings_loaded);
    }
    osm->string_table = NULL;
    osm->strings_loaded = 0;
    osm->flags = 0;
    if (osm->module_name)
      mem_free(osm->module_name);
    osm->module_name = NULL;
    mod_FreeModule(mod);
    return -2;
  }

  if (Osiris_module_init.module_is_static) {
    // the module is requesting to be static
    mprintf(0, "OSIRIS: Module (%s) is requesting to be static\n", osm->module_name);
    osm->flags |= OSIMF_NOUNLOAD;
  }

  // we have a successful module load
  mprintf(0, "OSIRIS: Game Module (%s) loaded successfully\n", basename);
  return loaded_id;
}

//	Osiris_LoadMissionModule
//	Purpose:
//		It will attempt to load the module as a game module.  If it succeeds
//	it will return the id of the module where it has been loaded.  If the module was already loaded
//	before calling this function, it will return the id to where the module is, and will not reload
//	the module.  Returns -1 if the module does not exist.  Returns -2 if the module couldn't initialize.
//	Returns -3 if the module is not a game module. Returns -4 if no module slots are available.
//	This technically doesn't load a mission module, as it should already be loaded by
//	Descent 3 prior.
int Osiris_LoadMissionModule(module *module_handle, const char *filename) {
  if ((Game_mode & GM_MULTI) && (Netgame.local_role != LR_SERVER)) {
    // no scripts for a client!
    return -1;
  }

  if (!module_handle) {
    return -1;
  }

  int loaded_id;

  // find an available slot.
  for (loaded_id = 0; loaded_id < MAX_LOADED_MODULES; loaded_id++) {
    if (!(OSIRIS_loaded_modules[loaded_id].flags & OSIMF_INUSE)) {
      // we found a free slot
      break;
    }
  }

  if (loaded_id >= MAX_LOADED_MODULES) {
    // no slots available
    mprintf(0, "OSIRIS: Osiris_LoadMissionModule(%s): No available slots\n", filename);
    Int3();
    return -4;
  }

  // make sure the module exists so we can load it
  if (!module_handle->handle) {
    // the module does not exist
    mprintf(0, "OSIRIS: Osiris_LoadMissionModule(%s): Module doesn't exist\n", filename);
    // Int3();
    return -1;
  }

  // the module has loaded, attempt to import all the game functions
  tOSIRISModule *osm = &OSIRIS_loaded_modules[loaded_id];
  memcpy(&osm->mod, module_handle, sizeof(module));
  module *mod = &osm->mod;

  // there are 5 functions we need to import
  // GetGOScriptID@4
  // CreateInstance@4
  // DestroyInstance@8
  // CallInstanceEvent@16
  // SaveRestoreState@8

  osm->InitializeDLL = NULL;
  osm->ShutdownDLL = NULL;
  osm->GetTriggerScriptID = NULL;
  osm->GetCOScriptList = NULL;
  osm->GetGOScriptID = (GetGOScriptID_fp)mod_GetSymbol(mod, "GetGOScriptID", 8);
  osm->CreateInstance = (CreateInstance_fp)mod_GetSymbol(mod, "CreateInstance", 4);
  osm->DestroyInstance = (DestroyInstance_fp)mod_GetSymbol(mod, "DestroyInstance", 8);
  osm->CallInstanceEvent = (CallInstanceEvent_fp)mod_GetSymbol(mod, "CallInstanceEvent", 16);
  osm->SaveRestoreState = (SaveRestoreState_fp)mod_GetSymbol(mod, "SaveRestoreState", 8);

  osm->flags = OSIMF_INUSE | OSIMF_DLLELSEWHERE;
  osm->module_name = mem_strdup(filename);
  osm->reference_count = 1;

#ifdef OSIRISDEBUG
  ASSERT(osm->RefRoot == NULL);
  osm->RefRoot = NULL;
#endif

  // make sure all of the functions imported ok
  if (!osm->GetGOScriptID || !osm->CreateInstance || !osm->DestroyInstance || !osm->SaveRestoreState ||
      !osm->CallInstanceEvent) {
    // there was an error importing a function
    mprintf(0, "OSIRIS: Osiris_LoadMissionModule(%s) couldn't import function.\n", filename);
    osm->flags = 0;
    tOSIRISCurrentMission.mission_loaded = false;
    Int3();
    // mod_FreeModule(mod);	//don't unload it! it's needed by the mission
    return -3;
  }

  // we have a successful module load
  tOSIRISCurrentMission.mission_loaded = true;
  tOSIRISCurrentMission.dll_id = loaded_id;
  if (Show_osiris_debug) {
    mprintf(0, "OSIRIS: Mission Game Module (%s) loaded successfully\n", filename);
  }
  return loaded_id;
}

//	Osiris_UnloadMissionModule
//	Purpose:
//		Instead of saving the handle returned from Osiris_LoadMissionModule() and calling
//	Osiris_UnloadModule() with that handle, you can just call this, instead of the call
//	to Osiris_UnloadModule().
void Osiris_UnloadMissionModule(void) {
  if ((Game_mode & GM_MULTI) && (Netgame.local_role != LR_SERVER)) {
    // no scripts for a client!
    return;
  }

  if (!tOSIRISCurrentMission.mission_loaded)
    return;

  int dll_id = tOSIRISCurrentMission.dll_id;

  Osiris_FreeModule(dll_id);
}

//	Osiris_BindScriptsToObject
//	Purpose:
//		Call this function after an object has been created to bind all the scripts associated
//	with it to the object.  This function must be called near the end of it's initialization,
//	to make sure that all fields have been filled in.  This function does not call any events.
//	This function will also load any dll's needed for it's game script.
//	returns false if nothing was bound.
bool Osiris_BindScriptsToObject(object *obj) {
  ASSERT(obj->osiris_script == NULL);
  if (obj->osiris_script) // already has a script bound!
    return true;

  if ((Game_mode & GM_MULTI) && (Netgame.local_role != LR_SERVER)) {
    // no scripts for a client!
    return false;
  }

  // make sure the object is valid
  if (!CANBEASSIGNEDSCRIPT(obj)) {
    // invalid object
    return false;
  }
  int real_type;
  real_type = (obj->type == OBJ_DUMMY) ? obj->dummy_type : obj->type;

  if (!CANTYPEBEASSIGNEDSCRIPT(real_type)) {
    // invalid type
    return false;
  }

  int dll_id, gos_id;
  void *gos_instance;
  tOSIRISScript *os;

  bool isdoor = (bool)(real_type == OBJ_DOOR);
  bool iscustomonly = (bool)HAVECUSTOMONLY(real_type);
  char *default_module_name;
  char *page_name;

  if (iscustomonly) {
    // this object can only have a custom script for it (i.e. OBJ_CAMERA)
    default_module_name = NULL;
    page_name = NULL;
  } else {
    if (isdoor) {
      default_module_name = Doors[obj->id].module_name;
      page_name = Doors[obj->id].name;
    } else {
      if (obj->custom_default_module_name) {
        // we have a custom default script
        default_module_name = obj->custom_default_module_name;
      } else {
        // use what was set in Object_info
        default_module_name = Object_info[obj->id].module_name;
      }

      if (obj->custom_default_script_name) {
        // we have a custom default script
        page_name = obj->custom_default_script_name;
      } else {
        // use what was set in Object_info
        if (Object_info[obj->id].script_name_override[0] != '\0')
          page_name = Object_info[obj->id].script_name_override;
        else
          page_name = Object_info[obj->id].name;
      }
    }
  }

  if (!iscustomonly) {
    // this object can have a default script
    ASSERT(default_module_name);
    ASSERT(page_name);

    // load up the dllname associated with the object and get the id
    dll_id = Osiris_LoadGameModule(default_module_name);
    if (dll_id < 0) {
      // there was an error finding this object's dll
      if (Show_osiris_debug) {
        mprintf(0, "OSIRIS: Unable to load module (%s) to bind to object (%s)\n", default_module_name, page_name);
      }
    } else {
      // allocate the memory for the object's scripts
      obj->osiris_script = (tOSIRISScript *)mem_malloc(sizeof(tOSIRISScript));
      if (!obj->osiris_script) {
        // out of memory
        mprintf(0, "OSIRIS: Out of memory trying to bind script\n");
        return false;
      }

      obj->osiris_script->default_script.script_instance = NULL;
      obj->osiris_script->custom_script.script_instance = NULL;
      obj->osiris_script->mission_script.script_instance = NULL;
      obj->osiris_script->level_script.script_instance = NULL;
      os = obj->osiris_script;

      // we have the module loaded for the object, now we need to setup it's default script
      gos_id = OSIRIS_loaded_modules[dll_id].GetGOScriptID(page_name, isdoor);

      if (gos_id == -1) {
        // the default script for this object does not exist in the dll set for it
        mprintf(0, "OSIRIS: Unable to find GOS ID for (%s) in (%s)!\n", page_name, default_module_name);
        Int3();
        Osiris_UnloadModule(dll_id);
      } else {

        // we now have the GOS ID for the script.  All future communication with the default script-dll is
        // to use this GOS ID.  Now try to create an instance of the object.  Hopefully we have enough memory.
        gos_instance = OSIRIS_loaded_modules[dll_id].CreateInstance(gos_id);
        if (!gos_instance) {
          // we had an error obtaining the instance of the GOS...ugh
          mprintf(0, "OSIRIS: Unable to create GOS instance for (%s)\n", page_name);
          Int3();
          Osiris_UnloadModule(dll_id);
        } else {

          // all looks good! we're guaranteed to have at least one script for this object
          // setup default script values
          os->default_script.DLLID = dll_id;
          os->default_script.script_id = gos_id;
          os->default_script.script_instance = gos_instance;

          // add to the list for this object
#ifdef OSIRISDEBUG
          tRefObj *node;
          if (OSIRIS_loaded_modules[dll_id].RefRoot == NULL) {
            node = OSIRIS_loaded_modules[dll_id].RefRoot = (tRefObj *)mem_malloc(sizeof(tRefObj));
          } else {
            node = OSIRIS_loaded_modules[dll_id].RefRoot;
            while (node->next)
              node = node->next;
            node->next = (tRefObj *)mem_malloc(sizeof(tRefObj));
            node = node->next;
          }
          node->objnum = OBJNUM(obj);
          node->id = obj->id;
          node->dummy = (obj->type == OBJ_DUMMY) ? true : false;
          node->type = (node->dummy == false) ? obj->type : obj->dummy_type;
          node->next = NULL;
#endif
        }
      }
    }
  }

  // now check for a GOS and COS in the level dll (if available)
  if (tOSIRISCurrentLevel.level_loaded) {
    dll_id = tOSIRISCurrentLevel.dll_id;

    ASSERT(OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE);
    if (OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE) {

      // check first for a GOS
      if (!iscustomonly) {
        // this script can have a GOS in the level script
        ASSERT(page_name);

        gos_id = OSIRIS_loaded_modules[dll_id].GetGOScriptID(page_name, isdoor);
        if (gos_id != -1) {
          if (!obj->osiris_script) {
            // we need to allocate memory for a script
            obj->osiris_script = (tOSIRISScript *)mem_malloc(sizeof(tOSIRISScript));
            if (!obj->osiris_script) {
              // out of memory
              mprintf(0, "OSIRIS: Out of memory trying to bind script\n");
              return false;
            }

            obj->osiris_script->default_script.script_instance = NULL;
            obj->osiris_script->custom_script.script_instance = NULL;
            obj->osiris_script->mission_script.script_instance = NULL;
            obj->osiris_script->level_script.script_instance = NULL;
          }
          os = obj->osiris_script;

          // ok, the level dll has a script for us
          gos_instance = OSIRIS_loaded_modules[dll_id].CreateInstance(gos_id);
          if (!gos_instance) {
            // we had an error obtaining the instance of the GOS...ick
            mprintf(0, "OSIRIS: Unable to create GOS instance from level dll for (%s)\n", page_name);
            Int3();
          } else {
            // ok, we got a valid instance
            os->level_script.DLLID = dll_id;
            os->level_script.script_id = gos_id;
            os->level_script.script_instance = gos_instance;

#ifdef OSIRISDEBUG
            tRefObj *node;
            if (OSIRIS_loaded_modules[dll_id].RefRoot == NULL) {
              node = OSIRIS_loaded_modules[dll_id].RefRoot = (tRefObj *)mem_malloc(sizeof(tRefObj));
            } else {
              node = OSIRIS_loaded_modules[dll_id].RefRoot;
              while (node->next)
                node = node->next;
              node->next = (tRefObj *)mem_malloc(sizeof(tRefObj));
              node = node->next;
            }
            node->objnum = OBJNUM(obj);
            node->id = obj->id;
            node->dummy = (obj->type == OBJ_DUMMY) ? true : false;
            node->type = (node->dummy == false) ? obj->type : obj->dummy_type;
            node->next = NULL;
#endif
          }
        }
      } // end if(!iscustomonly)

      // now check to see if we have a custom script for our handle
      int i;
      for (i = 0; i < tOSIRISCurrentLevel.num_customs; i++) {
        if (obj->handle == tOSIRISCurrentLevel.custom_handles[i]) {
          // we found a matching handle, connect it to the script
          gos_id = tOSIRISCurrentLevel.custom_ids[i];
          if (gos_id != -1) {

            if (!obj->osiris_script) {
              // we need to allocate memory for a script
              obj->osiris_script = (tOSIRISScript *)mem_malloc(sizeof(tOSIRISScript));
              if (!obj->osiris_script) {
                // out of memory
                mprintf(0, "OSIRIS: Out of memory trying to bind script\n");
                return false;
              }

              obj->osiris_script->default_script.script_instance = NULL;
              obj->osiris_script->custom_script.script_instance = NULL;
              obj->osiris_script->mission_script.script_instance = NULL;
              obj->osiris_script->level_script.script_instance = NULL;
            }
            os = obj->osiris_script;

            gos_instance = OSIRIS_loaded_modules[dll_id].CreateInstance(gos_id);
            if (!gos_instance) {
              // we had an error obtaining the instance of the COS...doh!
              mprintf(0, "OSIRIS: Unable to create COS instance from level dll for (%s)\n",
                       (page_name) ? (page_name) : "<No Name>");
              Int3();
            } else {
              // ok, everything is valid
              os->custom_script.DLLID = dll_id;
              os->custom_script.script_id = gos_id;
              os->custom_script.script_instance = gos_instance;

#ifdef OSIRISDEBUG
              tRefObj *node;
              if (OSIRIS_loaded_modules[dll_id].RefRoot == NULL) {
                node = OSIRIS_loaded_modules[dll_id].RefRoot = (tRefObj *)mem_malloc(sizeof(tRefObj));
              } else {
                node = OSIRIS_loaded_modules[dll_id].RefRoot;
                while (node->next)
                  node = node->next;
                node->next = (tRefObj *)mem_malloc(sizeof(tRefObj));
                node = node->next;
              }
              node->objnum = OBJNUM(obj);
              node->id = obj->id;
              node->dummy = (obj->type == OBJ_DUMMY) ? true : false;
              node->type = (node->dummy == false) ? obj->type : obj->dummy_type;
              node->next = NULL;
#endif

              if (iscustomonly) {
                mprintf(0, "OSIRIS: Attached custom script to 'custom only' object 0x%x\n", obj->handle);
              }
            }
          }
        }
      }
    }
  }

  // last we need to check the mission dll
  if (!iscustomonly) {
    // this object can have a script in the mission dll
    ASSERT(page_name);

    if (tOSIRISCurrentMission.mission_loaded) {
      dll_id = tOSIRISCurrentMission.dll_id;

      ASSERT(OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE);
      if (OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE) {

        // check first for a GOS
        gos_id = OSIRIS_loaded_modules[dll_id].GetGOScriptID(page_name, isdoor);
        if (gos_id != -1) {
          if (!obj->osiris_script) {
            // we need to allocate memory for a script
            obj->osiris_script = (tOSIRISScript *)mem_malloc(sizeof(tOSIRISScript));
            if (!obj->osiris_script) {
              // out of memory
              mprintf(0, "OSIRIS: Out of memory trying to bind script\n");
              return false;
            }

            obj->osiris_script->default_script.script_instance = NULL;
            obj->osiris_script->custom_script.script_instance = NULL;
            obj->osiris_script->mission_script.script_instance = NULL;
            obj->osiris_script->level_script.script_instance = NULL;
          }
          os = obj->osiris_script;

          // ok, the mission dll has a script for us
          gos_instance = OSIRIS_loaded_modules[dll_id].CreateInstance(gos_id);
          if (!gos_instance) {
            // we had an error obtaining the instance of the GOS...ick
            mprintf(0, "OSIRIS: Unable to create GOS instance from mission dll for (%s)\n", page_name);
            Int3();
          } else {
            // ok, we got a valid instance
            os->mission_script.DLLID = dll_id;
            os->mission_script.script_id = gos_id;
            os->mission_script.script_instance = gos_instance;

#ifdef OSIRISDEBUG
            tRefObj *node;
            if (OSIRIS_loaded_modules[dll_id].RefRoot == NULL) {
              node = OSIRIS_loaded_modules[dll_id].RefRoot = (tRefObj *)mem_malloc(sizeof(tRefObj));
            } else {
              node = OSIRIS_loaded_modules[dll_id].RefRoot;
              while (node->next)
                node = node->next;
              node->next = (tRefObj *)mem_malloc(sizeof(tRefObj));
              node = node->next;
            }
            node->objnum = OBJNUM(obj);
            node->id = obj->id;
            node->dummy = (obj->type == OBJ_DUMMY) ? true : false;
            node->type = (node->dummy == false) ? obj->type : obj->dummy_type;
            node->next = NULL;
#endif
          }
        }
      }
    }
  } // end if(!iscustomonly)

  return true;
}

//	Osiris_DetachScriptsFromObject
//	Purpose:
//		Call this function when an object is about to be destroyed.  This will unbind and remove
//	all scripts associated with that object.  This function does not call any events.
void Osiris_DetachScriptsFromObject(object *obj) {
  if ((Game_mode & GM_MULTI) && (Netgame.local_role != LR_SERVER)) {
    // no scripts for a client!
    return;
  }

  // make sure the object is valid
  if (!CANHAVEANYSCRIPT(obj)) {
    // invalid object
    return;
  }

  if (!obj->osiris_script) {
    return;
  }

  // free the memory for the allocated script
  tOSIRISSCRIPTID osid;
  osid.objhandle = obj->handle;
  osid.type = OBJECT_SCRIPT;
  Osiris_FreeMemoryForScript(&osid);

  int dll_id;
  tOSIRISScript *os;

  os = obj->osiris_script;

  // free up the scripts for the object, starting with it's default script
  if (os->default_script.script_instance) {
    // first we need to free the instance
    dll_id = os->default_script.DLLID;

#ifdef OSIRISDEBUG
    tRefObj *prev = NULL, *node = OSIRIS_loaded_modules[dll_id].RefRoot;
    while (node) {
      if (node->objnum == OBJNUM(obj)) {
        // free this node
        if (prev)
          prev->next = node->next;
        else {
          ASSERT(node == OSIRIS_loaded_modules[dll_id].RefRoot);
          OSIRIS_loaded_modules[dll_id].RefRoot = node->next;
        }

        mem_free(node);
        break;
      }
      prev = node;
      node = node->next;
    }
    ASSERT(node); // make sure we found the node
#endif

    ASSERT(OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE);
    if (OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE) {

      OSIRIS_loaded_modules[dll_id].DestroyInstance(os->default_script.script_id, os->default_script.script_instance);

      // now decrement the dll's count
      Osiris_UnloadModule(dll_id);
    }

    os->default_script.DLLID = 0;
    os->default_script.script_id = 0;
    os->default_script.script_instance = NULL;
  }

  // now check for the level script
  if (os->level_script.script_instance) {
    // free the instance
    dll_id = os->level_script.DLLID;

#ifdef OSIRISDEBUG
    tRefObj *prev = NULL, *node = OSIRIS_loaded_modules[dll_id].RefRoot;
    while (node) {
      if (node->objnum == OBJNUM(obj)) {
        // free this node
        if (prev)
          prev->next = node->next;
        else {
          ASSERT(node == OSIRIS_loaded_modules[dll_id].RefRoot);
          OSIRIS_loaded_modules[dll_id].RefRoot = node->next;
        }

        mem_free(node);
        break;
      }
      prev = node;
      node = node->next;
    }
    ASSERT(node); // make sure we found the node
#endif

    ASSERT(OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE);
    if (OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE) {

      OSIRIS_loaded_modules[dll_id].DestroyInstance(os->level_script.script_id, os->level_script.script_instance);
    }

    os->level_script.DLLID = 0;
    os->level_script.script_id = 0;
    os->level_script.script_instance = NULL;
  }

  // check for an attached custom script
  if (os->custom_script.script_instance) {
    // free the instance
    dll_id = os->custom_script.DLLID;

#ifdef OSIRISDEBUG
    tRefObj *prev = NULL, *node = OSIRIS_loaded_modules[dll_id].RefRoot;
    while (node) {
      if (node->objnum == OBJNUM(obj)) {
        // free this node
        if (prev)
          prev->next = node->next;
        else {
          ASSERT(node == OSIRIS_loaded_modules[dll_id].RefRoot);
          OSIRIS_loaded_modules[dll_id].RefRoot = node->next;
        }

        mem_free(node);
        break;
      }
      prev = node;
      node = node->next;
    }
    ASSERT(node); // make sure we found the node
#endif

    ASSERT(OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE);
    if (OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE) {

      OSIRIS_loaded_modules[dll_id].DestroyInstance(os->custom_script.script_id, os->custom_script.script_instance);
    }

    os->custom_script.DLLID = 0;
    os->custom_script.script_id = 0;
    os->custom_script.script_instance = NULL;
  }

  // check for a mission dll script
  if (os->mission_script.script_instance) {
    // free the instance
    dll_id = os->mission_script.DLLID;

#ifdef OSIRISDEBUG
    tRefObj *prev = NULL, *node = OSIRIS_loaded_modules[dll_id].RefRoot;
    while (node) {
      if (node->objnum == OBJNUM(obj)) {
        // free this node
        if (prev)
          prev->next = node->next;
        else {
          ASSERT(node == OSIRIS_loaded_modules[dll_id].RefRoot);
          OSIRIS_loaded_modules[dll_id].RefRoot = node->next;
        }

        mem_free(node);
        break;
      }
      prev = node;
      node = node->next;
    }
    ASSERT(node); // make sure we found the node
#endif

    ASSERT(OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE);
    if (OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE) {

      OSIRIS_loaded_modules[dll_id].DestroyInstance(os->mission_script.script_id, os->mission_script.script_instance);
    }

    os->mission_script.DLLID = 0;
    os->mission_script.script_id = 0;
    os->mission_script.script_instance = NULL;
  }

  // finally free up the memory allocated for the script struct
  mem_free(obj->osiris_script);
  obj->osiris_script = NULL;
}

//	Osiris_CallLevelEvent
//	Purpose:
//		Triggers an event for a level script.  Returns true if the default action should continue
//	to process.
bool Osiris_CallLevelEvent(int event, tOSIRISEventInfo *data) {
  if (!Osiris_level_script_loaded)
    return true;

  if ((Game_mode & GM_MULTI) && (Netgame.local_role != LR_SERVER)) {
    // no scripts for a client!
    return true;
  }

  if (!(Osiris_event_mask & OEM_LEVELS))
    return true; // level events are disabled

  if (!Osiris_IsEventEnabled(event))
    return true;

  int aux_event = -1;
  if (event == EVT_AI_NOTIFY) {
    switch (data->evt_ai_notify.notify_type) {
    case AIN_GOAL_COMPLETE:
      aux_event = EVT_AIN_GOALCOMPLETE;
      break;
    case AIN_GOAL_INVALID:
      aux_event = EVT_AIN_GOALFAIL;
      break;
    case AIN_GOAL_FAIL:
      aux_event = EVT_AIN_GOALFAIL;
      break;
    case AIN_GOAL_ERROR:
      aux_event = EVT_AIN_GOALFAIL;
      break;
    }
  }

  if (tOSIRISCurrentLevel.level_loaded) {
    // there is a loaded level, get it's dll id and call it's event
    int dll_id = tOSIRISCurrentLevel.dll_id;
    void *instance = tOSIRISCurrentLevel.instance;

    if (instance) {
      data->me_handle = OBJECT_HANDLE_NONE; // its a level script!...no me
      int16_t ret;

      ret = OSIRIS_loaded_modules[dll_id].CallInstanceEvent(0, instance, event, data);
      if (aux_event != -1) {
        // call child event
        ret = OSIRIS_loaded_modules[dll_id].CallInstanceEvent(0, instance, aux_event, data);
      }

      return (bool)((ret & CONTINUE_DEFAULT) != 0);
    }
  }

  return true;
}

//	Osiris_CallTriggerEvent
//	Purpose:
//		Triggers an event for a trigger script.  Returns true if the default action should continue
//	to process.
bool Osiris_CallTriggerEvent(int trignum, int event, tOSIRISEventInfo *ei) {
  if ((Game_mode & GM_MULTI) && (Netgame.local_role != LR_SERVER)) {
    // no scripts for a client!
    return true;
  }

  if (!(Osiris_event_mask & OEM_TRIGGERS))
    return true; // trigger events are disabled

  if (!Osiris_IsEventEnabled(event))
    return true;

  if (trignum < 0 || trignum >= Num_triggers)
    return true;

  if (!tOSIRISCurrentLevel.level_loaded)
    return true;

  int dll_id, script_id;
  int aux_event = -1;
  void *instance;

  if (event == EVT_AI_NOTIFY) {
    switch (ei->evt_ai_notify.notify_type) {
    case AIN_GOAL_COMPLETE:
      aux_event = EVT_AIN_GOALCOMPLETE;
      break;
    case AIN_GOAL_INVALID:
      aux_event = EVT_AIN_GOALFAIL;
      break;
    case AIN_GOAL_FAIL:
      aux_event = EVT_AIN_GOALFAIL;
      break;
    case AIN_GOAL_ERROR:
      aux_event = EVT_AIN_GOALFAIL;
      break;
    }
  }

  dll_id = tOSIRISCurrentLevel.dll_id;
  script_id = Triggers[trignum].osiris_script.script_id;
  instance = Triggers[trignum].osiris_script.script_instance;

  if (instance) {
    int16_t ret;
    ret = OSIRIS_loaded_modules[dll_id].CallInstanceEvent(script_id, instance, event, ei);

    if (aux_event != -1) {
      // call child event
      ret = OSIRIS_loaded_modules[dll_id].CallInstanceEvent(script_id, instance, aux_event, ei);
    }

    return (bool)((ret & CONTINUE_DEFAULT) != 0);
  }

  return true;
}

//	Osiris_EnableEvents
//	Purpose:
//		Enables the passed in mask of event types to be called
void Osiris_EnableEvents(uint8_t mask) { Osiris_event_mask |= mask; }

//	Osiris_DisableEvents
//	Purpose:
//		Disables the passed in mask of event types
void Osiris_DisableEvents(uint8_t mask) { Osiris_event_mask &= ~mask; }

//	Osiris_DisableCreateEvents
//	Purpose:
//		Disables any events involved when an object is created.  This is to be used for
//	Loading games/viewing demos, as so not to re-initialize good data.
void Osiris_DisableCreateEvents(void) { Osiris_create_events_disabled = true; }

//	Osiris_EnablesCreateEvents
//	Purpose:
//		Enables any events involved when an object is created.  This is to be used for
//	Loading games/viewing demos, as so not to re-initialize good data. (call when done with
// Osiris_DisableCreateEvents())
void Osiris_EnableCreateEvents(void) { Osiris_create_events_disabled = false; }

//	Osiris_IsEventEnabled
//	Purpose:
//		Returns true if the event is allowed to be called
bool Osiris_IsEventEnabled(int event) {
  switch (event) {
  case EVT_CREATED:
  case EVT_AI_INIT:
    return !Osiris_create_events_disabled;
    break;
  default:
    return true;
    break;
  }
  return true;
}

//	Osiris_CallEvent
//	Purpose:
//		Triggers an event for an object.  Pass in the event number and the associated
//	structure of data.  All events will be chained through the associated scripts of the
//	object (as long as they are available) in the order: custom script, level script,
//	mission script, and finally it's default script.  The chain breaks if one of the scripts
//	returns false on the call to their CallInstanceEvent().
bool Osiris_CallEvent(object *obj, int event, tOSIRISEventInfo *data) {
  if ((Game_mode & GM_MULTI) && (Netgame.local_role != LR_SERVER)) {
    // no scripts for a client!
    return true;
  }

  if (!(Osiris_event_mask & OEM_OBJECTS))
    return true; // object events are disabled

  if (!Osiris_IsEventEnabled(event))
    return true;

  // make sure the object is valid
  if (!CANHAVEANYSCRIPT(obj)) {
    // invalid object
    return true;
  }

  if (!obj->osiris_script) {
    // mprintf(0,"OSIRIS: Unhandled event (%d) for object id=%d\n",event,obj->id);
    return true; // no script for this object...hmm
  }

  int dll_id;
  int aux_event = -1; // event value
  tOSIRISScript *os;
  int16_t ret = CONTINUE_CHAIN | CONTINUE_DEFAULT;

  if (event == EVT_AI_NOTIFY) {
    switch (data->evt_ai_notify.notify_type) {
    case AIN_OBJ_KILLED:
      aux_event = EVT_AIN_OBJKILLED;
      break;
    case AIN_SEE_TARGET:
      aux_event = EVT_AIN_SEEPLAYER;
      break;
    case AIN_WHIT_OBJECT:
      aux_event = EVT_AIN_WHITOBJECT;
      break;
    case AIN_GOAL_COMPLETE:
      aux_event = EVT_AIN_GOALCOMPLETE;
      break;
    case AIN_GOAL_INVALID:
      aux_event = EVT_AIN_GOALFAIL;
      break;
    case AIN_GOAL_FAIL:
      aux_event = EVT_AIN_GOALFAIL;
      break;
    case AIN_GOAL_ERROR:
      aux_event = EVT_AIN_GOALFAIL;
      break;
    case AIN_MELEE_HIT:
      aux_event = EVT_AIN_MELEE_HIT;
      break;
    case AIN_MELEE_ATTACK_FRAME:
      aux_event = EVT_AIN_MELEE_ATTACK_FRAME;
      break;
    case AIN_MOVIE_START:
      aux_event = EVT_AIN_MOVIE_START;
      break;
    case AIN_MOVIE_END:
      aux_event = EVT_AIN_MOVIE_END;
      break;
    }
  }

  os = obj->osiris_script;
  data->me_handle = obj->handle;

  if (os->custom_script.script_instance) {
    dll_id = os->custom_script.DLLID;

    ASSERT(OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE);
    if (aux_event != -1) {
      // call the child event
      OSIRIS_loaded_modules[dll_id].CallInstanceEvent(os->custom_script.script_id, os->custom_script.script_instance,
                                                      aux_event, data);
    }

    if (OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE) {

      // call the event
      ret = OSIRIS_loaded_modules[dll_id].CallInstanceEvent(os->custom_script.script_id,
                                                            os->custom_script.script_instance, event, data);
    }
  }

  if ((ret & CONTINUE_CHAIN) && os->level_script.script_instance) {
    dll_id = os->level_script.DLLID;

    ASSERT(OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE);
    if (OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE) {

      if (aux_event != -1) {
        // call the child event
        OSIRIS_loaded_modules[dll_id].CallInstanceEvent(os->level_script.script_id, os->level_script.script_instance,
                                                        aux_event, data);
      }

      // call the event
      ret = OSIRIS_loaded_modules[dll_id].CallInstanceEvent(os->level_script.script_id,
                                                            os->level_script.script_instance, event, data);
    }
  }

  if ((ret & CONTINUE_CHAIN) && os->mission_script.script_instance) {
    dll_id = os->mission_script.DLLID;

    ASSERT(OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE);
    if (OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE) {
      if (aux_event != -1) {
        // call the child event
        OSIRIS_loaded_modules[dll_id].CallInstanceEvent(os->mission_script.script_id,
                                                        os->mission_script.script_instance, aux_event, data);
      }

      // call the event
      ret = OSIRIS_loaded_modules[dll_id].CallInstanceEvent(os->mission_script.script_id,
                                                            os->mission_script.script_instance, event, data);
    }
  }

  if ((ret & CONTINUE_CHAIN) && os->default_script.script_instance) {
    dll_id = os->default_script.DLLID;

    ASSERT(OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE);
    if (OSIRIS_loaded_modules[dll_id].flags & OSIMF_INUSE) {

      if (aux_event != -1) {
        // call the child event
        OSIRIS_loaded_modules[dll_id].CallInstanceEvent(os->default_script.script_id,
                                                        os->default_script.script_instance, aux_event, data);
      }

      // call the event
      ret = OSIRIS_loaded_modules[dll_id].CallInstanceEvent(os->default_script.script_id,
                                                            os->default_script.script_instance, event, data);
    }
  }

  return (bool)((ret & CONTINUE_DEFAULT) != 0);
}

#define MAX_OSIRIS_TIMERS 64
#define OITF_USED 0x0001
#define OITF_REPEATCALL 0x0002
#define OITF_TRIGGERTIMER 0x0004
#define OITF_LEVELTIMER 0x0008
#define OITF_CANCELONDEAD 0x0010
struct tOSIRISINTERNALTIMER {
  uint16_t flags;
  union {
    int objhandle;
    int trignum;
  };
  int id;
  int repeat_count;
  int objhandle_detonator;
  int handle;
  float timer_interval;
  float timer_next_signal;
  float timer_end;
};
tOSIRISINTERNALTIMER OsirisTimers[MAX_OSIRIS_TIMERS];

static inline int FORM_HANDLE(int counter, int slot) { return (((counter & 0xFFFFFF) << 8) | (slot & 0xFF)); }

static inline int GET_SLOT(int handle) { return (handle & 0xFF); }

int Osiris_timer_counter = 0;

//	Osiris_ProcessTimers
//	Purpose:
//		This function checks all timers currently running, if any need to be signaled it signals them.
void Osiris_ProcessTimers(void) {
  object *obj;
  bool signal, kill;
  tOSIRISEventInfo ei;

  for (int i = 0; i < MAX_OSIRIS_TIMERS; i++) {
    signal = false;
    kill = false;

    if (OsirisTimers[i].flags & OITF_USED) {

      if (OsirisTimers[i].flags & OITF_CANCELONDEAD) {
        obj = ObjGet(OsirisTimers[i].objhandle_detonator);
        if (!obj || (obj->type == OBJ_GHOST) ||
            (obj->type == OBJ_PLAYER && Players[obj->id].flags & (PLAYER_FLAGS_DYING | PLAYER_FLAGS_DEAD))) {
          // the detontator died...cancel the timer!
          OsirisTimers[i].flags &= ~OITF_USED;
          OsirisTimers[i].repeat_count = 0;

          tOSIRISEventInfo ei;
          ei.evt_timercancel.detonated = 1;
          ei.evt_timercancel.handle = OsirisTimers[i].handle;

          if (OsirisTimers[i].flags & OITF_TRIGGERTIMER) {
            Osiris_CallTriggerEvent(OsirisTimers[i].trignum, EVT_TIMERCANCEL, &ei);
          } else if (OsirisTimers[i].flags & OITF_LEVELTIMER) {
            Osiris_CallLevelEvent(EVT_TIMERCANCEL, &ei);
          } else {
            object *obj = ObjGet(OsirisTimers[i].objhandle);
            if (obj)
              Osiris_CallEvent(obj, EVT_TIMERCANCEL, &ei);
          }

          mprintf(0, "OSIRIS TIMER: Cancelling Timer (%d/%d)\n", OsirisTimers[i].handle, i);
          continue;
        }
      }

      if (OsirisTimers[i].timer_next_signal <= Gametime) {
        // this timer needs to be signaled
        signal = true;

        if (OsirisTimers[i].flags & OITF_REPEATCALL) {
          // this is a repeater
          if (OsirisTimers[i].repeat_count != -1) {
            // it has a finite repeat
            OsirisTimers[i].repeat_count--;
            if (OsirisTimers[i].repeat_count <= 0) {
              // remove the timer
              kill = true;
              OsirisTimers[i].repeat_count = 0;
            } else {
              // adjust for next signal
              OsirisTimers[i].timer_next_signal += OsirisTimers[i].timer_interval;
            }
          } else {
            // infinite repeat
            OsirisTimers[i].timer_next_signal += OsirisTimers[i].timer_interval;
          }
        }
      }

      if ((!((OsirisTimers[i].flags & OITF_REPEATCALL) && (OsirisTimers[i].repeat_count == -1))) &&
          OsirisTimers[i].timer_end <= Gametime) {

        // this timer has expired, remove it
        kill = true;
        // signal timer
        signal = true;
      }

      if (signal) {
        ei.evt_timer.game_time = Gametime;
        ei.evt_timer.id = OsirisTimers[i].id;
      }

      // this timer is currently in use, see what kind of trigger it is
      if (OsirisTimers[i].flags & OITF_TRIGGERTIMER) {
        // trigger timer
        if (signal) {
          Osiris_CallTriggerEvent(OsirisTimers[i].trignum, EVT_TIMER, &ei);
        }
      } else if (OsirisTimers[i].flags & OITF_LEVELTIMER) {
        // level timer
        if (signal) {
          Osiris_CallLevelEvent(EVT_TIMER, &ei);
        }
      } else {
        // object timer
        obj = ObjGet(OsirisTimers[i].objhandle);
        if (obj) {
          // process this object timer
          if (signal) {
            Osiris_CallEvent(obj, EVT_TIMER, &ei);
          }
        } else {
          // this object no longer exists, remove the timer
          kill = true;
        }
      }

      if (kill)
        OsirisTimers[i].flags &= ~OITF_USED;
    }
  }
}

//	Osiris_ResetAllTimers
//	Purpose:
//		Flushes all the timers
void Osiris_ResetAllTimers(void) {
  for (int i = 0; i < MAX_OSIRIS_TIMERS; i++) {
    OsirisTimers[i].flags = 0;
  }
}

//	Osiris_CreateTimer
//	Pupose:
//		Adds a timer to the list to be processed.  You'll receive a EVT_TIMER when the timer is signaled.
//	Returns an id to the timer, which can be used to cancel a timer. -1 on error.
int Osiris_CreateTimer(tOSIRISTIMER *ot) {
  // find an empty timer
  int i;
  for (i = 0; i < MAX_OSIRIS_TIMERS; i++) {
    if (!(OsirisTimers[i].flags & OITF_USED)) {
      break;
    }
  }

  if (i >= MAX_OSIRIS_TIMERS)
    return -1; // no more slots available

  // clear out the slots
  OsirisTimers[i].flags = 0;
  OsirisTimers[i].id = 0;
  OsirisTimers[i].objhandle = 0;
  OsirisTimers[i].repeat_count = 0;
  OsirisTimers[i].timer_end = 0;
  OsirisTimers[i].timer_interval = 0;
  OsirisTimers[i].timer_next_signal = 0;
  OsirisTimers[i].handle = 0;
  OsirisTimers[i].objhandle_detonator = OBJECT_HANDLE_NONE;

  // fill in the data
  OsirisTimers[i].flags |= OITF_USED;
  OsirisTimers[i].id = ot->id;
  OsirisTimers[i].timer_end = ot->timer_interval + Gametime;
  OsirisTimers[i].timer_interval = ot->timer_interval;
  OsirisTimers[i].timer_next_signal = ot->timer_interval + Gametime;

  if (ot->flags & OTF_TRIGGER) {
    OsirisTimers[i].trignum = ot->trigger_number;
    OsirisTimers[i].flags |= OITF_TRIGGERTIMER;
  } else if (ot->flags & OTF_LEVEL) {
    OsirisTimers[i].flags |= OITF_LEVELTIMER;
  } else {
    OsirisTimers[i].objhandle = ot->object_handle;
  }

  if (ot->flags & OTF_REPEATER) {
    // this is a repeater timer
    OsirisTimers[i].flags |= OITF_REPEATCALL;
    OsirisTimers[i].repeat_count = ot->repeat_count;
    if (ot->repeat_count != -1) {
      OsirisTimers[i].timer_end = (ot->timer_interval * ot->repeat_count) + Gametime;
    }
  }

  if (ot->flags & OTF_CANCELONDEAD) {
    // there is a detonator
    OsirisTimers[i].flags |= OITF_CANCELONDEAD;
    OsirisTimers[i].objhandle_detonator = ot->object_handle_detonator;
  }

  // create a handle
  Osiris_timer_counter++;
  int handle = FORM_HANDLE(Osiris_timer_counter, i);

  OsirisTimers[i].handle = handle;

  return handle;
}

//	Osiris_CancelTimer
//	Purpose:
//		Cancels a timer that's in use, given its handle
void Osiris_CancelTimer(int handle) {
  int slot;

  slot = GET_SLOT(handle);

  if (slot < 0 || slot >= MAX_OSIRIS_TIMERS)
    return;

  if (OsirisTimers[slot].handle != handle) // not the same timer
    return;

  OsirisTimers[slot].flags &= ~OITF_USED;

  tOSIRISEventInfo ei;
  ei.evt_timercancel.detonated = 0;
  ei.evt_timercancel.handle = handle;

  if (OsirisTimers[slot].flags & OITF_TRIGGERTIMER) {
    Osiris_CallTriggerEvent(OsirisTimers[slot].trignum, EVT_TIMERCANCEL, &ei);
  } else if (OsirisTimers[slot].flags & OITF_LEVELTIMER) {
    Osiris_CallLevelEvent(EVT_TIMERCANCEL, &ei);
  } else {
    object *obj = ObjGet(OsirisTimers[slot].objhandle);
    if (obj)
      Osiris_CallEvent(obj, EVT_TIMERCANCEL, &ei);
  }
}

//	Osiris_GetTimerHandle
//	Purpose:
//		Gets the handle for the timer with the specified id
int Osiris_GetTimerHandle(int id) {
  // Look for timer with this ID
  for (int i = 0; i < MAX_OSIRIS_TIMERS; i++)
    if ((OsirisTimers[i].flags & OITF_USED) && (OsirisTimers[i].id == id)) {
      return OsirisTimers[i].handle;
    }

  return 0;
}

//	Osiris_CancelTimer
//	Purpose:
//		Cancels a timer given its ID
void Osiris_CancelTimerID(int id) {
  // Look for timer with this ID
  for (int i = 0; i < MAX_OSIRIS_TIMERS; i++)
    if ((OsirisTimers[i].flags & OITF_USED) && (OsirisTimers[i].id == id)) {
      Osiris_CancelTimer(OsirisTimers[i].handle);
      return;
    }
}

//	Osiris_TimerExists
//	Purpose:
//		Returns true if the timer is valid
uint8_t Osiris_TimerExists(int handle) {
  int id = GET_SLOT(handle);

  if (id < 0 || id >= MAX_OSIRIS_TIMERS)
    return false;

  if (!(OsirisTimers[id].flags & OITF_USED))
    return false;

  if (OsirisTimers[id].handle != handle) // not the same timer
    return false;

  return true;
}

//	Osiris_TimerTimeRemaining
//	Purpose:
//		Returns the amount of time remaining on the specified timer
float Osiris_TimerTimeRemaining(int handle) {
  int id = GET_SLOT(handle);

  if (id < 0 || id >= MAX_OSIRIS_TIMERS)
    return -1.0;

  if (!(OsirisTimers[id].flags & OITF_USED))
    return -1.0;

  if (OsirisTimers[id].handle != handle) // not the same timer
    return -1.0;

  return (OsirisTimers[id].timer_end - Gametime);
}

#define OSIRIS_SYSTEM_FILEVERSION 0x01
//	Osiris_SaveSystemState
//	Purpose:
//		Saves the current state of the system (not the scripts!) to file
void Osiris_SaveSystemState(CFILE *file) {
  int i;
  int save_start = cftell(file);
  int checksum_pos;

  cf_WriteString(file, "OSIRIS");

  cf_WriteByte(file, OSIRIS_SYSTEM_FILEVERSION);

  checksum_pos = cftell(file);
  cf_WriteInt(file, 0); // this will be # of bytes writtens

  cf_WriteInt(file, Osiris_timer_counter);

  // save out the timer state
  for (i = 0; i < MAX_OSIRIS_TIMERS; i++) {
    if (OsirisTimers[i].flags & OITF_USED) {
      // write out information about this timer
      cf_WriteByte(file, 1); // timer exists
      cf_WriteShort(file, OsirisTimers[i].flags);
      cf_WriteInt(file, OsirisTimers[i].id);
      cf_WriteInt(file, OsirisTimers[i].repeat_count);
      cf_WriteFloat(file, OsirisTimers[i].timer_interval);
      cf_WriteFloat(file, OsirisTimers[i].timer_next_signal);
      cf_WriteFloat(file, OsirisTimers[i].timer_end);
      cf_WriteInt(file, OsirisTimers[i].objhandle);
      cf_WriteInt(file, OsirisTimers[i].objhandle_detonator);
      cf_WriteInt(file, OsirisTimers[i].handle);
    } else {
      cf_WriteByte(file, 0);
    }
  }

  // auto-save script mallocs
  Osiris_SaveMemoryChunks(file);

  // go through all loaded DLLs and save states
  // this is touchy, we need to make sure all DLL modules that are currently
  // loaded, be loaded when we restore states.  As a safety precaution, the Global
  // state will be saved as the following (an array of the following):
  // byte offset	size		description
  //	0			1			Number of loaded DLLs
  //		...
  //	+1			1			String length of module name
  //	+2			x			Name of the module
  //	+2+x		4			Size of global data stored
  //	+6+x		y			global data
  //		...

  // first count up the number of loaded modules
  int loaded_module_count = 0;
  for (i = 0; i < MAX_LOADED_MODULES; i++) {
    if (OSIRIS_loaded_modules[i].flags & OSIMF_INUSE) {
      // this is a loaded module
      loaded_module_count++;
    }
  }

  cf_WriteByte(file, loaded_module_count);

  for (i = 0; i < MAX_LOADED_MODULES; i++) {
    if (OSIRIS_loaded_modules[i].flags & OSIMF_INUSE) {
      // get the length of the module name
      int module_name_length = strlen(OSIRIS_loaded_modules[i].module_name) + 1;
      cf_WriteByte(file, module_name_length);
      cf_WriteBytes((uint8_t *)OSIRIS_loaded_modules[i].module_name, module_name_length, file);

      // now here is the tricky part, save the current fileposition, write a dummy 0 int
      int saved_file_pos = cftell(file);
      cf_WriteInt(file, 0);

      int global_size = OSIRIS_loaded_modules[i].SaveRestoreState(file, 1);
      int restore_file_pos = cftell(file);

      // now jump back and fill in the correct global data size, than jump back
      cfseek(file, saved_file_pos, SEEK_SET);
      cf_WriteInt(file, global_size);
      cfseek(file, restore_file_pos, SEEK_SET);
    }
  }

  // Go through all the objects and call their EVT_SAVESTATE (if they have a script)
  Osiris_SaveOMMS(file);

  tOSIRISEventInfo ei;
  ei.evt_savestate.fileptr = (void *)file;

  Osiris_CallLevelEvent(EVT_SAVESTATE, &ei);

  for (i = 0; i <= Num_triggers; i++) {
    Osiris_CallTriggerEvent(i, EVT_SAVESTATE, &ei);
  }

  for (i = 0; i < MAX_OBJECTS; i++) {
    if (Objects[i].type != OBJ_NONE && Objects[i].osiris_script) {
      Osiris_CallEvent(&Objects[i], EVT_SAVESTATE, &ei);
    }
  }

  int save_end = cftell(file);

  mprintf(0, "Osiris: Save state, %d bytes written\n", save_end - save_start);

  cfseek(file, checksum_pos, SEEK_SET);
  cf_WriteInt(file, save_end - save_start); // write out 'checksum'
  cfseek(file, save_end, SEEK_SET);
}

//	Osiris_RestoreSystemState
//	Purpose:
//		Restore the state of the system from file (does not restore scripts!)
bool Osiris_RestoreSystemState(CFILE *file) {
  int i;
  int restore_start = cftell(file);
  int num_bytes_to_be_restored;

  char tag[256];
  cf_ReadString(tag, 256, file);
  if (strcmp(tag, "OSIRIS")) {
    // Things aren't right...our tag is not here
    mprintf(0, "Missing OSIRIS tag\n");
    Int3();
    return false;
  }

  uint8_t version = (uint8_t)cf_ReadByte(file);

  num_bytes_to_be_restored = cf_ReadInt(file);

  Osiris_timer_counter = cf_ReadInt(file);

  // restore timer state
  for (i = 0; i < MAX_OSIRIS_TIMERS; i++) {
    if (cf_ReadByte(file)) {
      // timer is used
      OsirisTimers[i].flags = cf_ReadShort(file);
      OsirisTimers[i].id = cf_ReadInt(file);
      OsirisTimers[i].repeat_count = cf_ReadInt(file);
      OsirisTimers[i].timer_interval = cf_ReadFloat(file);
      OsirisTimers[i].timer_next_signal = cf_ReadFloat(file);
      OsirisTimers[i].timer_end = cf_ReadFloat(file);
      OsirisTimers[i].objhandle = cf_ReadInt(file);
      OsirisTimers[i].objhandle_detonator = cf_ReadInt(file);
      OsirisTimers[i].handle = cf_ReadInt(file);

    } else {
      OsirisTimers[i].flags = 0;
    }
  }

  // restore auto-saved memory chunks
  Osiris_RestoreMemoryChunks(file);

  // now we need to restore the global state of the DLLs
  // byte offset	size		description
  //	0			1			Number of loaded DLLs
  //		...
  //	+1			1			String length of module name
  //	+2			x			Name of the module
  //	+2+x		4			Size of global data stored
  //	+6+x		y			global data
  //		...
  int loaded_module_count = 0;
  for (i = 0; i < MAX_LOADED_MODULES; i++) {
    if (OSIRIS_loaded_modules[i].flags & OSIMF_INUSE) {
      // this is a loaded module
      loaded_module_count++;
    }
  }

  int read_module_count = cf_ReadByte(file);
  if (read_module_count != loaded_module_count) {
    // the current number of loaded module does not equal the number of modules that were loaded
    // when the state was saved.  This means that things are not going to be restored exactly for
    // sure.  We'll skip over those that are not loaded.  We're int3 here because I want to know
    // when this happens.
    mprintf(0,
             "OSIRIS: Restoring global state, the number of loaded modules is not the same as the restored count (%d "
             "vs. %d)\n",
             loaded_module_count, read_module_count);
    if (Demo_flags != DF_PLAYBACK) {
      Int3();
    }
  }

  // now go through all the read module count DLLs and see if that DLL is currently loaded
  for (i = 0; i < read_module_count; i++) {
    char read_module_name[_MAX_PATH];
    int read_module_namelen;
    int read_module_globalsize;
    int next_file_pos;
    int module_handle;

    read_module_namelen = cf_ReadByte(file);
    cf_ReadBytes((uint8_t *)read_module_name, read_module_namelen, file);
    read_module_name[read_module_namelen] = '\0';

    read_module_globalsize = cf_ReadInt(file);
    next_file_pos = cftell(file) + read_module_globalsize;

    module_handle = Osiris_FindLoadedModule(read_module_name);
    if (module_handle == -1) {
      mprintf(0, "OSIRIS: The module (%s) was not found while trying to restore the state\n", read_module_name);
      if (Demo_flags != DF_PLAYBACK) {
        Int3();
      }
    } else {
      OSIRIS_loaded_modules[module_handle].SaveRestoreState(file, 0);
    }

    cfseek(file, next_file_pos, SEEK_SET); // this ensures that we keep track in the file, even if the
                                           // DLL reads too much/little
  }

  Osiris_RestoreOMMS(file);

  // Go through all the objects and call their EVT_RESTORESTATE (if they have a script)
  tOSIRISEventInfo ei;

  ei.evt_restorestate.fileptr = (void *)file;

  Osiris_CallLevelEvent(EVT_RESTORESTATE, &ei);

  for (i = 0; i <= Num_triggers; i++) {
    Osiris_CallTriggerEvent(i, EVT_RESTORESTATE, &ei);
  }

  for (i = 0; i < MAX_OBJECTS; i++) {
    if (Objects[i].type != OBJ_NONE && Objects[i].osiris_script) {
      Osiris_CallEvent(&Objects[i], EVT_RESTORESTATE, &ei);
    }
  }

  int restore_end = cftell(file);

  mprintf(0, "Osiris: Restore state, %d bytes read\n", restore_end - restore_start);

  ASSERT(num_bytes_to_be_restored == (restore_end - restore_start));
  if (num_bytes_to_be_restored == (restore_end - restore_start))
    return true;
  else {
    if (Demo_flags == DF_PLAYBACK) {
      cfseek(file, restore_start + num_bytes_to_be_restored, SEEK_SET);
      return true;
    }
  }

  return false;
}

struct tOSIRISMEMNODE {
  tOSIRISMEMCHUNK chunk_id;
  void *memory;
  tOSIRISMEMNODE *next;
};

tOSIRISMEMNODE *Osiris_mem_root;

//	Osiris_InitMemoryManager
//	Purpose:
//		Initializes the memory manager for the scripts, for buffers that the scripts want
//	automatically restored/save.
void Osiris_InitMemoryManager(void) {
  Osiris_mem_root = NULL;
  atexit(Osiris_CloseMemoryManager);
}

//	Osiris_CloseMemoryManager
//	Purpose:
//		Shuts down the Osiris memory manager, freeing any unfreed memory
void Osiris_CloseMemoryManager(void) {
  tOSIRISMEMNODE *curr, *next;
  curr = Osiris_mem_root;
  while (curr) {
    next = curr->next;
    if (curr->memory)
      mem_free(curr->memory);
    mem_free(curr);
    curr = next;
  }
  Osiris_mem_root = NULL;
}

//	Osiris_AllocateMemory
//	Purpose:
//		Allocates a chunk of memory to be associated with a script.  It will automatically
//	save this memory to disk on game save, and will pass the pointer to this memory on EVT_RESTORE
void *Osiris_AllocateMemory(tOSIRISMEMCHUNK *mc) {
  // find the end of the list
  tOSIRISMEMNODE *curr, **error_node;

  if (!Osiris_mem_root) {
    // it'll be the first node
    Osiris_mem_root = (tOSIRISMEMNODE *)mem_malloc(sizeof(tOSIRISMEMNODE));
    if (!Osiris_mem_root)
      return NULL;

    curr = Osiris_mem_root;
    error_node = &Osiris_mem_root;
  } else {
    // traverse to the tail
    curr = Osiris_mem_root;
    while (curr->next) {
      curr = curr->next;
    }

    curr->next = (tOSIRISMEMNODE *)mem_malloc(sizeof(tOSIRISMEMNODE));
    if (!curr->next)
      return NULL;
    error_node = &curr->next;
    curr = curr->next;
  }

  // setup the data
  curr->next = NULL;
  curr->memory = mem_malloc(mc->size);
  if (!curr->memory) {
    // ack...out of memory
    *error_node = NULL; // make sure we cut off the list
    mem_free(curr);
    return NULL;
  }
  memcpy(&curr->chunk_id, mc, sizeof(tOSIRISMEMCHUNK));

  return curr->memory;
}

//	Osiris_FreeMemory
//	Purpose:
//		Frees a chunk of memory that was allocated by Osiris_AllocateMemory().
void Osiris_FreeMemory(void *mem_ptr) {
  tOSIRISMEMNODE *tofree;

  if (!Osiris_mem_root)
    return;

  if (Osiris_mem_root->memory == mem_ptr) {
    // free the base pointer
    tofree = Osiris_mem_root;
    Osiris_mem_root = Osiris_mem_root->next;
  } else {
    // traverse until we find the node
    tOSIRISMEMNODE *curr;
    curr = Osiris_mem_root;
    bool done = false;
    tofree = NULL;
    while (!done) {
      if (!curr->next) {
        done = true; // we didn't find it
      } else if (curr->next->memory == mem_ptr) {
        // this is the guy to free
        tofree = curr->next;
        done = true;
      } else
        curr = curr->next;
    }

    if (tofree) {
      // ok there is a value to free, pull the list back one from curr on
      curr->next = tofree->next;
    } else {
      // nothing to do
      return;
    }
  }

  // free the memory associated with tofree
  if (tofree->memory)
    mem_free(tofree->memory);
  mem_free(tofree);
}

bool compareid(tOSIRISSCRIPTID *sid, tOSIRISSCRIPTID *oid) {
  if (sid->type == oid->type) {
    if (sid->type == LEVEL_SCRIPT)
      return true;
    if (sid->type == TRIGGER_SCRIPT && sid->triggernum == oid->triggernum)
      return true;
    if (sid->type == OBJECT_SCRIPT && sid->objhandle == oid->objhandle)
      return true;
  }
  return false;
}

//	Osiris_FreeMemoryForScript
//	Purpose:
//		Frees all memory allocated for a given script
void Osiris_FreeMemoryForScript(tOSIRISSCRIPTID *sid) {
  if (!Osiris_mem_root)
    return;
  bool done = false;
  tOSIRISMEMNODE *curr, *next, *prev;
  curr = Osiris_mem_root;
  prev = NULL;

  while (!done) {
    if (!curr) {
      done = true;
    } else {
      next = curr->next;

      if (compareid(sid, &curr->chunk_id.my_id)) {
        // this has to go
        if (curr == Osiris_mem_root) {
          // removing root
          Osiris_mem_root = curr->next;

          if (curr->memory)
            mem_free(curr->memory);
          mem_free(curr);
        } else {
          // remove from list and pull back
          prev->next = curr->next;

          if (curr->memory)
            mem_free(curr->memory);
          mem_free(curr);
        }
      } else {
        prev = curr;
      }

      curr = next;
    }
  }
}

//	Osiris_SaveMemoryChunks
//	Purpose:
//		Saves out the 'auto manage' script memory to file
void Osiris_SaveMemoryChunks(CFILE *file) {
  tOSIRISMEMNODE *curr;
  curr = Osiris_mem_root;

  while (curr) {
    cf_WriteInt(file, curr->chunk_id.size);
    cf_WriteByte(file, curr->chunk_id.my_id.type);
    switch (curr->chunk_id.my_id.type) {
    case OBJECT_SCRIPT:
      cf_WriteInt(file, curr->chunk_id.my_id.objhandle);
      break;
    case TRIGGER_SCRIPT:
      cf_WriteInt(file, curr->chunk_id.my_id.triggernum);
      break;
    case LEVEL_SCRIPT:
      break;
    }
    cf_WriteInt(file, curr->chunk_id.id);
    cf_WriteBytes((uint8_t *)curr->memory, curr->chunk_id.size, file);
    curr = curr->next;
  }

  cf_WriteInt(file, 0);
}

//	Osiris_RestoreMemoryChunks
//	Purpose:
//		Restores the 'auto manage' from file, calls the EVT_MEMRESTORE
void Osiris_RestoreMemoryChunks(CFILE *file) {
  tOSIRISMEMNODE *curr;
  tOSIRISMEMNODE *memchunk;

  // Osiris_CloseMemoryManager(); We might not want to close this down...due to some objects don't get destroyed
  // Osiris_mem_root = NULL;
  curr = Osiris_mem_root;
  if (curr) {
    while (curr->next) {
      curr = curr->next;
    }
  }

  bool done = false;
  while (!done) {
    int size = cf_ReadInt(file);
    if (size == 0) {
      // we're done
      done = true;
    } else {
      // handle this node
      memchunk = (tOSIRISMEMNODE *)mem_malloc(sizeof(tOSIRISMEMNODE));
      if (!memchunk) {
        Error("Out of memory");
      }

      memchunk->chunk_id.size = size;
      memchunk->chunk_id.my_id.type = (script_type)cf_ReadByte(file);
      switch (memchunk->chunk_id.my_id.type) {
      case OBJECT_SCRIPT:
        memchunk->chunk_id.my_id.objhandle = cf_ReadInt(file);
        break;
      case TRIGGER_SCRIPT:
        memchunk->chunk_id.my_id.triggernum = cf_ReadInt(file);
        break;
      case LEVEL_SCRIPT:
        break;
      }
      memchunk->chunk_id.id = cf_ReadInt(file);
      memchunk->memory = mem_malloc(memchunk->chunk_id.size);
      if (!memchunk->memory) {
        Error("Out of memory");
      }
      cf_ReadBytes((uint8_t *)memchunk->memory, memchunk->chunk_id.size, file);
      memchunk->next = NULL;

      // ok, the node is setup, add it to the linked list
      if (!Osiris_mem_root) {
        Osiris_mem_root = curr = memchunk;
      } else {
        curr->next = memchunk;
        curr = curr->next;
      }

      tOSIRISEventInfo ei;
      ei.evt_memrestore.id = memchunk->chunk_id.id;
      ei.evt_memrestore.memory_ptr = memchunk->memory;

      // send an event to the object
      switch (memchunk->chunk_id.my_id.type) {
      case OBJECT_SCRIPT: {
        object *obj = ObjGet(memchunk->chunk_id.my_id.objhandle);
        if (obj) {
          Osiris_CallEvent(obj, EVT_MEMRESTORE, &ei);
        }
      } break;
      case TRIGGER_SCRIPT:
        Osiris_CallTriggerEvent(memchunk->chunk_id.my_id.triggernum, EVT_MEMRESTORE, &ei);
        break;
      case LEVEL_SCRIPT:
        Osiris_CallLevelEvent(EVT_MEMRESTORE, &ei);
        break;
      };
    }
  }
}

void _extractscript(char *script, char *tempfilename) { cf_CopyFile(tempfilename, script); }

int _getfreeextractslot(void) {
  // find a free slot
  for (int q = 0; q < MAX_LOADED_MODULES; q++) {
    if (!(OSIRIS_Extracted_scripts[q].flags & OESF_USED)) {
      return q;
    }
  }
  return -1;
}

void _clearextractedall(void) { Osiris_ClearExtractedScripts(false); }

int Osiris_ExtractScriptsFromHog(int library_handle, bool is_mission_hog) {
  if (library_handle == 0)
    return 0;

  mprintf(0, "OSIRIS: Extracting Scripts From Hog\n");

  char filename[_MAX_PATH], temp_filename[_MAX_PATH];
  char tempdir[_MAX_PATH], temp_file[_MAX_PATH], temp_fileext[_MAX_EXT];
  char temp_realname[_MAX_PATH];

  if (!OSIRIS_Extracted_script_dir) {
    strcpy(tempdir, Descent3_temp_directory);
    OSIRIS_Extracted_script_dir = mem_strdup(tempdir);
    if (!OSIRIS_Extracted_script_dir)
      Error("Out of memory");
  } else {
    strcpy(tempdir, OSIRIS_Extracted_script_dir);
  }

  int count = 0;

  const char *script_extension;
#if defined(MACOSX)
  script_extension = "*.dylib";
#elif defined(__LINUX__)
  script_extension = "*.so";
#elif defined(WIN32)
  script_extension = "*.dll";
#else
  #error Unsupported platform!
#endif

  int index;
  index = _getfreeextractslot();
  if (index == -1) {
    mprintf(0, "OSIRIS: Out of slots extracting scripts!!!!!!!!\n");
    Int3();
    goto ex_error;
  }

  mprintf(0, "Search started\n");
  if (cf_LibraryFindFirst(library_handle, script_extension, filename)) {

    mprintf(0, "	Found: %s...", filename);
    if (!ddio_GetTempFileName(tempdir, "d3s", temp_filename))
      Int3();
    else {
      ddio_SplitPath(temp_filename, NULL, temp_file, temp_fileext);
      strcat(temp_file, temp_fileext);

      OSIRIS_Extracted_scripts[index].flags = OESF_USED;
      OSIRIS_Extracted_scripts[index].temp_filename = mem_strdup(temp_file);

      ddio_SplitPath(filename, NULL, temp_realname, NULL);
      OSIRIS_Extracted_scripts[index].real_filename = mem_strdup(temp_realname);

      if (is_mission_hog) {
        OSIRIS_Extracted_scripts[index].flags |= OESF_MISSION;
      }

      // extract it out
      mprintf(0, "Extracting...");
      _extractscript(filename, temp_filename);
      mprintf(0, "Done\n");

      count++;

      while (cf_LibraryFindNext(filename)) {
        mprintf(0, "	Found: %s...", filename);

        index = _getfreeextractslot();
        if (index == -1) {
          mprintf(0, "OSIRIS: Out of slots extracting scripts!!!!!!!!\n");
          Int3();
          goto ex_error;
        }

        // generate temp filename
        if (!ddio_GetTempFileName(tempdir, "d3s", temp_filename))
          Int3();
        else {
          ddio_SplitPath(temp_filename, NULL, temp_file, temp_fileext);
          strcat(temp_file, temp_fileext);

          OSIRIS_Extracted_scripts[index].flags = OESF_USED;
          OSIRIS_Extracted_scripts[index].temp_filename = mem_strdup(temp_file);

          ddio_SplitPath(filename, NULL, temp_realname, NULL);
          OSIRIS_Extracted_scripts[index].real_filename = mem_strdup(temp_realname);

          if (is_mission_hog) {
            OSIRIS_Extracted_scripts[index].flags |= OESF_MISSION;
          }

          // extract it out
          mprintf(0, "Extracting...");
          _extractscript(filename, temp_filename);
          mprintf(0, "Done\n");

          count++;
        }
      }
    }
  }

  mprintf(0, "Done Extracting\n");

ex_error:
  cf_LibraryFindClose();

  static bool atex = false;

  if (!atex) {
    atexit(_clearextractedall);
    atex = true;
  }

  return count;
}

void Osiris_ClearExtractedScripts(bool mission_only) {
  mprintf(0, "OSIRIS: Removing Extracted DLLs\n");

  char fullpath[_MAX_PATH];
  if (!OSIRIS_Extracted_script_dir) {
    return;
  }

  for (int i = 0; i < MAX_LOADED_MODULES; i++) {
    if (OSIRIS_Extracted_scripts[i].flags & OESF_USED) {
      if (mission_only && (!(OSIRIS_Extracted_scripts[i].flags & OESF_MISSION)))
        continue;

      ASSERT(OSIRIS_Extracted_scripts[i].temp_filename);
      ASSERT(OSIRIS_Extracted_scripts[i].real_filename);
      if (!(OSIRIS_Extracted_scripts[i].temp_filename && OSIRIS_Extracted_scripts[i].real_filename))
        continue;

      ddio_MakePath(fullpath, OSIRIS_Extracted_script_dir, OSIRIS_Extracted_scripts[i].temp_filename, NULL);
      ddio_DeleteFile(fullpath);

      mem_free(OSIRIS_Extracted_scripts[i].temp_filename);
      mem_free(OSIRIS_Extracted_scripts[i].real_filename);
      OSIRIS_Extracted_scripts[i].temp_filename = NULL;
      OSIRIS_Extracted_scripts[i].real_filename = NULL;
      OSIRIS_Extracted_scripts[i].flags &= ~OESF_USED;
    }
  }

  if (!mission_only) {
    mem_free(OSIRIS_Extracted_script_dir);
    OSIRIS_Extracted_script_dir = NULL;
  }
}

/*
******************************************************************************
Osiris Mission Memory System:
----------------------------
        OMMS is a way to allocate memory that can be shared between scripts and/or modules.  This
memory can also last through level changes.  The basic process of using this system is someone
calls the OMMS_Malloc() to allocate a chunk of memory that is to be shared/global.  This returns
a handle which is to be used by OMMS_Attach(), to get the pointer to the memory, and OMMS_Detach(),
which informs Osiris that you're no longer using the pointer OMMS_Attach() returned. OMMS_Detach()
will NOT free the memory, only OMMS_Free() does that, and it only does that when the reference
count for the memory is at 0 (OMMS_Attach() increments the reference count, OMMS_Detach() decrements
the reference count).  Basically, just surround the code that uses the global memory with a call
to OMMS_Attach() and OMMS_Detach().  ALWAYS make sure that you detach before you leave the event.

// Allocates a block of global memory for this module, of size amount_of_memory.
// unique_identfier is the script provided unique ID which is used throughout.
// script_identifier is the pointer of data provided to the script in it's InitializeDLL function (really the name of
the script)
// Returns -1 if there isn't enough available memory
// Returns -2 if the unique identifier passed in is already used, but the requested amount_of_memory is different
// If the memory has already been allocated, it will return the handle.
OMMSHANDLE OMMS_Malloc(size_t amount_of_memory,uint32_t unique_identifier,char *script_identifier);

//	Attaches to a block of global OMMS memory.  As long as at least one module (or script) is
//	attached to a module, the memory will not be deleted. (Increments the reference count)
//	Returns NULL if the memory couldn't be attached (it has been either free'd or never malloced)
void *OMMS_Attach(OMMSHANDLE handle);

//	Detaches a block of global OMMS memory. (Reduces the reference count).
void OMMS_Detach(OMMSHANDLE handle);

//	Frees a block of global memory
//	Only has affect if you are attached to the memory.  Memory will _ONLY_ be deleted when the
//	following conditions are meant for the shared memory block:
//	1) The reference count for the global memory is at 0 (OMMS_Attach() adds to reference count, OMMS_Detach()
subtracts).
//	2) OMMS_Free() has been called for the block of global memory.
//	The conditions can happen in any order, but as soon as both are satisfied, the memory
//	becomes deleted and any pointer returned by OMMS_Attach() becomes invalid.
//
//	handle : the value returned by OMMS_Malloc()
 void OMMS_Free(OMMSHANDLE handle);

//	Returns an OMMSHANDLE to a block of global memory allocated by a module/script.  Pass
//	in the unique_identifier and the script_identifier that was passed in the OMMS_Malloc().
//	Note: script_identifier is really the filename of the module that called the OMMS_Malloc().
//	Returns -1 if the module was never OMMS_Malloc()'d.
OMMSHANDLE OMMS_Find(uint32_t unique_identifier,char *script_identifier);

//	Returns information about the OMMS memory given it's handle returned from the OMMS_Find() or
//	OMMS_Malloc(). Returns 0 if the handle was invalid, 1 if the information has been filled in;
//	Pass NULL in for those parameters you don't need information about.
char OMMS_GetInfo(OMMSHANDLE handle,uint32_t *mem_size,uint32_t *uid,uint16_t *reference_count,uint8_t *has_free_been_called);


******************************************************************************
*/

struct tOMMSNode {
  uint16_t id;
  uint32_t size_of_memory;
  uint32_t unique_id;
  uint16_t reference_count;
  uint8_t free_called;
  void *memory_ptr;
  tOMMSNode *next;
};

struct tOMMSHashNode {
  char *script_name;
  uint16_t base_id;
  tOMMSNode *root;
  tOMMSHashNode *next;
};

tOMMSHashNode *OMMS_Hash_node_root = NULL;
uint16_t OMMS_Current_base_id = 0;
uint16_t OMMS_Current_id = 0;

//	Searches through the hash nodes and looks for the one associated with
//	the script name, if one doesn't exist it will create one (if autocreate is true).
tOMMSHashNode *Osiris_OMMS_FindHashNode(char *script_name, bool autocreate);
//	frees all memory associated with the OMMSHashNode given (includes all script
//	memory...returns the next node in the list, NULL if none
tOMMSHashNode *Osiris_OMMS_DeleteHashNode(tOMMSHashNode *node);
//	finds an OMMS node, given the hash node to start at.  If it isn't
//	found than one is created. NULL if out of memory.
tOMMSNode *Osiris_OMMS_FindNode(tOMMSHashNode *root, uint32_t uid, bool autocreate);
//	Removes the OMMS node for the given HashNode (completly remove)
void Osiris_OMMS_RemoveNode(tOMMSHashNode *root, uint32_t uid);
//	Reduces the reference count for an OMMSNode by 1
void Osiris_OMMS_ReduceRefCount(tOMMSHashNode *root, tOMMSNode *node);
//	Calls free on a node
void Osiris_OMMS_CallFreeForNode(tOMMSHashNode *root, tOMMSNode *node);
//	Searches for the given OMMSHANDLE, returns NULL if not found.  if hash is
//	given it will return the hash root that the node was found in.
tOMMSNode *Osiris_OMMS_FindHandle(OMMSHANDLE handle, tOMMSHashNode **hash = NULL);

// Allocates a block of global memory for this module, of size amount_of_memory.
// unique_identfier is the script provided unique ID which is used throughout.
// script_identifier is the pointer of data provided to the script in it's InitializeDLL function (really the name of
// the script) Returns -1 if there isn't enough available memory Returns -2 if the unique identifier passed in is
// already used, but the requested amount_of_memory is different Returns -3 if the unique identifier passed in is
// already used, same size requested
static OMMSHANDLE Osiris_OMMS_Malloc(size_t amount_of_memory, uint32_t unique_identifier, char *script_identifier);

//	Attaches to a block of global OMMS memory.  As long as at least one module (or script) is
//	attached to a module, the memory will not be deleted. (Increments the reference count)
//	Returns NULL if the memory couldn't be attached (it has been either free'd or never malloced)
static void *Osiris_OMMS_Attach(OMMSHANDLE handle);

//	Detaches a block of global OMMS memory. (Reduces the reference count).
static void Osiris_OMMS_Detach(OMMSHANDLE handle);

//	Frees a block of global memory
//	Only has affect if you are attached to the memory.  Memory will _ONLY_ be deleted when the
//	following conditions are meant for the shared memory block:
//	1) The reference count for the global memory is at 0 (OMMS_Attach() adds to reference count, OMMS_Detach()
// subtracts). 	2) OMMS_Free() has been called for the block of global memory. 	The conditions can happen in any order,
// but
// as soon as both are satisfied, the memory 	becomes deleted and any pointer returned by OMMS_Attach() becomes
// invalid.
//
//	handle : the value returned by OMMS_Malloc()
static void Osiris_OMMS_Free(OMMSHANDLE handle);

//	Returns an OMMSHANDLE to a block of global memory allocated by a module/script.  Pass
//	in the unique_identifier and the script_identifier that was passed in the OMMS_Malloc().
//	Note: script_identifier is really the filename of the module that called the OMMS_Malloc().
//	Returns -1 if the module was never OMMS_Malloc()'d.
static OMMSHANDLE Osiris_OMMS_Find(uint32_t unique_identifier, char *script_identifier);

//	Returns information about the OMMS memory given it's handle returned from the OMMS_Find() or
//	OMMS_Malloc(). Returns 0 if the handle was invalid, 1 if the information has been filled in;
//	Pass NULL in for those parameters you don't need information about.
static char Osiris_OMMS_GetInfo(OMMSHANDLE handle, uint32_t *mem_size, uint32_t *uid, uint16_t *reference_count,
                                uint8_t *has_free_been_called);

void Osiris_InitOMMS(void) {
  OMMS_Current_base_id = 0;
  OMMS_Current_id = 0;
  OMMS_Hash_node_root = NULL;
  atexit(Osiris_CloseOMMS);
}

void Osiris_CloseOMMS(void) {
  mprintf(0, "OSIRIS: Resetting OMMS memory\n");

  tOMMSHashNode *curr = OMMS_Hash_node_root;

  while (curr) {
    curr = Osiris_OMMS_DeleteHashNode(curr);
  }

  OMMS_Hash_node_root = NULL;
}

void Osiris_SaveOMMS(CFILE *file) {
  // save out our current OMMS handle position
  cf_WriteShort(file, OMMS_Current_base_id);
  cf_WriteShort(file, OMMS_Current_id);

  // go through all the hash nodes write their information out
  tOMMSHashNode *currhash = OMMS_Hash_node_root;
  while (currhash) {
    // write out a 1, saying that we have a hash node here
    cf_WriteByte(file, 1);

    cf_WriteShort(file, currhash->base_id);

    // write out the info about this hash node
    int length;
    length = (currhash->script_name) ? strlen(currhash->script_name) : 0;
    cf_WriteByte(file, length);
    if (length)
      cf_WriteBytes((uint8_t *)currhash->script_name, length, file);

    // now go through all the nodes and write their data
    tOMMSNode *node;
    node = currhash->root;
    while (node) {
      // write out 1, for valid hash
      cf_WriteByte(file, 1);
      cf_WriteShort(file, node->id);

      // write out all the node data
      cf_WriteByte(file, node->free_called);
      cf_WriteShort(file, node->reference_count);
      cf_WriteInt(file, node->unique_id);
      cf_WriteInt(file, node->size_of_memory);
      if (node->size_of_memory)
        cf_WriteBytes((uint8_t *)node->memory_ptr, node->size_of_memory, file);

      node = node->next;
    }
    // write out terminating 0
    cf_WriteByte(file, 0);

    // go to the next hash node
    currhash = currhash->next;
  }

  // write out terminating 0
  cf_WriteByte(file, 0);
}

void Osiris_RestoreOMMS(CFILE *file) {
  ASSERT(OMMS_Hash_node_root == NULL);

  OMMS_Current_base_id = (uint16_t)cf_ReadShort(file);
  OMMS_Current_id = (uint16_t)cf_ReadShort(file);

  tOMMSHashNode *currhash;
  tOMMSNode *node;

  currhash = NULL;

  // we have to rebuild the nodes
  while (cf_ReadByte(file)) {
    if (!currhash) {
      currhash = OMMS_Hash_node_root = (tOMMSHashNode *)mem_malloc(sizeof(tOMMSHashNode));
    } else {
      currhash->next = (tOMMSHashNode *)mem_malloc(sizeof(tOMMSHashNode));
      currhash = currhash->next;
    }

    if (!currhash)
      Error("Out of memory");

    currhash->next = NULL;
    currhash->root = NULL;
    currhash->script_name = NULL;

    currhash->base_id = (uint16_t)cf_ReadShort(file);

    // read length of string
    int length = cf_ReadByte(file);
    if (length) {
      currhash->script_name = (char *)mem_malloc(length + 1);
      if (!currhash->script_name)
        Error("Out of Memory");
      else
        cf_ReadBytes((uint8_t *)currhash->script_name, length, file);
    }

    node = NULL;

    // now go through all the nodes and right their data
    while (cf_ReadByte(file)) {
      if (!node) {
        node = currhash->root = (tOMMSNode *)mem_malloc(sizeof(tOMMSNode));
      } else {
        node->next = (tOMMSNode *)mem_malloc(sizeof(tOMMSNode));
        node = node->next;
      }

      if (!node)
        Error("Out of memory");

      node->memory_ptr = NULL;
      node->next = NULL;

      node->id = (uint16_t)cf_ReadShort(file);

      node->free_called = (cf_ReadByte(file)) ? true : false;
      node->reference_count = (uint16_t)cf_ReadShort(file);
      node->unique_id = cf_ReadInt(file);
      node->size_of_memory = cf_ReadInt(file);
      if (node->size_of_memory) {
        node->memory_ptr = mem_malloc(node->size_of_memory);
        if (!node->memory_ptr)
          Error("Out of memory");

        cf_ReadBytes((uint8_t *)node->memory_ptr, node->size_of_memory, file);
      }
    } // end reading nodes
  }   // end reading hash nodes
}

//	Searches through the hash nodes and looks for the one associated with
//	the script name, if one doesn't exist it will create one (if autocreate is true).
tOMMSHashNode *Osiris_OMMS_FindHashNode(char *script_name, bool autocreate) {
  tOMMSHashNode *curr = OMMS_Hash_node_root;

  if (!OMMS_Hash_node_root) {
    // allocate the root node
    if (!autocreate)
      return NULL;

    curr = OMMS_Hash_node_root = (tOMMSHashNode *)mem_malloc(sizeof(tOMMSHashNode));
  } else {
    if (curr->script_name && !stricmp(curr->script_name, script_name)) {
      // the root node matches
      return curr;
    }

    // search through to see if we can find the node
    while (curr->next) {
      if (curr->next->script_name && !stricmp(curr->next->script_name, script_name)) {
        // curr->next is a matching node
        return curr->next;
      }

      curr = curr->next;
    }

    // allocate a node and tack it on the end
    if (!autocreate)
      return NULL;

    curr->next = (tOMMSHashNode *)mem_malloc(sizeof(tOMMSHashNode));
    curr = curr->next;
  }

  // if we get here then curr is a freshly allocated node
  curr->next = NULL;
  curr->root = NULL;
  curr->script_name = mem_strdup(script_name);
  curr->base_id = ++OMMS_Current_base_id;

  return curr;
}

//	frees all memory associated with the OMMSHashNode given (includes all script
//	memory...returns the next node in the list, NULL if none
tOMMSHashNode *Osiris_OMMS_DeleteHashNode(tOMMSHashNode *node) {
  if (!node)
    return NULL;
  tOMMSHashNode *ret = node->next;

  if (node->script_name)
    mem_free(node->script_name);

  tOMMSNode *curr, *next;
  curr = next = node->root;

  while (curr) {
    next = curr->next;
    mem_free(curr->memory_ptr);
    mem_free(curr);
    curr = next;
  }

  mem_free(node);

  return ret;
}

//	finds an OMMS node, given the hash node to start at.  If it isn't
//	found than one is created. NULL if out of memory.
tOMMSNode *Osiris_OMMS_FindNode(tOMMSHashNode *root, uint32_t uid, bool autocreate) {
  tOMMSNode *curr = root->root;

  if (!root->root) {
    // allocate the root node
    if (!autocreate)
      return NULL;

    curr = root->root = (tOMMSNode *)mem_malloc(sizeof(tOMMSNode));
  } else {
    if (curr->unique_id == uid) {
      // the root node matches
      return curr;
    }

    // search through to see if we can find the node
    while (curr->next) {
      if (curr->next->unique_id == uid) {
        // curr->next is a matching node
        return curr->next;
      }

      curr = curr->next;
    }

    // allocate a node and tack it on the end
    if (!autocreate)
      return NULL;

    curr->next = (tOMMSNode *)mem_malloc(sizeof(tOMMSNode));
    curr = curr->next;
  }

  // if we get here then curr is a freshly allocated node
  curr->free_called = 0;
  curr->memory_ptr = NULL;
  curr->next = NULL;
  curr->reference_count = 0;
  curr->size_of_memory = 0;
  curr->unique_id = uid;
  curr->id = ++OMMS_Current_id;

  return curr;
}

//	Removes the OMMS node for the given HashNode (completly remove)
void Osiris_OMMS_RemoveNode(tOMMSHashNode *root, uint32_t uid) {
  tOMMSNode *curr = root->root;
  tOMMSNode *node_to_free = NULL;

  if (!root->root)
    return;

  if (root->root->unique_id == uid) {
    // the root node matches
    node_to_free = root->root;
    root->root = NULL;
    goto free_mem;
  }

  // search through to see if we can find the node
  while (curr->next) {
    if (curr->next->unique_id == uid) {
      // curr->next is a matching node
      node_to_free = curr->next;
      curr->next = curr->next->next;
      goto free_mem;
    }
    curr = curr->next;
  }

free_mem:
  if (!node_to_free)
    return;

  mprintf(0, "OMMS: deleting uid 0x%x\n", node_to_free->unique_id);
  if (node_to_free->memory_ptr)
    mem_free(node_to_free->memory_ptr);
  mem_free(node_to_free);
}

//	Reduces the reference count for an OMMSNode by 1
void Osiris_OMMS_ReduceRefCount(tOMMSHashNode *root, tOMMSNode *node) {
  if (!node)
    return;

  node->reference_count--;

  if (node->reference_count <= 0 && node->free_called) {
    // free the node
    Osiris_OMMS_RemoveNode(root, node->unique_id);
  }
}

//	Calls free on a node
void Osiris_OMMS_CallFreeForNode(tOMMSHashNode *root, tOMMSNode *node) {
  if (!node)
    return;

  node->free_called = 1;

  if (node->reference_count <= 0 && node->free_called) {
    // free the node
    Osiris_OMMS_RemoveNode(root, node->unique_id);
  }
}

//	Searches for the given OMMSHANDLE, returns NULL if not found.  if hash is
//	given it will return the hash root that the node was found in.
tOMMSNode *Osiris_OMMS_FindHandle(OMMSHANDLE handle, tOMMSHashNode **hash) {
  tOMMSHashNode *hashcurr = OMMS_Hash_node_root;
  tOMMSNode *nodecurr;
  uint16_t base_id;
  uint16_t id;
  base_id = ((handle & 0xFFFF0000) >> 16);
  id = (handle & 0x0000FFFF);

  while (hashcurr) {
    if (hashcurr->base_id == base_id) {

      nodecurr = hashcurr->root;
      while (nodecurr) {
        if (nodecurr->id == id) {
          if (hash)
            *hash = hashcurr;
          return nodecurr;
        }

        nodecurr = nodecurr->next;
      }
    }

    hashcurr = hashcurr->next;
  }

  return NULL;
}

// Allocates a block of global memory for this module, of size amount_of_memory.
// unique_identfier is the script provided unique ID which is used throughout.
// script_identifier is the pointer of data provided to the script in it's InitializeDLL function (really the name of
// the script) Returns -1 if there isn't enough available memory Returns -2 if the unique identifier passed in is
// already used, but the requested amount_of_memory is different If the memory has already been allocated, it will
// return the handle.
OMMSHANDLE Osiris_OMMS_Malloc(size_t amount_of_memory, uint32_t unique_identifier, char *script_identifier) {
  ASSERT(amount_of_memory > 0);
  if (amount_of_memory <= 0)
    return -1;

  // find the correct hash node
  tOMMSHashNode *hash = Osiris_OMMS_FindHashNode(script_identifier, true);
  if (!hash)
    return -1;

  tOMMSNode *node = Osiris_OMMS_FindNode(hash, unique_identifier, true);
  if (!node)
    return -1;

  if (node->memory_ptr) {
    // the memory already has been allocated
    if (node->size_of_memory == amount_of_memory)
      return ((hash->base_id << 16) | (node->id));

    // the size's requested don't match
    return -2;
  }

  // we need to allocate the memory
  node->memory_ptr = mem_malloc(amount_of_memory);
  if (!node->memory_ptr)
    return -1; // out of memory

  node->size_of_memory = amount_of_memory;
  node->unique_id = unique_identifier;

  mprintf(0, "OMMS: malloc handle (0x%x%x) size = %d\n", hash->base_id, node->id, node->size_of_memory);
  return ((hash->base_id << 16) | (node->id));
}

//	Attaches to a block of global OMMS memory.  As long as at least one module (or script) is
//	attached to a module, the memory will not be deleted. (Increments the reference count)
//	Returns NULL if the memory couldn't be attached (it has been either free'd or never malloced)
void *Osiris_OMMS_Attach(OMMSHANDLE handle) {
  tOMMSNode *node = Osiris_OMMS_FindHandle(handle);
  if (!node)
    return NULL;

  if (node->memory_ptr) {
    node->reference_count++;
  }
  return node->memory_ptr;
}

//	Detaches a block of global OMMS memory. (Reduces the reference count).
void Osiris_OMMS_Detach(OMMSHANDLE handle) {
  tOMMSHashNode *hash;
  tOMMSNode *node = Osiris_OMMS_FindHandle(handle, &hash);
  if (!node)
    return;

  Osiris_OMMS_ReduceRefCount(hash, node);
}

//	Frees a block of global memory
//	Only has affect if you are attached to the memory.  Memory will _ONLY_ be deleted when the
//	following conditions are meant for the shared memory block:
//	1) The reference count for the global memory is at 0 (OMMS_Attach() adds to reference count, OMMS_Detach()
// subtracts). 	2) OMMS_Free() has been called for the block of global memory. 	The conditions can happen in any order,
// but
// as soon as both are satisfied, the memory 	becomes deleted and any pointer returned by OMMS_Attach() becomes
// invalid.
//
//	handle : the value returned by OMMS_Malloc()
void Osiris_OMMS_Free(OMMSHANDLE handle) {
  tOMMSHashNode *hash;
  tOMMSNode *node = Osiris_OMMS_FindHandle(handle, &hash);
  if (!node)
    return;

  Osiris_OMMS_CallFreeForNode(hash, node);
}

//	Returns an OMMSHANDLE to a block of global memory allocated by a module/script.  Pass
//	in the unique_identifier and the script_identifier that was passed in the OMMS_Malloc().
//	Note: script_identifier is really the filename of the module that called the OMMS_Malloc().
//	Returns -1 if the module was never OMMS_Malloc()'d.
OMMSHANDLE Osiris_OMMS_Find(uint32_t unique_identifier, char *script_identifier) {
  tOMMSHashNode *hash = Osiris_OMMS_FindHashNode(script_identifier, false);
  if (!hash)
    return -1;

  tOMMSNode *node = Osiris_OMMS_FindNode(hash, unique_identifier, false);
  if (!node)
    return -1;
  if (!node->memory_ptr)
    return -1;

  return ((hash->base_id << 16) | (node->id));
}

//	Returns information about the OMMS memory given it's handle returned from the OMMS_Find() or
//	OMMS_Malloc(). Returns 0 if the handle was invalid, 1 if the information has been filled in;
//	Pass NULL in for those parameters you don't need information about.
char Osiris_OMMS_GetInfo(OMMSHANDLE handle, uint32_t *mem_size, uint32_t *uid, uint16_t *reference_count,
                         uint8_t *has_free_been_called) {
  if (mem_size)
    *mem_size = 0;
  if (uid)
    *uid = 0;
  if (reference_count)
    *reference_count = 0;
  if (has_free_been_called)
    *has_free_been_called = 0;

  tOMMSHashNode *hash;
  tOMMSNode *node = Osiris_OMMS_FindHandle(handle, &hash);
  if (!node)
    return 0;

  if (mem_size)
    *mem_size = node->size_of_memory;
  if (uid)
    *uid = node->unique_id;
  if (reference_count)
    *reference_count = node->reference_count;
  if (has_free_been_called)
    *has_free_been_called = node->free_called;

  return 1;
}

//*****************************************************************************
//	Osiris_CreateModuleInitStruct
//	Purpose:
//		This function initializes a Module Init Struct with all the needed data to get sent
//	to the module during initialization.
void Osiris_CreateModuleInitStruct(tOSIRISModuleInit *mi) {
  int i = 0;

  // fill in function pointers here
  mi->fp[i++] = (int *)MonoPrintf;
  mi->fp[i++] = (int *)msafe_CallFunction;
  mi->fp[i++] = (int *)msafe_GetValue;
  mi->fp[i++] = (int *)osipf_CallObjectEvent;
  mi->fp[i++] = (int *)osipf_CallTriggerEvent;
  mi->fp[i++] = (int *)osipf_SoundTouch;
  mi->fp[i++] = (int *)osipf_ObjectFindID;
  mi->fp[i++] = (int *)osipf_WeaponFindID;
  mi->fp[i++] = (int *)osipf_ObjectGetTimeLived;
  mi->fp[i++] = (int *)osipf_GetGunPos;
  mi->fp[i++] = (int *)osipf_RoomValue;
  mi->fp[i++] = (int *)osipf_IsRoomValid;
  mi->fp[i++] = (int *)osipf_GetAttachParent;
  mi->fp[i++] = (int *)osipf_GetNumAttachSlots;
  mi->fp[i++] = (int *)osipf_GetAttachChildHandle;
  mi->fp[i++] = (int *)osipf_AttachObjectAP;
  mi->fp[i++] = (int *)osipf_AttachObjectRad;
  mi->fp[i++] = (int *)osipf_UnattachFromParent;
  mi->fp[i++] = (int *)osipf_UnattachChild;
  mi->fp[i++] = (int *)osipf_UnattachChildren;
  mi->fp[i++] = (int *)osipf_RayCast;
  mi->fp[i++] = (int *)osipf_AIGetPathID;
  mi->fp[i++] = (int *)osipf_AIGoalFollowPathSimple;
  mi->fp[i++] = (int *)osipf_AIPowerSwitch;
  mi->fp[i++] = (int *)osipf_AITurnTowardsVectors;
  mi->fp[i++] = (int *)osipf_AISetType;
  mi->fp[i++] = (int *)osipf_AIFindHidePos;
  mi->fp[i++] = (int *)osipf_AIGoalAddEnabler;
  mi->fp[i++] = (int *)osipf_AIGoalAdd;
  mi->fp[i++] = (int *)osipf_AIGoalClear;
  mi->fp[i++] = (int *)osipf_AIValue;
  mi->fp[i++] = (int *)osipf_AIFindObjOfType;
  mi->fp[i++] = (int *)osipf_AIGetRoomPathPoint;
  mi->fp[i++] = (int *)osipf_AIFindEnergyCenter;
  mi->fp[i++] = (int *)osipf_AIGetDistToObj;
  mi->fp[i++] = (int *)osipf_AISetGoalFlags;
  mi->fp[i++] = (int *)osipf_AISetGoalCircleDist;
  mi->fp[i++] = (int *)osipf_CFReadBytes;
  mi->fp[i++] = (int *)osipf_CFReadInt;
  mi->fp[i++] = (int *)osipf_CFReadShort;
  mi->fp[i++] = (int *)osipf_CFReadByte;
  mi->fp[i++] = (int *)osipf_CFReadFloat;
  mi->fp[i++] = (int *)osipf_CFReadDouble;
  mi->fp[i++] = (int *)osipf_CFReadString;
  mi->fp[i++] = (int *)osipf_CFWriteBytes;
  mi->fp[i++] = (int *)osipf_CFWriteString;
  mi->fp[i++] = (int *)osipf_CFWriteInt;
  mi->fp[i++] = (int *)osipf_CFWriteShort;
  mi->fp[i++] = (int *)osipf_CFWriteByte;
  mi->fp[i++] = (int *)osipf_CFWriteFloat;
  mi->fp[i++] = (int *)osipf_CFWriteDouble;
  mi->fp[i++] = (int *)Osiris_AllocateMemory;
  mi->fp[i++] = (int *)Osiris_FreeMemory;
  mi->fp[i++] = (int *)Osiris_CancelTimer;
  mi->fp[i++] = (int *)Osiris_CreateTimer;
  mi->fp[i++] = (int *)msafe_DoPowerup;
  mi->fp[i++] = (int *)osipf_ObjCreate;
  mi->fp[i++] = (int *)osipf_GameTime;
  mi->fp[i++] = (int *)osipf_FrameTime;
  mi->fp[i++] = (int *)osipf_ObjWBValue;
  mi->fp[i++] = (int *)Osiris_TimerExists;
  mi->fp[i++] = (int *)osipf_ObjectValue;
  mi->fp[i++] = (int *)osipf_MatcenValue;
  mi->fp[i++] = (int *)osipf_MatcenReset;
  mi->fp[i++] = (int *)osipf_MatcenCopy;
  mi->fp[i++] = (int *)osipf_MatcenCreate;
  mi->fp[i++] = (int *)osipf_MatcenFindId;
  mi->fp[i++] = (int *)osipf_MissionFlagSet;
  mi->fp[i++] = (int *)osipf_MissionFlagGet;
  mi->fp[i++] = (int *)osipf_PlayerValue;
  mi->fp[i++] = (int *)osipf_ObjectCustomAnim;
  mi->fp[i++] = (int *)osipf_PlayerAddHudMessage;
  mi->fp[i++] = (int *)osipf_ObjGhost;
  mi->fp[i++] = (int *)osipf_ObjBurning;
  mi->fp[i++] = (int *)osipf_ObjIsEffect;
  mi->fp[i++] = (int *)osipf_CFopen;
  mi->fp[i++] = (int *)osipf_CFclose;
  mi->fp[i++] = (int *)osipf_CFtell;
  mi->fp[i++] = (int *)osipf_CFeof;
  mi->fp[i++] = (int *)osipf_SoundStop;
  mi->fp[i++] = (int *)osipf_SoundPlay2d;
  mi->fp[i++] = (int *)osipf_SoundPlay3d;
  mi->fp[i++] = (int *)osipf_SoundFindId;
  mi->fp[i++] = (int *)osipf_AIIsObjFriend;
  mi->fp[i++] = (int *)osipf_AIIsObjEnemy;
  mi->fp[i++] = (int *)osipf_AIGoalValue;
  mi->fp[i++] = (int *)osipf_AIGetNearbyObjs;
  mi->fp[i++] = (int *)osipf_AIGetCurGoalIndex;
  mi->fp[i++] = (int *)Osiris_OMMS_Malloc;
  mi->fp[i++] = (int *)Osiris_OMMS_Attach;
  mi->fp[i++] = (int *)Osiris_OMMS_Detach;
  mi->fp[i++] = (int *)Osiris_OMMS_Free;
  mi->fp[i++] = (int *)Osiris_OMMS_Find;
  mi->fp[i++] = (int *)Osiris_OMMS_GetInfo;
  mi->fp[i++] = (int *)Cinematic_Start;
  mi->fp[i++] = (int *)Cinematic_Stop;
  mi->fp[i++] = (int *)osipf_FindSoundName;
  mi->fp[i++] = (int *)osipf_FindRoomName;
  mi->fp[i++] = (int *)osipf_FindTriggerName;
  mi->fp[i++] = (int *)osipf_FindObjectName;
  mi->fp[i++] = (int *)osipf_GetTriggerRoom;
  mi->fp[i++] = (int *)osipf_GetTriggerFace;
  mi->fp[i++] = (int *)osipf_FindDoorName;
  mi->fp[i++] = (int *)osipf_FindTextureName;
  mi->fp[i++] = (int *)osipf_CreateRandomSparks;
  mi->fp[i++] = (int *)Osiris_CancelTimerID;
  mi->fp[i++] = (int *)osipf_GetGroundPos;
  mi->fp[i++] = (int *)osipf_EnableShip;
  mi->fp[i++] = (int *)osipf_IsShipEnabled;
  mi->fp[i++] = (int *)osipf_PathGetInformation;
  mi->fp[i++] = (int *)Cinematic_StartCannedScript;
  mi->fp[i++] = (int *)osipf_FindMatcenName;
  mi->fp[i++] = (int *)osipf_FindPathName;
  mi->fp[i++] = (int *)osipf_FindLevelGoalName;
  mi->fp[i++] = (int *)osipf_ObjectFindType;
  mi->fp[i++] = (int *)osipf_LGoalValue;
  mi->fp[i++] = (int *)osipf_ObjMakeListOfType;
  mi->fp[i++] = (int *)osipf_ObjKill;
  //	mi->fp[i++] = (int *)osipf_AIAreRoomsReachable;
  mi->fp[i++] = (int *)osipf_AIIsDestReachable;
  mi->fp[i++] = (int *)osipf_AIIsObjReachable;
  mi->fp[i++] = (int *)osipf_GameGetDiffLevel;
  mi->fp[i++] = (int *)osipf_GetLanguageSetting;
  mi->fp[i++] = (int *)osipf_PathValue;

  // fill in the remaining with NULL
  for (; i < MAX_MODULEFUNCS; i++) {
    mi->fp[i] = NULL;
  }
}