Skip to content

feat: 适配全新的关闭游戏(光速级杀进程)#578

Open
Usagi-wusaqi wants to merge 5 commits into
OneDragon-Anything:mainfrom
Usagi-wusaqi:feat/close_game
Open

feat: 适配全新的关闭游戏(光速级杀进程)#578
Usagi-wusaqi wants to merge 5 commits into
OneDragon-Anything:mainfrom
Usagi-wusaqi:feat/close_game

Conversation

@Usagi-wusaqi

@Usagi-wusaqi Usagi-wusaqi commented Feb 18, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

更新说明

  • Bug 修复

    • 改进了在 Windows 下关闭游戏的进程终止逻辑,提升关闭可靠性、减少残留进程并改善日志反馈。
  • 代码质量改进

    • 优化了控制器相关的类型声明与导入,提升类型一致性与初始化稳定性。
    • 更新了若干文档注释与格式以提高可读性与维护性。

Copilot AI review requested due to automatic review settings February 18, 2026 04:38
@coderabbitai

coderabbitai Bot commented Feb 18, 2026

Copy link
Copy Markdown

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

在 PcControllerBase 中新增 Windows 专用的进程终止逻辑:通过 ctypes 调用 WinAPI 获取窗口与 PID,使用 OpenProcess/TerminateProcess 强制结束进程;同时更新导入与将 xbox_controller/ds4_controller 的类型注解为 Type | None。(≤50字)

Changes

Cohort / File(s) Summary
Windows 进程终止与注解更新
src/one_dragon/base/controller/pc_controller_base.py
close_game 中新增 Windows 专用流程:使用 ctypes / ctypes.wintypes 与 WinAPI(获取窗口句柄、GetWindowThreadProcessId 获取 PID、配置 OpenProcess/TerminateProcess/CloseHandle 的 argtypes/restype 并调用终止进程)。新增 ctypes, lru_cache 等导入。将 xbox_controllerds4_controller 的类型注解由 Optional[...] 改为 `Type

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: 记录日志(成功/失败)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🐰 窗边细嗅代码香,
ctypes 指路寻 PID,
OpenProcess 轻声唱,
TerminateProcess 放归光,
小兔点爪,合并忙 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题清楚地总结了主要变更:实现新的游戏关闭方式(通过Windows进程终止API),与changeset的核心改动完全相关。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/one_dragon/base/controller/pc_controller_base.py Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/one_dragon/base/controller/pc_controller_base.py Outdated
Comment thread src/one_dragon/base/controller/pc_controller_base.py Outdated
@Usagi-wusaqi
Usagi-wusaqi marked this pull request as draft February 18, 2026 04:46
@Usagi-wusaqi
Usagi-wusaqi marked this pull request as ready for review February 18, 2026 05:04

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/one_dragon/base/controller/pc_controller_base.py

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/one_dragon/base/controller/pc_controller_base.py Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 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).

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/one_dragon/base/controller/pc_controller_base.py (1)

24-31: GetWindowThreadProcessId 缺少 restype 声明

其他三个 WinAPI 函数都同时设置了 argtypesrestype,但 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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/one_dragon/base/controller/pc_controller_base.py
Comment thread src/one_dragon/base/controller/pc_controller_base.py
Comment thread src/one_dragon/base/controller/pc_controller_base.py
@Usagi-wusaqi
Usagi-wusaqi marked this pull request as draft February 20, 2026 00:16
@Usagi-wusaqi
Usagi-wusaqi marked this pull request as ready for review April 5, 2026 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants