Skip to content

Commit

Permalink
Add the --dolphin-process-name command-line option.
Browse files Browse the repository at this point in the history
Used to override the name of the Dolphin Emulator process that the DME
will search for in the system while trying to hook.

The short version of the option is `-d`.

The [undocumented] `DME_DOLPHIN_PROCESS_NAME` environment variable can
be used alternatively.

Fixes #42.
  • Loading branch information
cristian64 committed Mar 6, 2024
1 parent 4b70c36 commit d3cd18d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
9 changes: 8 additions & 1 deletion Source/DolphinProcess/Linux/LinuxDolphinProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../../Common/CommonUtils.h"
#include "../../Common/MemoryCommon.h"

#include <cstdlib>
#include <cstring>
#include <dirent.h>
#include <fstream>
Expand Down Expand Up @@ -106,6 +107,8 @@ bool LinuxDolphinProcess::findPID()
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)))
Expand All @@ -118,7 +121,11 @@ bool LinuxDolphinProcess::findPID()
std::string line;
aCmdLineFile.open("/proc/" + std::string(directoryEntry->d_name) + "/comm");
getline(aCmdLineFile, line);
if (line == "dolphin-emu" || line == "dolphin-emu-qt2" || line == "dolphin-emu-wx")

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();
Expand Down
10 changes: 8 additions & 2 deletions Source/DolphinProcess/Mac/MacDolphinProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include "../../Common/CommonUtils.h"
#include "../../Common/MemoryCommon.h"

#include <cstdlib>
#include <memory>
#include <string_view>
#include <sys/sysctl.h>
#include <mach/mach_vm.h>

Expand All @@ -22,11 +24,15 @@ bool MacDolphinProcess::findPID()
if(sysctl((int*) mib, 4, procs.get(), &procSize, NULL, 0) == -1)
return false;

static const char* const s_dolphinProcessName{std::getenv("DME_DOLPHIN_PROCESS_NAME")};

m_PID = -1;
for(int i = 0; i < procSize / sizeof(kinfo_proc); i++)
{
if(std::strcmp(procs[i].kp_proc.p_comm, "Dolphin") == 0 ||
std::strcmp(procs[i].kp_proc.p_comm, "dolphin-emu") == 0)
const std::string_view name{procs[i].kp_proc.p_comm};
const bool match{s_dolphinProcessName ? name == s_dolphinProcessName :
(name == "Dolphin" || name == "dolphin-emu")};
if (match)
{
m_PID = procs[i].kp_proc.p_pid;
}
Expand Down
23 changes: 20 additions & 3 deletions Source/DolphinProcess/Windows/WindowsDolphinProcess.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#ifdef _WIN32
#ifndef _WIN32

#include "WindowsDolphinProcess.h"
#include "../../Common/CommonUtils.h"
#include "../../Common/MemoryCommon.h"

#include <Psapi.h>
#ifdef UNICODE
#include <codecvt>
#endif
#include <cstdlib>
#ifdef UNICODE
#include <locale>
#endif
#include <string>
#include <tlhelp32.h>

Expand All @@ -16,6 +23,8 @@ bool WindowsDolphinProcess::findPID()
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);

static const char* const s_dolphinProcessName{std::getenv("DME_DOLPHIN_PROCESS_NAME")};

HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

m_PID = -1;
Expand All @@ -25,11 +34,19 @@ bool WindowsDolphinProcess::findPID()
{
#ifdef UNICODE
const std::wstring exeFile{entry.szExeFile};
if (exeFile == L"Dolphin.exe" || exeFile == L"DolphinQt2.exe" || exeFile == L"DolphinWx.exe")
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
const bool match{s_dolphinProcessName ?
exeFile == converter.from_bytes(s_dolphinProcessName) :
(exeFile == L"Dolphin.exe" || exeFile == L"DolphinQt2.exe" ||
exeFile == L"DolphinWx.exe")};
#else
const std::string exeFile{entry.szExeFile};
if (exeFile == "Dolphin.exe" || exeFile == "DolphinQt2.exe" || exeFile == "DolphinWx.exe")
const bool match{s_dolphinProcessName ?
exeFile == s_dolphinProcessName :
(exeFile == "Dolphin.exe" || exeFile == "DolphinQt2.exe" ||
exeFile == "DolphinWx.exe")};
#endif
if (match)
{
m_PID = entry.th32ProcessID;
break;
Expand Down
16 changes: 16 additions & 0 deletions Source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,24 @@ int main(int argc, char** argv)
"the Dolphin Emulator's emulated memory."));
parser.addHelpOption();
parser.addVersionOption();

const QCommandLineOption dolphinProcessNameOption(
QStringList() << "d"
<< "dolphin-process-name",
QObject::tr("Specify custom name for the Dolphin Emulator process. File extension must be "
"included too. By default, platform-specific names are used (e.g. "
"\"Dolphin.exe\" on Windows, or \"dolphin-emu\" on Linux or macOS)."),
"dolphin_process_name");
parser.addOption(dolphinProcessNameOption);

parser.process(app);

const QString dolphinProcessName{parser.value(dolphinProcessNameOption)};
if (!dolphinProcessName.isEmpty())
{
qputenv("DME_DOLPHIN_PROCESS_NAME", dolphinProcessName.toStdString().c_str());
}

MainWindow window;
window.show();
return app.exec();
Expand Down

0 comments on commit d3cd18d

Please sign in to comment.