Simple patch engine

This commit is contained in:
Nekotekina 2017-03-29 02:54:05 +03:00
parent 77e88741bc
commit 78b9c64f67
5 changed files with 160 additions and 0 deletions

105
Utilities/bin_patch.cpp Normal file
View File

@ -0,0 +1,105 @@
#include "bin_patch.h"
#include "yaml-cpp/yaml.h"
#include "File.h"
#include "Config.h"
template <>
void fmt_class_string<patch_type>::format(std::string& out, u64 arg)
{
format_enum(out, arg, [](patch_type value)
{
switch (value)
{
case patch_type::byte: return "byte";
case patch_type::le16: return "le16";
case patch_type::le32: return "le32";
case patch_type::le64: return "le64";
case patch_type::be16: return "be16";
case patch_type::be32: return "be32";
case patch_type::be64: return "be64";
}
return unknown;
});
}
void patch_engine::append(const std::string& patch)
{
if (fs::file f{patch})
{
auto root = YAML::Load(f.to_string());
for (auto pair : root)
{
auto& name = pair.first.Scalar();
auto& data = m_map[name];
for (auto patch : pair.second)
{
u64 type64 = 0;
cfg::try_to_enum_value(&type64, &fmt_class_string<patch_type>::format, patch[0].Scalar());
struct patch info;
info.type = static_cast<patch_type>(type64);
info.offset = patch[1].as<u32>();
info.value = patch[2].as<u64>();
data.emplace_back(info);
}
}
}
}
void patch_engine::apply(const std::string& name, u8* dst) const
{
const auto found = m_map.find(name);
if (found == m_map.cend())
{
return;
}
// Apply modifications sequentially
for (const auto& p : found->second)
{
auto ptr = dst + p.offset;
switch (p.type)
{
case patch_type::byte:
{
*ptr = static_cast<u8>(p.value);
break;
}
case patch_type::le16:
{
*reinterpret_cast<le_t<u16, 1>*>(ptr) = static_cast<u16>(p.value);
break;
}
case patch_type::le32:
{
*reinterpret_cast<le_t<u32, 1>*>(ptr) = static_cast<u32>(p.value);
break;
}
case patch_type::le64:
{
*reinterpret_cast<le_t<u64, 1>*>(ptr) = static_cast<u64>(p.value);
break;
}
case patch_type::be16:
{
*reinterpret_cast<be_t<u16, 1>*>(ptr) = static_cast<u16>(p.value);
break;
}
case patch_type::be32:
{
*reinterpret_cast<be_t<u32, 1>*>(ptr) = static_cast<u32>(p.value);
break;
}
case patch_type::be64:
{
*reinterpret_cast<be_t<u64, 1>*>(ptr) = static_cast<u64>(p.value);
break;
}
}
}
}

37
Utilities/bin_patch.h Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#include "BEType.h"
#include <vector>
#include <string>
#include <unordered_map>
enum class patch_type
{
byte,
le16,
le32,
le64,
be16,
be32,
be64,
};
class patch_engine
{
struct patch
{
patch_type type;
u32 offset;
u64 value;
};
// Database
std::unordered_map<std::string, std::vector<patch>> m_map;
public:
// Load from file
void append(const std::string& path);
// Apply patch
void apply(const std::string& name, u8* dst) const;
};

View File

@ -2,6 +2,7 @@
#include "Utilities/Config.h"
#include "Utilities/AutoPause.h"
#include "Utilities/event.h"
#include "Utilities/bin_patch.h"
#include "Emu/Memory/Memory.h"
#include "Emu/System.h"
@ -99,6 +100,9 @@ void Emulator::Init()
fs::create_dir(dev_hdd1 + "game/");
fs::create_path(dev_hdd1);
fs::create_path(dev_usb);
// Initialize patch engine
fxm::make_always<patch_engine>()->append(fs::get_config_dir() + "/patch.yml");
}
void Emulator::SetPath(const std::string& path, const std::string& elf_path)
@ -215,6 +219,10 @@ void Emulator::Load()
LOG_NOTICE(LOADER, "Used configuration:\n%s\n", cfg::root.to_string());
// Load patches from different locations
fxm::check_unlocked<patch_engine>()->append(fs::get_config_dir() + "data/" + m_title_id + "/patch.yml");
fxm::check_unlocked<patch_engine>()->append(m_cache_path + "/patch.yml");
// Mount all devices
const std::string emu_dir_ = g_cfg_vfs_emulator_dir;
const std::string emu_dir = emu_dir_.empty() ? fs::get_config_dir() : emu_dir_;

View File

@ -73,6 +73,9 @@
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\Utilities\AutoPause.cpp" />
<ClCompile Include="..\Utilities\bin_patch.cpp">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\Utilities\cond.cpp">
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
@ -396,6 +399,7 @@
<ClInclude Include="..\Utilities\AtomicPtr.h" />
<ClInclude Include="..\Utilities\AutoPause.h" />
<ClInclude Include="..\Utilities\BEType.h" />
<ClInclude Include="..\Utilities\bin_patch.h" />
<ClInclude Include="..\Utilities\BitField.h" />
<ClInclude Include="..\Utilities\bit_set.h" />
<ClInclude Include="..\Utilities\cfmt.h" />

View File

@ -908,6 +908,9 @@
<ClCompile Include="Loader\TAR.cpp">
<Filter>Loader</Filter>
</ClCompile>
<ClCompile Include="..\Utilities\bin_patch.cpp">
<Filter>Utilities</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Crypto\aes.h">
@ -1738,5 +1741,8 @@
<ClInclude Include="Loader\TAR.h">
<Filter>Loader</Filter>
</ClInclude>
<ClInclude Include="..\Utilities\bin_patch.h">
<Filter>Utilities</Filter>
</ClInclude>
</ItemGroup>
</Project>