File: ScriptLevelInterface.cpp

package info (click to toggle)
descent3 1.5.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 35,256 kB
  • sloc: cpp: 416,147; ansic: 3,233; sh: 10; makefile: 8
file content (2516 lines) | stat: -rw-r--r-- 73,133 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
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
/*
* 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/editor/ScriptLevelInterface.cpp $
* $Revision: 1.1.1.1 $
* $Date: 2003-08-26 03:57:38 $
* $Author: kevinb $
*
* ScriptLevelInterface.cpp : implementation file
*
* $Log: not supported by cvs2svn $
 *
 * 24    5/11/99 12:13p Jason
 * added checks for messed up gamefiles
 *
 * 23    2/25/99 12:34p Jeff
 * don't display message that someone has a file checked out during mass
 * compile
 *
 * 22    2/24/99 9:39p Jeff
 * only sync scripts when needed
 *
 * 21    2/23/99 11:52p Jeff
 * new script dll sync dialog
 *
 * 20    2/21/99 8:04p Jeff
 * better handling of out-of-sync scripts
 *
 * 19    2/18/99 8:29p Nate
 * commented out some manage code
 *
 * 18    2/18/99 10:58a Jeff
 * added a 'find in files' for gamefiles manage system
 *
 * 17    2/17/99 6:54p Jeff
 * added check box for auto check in/out scripts with levels...fix
 * checksum mprintf bug
 *
 * 16    2/09/99 4:43p Jeff
 * fixed resource leak
 *
 * 15    2/01/99 11:55a Jeff
 * automatically check in/out .msg and .str files
 *
 * 14    1/13/99 5:01p Jeff
 * don't auto-checkin dll if the .cpp doesn't get checked in for a new
 * file
 *
 * 13    1/11/99 3:34p Jeff
 * added checks for when going editor->game to see if scripts are out of
 * date, if so give the option of breaking out.  Add some options to mass
 * script compiler, along with a toolbar shortcut.
 *
 * 12    1/05/99 4:54p Jeff
 * save the last script edited and set the on init
 *
 * 11    1/05/99 4:33p Jeff
 * when "Display non checkout files" is checked, display the .cpp files in
 * the directory...not just whats in the table file
 *
 * 10    12/21/98 6:41p Jeff
 * auto-compile, check in and check back out for when new scripts are
 * created. (yes must be said to the data update)
 *
 * 9     12/20/98 9:57p Jeff
 * added ability to display scripts not checked out.  don't display
 * message on checkin/out
 *
 * 8     12/18/98 12:11p Jeff
 * created a function to determine a script's type and hooked it in
 *
 * 7     12/18/98 12:11p Jeff
 * added mass script compile dialog
 *
 * 6     12/18/98 10:40a Jeff
 * save out settings to registry
 *
 * 5     12/13/98 7:50p Jeff
 * automatically add new DLLs that aren't in the manage
 * system...implemented create new script function, even though it's still
 * in infancy.
 *
 * 4     12/13/98 3:10a Jeff
 * fixed some bugs.  Automatically check in/out/etc .dll when operating
 * with .cpp.  Hooked in compiler and compiler configuration.  Very
 * complete.
 *
 * 3     12/11/98 10:34p Jeff
 * Updated the script/level interface, all manage system integration is
 * complete.  External Script Editor loading is setup too.
 *
 * 2     12/11/98 5:50p Jeff
 * implemented and added changes regarding Level&Scripting manage system
 * and compiler interface
*
* $NoKeywords: $
*/

// ScriptLevelInterface.cpp : implementation file
//

#include "stdafx.h"
#include "editor.h"
#include "ScriptLevelInterface.h"
#include "CreateNewScriptDlg.h"
#include "VirtualCompilerConfig.h"
#include "ScriptMassCompile.h"
#include "gamefile.h"
#include "gamefilepage.h"
#include "descent.h"
#include "mono.h"
#include "ddio.h"
#include "pserror.h"
#include "manage.h"
#include "mem.h"
#include "ScriptCompilerAPI.h"
#include "AppDatabase.h"
#include "Descent.h"
#include "ScriptSyncDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

bool AddNewGameFile(char *fullpath, char *directory);
bool DeleteGamefile(char *tempbuffer);
bool CheckInGamefile(char *tempbuffer, bool giveok);
bool CheckOutGamefile(char *tempbuffer, bool show_ok_confirmation, bool report_who_has_locked = true);

char LastScriptSelected[256];
bool Scripts_need_sync = true;

/////////////////////////////////////////////////////////////////////////////
// CScriptLevelInterface dialog

CScriptLevelInterface::CScriptLevelInterface(CWnd *pParent /*=NULL*/) : CDialog(CScriptLevelInterface::IDD, pParent) {
  //{{AFX_DATA_INIT(CScriptLevelInterface)
  m_ViewType = -1;
  m_ScriptType = 0;
  m_ShowNonCheckedOut = FALSE;
  m_AutoCheckScripts = FALSE;
  //}}AFX_DATA_INIT

  InCritical = false;
}

void CScriptLevelInterface::DoDataExchange(CDataExchange *pDX) {
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CScriptLevelInterface)
  DDX_Control(pDX, IDC_LIST_AVAILABLE, m_ListAvailable);
  DDX_Control(pDX, IDC_LIST_CHECKEDOUT, m_ListCheckedOut);
  DDX_Control(pDX, IDC_SELECTED_SCRIPT, m_ScriptToCompile);
  DDX_Radio(pDX, IDC_RADIO_LEVELS, m_ViewType);
  DDX_Radio(pDX, IDC_SCRIPTGAME, m_ScriptType);
  DDX_Check(pDX, IDC_LIST_NONCHECKED_OUT, m_ShowNonCheckedOut);
  DDX_Check(pDX, IDC_AUTOCHECKSCRIPTS, m_AutoCheckScripts);
  //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CScriptLevelInterface, CDialog)
//{{AFX_MSG_MAP(CScriptLevelInterface)
ON_BN_CLICKED(IDC_CHECKOUT, OnCheckout)
ON_BN_CLICKED(IDC_CHECKIN, OnCheckin)
ON_BN_CLICKED(IDC_UNDOCHECKOUT, OnUndocheckout)
ON_BN_CLICKED(IDC_RADIO_SCRIPTS, OnRadioScripts)
ON_BN_CLICKED(IDC_RADIO_LEVELS, OnRadioLevels)
ON_BN_CLICKED(IDC_COMPILE, OnCompile)
ON_BN_CLICKED(IDC_EDITSCRIPT, OnEditscript)
ON_CBN_CLOSEUP(IDC_SELECTED_SCRIPT, OnCloseupSelectedScript)
ON_BN_CLICKED(IDC_ADDNEW, OnAddnew)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_CREATESCRIPT, OnCreatescript)
ON_WM_CLOSE()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_CONFIGCOMPILER, OnConfigcompiler)
ON_BN_CLICKED(IDC_MASSCOMPILE, OnMasscompile)
ON_CBN_SELCHANGE(IDC_SELECTED_SCRIPT, OnSelchangeSelectedScript)
ON_BN_CLICKED(IDC_LIST_NONCHECKED_OUT, OnListNoncheckedOut)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScriptLevelInterface message handlers

void CScriptLevelInterface::SetStatus(char *str) {
  CWnd *wnd = (CWnd *)GetDlgItem(IDC_STATUS);
  wnd->SetWindowText(str);
  Descent->defer();
}

void CScriptLevelInterface::OnCheckout() {
  UpdateData(true);
  InCritical = true;
  SetStatus("Checking out files");
  if (m_ViewType == 0) {
    // we're working with levels & level scripts
    if (CheckOutLevels())
      UpdateDialog();
  } else {
    // we're working with game scripts
    if (CheckOutScripts())
      UpdateDialog();
  }
  InCritical = false;
  SetStatus("Ready");
  Scripts_need_sync = true;
}

void CScriptLevelInterface::OnCheckin() {
  UpdateData(true);
  InCritical = true;
  SetStatus("Checking in files");
  if (m_ViewType == 0) {
    // we're working with levels & level scripts
    if (CheckInLevels())
      UpdateDialog();
  } else {
    // we're working with game scripts
    if (CheckInScripts())
      UpdateDialog();
  }
  InCritical = false;
  SetStatus("Ready");
}

void CScriptLevelInterface::OnUndocheckout() {
  UpdateData(true);
  InCritical = true;
  SetStatus("Undoing Checkout on files");
  if (m_ViewType == 0) {
    // we're working with levels & level scripts
    if (UndoCheckOutLevels())
      UpdateDialog();
  } else {
    // we're working with game scripts
    if (UndoCheckOutScripts())
      UpdateDialog();
  }
  InCritical = false;
  SetStatus("Ready");
  Scripts_need_sync = true;
}

void CScriptLevelInterface::OnRadioScripts() { UpdateDialog(); }

void CScriptLevelInterface::OnRadioLevels() { UpdateDialog(); }

CEdit *CompileOutputWindow;
CString CompileOutputData;

void CompileOutputCallback(char *str) {
  CompileOutputData += str;
  CompileOutputWindow->SetWindowText(CompileOutputData);
}

void CScriptLevelInterface::OnCompile() {
  UpdateData(true);

  SetStatus("Compiling...");

  bool dll_exist = true;

  char buffer[_MAX_PATH], filename[_MAX_PATH];
  m_ScriptToCompile.GetLBText(m_ScriptToCompile.GetCurSel(), buffer);
  ddio_SplitPath(buffer, NULL, filename, NULL);
  strcat(filename, ".dll");
  if (!cfexist(filename)) {
    // there isn't a dll yet...lets make sure one isn't in the manage system
    bool found;
    int i;

    found = false;
    for (i = 0; i < MAX_GAMEFILES; i++) {
      if (Gamefiles[i].used && (!stricmp(filename, Gamefiles[i].name))) {
        found = true;
        break;
      }
    }

    if (!found) {
      // ok there wasn't one in the gamefiles make sure there isn't a tracklocked
      if (mng_FindTrackLock(filename, PAGETYPE_GAMEFILE) == -1) {
        // there isn't one in the track lock...this is our first compile!
        dll_exist = false;
      }
    }
  }

  tCompilerInfo ci;
  ci.callback = CompileOutputCallback;
  ci.script_type = (m_ScriptType == 0) ? ST_GAME : ST_LEVEL;
  strcpy(ci.source_filename, buffer);
  CompileOutputWindow = (CEdit *)GetDlgItem(IDC_COMPILER_OUTPUT);
  CompileOutputData = "";

  ScriptCompile(&ci);

  SetStatus("Ready");
  CompileOutputData = ""; // no need for this to hang around

  if (!dll_exist && cfexist(filename)) {
    // yeah! we created a dll, ask the user if they want to put it into the manage system
    if (Network_up) {
      sprintf(buffer,
              "Congratulations! It looks like a dll was successfully created. I\ndon't see the DLL in the manage "
              "system, so\nwould you like me to add it into the manage system for you?(HIGHLY recommended)");
      if (MessageBox(buffer, "Congratulations", MB_YESNO) == IDYES) {
        ddio_MakePath(buffer, LocalScriptDir, filename, NULL);
        if (!cfexist(buffer)) {
          sprintf(buffer,
                  "I can't seem to find %s in your data\\scripts directory...Sorry,\nbut I can't automatically add it "
                  "for you.\nYou'll have to manually add %s into the manage system.",
                  filename, filename);
          MessageBox(buffer, "Uh Oh!", MB_OK);
        } else {
          if (AddNewGameFile(buffer, "scripts")) {
            sprintf(buffer,
                    "%s has been added into the manage system.  Don't forget to check it in to make sure it stays in "
                    "the\nmanage system!",
                    filename);
            MessageBox(buffer, "Success", MB_OK);
          } else {
            sprintf(buffer,
                    "There was a problem adding %s\ninto the manage system. You'll have\nto do it automatically.",
                    filename);
            MessageBox(buffer, "Error", MB_OK);
          }
        }
      }
    } else {
      sprintf(buffer, "Doh! Congratulations! You compiled the DLL successfully.  However,\nI don't see it in the "
                      "manage system.  I'd offer to add it\nautomatically for you, but you said NO to the\ndata "
                      "update.  You'll have to add it manually.");
      MessageBox(buffer, "Doh!", MB_OK);
    }
  }
}

void CScriptLevelInterface::OnEditscript() {
  CComboBox *combo = (CComboBox *)GetDlgItem(IDC_SELECTED_SCRIPT);
  if (!combo)
    return;

  char tempbuf[_MAX_PATH], fullpath[_MAX_PATH];

  int index = combo->GetCurSel();
  combo->GetLBText(index, tempbuf);

  if (m_ShowNonCheckedOut && (mng_FindTrackLock(tempbuf, PAGETYPE_GAMEFILE) == -1)) {
    // display a warning that this file is not checked out
    MessageBox("This file is NOT checked out, you'll lose all changes on your\nnext data update!", "Warning", MB_OK);
  }

  ddio_MakePath(fullpath, LocalScriptDir, tempbuf, NULL);
  if (!cfexist(fullpath)) {
    sprintf(tempbuf, "Weird, I couldn't find %s to open...", fullpath);
    MessageBox(tempbuf, "Error", MB_OK);
    return;
  }

  SHELLEXECUTEINFO sei;

  sei.cbSize = sizeof(sei);
  sei.fMask = SEE_MASK_NOCLOSEPROCESS;
  sei.hwnd = m_hWnd;
  sei.lpVerb = "open";
  sei.lpFile = fullpath;
  sei.lpParameters = NULL;
  sei.lpDirectory = LocalScriptDir;
  sei.nShow = SW_NORMAL;

  // int res = (int)ShellExecute(m_hWnd,"open",fullpath,NULL,LocalScriptDir,0);
  int res;

  ShellExecuteEx(&sei);
  res = (int)sei.hInstApp;

  if (res <= 32) {
    char buffer[256];
    switch (res) {
    case 0:
      strcpy(buffer, "The operating system is out of memory or resources.");
      break;
    case ERROR_FILE_NOT_FOUND:
      strcpy(buffer, "The specified file was not found.");
      break;
    case ERROR_PATH_NOT_FOUND:
      strcpy(buffer, "The specified path was not found.");
      break;
    case ERROR_BAD_FORMAT:
      strcpy(buffer, "The .exe file is invalid (non-Win32� .exe or error in .exe image).");
      break;
    case SE_ERR_ACCESSDENIED:
      strcpy(buffer, "The operating system denied access to the specified file.");
      break;
    case SE_ERR_ASSOCINCOMPLETE:
      strcpy(buffer, "The file name association is incomplete or invalid.");
      break;
    case SE_ERR_DDEBUSY:
      strcpy(buffer, "The DDE transaction could not be completed because other DDE transactions were being processed.");
      break;
    case SE_ERR_DDEFAIL:
      strcpy(buffer, "The DDE transaction failed.");
      break;
    case SE_ERR_DDETIMEOUT:
      strcpy(buffer, "The DDE transaction could not be completed because the request timed out.");
      break;
    case SE_ERR_DLLNOTFOUND:
      strcpy(buffer, "The specified dynamic-link library was not found.");
      break;
    // case SE_ERR_FNF:
    //	strcpy(buffer,"The specified file was not found.");
    //	break;
    case SE_ERR_NOASSOC:
      strcpy(buffer, "There is no application associated with the given file name extension.\nGet Jeff and he'll fix "
                     "this problem for you quickly and easily.");
      break;
    case SE_ERR_OOM:
      strcpy(buffer, "There was not enough memory to complete the operation.");
      break;
    // case SE_ERR_PNF:
    //	strcpy(buffer,"The specified path was not found.");
    //	break;
    case SE_ERR_SHARE:
      strcpy(buffer, "A sharing violation occurred.");
      break;
    default:
      strcpy(buffer, "An unknown error occurred");
      break;
    }
    MessageBox(buffer, "Error", MB_OK);
  }

  Scripts_need_sync = true;
}

void CScriptLevelInterface::OnCloseupSelectedScript() {
  UpdateData(true);

  SetStatus("Determining script type...");

  char buffer[_MAX_PATH];
  int currsel = m_ScriptToCompile.GetCurSel();
  if (currsel >= 0) {
    m_ScriptToCompile.GetLBText(currsel, buffer);

    strcpy(LastScriptSelected, buffer);

    uint8_t type = DetermineScriptType(buffer);

    switch (type) {
    case ST_GAME:
      m_ScriptType = 0;
      break;
    case ST_LEVEL:
      m_ScriptType = 1;
      break;
    }

    UpdateData(false);
  }

  SetStatus("Ready");
}

void CScriptLevelInterface::OnSelchangeSelectedScript() { OnCloseupSelectedScript(); }

void CScriptLevelInterface::UpdateAvailableWithLevels() {
  CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST_AVAILABLE);
  if (!list)
    return;

  list->ResetContent();

  SetStatus("Updating available levels");

  char buffer[_MAX_PATH];
  int length;

  for (int i = 0; i < MAX_GAMEFILES; i++) {
    if (Gamefiles[i].used) {
      // we're only adding .d3l's to this list
      length = strlen(Gamefiles[i].name);
      if (!stricmp(&Gamefiles[i].name[length - 4], ".d3l") &&
          (mng_FindTrackLock(Gamefiles[i].name, PAGETYPE_GAMEFILE) == -1)) {
        strcpy(buffer, Gamefiles[i].name);
        buffer[length - 4] = '\0';
        list->AddString(buffer);
      }
    }
  }

  SetStatus("Ready");
}

void CScriptLevelInterface::UpdateAvailableWithScripts() {
  CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST_AVAILABLE);
  if (!list)
    return;

  list->ResetContent();

  SetStatus("Updating available scripts");

  char buffer[_MAX_PATH];
  int length;

  for (int i = 0; i < MAX_GAMEFILES; i++) {
    if (Gamefiles[i].used) {
      // we're only adding .d3l's to this list
      length = strlen(Gamefiles[i].name);
      if (!stricmp(&Gamefiles[i].name[length - 4], ".cpp") &&
          (mng_FindTrackLock(Gamefiles[i].name, PAGETYPE_GAMEFILE) == -1)) {
        strcpy(buffer, Gamefiles[i].name);
        buffer[length - 4] = '\0';
        list->AddString(buffer);
      }
    }
  }

  SetStatus("Ready");
}

void CScriptLevelInterface::UpdateCheckedOutWithLevels() {
  CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST_CHECKEDOUT);
  if (!list)
    return;

  list->ResetContent();

  SetStatus("Updating checked out levels");

  char buffer[_MAX_PATH];
  int length;

  for (int i = 0; i < MAX_TRACKLOCKS; i++) {
    if (GlobalTrackLocks[i].used && GlobalTrackLocks[i].pagetype == PAGETYPE_GAMEFILE) {

      length = strlen(GlobalTrackLocks[i].name);
      if (!stricmp(&GlobalTrackLocks[i].name[length - 4], ".d3l")) {
        strcpy(buffer, GlobalTrackLocks[i].name);
        buffer[length - 4] = '\0';
        list->AddString(buffer);
      }
    }
  }

  SetStatus("Ready");
}

void CScriptLevelInterface::UpdateCheckedOutWithScripts() {
  CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST_CHECKEDOUT);
  if (!list)
    return;

  list->ResetContent();

  SetStatus("Updating checked out scripts");

  char buffer[_MAX_PATH];
  int length;

  for (int i = 0; i < MAX_TRACKLOCKS; i++) {
    if (GlobalTrackLocks[i].used && GlobalTrackLocks[i].pagetype == PAGETYPE_GAMEFILE) {

      length = strlen(GlobalTrackLocks[i].name);
      if (!stricmp(&GlobalTrackLocks[i].name[length - 4], ".cpp")) {
        strcpy(buffer, GlobalTrackLocks[i].name);
        buffer[length - 4] = '\0';
        list->AddString(buffer);
      }
    }
  }

  SetStatus("Ready");
}

void CScriptLevelInterface::UpdateScriptListWithLevels() {
  CComboBox *combo = (CComboBox *)GetDlgItem(IDC_SELECTED_SCRIPT);
  if (!combo)
    return;

  combo->ResetContent();

  SetStatus("Updating script list of level scripts");

  char buffer[_MAX_PATH];
  int length, i;

  if (m_ShowNonCheckedOut) {
    char olddir[_MAX_PATH];
    ddio_GetWorkingDir(olddir, _MAX_PATH);
    ddio_SetWorkingDir(LocalScriptDir);

    for (i = 0; i < MAX_GAMEFILES; i++) {
      if (Gamefiles[i].used) {
        // look for d3l's
        length = strlen(Gamefiles[i].name);

        if (length > 4 && !stricmp(&Gamefiles[i].name[length - 4], ".d3l")) {
          strcpy(buffer, Gamefiles[i].name);
          buffer[length - 4] = '\0';
          strcat(buffer, ".cpp");

          // ok we have a d3l, look to see if there is a script available for it
          char temp[_MAX_PATH];
          if (ddio_FindFileStart(buffer, temp)) {
            combo->AddString(buffer);
          }
          ddio_FindFileClose();
          /*
          for ( int j=0; j<MAX_GAMEFILES;j++){
                  if(!stricmp(Gamefiles[j].name,buffer)){
                          //we found a corresponding script
                          combo->AddString(buffer);
                          break;
                  }
          }
          */
        }
      }
    }

    ddio_SetWorkingDir(olddir);
  } else {

    for (i = 0; i < MAX_TRACKLOCKS; i++) {
      if (GlobalTrackLocks[i].used && GlobalTrackLocks[i].pagetype == PAGETYPE_GAMEFILE) {

        // look for checked out d3l's
        length = strlen(GlobalTrackLocks[i].name);

        if (length > 4 && !stricmp(&GlobalTrackLocks[i].name[length - 4], ".d3l")) {
          strcpy(buffer, GlobalTrackLocks[i].name);
          buffer[length - 4] = '\0';
          strcat(buffer, ".cpp");

          // ok we have a checked out d3l, look to see if there is a script available for it
          for (int j = 0; j < MAX_TRACKLOCKS; j++) {
            if (!stricmp(GlobalTrackLocks[j].name, buffer)) {
              // we found a corresponding script...
              combo->AddString(buffer);
              break;
            }
          }
        }
      }
    }
  }

  if (LastScriptSelected[0] != '\0' && cfexist(LastScriptSelected))
    combo->SetCurSel(combo->FindStringExact(0, LastScriptSelected));
  else
    combo->SetCurSel(0);

  SetStatus("Ready");
}

void CScriptLevelInterface::UpdateScriptListWithScripts() {
  CComboBox *combo = (CComboBox *)GetDlgItem(IDC_SELECTED_SCRIPT);
  if (!combo)
    return;

  combo->ResetContent();

  SetStatus("Updating script list with scripts");

  char buffer[_MAX_PATH];
  int length, i;

  if (m_ShowNonCheckedOut) {
    char olddir[_MAX_PATH];
    ddio_GetWorkingDir(olddir, _MAX_PATH);
    ddio_SetWorkingDir(LocalScriptDir);

    if (ddio_FindFileStart("*.cpp", buffer)) {
      combo->AddString(buffer);
      while (ddio_FindNextFile(buffer)) {
        combo->AddString(buffer);
      }
    }
    ddio_FindFileClose();

    ddio_SetWorkingDir(olddir);

    /*
    for (i=0;i<MAX_GAMEFILES;i++){
            if(Gamefiles[i].used){

                    //look for cpp's
                    length = strlen(Gamefiles[i].name);

                    if(length>4 && !stricmp(&Gamefiles[i].name[length-4],".cpp")){
                            strcpy(buffer,Gamefiles[i].name);
                            combo->AddString(buffer);
                    }
            }
    }
    */

  } else {

    for (i = 0; i < MAX_TRACKLOCKS; i++) {
      if (GlobalTrackLocks[i].used && GlobalTrackLocks[i].pagetype == PAGETYPE_GAMEFILE) {

        // look for checked out cpp's
        length = strlen(GlobalTrackLocks[i].name);

        if (length > 4 && !stricmp(&GlobalTrackLocks[i].name[length - 4], ".cpp")) {
          strcpy(buffer, GlobalTrackLocks[i].name);
          combo->AddString(buffer);
        }
      }
    }
  }

  if (LastScriptSelected[0] != '\0' && cfexist(LastScriptSelected))
    combo->SetCurSel(combo->FindStringExact(0, LastScriptSelected));
  else
    combo->SetCurSel(0);

  SetStatus("Ready");
}

void CScriptLevelInterface::UpdateDialog() {
  UpdateData(true);

  CWnd *stat1, *stat2;
  stat1 = (CWnd *)GetDlgItem(IDC_AVAILABLE_GRP);
  stat2 = (CWnd *)GetDlgItem(IDC_CHECKEDOUT_GRP);

  if (m_ViewType == 0) {
    // we're working with levels & level scripts
    UpdateAvailableWithLevels();
    UpdateCheckedOutWithLevels();
    UpdateScriptListWithLevels();
    stat1->SetWindowText("Levels Available");
    stat2->SetWindowText("Levels CheckedOut");
  } else {
    // we're working with game scripts
    UpdateAvailableWithScripts();
    UpdateCheckedOutWithScripts();
    UpdateScriptListWithScripts();
    stat1->SetWindowText("Scripts Available");
    stat2->SetWindowText("Scripts CheckedOut");
  }
}

BOOL CScriptLevelInterface::OnInitDialog() {
  CDialog::OnInitDialog();
  int value;
  bool bvalue;

  // restore settings
  if (Database->read_int("EditorScriptViewType", &value)) {
    m_ViewType = value;
  } else {
    m_ViewType = 0;
  }

  if (Database->read_int("EditorScriptType", &value)) {
    m_ScriptType = value;
  } else {
    m_ScriptType = 0;
  }

  if (Database->read("EditorScriptShowNonCheckedOut", &bvalue)) {
    m_ShowNonCheckedOut = bvalue;
  } else {
    m_ShowNonCheckedOut = 0;
  }

  if (Database->read("EditorAutoCheckScriptsWithLevels", &bvalue)) {
    m_AutoCheckScripts = bvalue;
  } else {
    m_AutoCheckScripts = 0;
  }

  value = 256;
  if (!Database->read("EditorLastScript", LastScriptSelected, &value)) {
    LastScriptSelected[0] = '\0';
  }

  UpdateData(false);
  UpdateDialog();

  SetStatus("Ready");

  return TRUE;
}

void CScriptLevelInterface::OnAddnew() {
  InCritical = true;
  SetStatus("Adding new files");

  if (m_ViewType == 0) {
    // we're working with levels & level scripts
    if (AddNewLevel())
      UpdateDialog();
  } else {
    // we're working with game scripts
    if (AddNewScript())
      UpdateDialog();
  }
  InCritical = false;
  SetStatus("Ready");
  Scripts_need_sync = true;
}

void CScriptLevelInterface::OnDelete() {
  UpdateData(true);
  InCritical = true;
  SetStatus("Deleting files");

  if (m_ViewType == 0) {
    if (DeleteLevel())
      UpdateDialog();
  } else {
    if (DeleteScript())
      UpdateDialog();
  }

  InCritical = false;
  SetStatus("Ready");
}

void CScriptLevelInterface::OnCreatescript() {
  CCreateNewScriptDlg dlg;

  if (dlg.DoModal()) {
    int i;
    char filename[_MAX_PATH];
    strcpy(filename, dlg.m_Filename.GetBuffer(0));

    // see if we should add this to the manage system
    for (i = 0; i < MAX_GAMEFILES; i++) {
      if (Gamefiles[i].used && (!stricmp(Gamefiles[i].name, filename))) {
        // ok we found the file...no need to add it
        break;
      }
    }

    bool on_network = false;

    if (i < MAX_GAMEFILES || (mng_FindTrackLock(filename, PAGETYPE_GAMEFILE) != -1)) {
      char buffer[_MAX_PATH];
      sprintf(buffer,
              "Just so you know, %s\nalready exists in the manage system, do you still want\nto create a new file of "
              "the same name?",
              filename);
      on_network = true;
      if (MessageBox(buffer, "Warning", MB_YESNO) != IDYES)
        return;
    }

    if (!ScriptCreateEmptyScript(filename, (dlg.m_ScriptType == 0) ? ST_LEVEL : ST_GAME)) {
      MessageBox("Unable to create file", "Error", MB_OK);
      return;
    }

    bool added_to_network = false;

    // we never found the file in the manage system
    if (!on_network) {
      if (Network_up) {
        char buffer[_MAX_PATH + 100];
        sprintf(buffer, "Do you want to add\n%s\nto the network?", filename);

        if (cfexist(filename) && MessageBox(buffer, "Add to Data", MB_YESNO) == IDYES) {
          ddio_MakePath(buffer, LocalScriptDir, filename, NULL);
          AddNewGameFile(buffer, "scripts");
          UpdateDialog();
          added_to_network = true;
        }
      } else {
        // doh! they didn't say yes
        char buffer[_MAX_PATH];
        sprintf(buffer, "I would ask you if you wanted to add the new\nscript to the manage system, but\nsince you "
                        "said no to the data update, you'll have\nto add it manually");
        MessageBox(buffer, "Doh!", MB_OK);
      }

      // now auto-compile the DLL
      SetStatus("Initial Compile");
      tCompilerInfo ci;
      ci.callback = CompileOutputCallback;
      ci.script_type = (dlg.m_ScriptType == 0) ? ST_LEVEL : ST_GAME;
      strcpy(ci.source_filename, filename);
      CompileOutputWindow = (CEdit *)GetDlgItem(IDC_COMPILER_OUTPUT);
      CompileOutputData = "";
      ScriptCompile(&ci);

      if (Network_up) {
        SetStatus("Adding compiled script to manage system");
        char dllname[_MAX_PATH], buffer[_MAX_PATH];
        ddio_SplitPath(filename, NULL, dllname, NULL);
        strcat(dllname, ".dll");
        if (cfexist(dllname) && added_to_network) {
          ddio_MakePath(buffer, LocalScriptDir, dllname, NULL);
          AddNewGameFile(buffer, "scripts");
          UpdateDialog();
        }

        SetStatus("Auto-Checking in");

      try_again:
        if (!mng_MakeLocker())
          goto try_again;

        bool sscr = CheckInGamefile(filename, false);
        bool cscr = CheckInGamefile(dllname, false);

        SetStatus("Auto-Checking out");
        if (sscr)
          CheckOutGamefile(filename, false);
        if (cscr)
          CheckOutGamefile(dllname, false);

        mng_EraseLocker();
      }
    }
  }

  SetStatus("Ready");
}

void CScriptLevelInterface::OnConfigcompiler() {
  CVirtualCompilerConfig dlg;

  SetStatus("Configuring Compiler");
  dlg.DoModal();
  SetStatus("Ready");
}

void CScriptLevelInterface::OnClose() {
  if (InCritical)
    return;

  CDialog::OnClose();
}

void CScriptLevelInterface::OnDestroy() { CDialog::OnDestroy(); }

void CScriptLevelInterface::OnOK() {
  UpdateData(true);

  // write out settings
  Database->write("EditorScriptViewType", m_ViewType);
  Database->write("EditorScriptType", m_ScriptType);
  Database->write("EditorScriptShowNonCheckedOut", m_ShowNonCheckedOut);
  Database->write("EditorLastScript", LastScriptSelected, strlen(LastScriptSelected) + 1);
  Database->write("EditorAutoCheckScriptsWithLevels", m_AutoCheckScripts);

  CDialog::OnOK();
}

void CScriptLevelInterface::OnMasscompile() {
  CScriptMassCompile dlg;
  SetStatus("Mass compiling scripts...");
  dlg.DoModal();
  UpdateDialog();
  SetStatus("Ready");
}

void CScriptLevelInterface::OnListNoncheckedOut() { UpdateDialog(); }

// ======================File Checkin/Checkout Functions======================
bool CScriptLevelInterface::AddNewLevel(void) {
  char filename[255];

  if (!Network_up) {
    OutrageMessageBox("You need to say Yes on the initial data update, in order to\nadd files\n");
    return false;
  }

  // Get the filename of the representing image
  CString filter = "Descent III Levels (*.d3l)|*.d3l||";

  if (!OpenFileDialog(this, (LPCTSTR)filter, filename, Current_files_dir, sizeof(Current_files_dir)))
    return false;

  return AddNewGameFile(filename, "levels");
}

bool CScriptLevelInterface::AddNewScript(void) {
  char filename[255];

  if (!Network_up) {
    OutrageMessageBox("You need to say Yes on the initial data update, in order to\nadd files\n");
    return false;
  }

  // Get the filename of the representing image
  CString filter = "Descent III Script Source (*.cpp)|*.cpp|Descent III Compiled Script (*.dll)|*.dll||";

  if (!OpenFileDialog(this, (LPCTSTR)filter, filename, Current_files_dir, sizeof(Current_files_dir)))
    return false;

  return AddNewGameFile(filename, "scripts");
}

bool DeleteGamefile(char *tempbuffer) {
  int tl;
  mngs_Pagelock pl;
  bool removed = false;

  // verify that we have it locked
  if ((tl = mng_FindTrackLock(tempbuffer, PAGETYPE_GAMEFILE)) == -1) {
    OutrageMessageBox("%s is not yours to delete. Hmmm. Get Jeff", tempbuffer);
    Int3();
    return false;
  }

  strcpy(pl.name, tempbuffer);
  pl.pagetype = PAGETYPE_GAMEFILE;

  // Check to see if this is a local gamefile only.  If so, only delete it locally
  if (mng_CheckIfPageOwned(&pl, TableUser) != 1) {
    mng_FreeTrackLock(tl);
    if (!mng_DeletePage(tempbuffer, PAGETYPE_GAMEFILE, 1)) {
      mprintf(0, ErrorString);
      Int3();
    } else {
      removed = true;
    }
  } else { // if its network, delete it from both the net and local drives
    mng_FreeTrackLock(tl);
    mng_DeletePage(tempbuffer, PAGETYPE_GAMEFILE, 1);
    mng_DeletePage(tempbuffer, PAGETYPE_GAMEFILE, 0);
    mng_DeletePagelock(tempbuffer, PAGETYPE_GAMEFILE);
    removed = true;
  }

  if (removed) {
    for (int gf = 0; gf < MAX_GAMEFILES; gf++) {
      if (Gamefiles[gf].used && (!stricmp(tempbuffer, Gamefiles[gf].name))) {
        FreeGamefile(gf);
        break;
      }
    }
  }

  return true;
}

bool AddNewGameFile(char *fullpath, char *directory) {
  char cur_name[100];
  char pathname[100], name[100], extension[100];
  int gamefile_handle;

  if (!Network_up) {
    return false;
  }

  //	Okay, we selected a file. Lets do what needs to be done here.
  ddio_SplitPath(fullpath, pathname, name, extension);
  sprintf(cur_name, "%s%s", name, extension);

  if ((FindGamefileName(cur_name)) != -1) {
    OutrageMessageBox("There is already a file with that name.  Rename the file and then try to add it.");
    return false;
  }

  gamefile_handle = AllocGamefile();

  strcpy(Gamefiles[gamefile_handle].name, cur_name);
  strcpy(Gamefiles[gamefile_handle].dir_name, directory);

  // Finally, save a local copy of the model/anim and alloc a tracklock
  mprintf(0, "Making a copy of this file locally...\n");

  char destname[100];
  ddio_MakePath(destname, LocalD3Dir, "data", directory, Gamefiles[gamefile_handle].name, NULL);
  if (stricmp(destname, fullpath)) // only copy if they are different
    cf_CopyFile(destname, fullpath);

  mng_AllocTrackLock(cur_name, PAGETYPE_GAMEFILE);
  return true;
}

bool CheckInGamefile(char *tempbuffer, bool show_ok_confirmation) {
  mngs_Pagelock temp_pl;
  int r, n;

  for (n = 0; n < MAX_GAMEFILES; n++) {
    if (Gamefiles[n].used && (!stricmp(Gamefiles[n].name, tempbuffer))) {
      break;
    }
  }

  if (n == MAX_GAMEFILES)
    return false;

  // Make sure we own this gamefile
  strcpy(temp_pl.name, Gamefiles[n].name);
  temp_pl.pagetype = PAGETYPE_GAMEFILE;

  r = mng_CheckIfPageOwned(&temp_pl, TableUser);
  if (r < 0)
    OutrageMessageBox(ErrorString);
  else if (r == 0)
    OutrageMessageBox(InfoString);
  else {
    // Change the pagelock state to UNLOCKED
    strcpy(temp_pl.holder, "UNLOCKED");
    if (!mng_ReplacePagelock(temp_pl.name, &temp_pl)) {
      AfxMessageBox(ErrorString, MB_OK);
      return false;
    } else {
      // Now actually replace the copy on the net with our local one
      if (!mng_ReplacePage(Gamefiles[n].name, Gamefiles[n].name, n, PAGETYPE_GAMEFILE, 0))
        OutrageMessageBox(ErrorString);
      else {
        // Save this gamefile file to the network for all

        char destname[100], srcname[100];

        ddio_MakePath(srcname, LocalD3Dir, "data", Gamefiles[n].dir_name, Gamefiles[n].name, NULL);
        ddio_MakePath(destname, NetD3Dir, "data", Gamefiles[n].dir_name, Gamefiles[n].name, NULL);

        cf_CopyFile(destname, srcname);

        // Delete it from local pagefile if its there
        int dret = mng_DeletePage(Gamefiles[n].name, PAGETYPE_GAMEFILE, 1);
        ASSERT(dret == 1);

        // Free the tracklock
        int p = mng_FindTrackLock(Gamefiles[n].name, PAGETYPE_GAMEFILE);
        ASSERT(p != -1);
        mng_FreeTrackLock(p);

        if (show_ok_confirmation)
          OutrageMessageBox("%s checked in.", tempbuffer);

        // Make sure its checked in
        if (cf_Diff(destname, srcname)) {
          ASSERT(1);                      // Get Jason! File didn't check in correctly!
          cf_CopyFile(destname, srcname); // Do this so we can trace whats happening
        }
      }
    }
  }
  return true;
}

bool CheckOutGamefile(char *tempbuffer, bool show_ok_confirmation, bool report_who_has_locked) {
  int n;
  mngs_Pagelock temp_pl;
  mngs_gamefile_page gamefilepage;
  int r;

  for (n = 0; n < MAX_GAMEFILES; n++) {
    if (Gamefiles[n].used && (!stricmp(Gamefiles[n].name, tempbuffer))) {
      break;
    }
  }

  if (n == MAX_GAMEFILES)
    return false;

  // Make sure it can be locked
  strcpy(temp_pl.name, Gamefiles[n].name);
  temp_pl.pagetype = PAGETYPE_GAMEFILE;

  r = mng_CheckIfPageLocked(&temp_pl);
  if (r == 2) {
    int answer;
    answer = OutrageMessageBox(MBOX_YESNO, "This page is not even in the table file, or the database maybe corrupt.  "
                                           "Override to 'Unlocked'? (Select NO if you don't know what you're doing)");
    if (answer == IDYES) {
      strcpy(temp_pl.holder, "UNLOCKED");
      if (!mng_ReplacePagelock(temp_pl.name, &temp_pl))
        AfxMessageBox(ErrorString, MB_OK);
    }
  } else if (r < 0)
    OutrageMessageBox(ErrorString);
  else if (r == 1) {
    if (report_who_has_locked)
      OutrageMessageBox(InfoString);
  } else {

    // Everything is ok.  Tell the network we're locking it and get a copy to
    // our local drive
    strcpy(temp_pl.holder, TableUser);

    // Search thru the net pagefile and get a new copy in RAM in case anyone
    // changed it since we started the editor
    if (mng_FindSpecificGamefilePage(temp_pl.name, &gamefilepage, 0)) {
      if (mng_AssignGamefilePageToGamefile(&gamefilepage, n)) {
        if (!mng_ReplacePage(Gamefiles[n].name, Gamefiles[n].name, n, PAGETYPE_GAMEFILE, 1)) {
          OutrageMessageBox("There was problem writing that page(%s) locally!", tempbuffer);
          return false;
        } else {

          if (!mng_ReplacePagelock(temp_pl.name, &temp_pl)) {
            AfxMessageBox(ErrorString, MB_OK);
            return false;
          }
        }

        if (show_ok_confirmation)
          OutrageMessageBox("%s locked.", tempbuffer);
      } else
        OutrageMessageBox("There was a problem loading this gamefile(%s).  You might encounter problems in dealing "
                          "with it.  Good luck!",
                          tempbuffer);

      mng_AllocTrackLock(Gamefiles[n].name, PAGETYPE_GAMEFILE);
    } else
      OutrageMessageBox("Couldn't find that gamefile(%s) in the table file!", tempbuffer);
  }

  return true;
}

bool UndoCheckOutGamefile(char *tempbuffer) {
  int tl, n;
  mngs_Pagelock pl;
  mngs_gamefile_page gamefilepage;

  for (n = 0; n < MAX_GAMEFILES; n++) {
    if (Gamefiles[n].used && (!stricmp(Gamefiles[n].name, tempbuffer))) {
      break;
    }
  }

  if (n == MAX_GAMEFILES)
    return false;

  // Should have this item locked
  if ((tl = mng_FindTrackLock(Gamefiles[n].name, PAGETYPE_GAMEFILE)) == -1)
    return false;

  // Make sure its to be deleted
  if (OutrageMessageBox(MBOX_YESNO,
                        "Are you sure you want to undo your lock on %s and lose any changes you may have made?",
                        tempbuffer) != IDYES)
    return false;

  strcpy(pl.name, Gamefiles[n].name);
  pl.pagetype = PAGETYPE_GAMEFILE;

  mng_FreeTrackLock(tl);

  // Delete local page
  if (!mng_DeletePage(Gamefiles[n].name, PAGETYPE_GAMEFILE, 1)) {
    mprintf(0, ErrorString);
    Int3();
  }

  // Get old data from net
  if (!mng_FindSpecificGamefilePage(pl.name, &gamefilepage, 0)) {
    Int3();
    return false;
  }

  if (!mng_AssignGamefilePageToGamefile(&gamefilepage, n)) {
    Int3();
    return false;
  }

  return true;
}

bool CScriptLevelInterface::DeleteLevel() {
  CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST_CHECKEDOUT);
  int listbox_size = list->GetCount();
  int list_size = 0;
  int i;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      list_size++;
    }
  }

  if (list_size == 0)
    return false;
  int *index_map = (int *)mem_malloc(list_size * sizeof(int));
  if (!index_map)
    return false;

  int index = 0;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      index_map[index] = i;
      index++;
    }
  }

  if (!mng_MakeLocker()) {
    mem_free(index_map);
    return false;
  }

  int j;
  bool changed = false;
  char buffer[_MAX_PATH + 200];
  char tempbuffer[_MAX_PATH];
  char savename[_MAX_PATH];

  for (i = 0; i < list_size; i++) {
    index = index_map[i];

    list->GetText(index, tempbuffer);
    strcpy(savename, tempbuffer);
    strcat(tempbuffer, ".d3l");

    // this item is checked...we wish to delete
    sprintf(buffer, "Are you sure you want to delete the level\n%s?", tempbuffer);
    if (MessageBox(buffer, "Friendly Warning", MB_YESNO) == IDYES) {
      // go ahead and delete the sucker!
      char t[256];
      sprintf(t, "Deleting: %s", tempbuffer);
      SetStatus(t);
      if (DeleteGamefile(tempbuffer))
        changed = true;
    }

    bool script_also;
    script_also = false;

    // ok the level has been deleted, see if there is a corresponding .cpp file
    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".cpp");
    for (j = 0; j < MAX_TRACKLOCKS; j++) {
      if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
          (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
        // ok, we found a possibly matching script...ask if they want that
        // removed also...
        sprintf(buffer, "A possible level script (%s) was found, do you want\nto remove this also?", tempbuffer);
        if (m_AutoCheckScripts || MessageBox(buffer, "Friendly Warning", MB_YESNO) == IDYES) {
          // go ahead and delete the sucker!
          char t[256];
          sprintf(t, "Deleting: %s", tempbuffer);
          SetStatus(t);

          if (DeleteGamefile(tempbuffer)) {
            changed = true;
            script_also = true;
          }
        }
        break;
      }
    }

    // ok, now see if there was a DLL too
    if (script_also) {
      strcpy(tempbuffer, savename);
      strcat(tempbuffer, ".dll");
      for (j = 0; j < MAX_TRACKLOCKS; j++) {
        if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
            (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
          // ok, we found a possibly matching script...ask if they want that
          // removed also...
          // go ahead and delete the sucker!
          char t[256];
          sprintf(t, "Deleting: %s", tempbuffer);
          SetStatus(t);

          if (DeleteGamefile(tempbuffer))
            changed = true;
          break;
        }
      }

      strcpy(tempbuffer, savename);
      strcat(tempbuffer, ".msg");
      for (j = 0; j < MAX_TRACKLOCKS; j++) {
        if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
            (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
          // ok, we found a possibly matching script...ask if they want that
          // removed also...
          // go ahead and delete the sucker!
          char t[256];
          sprintf(t, "Deleting: %s", tempbuffer);
          SetStatus(t);

          if (DeleteGamefile(tempbuffer))
            changed = true;
          break;
        }
      }

      strcpy(tempbuffer, savename);
      strcat(tempbuffer, ".str");
      for (j = 0; j < MAX_TRACKLOCKS; j++) {
        if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
            (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
          // ok, we found a possibly matching script...ask if they want that
          // removed also...
          // go ahead and delete the sucker!
          char t[256];
          sprintf(t, "Deleting: %s", tempbuffer);
          SetStatus(t);

          if (DeleteGamefile(tempbuffer))
            changed = true;
          break;
        }
      }
    }
  }

  mng_EraseLocker();
  mem_free(index_map);

  return changed;
}

bool CScriptLevelInterface::DeleteScript() {
  CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST_CHECKEDOUT);
  int listbox_size = list->GetCount();
  int list_size = 0;
  int i;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      list_size++;
    }
  }

  if (list_size == 0)
    return false;
  int *index_map = (int *)mem_malloc(list_size * sizeof(int));
  if (!index_map)
    return false;

  int index = 0;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      index_map[index] = i;
      index++;
    }
  }

  if (!mng_MakeLocker()) {
    mem_free(index_map);
    return false;
  }

  int j;
  bool changed = false;
  char buffer[_MAX_PATH + 200];
  char tempbuffer[_MAX_PATH];
  char savename[_MAX_PATH];

  for (i = 0; i < list_size; i++) {
    index = index_map[i];

    list->GetText(index, tempbuffer);
    strcpy(savename, tempbuffer);
    strcat(tempbuffer, ".cpp");

    // this item is checked...we wish to delete
    sprintf(buffer, "Are you sure you want to delete script source file\n%s?", tempbuffer);
    if (MessageBox(buffer, "Friendly Warning", MB_YESNO) == IDYES) {
      // go ahead and delete the sucker!
      char t[256];
      sprintf(t, "Deleting: %s", tempbuffer);
      SetStatus(t);

      if (DeleteGamefile(tempbuffer))
        changed = true;
    }

    // ok, now see if there was a DLL too
    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".dll");
    for (j = 0; j < MAX_TRACKLOCKS; j++) {
      if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
          (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
        // ok, we found a possibly matching script...ask if they want that
        // removed also...
        // go ahead and delete the sucker!
        char t[256];
        sprintf(t, "Deleting: %s", tempbuffer);
        SetStatus(t);

        if (DeleteGamefile(tempbuffer))
          changed = true;
        break;
      }
    }

    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".msg");
    for (j = 0; j < MAX_TRACKLOCKS; j++) {
      if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
          (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
        // ok, we found a possibly matching script...ask if they want that
        // removed also...
        // go ahead and delete the sucker!
        char t[256];
        sprintf(t, "Deleting: %s", tempbuffer);
        SetStatus(t);

        if (DeleteGamefile(tempbuffer))
          changed = true;
        break;
      }
    }

    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".str");
    for (j = 0; j < MAX_TRACKLOCKS; j++) {
      if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
          (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
        // ok, we found a possibly matching script...ask if they want that
        // removed also...
        // go ahead and delete the sucker!
        char t[256];
        sprintf(t, "Deleting: %s", tempbuffer);
        SetStatus(t);

        if (DeleteGamefile(tempbuffer))
          changed = true;
        break;
      }
    }
  }

  mng_EraseLocker();
  mem_free(index_map);

  return changed;
}

bool CScriptLevelInterface::CheckInScripts() {
  CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST_CHECKEDOUT);
  int listbox_size = list->GetCount();
  int list_size = 0;
  int i;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      list_size++;
    }
  }

  if (list_size == 0)
    return false;
  int *index_map = (int *)mem_malloc(list_size * sizeof(int));
  if (!index_map)
    return false;

  int index = 0;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      index_map[index] = i;
      index++;
    }
  }

  if (!mng_MakeLocker()) {
    mem_free(index_map);
    return false;
  }

  int j;
  bool changed = false;
  char tempbuffer[_MAX_PATH];
  char savename[_MAX_PATH];

  for (i = 0; i < list_size; i++) {
    index = index_map[i];

    list->GetText(index, tempbuffer);
    strcpy(savename, tempbuffer);
    strcat(tempbuffer, ".cpp");

    char t[256];
    sprintf(t, "Checking In: %s", tempbuffer);
    SetStatus(t);

    // this item is checked...we wish to checkin
    if (CheckInGamefile(tempbuffer, false))
      changed = true;

    // ok, now see if there was a DLL too
    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".dll");
    for (j = 0; j < MAX_TRACKLOCKS; j++) {
      if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
          (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
        // ok, we found a possibly matching script...ask if they want that
        // checked in also...
        char t[256];
        sprintf(t, "Checking In: %s", tempbuffer);
        SetStatus(t);

        if (CheckInGamefile(tempbuffer, false))
          changed = true;
        break;
      }
    }

    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".msg");
    for (j = 0; j < MAX_TRACKLOCKS; j++) {
      if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
          (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
        // ok, we found a possibly matching script...ask if they want that
        // checked in also...
        char t[256];
        sprintf(t, "Checking In: %s", tempbuffer);
        SetStatus(t);

        if (CheckInGamefile(tempbuffer, false))
          changed = true;
        break;
      }
    }

    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".str");
    for (j = 0; j < MAX_TRACKLOCKS; j++) {
      if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
          (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
        // ok, we found a possibly matching script...ask if they want that
        // checked in also...
        char t[256];
        sprintf(t, "Checking In: %s", tempbuffer);
        SetStatus(t);

        if (CheckInGamefile(tempbuffer, false))
          changed = true;
        break;
      }
    }
  }

  mng_EraseLocker();

  return changed;
}

bool CScriptLevelInterface::CheckInLevels() {
  CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST_CHECKEDOUT);
  int listbox_size = list->GetCount();
  int list_size = 0;
  int i;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      list_size++;
    }
  }

  if (list_size == 0)
    return false;
  int *index_map = (int *)mem_malloc(list_size * sizeof(int));
  if (!index_map)
    return false;

  int index = 0;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      index_map[index] = i;
      index++;
    }
  }

  if (!mng_MakeLocker()) {
    mem_free(index_map);
    return false;
  }

  int j;
  bool changed = false;
  char buffer[_MAX_PATH + 200];
  char tempbuffer[_MAX_PATH];
  char savename[_MAX_PATH];

  for (i = 0; i < list_size; i++) {
    index = index_map[i];

    list->GetText(index, tempbuffer);
    strcpy(savename, tempbuffer);
    strcat(tempbuffer, ".d3l");

    char t[256];
    sprintf(t, "Checking In: %s", tempbuffer);
    SetStatus(t);

    // go ahead and check in
    if (CheckInGamefile(tempbuffer, false))
      changed = true;

    bool script_also;
    script_also = false;

    // ok the level has been checked in, see if there is a corresponding .cpp file
    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".cpp");
    for (j = 0; j < MAX_TRACKLOCKS; j++) {
      if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
          (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
        // ok, we found a possibly matching script...ask if they want that
        // checked in also...
        sprintf(buffer,
                "A possible level script (%s) was found, do you want\nto check this in also? (HIGHLY recommended)",
                tempbuffer);
        if (m_AutoCheckScripts || MessageBox(buffer, "Friendly Warning", MB_YESNO) == IDYES) {
          char t[256];
          sprintf(t, "Checking In: %s", tempbuffer);
          SetStatus(t);

          if (CheckInGamefile(tempbuffer, false)) {
            changed = true;
            script_also = true;
          }
        }
        break;
      }
    }

    // ok, now see if there was a DLL too
    if (script_also) {
      strcpy(tempbuffer, savename);
      strcat(tempbuffer, ".dll");
      for (j = 0; j < MAX_TRACKLOCKS; j++) {
        if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
            (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
          // ok, we found a possibly matching script...ask if they want that
          // checked in also...
          char t[256];
          sprintf(t, "Checking In: %s", tempbuffer);
          SetStatus(t);

          if (CheckInGamefile(tempbuffer, false))
            changed = true;
          break;
        }
      }

      strcpy(tempbuffer, savename);
      strcat(tempbuffer, ".msg");
      for (j = 0; j < MAX_TRACKLOCKS; j++) {
        if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
            (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
          // ok, we found a possibly matching script...ask if they want that
          // checked in also...
          char t[256];
          sprintf(t, "Checking In: %s", tempbuffer);
          SetStatus(t);

          if (CheckInGamefile(tempbuffer, false))
            changed = true;
          break;
        }
      }

      strcpy(tempbuffer, savename);
      strcat(tempbuffer, ".str");
      for (j = 0; j < MAX_TRACKLOCKS; j++) {
        if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
            (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
          // ok, we found a possibly matching script...ask if they want that
          // checked in also...
          char t[256];
          sprintf(t, "Checking In: %s", tempbuffer);
          SetStatus(t);

          if (CheckInGamefile(tempbuffer, false))
            changed = true;
          break;
        }
      }
    }
  }

  mng_EraseLocker();
  mem_free(index_map);

  return changed;
}

bool CScriptLevelInterface::CheckOutScripts() {
  CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST_AVAILABLE);
  int listbox_size = list->GetCount();
  int list_size = 0;
  int i;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      list_size++;
    }
  }

  if (list_size == 0)
    return false;
  int *index_map = (int *)mem_malloc(list_size * sizeof(int));
  if (!index_map)
    return false;

  int index = 0;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      index_map[index] = i;
      index++;
    }
  }

  if (!mng_MakeLocker()) {
    mem_free(index_map);
    return false;
  }

  int j;
  bool changed = false;
  char tempbuffer[_MAX_PATH];
  char savename[_MAX_PATH];

  for (i = 0; i < list_size; i++) {
    index = index_map[i];

    list->GetText(index, tempbuffer);
    strcpy(savename, tempbuffer);
    strcat(tempbuffer, ".cpp");

    char t[256];
    sprintf(t, "Checking Out: %s", tempbuffer);
    SetStatus(t);

    // this item is checked...we wish to checkin
    if (CheckOutGamefile(tempbuffer, false))
      changed = true;

    // ok, now see if there was a DLL too
    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".dll");
    for (j = 0; j < MAX_GAMEFILES; j++) {
      if ((Gamefiles[j].used) && (!stricmp(Gamefiles[j].name, tempbuffer)) &&
          (mng_FindTrackLock(tempbuffer, PAGETYPE_GAMEFILE) == -1)) {
        // ok, we found a possibly matching script...ask if they want that
        // checked out also...
        // go ahead and delete the sucker!
        char t[256];
        sprintf(t, "Checking Out: %s", tempbuffer);
        SetStatus(t);

        if (CheckOutGamefile(tempbuffer, false))
          changed = true;
        break;
      }
    }

    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".msg");
    for (j = 0; j < MAX_GAMEFILES; j++) {
      if ((Gamefiles[j].used) && (!stricmp(Gamefiles[j].name, tempbuffer)) &&
          (mng_FindTrackLock(tempbuffer, PAGETYPE_GAMEFILE) == -1)) {
        // ok, we found a possibly matching script...ask if they want that
        // checked out also...
        // go ahead and delete the sucker!
        char t[256];
        sprintf(t, "Checking Out: %s", tempbuffer);
        SetStatus(t);

        if (CheckOutGamefile(tempbuffer, false))
          changed = true;
        break;
      }
    }

    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".str");
    for (j = 0; j < MAX_GAMEFILES; j++) {
      if ((Gamefiles[j].used) && (!stricmp(Gamefiles[j].name, tempbuffer)) &&
          (mng_FindTrackLock(tempbuffer, PAGETYPE_GAMEFILE) == -1)) {
        // ok, we found a possibly matching script...ask if they want that
        // checked out also...
        // go ahead and delete the sucker!
        char t[256];
        sprintf(t, "Checking Out: %s", tempbuffer);
        SetStatus(t);

        if (CheckOutGamefile(tempbuffer, false))
          changed = true;
        break;
      }
    }
  }

  mng_EraseLocker();
  mem_free(index_map);

  return changed;
}

bool CScriptLevelInterface::CheckOutLevels() {
  CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST_AVAILABLE);
  int listbox_size = list->GetCount();
  int list_size = 0;
  int i;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      list_size++;
    }
  }

  if (list_size == 0)
    return false;
  int *index_map = (int *)mem_malloc(list_size * sizeof(int));
  if (!index_map)
    return false;

  int index = 0;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      index_map[index] = i;
      index++;
    }
  }

  if (!mng_MakeLocker()) {
    mem_free(index_map);
    return false;
  }

  int j;
  bool changed = false;
  char buffer[_MAX_PATH + 200];
  char tempbuffer[_MAX_PATH];
  char savename[_MAX_PATH];

  for (i = 0; i < list_size; i++) {
    index = index_map[i];

    list->GetText(index, tempbuffer);
    strcpy(savename, tempbuffer);
    strcat(tempbuffer, ".d3l");

    char t[256];
    sprintf(t, "Checking Out: %s", tempbuffer);
    SetStatus(t);

    // this item is checked...we wish to checkin
    if (CheckOutGamefile(tempbuffer, false))
      changed = true;

    bool script_also;
    script_also = false;

    // ok the level has been checked in, see if there is a corresponding .cpp file
    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".cpp");
    for (j = 0; j < MAX_GAMEFILES; j++) {
      if ((Gamefiles[j].used) && (!stricmp(Gamefiles[j].name, tempbuffer)) &&
          (mng_FindTrackLock(tempbuffer, PAGETYPE_GAMEFILE) == -1)) {
        // ok, we found a possibly matching script...ask if they want that
        // checked out also...
        sprintf(buffer,
                "A possible level script (%s) was found, do you want\nto check this out also? (HIGHLY recommended)",
                tempbuffer);
        if (m_AutoCheckScripts || MessageBox(buffer, "Friendly Warning", MB_YESNO) == IDYES) {
          // go ahead and delete the sucker!
          char t[256];
          sprintf(t, "Checking Out: %s", tempbuffer);
          SetStatus(t);

          if (CheckOutGamefile(tempbuffer, false)) {
            changed = true;
            script_also = true;
          }
        }
        break;
      }
    }

    // ok, now see if there was a DLL too
    if (script_also) {
      strcpy(tempbuffer, savename);
      strcat(tempbuffer, ".dll");
      for (j = 0; j < MAX_GAMEFILES; j++) {
        if ((Gamefiles[j].used) && (!stricmp(Gamefiles[j].name, tempbuffer)) &&
            (mng_FindTrackLock(tempbuffer, PAGETYPE_GAMEFILE) == -1)) {
          // ok, we found a possibly matching script...ask if they want that
          // checked out also...
          // go ahead and delete the sucker!
          char t[256];
          sprintf(t, "Checking Out: %s", tempbuffer);
          SetStatus(t);

          if (CheckOutGamefile(tempbuffer, false))
            changed = true;
          break;
        }
      }

      strcpy(tempbuffer, savename);
      strcat(tempbuffer, ".msg");
      for (j = 0; j < MAX_GAMEFILES; j++) {
        if ((Gamefiles[j].used) && (!stricmp(Gamefiles[j].name, tempbuffer)) &&
            (mng_FindTrackLock(tempbuffer, PAGETYPE_GAMEFILE) == -1)) {
          // ok, we found a possibly matching script...ask if they want that
          // checked out also...
          // go ahead and delete the sucker!
          char t[256];
          sprintf(t, "Checking Out: %s", tempbuffer);
          SetStatus(t);

          if (CheckOutGamefile(tempbuffer, false))
            changed = true;
          break;
        }
      }

      strcpy(tempbuffer, savename);
      strcat(tempbuffer, ".str");
      for (j = 0; j < MAX_GAMEFILES; j++) {
        if ((Gamefiles[j].used) && (!stricmp(Gamefiles[j].name, tempbuffer)) &&
            (mng_FindTrackLock(tempbuffer, PAGETYPE_GAMEFILE) == -1)) {
          // ok, we found a possibly matching script...ask if they want that
          // checked out also...
          // go ahead and delete the sucker!
          char t[256];
          sprintf(t, "Checking Out: %s", tempbuffer);
          SetStatus(t);

          if (CheckOutGamefile(tempbuffer, false))
            changed = true;
          break;
        }
      }
    }
  }

  mng_EraseLocker();
  mem_free(index_map);

  return changed;
}

bool CScriptLevelInterface::UndoCheckOutScripts() {
  CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST_CHECKEDOUT);
  int listbox_size = list->GetCount();
  int list_size = 0;
  int i;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      list_size++;
    }
  }

  if (list_size == 0)
    return false;
  int *index_map = (int *)mem_malloc(list_size * sizeof(int));
  if (!index_map)
    return false;

  int index = 0;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      index_map[index] = i;
      index++;
    }
  }

  if (!mng_MakeLocker()) {
    mem_free(index_map);
    return false;
  }

  int j;
  bool changed = false;
  char tempbuffer[_MAX_PATH];
  char savename[_MAX_PATH];

  for (i = 0; i < list_size; i++) {
    index = index_map[i];

    list->GetText(index, tempbuffer);
    strcpy(savename, tempbuffer);
    strcat(tempbuffer, ".cpp");

    char t[256];
    sprintf(t, "UndoCheck Out: %s", tempbuffer);
    SetStatus(t);

    // this item is checked...we wish to checkin
    if (UndoCheckOutGamefile(tempbuffer))
      changed = true;

    // ok, now see if there was a DLL too
    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".dll");
    for (j = 0; j < MAX_TRACKLOCKS; j++) {
      if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
          (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
        // ok, we found a possibly matching script...ask if they want that
        // undone also...
        // go ahead and delete the sucker!
        char t[256];
        sprintf(t, "UndoCheck Out: %s", tempbuffer);
        SetStatus(t);

        if (UndoCheckOutGamefile(tempbuffer))
          changed = true;
        break;
      }
    }

    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".msg");
    for (j = 0; j < MAX_TRACKLOCKS; j++) {
      if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
          (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
        // ok, we found a possibly matching script...ask if they want that
        // undone also...
        // go ahead and delete the sucker!
        char t[256];
        sprintf(t, "UndoCheck Out: %s", tempbuffer);
        SetStatus(t);

        if (UndoCheckOutGamefile(tempbuffer))
          changed = true;
        break;
      }
    }

    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".str");
    for (j = 0; j < MAX_TRACKLOCKS; j++) {
      if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
          (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
        // ok, we found a possibly matching script...ask if they want that
        // undone also...
        // go ahead and delete the sucker!
        char t[256];
        sprintf(t, "UndoCheck Out: %s", tempbuffer);
        SetStatus(t);

        if (UndoCheckOutGamefile(tempbuffer))
          changed = true;
        break;
      }
    }
  }

  mng_EraseLocker();

  return changed;
}

bool CScriptLevelInterface::UndoCheckOutLevels() {
  CCheckListBox *list = (CCheckListBox *)GetDlgItem(IDC_LIST_CHECKEDOUT);
  int listbox_size = list->GetCount();
  int list_size = 0;
  int i;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      list_size++;
    }
  }

  if (list_size == 0)
    return false;
  int *index_map = (int *)mem_malloc(list_size * sizeof(int));
  if (!index_map)
    return false;

  int index = 0;
  for (i = 0; i < listbox_size; i++) {
    if (list->GetCheck(i)) {
      index_map[index] = i;
      index++;
    }
  }

  if (!mng_MakeLocker()) {
    mem_free(index_map);
    return false;
  }

  int j;
  bool changed = false;
  char buffer[_MAX_PATH + 200];
  char tempbuffer[_MAX_PATH];
  char savename[_MAX_PATH];

  for (i = 0; i < list_size; i++) {
    index = index_map[i];

    list->GetText(index, tempbuffer);
    strcpy(savename, tempbuffer);
    strcat(tempbuffer, ".d3l");

    char t[256];
    sprintf(t, "UndoCheck Out: %s", tempbuffer);
    SetStatus(t);

    // go ahead and check in
    if (UndoCheckOutGamefile(tempbuffer))
      changed = true;

    bool script_also;
    script_also = false;

    // ok the level has been checked in, see if there is a corresponding .cpp file
    strcpy(tempbuffer, savename);
    strcat(tempbuffer, ".cpp");
    for (j = 0; j < MAX_TRACKLOCKS; j++) {
      if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
          (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
        // ok, we found a possibly matching script...ask if they want that
        // checked in also...
        sprintf(
            buffer,
            "A possible level script (%s) was found, do you want\nto undo this check out also? (HIGHLY recommended)",
            tempbuffer);
        if (m_AutoCheckScripts || MessageBox(buffer, "Friendly Warning", MB_YESNO) == IDYES) {
          // go ahead and delete the sucker!
          char t[256];
          sprintf(t, "UndoCheck Out: %s", tempbuffer);
          SetStatus(t);

          if (UndoCheckOutGamefile(tempbuffer)) {
            changed = true;
            script_also = true;
          }
        }
        break;
      }
    }

    // ok, now see if there was a DLL too
    if (script_also) {
      strcpy(tempbuffer, savename);
      strcat(tempbuffer, ".dll");
      for (j = 0; j < MAX_TRACKLOCKS; j++) {
        if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
            (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
          // ok, we found a possibly matching script...ask if they want that
          // undone also...
          // go ahead and undo the sucker!
          char t[256];
          sprintf(t, "UndoCheck Out: %s", tempbuffer);
          SetStatus(t);

          if (UndoCheckOutGamefile(tempbuffer))
            changed = true;
          break;
        }
      }

      strcpy(tempbuffer, savename);
      strcat(tempbuffer, ".msg");
      for (j = 0; j < MAX_TRACKLOCKS; j++) {
        if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
            (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
          // ok, we found a possibly matching script...ask if they want that
          // undone also...
          // go ahead and undo the sucker!
          char t[256];
          sprintf(t, "UndoCheck Out: %s", tempbuffer);
          SetStatus(t);

          if (UndoCheckOutGamefile(tempbuffer))
            changed = true;
          break;
        }
      }

      strcpy(tempbuffer, savename);
      strcat(tempbuffer, ".str");
      for (j = 0; j < MAX_TRACKLOCKS; j++) {
        if ((GlobalTrackLocks[j].used) && (GlobalTrackLocks[j].pagetype == PAGETYPE_GAMEFILE) &&
            (!stricmp(GlobalTrackLocks[j].name, tempbuffer))) {
          // ok, we found a possibly matching script...ask if they want that
          // undone also...
          // go ahead and undo the sucker!
          char t[256];
          sprintf(t, "UndoCheck Out: %s", tempbuffer);
          SetStatus(t);

          if (UndoCheckOutGamefile(tempbuffer))
            changed = true;
          break;
        }
      }
    }
  }

  mng_EraseLocker();
  mem_free(index_map);

  return changed;
}
// returns true if the given .cpp file is has an out of date dll (or non-existant).  Working
//	dir MUST be set to data\scripts before calling this function
bool IsScriptOutofDate(char *name) {
  bool out_of_date = false;

  HANDLE hFindFilecpp = INVALID_HANDLE_VALUE, hFindFiledll = INVALID_HANDLE_VALUE;
  WIN32_FIND_DATA FindFileDatacpp, FindFileDatadll;
  char fname[_MAX_PATH], ext[_MAX_EXT];

  hFindFilecpp = FindFirstFile(name, &FindFileDatacpp);
  if (hFindFilecpp != INVALID_HANDLE_VALUE) {
    ddio_SplitPath(FindFileDatacpp.cFileName, NULL, fname, ext);
    strcat(fname, ".dll");

    hFindFiledll = FindFirstFile(fname, &FindFileDatadll);
    if (hFindFiledll == INVALID_HANDLE_VALUE ||
        CompareFileTime(&FindFileDatadll.ftLastWriteTime, &FindFileDatacpp.ftLastWriteTime) < 0) {
      // out of date
      out_of_date = true;
    }
    if (hFindFiledll != INVALID_HANDLE_VALUE)
      FindClose(hFindFiledll);
    hFindFiledll = INVALID_HANDLE_VALUE;
  } else {
    char error[_MAX_PATH];
    sprintf(error, "'%s' not found", name);
    AfxMessageBox(error);
    out_of_date = true;
  }
  if (hFindFilecpp != INVALID_HANDLE_VALUE)
    FindClose(hFindFilecpp);
  hFindFilecpp = INVALID_HANDLE_VALUE;

  return out_of_date;
}

#include "module.h"
#include "osiris_share.h"
extern tOSIRISModuleInit Osiris_module_init;
typedef char(DLLFUNCCALL *InitializeDLL_fp)(tOSIRISModuleInit *function_list);
typedef void(DLLFUNCCALL *ShutdownDLL_fp)(void);

// returns true if the given .cpp/.dll file is out of sync (or non-existant).  Working directory
// doesn't necessarily have to be set to data\scripts.
bool IsScriptOutofSync(char *name) {
  bool out_of_sync = false;
  InitializeDLL_fp initdll;
  ShutdownDLL_fp shutdowndll;
  module mod;

  char filename[_MAX_PATH], ext[_MAX_EXT];
  char path[_MAX_PATH];

  ddio_SplitPath(name, NULL, filename, ext);

  Osiris_module_init.string_count = 0;
  Osiris_module_init.string_table = NULL;
  Osiris_module_init.module_is_static = false;
  Osiris_module_init.module_identifier = 0;
  Osiris_module_init.script_identifier = filename;

  strcat(filename, ".dll");
  ddio_MakePath(path, LocalScriptDir, filename, NULL);
  mprintf(0, "	Checking %s...", filename);

  if (mod_LoadModule(&mod, path)) {
    initdll = (InitializeDLL_fp)mod_GetSymbol(&mod, "InitializeDLL", 4);
    if (initdll) {
      if (!initdll(&Osiris_module_init)) {
        // dll does not initialize...out of synce
        out_of_sync = true;
        mprintf(0, "OUT OF SYNC!\n");
      } else {
        mprintf(0, "Not Out of Sync\n");
        shutdowndll = (ShutdownDLL_fp)mod_GetSymbol(&mod, "ShutdownDLL", 0);
        if (shutdowndll) {
          shutdowndll();
        }
      }
    } else {
      // unable to load
      mprintf(0, "UNABLE TO GET INITIALIZEDLL\n");
    }

    mod_FreeModule(&mod);
  } else {
    mprintf(0, "MODULE NOT FOUND\n");
  }

  return out_of_sync;
}

// Returns true if there are out of date script files (only checks those in manage system)
int AreScriptsOutofDate(void) {
  if (Scripts_need_sync) {
    CScriptSyncDialog dlg;
    dlg.DoModal();
    Scripts_need_sync = false;
    return dlg.m_ErrorCount;
  }
  return 0;

  /*
  int count = 0;
  char old_dir[_MAX_PATH];

  ddio_GetWorkingDir(old_dir,_MAX_PATH);

  // First check for out-of-sync scripts (attempt load aigame and generic)
  mprintf(0,"Checking for out of sync scripts...\n");
  if(IsScriptOutofSync("aigame.dll")||IsScriptOutofSync("generic.dll"))
  {
          OutrageMessageBox(MBOX_OK,"WARNING! You have out of sync scripts...you MUST recompile your scripts!!!!!");
  }


  mprintf(0,"Checking for out of date scripts...");
  ddio_SetWorkingDir(LocalScriptDir);

  for (int i=0;i<MAX_GAMEFILES;i++){
          if (Gamefiles[i].used){
                  //we're only adding .d3l's to this list
                  int length = strlen(Gamefiles[i].name);
                  if(!stricmp(&Gamefiles[i].name[length-4],".cpp") && IsScriptOutofDate(Gamefiles[i].name)){
                          count++;
                  }
          }
  }

  ddio_SetWorkingDir(old_dir);

  mprintf(0,"Found %d OOD scripts\n",count);
  return count;
  */
}

/*
#include "psglob.h"
class tFindInManage
{
public:
        tFindInManage() {searching = false;}

        bool searching;
        bool in_Gamefiles;
        int curr_index;
        char glob_string[PAGENAME_LEN];
};
tFindInManage FindInManageData;

bool FindManageFirst(char *buffer,char *wildcard);
bool FindManageNext(char *buffer);
void FindManageClose(void);


bool FindManageFirst(char *buffer,char *wildcard)
{
        if(FindInManageData.searching)
                FindManageClose();

        FindInManageData.searching = true;
        strcpy(FindInManageData.glob_string,wildcard);
        FindInManageData.curr_index = 0;
        FindInManageData.in_Gamefiles = true;

        return FindManageNext(buffer);
}

bool FindManageNext(char *buffer)
{
        if(!FindInManageData.searching)
                return false;

        if(FindInManageData.in_Gamefiles)
        {

                for(;FindInManageData.curr_index<MAX_GAMEFILES;FindInManageData.curr_index++)
                {
                        if(Gamefiles[FindInManageData.curr_index].used &&
                                PSGlobMatch(FindInManageData.glob_string,Gamefiles[FindInManageData.curr_index].name,false,false))
                        {
                                //match
                                strcpy(buffer,Gamefiles[FindInManageData.curr_index].name);
                                FindInManageData.curr_index++;
                                return true;
                        }
                }

                FindInManageData.curr_index = 0;
                FindInManageData.in_Gamefiles = false;
        }

        for(;FindInManageData.curr_index<MAX_TRACKLOCKS;FindInManageData.curr_index++)
        {
                if(GlobalTrackLocks[FindInManageData.curr_index].used &&
GlobalTrackLocks[FindInManageData.curr_index].pagetype==PAGETYPE_GAMEFILE &&
                        PSGlobMatch(FindInManageData.glob_string,GlobalTrackLocks[FindInManageData.curr_index].name,false,false))
                {
                        //match
                        strcpy(buffer,GlobalTrackLocks[FindInManageData.curr_index].name);
                        FindInManageData.curr_index++;
                        return true;
                }
        }

        return false;
}

void FindManageClose(void)
{
        FindInManageData.searching = false;
}
*/