Skip to content

Commit

Permalink
Migrate to mojmap + YALMM.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Oct 12, 2024
1 parent 14b2b19 commit d187df6
Show file tree
Hide file tree
Showing 66 changed files with 634 additions and 529 deletions.
124 changes: 121 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import net.fabricmc.loom.api.mappings.layered.MappingContext
import net.fabricmc.loom.api.mappings.layered.MappingLayer
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace
import net.fabricmc.loom.api.mappings.layered.spec.MappingsSpec
import net.fabricmc.loom.configuration.providers.mappings.intermediary.IntermediaryMappingLayer
import net.fabricmc.loom.configuration.providers.mappings.utils.DstNameFilterMappingVisitor
import net.fabricmc.loom.task.RemapJarTask
import net.fabricmc.loom.util.download.DownloadException
import net.fabricmc.mappingio.MappingVisitor
import net.fabricmc.mappingio.adapter.MappingDstNsReorder
import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch
import net.fabricmc.mappingio.format.proguard.ProGuardFileReader
import net.fabricmc.mappingio.tree.MemoryMappingTree
import java.io.IOException
import java.io.UncheckedIOException
import java.nio.file.Files
import java.nio.file.Path
import java.util.regex.Pattern

plugins {
id("fabric-loom").version("1.8.+")
Expand Down Expand Up @@ -36,14 +53,115 @@ repositories {
url = uri("https://maven.terraformersmc.com/releases")
}
maven {
name = "QuiltMC"
url = uri("https://maven.quiltmc.org/repository/release")
name = "Gegy"
url = uri("https://maven.gegy.dev/releases/")
}
}

// Based off Loom, this is required as the releases at the time of writing this buildscript have
// a flaw with the mapping layering preventing the usage of the usual MojangMappingLayer.
@Suppress("UnstableApiUsage")
internal data class MojangMappingLayer(
val clientMappings: Path, val serverMappings: Path, val nameSyntheticMembers: Boolean,
val intermediaryMappings: MemoryMappingTree, val logger: Logger
) : MappingLayer {
@Throws(IOException::class)
override fun visit(mappingVisitor: MappingVisitor) {
val mojmap = MemoryMappingTree()

// Filter out field names matching the pattern
val nameFilter = DstNameFilterMappingVisitor(mojmap, SYNTHETIC_NAME_PATTERN)

// Make official the source namespace
val nsSwitch = MappingSourceNsSwitch(if (nameSyntheticMembers) mojmap else nameFilter, MappingsNamespace.OFFICIAL.toString())

Files.newBufferedReader(clientMappings).use { clientBufferedReader ->
Files.newBufferedReader(serverMappings).use { serverBufferedReader ->
ProGuardFileReader.read(
clientBufferedReader,
MappingsNamespace.NAMED.toString(),
MappingsNamespace.OFFICIAL.toString(),
nsSwitch
)
ProGuardFileReader.read(
serverBufferedReader,
MappingsNamespace.NAMED.toString(),
MappingsNamespace.OFFICIAL.toString(),
nsSwitch
)
}
}

intermediaryMappings.accept(MappingDstNsReorder(mojmap, MappingsNamespace.INTERMEDIARY.toString()))

val switch = MappingSourceNsSwitch(MappingDstNsReorder(mappingVisitor, MappingsNamespace.NAMED.toString()), MappingsNamespace.INTERMEDIARY.toString(), true)
mojmap.accept(switch)
}

override fun getSourceNamespace(): MappingsNamespace {
return MappingsNamespace.INTERMEDIARY
}

override fun dependsOn(): List<Class<out MappingLayer?>> {
return listOf(IntermediaryMappingLayer::class.java)
}

companion object {
private val SYNTHETIC_NAME_PATTERN: Pattern = Pattern.compile("^(access|this|val\\\$this|lambda\\$.*)\\$[0-9]+$")
}
}

@Suppress("UnstableApiUsage")
internal data class MojangMappingsSpec(val nameSyntheticMembers: Boolean) : MappingsSpec<MojangMappingLayer?> {
override fun createLayer(context: MappingContext): MojangMappingLayer {
val versionInfo = context.minecraftProvider().versionInfo
val clientDownload = versionInfo.download(MANIFEST_CLIENT_MAPPINGS)
val serverDownload = versionInfo.download(MANIFEST_SERVER_MAPPINGS)

if (clientDownload == null) {
throw RuntimeException("Failed to find official mojang mappings for " + context.minecraftVersion())
}

val clientMappings = context.workingDirectory("mojang").resolve("client.txt")
val serverMappings = context.workingDirectory("mojang").resolve("server.txt")

try {
context.download(clientDownload.url())
.sha1(clientDownload.sha1())
.downloadPath(clientMappings)

context.download(serverDownload.url())
.sha1(serverDownload.sha1())
.downloadPath(serverMappings)
} catch (e: DownloadException) {
throw UncheckedIOException("Failed to download mappings", e)
}

return MojangMappingLayer(
clientMappings,
serverMappings,
nameSyntheticMembers,
context.intermediaryTree().get(),
context.logger
)
}

companion object {
// Keys in dependency manifest
private const val MANIFEST_CLIENT_MAPPINGS = "client_mappings"
private const val MANIFEST_SERVER_MAPPINGS = "server_mappings"
}
}

dependencies {
minecraft("com.mojang:minecraft:${mcVersion}")
mappings("org.quiltmc:quilt-mappings:${mcVersion}+build.${project.property("quilt_mappings")}:intermediary-v2")
@Suppress("UnstableApiUsage")
mappings(loom.layered {
addLayer(MojangMappingsSpec(false))
// Parchment is currently broken when used with the hacked mojmap layer due to remapping shenanigans.
//parchment("org.parchmentmc.data:parchment-${mcVersion}:${project.property("parchment_mappings")}@zip")
mappings("dev.lambdaurora:yalmm:${mcVersion}+build.${project.property("yalmm_mappings")}")
})
modImplementation("net.fabricmc:fabric-loader:${project.property("loader_version")}")

fabricModules.stream().map { fabricApi.module(it, project.property("fabric_api_version") as String) }.forEach {
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx2G

minecraft_version=1.21
quilt_mappings=2
minecraft_version=1.21.1
yalmm_mappings=2
loader_version=0.15.11

# Mod Properties
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/lambdaurora/spruceui/SpruceTexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

package dev.lambdaurora.spruceui;

import net.minecraft.text.Text;
import net.minecraft.network.chat.Text;

/**
* Represents a text utility class.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/lambdaurora/spruceui/SpruceTextures.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

package dev.lambdaurora.spruceui;

import net.minecraft.client.gui.screen.Screen;
import net.minecraft.util.Identifier;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.resources.Identifier;

/**
* Contains the identifiers of various useful textures.
Expand Down
36 changes: 20 additions & 16 deletions src/main/java/dev/lambdaurora/spruceui/Tooltip.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

import com.google.common.collect.Queues;
import dev.lambdaurora.spruceui.widget.SpruceWidget;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.tooltip.DefaultTooltipPositioner;
import net.minecraft.text.OrderedText;
import net.minecraft.text.StringVisitable;
import net.minecraft.client.gui.screens.inventory.tooltip.DefaultTooltipPositioner;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.util.FormattedCharSequence;
import org.jetbrains.annotations.ApiStatus;

import java.util.List;
Expand All @@ -35,17 +35,17 @@ public class Tooltip implements SprucePositioned {
private static boolean delayed = false;
private final int x;
private final int y;
private final List<OrderedText> tooltip;
private final List<FormattedCharSequence> tooltip;

public Tooltip(int x, int y, String tooltip, int parentWidth) {
this(x, y, StringVisitable.plain(tooltip), parentWidth);
this(x, y, FormattedText.of(tooltip), parentWidth);
}

public Tooltip(int x, int y, StringVisitable tooltip, int parentWidth) {
this(x, y, MinecraftClient.getInstance().textRenderer.wrapLines(tooltip, Math.max(parentWidth * 2 / 3, 200)));
public Tooltip(int x, int y, FormattedText tooltip, int parentWidth) {
this(x, y, Minecraft.getInstance().font.wrapLines(tooltip, Math.max(parentWidth * 2 / 3, 200)));
}

public Tooltip(int x, int y, List<OrderedText> tooltip) {
public Tooltip(int x, int y, List<FormattedCharSequence> tooltip) {
this.x = x;
this.y = y;
this.tooltip = tooltip;
Expand All @@ -55,11 +55,11 @@ public static Tooltip create(int x, int y, String tooltip, int parentWidth) {
return new Tooltip(x, y, tooltip, parentWidth);
}

public static Tooltip create(int x, int y, StringVisitable tooltip, int parentWidth) {
public static Tooltip create(int x, int y, FormattedText tooltip, int parentWidth) {
return new Tooltip(x, y, tooltip, parentWidth);
}

public static Tooltip create(int x, int y, List<OrderedText> tooltip) {
public static Tooltip create(int x, int y, List<FormattedCharSequence> tooltip) {
return new Tooltip(x, y, tooltip);
}

Expand Down Expand Up @@ -88,7 +88,7 @@ public boolean shouldRender() {
* @param graphics The GuiGraphics instance used to render.
*/
public void render(GuiGraphics graphics) {
graphics.drawTooltip(MinecraftClient.getInstance().textRenderer, this.tooltip, DefaultTooltipPositioner.INSTANCE, this.x, this.y);
graphics.drawTooltip(Minecraft.getInstance().font, this.tooltip, DefaultTooltipPositioner.INSTANCE, this.x, this.y);
}

/**
Expand All @@ -107,10 +107,15 @@ public void queue() {
* @param <T> the type of the widget
* @since 1.6.0
*/
public static <T extends Tooltipable & SpruceWidget> void queueFor(T widget, int mouseX, int mouseY, int tooltipTicks,
public static <T extends Tooltipable & SpruceWidget> void queueFor(
T widget,
int mouseX,
int mouseY,
int tooltipTicks,
IntConsumer tooltipTicksSetter,
long lastTick,
LongConsumer lastTickSetter) {
LongConsumer lastTickSetter
) {
if (widget.isVisible()) {
widget.getTooltip().ifPresent(tooltip -> {
long currentRender = System.currentTimeMillis();
Expand All @@ -125,8 +130,7 @@ public static <T extends Tooltipable & SpruceWidget> void queueFor(T widget, int
tooltipTicksSetter.accept(0);

if (!tooltip.getString().isEmpty() && tooltipTicks >= 45) {
var wrappedTooltipText = MinecraftClient.getInstance().textRenderer.wrapLines(
tooltip, Math.max(widget.getWidth() * 2 / 3, 200));
var wrappedTooltipText = Minecraft.getInstance().font.wrapLines(tooltip, Math.max(widget.getWidth() * 2 / 3, 200));
if (widget.isMouseHovered())
create(mouseX, mouseY, wrappedTooltipText).queue();
else if (widget.isFocused())
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/lambdaurora/spruceui/Tooltipable.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

package dev.lambdaurora.spruceui;

import net.minecraft.text.Text;
import net.minecraft.network.chat.Text;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import dev.lambdaurora.spruceui.widget.SpruceWidget;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.util.math.MatrixStack;

/**
* Represents a background which can be rendered on a widget.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
import dev.lambdaurora.spruceui.SpruceTextures;
import dev.lambdaurora.spruceui.widget.SpruceWidget;
import dev.lambdaurora.spruceui.widget.WithBorder;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.util.Identifier;
import net.minecraft.resources.Identifier;

/**
* Represents a background used for menus.
Expand All @@ -27,7 +27,7 @@
* @since 5.1.0
*/
public record MenuBackground(Identifier texture, Identifier inWorldTexture) implements Background {
private static final MinecraftClient CLIENT = MinecraftClient.getInstance();
private static final Minecraft CLIENT = Minecraft.getInstance();

public static final MenuBackground MENU_LIST = new MenuBackground(
SpruceTextures.MENU_LIST_BACKGROUND,
Expand Down Expand Up @@ -56,7 +56,7 @@ public void render(GuiGraphics graphics, SpruceWidget widget, int vOffset, int m
}

RenderSystem.enableBlend();
Identifier identifier = CLIENT.world == null ? this.inWorldTexture : this.texture;
Identifier identifier = CLIENT.level == null ? this.inWorldTexture : this.texture;
graphics.drawTexture(
identifier,
x, y,
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/dev/lambdaurora/spruceui/border/MenuBorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import com.mojang.blaze3d.systems.RenderSystem;
import dev.lambdaurora.spruceui.SpruceTextures;
import dev.lambdaurora.spruceui.widget.SpruceWidget;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.util.Identifier;
import net.minecraft.resources.Identifier;

/**
* Represents a typical menu border.
Expand All @@ -28,7 +28,7 @@
* @since 5.1.0
*/
public record MenuBorder(boolean top, boolean right, boolean bottom, boolean left) implements Border {
private static final MinecraftClient CLIENT = MinecraftClient.getInstance();
private static final Minecraft CLIENT = Minecraft.getInstance();
private static final int THICKNESS = 2;

public static final MenuBorder LIST = new MenuBorder(true, false, true, false);
Expand All @@ -39,7 +39,7 @@ public void render(GuiGraphics graphics, SpruceWidget widget, int mouseX, int mo
RenderSystem.enableBlend();

if (this.top) {
Identifier topTexture = CLIENT.world == null ? SpruceTextures.MENU_TOP_BORDER : SpruceTextures.INWORLD_MENU_TOP_BORDER;
Identifier topTexture = CLIENT.level == null ? SpruceTextures.MENU_TOP_BORDER : SpruceTextures.INWORLD_MENU_TOP_BORDER;

int width = widget.getWidth();

Expand All @@ -53,14 +53,14 @@ public void render(GuiGraphics graphics, SpruceWidget widget, int mouseX, int mo
}

if (this.top && this.right) {
Identifier cornerTexture = CLIENT.world == null ? SpruceTextures.MENU_TOP_RIGHT_BORDER : SpruceTextures.INWORLD_MENU_TOP_RIGHT_BORDER;
Identifier cornerTexture = CLIENT.level == null ? SpruceTextures.MENU_TOP_RIGHT_BORDER : SpruceTextures.INWORLD_MENU_TOP_RIGHT_BORDER;
graphics.drawTexture(cornerTexture,
widget.getEndX() - THICKNESS, widget.getY(), 0, 0, THICKNESS, THICKNESS, THICKNESS, THICKNESS
);
}

if (this.right) {
Identifier rightTexture = CLIENT.world == null ? SpruceTextures.MENU_RIGHT_BORDER : SpruceTextures.INWORLD_MENU_RIGHT_BORDER;
Identifier rightTexture = CLIENT.level == null ? SpruceTextures.MENU_RIGHT_BORDER : SpruceTextures.INWORLD_MENU_RIGHT_BORDER;

int y = widget.getY();
int height = widget.getHeight();
Expand All @@ -80,15 +80,15 @@ public void render(GuiGraphics graphics, SpruceWidget widget, int mouseX, int mo
}

if (this.bottom && this.right) {
Identifier cornerTexture = CLIENT.world == null
Identifier cornerTexture = CLIENT.level == null
? SpruceTextures.MENU_BOTTOM_RIGHT_BORDER : SpruceTextures.INWORLD_MENU_BOTTOM_RIGHT_BORDER;
graphics.drawTexture(cornerTexture,
widget.getEndX() - THICKNESS, widget.getEndY() - THICKNESS, 0, 0, THICKNESS, THICKNESS, THICKNESS, THICKNESS
);
}

if (this.bottom) {
Identifier bottomTexture = CLIENT.world == null ? SpruceTextures.MENU_BOTTOM_BORDER : SpruceTextures.INWORLD_MENU_BOTTOM_BORDER;
Identifier bottomTexture = CLIENT.level == null ? SpruceTextures.MENU_BOTTOM_BORDER : SpruceTextures.INWORLD_MENU_BOTTOM_BORDER;

int width = widget.getWidth();

Expand Down
Loading

0 comments on commit d187df6

Please sign in to comment.