Skip to content

Commit

Permalink
Merge pull request #5183 from lioncash/sig
Browse files Browse the repository at this point in the history
MEGASignatureDB: Minor changes
  • Loading branch information
Parlane committed Mar 28, 2017
2 parents fee2577 + 497292c commit 9930052
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 71 deletions.
131 changes: 68 additions & 63 deletions Source/Core/Core/PowerPC/SignatureDB/MEGASignatureDB.cpp
Expand Up @@ -8,6 +8,8 @@
#include <cstdlib>
#include <fstream>
#include <limits>
#include <sstream>
#include <utility>

#include "Common/FileUtil.h"
#include "Common/Logging/Log.h"
Expand All @@ -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);

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;
}
constexpr size_t INSTRUCTION_HEXSTRING_LENGTH = 8;

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)
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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());
}
7 changes: 0 additions & 7 deletions Source/Core/Core/PowerPC/SignatureDB/MEGASignatureDB.h
Expand Up @@ -4,7 +4,6 @@

#pragma once

#include <sstream>
#include <string>
#include <vector>

Expand Down Expand Up @@ -47,11 +46,5 @@ class MEGASignatureDB
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;
};
2 changes: 1 addition & 1 deletion Source/Core/DolphinWX/MainMenuBar.cpp
Expand Up @@ -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"));
Expand Down

0 comments on commit 9930052

Please sign in to comment.