PatchEngine: Make PatchType an enum class

Makes the enum strongly typed. A function for retrieving the string
representation of the enum is also added, which allows hiding the array
that contains all of the strings from view (i.e. we operate on the API,
not the exposed internals). This also allows us to bounds check any
querying for the strings.
This commit is contained in:
Lioncash
2018-05-13 14:10:58 -04:00
parent a166cf2481
commit 0995cfef6a
6 changed files with 53 additions and 40 deletions

View File

@ -9,6 +9,8 @@
#include "Core/PatchEngine.h"
#include <algorithm>
#include <array>
#include <iterator>
#include <map>
#include <set>
#include <string>
@ -26,15 +28,20 @@
namespace PatchEngine
{
const char* PatchTypeStrings[] = {
constexpr std::array<const char*, 3> s_patch_type_strings{{
"byte",
"word",
"dword",
};
}};
static std::vector<Patch> onFrame;
static std::map<u32, int> speedHacks;
const char* PatchTypeAsString(PatchType type)
{
return s_patch_type_strings.at(static_cast<int>(type));
}
void LoadPatchSection(const std::string& section, std::vector<Patch>& patches, IniFile& globalIni,
IniFile& localIni)
{
@ -97,8 +104,10 @@ void LoadPatchSection(const std::string& section, std::vector<Patch>& patches, I
success &= TryParse(items[0], &pE.address);
success &= TryParse(items[2], &pE.value);
pE.type = PatchType(std::find(PatchTypeStrings, PatchTypeStrings + 3, items[1]) -
PatchTypeStrings);
const auto iter =
std::find(s_patch_type_strings.begin(), s_patch_type_strings.end(), items[1]);
pE.type = PatchType(std::distance(s_patch_type_strings.begin(), iter));
success &= (pE.type != (PatchType)3);
if (success)
{
@ -173,13 +182,13 @@ static void ApplyPatches(const std::vector<Patch>& patches)
u32 value = entry.value;
switch (entry.type)
{
case PATCH_8BIT:
PowerPC::HostWrite_U8((u8)value, addr);
case PatchType::Patch8Bit:
PowerPC::HostWrite_U8(static_cast<u8>(value), addr);
break;
case PATCH_16BIT:
PowerPC::HostWrite_U16((u16)value, addr);
case PatchType::Patch16Bit:
PowerPC::HostWrite_U16(static_cast<u16>(value), addr);
break;
case PATCH_32BIT:
case PatchType::Patch32Bit:
PowerPC::HostWrite_U32(value, addr);
break;
default: