Skip to content

refactor: 提取Go资源加载的共用逻辑#1582

Merged
MistEO merged 2 commits intoMaaEnd:v2from
isHarryh:v2
Mar 24, 2026
Merged

refactor: 提取Go资源加载的共用逻辑#1582
MistEO merged 2 commits intoMaaEnd:v2from
isHarryh:v2

Conversation

@isHarryh
Copy link
Copy Markdown
Member

@isHarryh isHarryh commented Mar 24, 2026

概要

此 PR 将 MapTracker 和 ItemTransfer 所使用的资源加载逻辑合并到 resource 包。

Summary by Sourcery

将通用的 Go 资源加载和路径解析逻辑提取到一个共享的 pkg/resource 模块中,并更新现有使用方以使用该模块。

增强内容:

  • 引入 pkg/resource 工具,用于以统一的搜索策略并结合 Maa 资源下沉集成来解析和读取资源文件。
  • 重构 map-tracker,使其在模板、地图目录、预览图片和外部数据加载方面依赖共享的资源辅助工具。
  • 重构 itemtransfer,通过共享的资源读取器加载 item_order.json,而不是使用自定义的目录发现逻辑。
Original summary in English

Summary by Sourcery

Extract shared Go resource loading and path resolution logic into a common pkg/resource module and update existing consumers to use it.

Enhancements:

  • Introduce pkg/resource utilities for resolving and reading resource files with a shared search strategy and Maa resource sink integration.
  • Refactor map-tracker to rely on the shared resource helpers for templates, map directories, preview images, and external data loading.
  • Refactor itemtransfer to load item_order.json via the shared resource reader instead of custom directory discovery logic.

Copilot AI review requested due to automatic review settings March 24, 2026 14:28
@isHarryh isHarryh requested review from Daydreamer114 and removed request for Copilot March 24, 2026 14:29
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - 我发现了 2 个问题,并给了一些整体层面的反馈:

  • 新的共享函数 resource.FindResource 相比之前的 MapTracker/ItemTransfer 实现明显不那么灵活(例如:不再去掉前缀 resource/,不再向资源路径的父目录回溯搜索),这可能会破坏现有的部署目录结构;建议增加等价的回退匹配模式,以保持行为向后兼容。
  • 移除对 MAAEND_ITEMTRANSFER_DATA_DIR 的支持会改变 item_order.json 被覆写/定位的方式;如果某些环境仍然需要基于环境变量的覆写能力,建议在共享的 resource 包中重新引入这类逻辑,而不是完全删除。
面向 AI Agent 的提示词
Please address the comments from this code review:

## Overall Comments
- The new shared `resource.FindResource` logic is noticeably less flexible than the previous `MapTracker`/`ItemTransfer` implementations (e.g. no stripping of leading `resource/`, no parent-of-resource-path search), which may break existing deployment layouts; consider adding equivalent fallback patterns so behavior remains backward compatible.
- The removal of `MAAEND_ITEMTRANSFER_DATA_DIR` support changes how `item_order.json` can be overridden/located; if this env-based override is still needed in some environments, consider reintroducing it inside the shared `resource` package rather than dropping it entirely.

## Individual Comments

### Comment 1
<location path="agent/go-service/pkg/resource/resource_sink.go" line_range="29-37" />
<code_context>
-type resourcePathSink struct{}
-
-// OnResourceLoading captures the resource path when a resource is loaded.
-func (c *resourcePathSink) OnResourceLoading(resource *maa.Resource, status maa.EventStatus, detail maa.ResourceLoadingDetail) {
-	if status != maa.EventStatusSucceeded || detail.Path == "" {
-		return
</code_context>
<issue_to_address>
**issue (bug_risk):** The stored resourcePath is a file path but later treated as a base directory, which can produce invalid joined paths.

`OnResourceLoading` stores `detail.Path` (or its absolute form) directly in `resourcePath`, and `getResourceBase` returns it. `FindResource` then calls `filepath.Join(base, rel)`. When `detail.Path` is a file path (the usual case), `base` is not a directory, so the join yields invalid paths like `/path/to/some.png/data/ItemTransfer/item_order.json`. The old `FindResource` logic handled this by using `filepath.Dir(base)`; the new helper does not. Either store `filepath.Dir(absPath)` as the base, or have `getResourceBase` wrap the stored path with `filepath.Dir` before joining.
</issue_to_address>

### Comment 2
<location path="agent/go-service/itemtransfer/types.go" line_range="57" />
<code_context>
-			return
-		}
-		b, err := os.ReadFile(filepath.Join(dir, "item_order.json"))
+		b, err := resource.ReadResource("data/ItemTransfer/item_order.json")
 		if err != nil {
 			cachedDataErr = err
</code_context>
<issue_to_address>
**question (bug_risk):** Removing the custom directory search (incl. env override) may break deployments that rely on MAAEND_ITEMTRANSFER_DATA_DIR or deeper search paths.

The previous `loadItemOrderData` respected `MAAEND_ITEMTRANSFER_DATA_DIR`, walked multiple parent directories, and fell back to paths like `assets/data/ItemTransfer`. Switching to `resource.ReadResource("data/ItemTransfer/item_order.json")` drops the env override and narrows the search to a few fixed locations. If existing deployments depend on that configurability, this is a breaking change. Consider wiring the env-based base directory (or equivalent) into `getStandardResourceBase` or this call so those setups continue to work.
</issue_to_address>

Sourcery 对开源项目免费——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请对每条评论点 👍 或 👎,我会根据这些反馈改进以后的评审质量。
Original comment in English

Hey - I've found 2 issues, and left some high level feedback:

  • The new shared resource.FindResource logic is noticeably less flexible than the previous MapTracker/ItemTransfer implementations (e.g. no stripping of leading resource/, no parent-of-resource-path search), which may break existing deployment layouts; consider adding equivalent fallback patterns so behavior remains backward compatible.
  • The removal of MAAEND_ITEMTRANSFER_DATA_DIR support changes how item_order.json can be overridden/located; if this env-based override is still needed in some environments, consider reintroducing it inside the shared resource package rather than dropping it entirely.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new shared `resource.FindResource` logic is noticeably less flexible than the previous `MapTracker`/`ItemTransfer` implementations (e.g. no stripping of leading `resource/`, no parent-of-resource-path search), which may break existing deployment layouts; consider adding equivalent fallback patterns so behavior remains backward compatible.
- The removal of `MAAEND_ITEMTRANSFER_DATA_DIR` support changes how `item_order.json` can be overridden/located; if this env-based override is still needed in some environments, consider reintroducing it inside the shared `resource` package rather than dropping it entirely.

## Individual Comments

### Comment 1
<location path="agent/go-service/pkg/resource/resource_sink.go" line_range="29-37" />
<code_context>
-type resourcePathSink struct{}
-
-// OnResourceLoading captures the resource path when a resource is loaded.
-func (c *resourcePathSink) OnResourceLoading(resource *maa.Resource, status maa.EventStatus, detail maa.ResourceLoadingDetail) {
-	if status != maa.EventStatusSucceeded || detail.Path == "" {
-		return
</code_context>
<issue_to_address>
**issue (bug_risk):** The stored resourcePath is a file path but later treated as a base directory, which can produce invalid joined paths.

`OnResourceLoading` stores `detail.Path` (or its absolute form) directly in `resourcePath`, and `getResourceBase` returns it. `FindResource` then calls `filepath.Join(base, rel)`. When `detail.Path` is a file path (the usual case), `base` is not a directory, so the join yields invalid paths like `/path/to/some.png/data/ItemTransfer/item_order.json`. The old `FindResource` logic handled this by using `filepath.Dir(base)`; the new helper does not. Either store `filepath.Dir(absPath)` as the base, or have `getResourceBase` wrap the stored path with `filepath.Dir` before joining.
</issue_to_address>

### Comment 2
<location path="agent/go-service/itemtransfer/types.go" line_range="57" />
<code_context>
-			return
-		}
-		b, err := os.ReadFile(filepath.Join(dir, "item_order.json"))
+		b, err := resource.ReadResource("data/ItemTransfer/item_order.json")
 		if err != nil {
 			cachedDataErr = err
</code_context>
<issue_to_address>
**question (bug_risk):** Removing the custom directory search (incl. env override) may break deployments that rely on MAAEND_ITEMTRANSFER_DATA_DIR or deeper search paths.

The previous `loadItemOrderData` respected `MAAEND_ITEMTRANSFER_DATA_DIR`, walked multiple parent directories, and fell back to paths like `assets/data/ItemTransfer`. Switching to `resource.ReadResource("data/ItemTransfer/item_order.json")` drops the env override and narrows the search to a few fixed locations. If existing deployments depend on that configurability, this is a breaking change. Consider wiring the env-based base directory (or equivalent) into `getStandardResourceBase` or this call so those setups continue to work.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copilot AI review requested due to automatic review settings March 24, 2026 14:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

此 PR 将 go-service 中 MapTracker 与 ItemTransfer 先前各自实现的资源路径探测/读取逻辑,抽取为共享的 pkg/resource,并在主注册流程中统一注册 Maa Resource sink,以便在不同工作目录/安装布局下更稳定地定位资源文件。

Changes:

  • 新增 agent/go-service/pkg/resource:通过 Maa resource sink 缓存资源基路径,并提供 FindResource/ReadResource 统一查找与读取接口
  • 重构 map-tracker:模板、地图目录与外部数据读取改用共享 resource 工具
  • 重构 itemtransfer:item_order.json 改用共享读取器加载,并移除 README 中对应的环境变量说明

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
agent/go-service/register.go 在全局注册阶段统一启用 resource sink
agent/go-service/pkg/resource/resource_sink.go 新增 Maa ResourceEventSink:捕获并缓存资源路径
agent/go-service/pkg/resource/resource_reader.go 新增统一资源查找/读取逻辑(FindResource/ReadResource)
agent/go-service/map-tracker/register.go 移除 map-tracker 内部单独的 resource sink 注册
agent/go-service/map-tracker/move.go 预览地图读取改为使用共享 FindResource
agent/go-service/map-tracker/internal/resource.go MapTracker 资源加载改用共享 FindResource/ReadResource
agent/go-service/map-tracker/big_map_pick.go 外部数据读取改为使用共享 ReadResource
agent/go-service/itemtransfer/types.go item_order.json 加载改为使用共享 ReadResource
agent/go-service/itemtransfer/register.go 移除 itemtransfer 内部单独的 resource sink 注册
agent/go-service/itemtransfer/README.md 移除已不再生效的数据目录环境变量说明

@MistEO MistEO merged commit ffcc7ab into MaaEnd:v2 Mar 24, 2026
20 checks passed
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.

3 participants