From b891a9496a2cea2c4b21d2a21700dc6bcc35826e Mon Sep 17 00:00:00 2001 From: sup39 Date: Sun, 26 Jun 2022 11:05:26 +0900 Subject: [PATCH] [v0.1.1] use psutil to find pid of dolphin --- .gitignore | 1 + CHANGELOG.md | 4 +++ setup.cfg | 8 ++++-- src/dolphin/memorylib.py | 59 ++++++++-------------------------------- 4 files changed, 21 insertions(+), 51 deletions(-) diff --git a/.gitignore b/.gitignore index 5f32ca2..87f0235 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__/ *.egg-info/ /dist/ /build/ +/.venv*/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 630c651..3ccff34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # 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) Complete all basic functions, including: - find dolphin, init shared memory diff --git a/setup.cfg b/setup.cfg index 7829559..c55e3b7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,14 +1,15 @@ [metadata] name = sup-dolphin-memory-lib -version = 0.1.0 +version = 0.1.1 author = sup39 author_email = sms@sup39.dev description = A Python library for reading and writing the memory of an emulated game in Dolphin long_description = file: README.md 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 = - Bug Tracker = https://github.com/sup39/dolphin-memory-lib/issues + Bug Tracker = https://github.com/sup39/sup-dolphin-memory-lib/issues classifiers = Programming Language :: Python :: 3 License :: OSI Approved :: MIT License @@ -20,6 +21,7 @@ package_dir = packages = find: python_requires = >=3.8 install_requires = + psutil ~= 5.9 [options.packages.find] where = src diff --git a/src/dolphin/memorylib.py b/src/dolphin/memorylib.py index 5d194fa..1fa36fe 100644 --- a/src/dolphin/memorylib.py +++ b/src/dolphin/memorylib.py @@ -13,57 +13,20 @@ https://github.com/aldelaro5/Dolphin-memory-engine ''' import os +from psutil import process_iter from struct import pack, unpack, calcsize from multiprocessing.shared_memory import SharedMemory -if os.name == 'nt': - # windows - from ctypes import Structure, POINTER, sizeof, byref, windll - from ctypes.wintypes import DWORD, ULONG, LONG, CHAR, MAX_PATH - kernel32 = windll.kernel32 - 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 [ - proc.pid - for proc in psutil.process_iter() - if proc.name() in ('dolphin-emu', 'dolphin-emu-qt2', 'dolphin-emu-wx') - ] +dolphinProcNames = \ + ('Dolphin.exe', 'DolphinQt2.exe', 'DolphinWx.exe') if os.name == 'nt' \ + else ('dolphin-emu', 'dolphin-emu-qt2', 'dolphin-emu-wx') + +def find_dolphin(): + return [ + proc.pid + for proc in process_iter() + if proc.name() in dolphinProcNames + ] ''' @typedef {(int|str) | [(int|str), ...int[]]} Addr