diff --git a/src/main/java/net/legacyfabric/legacylooming/LegacyLoomingExtensionAPI.java b/src/main/java/net/legacyfabric/legacylooming/LegacyLoomingExtensionAPI.java index a66ba72..d3eea93 100644 --- a/src/main/java/net/legacyfabric/legacylooming/LegacyLoomingExtensionAPI.java +++ b/src/main/java/net/legacyfabric/legacylooming/LegacyLoomingExtensionAPI.java @@ -1,9 +1,15 @@ package net.legacyfabric.legacylooming; +import org.gradle.api.Project; import org.gradle.api.provider.Property; public interface LegacyLoomingExtensionAPI { + static LegacyLoomingExtensionAPI get(Project project) { + return project.getExtensions().findByType(LegacyLoomingExtensionAPI.class); + } + Property getIntermediaryVersion(); + @Deprecated Property useLFIntermediary(); } diff --git a/src/main/java/net/legacyfabric/legacylooming/LegacyLoomingExtensionImpl.java b/src/main/java/net/legacyfabric/legacylooming/LegacyLoomingExtensionImpl.java index ca5660c..2f4f925 100644 --- a/src/main/java/net/legacyfabric/legacylooming/LegacyLoomingExtensionImpl.java +++ b/src/main/java/net/legacyfabric/legacylooming/LegacyLoomingExtensionImpl.java @@ -5,6 +5,8 @@ public class LegacyLoomingExtensionImpl implements LegacyLoomingExtensionAPI { protected final Property intermediaryVersion; + + @Deprecated protected final Property useLFIntermediary; public LegacyLoomingExtensionImpl(Project project) { @@ -16,6 +18,7 @@ public Property getIntermediaryVersion() { return this.intermediaryVersion; } + @Deprecated @Override public Property useLFIntermediary() { return this.useLFIntermediary; diff --git a/src/main/java/net/legacyfabric/legacylooming/LegacyLoomingGradlePlugin.java b/src/main/java/net/legacyfabric/legacylooming/LegacyLoomingGradlePlugin.java index 11e26f1..a460a84 100644 --- a/src/main/java/net/legacyfabric/legacylooming/LegacyLoomingGradlePlugin.java +++ b/src/main/java/net/legacyfabric/legacylooming/LegacyLoomingGradlePlugin.java @@ -32,59 +32,59 @@ public void apply(PluginAware target) { if (target instanceof Project project) { project.getLogger().lifecycle("Legacy Looming: " + VERSION); - if (!OperatingSystem.CURRENT_OS.isWindows()) { - LoomGradleExtension.get(project).getLibraryProcessors().add(LWJGL2LibraryProcessor::new); - } + LoomGradleExtension loom = LoomGradleExtension.get(project); - var extension = project.getExtensions().create(LegacyLoomingExtensionAPI.class, "legacyLooming", LegacyLoomingExtensionImpl.class, project); + addLibraryProcessors(loom); - project.getExtensions().getByType(LoomGradleExtensionAPI.class) - .setIntermediateMappingsProvider(LegacyFabricIntermediaryMappingsProvider.class, provider -> { - provider.getIntermediaryUrl() - .convention(extension.useLFIntermediary().map( val -> { - if (val) { - return extension.getIntermediaryVersion().map(Constants::getIntermediaryURL); - } else { - return project.provider(() -> "https://maven.fabricmc.net/net/fabricmc/intermediary/%1$s/intermediary-%1$s-v2.jar"); - } - }).map(Provider::get)) - .finalizeValueOnRead(); - - provider.getNameProperty() - .convention(extension.getIntermediaryVersion().map(Constants::getIntermediaryName)) - .finalizeValueOnRead(); - - provider.getRefreshDeps().set(project.provider(() -> LoomGradleExtension.get(project).refreshDeps())); - }); + var extension = project.getExtensions().create(LegacyLoomingExtensionAPI.class, "legacyLooming", LegacyLoomingExtensionImpl.class, project); project.getExtensions().create("legacy", LegacyUtilsExtension.class, project); - project.getTasks().configureEach(task -> { - if (task instanceof AbstractRemapJarTask remapJarTask) { - remapJarTask.doLast(task1 -> { - try { - ZipUtils.transform(remapJarTask.getArchiveFile().get().getAsFile().toPath(), Map.of(MANIFEST_PATH, bytes -> { - var manifest = new Manifest(new ByteArrayInputStream(bytes)); - - var attributes = manifest.getMainAttributes(); - attributes.putValue(Constants.VERSION_PROPERTY, VERSION); - attributes.putValue(Constants.INTERMEDIARY_PROPERTY, extension.getIntermediaryVersion().get().toString()); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - manifest.write(out); - return out.toByteArray(); - })); - } catch (IOException e) { - throw new RuntimeException(e); - } - }); - } - }); - - // override loom's migrateMappings to fix issues - var migrateTask = project.getTasks().replace("migrateMappings", MigrateLegacyMappingsTask.class); - migrateTask.setDescription("Migrates mappings to a new version."); - migrateTask.getOutputs().upToDateWhen(o -> false); + setIntermediary(project, extension, loom); + setManifestAttributes(project, extension); + overrideMigrateTask(project); } } + + private static void addLibraryProcessors(LoomGradleExtension loom) { + if (!OperatingSystem.CURRENT_OS.isWindows()) { + loom.getLibraryProcessors().add(LWJGL2LibraryProcessor::new); + } + } + + private static void setIntermediary(Project project, LegacyLoomingExtensionAPI api, LoomGradleExtensionAPI loom) { + loom.setIntermediateMappingsProvider(LegacyFabricIntermediaryMappingsProvider.class, + provider -> provider.configure(project, api, loom)); + } + + private static void setManifestAttributes(Project project, LegacyLoomingExtensionAPI extension) { + project.getTasks().configureEach(task -> { + if (task instanceof AbstractRemapJarTask remapJarTask) { + remapJarTask.doLast(task1 -> { + try { + ZipUtils.transform(remapJarTask.getArchiveFile().get().getAsFile().toPath(), Map.of(MANIFEST_PATH, bytes -> { + var manifest = new Manifest(new ByteArrayInputStream(bytes)); + + var attributes = manifest.getMainAttributes(); + attributes.putValue(Constants.VERSION_PROPERTY, VERSION); + attributes.putValue(Constants.INTERMEDIARY_PROPERTY, extension.getIntermediaryVersion().get().toString()); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + manifest.write(out); + return out.toByteArray(); + })); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } + }); + } + + private static void overrideMigrateTask(Project project) { + // override loom's migrateMappings to fix issues + var migrateTask = project.getTasks().replace("migrateMappings", MigrateLegacyMappingsTask.class); + migrateTask.setDescription("Migrates mappings to a new version."); + migrateTask.getOutputs().upToDateWhen(o -> false); + } } diff --git a/src/main/java/net/legacyfabric/legacylooming/LegacyUtilsExtension.java b/src/main/java/net/legacyfabric/legacylooming/LegacyUtilsExtension.java index 609e8c8..0a70602 100644 --- a/src/main/java/net/legacyfabric/legacylooming/LegacyUtilsExtension.java +++ b/src/main/java/net/legacyfabric/legacylooming/LegacyUtilsExtension.java @@ -21,7 +21,7 @@ public class LegacyUtilsExtension { public LegacyUtilsExtension(Project project) { this.project = project; - this.extension = this.project.getExtensions().findByType(LegacyLoomingExtensionAPI.class); + this.extension = LegacyLoomingExtensionAPI.get(project); } private static final HashMap> moduleVersionCache = new HashMap<>(); diff --git a/src/main/java/net/legacyfabric/legacylooming/providers/LegacyFabricIntermediaryMappingsProvider.java b/src/main/java/net/legacyfabric/legacylooming/providers/LegacyFabricIntermediaryMappingsProvider.java index 3408059..281e261 100644 --- a/src/main/java/net/legacyfabric/legacylooming/providers/LegacyFabricIntermediaryMappingsProvider.java +++ b/src/main/java/net/legacyfabric/legacylooming/providers/LegacyFabricIntermediaryMappingsProvider.java @@ -1,14 +1,40 @@ package net.legacyfabric.legacylooming.providers; +import net.fabricmc.loom.LoomGradleExtension; +import net.fabricmc.loom.api.LoomGradleExtensionAPI; import net.fabricmc.loom.configuration.providers.mappings.IntermediaryMappingsProvider; -import org.gradle.api.provider.Property; +import net.legacyfabric.legacylooming.Constants; +import net.legacyfabric.legacylooming.LegacyLoomingExtensionAPI; +import org.gradle.api.Project; import org.jetbrains.annotations.NotNull; public abstract class LegacyFabricIntermediaryMappingsProvider extends IntermediaryMappingsProvider { - public abstract Property getNameProperty(); + private String name; + private String defaultUrl = ""; @Override public @NotNull String getName() { - return this.getNameProperty().get(); + String defaultUrl = this.defaultUrl; + String url = getIntermediaryUrl().get(); + + if (!defaultUrl.equals(url)) { + // make sure the name is changed when the user defines a + // custom intermediary url, to ensure the default cache + // file does not get corrupted with other intermediaries + return name += "-" + Integer.toHexString(url.hashCode()); + } + + return this.name; + } + + public void configure(Project project, LegacyLoomingExtensionAPI api, LoomGradleExtensionAPI loom) { + this.name = api.getIntermediaryVersion().map(Constants::getIntermediaryName).get(); + this.defaultUrl = loom.getIntermediaryUrl().get(); + + this.getIntermediaryUrl() + .convention(api.getIntermediaryVersion().map(Constants::getIntermediaryURL)) + .finalizeValueOnRead(); + + this.getRefreshDeps().set(project.provider(() -> LoomGradleExtension.get(project).refreshDeps())); } }