Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Jan 28, 2024
1 parent 560f96c commit d7580d3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -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<Integer> getIntermediaryVersion();

@Deprecated
Property<Boolean> useLFIntermediary();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

public class LegacyLoomingExtensionImpl implements LegacyLoomingExtensionAPI {
protected final Property<Integer> intermediaryVersion;

@Deprecated
protected final Property<Boolean> useLFIntermediary;

public LegacyLoomingExtensionImpl(Project project) {
Expand All @@ -16,6 +18,7 @@ public Property<Integer> getIntermediaryVersion() {
return this.intermediaryVersion;
}

@Deprecated
@Override
public Property<Boolean> useLFIntermediary() {
return this.useLFIntermediary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Map<String, String>> moduleVersionCache = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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()));
}
}

0 comments on commit d7580d3

Please sign in to comment.