forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cheevos.c
2367 lines (1963 loc) · 58.2 KB
/
cheevos.c
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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2015-2016 - Andre Leiradella
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <ctype.h>
#include <formats/jsonsax.h>
#include <streams/file_stream.h>
#include <rhash.h>
#include <libretro.h>
#include "cheevos.h"
#include "command.h"
#include "dynamic.h"
#include "system.h"
#include "network/net_http_special.h"
#include "tasks/tasks_internal.h"
#include "configuration.h"
#include "performance_counters.h"
#include "msg_hash.h"
#include "runloop.h"
#include "core.h"
#ifdef HAVE_MENU
#include "menu/menu_driver.h"
#include "menu/menu_entries.h"
#endif
#include "verbosity.h"
/* Define this macro to prevent cheevos from being deactivated. */
#undef CHEEVOS_DONT_DEACTIVATE
/* Define this macro to log URLs (will log the user token). */
#undef CHEEVOS_LOG_URLS
/* Define this macro to dump all cheevos' addresses. */
#undef CHEEVOS_DUMP_ADDRS
/* Define this macro to remove HTTP timeouts. */
#undef CHEEVOS_NO_TIMEOUT
#define JSON_KEY_GAMEID 0xb4960eecU
#define JSON_KEY_ACHIEVEMENTS 0x69749ae1U
#define JSON_KEY_ID 0x005973f2U
#define JSON_KEY_MEMADDR 0x1e76b53fU
#define JSON_KEY_TITLE 0x0e2a9a07U
#define JSON_KEY_DESCRIPTION 0xe61a1f69U
#define JSON_KEY_POINTS 0xca8fce22U
#define JSON_KEY_AUTHOR 0xa804edb8U
#define JSON_KEY_MODIFIED 0xdcea4fe6U
#define JSON_KEY_CREATED 0x3a84721dU
#define JSON_KEY_BADGENAME 0x887685d9U
#define JSON_KEY_CONSOLE_ID 0x071656e5U
#define JSON_KEY_TOKEN 0x0e2dbd26U
#define JSON_KEY_FLAGS 0x0d2e96b2U
enum
{
CHEEVOS_CONSOLE_MEGA_DRIVE = 1,
CHEEVOS_CONSOLE_NINTENDO_64 = 2,
CHEEVOS_CONSOLE_SUPER_NINTENDO = 3,
CHEEVOS_CONSOLE_GAMEBOY = 4,
CHEEVOS_CONSOLE_GAMEBOY_ADVANCE = 5,
CHEEVOS_CONSOLE_GAMEBOY_COLOR = 6,
CHEEVOS_CONSOLE_NINTENDO = 7,
CHEEVOS_CONSOLE_PC_ENGINE = 8,
CHEEVOS_CONSOLE_SEGA_CD = 9,
CHEEVOS_CONSOLE_SEGA_32X = 10,
CHEEVOS_CONSOLE_MASTER_SYSTEM = 11
};
enum
{
CHEEVOS_VAR_SIZE_BIT_0,
CHEEVOS_VAR_SIZE_BIT_1,
CHEEVOS_VAR_SIZE_BIT_2,
CHEEVOS_VAR_SIZE_BIT_3,
CHEEVOS_VAR_SIZE_BIT_4,
CHEEVOS_VAR_SIZE_BIT_5,
CHEEVOS_VAR_SIZE_BIT_6,
CHEEVOS_VAR_SIZE_BIT_7,
CHEEVOS_VAR_SIZE_NIBBLE_LOWER,
CHEEVOS_VAR_SIZE_NIBBLE_UPPER,
/* Byte, */
CHEEVOS_VAR_SIZE_EIGHT_BITS, /* =Byte, */
CHEEVOS_VAR_SIZE_SIXTEEN_BITS,
CHEEVOS_VAR_SIZE_THIRTYTWO_BITS,
CHEEVOS_VAR_SIZE_LAST
}; /* cheevos_var_t.size */
enum
{
/* compare to the value of a live address in RAM */
CHEEVOS_VAR_TYPE_ADDRESS,
/* a number. assume 32 bit */
CHEEVOS_VAR_TYPE_VALUE_COMP,
/* the value last known at this address. */
CHEEVOS_VAR_TYPE_DELTA_MEM,
/* a custom user-set variable */
CHEEVOS_VAR_TYPE_DYNAMIC_VAR,
CHEEVOS_VAR_TYPE_LAST
}; /* cheevos_var_t.type */
enum
{
CHEEVOS_COND_OP_EQUALS,
CHEEVOS_COND_OP_LESS_THAN,
CHEEVOS_COND_OP_LESS_THAN_OR_EQUAL,
CHEEVOS_COND_OP_GREATER_THAN,
CHEEVOS_COND_OP_GREATER_THAN_OR_EQUAL,
CHEEVOS_COND_OP_NOT_EQUAL_TO,
CHEEVOS_COND_OP_LAST
}; /* cheevos_cond_t.op */
enum
{
CHEEVOS_COND_TYPE_STANDARD,
CHEEVOS_COND_TYPE_PAUSE_IF,
CHEEVOS_COND_TYPE_RESET_IF,
CHEEVOS_COND_TYPE_LAST
}; /* cheevos_cond_t.type */
enum
{
CHEEVOS_DIRTY_TITLE = 1 << 0,
CHEEVOS_DIRTY_DESC = 1 << 1,
CHEEVOS_DIRTY_POINTS = 1 << 2,
CHEEVOS_DIRTY_AUTHOR = 1 << 3,
CHEEVOS_DIRTY_ID = 1 << 4,
CHEEVOS_DIRTY_BADGE = 1 << 5,
CHEEVOS_DIRTY_CONDITIONS = 1 << 6,
CHEEVOS_DIRTY_VOTES = 1 << 7,
CHEEVOS_DIRTY_DESCRIPTION = 1 << 8,
CHEEVOS_DIRTY_ALL = (1 << 9) - 1
};
typedef struct
{
unsigned type;
unsigned req_hits;
unsigned curr_hits;
cheevos_var_t source;
unsigned op;
cheevos_var_t target;
} cheevos_cond_t;
typedef struct
{
cheevos_cond_t *conds;
unsigned count;
const char* expression;
} cheevos_condset_t;
typedef struct
{
unsigned id;
const char *title;
const char *description;
const char *author;
const char *badge;
unsigned points;
unsigned dirty;
int active;
int modified;
cheevos_condset_t *condsets;
unsigned count;
} cheevo_t;
typedef struct
{
cheevo_t *cheevos;
unsigned count;
} cheevoset_t;
typedef struct
{
int is_element;
} cheevos_deactivate_t;
typedef struct
{
unsigned key_hash;
int is_key;
const char *value;
size_t length;
} cheevos_getvalueud_t;
typedef struct
{
int in_cheevos;
uint32_t field_hash;
unsigned core_count;
unsigned unofficial_count;
} cheevos_countud_t;
typedef struct
{
const char *string;
size_t length;
} cheevos_field_t;
typedef struct
{
int in_cheevos;
int is_console_id;
unsigned core_count;
unsigned unofficial_count;
cheevos_field_t *field;
cheevos_field_t id, memaddr, title, desc, points, author;
cheevos_field_t modified, created, badge, flags;
} cheevos_readud_t;
typedef struct
{
unsigned (*finder)(const struct retro_game_info *, retro_time_t);
const char *name;
const uint32_t *ext_hashes;
} cheevos_finder_t;
typedef struct
{
int loaded;
int console_id;
bool core_supports;
cheevoset_t core;
cheevoset_t unofficial;
char token[32];
retro_ctx_memory_info_t meminfo[4];
} cheevos_locals_t;
static cheevos_locals_t cheevos_locals =
{
0,
0,
true,
{NULL, 0},
{NULL, 0},
{0},
};
static int cheats_are_enabled = 0;
static int cheats_were_enabled = 0;
/*****************************************************************************
Supporting functions.
*****************************************************************************/
static uint32_t cheevos_djb2(const char* str, size_t length)
{
const unsigned char *aux = (const unsigned char*)str;
const unsigned char *end = aux + length;
uint32_t hash = 5381;
while (aux < end)
hash = (hash << 5) + hash + *aux++;
return hash;
}
static int cheevos_http_get(const char **result, size_t *size,
const char *url, retro_time_t *timeout)
{
const char *msg = NULL;
#ifdef CHEEVOS_NO_TIMEOUT
int ret = net_http_get(result, size, url, NULL);
#else
int ret = net_http_get(result, size, url, timeout);
#endif
switch (ret)
{
case NET_HTTP_GET_OK:
return ret;
case NET_HTTP_GET_MALFORMED_URL:
msg = "malformed url";
break;
case NET_HTTP_GET_CONNECT_ERROR:
msg = "connect error";
break;
case NET_HTTP_GET_TIMEOUT:
msg = "timeout";
break;
default:
msg = "?";
break;
}
RARCH_ERR("CHEEVOS error getting %s: %s\n", url, msg);
return ret;
}
static int cheevos_getvalue__json_key(void *userdata,
const char *name, size_t length)
{
cheevos_getvalueud_t* ud = (cheevos_getvalueud_t*)userdata;
ud->is_key = cheevos_djb2(name, length) == ud->key_hash;
return 0;
}
static int cheevos_getvalue__json_string(void *userdata,
const char *string, size_t length)
{
cheevos_getvalueud_t* ud = (cheevos_getvalueud_t*)userdata;
if (ud->is_key)
{
ud->value = string;
ud->length = length;
ud->is_key = 0;
}
return 0;
}
static int cheevos_getvalue__json_boolean(void *userdata, int istrue)
{
cheevos_getvalueud_t* ud = (cheevos_getvalueud_t*)userdata;
if ( ud->is_key )
{
ud->value = istrue ? "true" : "false";
ud->length = istrue ? 4 : 5;
ud->is_key = 0;
}
return 0;
}
static int cheevos_getvalue__json_null(void *userdata)
{
cheevos_getvalueud_t* ud = (cheevos_getvalueud_t*)userdata;
if ( ud->is_key )
{
ud->value = "null";
ud->length = 4;
ud->is_key = 0;
}
return 0;
}
static int cheevos_get_value(const char *json, unsigned key_hash,
char *value, size_t length)
{
static const jsonsax_handlers_t handlers =
{
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
cheevos_getvalue__json_key,
NULL,
cheevos_getvalue__json_string,
cheevos_getvalue__json_string, /* number */
cheevos_getvalue__json_boolean,
cheevos_getvalue__json_null
};
cheevos_getvalueud_t ud;
ud.key_hash = key_hash;
ud.is_key = 0;
ud.value = NULL;
ud.length = 0;
*value = 0;
if ((jsonsax_parse(json, &handlers, (void*)&ud) == JSONSAX_OK)
&& ud.value && ud.length < length)
{
strncpy(value, ud.value, length);
value[ud.length] = 0;
return 0;
}
return -1;
}
/*****************************************************************************
Count number of achievements in a JSON file.
*****************************************************************************/
static int cheevos_count__json_end_array(void *userdata)
{
cheevos_countud_t* ud = (cheevos_countud_t*)userdata;
ud->in_cheevos = 0;
return 0;
}
static int cheevos_count__json_key(void *userdata,
const char *name, size_t length)
{
cheevos_countud_t* ud = (cheevos_countud_t*)userdata;
ud->field_hash = cheevos_djb2(name, length);
if (ud->field_hash == JSON_KEY_ACHIEVEMENTS)
ud->in_cheevos = 1;
return 0;
}
static int cheevos_count__json_number(void *userdata,
const char *number, size_t length)
{
long flags;
cheevos_countud_t* ud = (cheevos_countud_t*)userdata;
if (ud->in_cheevos && ud->field_hash == JSON_KEY_FLAGS)
{
flags = strtol(number, NULL, 10);
switch (flags)
{
case 3: /* Core achievements */
ud->core_count++;
break;
case 5: /* Unofficial achievements */
ud->unofficial_count++;
break;
default:
break;
}
}
return 0;
}
static int cheevos_count_cheevos(const char *json,
unsigned *core_count, unsigned *unofficial_count)
{
static const jsonsax_handlers_t handlers =
{
NULL,
NULL,
NULL,
NULL,
NULL,
cheevos_count__json_end_array,
cheevos_count__json_key,
NULL,
NULL,
cheevos_count__json_number,
NULL,
NULL
};
int res;
cheevos_countud_t ud;
ud.in_cheevos = 0;
ud.core_count = 0;
ud.unofficial_count = 0;
res = jsonsax_parse(json, &handlers, (void*)&ud);
*core_count = ud.core_count;
*unofficial_count = ud.unofficial_count;
return res;
}
/*****************************************************************************
Parse the MemAddr field.
*****************************************************************************/
static unsigned cheevos_prefix_to_comp_size(char prefix)
{
/* Careful not to use ABCDEF here, this denotes part of an actual variable! */
switch( toupper( prefix ) )
{
case 'M':
return CHEEVOS_VAR_SIZE_BIT_0;
case 'N':
return CHEEVOS_VAR_SIZE_BIT_1;
case 'O':
return CHEEVOS_VAR_SIZE_BIT_2;
case 'P':
return CHEEVOS_VAR_SIZE_BIT_3;
case 'Q':
return CHEEVOS_VAR_SIZE_BIT_4;
case 'R':
return CHEEVOS_VAR_SIZE_BIT_5;
case 'S':
return CHEEVOS_VAR_SIZE_BIT_6;
case 'T':
return CHEEVOS_VAR_SIZE_BIT_7;
case 'L':
return CHEEVOS_VAR_SIZE_NIBBLE_LOWER;
case 'U':
return CHEEVOS_VAR_SIZE_NIBBLE_UPPER;
case 'H':
return CHEEVOS_VAR_SIZE_EIGHT_BITS;
case 'X':
return CHEEVOS_VAR_SIZE_THIRTYTWO_BITS;
default:
case ' ':
break;
}
return CHEEVOS_VAR_SIZE_SIXTEEN_BITS;
}
static unsigned cheevos_read_hits(const char **memaddr)
{
char *end = NULL;
const char *str = *memaddr;
unsigned num_hits = 0;
if (*str == '(' || *str == '.')
{
num_hits = strtol(str + 1, &end, 10);
str = end + 1;
}
*memaddr = str;
return num_hits;
}
static unsigned cheevos_parse_operator(const char **memaddr)
{
unsigned char op;
const char *str = *memaddr;
if (*str == '=' && str[1] == '=')
{
op = CHEEVOS_COND_OP_EQUALS;
str += 2;
}
else if (*str == '=')
{
op = CHEEVOS_COND_OP_EQUALS;
str++;
}
else if (*str == '!' && str[1] == '=')
{
op = CHEEVOS_COND_OP_NOT_EQUAL_TO;
str += 2;
}
else if (*str == '<' && str[1] == '=')
{
op = CHEEVOS_COND_OP_LESS_THAN_OR_EQUAL;
str += 2;
}
else if (*str == '<')
{
op = CHEEVOS_COND_OP_LESS_THAN;
str++;
}
else if (*str == '>' && str[1] == '=')
{
op = CHEEVOS_COND_OP_GREATER_THAN_OR_EQUAL;
str += 2;
}
else if (*str == '>')
{
op = CHEEVOS_COND_OP_GREATER_THAN;
str++;
}
else
{
RARCH_ERR("CHEEVOS Unknown operator %c\n", *str);
op = CHEEVOS_COND_OP_EQUALS;
}
*memaddr = str;
return op;
}
void cheevos_parse_guest_addr(cheevos_var_t *var, unsigned value)
{
rarch_system_info_t *system;
runloop_ctl(RUNLOOP_CTL_SYSTEM_INFO_GET, &system);
var->bank_id = -1;
var->value = value;
if (system->mmaps.num_descriptors != 0)
{
const struct retro_memory_descriptor *desc = NULL;
const struct retro_memory_descriptor *end = NULL;
switch (cheevos_locals.console_id)
{
case CHEEVOS_CONSOLE_GAMEBOY_ADVANCE:
/* Patch the address to correctly map it to the mmaps */
if (var->value < 0x8000) /* Internal RAM */
var->value += 0x3000000;
else /* Work RAM */
var->value += 0x2000000 - 0x8000;
break;
case CHEEVOS_CONSOLE_PC_ENGINE:
var->value += 0x1f0000;
break;
default:
break;
}
desc = system->mmaps.descriptors;
end = desc + system->mmaps.num_descriptors;
for (; desc < end; desc++)
{
if ((var->value & desc->select) == desc->start)
{
var->bank_id = desc - system->mmaps.descriptors;
var->value = var->value - desc->start + desc->offset;
break;
}
}
}
else
{
unsigned i;
for (i = 0; i < ARRAY_SIZE(cheevos_locals.meminfo); i++)
{
if (var->value < cheevos_locals.meminfo[i].size)
{
var->bank_id = i;
break;
}
var->value -= cheevos_locals.meminfo[i].size;
}
}
}
static void cheevos_parse_var(cheevos_var_t *var, const char **memaddr)
{
char *end = NULL;
const char *str = *memaddr;
unsigned base = 16;
if (toupper(*str) == 'D' && str[1] == '0' && toupper(str[2]) == 'X')
{
/* d0x + 4 hex digits */
str += 3;
var->type = CHEEVOS_VAR_TYPE_DELTA_MEM;
}
else if (*str == '0' && toupper(str[1]) == 'X')
{
/* 0x + 4 hex digits */
str += 2;
var->type = CHEEVOS_VAR_TYPE_ADDRESS;
}
else
{
var->type = CHEEVOS_VAR_TYPE_VALUE_COMP;
if (toupper(*str) == 'H')
str++;
else
base = 10;
}
if (var->type != CHEEVOS_VAR_TYPE_VALUE_COMP)
{
var->size = cheevos_prefix_to_comp_size(*str);
if (var->size != CHEEVOS_VAR_SIZE_SIXTEEN_BITS)
str++;
}
var->value = strtol(str, &end, base);
*memaddr = end;
switch (var->type)
{
case CHEEVOS_VAR_TYPE_ADDRESS:
case CHEEVOS_VAR_TYPE_DELTA_MEM:
cheevos_parse_guest_addr(var, var->value);
#ifdef CHEEVOS_DUMP_ADDRS
RARCH_LOG("CHEEVOS var %03d:%08X\n", var->bank_id + 1, var->value);
#endif
break;
default:
break;
}
}
static void cheevos_parse_cond(cheevos_cond_t *cond, const char **memaddr)
{
const char* str = *memaddr;
if (*str == 'R' && str[1] == ':')
{
cond->type = CHEEVOS_COND_TYPE_RESET_IF;
str += 2;
}
else if (*str == 'P' && str[1] == ':')
{
cond->type = CHEEVOS_COND_TYPE_PAUSE_IF;
str += 2;
}
else
cond->type = CHEEVOS_COND_TYPE_STANDARD;
cheevos_parse_var(&cond->source, &str);
cond->op = cheevos_parse_operator(&str);
cheevos_parse_var(&cond->target, &str);
cond->curr_hits = 0;
cond->req_hits = cheevos_read_hits(&str);
*memaddr = str;
}
static unsigned cheevos_count_cond_sets(const char *memaddr)
{
cheevos_cond_t cond;
unsigned count = 0;
do
{
do
{
/* Skip any characters up until the start of the achievement condition */
while ( *memaddr == ' '
|| *memaddr == '_'
|| *memaddr == '|'
|| *memaddr == 'S')
memaddr++;
cheevos_parse_cond(&cond, &memaddr);
}
while ( *memaddr == '_'
|| *memaddr == 'R'
|| *memaddr == 'P'); /* AND, ResetIf, PauseIf */
count++;
}
while (*memaddr == 'S'); /* Repeat for all subconditions if they exist */
return count;
}
static unsigned cheevos_count_conds_in_set(const char *memaddr, unsigned set)
{
cheevos_cond_t cond;
unsigned index = 0;
unsigned count = 0;
do
{
do
{
/* Skip any characters up until the start of the achievement condition */
while ( *memaddr == ' '
|| *memaddr == '_'
|| *memaddr == '|'
|| *memaddr == 'S')
memaddr++;
cheevos_parse_cond(&cond, &memaddr);
if (index == set)
count++;
}
while (*memaddr == '_' || *memaddr == 'R' || *memaddr == 'P'); /* AND, ResetIf, PauseIf */
}
while (*memaddr == 'S'); /* Repeat for all subconditions if they exist */
return count;
}
static void cheevos_parse_memaddr(cheevos_cond_t *cond, const char *memaddr)
{
do
{
do
{
/* Skip any characters up until the start of the achievement condition */
while ( *memaddr == ' '
|| *memaddr == '_'
|| *memaddr == '|'
|| *memaddr == 'S')
memaddr++;
cheevos_parse_cond(cond++, &memaddr);
}
while (*memaddr == '_' || *memaddr == 'R' || *memaddr == 'P'); /* AND, ResetIf, PauseIf */
}
while (*memaddr == 'S'); /* Repeat for all subconditions if they exist */
}
/*****************************************************************************
Load achievements from a JSON string.
*****************************************************************************/
static INLINE const char *cheevos_dupstr(const cheevos_field_t *field)
{
char *string = (char*)malloc(field->length + 1);
if (!string)
return NULL;
memcpy ((void*)string, (void*)field->string, field->length);
string[field->length] = 0;
return string;
}
static int cheevos_new_cheevo(cheevos_readud_t *ud)
{
unsigned set;
const cheevos_condset_t *end = NULL;
cheevos_condset_t *condset = NULL;
cheevo_t *cheevo = NULL;
int flags = strtol(ud->flags.string, NULL, 10);
if (flags == 3)
cheevo = cheevos_locals.core.cheevos + ud->core_count++;
else
cheevo = cheevos_locals.unofficial.cheevos + ud->unofficial_count++;
cheevo->id = strtol(ud->id.string, NULL, 10);
cheevo->title = cheevos_dupstr(&ud->title);
cheevo->description = cheevos_dupstr(&ud->desc);
cheevo->author = cheevos_dupstr(&ud->author);
cheevo->badge = cheevos_dupstr(&ud->badge);
cheevo->points = strtol(ud->points.string, NULL, 10);
cheevo->dirty = 0;
cheevo->active = 1; /* flags == 3; */
cheevo->modified = 0;
if (!cheevo->title || !cheevo->description || !cheevo->author || !cheevo->badge)
{
free((void*)cheevo->title);
free((void*)cheevo->description);
free((void*)cheevo->author);
free((void*)cheevo->badge);
return -1;
}
cheevo->count = cheevos_count_cond_sets(ud->memaddr.string);
if (cheevo->count)
{
cheevo->condsets = (cheevos_condset_t*)
malloc(cheevo->count * sizeof(cheevos_condset_t));
if (!cheevo->condsets)
return -1;
memset((void*)cheevo->condsets, 0,
cheevo->count * sizeof(cheevos_condset_t));
end = cheevo->condsets + cheevo->count;
set = 0;
for (condset = cheevo->condsets; condset < end; condset++)
{
condset->count =
cheevos_count_conds_in_set(ud->memaddr.string, set++);
if (condset->count)
{
condset->conds = (cheevos_cond_t*)
malloc(condset->count * sizeof(cheevos_cond_t));
if (!condset->conds)
return -1;
memset((void*)condset->conds, 0,
condset->count * sizeof(cheevos_cond_t));
condset->expression = cheevos_dupstr(&ud->memaddr);
cheevos_parse_memaddr(condset->conds, ud->memaddr.string);
}
else
condset->conds = NULL;
}
}
return 0;
}
static int cheevos_read__json_key( void *userdata,
const char *name, size_t length)
{
cheevos_readud_t *ud = (cheevos_readud_t*)userdata;
uint32_t hash = cheevos_djb2(name, length);
ud->field = NULL;
if (hash == JSON_KEY_ACHIEVEMENTS)
ud->in_cheevos = 1;
else if (hash == JSON_KEY_CONSOLE_ID)
ud->is_console_id = 1;
else if (ud->in_cheevos)
{
switch ( hash )
{
case JSON_KEY_ID:
ud->field = &ud->id;
break;
case JSON_KEY_MEMADDR:
ud->field = &ud->memaddr;
break;
case JSON_KEY_TITLE:
ud->field = &ud->title;
break;
case JSON_KEY_DESCRIPTION:
ud->field = &ud->desc;
break;
case JSON_KEY_POINTS:
ud->field = &ud->points;
break;
case JSON_KEY_AUTHOR:
ud->field = &ud->author;
break;
case JSON_KEY_MODIFIED:
ud->field = &ud->modified;
break;
case JSON_KEY_CREATED:
ud->field = &ud->created;
break;
case JSON_KEY_BADGENAME:
ud->field = &ud->badge;
break;
case JSON_KEY_FLAGS:
ud->field = &ud->flags;
break;
}
}
return 0;
}
static int cheevos_read__json_string(void *userdata,
const char *string, size_t length)
{
cheevos_readud_t *ud = (cheevos_readud_t*)userdata;
if (ud->field)
{
ud->field->string = string;
ud->field->length = length;
}
return 0;
}
static int cheevos_read__json_number(void *userdata,
const char *number, size_t length)
{
cheevos_readud_t *ud = (cheevos_readud_t*)userdata;
if (ud->field)
{
ud->field->string = number;
ud->field->length = length;
}
else if (ud->is_console_id)
{
cheevos_locals.console_id = strtol(number, NULL, 10);
ud->is_console_id = 0;
}
return 0;
}
static int cheevos_read__json_end_object(void *userdata)
{
cheevos_readud_t *ud = (cheevos_readud_t*)userdata;