Skip to content

Commit

Permalink
M1 Fix (Use LegacyFabric LWJGL + Gambac) (#2)
Browse files Browse the repository at this point in the history
* Fix

* Update LegacyRepositoryHandler.java

* Update LWJGL2LibraryProcessor.java

* Delete dead code

* Readd babric maven, filter legacyfabric one to just lwjgl

* Add gambac

* Yeet unused imports
  • Loading branch information
calmilamsy authored Jul 1, 2024
1 parent 37478b6 commit 850ad94
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 12 deletions.
12 changes: 12 additions & 0 deletions src/main/java/babric/BabricExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package babric;

import org.gradle.api.Project;
import org.gradle.api.provider.Property;

public class BabricExtension {
public final Property<Boolean> disableM1Fixes;

public BabricExtension(Project project) {
disableM1Fixes = project.getObjects().property(Boolean.class).convention(false);
}
}
12 changes: 11 additions & 1 deletion src/main/java/babric/BabricLoomPlugin.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package babric;

import babric.mappings.BabricIntermediaryProvider;
import babric.processor.GambacLibraryProcessor;
import babric.processor.LWJGL2LibraryProcessor;
import babric.processor.NestFixingJarProcessor;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.api.LoomGradleExtensionAPI;
import net.fabricmc.loom.configuration.providers.minecraft.library.LibraryProcessorManager;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.plugins.PluginAware;
import org.gradle.api.provider.ListProperty;

import java.util.Map;
import java.util.Objects;
Expand All @@ -17,17 +21,23 @@ public class BabricLoomPlugin implements Plugin<PluginAware> {
@Override
public void apply(PluginAware target) {
target.apply(Map.of("plugin", "fabric-loom"));
target.getPlugins().apply(BabricRepositoryHandler.class);

if (target instanceof Project project) applyProject(project);
}

private void applyProject(Project project) {
project.getLogger().lifecycle("Babric loom: " + VERSION);

BabricRepositoryHandler.declareRepositories(project.getRepositories());
ListProperty<LibraryProcessorManager.LibraryProcessorFactory> libraryProcessors = LoomGradleExtension.get(project).getLibraryProcessors();
libraryProcessors.add(LWJGL2LibraryProcessor::new);

LoomGradleExtensionAPI extension = (LoomGradleExtensionAPI) project.getExtensions().getByName("loom");

BabricExtension babricExtension = project.getExtensions().create("babric", BabricExtension.class, project);

libraryProcessors.add((platform, libraryContext) -> new GambacLibraryProcessor(platform, libraryContext, extension, project, babricExtension));

extension.setIntermediateMappingsProvider(BabricIntermediaryProvider.class, provider -> {
provider.getIntermediaryUrl().set(extension.getIntermediaryUrl());
provider.getRefreshDeps().set(project.provider(() -> LoomGradleExtension.get(project).refreshDeps()));
Expand Down
50 changes: 44 additions & 6 deletions src/main/java/babric/BabricRepositoryHandler.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,54 @@
/**
Swiped from https://github.com/Legacy-Fabric/legacy-looming/blob/dev/1.7/src/main/java/net/legacyfabric/legacylooming/LegacyRepositoryHandler.java with permission.
**/

package babric;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.dsl.RepositoryHandler;
import org.gradle.api.initialization.Settings;
import org.gradle.api.invocation.Gradle;
import org.gradle.api.plugins.PluginAware;
import org.jetbrains.annotations.NotNull;

public class BabricRepositoryHandler implements Plugin<PluginAware> {
@Override
public void apply(@NotNull PluginAware pluginAware) {
if (pluginAware instanceof Settings settings) {
declareRepositories(settings.getDependencyResolutionManagement().getRepositories());

settings.getGradle().getPluginManager().apply(BabricRepositoryHandler.class);
} else if (pluginAware instanceof Project project) {
if (project.getGradle().getPlugins().hasPlugin(BabricRepositoryHandler.class)) {
return;
}

declareRepositories(project.getRepositories());
} else if (pluginAware instanceof Gradle) {
return;
} else {
throw new IllegalArgumentException("Expected target to be a Project or Settings, but was a " + pluginAware.getClass());
}
}

public class BabricRepositoryHandler {
protected static void declareRepositories(RepositoryHandler repositories) {
private void declareRepositories(RepositoryHandler repositories) {
repositories.maven(repo -> {
repo.setName("Legacy Fabric");
repo.setUrl(Constants.LEGACYFABRIC_MAVEN);
repo.content(content -> {
content.includeGroup("org.lwjgl.lwjgl"); // Only for LWJGL
});
});
repositories.maven(repo -> { // No filter, all parts of this repo are used in a project.
repo.setName("Babric");
repo.setUrl(Constants.MAVEN);
repo.setUrl(Constants.BABRIC_MAVEN);
});
repositories.maven(repo -> { // No filter, all parts of this repo are used in a project.
repo.setName("Modrinth");
repo.setUrl(Constants.MODRINTH_MAVEN);
repo.content(content -> {
content.includeGroup("babric");
content.includeGroupByRegex("babric.*");
content.includeGroup("org.lwjgl.lwjgl");
content.includeModule("maven.modrinth", "gambac"); // Allow only gambac, don't let folk rely on this adding MR for them.
});
});
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/babric/Constants.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package babric;

public class Constants {
public static final String MAVEN = "https://maven.glass-launcher.net/babric/";
public static final String LEGACYFABRIC_MAVEN = "https://repo.legacyfabric.net/repository/legacyfabric/";
public static final String BABRIC_MAVEN = "https://maven.glass-launcher.net/babric/";
public static final String MODRINTH_MAVEN = "https://api.modrinth.com/maven/";
}
67 changes: 67 additions & 0 deletions src/main/java/babric/processor/GambacLibraryProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
Swiped from https://github.com/Legacy-Fabric/legacy-looming/blob/dev/1.7/src/main/java/net/legacyfabric/legacylooming/providers/LWJGL2LibraryProcessor.java with permission.
**/

package babric.processor;

import babric.BabricExtension;
import net.fabricmc.loom.api.LoomGradleExtensionAPI;
import net.fabricmc.loom.configuration.providers.minecraft.library.Library;
import net.fabricmc.loom.configuration.providers.minecraft.library.LibraryContext;
import net.fabricmc.loom.configuration.providers.minecraft.library.LibraryProcessor;
import net.fabricmc.loom.util.Platform;
import org.gradle.api.Project;
import org.gradle.api.artifacts.dsl.RepositoryHandler;

import java.util.function.Consumer;
import java.util.function.Predicate;

public class GambacLibraryProcessor extends LibraryProcessor {
public static final String VERSION = "1.1.0";
private boolean applied = false;
private final LoomGradleExtensionAPI loom;
private final Project project;
private final BabricExtension babricExtension;

public GambacLibraryProcessor(Platform platform, LibraryContext context, LoomGradleExtensionAPI loom, Project project, BabricExtension babricExtension) {
super(platform, context);
this.loom = loom;
this.project = project;
this.babricExtension = babricExtension;
}

@Override
public ApplicationResult getApplicationResult() {
if (babricExtension.disableM1Fixes.get()) {
return ApplicationResult.DONT_APPLY;
}
if (!loom.getMinecraftVersion().get().equals("b1.7.3")) {

project.getLogger().warn("M1 fix being ignored. Unsupported minecraft version.");
return ApplicationResult.DONT_APPLY;
}

return ApplicationResult.MUST_APPLY;
}

@Override
public Predicate<Library> apply(Consumer<Library> dependencyConsumer) {
if (!applied) {
applied = true;

dependencyConsumer.accept(Library.fromMaven("maven.modrinth:gambac:" + VERSION, Library.Target.LOCAL_MOD));
}

return ALLOW_ALL;
}

@Override
public void applyRepositories(RepositoryHandler repositories) {
repositories.exclusiveContent(repository -> {
repository.forRepositories(repositories.findByName("Modrinth"));
repository.filter(filter -> {
filter.includeModule("maven.modrinth", "gambac"); // Allow only gambac, don't let folk rely on this adding MR for them.
});
});
}
}
76 changes: 76 additions & 0 deletions src/main/java/babric/processor/LWJGL2LibraryProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
Swiped from https://github.com/Legacy-Fabric/legacy-looming/blob/dev/1.7/src/main/java/net/legacyfabric/legacylooming/providers/LWJGL2LibraryProcessor.java with permission.
**/

package babric.processor;

import net.fabricmc.loom.configuration.providers.minecraft.library.Library;
import net.fabricmc.loom.configuration.providers.minecraft.library.LibraryContext;
import net.fabricmc.loom.configuration.providers.minecraft.library.LibraryProcessor;
import net.fabricmc.loom.util.Platform;
import org.gradle.api.artifacts.dsl.RepositoryHandler;

import java.util.function.Consumer;
import java.util.function.Predicate;

public class LWJGL2LibraryProcessor extends LibraryProcessor {
public static final String VERSION = "2.9.4+legacyfabric.8";
private boolean applied = false;
public LWJGL2LibraryProcessor(Platform platform, LibraryContext context) {
super(platform, context);
}

@Override
public ApplicationResult getApplicationResult() {
if (!context.usesLWJGL3()) {

return ApplicationResult.MUST_APPLY;
}

return ApplicationResult.DONT_APPLY;
}

@Override
public Predicate<Library> apply(Consumer<Library> dependencyConsumer) {
return library -> {
if (!applied) {
applied = true;
final Library[] libs = new Library[]{
Library.fromMaven("org.lwjgl.lwjgl:lwjgl-platform:" + VERSION + ":" + getNativeClassifier(), Library.Target.NATIVES),
Library.fromMaven("org.lwjgl.lwjgl:lwjgl_util:" + VERSION, Library.Target.COMPILE),
Library.fromMaven("org.lwjgl.lwjgl:lwjgl:" + VERSION, Library.Target.COMPILE)
};

for (Library lib : libs) {
dependencyConsumer.accept(lib);
}
}

return !library.group().equals("org.lwjgl.lwjgl");
};
}

@Override
public void applyRepositories(RepositoryHandler repositories) {
repositories.exclusiveContent(repository -> {
repository.forRepositories(repositories.findByName("Legacy Fabric"));
repository.filter(filter -> {
filter.includeGroup("org.lwjgl.lwjgl");
});
});
}

public static String getNativeClassifier() {
switch (Platform.CURRENT.getOperatingSystem()) {
case MAC_OS -> {
return "natives-osx";
}
case LINUX -> {
return "natives-linux";
}
default -> {
return "natives-windows";
}
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/babric/processor/NestFixingJarProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import net.fabricmc.loom.api.processor.SpecContext;
import net.fabricmc.loom.util.fmj.FabricModJson;
import net.fabricmc.stitch.commands.CommandFixNesting;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
Expand All @@ -25,7 +25,7 @@ public void processJar(Path path, Spec spec, ProcessorContext processorContext)
}

@Override
public String getName() {
public @NotNull String getName() {
return "babric:fix-nesting";
}

Expand Down
8 changes: 6 additions & 2 deletions test-mod/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ loom {
intermediaryUrl.set("https://maven.glass-launcher.net/babric/babric/intermediary/%1\$s/intermediary-%1\$s-v2.jar")
}

babric {
// disableM1Fixes.set(true) // Uncomment this to disable gambac (M1 colours fix.)
}

repositories {
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.
maven { url = "https://maven.glass-launcher.net/snapshots"}
maven{url = "https://maven.glass-launcher.net/releases"}
maven { url = "https://maven.glass-launcher.net/snapshots" }
maven { url = "https://maven.glass-launcher.net/releases" }
}

dependencies {
Expand Down

0 comments on commit 850ad94

Please sign in to comment.