Skip to content

Commit

Permalink
allow auto speed for pcsx_rearmed (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored Dec 21, 2024
1 parent 93eca32 commit 3a91a58
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 38 deletions.
87 changes: 60 additions & 27 deletions src/rc_libretro.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static const rc_disallowed_setting_t _rc_disallowed_neocd_settings[] = {
};

static const rc_disallowed_setting_t _rc_disallowed_pcsx_rearmed_settings[] = {
{ "pcsx_rearmed_psxclock", "<55" },
{ "pcsx_rearmed_psxclock", ",!auto,<55" },
{ "pcsx_rearmed_region", "pal" },
{ NULL, NULL }
};
Expand Down Expand Up @@ -202,14 +202,14 @@ static const rc_disallowed_core_settings_t rc_disallowed_core_settings[] = {
{ NULL, NULL }
};

static int rc_libretro_string_equal_nocase_wildcard(const char* test, const char* value) {
static int rc_libretro_string_equal_nocase_wildcard(const char* test, const char* match) {
char c1, c2;
while ((c1 = *test++)) {
if (tolower(c1) != tolower(c2 = *value++) && c2 != '?')
if (tolower(c1) != tolower(c2 = *match++) && c2 != '?')
return (c2 == '*');
}

return (*value == '\0');
return (*match == '\0');
}

static int rc_libretro_numeric_less_than(const char* test, const char* value) {
Expand All @@ -218,7 +218,50 @@ static int rc_libretro_numeric_less_than(const char* test, const char* value) {
return (test_num < value_num);
}

static int rc_libretro_match_token(const char* val, const char* token, size_t size, int* result) {
if (*token == '!') {
/* !X => if X is a match, it's explicitly allowed. match with result = false */
if (rc_libretro_match_token(val, token + 1, size - 1, result)) {
*result = 0;
return 1;
}
}

if (*token == '<') {
/* if val < token, match with result = true */
char buffer[128];
memcpy(buffer, token + 1, size - 1);
buffer[size - 1] = '\0';
if (rc_libretro_numeric_less_than(val, buffer)) {
*result = 1;
return 1;
}
}

if (memcmp(token, val, size) == 0 && val[size] == 0) {
/* exact match, match with result = true */
*result = 1;
return 1;
}
else {
/* check for case insensitive match */
char buffer[128];
memcpy(buffer, token, size);
buffer[size] = '\0';
if (rc_libretro_string_equal_nocase_wildcard(val, buffer)) {
/* case insensitive match, match with result = true */
*result = 1;
return 1;
}
}

/* no match */
return 0;
}

static int rc_libretro_match_value(const char* val, const char* match) {
int result = 0;

/* if value starts with a comma, it's a CSV list of potential matches */
if (*match == ',') {
do {
Expand All @@ -229,33 +272,23 @@ static int rc_libretro_match_value(const char* val, const char* match) {
++match;

size = match - ptr;
if (val[size] == '\0') {
if (memcmp(ptr, val, size) == 0) {
return 1;
}
else {
char buffer[128];
memcpy(buffer, ptr, size);
buffer[size] = '\0';
if (rc_libretro_string_equal_nocase_wildcard(buffer, val))
return 1;
}
}
} while (*match == ',');
if (rc_libretro_match_token(val, ptr, size, &result))
return result;

return 0;
} while (*match == ',');
}
else {
/* a leading exclamation point means the provided value(s) are not forbidden (are allowed) */
if (*match == '!')
return !rc_libretro_match_value(val, &match[1]);

/* a leading exclamation point means the provided value(s) are not forbidden (are allowed) */
if (*match == '!')
return !rc_libretro_match_value(val, &match[1]);

/* a leading less tahn means the provided value is the minimum allowed */
if (*match == '<')
return rc_libretro_numeric_less_than(val, &match[1]);
/* just a single value, attempt to match it */
if (rc_libretro_match_token(val, match, strlen(match), &result))
return result;
}

/* just a single value, attempt to match it */
return rc_libretro_string_equal_nocase_wildcard(val, match);
/* value did not match filters, assume it's allowed */
return 0;
}

int rc_libretro_is_setting_allowed(const rc_disallowed_setting_t* disallowed_settings, const char* setting, const char* value) {
Expand Down
23 changes: 12 additions & 11 deletions test/test_rc_libretro.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,13 +698,13 @@ void test_rc_libretro(void) {
TEST_SUITE_BEGIN();

/* rc_libretro_disallowed_settings */
TEST_PARAMS3(test_allowed_setting, "Beetle PSX", "beetle_psx_cpu_freq_scale", "750%");
TEST_PARAMS3(test_allowed_setting, "Beetle PSX", "beetle_psx_cpu_freq_scale", "100%(native)");
TEST_PARAMS3(test_allowed_setting, "Beetle PSX", "beetle_psx_cpu_freq_scale", "750%");
TEST_PARAMS3(test_allowed_setting, "Beetle PSX", "beetle_psx_cpu_freq_scale", "100%(native)");
TEST_PARAMS3(test_disallowed_setting, "Beetle PSX", "beetle_psx_cpu_freq_scale", "99%");
TEST_PARAMS3(test_disallowed_setting, "Beetle PSX", "beetle_psx_cpu_freq_scale", "50%");

TEST_PARAMS3(test_allowed_setting, "Beetle PSX HW", "beetle_psx_hw_cpu_freq_scale", "750%");
TEST_PARAMS3(test_allowed_setting, "Beetle PSX HW", "beetle_psx_hw_cpu_freq_scale", "100%(native)");
TEST_PARAMS3(test_allowed_setting, "Beetle PSX HW", "beetle_psx_hw_cpu_freq_scale", "750%");
TEST_PARAMS3(test_allowed_setting, "Beetle PSX HW", "beetle_psx_hw_cpu_freq_scale", "100%(native)");
TEST_PARAMS3(test_disallowed_setting, "Beetle PSX HW", "beetle_psx_hw_cpu_freq_scale", "99%");
TEST_PARAMS3(test_disallowed_setting, "Beetle PSX HW", "beetle_psx_hw_cpu_freq_scale", "50%");

Expand Down Expand Up @@ -734,8 +734,8 @@ void test_rc_libretro(void) {
TEST_PARAMS3(test_disallowed_setting, "FCEUmm", "fceumm_region", "Dendy");
TEST_PARAMS3(test_allowed_setting, "FCEUmm", "fceumm_palette", "default"); /* setting we don't care about */

TEST_PARAMS3(test_allowed_setting, "Flycast", "reicast_sh4clock", "500");
TEST_PARAMS3(test_allowed_setting, "Flycast", "reicast_sh4clock", "200");
TEST_PARAMS3(test_allowed_setting, "Flycast", "reicast_sh4clock", "500");
TEST_PARAMS3(test_allowed_setting, "Flycast", "reicast_sh4clock", "200");
TEST_PARAMS3(test_disallowed_setting, "Flycast", "reicast_sh4clock", "190");
TEST_PARAMS3(test_disallowed_setting, "Flycast", "reicast_sh4clock", "50");

Expand Down Expand Up @@ -796,9 +796,10 @@ void test_rc_libretro(void) {
TEST_PARAMS3(test_allowed_setting, "PCSX-ReARMed", "pcsx_rearmed_region", "Auto");
TEST_PARAMS3(test_allowed_setting, "PCSX-ReARMed", "pcsx_rearmed_region", "NTSC");
TEST_PARAMS3(test_disallowed_setting, "PCSX-ReARMed", "pcsx_rearmed_region", "PAL");
TEST_PARAMS3(test_allowed_setting, "PCSX-ReARMed", "pcsx_rearmed_psxclock", "100");
TEST_PARAMS3(test_allowed_setting, "PCSX-ReARMed", "pcsx_rearmed_psxclock", "57");
TEST_PARAMS3(test_allowed_setting, "PCSX-ReARMed", "pcsx_rearmed_psxclock", "55");
TEST_PARAMS3(test_allowed_setting, "PCSX-ReARMed", "pcsx_rearmed_psxclock", "auto");
TEST_PARAMS3(test_allowed_setting, "PCSX-ReARMed", "pcsx_rearmed_psxclock", "100");
TEST_PARAMS3(test_allowed_setting, "PCSX-ReARMed", "pcsx_rearmed_psxclock", "57");
TEST_PARAMS3(test_allowed_setting, "PCSX-ReARMed", "pcsx_rearmed_psxclock", "55");
TEST_PARAMS3(test_disallowed_setting, "PCSX-ReARMed", "pcsx_rearmed_psxclock", "54");
TEST_PARAMS3(test_disallowed_setting, "PCSX-ReARMed", "pcsx_rearmed_psxclock", "30");

Expand Down Expand Up @@ -831,8 +832,8 @@ void test_rc_libretro(void) {
TEST_PARAMS3(test_allowed_setting, "Snes9x", "snes9x_layer_5", "enabled");
TEST_PARAMS3(test_disallowed_setting, "Snes9x", "snes9x_layer_5", "disabled");

TEST_PARAMS3(test_allowed_setting, "SwanStation", "swanstation_CPU_Overclock", "1000");
TEST_PARAMS3(test_allowed_setting, "SwanStation", "swanstation_CPU_Overclock", "100");
TEST_PARAMS3(test_allowed_setting, "SwanStation", "swanstation_CPU_Overclock", "1000");
TEST_PARAMS3(test_allowed_setting, "SwanStation", "swanstation_CPU_Overclock", "100");
TEST_PARAMS3(test_disallowed_setting, "SwanStation", "swanstation_CPU_Overclock", "99");
TEST_PARAMS3(test_disallowed_setting, "SwanStation", "swanstation_CPU_Overclock", "50");

Expand Down

0 comments on commit 3a91a58

Please sign in to comment.