Merge pull request #5183 from lioncash/sig

MEGASignatureDB: Minor changes
This commit is contained in:
Matthew Parlane 2017-03-29 10:48:45 +13:00 committed by GitHub
commit 9930052725
3 changed files with 69 additions and 71 deletions

View File

@ -8,6 +8,8 @@
#include <cstdlib>
#include <fstream>
#include <limits>
#include <sstream>
#include <utility>
#include "Common/FileUtil.h"
#include "Common/Logging/Log.h"
@ -16,66 +18,11 @@
#include "Core/PowerPC/PPCSymbolDB.h"
#include "Core/PowerPC/PowerPC.h"
static constexpr size_t INSTRUCTION_HEXSTRING_LENGTH = 8;
MEGASignatureDB::MEGASignatureDB() = default;
MEGASignatureDB::~MEGASignatureDB() = default;
bool MEGASignatureDB::Load(const std::string& file_path)
namespace
{
std::string line;
std::ifstream ifs;
OpenFStream(ifs, file_path, std::ios_base::in);
constexpr size_t INSTRUCTION_HEXSTRING_LENGTH = 8;
if (!ifs)
return false;
for (size_t i = 1; std::getline(ifs, line); ++i)
{
std::istringstream iss(line);
MEGASignature sig;
if (GetCode(&sig, &iss) && GetName(&sig, &iss) && GetRefs(&sig, &iss))
{
m_signatures.push_back(sig);
}
else
{
WARN_LOG(OSHLE, "MEGA database failed to parse line %zu", i);
}
}
return true;
}
void MEGASignatureDB::Apply(PPCSymbolDB* symbol_db) const
{
for (auto& it : symbol_db->AccessSymbols())
{
u32 hash = it.first;
auto& symbol = it.second;
for (const auto& sig : m_signatures)
{
if (Compare(symbol.address, symbol.size, sig))
{
symbol.name = sig.name;
INFO_LOG(OSHLE, "Found %s at %08x (size: %08x)!", sig.name.c_str(), symbol.address,
symbol.size);
break;
}
}
}
symbol_db->Index();
}
void MEGASignatureDB::List() const
{
for (const auto& entry : m_signatures)
{
DEBUG_LOG(OSHLE, "%s : %zu bytes", entry.name.c_str(), entry.code.size() * sizeof(u32));
}
INFO_LOG(OSHLE, "%zu functions known in current MEGA database.", m_signatures.size());
}
bool MEGASignatureDB::GetCode(MEGASignature* sig, std::istringstream* iss) const
bool GetCode(MEGASignature* sig, std::istringstream* iss)
{
std::string code;
if ((*iss >> code) && (code.length() % INSTRUCTION_HEXSTRING_LENGTH) == 0)
@ -99,7 +46,7 @@ bool MEGASignatureDB::GetCode(MEGASignature* sig, std::istringstream* iss) const
return false;
}
bool MEGASignatureDB::GetFunctionName(std::istringstream* iss, std::string* name) const
bool GetFunctionName(std::istringstream* iss, std::string* name)
{
std::string buffer;
@ -117,13 +64,13 @@ bool MEGASignatureDB::GetFunctionName(std::istringstream* iss, std::string* name
return true;
}
bool MEGASignatureDB::GetName(MEGASignature* sig, std::istringstream* iss) const
bool GetName(MEGASignature* sig, std::istringstream* iss)
{
std::string unknown;
return (*iss >> unknown) && GetFunctionName(iss, &sig->name);
}
bool MEGASignatureDB::GetRefs(MEGASignature* sig, std::istringstream* iss) const
bool GetRefs(MEGASignature* sig, std::istringstream* iss)
{
std::string num, ref;
u32 ref_count = 1;
@ -145,7 +92,7 @@ bool MEGASignatureDB::GetRefs(MEGASignature* sig, std::istringstream* iss) const
WARN_LOG(OSHLE, "MEGA database failed to parse reference %u name", ref_count);
return false;
}
sig->refs.emplace_back(static_cast<u32>(offset), ref);
sig->refs.emplace_back(static_cast<u32>(offset), std::move(ref));
ref_count += 1;
num.clear();
@ -154,7 +101,7 @@ bool MEGASignatureDB::GetRefs(MEGASignature* sig, std::istringstream* iss) const
return true;
}
bool MEGASignatureDB::Compare(u32 address, u32 size, const MEGASignature& sig) const
bool Compare(u32 address, u32 size, const MEGASignature& sig)
{
if (size != sig.code.size() * sizeof(u32))
return false;
@ -167,3 +114,61 @@ bool MEGASignatureDB::Compare(u32 address, u32 size, const MEGASignature& sig) c
}
return true;
}
} // Anonymous namespace
MEGASignatureDB::MEGASignatureDB() = default;
MEGASignatureDB::~MEGASignatureDB() = default;
bool MEGASignatureDB::Load(const std::string& file_path)
{
std::ifstream ifs;
OpenFStream(ifs, file_path, std::ios_base::in);
if (!ifs)
return false;
std::string line;
for (size_t i = 1; std::getline(ifs, line); ++i)
{
std::istringstream iss(line);
MEGASignature sig;
if (GetCode(&sig, &iss) && GetName(&sig, &iss) && GetRefs(&sig, &iss))
{
m_signatures.push_back(std::move(sig));
}
else
{
WARN_LOG(OSHLE, "MEGA database failed to parse line %zu", i);
}
}
return true;
}
void MEGASignatureDB::Apply(PPCSymbolDB* symbol_db) const
{
for (auto& it : symbol_db->AccessSymbols())
{
auto& symbol = it.second;
for (const auto& sig : m_signatures)
{
if (Compare(symbol.address, symbol.size, sig))
{
symbol.name = sig.name;
INFO_LOG(OSHLE, "Found %s at %08x (size: %08x)!", sig.name.c_str(), symbol.address,
symbol.size);
break;
}
}
}
symbol_db->Index();
}
void MEGASignatureDB::List() const
{
for (const auto& entry : m_signatures)
{
DEBUG_LOG(OSHLE, "%s : %zu bytes", entry.name.c_str(), entry.code.size() * sizeof(u32));
}
INFO_LOG(OSHLE, "%zu functions known in current MEGA database.", m_signatures.size());
}

View File

@ -4,7 +4,6 @@
#pragma once
#include <sstream>
#include <string>
#include <vector>
@ -47,11 +46,5 @@ public:
void List() const;
private:
bool GetCode(MEGASignature* sig, std::istringstream* iss) const;
bool GetFunctionName(std::istringstream* iss, std::string* name) const;
bool GetName(MEGASignature* sig, std::istringstream* iss) const;
bool GetRefs(MEGASignature* sig, std::istringstream* iss) const;
bool Compare(u32 address, u32 size, const MEGASignature& sig) const;
std::vector<MEGASignature> m_signatures;
};

View File

@ -466,7 +466,7 @@ wxMenu* MainMenuBar::CreateSymbolsMenu() const
"used in multiple games, by loading them from a .dsy file."));
symbols_menu->Append(
IDM_USE_MEGA_SIGNATURE_FILE, _("Apply &MEGA Signature File..."),
_("Must use Generate Symbol Map first! Recognise names of any standard library functions "
_("Must use Generate Symbols first! Recognise names of any standard library functions "
"used in multiple games, by loading them from a .mega file."));
symbols_menu->AppendSeparator();
symbols_menu->Append(IDM_PATCH_HLE_FUNCTIONS, _("&Patch HLE Functions"));