Skip to content

Commit

Permalink
Refactor field migration into a more generalized migrator + add metho… (
Browse files Browse the repository at this point in the history
#209)

* Refactor field migration into a more generalized migrator + add method inheritance migrator

* Fix typo and clean up the method

* I can't spell
  • Loading branch information
shedaniel authored May 7, 2024
1 parent ff3546e commit 52b59fe
Show file tree
Hide file tree
Showing 5 changed files with 442 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.nio.file.StandardOpenOption;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -52,38 +53,25 @@

import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
import net.fabricmc.loom.configuration.providers.mappings.MappingConfiguration;
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftProvider;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.loom.util.ThreadingUtils;
import net.fabricmc.loom.util.service.SharedServiceManager;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.format.tiny.Tiny2FileWriter;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.mappingio.tree.MappingTreeView;
import net.fabricmc.mappingio.tree.MemoryMappingTree;

public final class FieldMigratedMappingConfiguration extends MappingConfiguration {
public final class FieldMappingsMigrator implements MappingsMigrator {
private List<Map.Entry<FieldMember, String>> migratedFields = new ArrayList<>();
public Path migratedFieldsCache;
private Path rawTinyMappings;
private Path rawTinyMappingsWithSrg;
private Path rawTinyMappingsWithMojang;

public FieldMigratedMappingConfiguration(String mappingsIdentifier, Path mappingsWorkingDir) {
super(mappingsIdentifier, mappingsWorkingDir);
}

@Override
protected void setup(Project project, SharedServiceManager serviceManager, MinecraftProvider minecraftProvider, Path inputJar) throws IOException {
final Path forgeCache = ForgeProvider.getForgeCache(project);
Files.createDirectories(forgeCache);
migratedFieldsCache = forgeCache.resolve("migrated-fields.json");
public long setup(Project project, MinecraftProvider minecraftProvider, Path cache, Path rawMappings, boolean hasSrg, boolean hasMojang) throws IOException {
migratedFieldsCache = cache.resolve("migrated-fields.json");
migratedFields.clear();

if (minecraftProvider.refreshDeps()) {
Files.deleteIfExists(migratedFieldsCache);
} else if (Files.exists(migratedFieldsCache)) {
if (!minecraftProvider.refreshDeps() && Files.exists(migratedFieldsCache)) {
try (BufferedReader reader = Files.newBufferedReader(migratedFieldsCache)) {
Map<String, String> map = new Gson().fromJson(reader, new TypeToken<Map<String, String>>() {
}.getType());
Expand All @@ -93,80 +81,57 @@ protected void setup(Project project, SharedServiceManager serviceManager, Minec
migratedFields.add(new AbstractMap.SimpleEntry<>(new FieldMember(split[0], split[1]), newDescriptor));
});
}
}
} else {
Files.deleteIfExists(migratedFieldsCache);
migratedFields.clear();

super.setup(project, serviceManager, minecraftProvider, inputJar);
}
if (hasSrg) {
migratedFields.addAll(generateNewFieldMigration(project, MinecraftPatchedProvider.get(project).getMinecraftPatchedIntermediateJar(), MappingsNamespace.SRG.toString(), rawMappings).entrySet());
} else if (hasMojang) {
migratedFields.addAll(generateNewFieldMigration(project, MinecraftPatchedProvider.get(project).getMinecraftPatchedIntermediateJar(), MappingsNamespace.MOJANG.toString(), rawMappings).entrySet());
}

public static String createForgeMappingsIdentifier(LoomGradleExtension extension, String mappingsName, String version, String classifier, String minecraftVersion) {
final String base = FieldMigratedMappingConfiguration.createMappingsIdentifier(mappingsName, version, classifier, minecraftVersion);
final String platform = extension.getPlatform().get().id();
final String forgeVersion = extension.getForgeProvider().getVersion().getCombined();
return base + "-" + platform + "-" + forgeVersion;
Map<String, String> map = new HashMap<>();
migratedFields.forEach(entry -> {
map.put(entry.getKey().owner + "#" + entry.getKey().field, entry.getValue());
});
Files.writeString(migratedFieldsCache, new Gson().toJson(map));
}

this.migratedFields.sort(Comparator.comparing(entry -> entry.getKey().owner + "#" + entry.getKey().field));
return migratedFields.hashCode();
}

@Override
protected void manipulateMappings(Project project, Path mappingsJar) throws IOException {
public void migrate(Project project, List<MappingsEntry> entries) {
Stopwatch stopwatch = Stopwatch.createStarted();
LoomGradleExtension extension = LoomGradleExtension.get(project);
this.rawTinyMappings = tinyMappings;
this.rawTinyMappingsWithSrg = tinyMappingsWithSrg;
this.rawTinyMappingsWithMojang = tinyMappingsWithMojang;

tinyMappings = mappingsWorkingDir().resolve("mappings-field-migrated.tiny");
tinyMappingsWithSrg = mappingsWorkingDir().resolve("mappings-srg-field-migrated.tiny");
tinyMappingsWithMojang = mappingsWorkingDir().resolve("mappings-mojang-field-migrated.tiny");

try {
updateFieldMigration(project, extension.isNeoForge(), extension.shouldGenerateSrgTiny());
updateFieldMigration(project, entries);
} catch (IOException e) {
throw new UncheckedIOException(e);
}

project.getLogger().info(":migrated {} fields in " + stopwatch.stop(), extension.getPlatform().get().id());
}

public void updateFieldMigration(Project project, boolean hasMojang, boolean hasSrg) throws IOException {
if (!Files.exists(migratedFieldsCache)) {
migratedFields.clear();

if (hasSrg) {
migratedFields.addAll(generateNewFieldMigration(project, MinecraftPatchedProvider.get(project).getMinecraftPatchedIntermediateJar(), MappingsNamespace.SRG.toString(), rawTinyMappingsWithSrg).entrySet());
} else if (hasMojang) {
migratedFields.addAll(generateNewFieldMigration(project, MinecraftPatchedProvider.get(project).getMinecraftPatchedIntermediateJar(), MappingsNamespace.MOJANG.toString(), rawTinyMappingsWithMojang).entrySet());
}
public void updateFieldMigration(Project project, List<MappingsEntry> entries) throws IOException {
Table<String, String, String> fieldDescriptorMap = HashBasedTable.create();

Map<String, String> map = new HashMap<>();
migratedFields.forEach(entry -> {
map.put(entry.getKey().owner + "#" + entry.getKey().field, entry.getValue());
});
Files.writeString(migratedFieldsCache, new Gson().toJson(map));
Files.deleteIfExists(tinyMappings);
Files.deleteIfExists(tinyMappingsWithSrg);
Files.deleteIfExists(tinyMappingsWithMojang);
for (Map.Entry<FieldMember, String> entry : migratedFields) {
fieldDescriptorMap.put(entry.getKey().owner, entry.getKey().field, entry.getValue());
}

if (Files.notExists(tinyMappings) || (hasSrg && Files.notExists(tinyMappingsWithSrg)) || (hasMojang && Files.notExists(tinyMappingsWithMojang))) {
Table<String, String, String> fieldDescriptorMap = HashBasedTable.create();

for (Map.Entry<FieldMember, String> entry : migratedFields) {
fieldDescriptorMap.put(entry.getKey().owner, entry.getKey().field, entry.getValue());
}

injectMigration(project, fieldDescriptorMap, rawTinyMappings, tinyMappings);

if (hasSrg) {
injectMigration(project, fieldDescriptorMap, rawTinyMappingsWithSrg, tinyMappingsWithSrg);
} else if (hasMojang) {
injectMigration(project, fieldDescriptorMap, rawTinyMappingsWithMojang, tinyMappingsWithMojang);
}
for (MappingsEntry entry : entries) {
injectMigration(project, fieldDescriptorMap, entry.path());
}
}

private static void injectMigration(Project project, Table<String, String, String> fieldDescriptorMap, Path source, Path out) throws IOException {
private static void injectMigration(Project project, Table<String, String, String> fieldDescriptorMap, Path path) throws IOException {
MemoryMappingTree mappings = new MemoryMappingTree();

try (BufferedReader reader = Files.newBufferedReader(source)) {
try (BufferedReader reader = Files.newBufferedReader(path)) {
MappingReader.read(reader, mappings);
}

Expand All @@ -186,7 +151,7 @@ private static void injectMigration(Project project, Table<String, String, Strin
}
}

try (Writer writer = Files.newBufferedWriter(out, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
try (Writer writer = Files.newBufferedWriter(path, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
mappings.accept(new Tiny2FileWriter(writer, false));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2024 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package net.fabricmc.loom.configuration.providers.forge;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;

import com.google.common.base.Stopwatch;
import org.gradle.api.Project;

import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.configuration.providers.mappings.MappingConfiguration;

public final class ForgeMigratedMappingConfiguration extends MappingConfiguration {
private final List<MappingsMigrator> migrators = List.of(new FieldMappingsMigrator(), new MethodInheritanceMappingsMigrator());
private Path hashPath;
private Path rawTinyMappings;
private Path rawTinyMappingsWithSrg;
private Path rawTinyMappingsWithMojang;
private long hash;

public ForgeMigratedMappingConfiguration(String mappingsIdentifier, Path mappingsWorkingDir) {
super(mappingsIdentifier, mappingsWorkingDir);
}

@Override
protected void manipulateMappings(Project project, Path mappingsJar) throws IOException {
LoomGradleExtension extension = LoomGradleExtension.get(project);
final Path forgeCache = ForgeProvider.getForgeCache(project);
Files.createDirectories(forgeCache);

boolean hasSrg = extension.shouldGenerateSrgTiny();
boolean hasMojang = extension.isNeoForge();

this.hashPath = forgeCache.resolve("mappings-migrated.hash");
this.hash = 1;

this.rawTinyMappings = this.tinyMappings;
this.rawTinyMappingsWithSrg = this.tinyMappingsWithSrg;
this.rawTinyMappingsWithMojang = this.tinyMappingsWithMojang;
Path rawTinyMappingsWithNs = hasSrg ? this.rawTinyMappingsWithSrg : hasMojang ? this.rawTinyMappingsWithMojang : this.rawTinyMappings;

this.tinyMappings = mappingsWorkingDir().resolve("mappings-migrated.tiny");
this.tinyMappingsWithSrg = mappingsWorkingDir().resolve("mappings-srg-migrated.tiny");
this.tinyMappingsWithMojang = mappingsWorkingDir().resolve("mappings-mojang-migrated.tiny");
Path tinyMappingsWithNs = hasSrg ? this.tinyMappingsWithSrg : hasMojang ? this.tinyMappingsWithMojang : this.tinyMappings;

for (MappingsMigrator migrator : this.migrators) {
hash = hash * 31 + migrator.setup(project, extension.getMinecraftProvider(), forgeCache, rawTinyMappingsWithNs, hasSrg, hasMojang);
}

if (!isOutdated(extension, hasSrg, hasMojang)) {
project.getLogger().info(":manipulated {} mappings are up to date", extension.getPlatform().get().id());
return;
}

Stopwatch stopwatch = Stopwatch.createStarted();
Files.copy(this.rawTinyMappings, this.tinyMappings, StandardCopyOption.REPLACE_EXISTING);
Files.copy(rawTinyMappingsWithNs, tinyMappingsWithNs, StandardCopyOption.REPLACE_EXISTING);

Files.writeString(this.hashPath, Long.toString(this.hash), StandardCharsets.UTF_8);

for (MappingsMigrator migrator : this.migrators) {
Path path = Files.createTempFile("mappings-working", ".tiny");
Path pathWithNs = Files.createTempFile("mappings-working-ns", ".tiny");
Files.copy(this.tinyMappings, path, StandardCopyOption.REPLACE_EXISTING);
Files.copy(tinyMappingsWithNs, pathWithNs, StandardCopyOption.REPLACE_EXISTING);

List<MappingsMigrator.MappingsEntry> entries = List.of(new MappingsMigrator.MappingsEntry(path), new MappingsMigrator.MappingsEntry(pathWithNs));
migrator.migrate(project, entries);

Files.copy(path, this.tinyMappings, StandardCopyOption.REPLACE_EXISTING);
Files.copy(pathWithNs, tinyMappingsWithNs, StandardCopyOption.REPLACE_EXISTING);
Files.deleteIfExists(path);
Files.deleteIfExists(pathWithNs);
}

project.getLogger().info(":manipulated {} mappings in " + stopwatch.stop(), extension.getPlatform().get().id());
}

private boolean isOutdated(LoomGradleExtension extension, boolean hasSrg, boolean hasMojang) throws IOException {
if (extension.refreshDeps()) return true;
if (Files.notExists(this.tinyMappings)) return true;
if (hasSrg && Files.notExists(this.tinyMappingsWithSrg)) return true;
if (hasMojang && Files.notExists(this.tinyMappingsWithMojang)) return true;
if (Files.notExists(this.hashPath)) return true;
String hashStr = Files.readString(hashPath, StandardCharsets.UTF_8);
return !Long.toString(this.hash).equals(hashStr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2024 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package net.fabricmc.loom.configuration.providers.forge;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import org.gradle.api.Project;

import net.fabricmc.loom.configuration.providers.minecraft.MinecraftProvider;

public interface MappingsMigrator {
long setup(Project project, MinecraftProvider minecraftProvider, Path cache, Path rawMappings, boolean hasSrg, boolean hasMojang) throws IOException;

void migrate(Project project, List<MappingsEntry> entries) throws IOException;

record MappingsEntry(Path path) {
}
}
Loading

0 comments on commit 52b59fe

Please sign in to comment.