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
|
/***************************************************************************
cheat.c
MAME cheat system.
****************************************************************************
Copyright Aaron Giles
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name 'MAME' nor the names of its contributors may be
used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
****************************************************************************
Cheat XML format:
<mamecheat version="1">
<cheat desc="blah">
<parameter min="minval(0)" max="maxval(numitems)" step="stepval(1)">
<item value="itemval(previtemval|minval+stepval)">text</item>
...
</parameter>
<script state="on|off|run|change(run)">
<action condition="condexpr(1)">expression</action>
...
<output condition="condexpr(1)" format="format(required)" line="line(0)" align="left|center|right(left)">
<argument count="count(1)">expression</argument>
</output>
...
</script>
...
<comment>
... text ...
</comment>
</cheat>
...
</mamecheat>
****************************************************************************
Expressions are standard debugger expressions. Note that & and
< must be escaped per XML rules. Within attributes you must use
& and <. For tags, you can also use <![CDATA[ ... ]]>.
Each cheat has its own context-specific variables:
temp0-temp9 -- 10 temporary variables for any use
param -- the current value of the cheat parameter
frame -- the current frame index
argindex -- for arguments with multiple iterations, this is the index
By default, each cheat has 10 temporary variables that are
persistent while executing its scripts. Additional temporary
variables may be requested via the 'tempvariables' attribute
on the cheat.
****************************************************************************
Cheats are generally broken down into categories based on
which actions are defined and whether or not there is a
parameter present:
---- Actions -----
On Off Run Chg Param? Type
=== === === === ====== =================================
N N N ? None Text-only (displays text in menu)
Y N N ? None Oneshot (select to activate)
Y Y N ? None On/Off (select to toggle)
? ? Y ? None On/Off (select to toggle)
? N N Y Any Oneshot parameter (select to alter)
? Y ? ? Value Value parameter (off or a live value)
? ? Y ? Value Value parameter (off or a live value)
? Y ? ? List Item list parameter (off or a live value)
? ? Y ? List Item list parameter (off or a live value)
***************************************************************************/
#include "emu.h"
#include "emuopts.h"
#include "xmlfile.h"
#include "ui.h"
#include "uimenu.h"
#include "cheat.h"
#include "debug/debugcpu.h"
#include <ctype.h>
//**************************************************************************
// PARAMETERS
//**************************************************************************
// turn this on to enable removing duplicate cheats; not sure if we should
#define REMOVE_DUPLICATE_CHEATS 0
//**************************************************************************
// NUMBER AND FORMAT
//**************************************************************************
//-------------------------------------------------
// format - format an integer according to
// the format
//-------------------------------------------------
inline const char *number_and_format::format(astring &string) const
{
switch (m_format)
{
default:
case XML_INT_FORMAT_DECIMAL:
string.printf("%d", (UINT32)m_value);
break;
case XML_INT_FORMAT_DECIMAL_POUND:
string.printf("#%d", (UINT32)m_value);
break;
case XML_INT_FORMAT_HEX_DOLLAR:
string.printf("$%X", (UINT32)m_value);
break;
case XML_INT_FORMAT_HEX_C:
string.printf("0x%X", (UINT32)m_value);
break;
}
return string;
}
//**************************************************************************
// CHEAT PARAMETER
//**************************************************************************
//-------------------------------------------------
// cheat_parameter - constructor
//-------------------------------------------------
cheat_parameter::cheat_parameter(cheat_manager &manager, symbol_table &symbols, const char *filename, xml_data_node ¶mnode)
: m_value(0),
m_itemlist(manager.machine().m_respool)
{
// read the core attributes
m_minval = number_and_format(xml_get_attribute_int(¶mnode, "min", 0), xml_get_attribute_int_format(¶mnode, "min"));
m_maxval = number_and_format(xml_get_attribute_int(¶mnode, "max", 0), xml_get_attribute_int_format(¶mnode, "max"));
m_stepval = number_and_format(xml_get_attribute_int(¶mnode, "step", 1), xml_get_attribute_int_format(¶mnode, "step"));
// iterate over items
for (xml_data_node *itemnode = xml_get_sibling(paramnode.child, "item"); itemnode != NULL; itemnode = xml_get_sibling(itemnode->next, "item"))
{
// check for NULL text
if (itemnode->value == NULL || itemnode->value[0] == 0)
throw emu_fatalerror("%s.xml(%d): item is missing text\n", filename, itemnode->line);
// check for non-existant value
if (xml_get_attribute(itemnode, "value") == NULL)
throw emu_fatalerror("%s.xml(%d): item is value\n", filename, itemnode->line);
// extract the parameters
UINT64 value = xml_get_attribute_int(itemnode, "value", 0);
int format = xml_get_attribute_int_format(itemnode, "value");
// allocate and append a new item
item &curitem = m_itemlist.append(*auto_alloc(&manager.machine(), item(itemnode->value, value, format)));
// ensure the maximum expands to suit
m_maxval = MAX(m_maxval, curitem.value());
}
// add a variable to the symbol table for our value
symbols.add("param", symbol_table::READ_ONLY, &m_value);
}
//-------------------------------------------------
// text - return the current text for a parameter
//-------------------------------------------------
const char *cheat_parameter::text()
{
// are we a value cheat?
if (!has_itemlist())
m_curtext.format("%d (0x%X)", UINT32(m_value), UINT32(m_value));
// if not, we're an item cheat
else
{
m_curtext.format("??? (%d)", UINT32(m_value));
for (item *curitem = m_itemlist.first(); curitem != NULL; curitem = curitem->next())
if (curitem->value() == m_value)
{
m_curtext.cpy(curitem->text());
break;
}
}
return m_curtext;
}
//-------------------------------------------------
// save - save a single cheat parameter
//-------------------------------------------------
void cheat_parameter::save(mame_file &cheatfile) const
{
// output the parameter tag
mame_fprintf(&cheatfile, "\t\t<parameter");
// if no items, just output min/max/step
astring string;
if (!has_itemlist())
{
if (m_minval != 0)
mame_fprintf(&cheatfile, " min=\"%s\"", m_minval.format(string));
if (m_maxval != 0)
mame_fprintf(&cheatfile, " max=\"%s\"", m_maxval.format(string));
if (m_stepval != 1)
mame_fprintf(&cheatfile, " step=\"%s\"", m_stepval.format(string));
mame_fprintf(&cheatfile, "/>\n");
}
// iterate over items
else
{
for (const item *curitem = m_itemlist.first(); curitem != NULL; curitem = curitem->next())
mame_fprintf(&cheatfile, "\t\t\t<item value=\"%s\">%s</item>\n", curitem->value().format(string), curitem->text());
mame_fprintf(&cheatfile, "\t\t</parameter>\n");
}
}
//-------------------------------------------------
// set_minimum_state - set the minimum state
//-------------------------------------------------
bool cheat_parameter::set_minimum_state()
{
UINT64 origvalue = m_value;
// set based on whether we have an item list
m_value = (!has_itemlist()) ? m_minval : m_itemlist.first()->value();
return (m_value != origvalue);
}
//-------------------------------------------------
// set_minimum_state - set the previous state
//-------------------------------------------------
bool cheat_parameter::set_prev_state()
{
UINT64 origvalue = m_value;
// are we a value cheat?
if (!has_itemlist())
{
if (m_value < m_minval + m_stepval)
m_value = m_minval;
else
m_value -= m_stepval;
}
// if not, we're an item cheat
else
{
item *curitem, *previtem = NULL;
for (curitem = m_itemlist.first(); curitem != NULL; previtem = curitem, curitem = curitem->next())
if (curitem->value() == m_value)
break;
if (previtem != NULL)
m_value = previtem->value();
}
return (m_value != origvalue);
}
//-------------------------------------------------
// set_next_state - advance to the next state
//-------------------------------------------------
bool cheat_parameter::set_next_state()
{
UINT64 origvalue = m_value;
// are we a value cheat?
if (!has_itemlist())
{
if (m_value > m_maxval - m_stepval)
m_value = m_maxval;
else
m_value += m_stepval;
}
// if not, we're an item cheat
else
{
item *curitem;
for (curitem = m_itemlist.first(); curitem != NULL; curitem = curitem->next())
if (curitem->value() == m_value)
break;
if (curitem != NULL && curitem->next() != NULL)
m_value = curitem->next()->value();
}
return (m_value != origvalue);
}
//**************************************************************************
// CHEAT SCRIPT
//**************************************************************************
//-------------------------------------------------
// cheat_script - constructor
//-------------------------------------------------
cheat_script::cheat_script(cheat_manager &manager, symbol_table &symbols, const char *filename, xml_data_node &scriptnode)
: m_entrylist(manager.machine().m_respool),
m_state(SCRIPT_STATE_RUN)
{
// read the core attributes
const char *state = xml_get_attribute_string(&scriptnode, "state", "run");
if (strcmp(state, "on") == 0)
m_state = SCRIPT_STATE_ON;
else if (strcmp(state, "off") == 0)
m_state = SCRIPT_STATE_OFF;
else if (strcmp(state, "change") == 0)
m_state = SCRIPT_STATE_CHANGE;
else if (strcmp(state, "run") != 0)
throw emu_fatalerror("%s.xml(%d): invalid script state '%s'\n", filename, scriptnode.line, state);
// iterate over nodes within the script
for (xml_data_node *entrynode = scriptnode.child; entrynode != NULL; entrynode = entrynode->next)
{
// handle action nodes
if (strcmp(entrynode->name, "action") == 0)
m_entrylist.append(*auto_alloc(&manager.machine(), script_entry(manager, symbols, filename, *entrynode, true)));
// handle output nodes
else if (strcmp(entrynode->name, "output") == 0)
m_entrylist.append(*auto_alloc(&manager.machine(), script_entry(manager, symbols, filename, *entrynode, false)));
// anything else is ignored
else
{
mame_printf_warning("%s.xml(%d): unknown script item '%s' will be lost if saved\n", filename, entrynode->line, entrynode->name);
continue;
}
}
}
//-------------------------------------------------
// execute - execute ourself
//-------------------------------------------------
void cheat_script::execute(cheat_manager &manager, UINT64 &argindex)
{
// do nothing if disabled
if (!manager.enabled())
return;
// iterate over entries
for (script_entry *entry = m_entrylist.first(); entry != NULL; entry = entry->next())
entry->execute(manager, argindex);
}
//-------------------------------------------------
// save - save a single cheat script
//-------------------------------------------------
void cheat_script::save(mame_file &cheatfile) const
{
// output the script tag
mame_fprintf(&cheatfile, "\t\t<script");
switch (m_state)
{
case SCRIPT_STATE_OFF: mame_fprintf(&cheatfile, " state=\"off\""); break;
case SCRIPT_STATE_ON: mame_fprintf(&cheatfile, " state=\"on\""); break;
default:
case SCRIPT_STATE_RUN: mame_fprintf(&cheatfile, " state=\"run\""); break;
case SCRIPT_STATE_CHANGE: mame_fprintf(&cheatfile, " state=\"change\""); break;
}
mame_fprintf(&cheatfile, ">\n");
// output entries
for (const script_entry *entry = m_entrylist.first(); entry != NULL; entry = entry->next())
entry->save(cheatfile);
// close the tag
mame_fprintf(&cheatfile, "\t\t</script>\n");
}
//-------------------------------------------------
// script_entry - constructor
//-------------------------------------------------
cheat_script::script_entry::script_entry(cheat_manager &manager, symbol_table &symbols, const char *filename, xml_data_node &entrynode, bool isaction)
: m_next(NULL),
m_condition(&symbols),
m_expression(&symbols),
m_arglist(manager.machine().m_respool)
{
const char *expression = NULL;
try
{
// read the condition if present
expression = xml_get_attribute_string(&entrynode, "condition", NULL);
if (expression != NULL)
m_condition.parse(expression);
// if this is an action, parse the expression
if (isaction)
{
expression = entrynode.value;
if (expression == NULL || expression[0] == 0)
throw emu_fatalerror("%s.xml(%d): missing expression in action tag\n", filename, entrynode.line);
m_expression.parse(expression);
}
// otherwise, parse the attributes and arguments
else
{
// extract format
const char *format = xml_get_attribute_string(&entrynode, "format", NULL);
if (format == NULL || format[0] == 0)
throw emu_fatalerror("%s.xml(%d): missing format in output tag\n", filename, entrynode.line);
m_format.cpy(format);
// extract other attributes
m_line = xml_get_attribute_int(&entrynode, "line", 0);
m_justify = JUSTIFY_LEFT;
const char *align = xml_get_attribute_string(&entrynode, "align", "left");
if (strcmp(align, "center") == 0)
m_justify = JUSTIFY_CENTER;
else if (strcmp(align, "right") == 0)
m_justify = JUSTIFY_RIGHT;
else if (strcmp(align, "left") != 0)
throw emu_fatalerror("%s.xml(%d): invalid alignment '%s' specified\n", filename, entrynode.line, align);
// then parse arguments
int totalargs = 0;
for (xml_data_node *argnode = xml_get_sibling(entrynode.child, "argument"); argnode != NULL; argnode = xml_get_sibling(argnode->next, "argument"))
{
output_argument &curarg = m_arglist.append(*auto_alloc(&manager.machine(), output_argument(manager, symbols, filename, *argnode)));
// verify we didn't overrun the argument count
totalargs += curarg.count();
if (totalargs > MAX_ARGUMENTS)
throw emu_fatalerror("%s.xml(%d): too many arguments (found %d, max is %d)\n", filename, argnode->line, totalargs, MAX_ARGUMENTS);
}
// validate the format against the arguments
validate_format(filename, entrynode.line);
}
}
catch (expression_error &err)
{
throw emu_fatalerror("%s.xml(%d): error parsing cheat expression \"%s\" (%s)\n", filename, entrynode.line, expression, err.code_string());
}
}
//-------------------------------------------------
// execute - execute a single script entry
//-------------------------------------------------
void cheat_script::script_entry::execute(cheat_manager &manager, UINT64 &argindex)
{
// evaluate the condition
if (!m_condition.is_empty())
{
try
{
UINT64 result = m_condition.execute();
if (result == 0)
return;
}
catch (expression_error &err)
{
mame_printf_warning("Error executing conditional expression \"%s\": %s\n", m_condition.original_string(), err.code_string());
return;
}
}
// if there is an action, execute it
if (!m_expression.is_empty())
{
try
{
m_expression.execute();
}
catch (expression_error &err)
{
mame_printf_warning("Error executing expression \"%s\": %s\n", m_expression.original_string(), err.code_string());
}
}
// if there is a string to display, compute it
if (m_format)
{
// iterate over arguments and evaluate them
UINT64 params[MAX_ARGUMENTS];
int curarg = 0;
for (output_argument *arg = m_arglist.first(); arg != NULL; arg = arg->next())
curarg += arg->values(argindex, ¶ms[curarg]);
// generate the astring
manager.get_output_astring(m_line, m_justify).printf(m_format,
(UINT32)params[0], (UINT32)params[1], (UINT32)params[2], (UINT32)params[3],
(UINT32)params[4], (UINT32)params[5], (UINT32)params[6], (UINT32)params[7],
(UINT32)params[8], (UINT32)params[9], (UINT32)params[10], (UINT32)params[11],
(UINT32)params[12], (UINT32)params[13], (UINT32)params[14], (UINT32)params[15],
(UINT32)params[16], (UINT32)params[17], (UINT32)params[18], (UINT32)params[19],
(UINT32)params[20], (UINT32)params[21], (UINT32)params[22], (UINT32)params[23],
(UINT32)params[24], (UINT32)params[25], (UINT32)params[26], (UINT32)params[27],
(UINT32)params[28], (UINT32)params[29], (UINT32)params[30], (UINT32)params[31]);
}
}
//-------------------------------------------------
// save - save a single action or output
//-------------------------------------------------
void cheat_script::script_entry::save(mame_file &cheatfile) const
{
astring tempstring;
// output an action
if (!m_format)
{
mame_fprintf(&cheatfile, "\t\t\t<action");
if (!m_condition.is_empty())
mame_fprintf(&cheatfile, " condition=\"%s\"", cheat_manager::quote_expression(tempstring, m_condition));
mame_fprintf(&cheatfile, ">%s</action>\n", cheat_manager::quote_expression(tempstring, m_expression));
}
// output an output
else
{
mame_fprintf(&cheatfile, "\t\t\t<output format=\"%s\"", m_format.cstr());
if (!m_condition.is_empty())
mame_fprintf(&cheatfile, " condition=\"%s\"", cheat_manager::quote_expression(tempstring, m_condition));
if (m_line != 0)
mame_fprintf(&cheatfile, " line=\"%d\"", m_line);
if (m_justify == JUSTIFY_CENTER)
mame_fprintf(&cheatfile, " align=\"center\"");
else if (m_justify == JUSTIFY_RIGHT)
mame_fprintf(&cheatfile, " align=\"right\"");
if (m_arglist.count() == 0)
mame_fprintf(&cheatfile, " />\n");
// output arguments
else
{
mame_fprintf(&cheatfile, ">\n");
for (const output_argument *curarg = m_arglist.first(); curarg != NULL; curarg = curarg->next())
curarg->save(cheatfile);
mame_fprintf(&cheatfile, "\t\t\t</output>\n");
}
}
}
//-------------------------------------------------
// validate_format - check that a format string
// has the correct number and type of arguments
//-------------------------------------------------
void cheat_script::script_entry::validate_format(const char *filename, int line)
{
// first count arguments
int argsprovided = 0;
for (const output_argument *curarg = m_arglist.first(); curarg != NULL; curarg = curarg->next())
argsprovided += curarg->count();
// now scan the string for valid argument usage
const char *p = strchr(m_format, '%');
int argscounted = 0;
while (p != NULL)
{
// skip past any valid attributes
p++;
while (strchr("lh0123456789.-+ #", *p) != NULL)
p++;
// look for a valid type
if (strchr("cdiouxX", *p) == NULL)
throw emu_fatalerror("%s.xml(%d): invalid format specification \"%s\"\n", filename, line, m_format.cstr());
argscounted++;
// look for the next one
p = strchr(p, '%');
}
// did we match?
if (argscounted < argsprovided)
throw emu_fatalerror("%s.xml(%d): too many arguments provided (%d) for format \"%s\"\n", filename, line, argsprovided, m_format.cstr());
if (argscounted > argsprovided)
throw emu_fatalerror("%s.xml(%d): not enough arguments provided (%d) for format \"%s\"\n", filename, line, argsprovided, m_format.cstr());
}
//-------------------------------------------------
// output_argument - constructor
//-------------------------------------------------
cheat_script::script_entry::output_argument::output_argument(cheat_manager &manager, symbol_table &symbols, const char *filename, xml_data_node &argnode)
: m_next(NULL),
m_expression(&symbols),
m_count(0)
{
// first extract attributes
m_count = xml_get_attribute_int(&argnode, "count", 1);
// read the expression
const char *expression = argnode.value;
if (expression == NULL || expression[0] == 0)
throw emu_fatalerror("%s.xml(%d): missing expression in argument tag\n", filename, argnode.line);
// parse it
try
{
m_expression.parse(expression);
}
catch (expression_error &err)
{
throw emu_fatalerror("%s.xml(%d): error parsing cheat expression \"%s\" (%s)\n", filename, argnode.line, expression, err.code_string());
}
}
//-------------------------------------------------
// value - return the evaluated value of the
// given output argument
//-------------------------------------------------
int cheat_script::script_entry::output_argument::values(UINT64 &argindex, UINT64 *result)
{
for (argindex = 0; argindex < m_count; argindex++)
{
try
{
result[argindex] = m_expression.execute();
}
catch (expression_error &err)
{
mame_printf_warning("Error executing argument expression \"%s\": %s\n", m_expression.original_string(), err.code_string());
}
}
return m_count;
}
//-------------------------------------------------
// save - save a single output argument
//-------------------------------------------------
void cheat_script::script_entry::output_argument::save(mame_file &cheatfile) const
{
astring tempstring;
mame_fprintf(&cheatfile, "\t\t\t\t<argument");
if (m_count != 1)
mame_fprintf(&cheatfile, " count=\"%d\"", (int)m_count);
mame_fprintf(&cheatfile, ">%s</argument>\n", cheat_manager::quote_expression(tempstring, m_expression));
}
//**************************************************************************
// CHEAT ENTRY
//**************************************************************************
//-------------------------------------------------
// cheat_entry - constructor
//-------------------------------------------------
cheat_entry::cheat_entry(cheat_manager &manager, symbol_table &globaltable, const char *filename, xml_data_node &cheatnode)
: m_manager(manager),
m_next(NULL),
m_parameter(NULL),
m_on_script(NULL),
m_off_script(NULL),
m_change_script(NULL),
m_run_script(NULL),
m_symbols(&manager.machine(), &globaltable),
m_state(SCRIPT_STATE_OFF),
m_numtemp(DEFAULT_TEMP_VARIABLES),
m_argindex(0)
{
// reset scripts
try
{
// pull the variable count out ahead of things
int tempcount = xml_get_attribute_int(&cheatnode, "tempvariables", DEFAULT_TEMP_VARIABLES);
if (tempcount < 1)
throw emu_fatalerror("%s.xml(%d): invalid tempvariables attribute (%d)\n", filename, cheatnode.line, tempcount);
// allocate memory for the cheat
m_numtemp = tempcount;
// get the description
const char *description = xml_get_attribute_string(&cheatnode, "desc", NULL);
if (description == NULL || description[0] == 0)
throw emu_fatalerror("%s.xml(%d): empty or missing desc attribute on cheat\n", filename, cheatnode.line);
m_description = description;
// create the symbol table
m_symbols.add("argindex", symbol_table::READ_ONLY, &m_argindex);
astring tempname;
for (int curtemp = 0; curtemp < tempcount; curtemp++)
m_symbols.add(tempname.format("temp%d", curtemp), symbol_table::READ_WRITE);
// read the first comment node
xml_data_node *commentnode = xml_get_sibling(cheatnode.child, "comment");
if (commentnode != NULL)
{
// set the value if not NULL
if (commentnode->value != NULL && commentnode->value[0] != 0)
m_comment.cpy(commentnode->value);
// only one comment is kept
commentnode = xml_get_sibling(commentnode->next, "comment");
if (commentnode != NULL)
mame_printf_warning("%s.xml(%d): only one comment node is retained; ignoring additional nodes\n", filename, commentnode->line);
}
// read the first parameter node
xml_data_node *paramnode = xml_get_sibling(cheatnode.child, "parameter");
if (paramnode != NULL)
{
// load this parameter
m_parameter = auto_alloc(&manager.machine(), cheat_parameter(manager, m_symbols, filename, *paramnode));
// only one parameter allowed
paramnode = xml_get_sibling(paramnode->next, "parameter");
if (paramnode != NULL)
mame_printf_warning("%s.xml(%d): only one parameter node allowed; ignoring additional nodes\n", filename, paramnode->line);
}
// read the script nodes
for (xml_data_node *scriptnode = xml_get_sibling(cheatnode.child, "script"); scriptnode != NULL; scriptnode = xml_get_sibling(scriptnode->next, "script"))
{
// load this entry
cheat_script *curscript = auto_alloc(&manager.machine(), cheat_script(manager, m_symbols, filename, *scriptnode));
// if we have a script already for this slot, it is an error
cheat_script *&slot = script_for_state(curscript->state());
if (slot != NULL)
mame_printf_warning("%s.xml(%d): only one on script allowed; ignoring additional scripts\n", filename, scriptnode->line);
else
slot = curscript;
}
}
catch (emu_fatalerror &)
{
// call our destructor to clean up and re-throw
this->~cheat_entry();
throw;
}
}
//-------------------------------------------------
// ~cheat_entry - destructor
//-------------------------------------------------
cheat_entry::~cheat_entry()
{
auto_free(&m_manager.machine(), m_on_script);
auto_free(&m_manager.machine(), m_off_script);
auto_free(&m_manager.machine(), m_change_script);
auto_free(&m_manager.machine(), m_run_script);
auto_free(&m_manager.machine(), m_parameter);
}
//-------------------------------------------------
// save - save a single cheat entry
//-------------------------------------------------
void cheat_entry::save(mame_file &cheatfile) const
{
// determine if we have scripts
bool has_scripts = (m_off_script != NULL || m_on_script != NULL || m_run_script != NULL || m_change_script != NULL);
// output the cheat tag
mame_fprintf(&cheatfile, "\t<cheat desc=\"%s\"", m_description.cstr());
if (m_numtemp != DEFAULT_TEMP_VARIABLES)
mame_fprintf(&cheatfile, " tempvariables=\"%d\"", m_numtemp);
if (!m_comment && m_parameter == NULL && !has_scripts)
mame_fprintf(&cheatfile, " />\n");
else
{
mame_fprintf(&cheatfile, ">\n");
// save the comment
if (m_comment)
mame_fprintf(&cheatfile, "\t\t<comment><![CDATA[\n%s\n\t\t]]></comment>\n", m_comment.cstr());
// output the parameter, if present
if (m_parameter != NULL)
m_parameter->save(cheatfile);
// output the script nodes
if (m_on_script != NULL)
m_on_script->save(cheatfile);
if (m_off_script != NULL)
m_off_script->save(cheatfile);
if (m_change_script != NULL)
m_change_script->save(cheatfile);
if (m_run_script != NULL)
m_run_script->save(cheatfile);
// close the cheat tag
mame_fprintf(&cheatfile, "\t</cheat>\n");
}
}
//-------------------------------------------------
// activate - activate a oneshot cheat
//-------------------------------------------------
bool cheat_entry::activate()
{
bool changed = false;
// if cheats have been toggled off no point in even trying to do anything
if (!m_manager.enabled())
return changed;
// if we're a oneshot cheat, execute the "on" script and indicate change
if (is_oneshot())
{
execute_on_script();
changed = true;
popmessage("Activated %s", m_description.cstr());
}
// if we're a oneshot parameter cheat and we're active, execute the "state change" script and indicate change
else if (is_oneshot_parameter() && m_state != SCRIPT_STATE_OFF)
{
execute_change_script();
changed = true;
popmessage("Activated\n %s = %s", m_description.cstr(), m_parameter->text());
}
return changed;
}
//-------------------------------------------------
// select_default_state - select the default
// state for a cheat, or activate a oneshot cheat
//-------------------------------------------------
bool cheat_entry::select_default_state()
{
bool changed = false;
// if we're a oneshot cheat, there is no default state
if (is_oneshot())
;
// all other types switch to the "off" state
else
changed = set_state(SCRIPT_STATE_OFF);
return changed;
}
//-------------------------------------------------
// select_previous_state - select the previous
// state for a cheat
//-------------------------------------------------
bool cheat_entry::select_previous_state()
{
bool changed = false;
// if we're a oneshot, there is no previous state
if (is_oneshot())
;
// if we're on/off, toggle to off
else if (is_onoff())
changed = set_state(SCRIPT_STATE_OFF);
// if we have a parameter, set the previous state
else if (m_parameter != NULL)
{
// if we're at our minimum, turn off
if (m_parameter->is_minimum())
changed = set_state(SCRIPT_STATE_OFF);
else
{
// if we changed, ensure we are in the running state and signal state change
changed = m_parameter->set_prev_state();
if (changed)
{
set_state(SCRIPT_STATE_RUN);
if (!is_oneshot_parameter())
execute_change_script();
}
}
}
return changed;
}
//-------------------------------------------------
// select_next_state - select the next state for
// a cheat
//-------------------------------------------------
bool cheat_entry::select_next_state()
{
bool changed = false;
// if we're a oneshot, there is no next state
if (is_oneshot())
;
// if we're on/off, toggle to running state
else if (is_onoff())
changed = set_state(SCRIPT_STATE_RUN);
// if we have a parameter, set the next state
else if (m_parameter != NULL)
{
// if we're off, switch on to the minimum state
if (m_state == SCRIPT_STATE_OFF)
{
changed = set_state(SCRIPT_STATE_RUN);
m_parameter->set_minimum_state();
}
// otherwise, switch to the next state
else
changed = m_parameter->set_next_state();
// if we changed, signal a state change
if (changed && !is_oneshot_parameter())
execute_change_script();
}
return changed;
}
//-------------------------------------------------
// menu_text - return the text needed to display
// this cheat in a menu item
//-------------------------------------------------
void cheat_entry::menu_text(astring &description, astring &state, UINT32 &flags)
{
// description is standard
description.cpy(m_description);
state.reset();
flags = 0;
// some cheat entries are just text for display
if (is_text_only())
{
if (description)
{
description.trimspace();
if (!description)
description.cpy(MENU_SEPARATOR_ITEM);
}
flags = MENU_FLAG_DISABLE;
}
// if we have no parameter and no run or off script, it's a oneshot cheat
else if (is_oneshot())
state.cpy("Set");
// if we have no parameter, it's just on/off
else if (is_onoff())
{
state.cpy((m_state == SCRIPT_STATE_RUN) ? "On" : "Off");
flags = (m_state != 0) ? MENU_FLAG_LEFT_ARROW : MENU_FLAG_RIGHT_ARROW;
}
// if we have a value parameter, compute it
else if (m_parameter != NULL)
{
if (m_state == SCRIPT_STATE_OFF)
{
state.cpy(is_oneshot_parameter() ? "Set" : "Off");
flags = MENU_FLAG_RIGHT_ARROW;
}
else
{
state.cpy(m_parameter->text());
flags = MENU_FLAG_LEFT_ARROW;
if (!m_parameter->is_maximum())
flags |= MENU_FLAG_RIGHT_ARROW;
}
}
}
//-------------------------------------------------
// set_state - switch to the given state
//-------------------------------------------------
bool cheat_entry::set_state(script_state newstate)
{
// if we're already in the state, indicate no change
if (m_state == newstate)
return false;
// change to the state and run the appropriate script
m_state = newstate;
if (newstate == SCRIPT_STATE_OFF)
execute_off_script();
else if (newstate == SCRIPT_STATE_ON || newstate == SCRIPT_STATE_RUN)
execute_on_script();
return true;
}
//-------------------------------------------------
// script_for_state - get a reference to the
// given script pointer
//-------------------------------------------------
cheat_script *&cheat_entry::script_for_state(script_state state)
{
switch (state)
{
case SCRIPT_STATE_ON: return m_on_script;
case SCRIPT_STATE_OFF: return m_off_script;
case SCRIPT_STATE_CHANGE: return m_change_script;
default:
case SCRIPT_STATE_RUN: return m_run_script;
}
}
//**************************************************************************
// CHEAT MANAGER
//**************************************************************************
//-------------------------------------------------
// cheat_manager - constructor
//-------------------------------------------------
cheat_manager::cheat_manager(running_machine &machine)
: m_machine(machine),
m_cheatlist(machine.m_respool),
m_disabled(true),
m_symtable(&machine)
{
// if the cheat engine is disabled, we're done
if (!options_get_bool(machine.options(), OPTION_CHEAT))
return;
// request a callback
machine.add_notifier(MACHINE_NOTIFY_FRAME, frame_update_static);
// create a global symbol table
m_symtable.add("frame", symbol_table::READ_ONLY, &m_framecount);
m_symtable.add("frombcd", NULL, 1, 1, execute_frombcd);
m_symtable.add("tobcd", NULL, 1, 1, execute_tobcd);
// we rely on the debugger expression callbacks; if the debugger isn't
// enabled, we must jumpstart them manually
if ((machine.debug_flags & DEBUG_FLAG_ENABLED) == 0)
debug_cpu_init(&machine);
// configure for memory access (shared with debugger)
debug_cpu_configure_memory(machine, m_symtable);
// load the cheats
reload();
}
//-------------------------------------------------
// set_enable - globally enable or disable the
// cheat engine
//-------------------------------------------------
void cheat_manager::set_enable(bool enable)
{
// if the cheat engine is disabled, we're done
if (!options_get_bool(m_machine.options(), OPTION_CHEAT))
return;
// if we're enabled currently and we don't want to be, turn things off
if (!m_disabled && !enable)
{
// iterate over running cheats and execute any OFF Scripts
for (cheat_entry *cheat = m_cheatlist.first(); cheat != NULL; cheat = cheat->next())
if (cheat->state() == SCRIPT_STATE_RUN)
cheat->execute_off_script();
popmessage("Cheats Disabled");
m_disabled = true;
}
// if we're disabled currently and we want to be enabled, turn things on
else if (m_disabled && enable)
{
// iterate over running cheats and execute any ON Scripts
m_disabled = false;
for (cheat_entry *cheat = m_cheatlist.first(); cheat != NULL; cheat = cheat->next())
if (cheat->state() == SCRIPT_STATE_RUN)
cheat->execute_on_script();
popmessage("Cheats Enabled");
}
}
//-------------------------------------------------
// reload - re-initialize the cheat engine, and
// and reload the cheat file(s)
//-------------------------------------------------
void cheat_manager::reload()
{
// if the cheat engine is disabled, we're done
if (!options_get_bool(m_machine.options(), OPTION_CHEAT))
return;
// free everything
m_cheatlist.reset();
// reset state
m_framecount = 0;
m_numlines = 0;
m_lastline = 0;
m_disabled = false;
// load the cheat file, MESS will load a crc32.xml ( eg. 01234567.xml )
// and MAME will load gamename.xml
device_image_interface *image = NULL;
for (bool gotone = m_machine.m_devicelist.first(image); gotone; gotone = image->next(image))
if (image->exists())
{
// if we are loading through software lists, try to load shortname.xml
if (image->software_entry() != NULL)
{
load_cheats(image->basename());
break;
}
else
{
UINT32 crc = image->crc();
if (crc != 0)
{
astring filename;
filename.printf("%08X", crc);
load_cheats(filename);
break;
}
}
}
// if we haven't found the cheats yet, load by basename
if (m_cheatlist.count() == 0)
load_cheats(m_machine.basename());
// temporary: save the file back out as output.xml for comparison
if (m_cheatlist.count() != 0)
save_all("output");
}
//-------------------------------------------------
// cheat_list_save - save a cheat file from
// memory to the given filename
//-------------------------------------------------
bool cheat_manager::save_all(const char *filename)
{
// open the file with the proper name
astring fname(filename, ".xml");
mame_file *cheatfile;
file_error filerr = mame_fopen(SEARCHPATH_CHEAT, fname, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS, &cheatfile);
// if that failed, return nothing
if (filerr != FILERR_NONE)
return false;
// wrap the rest of catch errors
try
{
// output the outer layers
mame_fprintf(cheatfile, "<?xml version=\"1.0\"?>\n");
mame_fprintf(cheatfile, "<!-- This file is autogenerated; comments and unknown tags will be stripped -->\n");
mame_fprintf(cheatfile, "<mamecheat version=\"%d\">\n", CHEAT_VERSION);
// iterate over cheats in the list and save them
for (cheat_entry *cheat = m_cheatlist.first(); cheat != NULL; cheat = cheat->next())
cheat->save(*cheatfile);
// close out the file
mame_fprintf(cheatfile, "</mamecheat>\n");
mame_fclose(cheatfile);
return true;
}
// catch errors and cleanup
catch (emu_fatalerror &err)
{
mame_printf_error("%s\n", err.string());
mame_fclose(cheatfile);
osd_rmfile(filename);
}
return false;
}
//-------------------------------------------------
// render_text - called by the UI system to
// render text
//-------------------------------------------------
void cheat_manager::render_text(render_container &container)
{
// render any text and free it along the way
for (int linenum = 0; linenum < ARRAY_LENGTH(m_output); linenum++)
if (m_output[linenum])
{
// output the text
ui_draw_text_full(&container, m_output[linenum],
0.0f, (float)linenum * ui_get_line_height(m_machine), 1.0f,
m_justify[linenum], WRAP_NEVER, DRAW_OPAQUE,
ARGB_WHITE, ARGB_BLACK, NULL, NULL);
}
}
//-------------------------------------------------
// get_output_astring - return a reference to
// the given row's string, and set the
// justification
//-------------------------------------------------
astring &cheat_manager::get_output_astring(int row, int justify)
{
// if the row is not specified, grab the next one
if (row == 0)
row = (m_lastline >= 0) ? m_lastline + 1 : m_lastline - 1;
// remember the last request
m_lastline = row;
// invert if negative
row = (row < 0) ? m_numlines + row : row - 1;
// clamp within range
row = MAX(row, 0);
row = MIN(row, m_numlines - 1);
// return the appropriate string
m_justify[row] = justify;
return m_output[row];
}
//-------------------------------------------------
// quote_expression - quote an expression
// string so that it is valid to embed in an XML
// document
//-------------------------------------------------
const char *cheat_manager::quote_expression(astring &string, const parsed_expression &expression)
{
string.cpy(expression.original_string());
string.replace(0, " && ", " and ");
string.replace(0, " &&", " and ");
string.replace(0, "&& ", " and ");
string.replace(0, "&&", " and ");
string.replace(0, " & ", " band ");
string.replace(0, " &", " band ");
string.replace(0, "& ", " band ");
string.replace(0, "&", " band ");
string.replace(0, " <= ", " le ");
string.replace(0, " <=", " le ");
string.replace(0, "<= ", " le ");
string.replace(0, "<=", " le ");
string.replace(0, " < ", " lt ");
string.replace(0, " <", " lt ");
string.replace(0, "< ", " lt ");
string.replace(0, "<", " lt ");
string.replace(0, " << ", " lshift ");
string.replace(0, " <<", " lshift ");
string.replace(0, "<< ", " lshift ");
string.replace(0, "<<", " lshift ");
return string;
}
//-------------------------------------------------
// execute_frombcd - convert a value from BCD
//-------------------------------------------------
UINT64 cheat_manager::execute_frombcd(symbol_table &table, void *ref, int params, const UINT64 *param)
{
UINT64 value = param[0];
UINT64 multiplier = 1;
UINT64 result = 0;
while (value != 0)
{
result += (value & 0x0f) * multiplier;
value >>= 4;
multiplier *= 10;
}
return result;
}
//-------------------------------------------------
// execute_tobcd - convert a value to BCD
//-------------------------------------------------
UINT64 cheat_manager::execute_tobcd(symbol_table &table, void *ref, int params, const UINT64 *param)
{
UINT64 value = param[0];
UINT64 result = 0;
UINT8 shift = 0;
while (value != 0)
{
result += (value % 10) << shift;
value /= 10;
shift += 4;
}
return result;
}
//-------------------------------------------------
// frame_update - per-frame callback
//-------------------------------------------------
void cheat_manager::frame_update_static(running_machine &machine)
{
machine.cheat().frame_update();
}
void cheat_manager::frame_update()
{
// set up for accumulating output
m_lastline = 0;
m_numlines = floor(1.0f / ui_get_line_height(m_machine));
m_numlines = MIN(m_numlines, ARRAY_LENGTH(m_output));
for (int linenum = 0; linenum < ARRAY_LENGTH(m_output); linenum++)
m_output[linenum].reset();
// iterate over running cheats and execute them
for (cheat_entry *cheat = m_cheatlist.first(); cheat != NULL; cheat = cheat->next())
cheat->frame_update();
// increment the frame counter
m_framecount++;
}
//-------------------------------------------------
// load_cheats - load a cheat file into memory
// and create the cheat entry list
//-------------------------------------------------
void cheat_manager::load_cheats(const char *filename)
{
xml_data_node *rootnode = NULL;
mame_file *cheatfile = NULL;
try
{
// open the file with the proper name
astring fname(filename, ".xml");
file_error filerr = mame_fopen(SEARCHPATH_CHEAT, fname, OPEN_FLAG_READ, &cheatfile);
// loop over all instrances of the files found in our search paths
while (filerr == FILERR_NONE)
{
mame_printf_verbose("Loading cheats file from %s\n", mame_file_full_name(cheatfile).cstr());
// read the XML file into internal data structures
xml_parse_options options = { 0 };
xml_parse_error error;
options.error = &error;
rootnode = xml_file_read(mame_core_file(cheatfile), &options);
// if unable to parse the file, just bail
if (rootnode == NULL)
throw emu_fatalerror("%s.xml(%d): error parsing XML (%s)\n", filename, error.error_line, error.error_message);
// find the layout node
xml_data_node *mamecheatnode = xml_get_sibling(rootnode->child, "mamecheat");
if (mamecheatnode == NULL)
throw emu_fatalerror("%s.xml: missing mamecheatnode node", filename);
// validate the config data version
int version = xml_get_attribute_int(mamecheatnode, "version", 0);
if (version != CHEAT_VERSION)
throw emu_fatalerror("%s.xml(%d): Invalid cheat XML file: unsupported version", filename, mamecheatnode->line);
// parse all the elements
for (xml_data_node *cheatnode = xml_get_sibling(mamecheatnode->child, "cheat"); cheatnode != NULL; cheatnode = xml_get_sibling(cheatnode->next, "cheat"))
{
// load this entry
cheat_entry *curcheat = auto_alloc(&m_machine, cheat_entry(*this, m_symtable, filename, *cheatnode));
// make sure we're not a duplicate
cheat_entry *scannode = NULL;
if (REMOVE_DUPLICATE_CHEATS)
for (scannode = m_cheatlist.first(); scannode != NULL; scannode = scannode->next())
if (strcmp(scannode->description(), curcheat->description()) == 0)
{
mame_printf_verbose("Ignoring duplicate cheat '%s' from file %s\n", curcheat->description(), mame_file_full_name(cheatfile).cstr());
break;
}
// add to the end of the list
if (scannode == NULL)
m_cheatlist.append(*curcheat);
else
auto_free(&m_machine, curcheat);
}
// free the file and loop for the next one
xml_file_free(rootnode);
// open the next file in sequence
filerr = mame_fclose_and_open_next(&cheatfile, fname, OPEN_FLAG_READ);
}
}
// handle errors cleanly
catch (emu_fatalerror &err)
{
mame_printf_error("%s\n", err.string());
m_cheatlist.reset();
if (rootnode != NULL)
xml_file_free(rootnode);
if (cheatfile != NULL)
mame_fclose(cheatfile);
}
}
|