Skip to content

Commit aa0addf

Browse files
committed
[librii] dolphin: Misc refactor
1 parent acb3ca2 commit aa0addf

File tree

2 files changed

+12
-43
lines changed

2 files changed

+12
-43
lines changed

source/librii/dolphin/Dolphin.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,7 @@ std::optional<int> GetDolphinPID() {
3737
return std::nullopt;
3838
}
3939

40-
SharedMem::~SharedMem() {
41-
if (sharedMem != nullptr) {
42-
#ifdef _WIN32
43-
UnmapViewOfFile(sharedMem);
44-
#endif
45-
sharedMem = nullptr;
46-
}
47-
}
48-
Result<SharedMem> SharedMem::from(std::string memFileName) {
40+
Result<SharedMem> SharedMem_from(std::string memFileName) {
4941
#ifdef _WIN32
5042
HANDLE hMapFile =
5143
CreateFileMappingA(INVALID_HANDLE_VALUE, // Use paging file.
@@ -75,15 +67,16 @@ Result<SharedMem> SharedMem::from(std::string memFileName) {
7567
return std::unexpected(s);
7668
}
7769

78-
return SharedMem(lpMapAddress);
70+
return SharedMem(
71+
lpMapAddress, +[](void* s) -> void { UnmapViewOfFile(s); });
7972
#else
8073
return std::unexpected("Non-windows unsuported");
8174
#endif
8275
}
8376

8477
Result<SharedMem> OpenDolphin(int pid) {
8578
std::string memFileName = "dolphin-emu." + std::to_string(pid);
86-
return SharedMem::from(memFileName);
79+
return SharedMem_from(memFileName);
8780
}
8881

8982
void DolphinAc::dumpMemoryLayout() {

source/librii/dolphin/Dolphin.hpp

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,8 @@
66

77
namespace librii::dolphin {
88

9-
struct SharedMem {
10-
static Result<SharedMem> from(std::string memFileName);
11-
SharedMem() : sharedMem(nullptr) {}
12-
SharedMem(void* s) : sharedMem(s) {}
13-
SharedMem(const SharedMem&) = delete;
14-
SharedMem(SharedMem&& rhs) noexcept : sharedMem(rhs.sharedMem) {
15-
rhs.sharedMem = nullptr;
16-
}
17-
~SharedMem();
18-
void* get() const { return sharedMem; }
19-
20-
void* sharedMem;
21-
};
9+
using SharedMem = std::unique_ptr<void, void (*)(void*)>;
10+
Result<SharedMem> SharedMem_from(std::string memFileName);
2211

2312
std::optional<int> GetDolphinPID();
2413

@@ -77,27 +66,14 @@ struct DolphinAc {
7766
Hooked,
7867
UnHooked,
7968
};
80-
static inline uint32_t code[] = {
81-
llvm::ByteSwap_32(0x029f0010), llvm::ByteSwap_32(0x029f0033),
82-
llvm::ByteSwap_32(0x029f0034), llvm::ByteSwap_32(0x029f0035)};
83-
84-
void* find_sequence(void* start, size_t size) {
85-
uint32_t* p = (uint32_t*)start;
86-
for (size_t i = 0; i < size - 4 + 1; i++, p++) {
87-
if (memcmp(p, code, sizeof(code)) == 0) {
88-
return p;
89-
}
90-
}
91-
return NULL;
92-
}
9369
void hook() {
9470
mPID = GetDolphinPID();
9571
if (mPID) {
96-
new (&mSHM) SharedMem(*OpenDolphin(*mPID));
97-
dumpMemoryLayout();
98-
void* addr = find_sequence((char*)mSHM.get() + GetRamSize(mSHM),
99-
GetExRamSize() / 4);
100-
fmt::print("MEM2 at {:x}\n", (unsigned long long)addr);
72+
auto dol = OpenDolphin(*mPID);
73+
if (dol) {
74+
new (&mSHM) SharedMem(std::move(*dol));
75+
dumpMemoryLayout();
76+
}
10177
}
10278
}
10379

@@ -135,7 +111,7 @@ struct DolphinAc {
135111
void dumpMemoryLayout();
136112
void dumpRegion(const std::string& name, u32 virtualStart, u32 size);
137113

138-
SharedMem mSHM;
114+
SharedMem mSHM{nullptr, +[](void*) {}};
139115
std::optional<int> mPID;
140116
};
141117

0 commit comments

Comments
 (0)