[v0.1.1] use psutil to find pid of dolphin

This commit is contained in:
sup39 2022-06-26 11:05:26 +09:00
parent 2527142f07
commit b891a9496a
4 changed files with 21 additions and 51 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ __pycache__/
*.egg-info/ *.egg-info/
/dist/ /dist/
/build/ /build/
/.venv*/

View file

@ -1,4 +1,8 @@
# Change Log # Change Log
## \[v0.1.1] use psutil to find pid of dolphin (2022/06/26)
Use psutil to achieve cross-platform pid finding.
This also fixes the dependency issue on Linux.
## \[v0.1.0] pack into one PyPI package (2022/06/25) ## \[v0.1.0] pack into one PyPI package (2022/06/25)
Complete all basic functions, including: Complete all basic functions, including:
- find dolphin, init shared memory - find dolphin, init shared memory

View file

@ -1,14 +1,15 @@
[metadata] [metadata]
name = sup-dolphin-memory-lib name = sup-dolphin-memory-lib
version = 0.1.0 version = 0.1.1
author = sup39 author = sup39
author_email = sms@sup39.dev author_email = sms@sup39.dev
description = A Python library for reading and writing the memory of an emulated game in Dolphin description = A Python library for reading and writing the memory of an emulated game in Dolphin
long_description = file: README.md long_description = file: README.md
long_description_content_type = text/markdown long_description_content_type = text/markdown
url = https://github.com/sup39/dolphin-memory-lib url = https://github.com/sup39/sup-dolphin-memory-lib
license = MIT
project_urls = project_urls =
Bug Tracker = https://github.com/sup39/dolphin-memory-lib/issues Bug Tracker = https://github.com/sup39/sup-dolphin-memory-lib/issues
classifiers = classifiers =
Programming Language :: Python :: 3 Programming Language :: Python :: 3
License :: OSI Approved :: MIT License License :: OSI Approved :: MIT License
@ -20,6 +21,7 @@ package_dir =
packages = find: packages = find:
python_requires = >=3.8 python_requires = >=3.8
install_requires = install_requires =
psutil ~= 5.9
[options.packages.find] [options.packages.find]
where = src where = src

View file

@ -13,56 +13,19 @@ https://github.com/aldelaro5/Dolphin-memory-engine
''' '''
import os import os
from psutil import process_iter
from struct import pack, unpack, calcsize from struct import pack, unpack, calcsize
from multiprocessing.shared_memory import SharedMemory from multiprocessing.shared_memory import SharedMemory
if os.name == 'nt': dolphinProcNames = \
# windows ('Dolphin.exe', 'DolphinQt2.exe', 'DolphinWx.exe') if os.name == 'nt' \
from ctypes import Structure, POINTER, sizeof, byref, windll else ('dolphin-emu', 'dolphin-emu-qt2', 'dolphin-emu-wx')
from ctypes.wintypes import DWORD, ULONG, LONG, CHAR, MAX_PATH
kernel32 = windll.kernel32 def find_dolphin():
NULL = 0
## https://docs.microsoft.com/ja-jp/windows/win32/api/tlhelp32/ns-tlhelp32-processentry32
class PROCESSENTRY32(Structure):
_fields_ = [
('dwSize', DWORD),
('cntUsage', DWORD),
('th32ProcessID', DWORD),
('th32DefaultHeapID', POINTER(ULONG)),
('th32ModuleID', DWORD),
('cntThreads', DWORD),
('th32ParentProcessID', DWORD),
('pcPriClassBase', LONG),
('dwFlags', DWORD),
('szExeFile', CHAR*MAX_PATH),
]
## https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-createtoolhelp32snapshot
TH32CS_SNAPPROCESS = 2
## find pids of dolphin
def find_dolphin():
# prepare entry struct
entry = PROCESSENTRY32()
entry.dwSize = sizeof(PROCESSENTRY32)
# prepare snapshot
snapshot = kernel32.CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL)
# find pids
pids = []
if kernel32.Process32First(snapshot, byref(entry)):
while True:
if entry.szExeFile in (b'Dolphin.exe', b'DolphinQt2.exe', b'DolphinWx.exe'):
pids.append(entry.th32ProcessID)
if not kernel32.Process32Next(snapshot, byref(entry)): break
kernel32.CloseHandle(snapshot);
# done
return pids
else:
# UNIX
import psutil
def find_dolphin():
return [ return [
proc.pid proc.pid
for proc in psutil.process_iter() for proc in process_iter()
if proc.name() in ('dolphin-emu', 'dolphin-emu-qt2', 'dolphin-emu-wx') if proc.name() in dolphinProcNames
] ]
''' '''