-
Notifications
You must be signed in to change notification settings - Fork 471
feat: 新增 ScreenshotTargetExpand 截图缩放模式 (面向 Unity 游戏的模板匹配场景) #1336
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,8 @@ bool ControllerAgent::set_option(MaaCtrlOption key, MaaOptionValue value, MaaOpt | |
| return set_image_target_long_side(value, val_size); | ||
| case MaaCtrlOption_ScreenshotTargetShortSide: | ||
| return set_image_target_short_side(value, val_size); | ||
| case MaaCtrlOption_ScreenshotTargetExpand: | ||
| return set_image_target_expand(value, val_size); | ||
| case MaaCtrlOption_ScreenshotUseRawSize: | ||
| return set_image_use_raw_size(value, val_size); | ||
| case MaaCtrlOption_MouseLockFollow: | ||
|
|
@@ -1097,6 +1099,16 @@ bool ControllerAgent::calc_target_image_size() | |
| return true; | ||
| } | ||
|
|
||
| if (image_target_expand_width_ > 0 && image_target_expand_height_ > 0) { | ||
| double sx = static_cast<double>(image_target_expand_width_) / image_raw_width_; | ||
| double sy = static_cast<double>(image_target_expand_height_) / image_raw_height_; | ||
| double scale = std::max(sx, sy); | ||
|
Comment on lines
+1102
to
+1105
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: 在这里使用 std::round 可能会破坏“尺寸 ≥ 参考尺寸”的保证;在 expand 模式下建议改用 ceil。 当 建议实现如下: if (image_target_expand_width_ > 0 && image_target_expand_height_ > 0) {
double sx = static_cast<double>(image_target_expand_width_) / image_raw_width_;
double sy = static_cast<double>(image_target_expand_height_) / image_raw_height_;
double scale = std::max(sx, sy);
image_target_width_ = static_cast<int>(std::ceil(image_raw_width_ * scale));
image_target_height_ = static_cast<int>(std::ceil(image_raw_height_ * scale));
LogInfo << "expand" << VAR(scale) << VAR(image_target_width_) << VAR(image_target_height_);
return true;
}
如果 Original comment in Englishsuggestion: Using std::round here can violate the ">= reference size" guarantee; consider ceil for expand mode. With Suggested implementation: if (image_target_expand_width_ > 0 && image_target_expand_height_ > 0) {
double sx = static_cast<double>(image_target_expand_width_) / image_raw_width_;
double sy = static_cast<double>(image_target_expand_height_) / image_raw_height_;
double scale = std::max(sx, sy);
image_target_width_ = static_cast<int>(std::ceil(image_raw_width_ * scale));
image_target_height_ = static_cast<int>(std::ceil(image_raw_height_ * scale));
LogInfo << "expand" << VAR(scale) << VAR(image_target_width_) << VAR(image_target_height_);
return true;
}
If ✅ Addressed in 4f27e6a: The expand branch now uses std::ceil for both dimensions and includes a comment explaining the reasoning, ensuring the scaled size always meets the “>= reference” guarantee as suggested.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :(差点被带偏了 |
||
| image_target_width_ = static_cast<int>(std::round(image_raw_width_ * scale)); | ||
| image_target_height_ = static_cast<int>(std::round(image_raw_height_ * scale)); | ||
|
Comment on lines
+1106
to
+1107
|
||
| LogInfo << "expand" << VAR(scale) << VAR(image_target_width_) << VAR(image_target_height_); | ||
| return true; | ||
| } | ||
|
|
||
| if (image_target_long_side_ == 0 && image_target_short_side_ == 0) { | ||
| LogError << "Invalid image target size"; | ||
| return false; | ||
|
|
@@ -1168,6 +1180,8 @@ bool ControllerAgent::set_image_target_long_side(MaaOptionValue value, MaaOption | |
| } | ||
| image_target_long_side_ = *reinterpret_cast<const int32_t*>(value); | ||
| image_target_short_side_ = 0; | ||
| image_target_expand_width_ = 0; | ||
| image_target_expand_height_ = 0; | ||
|
|
||
| clear_target_image_size(); | ||
|
|
||
|
|
@@ -1185,13 +1199,39 @@ bool ControllerAgent::set_image_target_short_side(MaaOptionValue value, MaaOptio | |
| } | ||
| image_target_long_side_ = 0; | ||
| image_target_short_side_ = *reinterpret_cast<const int32_t*>(value); | ||
| image_target_expand_width_ = 0; | ||
| image_target_expand_height_ = 0; | ||
|
|
||
| clear_target_image_size(); | ||
|
|
||
| LogInfo << "image_target_height_ = " << image_target_short_side_; | ||
| return true; | ||
| } | ||
|
|
||
| bool ControllerAgent::set_image_target_expand(MaaOptionValue value, MaaOptionValueSize val_size) | ||
| { | ||
| LogDebug; | ||
|
|
||
| if (val_size != sizeof(int32_t) * 2) { | ||
| LogError << "invalid value size: " << val_size; | ||
| return false; | ||
| } | ||
| auto* arr = reinterpret_cast<const int32_t*>(value); | ||
| if (arr[0] <= 0 || arr[1] <= 0) { | ||
| LogError << "invalid expand size" << VAR(arr[0]) << VAR(arr[1]); | ||
| return false; | ||
| } | ||
| image_target_expand_width_ = arr[0]; | ||
| image_target_expand_height_ = arr[1]; | ||
| image_target_long_side_ = 0; | ||
| image_target_short_side_ = 0; | ||
|
|
||
| clear_target_image_size(); | ||
|
|
||
| LogInfo << VAR(image_target_expand_width_) << VAR(image_target_expand_height_); | ||
| return true; | ||
| } | ||
|
|
||
| bool ControllerAgent::set_image_use_raw_size(MaaOptionValue value, MaaOptionValueSize val_size) | ||
| { | ||
| LogDebug; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (bug_risk): 当 expand 维度传入非整数数值时,行为可能会让人意外。
is_number()会接受浮点值,但代码后面使用的是as_integer()并转换为int32_t,这可能会产生静默截断。如果只接受整数尺寸,建议改为更严格的检查(例如使用is_integer()(若可用),或者显式验证数值在int32_t范围内且没有小数部分),以避免令人意外的行为。建议实现如下:
如果当前翻译单元中还未引入
std::numeric_limits,请在source/MaaAgentClient/Client/AgentClient.cpp顶部附近添加#include <limits>。如果你使用的 JSON/值类型不提供
is_integer(),请用等价的检查替代(例如is_int64(),或者结合is_number()与小数部分检查),并保持与所用 JSON 库 API 一致。Original comment in English
suggestion (bug_risk): Potentially surprising behavior when a non-integer number is passed for expand dimensions.
is_number()allows floating-point values, but the code then usesas_integer()and casts toint32_t, which may silently truncate. If only integral dimensions are valid, consider a stricter check (e.g.,is_integer()if available, or explicitly verifying the value is withinint32_trange and has no fractional part) to avoid surprising behavior.Suggested implementation:
If
std::numeric_limitsis not already available in this translation unit, add#include <limits>near the top ofsource/MaaAgentClient/Client/AgentClient.cpp.If the JSON/value type you're using does not provide
is_integer(), replace that call with an equivalent (e.g.,is_int64()or a combination ofis_number()and a fractional-part check) consistent with your JSON library’s API.