Skip to content

Conversation

@xuyuwtu
Copy link
Contributor

@xuyuwtu xuyuwtu commented Sep 27, 2025

添加类型: ActionHook,HandlerListHook,ServerApiHook
LazyPlugin: 添加了addCommands,addRestCommands, addHooks和内部使用的addReloadEvents以提供自动卸载功能。移动构造的内容到Initialize()以解决某些bug,修改子类的Initialize()以匹配更改,并把ChatCommands.Add()和RestApi.Register()全部移动到addCommands和addRestCommands
修改了两个子类的Initialize()以展示Hook如何使用

Sourcery 总结

通过引入统一的钩子抽象层和延迟注册集合,在 LazyPlugin 中自动化并集中化插件命令、REST 端点和钩子注册,并将现有插件迁移到新系统。

新功能:

  • 添加 HookBase 抽象:ActionHook, HandlerListHook, 和 ServerApiHook,用于一致的钩子注册和清理。
  • 扩展 LazyPlugin,增加 addCommands, addRestCommands, addHooks, 和 addReloadEvents 列表,并在 Initialize() 中实现自动注册,在 Dispose() 中实现清理。
  • 更新 CommandHelper.Register 以返回 TShockAPI.Command,并更新 RestHelper.Register 以返回 RestCommand 列表,用于延迟的端点注册。

改进:

  • 重构所有插件,用新的 LazyPlugin 集合替换直接的 ChatCommands.Add/Remove 和 RestApi.Register 调用,并调用 base.Initialize() 来触发注册。
  • 将配置文件重载事件的连接整合到 JsonConfigBase 中,并在 addReloadEvents 中捕获重载处理程序。
  • 引入动态 IL 解析工具 (CilUtils 和 Instruction) 以支持新的钩子创建机制。
Original summary in English

Summary by Sourcery

Automate and centralize plugin command, REST endpoint, and hook registration in LazyPlugin by introducing a unified hook abstraction layer and deferred registration collections, migrating existing plugins to the new system.

New Features:

  • Add HookBase abstractions: ActionHook, HandlerListHook, and ServerApiHook for consistent hook registration and cleanup.
  • Extend LazyPlugin with addCommands, addRestCommands, addHooks, and addReloadEvents lists and implement automatic registration in Initialize() and cleanup in Dispose().
  • Update CommandHelper.Register to return TShockAPI.Command and RestHelper.Register to return RestCommand lists for deferred endpoint registration.

Enhancements:

  • Refactor all plugins to replace direct ChatCommands.Add/Remove and RestApi.Register calls with the new LazyPlugin collections and call base.Initialize() to invoke registrations.
  • Consolidate config file reload event wiring into JsonConfigBase and capture reload handlers in addReloadEvents.
  • Introduce dynamic IL parsing utilities (CilUtils and Instruction) to support the new hook creation mechanism.

Copy link
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.

你好 - 我已经审查了你的更改 - 这里有一些反馈:

  • HookBase 的构造函数没有将提供的 loadType 赋值给 LoadType 属性,因此自动注册永远不会发生——请设置 LoadType = loadType。
  • ActionHook 中基于 IL 的钩子注销依赖于脆弱的字节码模式;请考虑添加防御性检查或回退路径以优雅地处理意外的 MSIL 布局。
  • 处置后,addCommands/addRestCommands/addHooks 集合未被清除,这可能导致插件重新初始化时出现重复注册——请考虑重置这些列表。
AI 代理提示
请解决此代码审查中的注释:

## 总体评论
- HookBase 的构造函数没有将提供的 loadType 赋值给 LoadType 属性,因此自动注册永远不会发生——请设置 LoadType = loadType。
- ActionHook 中基于 IL 的钩子注销依赖于脆弱的字节码模式;请考虑添加防御性检查或回退路径以优雅地处理意外的 MSIL 布局。
- 处置后,addCommands/addRestCommands/addHooks 集合未被清除,这可能导致插件重新初始化时出现重复注册——请考虑重置这些列表。

## 单独评论

### 评论 1
<location> `src/LazyAPI/Hooks/HookBase.cs:7` </location>
<code_context>
+{
+    public bool Registered { get; protected set; }
+    public HookLoadType LoadType { get; }
+    protected HookBase(HookLoadType loadType)
+    {
+        this.Registered = false;
</code_context>

<issue_to_address>
**问题 (bug_risk):** 构造函数未将 LoadType 参数赋值给属性。

在构造函数中赋值 'this.LoadType = loadType;' 以确保属性反映提供的参数。
</issue_to_address>

Sourcery 对开源免费 - 如果您喜欢我们的评论,请考虑分享它们 ✨
帮助我更有用!请对每个评论点击 👍 或 👎,我将使用反馈来改进您的评论。
Original comment in English

Hey there - I've reviewed your changes - here's some feedback:

  • HookBase's constructor isn't assigning the provided loadType to the LoadType property, so auto-registration will never occur—please set LoadType = loadType.
  • The IL-based hook unregistration in ActionHook relies on fragile bytecode patterns; consider adding defensive checks or fallback paths to handle unexpected MSIL layouts gracefully.
  • After disposing, the addCommands/addRestCommands/addHooks collections aren’t cleared, which could cause duplicate registrations if a plugin is reinitialized—consider resetting these lists.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- HookBase's constructor isn't assigning the provided loadType to the LoadType property, so auto-registration will never occur—please set LoadType = loadType.
- The IL-based hook unregistration in ActionHook relies on fragile bytecode patterns; consider adding defensive checks or fallback paths to handle unexpected MSIL layouts gracefully.
- After disposing, the addCommands/addRestCommands/addHooks collections aren’t cleared, which could cause duplicate registrations if a plugin is reinitialized—consider resetting these lists.

## Individual Comments

### Comment 1
<location> `src/LazyAPI/Hooks/HookBase.cs:7` </location>
<code_context>
+{
+    public bool Registered { get; protected set; }
+    public HookLoadType LoadType { get; }
+    protected HookBase(HookLoadType loadType)
+    {
+        this.Registered = false;
</code_context>

<issue_to_address>
**issue (bug_risk):** Constructor does not assign LoadType parameter to property.

Assign 'this.LoadType = loadType;' in the constructor to ensure the property reflects the provided parameter.
</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.

{
public bool Registered { get; protected set; }
public HookLoadType LoadType { get; }
protected HookBase(HookLoadType loadType)
Copy link
Contributor

Choose a reason for hiding this comment

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

问题 (bug_risk): 构造函数未将 LoadType 参数赋值给属性。

在构造函数中赋值 'this.LoadType = loadType;' 以确保属性反映提供的参数。

Original comment in English

issue (bug_risk): Constructor does not assign LoadType parameter to property.

Assign 'this.LoadType = loadType;' in the constructor to ensure the property reflects the provided parameter.

@ACaiCat ACaiCat added the enhancement New feature or request label Sep 28, 2025
@ACaiCat
Copy link
Member

ACaiCat commented Sep 29, 2025

别摸鱼了,看看吧 😭😭😭

ServerApi.Hooks.ProjectileAIUpdate.Register(this, this.ProjectAiUpdate);
TShockAPI.Commands.ChatCommands.Add(new Command("autofish", Commands.Afs, "af", "autofish"));
this.addCommands.Add(new Command("autofish", Commands.Afs, "af", "autofish"));
base.Initialize();
Copy link
Member

@ACaiCat ACaiCat Oct 1, 2025

Choose a reason for hiding this comment

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

每个地方都base.Initialize()太恶心了吧,有没有什么更好的办法

ACaiCat
ACaiCat previously requested changes Oct 1, 2025
Copy link
Member

@ACaiCat ACaiCat left a comment

Choose a reason for hiding this comment

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

我觉得最大的问题就是和LazyAPI的attribute有点重复,我觉得这两个应该得二选一

@ACaiCat ACaiCat marked this pull request as draft December 2, 2025 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants