User Tools

Site Tools


zh_cn:documentation:fabric_loom

This is an old revision of the document!


Fabric Loom

Fabric Loom,或者简称为 Loom,是个 Gradle 插件,用于 Fabric 生态系统的模组开发中。Loom 提供了在开发环境中安装 Minecraft 和模组的一系列工具,所以你可以针对 Minecraft 混淆及其发行版和版本之间的差异来将 Loom 与 Minecraft 链接起来。Loom 还提供了用于 Fabric 加载器的运行配置、mixin 编译处理,以及用于 Fabric Loader 的 jar 中 jar 系统的工具。

常用任务

  • migrateMappings:将当前源代码合并到指定的映射中。见 migratemappings
  • remapJar:产生包含 jar 任务的重映射的输出的 jar。同时也会将 jar 中 jar 包含在模组中。运行 build 时会调用。
  • genSources:使用默认的反编译器(CFR)反编译 Minecraft jar。
  • downloadAssets:下载配置的 Minecraft 版本的资源索引和对象,下载到用户缓存中。
  • genEclipseRuns:安装 Eclipse 运行配置并创建运行目录(如果还不存在)。
  • vscode:生成或覆盖 Visual Studio Code 的 launch.json 文件,启动配置在 .vscode 文件夹中,并创建运行目录,如果还不存在。
  • remapSourcesJar:只有在 AbstractArchiveTask sourcesJar 存在时存在。将 sourcesJar 任务的输出重映射。
  • runClient:将 Fabric Loader 作为 Minecraft 客户端运行的 JavaExec 任务。
  • runServer:将 Fabric Loader 作为 Minecraft 专用服务器运行的 JavaExec 任务。

依赖子项目

设立依赖其他 loom 项目的多项目构建时,你应该在依赖其他项目时使用 namedElements 配置。默认情况下,项目的“输出”会映射到中间名。namedElements 配置包含还未重映射的项目输出。

dependencies {
	implementation project(path: ":name", configuration: "namedElements")
}

选项

loom {
	// 设置访问加宽的路径,参见 https://fabricmc.net/wiki/zh_cn:tutorial:accesswideners
	accessWidenerPath = file("src/main/resources/modid.accesswidener")

	// 添加额外的 log4j 配置文件。
	log4jConfigs.from(file("log4j.xml"))

	// 若启用,输出的存档会自动重映射。
	remapArchives = true
	// 若启用,*Elements 配置中的 -dev jars 会被替换为重映射的 jar。
	setupRemappedVariants = true
	// 若启用,传递性的访问加宽会从依赖中应用。
	enableTransitiveAccessWideners = true
	// 若启用,log4j 只会在运行时类路径中,强制使用 SLF4j。
	runtimeOnlyLog4j = false

	// 若启用,只有与服务器有关的特性和 jars 会被设置。
	serverOnlyMinecraftJar()
	// 若设置,minecraft jar 会分为常规和仅客户端。这只是实验性的。Fabric 加载器暂时还不支持这个功能。
	splitMinecraftJar()

	// 用于配置存在的或者新的运行配置
	runs {
		client {
			// 添加 VM 变量
			vmArgs "-Dexample=true"
			// 添加 JVM 属性
			property("example", "true")
			// 添加 program 变量
			programArg "--example"
			// 需要运行的环境(或端),通常是 client 或 server。
			environment = "client"
			// 运行配置的完整名称,例如“Minecraft 客户端”。默认由基础名称决定。
			configName = "Minecraft 客户端"
			// 运行配置的默认主类。使用带有 fabric_installer.json 文件的模组加载器时,就会覆盖这个。
			defaultMainClass = ""
			// 此配置的运行目录,与根项目目录有关。
			runDir = "run"
			// 运行的源集,通常设为 sourceSets.test
			source = sourceSets.main
			// 若为 true,运行配置会为 IDE 生成。默认仅对根项目为 true。
			ideConfigGenerated = true

			// 配置以默认的客户端选项运行配置。
			client()

			// 配置以默认的服务器选项运行配置。
			server()
		}

		// 为测试创建基本运行配置的示例
		testClient {
			// 从另一个运行配置复制设置。
			inherit client

			configName = "测试 Minecraft 客户端"
			source = sourceSets.test
		}
	}

	// 配置所有运行配置以生成 ide 运行配置。对子项目很有用。
	runConfigs.configureEach {
		ideConfigGenerated = true
	}

	// 用于配置 mixin 选项,或应用到额外的源集。
	mixin {
		// When disabled tiny remapper will be used to remap Mixins instead of the AP. Experimental.
		useLegacyMixinAp = true
		// Set the default refmap name
		defaultRefmapName = "example.refmap.json"

		// See https://github.com/FabricMC/fabric-loom/blob/dev/0.11/src/main/java/net/fabricmc/loom/api/MixinExtensionAPI.java for options to add additional sourcesets
	}

	// 配置或添加新的反编译器
	decompilers {
		// 配置默认的反编译器,cfr 或 fernflower
		cfr {
			// 将额外选项传递到编译器
			options += [
				key: "value"
			]
			// 设置分叉 JVM 时使用的内存量(以兆字节为单位)
			memory = 4096
			// 设置反编译器可以使用的最大线程数。
			maxThreads = 8
		}
	}

	interfaceInjection {
		// 若启用,将会应用配置的注入的接口。
		enableDependencyInterfaceInjection = true
	}
}

remapJar {
	// Set the input jar for the task, also valid for remapSourcesJar
	inputFile = file("example.jar")
	// Set the source namespace, also valid for remapSourcesJar
	sourceNamespace = "named"
	// Set the target namespace, also valid for remapSourcesJar
	targetNamespace = "intermediary"
	// Add additional jar files to the remap classpath, also valid for remapSourcesJar
	classpath.from file("classpath.jar")

	// Add a nested mod jar to this task, the include configuration should be used for maven libraries and mods.
	nestedJars.from file("nested.jar")
	// When enabled nested jars will be included with the output jar.
	addNestedDependencies = true
}

dependencies {
	// Set the minecraft version.
	minecraft "com.mojang:minecraft:1.18.1"

	// Use mappings from maven.
	mappings "net.fabricmc:yarn:1.18.1+build.22:v2"

	// Use the offical mojang mappings
	mappings loom.officialMojangMappings()

	// Layered mappings using official mojang mappings and parchment.
	mappings loom.layered() {
		officialMojangMappings()
		// Use parchment mappings. NOTE: Parchment maven must be manually added. (https://maven.parchmentmc.org)
		parchment("org.parchmentmc.data:parchment-1.17.1:2021.09.05@zip")
	}

	// Remap a mod from maven and apply to gradle's implementation configuration
	// (Minor detail: it's not exactly applied *to* the configuration, but a clone of it intended for mod dependencies)
	modImplementation "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

	// Remap a mod from maven and apply to gradle's api configuration
	modApi "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

	// Remap a mod from maven and apply to gradle's compileOnly configuration
	modCompileOnly "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

	// Remap a mod from maven and apply to gradle's compileOnlyApi configuration
	modCompileOnlyApi "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

	// Remap a mod from maven and apply to gradle's runtimeOnly configuration
	modRuntimeOnly "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

	// Remap a mod from maven and apply to loom's localRuntime configuration.
	// Behaves like runtimeOnly but is not exposed in to dependents. A bit like testRuntimeOnly but for mods.
	modLocalRuntime "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

	// Include a mod jar in the remapped jar. None transitive.
	include "example:example-mod:1.1.1"

	// Include a none mod library jar in the remapped jar. A dummy mod will be generated. None transitive.
	include "example:example-lib:1.1.1"

	// Helper to aid with depending on a specific fabric api version.
	modImplementation fabricApi.module("fabric-api-base", "0.46.2+1.18")

	// Depend on a loom sub project by using the namedElements configuration.
	implementation project(path: ":name", configuration: "namedElements")
}

解决问题

Loom 或者 gradle 可能会由于缓存文件有问题而失败。运行 ./gradlew build –refresh-dependencies 将强制 gradle 和 loom 重新下载并重新创建所有文件。这可能需要几分钟,但是处理与缓存有关的问题时非常有用。

开发环境设置

Loom 旨在通过简单地在用户选择的 IDE 中设置工作区来开箱即用,因此背后做了很多事以创建带有 Minecraft 的开发环境:

  1. Downloads the client and server jar from official channels for the configured version of Minecraft.
  2. Merges the client and server jar to produce a merged jar with @Environment and @EnvironmentInterface annotations.
  3. Downloads the configured mappings.
  4. Remaps the merged jar with intermediary mappings to produce an intermediary jar.
  5. Remaps the merged jar with yarn mappings to produce a mapped jar.
  6. Optional: Decompiles the mapped jar to produce a mapped sources jar and linemap, and applies the linemap to the mapped jar.
  7. Adds dependencies of Minecraft.
  8. Downloads Minecraft assets.
  9. Processes and includes mod-augmented dependencies (and optionally extracts and remaps nested JARs).

缓存

  • ${GRADLE_HOME}/caches/fabric-loom: The user cache, a cache shared by all Loom projects for a user. Used to cache Minecraft assets, jars, merged jars, intermediary jars and mapped jars.
  • .gradle/loom-cache: The root project persistent cache, a cache shared by a project and its subprojects. Used to cache remapped mods as well as generated included mod JARs.
  • build/loom-cache: The root project build cache.
  • **/build/loom-cache: The (sub)project build cache.

依赖配置

  • minecraft: Defines the version of Minecraft to be used in the development environment.
  • mappings: Defines the mappings to be used in the development environment.
  • modCompile, modImplementation, modApi and modRuntime: Augmented variants of compile, implementation, api and runtime for mod dependencies. Will be remapped to match the mappings in the development environment and has any nested JARs removed. Nested JARs can optionally be extracted and remapped.
  • include: Declares a dependency that should be included as a jar-in-jar in the remapJar output. This dependency configuration is not transitive. For non-mod dependencies, Loom will generate a mod JAR with a fabric.mod.json using the name as the mod ID and the same version.

默认配置

  • Applies the following plugins: java, eclipse and idea.
  • Adds the following Maven repositories: Fabric https://maven.fabricmc.net/, Mojang https://libraries.minecraft.net/ and Maven Central.
  • Configures the idea extension to exclude directories .gradle, build, .idea and out, to download javadocs sources and to inherit output directories.
  • Configures the idea task to be finalized by the genIdeaWorkspace task.
  • Configures the eclipse task to be finalized by the genEclipseRuns task.
  • If an .idea folder exists in the root project, downloads assets (if not up-to-date) and installs run configurations in .idea/runConfigurations.
  • Adds net.fabricmc:fabric-mixin-compile-extensions and its dependencies with the annotationProcessor dependency configuration.
  • Configures all non-test JavaCompile tasks with configurations for the Mixin annotation processor.
  • Configures the remapJar task to output a JAR with the same name as the jar task output, then adds a “dev” classifier to the jar task.
  • Configures the remapSourcesJar task to process the sourcesJar task output if the task exists.
  • Adds the remapJar task and the remapSourcesJar task as dependencies of the build task.
  • Configures the remapJar task and the remapSourcesJar task to add their outputs as archives artifacts when executed.
  • For each MavenPublication (from the maven-publish plugin):
    • Manually appends dependencies to the POM for mod-augmented dependency configurations, provided the dependency configuration has a Maven scope.

All run configurations have the run directory ${projectDir}/run and the VM argument -Dfabric.development=true. The main classes for run configurations is usually defined by a fabric-installer.json file in the root of Fabric Loader's JAR file when it is included as a mod dependency, but the file can be defined by any mod dependency. If no such file is found, the main classes defaults to net.fabricmc.loader.launch.knot.KnotClient and net.fabricmc.loader.launch.knot.KnotServer.

The client run configuration is configured with –assetsIndex and –assetsDir program arguments pointing to the loom cache directory containing assets and the index file for the configured version of Minecraft. When running on OSX, the “-XstartOnFirstThread” VM argument is added.

zh_cn/documentation/fabric_loom.1647699848.txt.gz · Last modified: 2022/03/19 14:24 by solidblock