feat: 适配全新的关闭游戏(光速级杀进程)#578
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
Walkthrough在 PcControllerBase 中新增 Windows 专用的进程终止逻辑:通过 ctypes 调用 WinAPI 获取窗口与 PID,使用 OpenProcess/TerminateProcess 强制结束进程;同时更新导入与将 Changes
Sequence Diagram(s)sequenceDiagram
participant Controller as PcControllerBase
participant User32 as User32.dll
participant Kernel32 as Kernel32.dll
Controller->>User32: 查找/获取游戏窗口句柄 (hwnd)
User32-->>Controller: 返回 hwnd
Controller->>User32: GetWindowThreadProcessId(hwnd)
User32-->>Controller: 返回 PID
Controller->>Kernel32: OpenProcess(PROCESS_TERMINATE, PID)
Kernel32-->>Controller: 返回 process HANDLE
Controller->>Kernel32: TerminateProcess(HANDLE, exitCode)
Kernel32-->>Controller: 返回 成功/失败
Controller->>Kernel32: CloseHandle(HANDLE)
Controller-->>Controller: 记录日志(成功/失败)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 诗
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
该 PR 将 PC 端“关闭游戏”的实现从关闭窗口改为通过窗口句柄获取进程 ID 并直接终止进程,以适配“光速级杀进程”的关闭方式。
Changes:
close_game()改为GetWindowThreadProcessId+OpenProcess+TerminateProcess强制结束进程- 调整部分 import 顺序,并将控制器可选类型标注改为
X | None
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/one_dragon/base/controller/pc_controller_base.py`:
- Around line 188-190: In the method in
src/one_dragon/base/controller/pc_controller_base.py that checks for hwnd,
change the log call that currently uses log.info('未找到游戏窗口') to
log.warning('未找到游戏窗口') so the "未找到游戏窗口" failure path uses the same warning level
as the other failure branches (the ones checking hwnd-related failures at the
other branches); keep the message text unchanged and only adjust the logging
level.
- Around line 198-205: The OpenProcess call currently risks handle truncation on
64-bit Windows because ctypes defaults to 32-bit int return types; update the
Windows API declarations before calling them by setting
ctypes.windll.kernel32.OpenProcess.restype to wintypes.HANDLE and its argtypes
to (wintypes.DWORD, wintypes.BOOL, wintypes.DWORD), set
ctypes.windll.kernel32.TerminateProcess.restype to wintypes.BOOL and argtypes to
(wintypes.HANDLE, wintypes.UINT), and set
ctypes.windll.kernel32.CloseHandle.restype to wintypes.BOOL and argtypes to
(wintypes.HANDLE,) so that the handle returned by OpenProcess is a full 64-bit
HANDLE and TerminateProcess/CloseHandle receive the correct types when invoked
from pc_controller_base.py.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/one_dragon/base/controller/pc_controller_base.py`:
- Around line 24-25: 模块顶层直接访问 ctypes.windll 会在非 Windows 平台导入时抛出
AttributeError;将对 OpenProcess/TerminateProcess/CloseHandle 的类型声明和
restype/argtypes 设置移入 close_game 方法内,并在方法开头加平台守护(检查
sys.platform.startswith("win") 或 os.name == "nt"),然后在 close_game 中声明
PROCESS_TERMINATE = 0x0001、为 ctypes.windll.kernel32.OpenProcess 设置
restype/argtypes,为 TerminateProcess 和 CloseHandle 设置 restype/argtypes(使用
ctypes.wintypes.HANDLE、ctypes.wintypes.DWORD、ctypes.c_int 等合适类型),并在非 Windows
平台直接返回或抛出明确错误以避免导入失败。
---
Duplicate comments:
In `@src/one_dragon/base/controller/pc_controller_base.py`:
- Around line 190-192: 在 pc_controller_base 的窗口查找分支中,当前针对未找到游戏窗口的分支使用了
log.info('未找到游戏窗口'),应与其他失败分支保持一致改为 log.warning; locate 到检查变量 hwnd 的分支(if not
hwnd: ... return)并把该 log.info 替换为 log.warning,保持日志级别一致并保留原有消息文本和早期的 return 行为。
- Around line 200-207: OpenProcess, TerminateProcess and CloseHandle lack proper
ctypes restype/argtypes in the method, so handles are returned/truncated as
32-bit ints on x64 and TerminateProcess/CloseHandle receive invalid handles;
inside the method where
PROCESS_TERMINATE/OpenProcess/TerminateProcess/CloseHandle are used (the block
with pid and handle), declare OpenProcess.restype = wintypes.HANDLE and
OpenProcess.argtypes = (wintypes.DWORD, wintypes.BOOL, wintypes.DWORD) and
declare TerminateProcess.restype = wintypes.BOOL and TerminateProcess.argtypes =
(wintypes.HANDLE, wintypes.UINT) and CloseHandle.restype = wintypes.BOOL and
CloseHandle.argtypes = (wintypes.HANDLE,) before calling them so the
HANDLE/arguments are passed and returned correctly on 64-bit Windows.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/one_dragon/base/controller/pc_controller_base.py`:
- Line 196: GetWindowThreadProcessId is being called without setting ctypes
argtypes/restype which causes HWND to be treated as 32-bit int and can truncate
on x64; set GetWindowThreadProcessId.argtypes = [ctypes.wintypes.HWND,
ctypes.POINTER(ctypes.wintypes.DWORD)] and GetWindowThreadProcessId.restype =
ctypes.wintypes.DWORD (and import ctypes.wintypes) so the hwnd and pid pointer
are passed with the correct 64-bit pointer and DWORD types before calling
ctypes.windll.user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid)).
---
Duplicate comments:
In `@src/one_dragon/base/controller/pc_controller_base.py`:
- Around line 190-192: In the method in
src/one_dragon/base/controller/pc_controller_base.py that checks the window
handle (the branch using the local variable hwnd), change the logging call from
log.info('未找到游戏窗口') to log.warning(...) so it matches the other failure branches
(the warning calls around the hwnd check and the branches at the other failure
points). Locate the conditional that returns when not hwnd and replace the
info-level log with a warning-level log using the same message text to keep
consistency across the method.
- Around line 24-25: The module currently only sets
ctypes.windll.kernel32.OpenProcess.restype at import time, which leaks
process-global descriptor mutations and still omits argtypes for OpenProcess,
TerminateProcess and CloseHandle; move all ctypes type declarations into the
close_game method and use local references (e.g., kernel32 =
ctypes.windll.kernel32) and set both restype and argtypes for OpenProcess,
TerminateProcess and CloseHandle inside close_game (before using
PROCESS_TERMINATE) so you don't mutate module-level descriptors and the
functions have correct signatures.
- Around line 204-212: OpenProcess returns a 64-bit wintypes.HANDLE but
TerminateProcess and CloseHandle lack ctypes argtypes/restype so the HANDLE gets
truncated; inside the close_game method set proper signatures for all three APIs
(e.g., ensure OpenProcess.restype is wintypes.HANDLE and set its argtypes if not
already, then set TerminateProcess.argtypes=(wintypes.HANDLE, wintypes.UINT) and
TerminateProcess.restype=wintypes.BOOL, and set
CloseHandle.argtypes=(wintypes.HANDLE,) and CloseHandle.restype=wintypes.BOOL)
before calling OpenProcess/TerminateProcess/CloseHandle so 64-bit HANDLEs are
passed correctly and return values are reliable.
There was a problem hiding this comment.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@src/one_dragon/base/controller/pc_controller_base.py`:
- Around line 24-25: OpenProcess currently has restype set but argtypes missing;
set ctypes.windll.kernel32.OpenProcess.argtypes to [ctypes.wintypes.DWORD,
ctypes.wintypes.BOOL, ctypes.wintypes.DWORD] so ctypes will validate the
parameters (OpenProcess signature: dwDesiredAccess, bInheritHandle,
dwProcessId). Update the symbol ctypes.windll.kernel32.OpenProcess.argtypes in
pc_controller_base.py accordingly.
- Line 192: 将该分支中使用的 log.info('未找到游戏窗口') 改为 log.warning('未找到游戏窗口')
以与同一函数中其它失败分支(使用 log.warning 的分支)保持一致;在 pc_controller_base.py 中定位该 log
调用(在包含“未找到游戏窗口”的函数/方法中)并直接替换为 log.warning,保留原有消息文本和上下文变量不变。
- Around line 212-213: The calls to TerminateProcess and CloseHandle are missing
ctypes argtypes/restype so 64-bit HANDLEs returned by OpenProcess get truncated;
add proper type declarations at module scope (near other ctypes setup) for
kernel32.TerminateProcess and kernel32.CloseHandle and ensure wintypes is
imported—set TerminateProcess.argtypes to (wintypes.HANDLE, wintypes.UINT) and
restype to wintypes.BOOL, and set CloseHandle.argtypes to (wintypes.HANDLE,) and
restype to wintypes.BOOL so the HANDLE won’t be truncated when calling
ctypes.windll.kernel32.TerminateProcess and ctypes.windll.kernel32.CloseHandle
(refer to OpenProcess, TerminateProcess, CloseHandle symbols).
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/one_dragon/base/controller/pc_controller_base.py (1)
24-31:GetWindowThreadProcessId缺少restype声明其他三个 WinAPI 函数都同时设置了
argtypes和restype,但GetWindowThreadProcessId只设置了argtypes。虽然该函数返回DWORD(32 位),默认的c_int不会导致截断问题,但为了一致性和正确性建议补上。建议修复
ctypes.windll.user32.GetWindowThreadProcessId.argtypes = [ctypes.wintypes.HWND, ctypes.POINTER(ctypes.wintypes.DWORD)] +ctypes.windll.user32.GetWindowThreadProcessId.restype = ctypes.wintypes.DWORD🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/one_dragon/base/controller/pc_controller_base.py` around lines 24 - 31, GetWindowThreadProcessId has argtypes set but no restype; add restype = ctypes.wintypes.DWORD for consistency with the other WinAPI bindings (OpenProcess, TerminateProcess, CloseHandle) to ensure the function return type is correctly declared; update the binding for GetWindowThreadProcessId in pc_controller_base.py to set its restype to ctypes.wintypes.DWORD.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@src/one_dragon/base/controller/pc_controller_base.py`:
- Around line 196-198: Change the inconsistent log level at the HWND existence
check: replace the log.info('未找到游戏窗口') call for the hwnd falsy branch with
log.warning('未找到游戏窗口') so it matches the other failure branches (the hwnd check
in the same method) and preserves consistent warning-level reporting for error
conditions.
---
Nitpick comments:
In `@src/one_dragon/base/controller/pc_controller_base.py`:
- Around line 24-31: GetWindowThreadProcessId has argtypes set but no restype;
add restype = ctypes.wintypes.DWORD for consistency with the other WinAPI
bindings (OpenProcess, TerminateProcess, CloseHandle) to ensure the function
return type is correctly declared; update the binding for
GetWindowThreadProcessId in pc_controller_base.py to set its restype to
ctypes.wintypes.DWORD.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
3dc1853 to
a6a82cc
Compare
Summary by CodeRabbit
更新说明
Bug 修复
代码质量改进