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

Added Windows support for MemoryWatcher #6138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,6 @@ if(UNIX)
message(STATUS "Using named pipes as controller inputs")
add_definitions(-DUSE_PIPES=1)
message(STATUS "Watching game memory for changes")
add_definitions(-DUSE_MEMORYWATCHER=1)
endif()

if(ENABLE_ANALYTICS)
Expand Down
6 changes: 0 additions & 6 deletions Source/Core/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
#include "Core/DSPEmulator.h"
#include "Core/Host.h"
#include "Core/MemTools.h"
#ifdef USE_MEMORYWATCHER
#include "Core/MemoryWatcher.h"
#endif
#include "Core/Boot/Boot.h"
#include "Core/FifoPlayer/FifoPlayer.h"
#include "Core/HLE/HLE.h"
Expand Down Expand Up @@ -289,9 +287,7 @@ void Stop() // - Hammertime!
GCAdapter::ResetRumble();
#endif

#ifdef USE_MEMORYWATCHER
MemoryWatcher::Shutdown();
#endif
}

void DeclareAsCPUThread()
Expand Down Expand Up @@ -383,9 +379,7 @@ static void CpuThread()
}
#endif

#ifdef USE_MEMORYWATCHER
MemoryWatcher::Init();
#endif

// Enter CPU run loop. When we leave it - we are done.
CPU::Run();
Expand Down
4 changes: 3 additions & 1 deletion Source/Core/Core/Core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@
<ClCompile Include="IOS\USB\Bluetooth\WiimoteHIDAttr.cpp" />
<ClCompile Include="IOS\WFS\WFSSRV.cpp" />
<ClCompile Include="IOS\WFS\WFSI.cpp" />
<ClCompile Include="MemoryWatcher.cpp" />
<ClCompile Include="MemTools.cpp" />
<ClCompile Include="Movie.cpp" />
<ClCompile Include="NetPlayClient.cpp" />
Expand Down Expand Up @@ -486,6 +487,7 @@
<ClInclude Include="IOS\WFS\WFSSRV.h" />
<ClInclude Include="IOS\WFS\WFSI.h" />
<ClInclude Include="MachineContext.h" />
<ClInclude Include="MemoryWatcher.h" />
<ClInclude Include="MemTools.h" />
<ClInclude Include="Movie.h" />
<ClInclude Include="NetPlayClient.h" />
Expand Down Expand Up @@ -586,4 +588,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
4 changes: 3 additions & 1 deletion Source/Core/Core/Core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,7 @@
<ClCompile Include="IOS\Network\NCD\WiiNetConfig.cpp">
<Filter>IOS\Network\NCD</Filter>
</ClCompile>
<ClCompile Include="MemoryWatcher.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="BootManager.h" />
Expand Down Expand Up @@ -1559,8 +1560,9 @@
<ClInclude Include="HW\WiimoteCommon\WiimoteReport.h">
<Filter>HW %28Flipper/Hollywood%29\WiimoteCommon</Filter>
</ClInclude>
<ClInclude Include="MemoryWatcher.h" />
</ItemGroup>
<ItemGroup>
<Text Include="CMakeLists.txt" />
</ItemGroup>
</Project>
</Project>
24 changes: 24 additions & 0 deletions Source/Core/Core/MemoryWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
#include <iostream>
#include <memory>
#include <sstream>
#ifdef _WIN32
#include <fileapi.h>
#include <handleapi.h>
#else
#include <unistd.h>
#endif

#include "Common/FileUtil.h"
#include "Core/CoreTiming.h"
Expand Down Expand Up @@ -53,7 +58,11 @@ MemoryWatcher::~MemoryWatcher()
return;

m_running = false;
#ifdef _WIN32
CloseHandle(m_pipe);
#else
close(m_fd);
#endif
}

bool MemoryWatcher::LoadAddresses(const std::string& path)
Expand Down Expand Up @@ -83,12 +92,19 @@ void MemoryWatcher::ParseLine(const std::string& line)

bool MemoryWatcher::OpenSocket(const std::string& path)
{
#ifdef _WIN32
m_pipe = CreateFile(L"\\\\.\\pipe\\Dolphin Emulator\\MemoryWatcher", GENERIC_READ | GENERIC_WRITE,
0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);

return m_pipe != INVALID_HANDLE_VALUE;
#else
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;
#endif
}

u32 MemoryWatcher::ChasePointer(const std::string& line)
Expand Down Expand Up @@ -122,8 +138,16 @@ void MemoryWatcher::Step()
// Update the value
current_value = new_value;
std::string message = ComposeMessage(address, new_value);
#ifdef _WIN32
unsigned long written;
if (m_pipe != INVALID_HANDLE_VALUE)
WriteFile(m_pipe, message.c_str(), static_cast<DWORD>(message.size() + 1), &written,
nullptr);
#else
sendto(m_fd, message.c_str(), message.size() + 1, 0, reinterpret_cast<sockaddr*>(&m_addr),
sizeof(m_addr));
#endif
}
}
}

8 changes: 8 additions & 0 deletions Source/Core/Core/MemoryWatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
#pragma once

#include <map>
#ifndef _WIN32
#include <sys/socket.h>
#include <sys/un.h>
#endif
#include <vector>

#include "Common/CommonTypes.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.
//
Expand Down Expand Up @@ -37,8 +41,12 @@ class MemoryWatcher final

bool m_running;

#ifdef _WIN32
void* m_pipe;
#else
int m_fd;
sockaddr_un m_addr;
#endif

// Address as stored in the file -> list of offsets to follow
std::map<std::string, std::vector<u32>> m_addresses;
Expand Down