DynPatcher
在运行时动态编译并加载源自源码的 Mixin 补丁。
用户指南
概述
DynPatcher 允许你添加或修改 Fabric Mixin,而无需重新编译模组 JAR。放置在 patches 目录中的源 .java 文件会在启动时使用内置 Java 编译器进行编译,并向 Mixin 注册。
文件结构
当安装 DynPatcher 并启动游戏时,会在你的 .minecraft/config/dynpatcher/ 文件夹中创建以下目录结构:
patches/:在此处放置你的 Mixin .java 源文件。可以使用子目录来组织文件。
config.json:模组配置文件。
.cache/:包含已编译字节码和缓存索引的内部目录。请勿手动修改。
禁用补丁
若要禁用特定补丁而不删除源文件,请在 config.json 的 disabledPatches 下列出它们的相对路径:
{
"disabledPatches": [
"example/MyMixin.java"
]
}
路径相对于 config/dynpatcher/patches/ 目录。
源文件处理
- 重命名文件:如果重命名
.java 文件导致其文件名不再匹配其 public class 声明,DynPatcher 会在内存中的编译期间移除 public 修饰符,以避免编译器错误。
- 默认包:如果
.java 文件缺少 package 声明,DynPatcher 会将其自动重映射为内存中的 dynpatcher.generated,以便 Mixin 能够加载它。
- 环境过滤:Mixin 类可以注解
net.fabricmc.api.Environment(@Environment(EnvType.CLIENT) 或 @Environment(EnvType.SERVER))。未注解的 Mixin 将在所有环境中加载,而注解过的 Mixin 仅在其对应的目标环境(CLIENT 或 SERVER)中加载。
缓存
已编译的字节码缓存在 config/dynpatcher/.cache/ 中。只有当源文件的内容、时间戳更改,或者安装的模组版本发生变化时,才会重新编译该源文件。
开发者文档
架构
DynPatcher 作为 IMixinConfigPlugin 在早期启动阶段执行:
- 配置与发现:读取
config.json 并扫描 config/dynpatcher/patches/ 中的 .java 文件,过滤掉已禁用的补丁。
- 缓存解析:使用时间戳和 SHA-256 内容哈希值验证缓存的
.class 文件是否与 index.json 一致。
- 预处理:读取陈旧或未缓存的源文件,在内存中应用语法调整(注入默认包和剥离
public 修饰符)。
- 运行时编译:使用 Eclipse Compiler for Java (ECJ) 在内存中编译源对象。
- 字节码检查与修补:使用 ASM 检查已编译类的
net.fabricmc.api.Environment 注解(过滤掉与当前环境不兼容的 Mixin),并为未指定重映射偏好的 Mixin 注解追加 remap = false。
- Mixin 注册:动态生成每个包的 Mixin JSON 配置,并通过
Mixins.addConfiguration 进行注册。
DynPatcher
Dynamically compile and load Mixin patches from source at runtime.
User Guide
Overview
DynPatcher allows you to add or modify Fabric Mixins without recompiling the mod JAR. Source .java files placed in the patches directory are compiled at startup using an embedded Java compiler and registered with Mixin.
File Structure
When the game launches with DynPatcher installed, the following directory structure is created in your .minecraft/config/dynpatcher/ folder:
patches/: Place your Mixin .java source files here. Subdirectories can be used to organize files.
config.json: Mod configuration file.
.cache/: Internal directory containing compiled bytecode and the cache index. Do not modify manually.
Disabling Patches
To disable specific patches without removing the source files, list their relative paths under disabledPatches in config.json:
{
"disabledPatches": [
"example/MyMixin.java"
]
}
Paths are relative to the config/dynpatcher/patches/ directory.
Source File Handling
- Renamed files: If a
.java file is renamed such that its filename no longer matches its public class declaration, DynPatcher removes the public modifier in memory during compilation to avoid compiler errors.
- Default package: If a
.java file lacks a package declaration, DynPatcher automatically remaps it to dynpatcher.generated in memory so Mixin can load it.
- Environment filtering: Mixin classes can be annotated with
net.fabricmc.api.Environment (@Environment(EnvType.CLIENT) or @Environment(EnvType.SERVER)). Unannotated Mixins are loaded in all environments, while annotated Mixins are loaded only in their corresponding target environment (CLIENT or SERVER).
Caching
Compiled bytecode is cached in config/dynpatcher/.cache/. A source file is recompiled only if its content or timestamp changes, or if the installed mod version changes.
Developer Documentation
Architecture
DynPatcher executes during early startup as an IMixinConfigPlugin:
- Configuration & Discovery: Reads
config.json and scans config/dynpatcher/patches/ for .java files, filtering out disabled patches.
- Cache Resolution: Validates cached
.class files against index.json using timestamp and SHA-256 content hashes.
- Preprocessing: Reads stale or uncached sources, applying syntax adjustments (default package injection and
public modifier stripping) in memory.
- Runtime Compilation: Compiles source objects in memory using the Eclipse Compiler for Java (ECJ).
- Bytecode Inspection & Patching: Uses ASM to inspect compiled classes for
net.fabricmc.api.Environment annotations (filtering out Mixins incompatible with the current environment) and appends remap = false to Mixin annotations that do not specify a remapping preference.
- Mixin Registration: Dynamically generates per-package Mixin JSON configurations and registers them via
Mixins.addConfiguration.
评论 0
登录后参与评分
还没有评论,来抢沙发吧