forked from aldelaro5/dolphin-memory-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinuxDolphinProcess.cpp
289 lines (254 loc) · 7.16 KB
/
LinuxDolphinProcess.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifdef __linux__
#include "LinuxDolphinProcess.h"
#include "../../Common/CommonUtils.h"
#include "../../Common/MemoryCommon.h"
#include <cstdlib>
#include <cstring>
#include <dirent.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <sys/uio.h>
#include <vector>
namespace DolphinComm
{
bool LinuxDolphinProcess::obtainEmuRAMInformations()
{
std::ifstream theMapsFile("/proc/" + std::to_string(m_PID) + "/maps");
std::string line;
bool MEM1Found = false;
while (getline(theMapsFile, line))
{
std::vector<std::string> lineData;
std::string token;
std::stringstream ss(line);
while (getline(ss, token, ' '))
{
if (!token.empty())
lineData.push_back(token);
}
if (lineData.size() < 3)
continue;
bool foundDevShmDolphin = false;
for (const std::string& str : lineData)
{
if (str.substr(0, 19) == "/dev/shm/dolphinmem" || str.substr(0, 20) == "/dev/shm/dolphin-emu")
{
foundDevShmDolphin = true;
break;
}
}
if (!foundDevShmDolphin)
continue;
std::string offsetStr("0x" + lineData[2]);
const u32 offset{static_cast<u32>(std::stoul(offsetStr, nullptr, 16))};
if (offset != 0 && offset != Common::GetMEM1Size() + 0x40000)
continue;
u64 firstAddress = 0;
u64 SecondAddress = 0;
const std::size_t indexDash{lineData[0].find('-')};
std::string firstAddressStr("0x" + lineData[0].substr(0, indexDash));
std::string secondAddressStr("0x" + lineData[0].substr(indexDash + 1));
firstAddress = std::stoul(firstAddressStr, nullptr, 16);
SecondAddress = std::stoul(secondAddressStr, nullptr, 16);
if (SecondAddress - firstAddress == Common::GetMEM2Size() &&
offset == Common::GetMEM1Size() + 0x40000)
{
m_MEM2AddressStart = firstAddress;
m_MEM2Present = true;
if (MEM1Found)
break;
}
if (SecondAddress - firstAddress == Common::GetMEM1Size())
{
if (offset == 0x0)
{
m_emuRAMAddressStart = firstAddress;
MEM1Found = true;
}
else if (offset == Common::GetMEM1Size() + 0x40000)
{
m_emuARAMAdressStart = firstAddress;
m_ARAMAccessible = true;
}
}
}
// On Wii, there is no concept of speedhack so act as if we couldn't find it
if (m_MEM2Present)
{
m_emuARAMAdressStart = 0;
m_ARAMAccessible = false;
}
if (m_emuRAMAddressStart != 0)
return true;
// Here, Dolphin appears to be running, but the emulator isn't started
return false;
}
bool LinuxDolphinProcess::findPID()
{
DIR* directoryPointer = opendir("/proc/");
if (directoryPointer == nullptr)
return false;
static const char* const s_dolphinProcessName{std::getenv("DME_DOLPHIN_PROCESS_NAME")};
m_PID = -1;
struct dirent* directoryEntry = nullptr;
while (m_PID == -1 && (directoryEntry = readdir(directoryPointer)))
{
const char* const name{static_cast<const char*>(directoryEntry->d_name)};
std::istringstream conversionStream(name);
int aPID = 0;
if (!(conversionStream >> aPID))
continue;
std::ifstream aCmdLineFile;
std::string line;
aCmdLineFile.open("/proc/" + std::string(name) + "/comm");
getline(aCmdLineFile, line);
const bool match{s_dolphinProcessName ? line == s_dolphinProcessName :
(line == "dolphin-emu" || line == "dolphin-emu-qt2" ||
line == "dolphin-emu-wx")};
if (match)
m_PID = aPID;
aCmdLineFile.close();
}
closedir(directoryPointer);
const bool running{m_PID != -1};
return running;
}
bool LinuxDolphinProcess::readFromRAM(const u32 offset, char* buffer, const size_t size,
const bool withBSwap)
{
u64 RAMAddress = 0;
if (m_ARAMAccessible)
{
if (offset >= Common::ARAM_FAKESIZE)
RAMAddress = m_emuRAMAddressStart + offset - Common::ARAM_FAKESIZE;
else
RAMAddress = m_emuARAMAdressStart + offset;
}
else if (offset >= (Common::MEM2_START - Common::MEM1_START))
{
RAMAddress = m_MEM2AddressStart + offset - (Common::MEM2_START - Common::MEM1_START);
}
else
{
RAMAddress = m_emuRAMAddressStart + offset;
}
iovec local{};
local.iov_base = buffer;
local.iov_len = size;
iovec remote{};
remote.iov_base = reinterpret_cast<void*>(RAMAddress);
remote.iov_len = size;
const ssize_t nread{process_vm_readv(m_PID, &local, 1, &remote, 1, 0)};
if (nread == -1)
{
// A more specific error type should be available in `errno` (if ever interested).
return false;
}
if (static_cast<size_t>(nread) != size)
return false;
if (withBSwap)
{
switch (size)
{
case 2:
{
u16 halfword = 0;
std::memcpy(&halfword, buffer, sizeof(u16));
halfword = Common::bSwap16(halfword);
std::memcpy(buffer, &halfword, sizeof(u16));
break;
}
case 4:
{
u32 word = 0;
std::memcpy(&word, buffer, sizeof(u32));
word = Common::bSwap32(word);
std::memcpy(buffer, &word, sizeof(u32));
break;
}
case 8:
{
u64 doubleword = 0;
std::memcpy(&doubleword, buffer, sizeof(u64));
doubleword = Common::bSwap64(doubleword);
std::memcpy(buffer, &doubleword, sizeof(u64));
break;
}
default:
break;
}
}
return true;
}
bool LinuxDolphinProcess::writeToRAM(const u32 offset, const char* buffer, const size_t size,
const bool withBSwap)
{
u64 RAMAddress = 0;
if (m_ARAMAccessible)
{
if (offset >= Common::ARAM_FAKESIZE)
RAMAddress = m_emuRAMAddressStart + offset - Common::ARAM_FAKESIZE;
else
RAMAddress = m_emuARAMAdressStart + offset;
}
else if (offset >= (Common::MEM2_START - Common::MEM1_START))
{
RAMAddress = m_MEM2AddressStart + offset - (Common::MEM2_START - Common::MEM1_START);
}
else
{
RAMAddress = m_emuRAMAddressStart + offset;
}
char* bufferCopy = new char[size];
std::memcpy(bufferCopy, buffer, size);
iovec local{};
local.iov_base = bufferCopy;
local.iov_len = size;
iovec remote{};
remote.iov_base = reinterpret_cast<void*>(RAMAddress);
remote.iov_len = size;
if (withBSwap)
{
switch (size)
{
case 2:
{
u16 halfword = 0;
std::memcpy(&halfword, bufferCopy, sizeof(u16));
halfword = Common::bSwap16(halfword);
std::memcpy(bufferCopy, &halfword, sizeof(u16));
break;
}
case 4:
{
u32 word = 0;
std::memcpy(&word, bufferCopy, sizeof(u32));
word = Common::bSwap32(word);
std::memcpy(bufferCopy, &word, sizeof(u32));
break;
}
case 8:
{
u64 doubleword = 0;
std::memcpy(&doubleword, bufferCopy, sizeof(u64));
doubleword = Common::bSwap64(doubleword);
std::memcpy(bufferCopy, &doubleword, sizeof(u64));
break;
}
default:
break;
}
}
const ssize_t nwrote{process_vm_writev(m_PID, &local, 1, &remote, 1, 0)};
delete[] bufferCopy;
if (nwrote == -1)
{
// A more specific error type should be available in `errno` (if ever interested).
return false;
}
return static_cast<size_t>(nwrote) == size;
}
} // namespace DolphinComm
#endif