Skip to content
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

add Custombutton #31

Merged
merged 1 commit into from
Feb 10, 2025
Merged

add Custombutton #31

merged 1 commit into from
Feb 10, 2025

Conversation

ck2739046
Copy link
Contributor

@ck2739046 ck2739046 commented Feb 9, 2025

Summary by Sourcery

新功能:

  • 允许用户通过从指定目录加载按钮纹理来自定义按钮外观。
Original summary in English

Summary by Sourcery

New Features:

  • Allow users to customize button textures by loading them from a specified directory.

Copy link

sourcery-ai bot commented Feb 9, 2025

Here's the translation of the review guide to Chinese:

审阅者指南 by Sourcery

此拉取请求引入了一个实现自定义按钮纹理加载器的新模块。更改包括创建一个新的 CustomButton 类,该类从指定目录加载纹理,将它们初始化到字典中,并应用 Harmony 补丁以覆盖 ButtonControllerBase 和 ButtonManager 中默认的按钮纹理加载过程。

ButtonControllerBase 上 CustomButton 补丁的序列图

sequenceDiagram
    participant GS as GameSystem
    participant BC as ButtonControllerBase
    participant CB as CustomButton

    GS->>BC: Call LoadDefaultResources()
    alt Harmony patch active
        BC->>CB: Invoke LoadDefaultResourcesPrefix()
        CB->>CB: Reflect to obtain FlatButtonParam field
        CB->>CB: Iterate over button info and replace textures
        CB->>BC: Set _isFlatButtonLoaded to true
    end
    Note over CB,BC: Custom textures are applied if available, otherwise default textures loaded
Loading

ButtonManager 上 CustomButton 补丁的序列图

sequenceDiagram
    participant GS as GameSystem
    participant BM as ButtonManager
    participant CB as CustomButton

    GS->>BM: Call Initialize()
    alt Harmony patch active
        BM->>CB: Invoke ButtonManagerInitializePostfix(__instance)
        CB->>CB: Use reflection to access _containers and Container's SpritePath
        CB->>CB: Iterate container values, replacing sprites with custom ones if available
    end
    Note over CB,BM: Custom textures override default container sprites when available
Loading

CustomButton 模块的更新类图

classDiagram
    class CustomButton {
      <<static>>
      - string buttonDir = "LocalAssets/Buttons"
      - Dictionary~string, Sprite~ spriteDict
      - bool isInitialized
      + void OnBeforePatch()
      - void Initialize()
      + void LoadDefaultResourcesPrefix() <<HarmonyPrefix, HarmonyPatch>>
      + void ButtonManagerInitializePostfix(ButtonManager __instance) <<HarmonyPostfix, HarmonyPatch>>
    }
Loading

文件级更改

更改 详情 文件
创建 CustomButton 类以加载和管理自定义按钮纹理。
  • 建立配置节以定义按钮纹理目录。
  • 实现从提供的目录初始化纹理加载过程的方法。
  • 创建一个字典来存储从 PNG 文件生成的精灵。
AquaMai.Mods/Fancy/CustomButton.cs
集成 Harmony 补丁以覆盖默认按钮资源加载。
  • 为 ButtonControllerBase.LoadDefaultResources 添加 HarmonyPrefix 补丁,以在可用时用自定义精灵替换按钮纹理。
  • 使用反射修改箭头精灵以确保正确加载。
  • 为 ButtonManager.Initialize 添加 HarmonyPostfix 补丁,以使用自定义纹理更新容器按钮图像。
AquaMai.Mods/Fancy/CustomButton.cs

提示和命令

与 Sourcery 交互

  • 触发新审阅: 在拉取请求上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub 问题: 通过回复审阅评论要求 Sourcery 创建问题。您也可以用 @sourcery-ai issue 回复审阅评论来创建问题。
  • 生成拉取请求标题: 在拉取请求标题的任何位置写 @sourcery-ai 以随时生成标题。您也可以在拉取请求上评论 @sourcery-ai title 以随时(重新)生成标题。
  • 生成拉取请求摘要: 在拉取请求正文的任何位置写 @sourcery-ai summary 以随时生成 PR 摘要。您也可以在拉取请求上评论 @sourcery-ai summary 以随时(重新)生成摘要。
  • 生成审阅者指南: 在拉取请求上评论 @sourcery-ai guide 以随时(重新)生成审阅者指南。
  • 解决所有 Sourcery 评论: 在拉取请求上评论 @sourcery-ai resolve 以解决所有 Sourcery 评论。如果您已经处理了所有评论,不想再看到它们,这很有用。
  • 取消所有 Sourcery 审阅: 在拉取请求上评论 @sourcery-ai dismiss 以取消所有现有的 Sourcery 审阅。如果您想重新开始新的审阅,这特别有用 - 别忘了评论 @sourcery-ai review 以触发新的审阅!
  • 为问题生成行动计划: 在问题上评论 @sourcery-ai plan 以生成问题的行动计划。

自定义您的体验

访问您的仪表板以:

  • 启用或禁用审阅功能,如 Sourcery 生成的拉取请求摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、删除或编辑自定义审阅说明。
  • 调整其他审阅设置。

获取帮助

Original review guide in English

Reviewer's Guide by Sourcery

This pull request introduces a new module that implements a custom button texture loader. The changes include creating a new CustomButton class that loads textures from a specified directory, initializing them into a dictionary, and applying Harmony patches to override the default button texture loading process in ButtonControllerBase and ButtonManager.

Sequence diagram for CustomButton patch on ButtonControllerBase.LoadDefaultResources

sequenceDiagram
    participant GS as GameSystem
    participant BC as ButtonControllerBase
    participant CB as CustomButton

    GS->>BC: Call LoadDefaultResources()
    alt Harmony patch active
        BC->>CB: Invoke LoadDefaultResourcesPrefix()
        CB->>CB: Reflect to obtain FlatButtonParam field
        CB->>CB: Iterate over button info and replace textures
        CB->>BC: Set _isFlatButtonLoaded to true
    end
    Note over CB,BC: Custom textures are applied if available, otherwise default textures loaded
Loading

Sequence diagram for CustomButton patch on ButtonManager.Initialize

sequenceDiagram
    participant GS as GameSystem
    participant BM as ButtonManager
    participant CB as CustomButton

    GS->>BM: Call Initialize()
    alt Harmony patch active
        BM->>CB: Invoke ButtonManagerInitializePostfix(__instance)
        CB->>CB: Use reflection to access _containers and Container's SpritePath
        CB->>CB: Iterate container values, replacing sprites with custom ones if available
    end
    Note over CB,BM: Custom textures override default container sprites when available
Loading

Updated class diagram for the CustomButton module

classDiagram
    class CustomButton {
      <<static>>
      - string buttonDir = "LocalAssets/Buttons"
      - Dictionary~string, Sprite~ spriteDict
      - bool isInitialized
      + void OnBeforePatch()
      - void Initialize()
      + void LoadDefaultResourcesPrefix() <<HarmonyPrefix, HarmonyPatch>>
      + void ButtonManagerInitializePostfix(ButtonManager __instance) <<HarmonyPostfix, HarmonyPatch>>
    }
Loading

File-Level Changes

Change Details Files
Creation of the CustomButton class to load and manage custom button textures.
  • Established a configuration section to define the button texture directory.
  • Implemented a method to initialize the texture loading process from a provided directory.
  • Created a dictionary to store sprites generated from PNG files.
AquaMai.Mods/Fancy/CustomButton.cs
Integration of Harmony patches to override default button resource loading.
  • Added a HarmonyPrefix patch for ButtonControllerBase.LoadDefaultResources to replace button textures with custom sprites when available.
  • Modified arrow sprites using reflection to ensure they load correctly.
  • Added a HarmonyPostfix patch for ButtonManager.Initialize to update container button images with custom textures.
AquaMai.Mods/Fancy/CustomButton.cs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@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.

@ck2739046 - 我已经审查了你的更改 - 以下是一些反馈:

总体评论

  • 在使用反射调用之前,请验证它们返回非空值(例如,对于字段和属性),以防止可能出现的空引用异常。
以下是我在审查期间查看的内容
  • 🟡 一般性问题:发现1个问题
  • 🟢 安全性:一切看起来都很好
  • 🟢 测试:一切看起来都很好
  • 🟢 复杂性:一切看起来都很好
  • 🟢 文档:一切看起来都很好

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

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

Overall Comments:

  • Verify that reflection calls return non-null values (e.g., for fields and properties) before using them to prevent potential null reference exceptions.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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 static void OnBeforePatch() { Initialize(); }

private static void Initialize()
Copy link

Choose a reason for hiding this comment

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

建议: 纹理加载中的异常处理

虽然 catch 块记录了异常,但请考虑是否可以添加更多上下文,例如文件特定的详细信息,这可以帮助在批量文件处理期间更精确地定位纹理加载问题。

建议的实现:

            foreach (var file in Directory.EnumerateFiles(buttonDir_r, "*.png")) {
                try {
                    var fileName = Path.GetFileNameWithoutExtension(file)var bytes = File.ReadAllBytes(file)
                    // (纹理处理逻辑的其余部分)
                }
                catch (Exception ex)
                {
                    MelonLogger.Error($"加载纹理文件 '{file}' 时出错:{ex}");
                }

验证你的纹理处理逻辑的其余部分是否仍在内部 try 块中。
另外,确保任何潜在的外部依赖项都了解处理逻辑,以便后续文件的处理不受影响。

Original comment in English

suggestion: Exception handling in texture loading

While the catch block logs exceptions, consider if additional context, such as file-specific details, could help pinpoint issues during texture loading, especially during bulk file processing.

Suggested implementation:

            foreach (var file in Directory.EnumerateFiles(buttonDir_r, "*.png")) {
                try {
                    var fileName = Path.GetFileNameWithoutExtension(file);
                    var bytes = File.ReadAllBytes(file);
                    // (rest of texture processing logic)
                }
                catch (Exception ex)
                {
                    MelonLogger.Error($"Error loading texture file '{file}': {ex}");
                }

Verify that the rest of your texture processing logic inside the foreach loop remains inside the inner try block.
Also, ensure that any potential dependencies outside are aware of the handling logic so that the processing of subsequent files continues unaffected.

@clansty clansty merged commit daf3083 into MewoLab:main Feb 10, 2025
1 check passed
@ck2739046 ck2739046 deleted the customButton branch February 11, 2025 03:37
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