Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove primary cache #97

Merged
merged 6 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ Source/*.VC.opendb
Source/*.VC.db
Source/.vs/
Source/.vscode/
.vscode/
.idea/*
2 changes: 1 addition & 1 deletion Source/Common/CommonUtils.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@ inline u32 cacheIndexToOffset(u32 cacheIndex, bool considerAram)
}
return cacheIndex;
}
} // namespace Common
} // namespace Common
2 changes: 1 addition & 1 deletion Source/Common/MemoryCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,4 @@ std::string formatMemoryToString(const char* memory, const MemType type, const s
break;
}
}
} // namespace Common
} // namespace Common
4 changes: 2 additions & 2 deletions Source/Common/MemoryCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ enum class MemBase
base_hexadecimal,
base_octal,
base_binary,
base_none // Placeholder when the base doesn't matter (ie. string)
base_none // Placeholder when the base doesn't matter (ie. string)
};

enum class MemOperationReturnCode
Expand All @@ -63,4 +63,4 @@ char* formatStringToMemory(MemOperationReturnCode& returnCode, size_t& actualLen
std::string formatMemoryToString(const char* memory, const MemType type, const size_t length,
const MemBase base, const bool isUnsigned,
const bool withBSwap = false);
} // namespace Common
} // namespace Common
66 changes: 21 additions & 45 deletions Source/DolphinProcess/DolphinAccessor.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "DolphinAccessor.h"
#ifdef linux
#ifdef __linux__
#include "Linux/LinuxDolphinProcess.h"
#elif _WIN32
#include "Windows/WindowsDolphinProcess.h"
#endif

#include <cstring>
#include <memory>

#include "../Common/CommonUtils.h"
#include "../Common/MemoryCommon.h"
Expand All @@ -14,7 +15,6 @@ namespace DolphinComm
{
IDolphinProcess* DolphinAccessor::m_instance = nullptr;
DolphinAccessor::DolphinStatus DolphinAccessor::m_status = DolphinStatus::unHooked;
char* DolphinAccessor::m_updatedRAMCache = nullptr;

void DolphinAccessor::init()
{
Expand All @@ -32,7 +32,6 @@ void DolphinAccessor::init()
void DolphinAccessor::free()
{
delete m_instance;
delete[] m_updatedRAMCache;
}

void DolphinAccessor::hook()
Expand All @@ -49,7 +48,6 @@ void DolphinAccessor::hook()
else
{
m_status = DolphinStatus::hooked;
updateRAMCache();
}
}

Expand Down Expand Up @@ -119,12 +117,7 @@ bool DolphinAccessor::isValidConsoleAddress(const u32 address)
return false;
}

char* DolphinAccessor::getRAMCache()
{
return m_updatedRAMCache;
}

size_t DolphinAccessor::getRAMCacheSize()
size_t DolphinAccessor::getRAMTotalSize()
{
if (isMEM2Present())
{
Expand All @@ -140,72 +133,55 @@ size_t DolphinAccessor::getRAMCacheSize()
}
}

Common::MemOperationReturnCode DolphinAccessor::updateRAMCache()
Common::MemOperationReturnCode DolphinAccessor::readEntireRAM(char* buffer)
{
delete[] m_updatedRAMCache;
m_updatedRAMCache = nullptr;

// MEM2, if enabled, is read right after MEM1 in the cache so both regions are contigous
// MEM2, if enabled, is read right after MEM1 in the buffer so both regions are contigous
if (isMEM2Present())
{
m_updatedRAMCache = new char[Common::GetMEM1SizeReal() + Common::GetMEM2SizeReal()];

if (!DolphinComm::DolphinAccessor::readFromRAM(
Common::dolphinAddrToOffset(Common::MEM1_START, false), m_updatedRAMCache,
Common::dolphinAddrToOffset(Common::MEM1_START, false), buffer,
Common::GetMEM1SizeReal(), false))
return Common::MemOperationReturnCode::operationFailed;

// Read Wii extra RAM
if (!DolphinComm::DolphinAccessor::readFromRAM(
Common::dolphinAddrToOffset(Common::MEM2_START, false),
m_updatedRAMCache + Common::GetMEM1SizeReal(), Common::GetMEM2SizeReal(), false))
buffer + Common::GetMEM1SizeReal(), Common::GetMEM2SizeReal(), false))
return Common::MemOperationReturnCode::operationFailed;
}
else if (isARAMAccessible())
{
m_updatedRAMCache = new char[Common::ARAM_SIZE + Common::GetMEM1SizeReal()];
// read ARAM
if (!DolphinComm::DolphinAccessor::readFromRAM(
Common::dolphinAddrToOffset(Common::ARAM_START, true), m_updatedRAMCache,
Common::ARAM_SIZE, false))
Common::dolphinAddrToOffset(Common::ARAM_START, true), buffer, Common::ARAM_SIZE,
false))
return Common::MemOperationReturnCode::operationFailed;

// Read GameCube and Wii basic RAM
if (!DolphinComm::DolphinAccessor::readFromRAM(
Common::dolphinAddrToOffset(Common::MEM1_START, true),
m_updatedRAMCache + Common::ARAM_SIZE, Common::GetMEM1SizeReal(), false))
Common::dolphinAddrToOffset(Common::MEM1_START, true), buffer + Common::ARAM_SIZE,
Common::GetMEM1SizeReal(), false))
return Common::MemOperationReturnCode::operationFailed;
}
else
{
m_updatedRAMCache = new char[Common::GetMEM1SizeReal()];
if (!DolphinComm::DolphinAccessor::readFromRAM(
Common::dolphinAddrToOffset(Common::MEM1_START, false), m_updatedRAMCache,
Common::dolphinAddrToOffset(Common::MEM1_START, false), buffer,
Common::GetMEM1SizeReal(), false))
return Common::MemOperationReturnCode::operationFailed;
}

return Common::MemOperationReturnCode::OK;
}

std::string DolphinAccessor::getFormattedValueFromCache(const u32 ramIndex, Common::MemType memType,
size_t memSize, Common::MemBase memBase,
bool memIsUnsigned)
{
return Common::formatMemoryToString(&m_updatedRAMCache[ramIndex], memType, memSize, memBase,
memIsUnsigned, Common::shouldBeBSwappedForType(memType));
}

void DolphinAccessor::copyRawMemoryFromCache(char* dest, const u32 consoleAddress,
const size_t byteCount)
std::string DolphinAccessor::getFormattedValueFromMemory(const u32 ramIndex,
Common::MemType memType, size_t memSize,
Common::MemBase memBase,
bool memIsUnsigned)
{
if (isValidConsoleAddress(consoleAddress) &&
isValidConsoleAddress((consoleAddress + static_cast<u32>(byteCount)) - 1))
{
bool aramAccessible = isARAMAccessible();
u32 offset = Common::dolphinAddrToOffset(consoleAddress, isARAMAccessible());
u32 cacheIndex = Common::offsetToCacheIndex(offset, aramAccessible);
std::memcpy(dest, m_updatedRAMCache + cacheIndex, byteCount);
}
std::unique_ptr<char[]> buffer(new char[memSize]);
readFromRAM(ramIndex, buffer.get(), memSize, false);
return Common::formatMemoryToString(buffer.get(), memType, memSize, memBase, memIsUnsigned,
Common::shouldBeBSwappedForType(memType));
}
} // namespace DolphinComm
} // namespace DolphinComm
15 changes: 6 additions & 9 deletions Source/DolphinProcess/DolphinAccessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,15 @@ class DolphinAccessor
static bool isARAMAccessible();
static u64 getARAMAddressStart();
static bool isMEM2Present();
static char* getRAMCache();
static size_t getRAMCacheSize();
static Common::MemOperationReturnCode updateRAMCache();
static std::string getFormattedValueFromCache(const u32 ramIndex, Common::MemType memType,
size_t memSize, Common::MemBase memBase,
bool memIsUnsigned);
static void copyRawMemoryFromCache(char* dest, const u32 consoleAddress, const size_t byteCount);
static size_t getRAMTotalSize();
static Common::MemOperationReturnCode readEntireRAM(char* buffer);
static std::string getFormattedValueFromMemory(const u32 ramIndex, Common::MemType memType,
size_t memSize, Common::MemBase memBase,
bool memIsUnsigned);
static bool isValidConsoleAddress(const u32 address);

private:
static IDolphinProcess* m_instance;
static DolphinStatus m_status;
static char* m_updatedRAMCache;
};
} // namespace DolphinComm
} // namespace DolphinComm
36 changes: 8 additions & 28 deletions Source/DolphinProcess/IDolphinProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,20 @@ namespace DolphinComm
class IDolphinProcess
{
public:
virtual ~IDolphinProcess()
{
}
virtual ~IDolphinProcess() {}
virtual bool findPID() = 0;
virtual bool obtainEmuRAMInformations() = 0;
virtual bool readFromRAM(const u32 offset, char* buffer, const size_t size,
const bool withBSwap) = 0;
virtual bool writeToRAM(const u32 offset, const char* buffer, const size_t size,
const bool withBSwap) = 0;

int getPID() const
{
return m_PID;
};
u64 getEmuRAMAddressStart() const
{
return m_emuRAMAddressStart;
};
bool isMEM2Present() const
{
return m_MEM2Present;
};
bool isARAMAccessible() const
{
return m_ARAMAccessible;
};
u64 getARAMAddressStart() const
{
return m_emuARAMAdressStart;
};
u64 getMEM2AddressStart() const
{
return m_MEM2AddressStart;
};
int getPID() const { return m_PID; };
u64 getEmuRAMAddressStart() const { return m_emuRAMAddressStart; };
bool isMEM2Present() const { return m_MEM2Present; };
bool isARAMAccessible() const { return m_ARAMAccessible; };
u64 getARAMAddressStart() const { return m_emuARAMAdressStart; };
u64 getMEM2AddressStart() const { return m_MEM2AddressStart; };
u64 getMEM1ToMEM2Distance() const
{
if (!m_MEM2Present)
Expand All @@ -59,4 +39,4 @@ class IDolphinProcess
bool m_ARAMAccessible = false;
bool m_MEM2Present = false;
};
} // namespace DolphinComm
} // namespace DolphinComm
3 changes: 2 additions & 1 deletion Source/DolphinProcess/Linux/LinuxDolphinProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ bool LinuxDolphinProcess::findPID()
if (directoryPointer == nullptr)
return false;

m_PID = -1;
struct dirent* directoryEntry = nullptr;
while (m_PID == -1 && (directoryEntry = readdir(directoryPointer)))
{
Expand Down Expand Up @@ -265,5 +266,5 @@ bool LinuxDolphinProcess::writeToRAM(const u32 offset, const char* buffer, const

return true;
}
} // namespace DolphinComm
} // namespace DolphinComm
#endif
6 changes: 2 additions & 4 deletions Source/DolphinProcess/Linux/LinuxDolphinProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ namespace DolphinComm
class LinuxDolphinProcess : public IDolphinProcess
{
public:
LinuxDolphinProcess()
{
}
LinuxDolphinProcess() {}
bool findPID() override;
bool obtainEmuRAMInformations() override;
bool readFromRAM(const u32 offset, char* buffer, size_t size, const bool withBSwap) override;
bool writeToRAM(const u32 offset, const char* buffer, const size_t size,
const bool withBSwap) override;
};
} // namespace DolphinComm
} // namespace DolphinComm
#endif
3 changes: 2 additions & 1 deletion Source/DolphinProcess/Windows/WindowsDolphinProcess.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bool WindowsDolphinProcess::findPID()

HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

m_PID = -1;
if (Process32First(snapshot, &entry) == TRUE)
{
do
Expand Down Expand Up @@ -242,5 +243,5 @@ bool WindowsDolphinProcess::writeToRAM(const u32 offset, const char* buffer, con
delete[] bufferCopy;
return (bResult && nread == size);
}
} // namespace DolphinComm
} // namespace DolphinComm
#endif
6 changes: 2 additions & 4 deletions Source/DolphinProcess/Windows/WindowsDolphinProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ namespace DolphinComm
class WindowsDolphinProcess : public IDolphinProcess
{
public:
WindowsDolphinProcess()
{
}
WindowsDolphinProcess() {}
bool findPID() override;
bool obtainEmuRAMInformations() override;
bool readFromRAM(const u32 offset, char* buffer, const size_t size,
Expand All @@ -24,5 +22,5 @@ class WindowsDolphinProcess : public IDolphinProcess
private:
HANDLE m_hDolphin;
};
} // namespace DolphinComm
} // namespace DolphinComm
#endif
2 changes: 1 addition & 1 deletion Source/GUI/GUICommon.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ void changeApplicationStyle(int index)
}
}

} // namespace GUICommon
} // namespace GUICommon
2 changes: 1 addition & 1 deletion Source/GUI/GUICommon.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ QString getNameFromBase(const Common::MemBase base);
void changeApplicationStyle(int);

extern bool g_valueEditing;
} // namespace GUICommon
} // namespace GUICommon
14 changes: 8 additions & 6 deletions Source/GUI/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include <QVBoxLayout>
#include <string>

#include "GUICommon.h"
#include "../DolphinProcess/DolphinAccessor.h"
#include "../MemoryWatch/MemWatchEntry.h"
#include "GUICommon.h"
#include "Settings/DlgSettings.h"
#include "Settings/SConfig.h"

Expand Down Expand Up @@ -159,9 +159,9 @@ void MainWindow::makeLayouts()
if (SConfig::getInstance().getSplitterState().size())
splitter->restoreState(SConfig::getInstance().getSplitterState());

connect(splitter, &QSplitter::splitterMoved,
[splitter = splitter]()
{ SConfig::getInstance().setSplitterState(splitter->saveState()); });
connect(splitter, &QSplitter::splitterMoved, [splitter = splitter]() {
SConfig::getInstance().setSplitterState(splitter->saveState());
});

QVBoxLayout* mainLayout = new QVBoxLayout;
mainLayout->addWidget(m_lblDolphinStatus);
Expand Down Expand Up @@ -427,9 +427,11 @@ void MainWindow::onAbout()
tr("A RAM search made to facilitate research and reverse engineering of GameCube and Wii "
"games using the Dolphin emulator.") +
"<br>" +
tr("<a href=\"https://github.com/aldelaro5/dolphin-memory-engine\">Visit the project page</a> to learn more and check for updates.") +
tr("<a href=\"https://github.com/aldelaro5/dolphin-memory-engine\">Visit the project "
"page</a> to learn more and check for updates.") +
"<br><br>" +
tr("This program is licensed under the MIT license. You should have received a copy of the MIT license along with this program.");
tr("This program is licensed under the MIT license. You should have received a copy of the "
"MIT license along with this program.");

QMessageBox aboutBox;
aboutBox.setWindowTitle(title);
Expand Down
Loading