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

Use Shared Memory Access Method on Linux #2

Open
wants to merge 2 commits into
base: main
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
105 changes: 105 additions & 0 deletions memorylib_lin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import ctypes
import struct
import os
import sys
from struct import pack, unpack
from subprocess import check_output
from multiprocessing import shared_memory

class Dolphin(object):
def __init__(self):
self.pid = -1
self.memory = None

def reset(self):
self.pid = -1
self.memory = None

def find_dolphin(self):
try:
if check_output(["pidof", "dolphin-emu"]) != '\n':
self.pid = int(check_output(["pidof", "dolphin-emu"]))
if check_output(["pidof", "dolphin-emu-qt2"]) != '\n':
self.pid = int(check_output(["pidof", "dolphin-emu-qt2"]))
if check_output(["pidof", "dolphin-emu-wx"]) != '\n':
self.pid = int(check_output(["pidof", "dolphin-emu-wx"]))
except Exception: #subprocess.CalledProcessError
# Do nothing because self.pid cant be modified until a successful run of pidof
pass

if self.pid == -1:
return False

return True

def init_shared_memory(self):
print("Waiting for shared memory...")
while True:
try:
self.memory = shared_memory.SharedMemory('dolphin-emu.'+str(self.pid))
return True
except FileNotFoundError:
pass

def read_ram(self, offset, size):
return self.memory.buf[offset:offset+size]

def write_ram(self, offset, data):
self.memory.buf[offset:offset+len(data)] = data

def read_uint32(self, addr):
assert addr >= 0x80000000
value = self.read_ram(addr-0x80000000, 4)

return struct.unpack(">I", value)[0]

def write_uint32(self, addr, val):
assert addr >= 0x80000000
return self.write_ram(addr - 0x80000000, pack(">I", val))

def read_float(self, addr):
assert addr >= 0x80000000
value = self.read_ram(addr - 0x80000000, 4)

return struct.unpack(">f", value)[0]

def write_float(self, addr, val):
assert addr >= 0x80000000
return self.write_ram(addr - 0x80000000, struct.pack(">f", val))


if __name__ == "__main__":
dolphin = Dolphin()
import multiprocessing

if dolphin.find_dolphin():

print("Found Dolphin! ")
else:
print("Didn't find Dolphin")

print(dolphin.pid)

if dolphin.init_shared_memory():
print("We found MEM1 and/or MEM2!")
else:
print("We didn't find it...")

import random
randint = random.randint
from timeit import default_timer

start = default_timer()

print("Testing Shared Memory Method")
start = default_timer()
count = 500000
for i in range(count):
value = randint(0, 2**32-1)
dolphin.write_uint32(0x80000000, value)

result = dolphin.read_uint32(0x80000000)
assert result == value
diff = default_timer()-start
print(count/diff, "per sec")
print("time: ", diff)
187 changes: 0 additions & 187 deletions memtest_lin.py

This file was deleted.

4 changes: 2 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
echo "dolphin-memory-lib requires sudo permission to read and write to the emulator process memory."
sudo python memtest_lin.py
echo "dolphin-memory-lib requires you to run the library before you start a game on the emulator."
python memorylib_lin.py
read -n1 -r