ModContainerWrapper

基本信息

属性 值
类型 @Desugar record
实现 SortableCandidate
字段 ModContainer mod, ModCandidateV2 candidate, ASMModParserV2 parser

ModContainerWrapper 是 V2 引入的轻量 record,包装已构造的 ModContainer 与对应的 ModCandidateV2 / ASMModParserV2,让 ModCandidateV2Sorter 能用同一个排序器处理 mod 和 API-only jar。

实现

@Desugar
public record ModContainerWrapper(ModContainer mod, ModCandidateV2 candidate, ASMModParserV2 parser) implements SortableCandidate {
    @Override
    public @NotNull String getId() { return mod.getModId(); }

    @Override
    public @NotNull DefaultArtifactVersion getVersion() {
        return new DefaultArtifactVersion(mod.getVersion());
    }

    @Override
    public @NotNull File getFile() { return mod.getSource(); }

    public int getNestLevel() { return candidate.getNestLevel(); }
}

与 APIOnlyWrapper 区别

字段 ModContainerWrapper APIOnlyWrapper
ModContainer 有 无
candidate ModCandidateV2 ModCandidateV2
parser ASMModParserV2 无

SortableCandidate 三方法

方法 实现
getId() 委托 mod.getModId()
getVersion() new DefaultArtifactVersion(mod.getVersion())(每次新建)
getFile() mod.getSource()

使用

ModCandidateV2Sorter.computeClasspathOrder 把每个 SortableCandidate 看作排序节点。

LoaderV2.identifyMods 在 sort 完成后:

for (SortableCandidate sortable : resolvedCandidates.get()) {
    if (sortable instanceof ModContainerWrapper wrapper) {
        final ModContainer mod = wrapper.mod();
        uniqueCandidates.add(wrapper.candidate());
        dataTable.addContainer(mod);
        mods.add(mod);
    } else if (sortable instanceof APIOnlyWrapper apiWrapper) {
        // ... classpath 加 ...
    }
}

dataTable.addContainer(mod) 让 ASMDataTable 索引 mod 的 @Mod 注解。

nestLevel

getNestLevel() 在 MOD_COMPARATOR / COREMOD_COMPARATOR 的最后一位消费,决定嵌套 jar 内的 mod 与父 jar 内 mod 的相对顺序。

相关条目