Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Field Migration #168

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
Expand Down Expand Up @@ -58,7 +58,6 @@
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.loom.util.ThreadingUtils;
import net.fabricmc.loom.util.service.SharedServiceManager;
import net.fabricmc.loom.util.srg.SrgMerger;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.format.Tiny2Writer;
import net.fabricmc.mappingio.tree.MappingTree;
Expand Down Expand Up @@ -110,14 +109,6 @@ protected void manipulateMappings(Project project, Path mappingsJar) throws IOEx
this.rawTinyMappings = tinyMappings;
this.rawTinyMappingsWithSrg = tinyMappingsWithSrg;

if (extension.shouldGenerateSrgTiny()) {
if (Files.notExists(rawTinyMappingsWithSrg) || extension.refreshDeps()) {
// Merge tiny mappings with srg
SrgMerger.ExtraMappings extraMappings = SrgMerger.ExtraMappings.ofMojmapTsrg(getMojmapSrgFileIfPossible(project));
SrgMerger.mergeSrg(getRawSrgFile(project), rawTinyMappings, rawTinyMappingsWithSrg, extraMappings, true);
}
}

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

Expand All @@ -139,39 +130,46 @@ public void updateFieldMigration(Project project) throws IOException {
});
Files.writeString(migratedFieldsCache, new Gson().toJson(map));
Files.deleteIfExists(tinyMappings);
Files.deleteIfExists(tinyMappingsWithSrg);
}

if (!Files.exists(tinyMappings)) {
if (Files.notExists(tinyMappings) || Files.notExists(tinyMappingsWithSrg)) {
Table<String, String, String> fieldDescriptorMap = HashBasedTable.create();

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

MemoryMappingTree mappings = new MemoryMappingTree();
injectMigration(project, fieldDescriptorMap, rawTinyMappings, tinyMappings);
injectMigration(project, fieldDescriptorMap, rawTinyMappingsWithSrg, tinyMappingsWithSrg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, it makes applying the same thing to the Mojang-merged mappings much easier too

}
}

try (BufferedReader reader = Files.newBufferedReader(rawTinyMappings)) {
MappingReader.read(reader, mappings);
private static void injectMigration(Project project, Table<String, String, String> fieldDescriptorMap, Path source, Path out) throws IOException {
MemoryMappingTree mappings = new MemoryMappingTree();

for (MappingTree.ClassMapping classDef : new ArrayList<>(mappings.getClasses())) {
Map<String, String> row = fieldDescriptorMap.row(classDef.getName(MappingsNamespace.INTERMEDIARY.toString()));
try (BufferedReader reader = Files.newBufferedReader(source)) {
MappingReader.read(reader, mappings);
}

if (!row.isEmpty()) {
for (MappingTree.FieldMapping fieldDef : new ArrayList<>(classDef.getFields())) {
String newDescriptor = row.get(fieldDef.getName(MappingsNamespace.INTERMEDIARY.toString()));
for (MappingTree.ClassMapping classDef : new ArrayList<>(mappings.getClasses())) {
Map<String, String> row = fieldDescriptorMap.row(classDef.getName(MappingsNamespace.INTERMEDIARY.toString()));

if (newDescriptor != null) {
fieldDef.setSrcDesc(mappings.mapDesc(newDescriptor, mappings.getNamespaceId(MappingsNamespace.INTERMEDIARY.toString()), MappingTreeView.SRC_NAMESPACE_ID));
}
}
if (!row.isEmpty()) {
for (MappingTree.FieldMapping fieldDef : new ArrayList<>(classDef.getFields())) {
String newDescriptor = row.get(fieldDef.getName(MappingsNamespace.INTERMEDIARY.toString()));

if (newDescriptor != null) {
String prev = fieldDef.getDesc(MappingsNamespace.INTERMEDIARY.toString());
fieldDef.setSrcDesc(mappings.mapDesc(newDescriptor, mappings.getNamespaceId(MappingsNamespace.INTERMEDIARY.toString()), MappingTreeView.SRC_NAMESPACE_ID));
project.getLogger().info("Migrated field descriptor of field {}#{} from {} to {}", classDef.getName(MappingsNamespace.INTERMEDIARY.toString()), fieldDef.getName(MappingsNamespace.INTERMEDIARY.toString()), prev, newDescriptor);
}
}
}
}

StringWriter stringWriter = new StringWriter();
Tiny2Writer tiny2Writer = new Tiny2Writer(stringWriter, false);
mappings.accept(tiny2Writer);
Files.writeString(tinyMappings, stringWriter.toString(), StandardOpenOption.CREATE);
try (Writer writer = Files.newBufferedWriter(out, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
mappings.accept(new Tiny2Writer(writer, false));
}
}

Expand Down Expand Up @@ -242,7 +240,7 @@ public FieldVisitor visitField(int access, String name, String descriptor, Strin
String newDescriptorRemapped = DescriptorRemapper.remapDescriptor(newDescriptor,
clazz -> srgToIntermediary.getOrDefault(clazz, clazz));
migratedFields.put(new FieldMember(ownerIntermediary, fieldIntermediary), newDescriptorRemapped);
project.getLogger().info(ownerIntermediary + "#" + fieldIntermediary + ": " + descriptorIntermediary + " -> " + newDescriptorRemapped);
project.getLogger().info("Found migration of " + ownerIntermediary + "#" + fieldIntermediary + ": " + descriptorIntermediary + " -> " + newDescriptorRemapped);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ protected void setup(Project project, SharedServiceManager serviceManager, Minec

public void setupPost(Project project) throws IOException {
LoomGradleExtension extension = LoomGradleExtension.get(project);
manipulateMappings(project, tinyMappingsJar);

if (extension.shouldGenerateSrgTiny()) {
if (Files.notExists(tinyMappingsWithSrg) || extension.refreshDeps()) {
Expand All @@ -206,6 +205,8 @@ public void setupPost(Project project) throws IOException {
project.getLogger().info(":merged srg mappings in " + stopwatch.stop());
}
}

manipulateMappings(project, tinyMappingsJar);
}

public void applyToProject(Project project, DependencyInfo dependency) throws IOException {
Expand Down
Loading