Add support for sending game memory changes to outside processes

This commit is contained in:
spxtr committed 2015-12-29 17:59:16 -08:00
1 parent 29ec5b065a
commit 2f50560602
8 files changed
+157

No files matched your search

+4
View File
@@ -273,4 +273,8 @@ if(GDBSTUB)
set(SRCS ${SRCS} PowerPC/GDBStub.cpp)
endif(GDBSTUB)
if(UNIX)
set(SRCS ${SRCS} MemoryWatcher.cpp)
endif(UNIX)
add_dolphin_library(core "${SRCS}" "${LIBS}")
+15
View File
@@ -28,6 +28,9 @@
#include "Core/DSPEmulator.h"
#include "Core/Host.h"
#include "Core/MemTools.h"
#ifdef USE_MEMORYWATCHER
#include "Core/MemoryWatcher.h"
#endif
#include "Core/Movie.h"
#include "Core/NetPlayClient.h"
#include "Core/NetPlayProto.h"
@@ -109,6 +112,10 @@ static bool s_request_refresh_info = false;
static int s_pause_and_lock_depth = 0;
static bool s_is_framelimiter_temp_disabled = false;
#ifdef USE_MEMORYWATCHER
static std::unique_ptr<MemoryWatcher> s_memory_watcher;
#endif
#ifdef ThreadLocalStorage
static ThreadLocalStorage bool tls_is_cpu_thread = false;
#else
@@ -280,6 +287,10 @@ void Stop() // - Hammertime!
#if defined(__LIBUSB__) || defined(_WIN32)
SI_GCAdapter::ResetRumble();
#endif
#ifdef USE_MEMORYWATCHER
s_memory_watcher.reset();
#endif
}
void DeclareAsCPUThread()
@@ -349,6 +360,10 @@ static void CpuThread()
}
#endif
#ifdef USE_MEMORYWATCHER
s_memory_watcher = std::make_unique<MemoryWatcher>();
#endif
// Enter CPU run loop. When we leave it - we are done.
CPU::Run();
+85
View File
@@ -0,0 +1,85 @@
// Copyright 2015 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <fstream>
#include <iostream>
#include "Common/FileUtil.h"
#include "Common/Thread.h"
#include "Core/MemoryWatcher.h"
#include "Core/HW/Memmap.h"
// We don't want to kill the cpu, so sleep for this long after polling.
static const int SLEEP_DURATION = 10; // ms
MemoryWatcher::MemoryWatcher()
{
if (!LoadAddresses(File::GetUserPath(F_MEMORYWATCHERLOCATIONS_IDX)))
return;
if (!OpenSocket(File::GetUserPath(F_MEMORYWATCHERSOCKET_IDX)))
return;
m_running = true;
m_watcher_thread = std::thread(&MemoryWatcher::WatcherThread, this);
}
MemoryWatcher::~MemoryWatcher()
{
if (!m_running)
return;
m_running = false;
m_watcher_thread.join();
close(m_fd);
}
bool MemoryWatcher::LoadAddresses(const std::string& path)
{
std::ifstream locations(path);
if (!locations)
return false;
u32 data;
locations >> std::hex;
while (locations >> data)
m_values[data] = 0;
return m_values.size() > 0;
}
bool MemoryWatcher::OpenSocket(const std::string& path)
{
memset(&m_addr, 0, sizeof(m_addr));
m_addr.sun_family = AF_UNIX;
strncpy(m_addr.sun_path, path.c_str(), sizeof(m_addr.sun_path) - 1);
m_fd = socket(AF_UNIX, SOCK_DGRAM, 0);
return m_fd >= 0;
}
void MemoryWatcher::WatcherThread()
{
while (m_running)
{
for (auto& entry : m_values)
{
u32 address = entry.first;
u32& current_value = entry.second;
u32 new_value = Memory::Read_U32(address);
if (new_value != current_value)
{
current_value = new_value;
u32 buf[2] = {address, current_value};
sendto(
m_fd,
static_cast<void*>(buf),
sizeof(buf),
0,
reinterpret_cast<sockaddr*>(&m_addr),
sizeof(m_addr));
}
}
Common::SleepCurrentThread(SLEEP_DURATION);
}
}
+39
View File
@@ -0,0 +1,39 @@
// Copyright 2015 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <atomic>
#include <map>
#include <thread>
#include <sys/socket.h>
#include <sys/un.h>
// MemoryWatcher reads a file containing in-game memory addresses and outputs
// changes to those memory addresses to a unix domain socket as the game runs.
//
// The input file is a newline-separated list of hex memory addresses
// (without the "0x"). The output to the socket is two words long, the first
// containing the address, and the second containing the data as stored in
// game memory.
class MemoryWatcher final
{
public:
MemoryWatcher();
~MemoryWatcher();
private:
bool LoadAddresses(const std::string& path);
bool OpenSocket(const std::string& path);
void WatcherThread();
std::thread m_watcher_thread;
std::atomic_bool m_running;
int m_fd;
sockaddr_un m_addr;
// Address -> last value
std::map<u32, u32> m_values;
};