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
|
#include "rc_internal.h"
#include "rc_api_runtime.h"
#include "rc_consoles.h"
#include "rc_validate.h"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h> /* memset */
#include <ctype.h>
#ifdef _MSC_VER /* windows build */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <dirent.h>
#include <strings.h>
#define stricmp strcasecmp
#endif
/* usage exmaple:
*
* ./validator.exe d "E:\RetroAchievements\dump" | sort > results.txt
* grep -v ": OK" results.txt | grep -v "File: " | grep .
*/
static const char* flag_string(char type) {
switch (type) {
case RC_CONDITION_STANDARD: return "";
case RC_CONDITION_PAUSE_IF: return "PauseIf ";
case RC_CONDITION_RESET_IF: return "ResetIf ";
case RC_CONDITION_MEASURED_IF: return "MeasuredIf ";
case RC_CONDITION_TRIGGER: return "Trigger ";
case RC_CONDITION_MEASURED: return "Measured ";
case RC_CONDITION_ADD_SOURCE: return "AddSource ";
case RC_CONDITION_SUB_SOURCE: return "SubSource ";
case RC_CONDITION_REMEMBER: return "Remember ";
case RC_CONDITION_ADD_ADDRESS: return "AddAddress ";
case RC_CONDITION_ADD_HITS: return "AddHits ";
case RC_CONDITION_SUB_HITS: return "SubHits ";
case RC_CONDITION_RESET_NEXT_IF: return "ResetNextIf ";
case RC_CONDITION_AND_NEXT: return "AndNext ";
case RC_CONDITION_OR_NEXT: return "OrNext ";
default: return "Unknown";
}
}
static const char* type_string(char type) {
switch (type) {
case RC_OPERAND_ADDRESS: return "Mem";
case RC_OPERAND_DELTA: return "Delta";
case RC_OPERAND_CONST: return "Value";
case RC_OPERAND_FP: return "Float";
case RC_OPERAND_FUNC: return "Func";
case RC_OPERAND_PRIOR: return "Prior";
case RC_OPERAND_BCD: return "BCD";
case RC_OPERAND_INVERTED: return "Inverted";
case RC_OPERAND_RECALL: return "Recall";
default: return "Unknown";
}
}
static const char* size_string(char size) {
switch (size) {
case RC_MEMSIZE_8_BITS: return "8-bit";
case RC_MEMSIZE_16_BITS: return "16-bit";
case RC_MEMSIZE_24_BITS: return "24-bit";
case RC_MEMSIZE_32_BITS: return "32-bit";
case RC_MEMSIZE_LOW: return "Lower4";
case RC_MEMSIZE_HIGH: return "Upper4";
case RC_MEMSIZE_BIT_0: return "Bit0";
case RC_MEMSIZE_BIT_1: return "Bit1";
case RC_MEMSIZE_BIT_2: return "Bit2";
case RC_MEMSIZE_BIT_3: return "Bit3";
case RC_MEMSIZE_BIT_4: return "Bit4";
case RC_MEMSIZE_BIT_5: return "Bit5";
case RC_MEMSIZE_BIT_6: return "Bit6";
case RC_MEMSIZE_BIT_7: return "Bit7";
case RC_MEMSIZE_BITCOUNT: return "BitCount";
case RC_MEMSIZE_16_BITS_BE: return "16-bit BE";
case RC_MEMSIZE_24_BITS_BE: return "24-bit BE";
case RC_MEMSIZE_32_BITS_BE: return "32-bit BE";
case RC_MEMSIZE_VARIABLE: return "Variable";
default: return "Unknown";
}
}
static const char* operator_string(char oper) {
switch (oper) {
case RC_OPERATOR_NONE: return "";
case RC_OPERATOR_AND: return "&";
case RC_OPERATOR_XOR: return "^";
case RC_OPERATOR_MULT: return "*";
case RC_OPERATOR_DIV: return "/";
case RC_OPERATOR_MOD: return "%";
case RC_OPERATOR_ADD: return "+";
case RC_OPERATOR_SUB: return "-";
case RC_OPERATOR_EQ: return "=";
case RC_OPERATOR_NE: return "!=";
case RC_OPERATOR_GE: return ">=";
case RC_OPERATOR_GT: return ">";
case RC_OPERATOR_LE: return "<=";
case RC_OPERATOR_LT: return "<";
default: return "?";
}
}
static void append_condition(char result[], size_t result_size, const rc_condition_t* cond) {
const char* flag = flag_string(cond->type);
const char* src_type = type_string(cond->operand1.type);
const char* tgt_type = type_string(cond->operand2.type);
const char* cmp = operator_string(cond->oper);
char val1[32], val2[32];
if (rc_operand_is_memref(&cond->operand1))
snprintf(val1, sizeof(val1), "%s 0x%06x", size_string(cond->operand1.size), cond->operand1.value.memref->address);
else
snprintf(val1, sizeof(val1), "0x%06x", cond->operand1.value.num);
if (rc_operand_is_memref(&cond->operand2))
snprintf(val2, sizeof(val2), "%s 0x%06x", size_string(cond->operand2.size), cond->operand2.value.memref->address);
else
snprintf(val2, sizeof(val2), "0x%06x", cond->operand2.value.num);
const size_t message_len = strlen(result);
result += message_len;
result_size -= message_len;
if (cond->oper == RC_OPERATOR_NONE)
snprintf(result, result_size, ": %s%s %s", flag, src_type, val1);
else
snprintf(result, result_size, ": %s%s %s %s %s %s", flag, src_type, val1, cmp, tgt_type, val2);
}
static void append_invalid_condition(char result[], size_t result_size, const rc_condset_t* condset) {
if (strncmp(result, "Condition ", 10) == 0) {
int index = atoi(&result[10]);
const rc_condition_t* cond;
for (cond = condset->conditions; cond; cond = cond->next) {
if (--index == 0) {
const size_t error_length = strlen(result);
append_condition(result + error_length, result_size - error_length, cond);
return;
}
}
}
}
static void append_invalid_trigger_condition(char result[], size_t result_size, const rc_trigger_t* trigger) {
if (strncmp(result, "Alt", 3) == 0) {
int index = atoi(&result[3]);
const rc_condset_t* condset;
for (condset = trigger->alternative; condset; condset = condset->next) {
if (--index == 0) {
result += 4;
result_size -= 4;
while (isdigit(*result)) {
++result;
--result_size;
}
++result;
--result_size;
append_invalid_condition(result, result_size, condset);
return;
}
}
}
else if (strncmp(result, "Core ", 5) == 0) {
append_invalid_condition(result + 5, result_size - 5, trigger->requirement);
}
else {
append_invalid_condition(result, result_size, trigger->requirement);
}
}
static int validate_trigger(const char* trigger, char result[], size_t result_size, uint32_t console_id) {
char* buffer;
rc_trigger_t* compiled;
int success = 0;
int ret = rc_trigger_size(trigger);
if (ret < 0) {
snprintf(result, result_size, "%s", rc_error_str(ret));
return 0;
}
buffer = (char*)malloc(ret + 4);
if (!buffer) {
snprintf(result, result_size, "malloc failed");
return 0;
}
memset(buffer + ret, 0xCD, 4);
compiled = rc_parse_trigger(buffer, trigger, NULL, 0);
if (compiled == NULL) {
snprintf(result, result_size, "parse failed");
}
else if (*(unsigned*)&buffer[ret] != 0xCDCDCDCD) {
snprintf(result, result_size, "write past end of buffer");
}
else if (rc_validate_trigger_for_console(compiled, result, result_size, console_id)) {
snprintf(result, result_size, "%d OK", ret);
success = 1;
}
else {
append_invalid_trigger_condition(result, result_size, compiled);
}
free(buffer);
return success;
}
static int validate_leaderboard(const char* leaderboard, char result[], const size_t result_size, uint32_t console_id)
{
char* buffer;
rc_lboard_t* compiled;
int success = 0;
int ret = rc_lboard_size(leaderboard);
if (ret < 0) {
/* generic problem parsing the leaderboard, attempt to report where */
const char* start = leaderboard;
char part[4] = { 0,0,0,0 };
do {
char* next = strstr(start, "::");
part[0] = toupper((int)start[0]);
part[1] = toupper((int)start[1]);
part[2] = toupper((int)start[2]);
start += 4;
if (strcmp(part, "VAL") == 0) {
int ret2 = rc_value_size(start);
if (ret2 == ret) {
snprintf(result, result_size, "%s: %s", part, rc_error_str(ret));
return 0;
}
}
else {
int ret2 = rc_trigger_size(start);
if (ret2 == ret) {
snprintf(result, result_size, "%s: %s", part, rc_error_str(ret));
return 0;
}
}
if (!next)
break;
start = next + 2;
} while (1);
snprintf(result, result_size, "%s", rc_error_str(ret));
return 0;
}
buffer = (char*)malloc(ret + 4);
if (!buffer) {
snprintf(result, result_size, "malloc failed");
return 0;
}
memset(buffer + ret, 0xCD, 4);
compiled = rc_parse_lboard(buffer, leaderboard, NULL, 0);
if (compiled == NULL) {
snprintf(result, result_size, "parse failed");
}
else if (*(unsigned*)&buffer[ret] != 0xCDCDCDCD) {
snprintf(result, result_size, "write past end of buffer");
}
else {
snprintf(result, result_size, "STA: ");
success = rc_validate_trigger_for_console(&compiled->start, result + 5, result_size - 5, console_id);
if (!success) {
append_invalid_trigger_condition(result + 5, result_size - 5, &compiled->start);
}
else {
snprintf(result, result_size, "SUB: ");
success = rc_validate_trigger_for_console(&compiled->submit, result + 5, result_size - 5, console_id);
if (!success) {
append_invalid_trigger_condition(result + 5, result_size - 5, &compiled->submit);
}
else {
snprintf(result, result_size, "CAN: ");
success = rc_validate_trigger_for_console(&compiled->cancel, result + 5, result_size - 5, console_id);
if (!success) {
append_invalid_trigger_condition(result + 5, result_size - 5, &compiled->cancel);
}
else {
snprintf(result, result_size, "%d OK", ret);
}
}
}
}
free(buffer);
return success;
}
static int validate_macros(const rc_richpresence_t* richpresence, const char* script, char result[], const size_t result_size)
{
const uint16_t RC_FORMAT_UNKNOWN_MACRO = 103; /* enum not exposed by header */
rc_richpresence_display_t* display = richpresence->first_display;
while (display != NULL) {
rc_richpresence_display_part_t* part = display->display;
while (part != NULL) {
if (part->display_type == RC_FORMAT_UNKNOWN_MACRO) {
/* include opening parenthesis to prevent partial match */
size_t macro_len = strchr(part->text, '(') - part->text + 1;
/* find the display portion of the script */
const char* ptr = script;
int line = 1;
while (strncmp(ptr, "Display:", 8) != 0) {
while (*ptr != '\n')
++ptr;
++line;
++ptr;
}
/* find the first matching reference to the unknown macro */
do {
while (*ptr != '@') {
if (*ptr == '\n')
++line;
if (*ptr == '\0') {
/* unexpected, but prevent potential infinite loop */
snprintf(result, result_size, "Unknown macro \"%.*s\"", (int)(macro_len - 1), part->text);
return 0;
}
++ptr;
}
++ptr;
if (strncmp(ptr, part->text, macro_len) == 0) {
snprintf(result, result_size, "Line %d: Unknown macro \"%.*s\"", line, (int)(macro_len - 1), part->text);
return 0;
}
} while (1);
}
part = part->next;
}
display = display->next;
}
return 1;
}
static int validate_richpresence(const char* script, char result[], const size_t result_size, uint32_t console_id)
{
char* buffer;
rc_richpresence_t* compiled;
int lines;
int success = 0;
int ret = rc_richpresence_size_lines(script, &lines);
if (ret < 0) {
snprintf(result, result_size, "Line %d: %s", lines, rc_error_str(ret));
return 0;
}
buffer = (char*)malloc(ret + 4);
if (!buffer) {
snprintf(result, result_size, "malloc failed");
return 0;
}
memset(buffer + ret, 0xCD, 4);
compiled = rc_parse_richpresence(buffer, script, NULL, 0);
if (compiled == NULL) {
snprintf(result, result_size, "parse failed");
}
else if (*(unsigned*)&buffer[ret] != 0xCDCDCDCD) {
snprintf(result, result_size, "write past end of buffer");
}
else {
const rc_richpresence_display_t* display;
int index = 1;
for (display = compiled->first_display; display; display = display->next) {
const size_t prefix_length = snprintf(result, result_size, "Display%d: ", index++);
success = rc_validate_trigger_for_console(&display->trigger, result + prefix_length, result_size - prefix_length, console_id);
if (!success)
break;
}
if (success)
success = rc_validate_memrefs_for_console(rc_richpresence_get_memrefs(compiled), result, result_size, console_id);
if (success)
success = validate_macros(compiled, script, result, result_size);
if (success)
snprintf(result, result_size, "%d OK", ret);
}
free(buffer);
return success;
}
static void validate_richpresence_file(const char* richpresence_file, char result[], const size_t result_size)
{
char* file_contents;
size_t file_size;
FILE* file;
fopen_s(&file, richpresence_file, "rb");
if (!file) {
snprintf(result, result_size, "could not open file");
return;
}
fseek(file, 0, SEEK_END);
file_size = ftell(file);
fseek(file, 0, SEEK_SET);
file_contents = (char*)malloc(file_size + 1);
if (!file_contents) {
snprintf(result, result_size, "malloc failed");
return;
}
fread(file_contents, 1, file_size, file);
file_contents[file_size] = '\0';
fclose(file);
validate_richpresence(file_contents, result, result_size, 0);
free(file_contents);
}
static int validate_patchdata_file(const char* patchdata_file, const char* filename, int errors_only) {
char* file_contents;
size_t file_size;
FILE* file;
rc_api_fetch_game_data_response_t fetch_game_data_response;
int result;
size_t i;
char file_title[256];
char buffer[256];
int success = 1;
fopen_s(&file, patchdata_file, "rb");
if (!file) {
printf("File: %s: could not open file\n", filename);
return 0;
}
fseek(file, 0, SEEK_END);
file_size = ftell(file);
fseek(file, 0, SEEK_SET);
file_contents = (char*)malloc(file_size + 1);
fread(file_contents, 1, file_size, file);
file_contents[file_size] = '\0';
fclose(file);
/* rc_api_process_fetch_game_data_response expects the PatchData to be
* a subobject, but the DLL strips that when writing to the RACache.
* if it looks like the nested object, wrap it again */
if (strncmp(file_contents, "{\"ID\":", 6) == 0) {
char* expanded_contents = (char*)malloc(file_size + 15);
memcpy(expanded_contents, "{\"PatchData\":", 13);
memcpy(&expanded_contents[13], file_contents, file_size);
expanded_contents[file_size + 13] = '}';
expanded_contents[file_size + 14] = '\0';
free(file_contents);
file_contents = expanded_contents;
}
result = rc_api_process_fetch_game_data_response(&fetch_game_data_response, file_contents);
if (result != RC_OK) {
if (fetch_game_data_response.response.error_message)
printf("File: %s: %s\n", filename, fetch_game_data_response.response.error_message);
else
printf("File: %s: %s\n", filename, rc_error_str(result));
return 0;
}
free(file_contents);
snprintf(file_title, sizeof(file_title), "File: %s: %s\n", filename, fetch_game_data_response.title);
if (fetch_game_data_response.rich_presence_script && *fetch_game_data_response.rich_presence_script) {
result = validate_richpresence(fetch_game_data_response.rich_presence_script,
buffer, sizeof(buffer), fetch_game_data_response.console_id);
success &= result;
if (!result || !errors_only) {
printf("%s", file_title);
file_title[0] = '\0';
printf(" rich presence %d: %s\n", fetch_game_data_response.id, buffer);
}
}
for (i = 0; i < fetch_game_data_response.num_achievements; ++i) {
const char* trigger = fetch_game_data_response.achievements[i].definition;
result = validate_trigger(trigger, buffer, sizeof(buffer), fetch_game_data_response.console_id);
success &= result;
if (!result || !errors_only) {
if (file_title[0]) {
printf("%s", file_title);
file_title[0] = '\0';
}
printf(" achievement %d%s: %s\n", fetch_game_data_response.achievements[i].id,
(fetch_game_data_response.achievements[i].category == 3) ? "" : " (Unofficial)", buffer);
}
}
for (i = 0; i < fetch_game_data_response.num_leaderboards; ++i) {
result = validate_leaderboard(fetch_game_data_response.leaderboards[i].definition,
buffer, sizeof(buffer), fetch_game_data_response.console_id);
success &= result;
if (!result || !errors_only) {
if (file_title[0]) {
printf("%s", file_title);
file_title[0] = '\0';
}
printf(" leaderboard %d: %s\n", fetch_game_data_response.leaderboards[i].id, buffer);
}
}
rc_api_destroy_fetch_game_data_response(&fetch_game_data_response);
return success;
}
#ifdef _MSC_VER
static void validate_patchdata_directory(const char* patchdata_directory, int errors_only) {
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;
int need_newline = 0;
char filename[MAX_PATH];
snprintf(filename, sizeof(filename), "%s\\*.json", patchdata_directory);
if ((hFind = FindFirstFile(filename, &fdFile)) == INVALID_HANDLE_VALUE) {
printf("failed to open directory");
return;
}
do
{
if (!(fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
if (need_newline) {
printf("\n");
need_newline = 0;
}
snprintf(filename, sizeof(filename), "%s\\%s", patchdata_directory, fdFile.cFileName);
if (!validate_patchdata_file(filename, fdFile.cFileName, errors_only) || !errors_only)
need_newline = 1;
}
} while(FindNextFile(hFind, &fdFile));
FindClose(hFind);
}
#else
static void validate_patchdata_directory(const char* patchdata_directory, int errors_only) {
struct dirent* entry;
char* filename;
size_t filename_len;
char path[2048];
int need_newline = 0;
DIR* dir = opendir(patchdata_directory);
if (!dir) {
printf("failed to open directory");
return;
}
while ((entry = readdir(dir)) != NULL) {
filename = entry->d_name;
filename_len = strlen(filename);
if (filename_len > 5 && stricmp(&filename[filename_len - 5], ".json") == 0) {
if (need_newline) {
printf("\n");
need_newline = 0;
}
sprintf(path, "%s/%s", patchdata_directory, filename);
if (!validate_patchdata_file(path, filename, errors_only) || !errors_only)
need_newline = 1;
}
}
closedir(dir);
}
#endif
static int usage() {
printf("validator [type] [data]\n"
"\n"
"where [type] is one of the following:\n"
" a achievement, [data] = trigger definition\n"
" l leaderboard, [data] = leaderboard definition\n"
" r rich presence, [data] = path to rich presence script\n"
" f patchdata file, [data] = path to patchdata json file\n"
" d patchdata directory, [data] = path to directory containing one or more patchdata json files\n"
" e same as 'd', but only reports errors\n"
);
return 0;
}
int main(int argc, char* argv[]) {
char buffer[256];
if (argc < 3)
return usage();
switch (argv[1][0])
{
case 'a':
validate_trigger(argv[2], buffer, sizeof(buffer), 0);
printf("Achievement: %s\n", buffer);
break;
case 'l':
validate_leaderboard(argv[2], buffer, sizeof(buffer), 0);
printf("Leaderboard: %s\n", buffer);
break;
case 'r':
validate_richpresence_file(argv[2], buffer, sizeof(buffer));
printf("Rich Presence: %s\n", buffer);
break;
case 'f':
validate_patchdata_file(argv[2], argv[2], 0);
break;
case 'd':
printf("Directory: %s:\n", argv[2]);
validate_patchdata_directory(argv[2], 0);
break;
case 'e':
printf("Directory: %s:\n", argv[2]);
validate_patchdata_directory(argv[2], 1);
break;
default:
return usage();
}
return 0;
}
|