Skip to content

fix(comp): CompFile 的集合字段初始化为空列表,避免 ToJson 空引用崩溃#3352

Open
NoClassFoundError wants to merge 1 commit into
PCL-Community:devfrom
NoClassFoundError:fix/compfile-null-collections-3058
Open

fix(comp): CompFile 的集合字段初始化为空列表,避免 ToJson 空引用崩溃#3352
NoClassFoundError wants to merge 1 commit into
PCL-Community:devfrom
NoClassFoundError:fix/compfile-null-collections-3058

Conversation

@NoClassFoundError

@NoClassFoundError NoClassFoundError commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

CompFile 的 ModLoaders / RawGameVersions / GameVersions 三个 readonly 集合字段声明处未初始化,而 CompFile(JsonObject) 构造函数仅在缓存 JSON 含对应键时才赋值(if ContainsKey)。
当数据缺失这些键时字段保持 null,随后 CompFile.ToJson() 对其调用 .Select() 便抛出ArgumentNullException(Parameter 'source'),表现为启用/禁用某些模组时启动器直接崩溃弹错误窗口;ToListItem 中的 GameVersions.All(...) 亦会因 null 崩溃。
为这三个字段声明补上 = new(),与 Dependencies 等其他集合字段一致,从源头保证非 null。

resolves #3058

本PR的提交信息生成由Claude Code Opus 4.8辅助完成,由Opus 4.8对代码进行了审查。

Summary by Sourcery

Bug Fixes:

  • 确保 CompFile 中的 RawGameVersionsGameVersionsModLoaders 始终初始化为空列表,以避免在 JSON 序列化和进行列表操作时触发 ArgumentNullException
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Ensure RawGameVersions, GameVersions, and ModLoaders in CompFile are always initialized to empty lists to avoid ArgumentNullException during JSON serialization and list operations.

@pcl-ce-automation pcl-ce-automation Bot added 🛠️ 等待审查 Pull Request 已完善,等待维护者或负责人进行代码审查 size: XS PR 大小评估:微型 labels Jul 5, 2026
@sourcery-ai

sourcery-ai Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor
审查者指南(在小型 PR 上折叠)

审查者指南

在 CompFile 中将三个只读集合字段初始化为空列表,以防止在 JSON 序列化和列表项生成期间出现空引用崩溃。

文件级更改

更改 详情 文件
确保 CompFile 集合字段永远不为 null,以避免在执行 ToJson 和 ToListItem 操作时出现 ArgumentNullException。
  • 在声明处将 RawGameVersions 初始化为一个空的 List,而不是在 JSON 缺少对应键时将其保留为 null。
  • 在声明处将 GameVersions 初始化为一个空的 List,从而下游调用如 GameVersions.All(...) 不再因 null 崩溃。
  • 在声明处将 ModLoaders 初始化为一个空的 List,以防止在遍历或序列化加载器时出现与 null 相关的故障。
Plain Craft Launcher 2/Modules/Minecraft/ModComp.cs

与关联 Issue 的对照评估

Issue 目标 是否解决 说明
#3058 防止在启用/禁用某些模组时,如果其 CompFile JSON 缺少 ModLoaders/RawGameVersions/GameVersions 键,会导致启动器崩溃(ArgumentNullException)。

可能关联的 Issue


提示和命令

与 Sourcery 交互

  • 触发新的审查: 在 Pull Request 上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub Issue: 通过回复某条审查评论,要求 Sourcery 从该评论创建一个 Issue。你也可以在审查评论下回复 @sourcery-ai issue 来创建对应的 Issue。
  • 生成 Pull Request 标题: 在 Pull Request 标题中的任意位置写上 @sourcery-ai,即可随时生成标题。你也可以在 Pull Request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 Pull Request 摘要: 在 Pull Request 正文中的任意位置写上 @sourcery-ai summary,即可在你想要的位置生成 PR 摘要。你也可以在 Pull Request 中评论 @sourcery-ai summary 来在任何时候(重新)生成摘要。
  • 生成审查者指南: 在 Pull Request 中评论 @sourcery-ai guide,即可在任何时候(重新)生成审查者指南。
  • 解决所有 Sourcery 评论: 在 Pull Request 中评论 @sourcery-ai resolve,即可解决所有 Sourcery 评论。适用于你已经处理完所有评论且不想再看到它们的时候。
  • 忽略所有 Sourcery 审查: 在 Pull Request 中评论 @sourcery-ai dismiss,即可忽略所有现有的 Sourcery 审查。特别适合你希望以一次全新的审查开始时——别忘了评论 @sourcery-ai review 来触发新的审查!

自定义你的使用体验

访问你的 控制面板 以:

  • 启用或禁用审查功能,例如 Sourcery 生成的 Pull Request 摘要、审查者指南等。
  • 更改审查语言。
  • 添加、移除或编辑自定义审查指令。
  • 调整其他审查设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Initializes three readonly collection fields in CompFile to empty lists to prevent null reference crashes during JSON serialization and list item generation.

File-Level Changes

Change Details Files
Ensure CompFile collection fields are never null to avoid ArgumentNullException during ToJson and ToListItem operations.
  • Initialize RawGameVersions to an empty List at declaration instead of leaving it null when JSON lacks the corresponding key.
  • Initialize GameVersions to an empty List at declaration so downstream calls like GameVersions.All(...) no longer crash on null.
  • Initialize ModLoaders to an empty List at declaration to prevent null-related failures when iterating or serializing loaders.
Plain Craft Launcher 2/Modules/Minecraft/ModComp.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#3058 Prevent the launcher from crashing (ArgumentNullException) when enabling/disabling certain mods whose CompFile JSON lacks ModLoaders/RawGameVersions/GameVersions keys.

Possibly linked issues


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!

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

你好,我已经审查了你的修改,看起来非常棒!


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

Hey - I've reviewed your changes and they look great!


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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: XS PR 大小评估:微型 🛠️ 等待审查 Pull Request 已完善,等待维护者或负责人进行代码审查

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C#]: 在mod列表,启用/禁用某些特定mod时直接崩溃了。

2 participants