From 174994188123af06cb147a0de8b8783c9fc0a1da Mon Sep 17 00:00:00 2001 From: twinaphex Date: Mon, 23 Jan 2017 14:56:09 +0100 Subject: [PATCH] Refactor patch.c --- patch.c | 62 +++++++++++++++++++++----------------------- patch.h | 6 ++++- tasks/task_content.c | 10 ++++++- 3 files changed, 44 insertions(+), 34 deletions(-) diff --git a/patch.c b/patch.c index ce2f7c5d86..26da1fe5be 100644 --- a/patch.c +++ b/patch.c @@ -33,7 +33,6 @@ #include "msg_hash.h" #include "patch.h" #include "retroarch.h" -#include "runloop.h" #include "verbosity.h" enum bps_mode @@ -553,40 +552,30 @@ error: return false; } -static bool try_bps_patch(uint8_t **buf, ssize_t *size) +static bool try_bps_patch(bool allow_bps, const char *name_bps, + uint8_t **buf, ssize_t *size) { - global_t *global = global_get_ptr(); - bool allow_bps = !rarch_ctl(RARCH_CTL_IS_UPS_PREF, NULL) && !rarch_ctl(RARCH_CTL_IS_IPS_PREF, NULL); - - if (!allow_bps || string_is_empty(global->name.bps)) - return false; - - return apply_patch_content(buf, size, "BPS", global->name.bps, - bps_apply_patch); + if (allow_bps && !string_is_empty(name_bps)) + return apply_patch_content(buf, size, "BPS", name_bps, + bps_apply_patch); + return false; } -static bool try_ups_patch(uint8_t **buf, ssize_t *size) +static bool try_ups_patch(bool allow_ups, const char *name_ups, + uint8_t **buf, ssize_t *size) { - global_t *global = global_get_ptr(); - bool allow_ups = !rarch_ctl(RARCH_CTL_IS_BPS_PREF, NULL) && !rarch_ctl(RARCH_CTL_IS_IPS_PREF, NULL); - - if (!allow_ups || string_is_empty(global->name.ups)) - return false; - - return apply_patch_content(buf, size, "UPS", global->name.ups, - ups_apply_patch); + if (allow_ups && !string_is_empty(name_ups)) + return apply_patch_content(buf, size, "UPS", name_ups, + ups_apply_patch); + return false; } -static bool try_ips_patch(uint8_t **buf, ssize_t *size) +static bool try_ips_patch(bool allow_ips, + const char *name_ips, uint8_t **buf, ssize_t *size) { - global_t *global = global_get_ptr(); - bool allow_ips = !rarch_ctl(RARCH_CTL_IS_UPS_PREF, NULL) && !rarch_ctl(RARCH_CTL_IS_BPS_PREF, NULL); - - if (!allow_ips || string_is_empty(global->name.ips)) - return false; - - return apply_patch_content(buf, size, "IPS", global->name.ips, - ips_apply_patch); + if (allow_ips && !string_is_empty(name_ips)) + return apply_patch_content(buf, size, "IPS", name_ips, ips_apply_patch); + return false; } /** @@ -597,8 +586,17 @@ static bool try_ips_patch(uint8_t **buf, ssize_t *size) * Apply patch to the content file in-memory. * **/ -void patch_content(uint8_t **buf, ssize_t *size) +void patch_content( + const char *name_ips, + const char *name_bps, + const char *name_ups, + uint8_t **buf, + ssize_t *size) { + bool allow_ups = !rarch_ctl(RARCH_CTL_IS_BPS_PREF, NULL) && !rarch_ctl(RARCH_CTL_IS_IPS_PREF, NULL); + bool allow_ips = !rarch_ctl(RARCH_CTL_IS_UPS_PREF, NULL) && !rarch_ctl(RARCH_CTL_IS_BPS_PREF, NULL); + bool allow_bps = !rarch_ctl(RARCH_CTL_IS_UPS_PREF, NULL) && !rarch_ctl(RARCH_CTL_IS_IPS_PREF, NULL); + if ( (unsigned)rarch_ctl(RARCH_CTL_IS_IPS_PREF, NULL) + (unsigned)rarch_ctl(RARCH_CTL_IS_BPS_PREF, NULL) + (unsigned)rarch_ctl(RARCH_CTL_IS_UPS_PREF, NULL) > 1) @@ -608,9 +606,9 @@ void patch_content(uint8_t **buf, ssize_t *size) return; } - if ( !try_ips_patch(buf, size) - && !try_bps_patch(buf, size) - && !try_ups_patch(buf, size)) + if ( !try_ips_patch(allow_ips, name_ips, buf, size) + && !try_bps_patch(allow_bps, name_bps, buf, size) + && !try_ups_patch(allow_ups, name_ups, buf, size)) { RARCH_LOG("%s\n", msg_hash_to_str(MSG_DID_NOT_FIND_A_VALID_CONTENT_PATCH)); diff --git a/patch.h b/patch.h index 9311b555ac..33993d8468 100644 --- a/patch.h +++ b/patch.h @@ -36,7 +36,11 @@ RETRO_BEGIN_DECLS * Apply patch to the content file in-memory. * **/ -void patch_content(uint8_t **buf, ssize_t *size); +void patch_content( + const char *name_ips, + const char *name_bps, + const char *name_ups, + uint8_t **buf, ssize_t *size); RETRO_END_DECLS diff --git a/tasks/task_content.c b/tasks/task_content.c index 651a37fcc2..81f9208cff 100644 --- a/tasks/task_content.c +++ b/tasks/task_content.c @@ -331,7 +331,15 @@ static bool load_content_into_memory(unsigned i, const char *path, void **buf, /* Attempt to apply a patch. */ if (!rarch_ctl(RARCH_CTL_IS_PATCH_BLOCKED, NULL)) - patch_content(&ret_buf, length); + { + global_t *global = global_get_ptr(); + if (global) + patch_content( + global->name.ips, + global->name.bps, + global->name.ups, + &ret_buf, length); + } content_get_crc(&content_crc_ptr);