Skip to content

Commit

Permalink
implement package json merge
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Dec 24, 2024
1 parent b9fa5f2 commit 838be24
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 32 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
enigma = "2.5.2+local"
enigma = "2.5.3+local"

asm = "9.7.1"
quilt_json_parser = "0.3.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;

public class MojmapNameProposer extends NameProposer {
public static final String ID = "mojmap";
Expand Down Expand Up @@ -85,7 +85,12 @@ public static List<PackageEntry> readPackageJson(Gson gson, String path) {
try {
if (path != null) {
Reader jsonReader = new FileReader(path);
return gson.fromJson(jsonReader, PackageEntryList.class);
List<PackageEntry> entries = gson.fromJson(jsonReader, PackageEntryList.class);
for (PackageEntry entry : entries) {
setupInheritance(entry);
}

return entries;
}
} catch (FileNotFoundException e) {
Logger.warn("could not find old package definitions file");
Expand All @@ -94,15 +99,47 @@ public static List<PackageEntry> readPackageJson(Gson gson, String path) {
return null;
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
public static void writePackageJson(String packageNameOverridesPath, EntryTree<EntryMapping> mojmaps) {
private static void setupInheritance(PackageEntry entry) {
for (PackageEntry child : entry.children) {
child.parent = entry;
setupInheritance(child);
}
}

public static List<PackageEntry> readPackageJson(String path) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return readPackageJson(gson, path);
}

public static List<PackageEntry> updatePackageJson(List<PackageEntry> oldJson, EntryTree<EntryMapping> mappings) {
List<PackageEntry> newJson = createPackageJson(mappings);

List<PackageEntry> parsedRootEntries = readPackageJson(gson, packageNameOverridesPath);
// todo merge with new tree
for (PackageEntry rootEntry : oldJson) {
rootEntry.forEach(oldEntry -> {
if (oldEntry.deobf != null) {
PackageEntry newEntry = null;
String oldEntryString = oldEntry.toPackageString();

for (PackageEntry root : newJson) {
newEntry = root.findEntry(oldEntryString);
if (newEntry != null) {
break;
}
}

if (newEntry != null) {
newEntry.deobf = oldEntry.deobf;
}
}
});
}

return newJson;
}

public static List<PackageEntry> createPackageJson(EntryTree<EntryMapping> mappings) {
MappingsIndex index = new MappingsIndex(new PackageIndex());
index.indexMappings(mojmaps, ProgressListener.createEmpty());
index.indexMappings(mappings, ProgressListener.createEmpty());

var packageNames = index.getIndex(PackageIndex.class).getPackageNames();
List<PackageEntry> rootPackages = new ArrayList<>();
Expand Down Expand Up @@ -146,8 +183,14 @@ public static void writePackageJson(String packageNameOverridesPath, EntryTree<E
}
}

return rootPackages;
}

public static void writePackageJson(Path path, List<PackageEntry> entries) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();

try {
Files.write(Path.of(packageNameOverridesPath), gson.toJson(rootPackages).getBytes());
Files.write(path, gson.toJson(entries).getBytes());
} catch (IOException e) {
Logger.error(e, "could not write updated package name overrides");
}
Expand All @@ -168,6 +211,15 @@ public PackageEntry(String obf, String deobf) {
this.children = new ArrayList<>();
}

public void forEach(Consumer<PackageEntry> consumer) {
consumer.accept(this);

for (PackageEntry child : this.children) {
consumer.accept(child);
child.forEach(consumer);
}
}

public PackageEntry findEntry(String obf) {
if (this.equals(obf)) {
return this;
Expand All @@ -183,29 +235,20 @@ public PackageEntry findEntry(String obf) {
return null;
}

private boolean equals(String obfPackage) {
String[] packages = obfPackage.split("/");
Collections.reverse(Arrays.asList(packages));
public String toPackageString() {
List<String> packages = new ArrayList<>();
PackageEntry entry = this;
while (entry != null) {
packages.add(entry.obf);
entry = entry.parent;
}

return this.checkRecursively(packages);
Collections.reverse(packages);
return String.join("/", packages.toArray(new String[0]));
}

private boolean checkRecursively(String[] packageNames) {
PackageEntry checkedPackage = this;

for (String packageName : packageNames) {
if (checkedPackage == null) {
return true;
}

if (!checkedPackage.obf.equals(packageName)) {
return false;
}

checkedPackage = checkedPackage.parent;
}

return true;
private boolean equals(String obfPackage) {
return this.toPackageString().equals(obfPackage);
}
}
}
43 changes: 40 additions & 3 deletions src/test/java/org/quiltmc/enigma_plugin/MojmapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.quiltmc.enigma.api.class_provider.ClasspathClassProvider;
import org.quiltmc.enigma.api.source.TokenType;
import org.quiltmc.enigma.api.translation.mapping.EntryMapping;
import org.quiltmc.enigma.api.translation.mapping.serde.MappingParseException;
import org.quiltmc.enigma.api.translation.representation.MethodDescriptor;
import org.quiltmc.enigma.api.translation.representation.TypeDescriptor;
import org.quiltmc.enigma.api.translation.representation.entry.ClassEntry;
Expand Down Expand Up @@ -146,19 +147,55 @@ void testDynamicProposal() {
assertMapping(a, "com/mojang/math/Gaming", TokenType.DEOBFUSCATED);
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
void testPackageNameOverrideGeneration() throws IOException {
// todo make a more minimal one -- use a custom mapping set
void testOverrideGeneration() throws IOException, MappingParseException {
Path tempFile = Files.createTempFile("temp_package_overrides", "json");
Path mappingPath = Path.of("./src/test/resources/mojmap_test/example_mappings.mapping");
var mappings = project.getEnigma().readMappings(mappingPath).get();

MojmapNameProposer.writePackageJson(tempFile.toString(), MojmapNameProposer.mojmaps);
var entries = MojmapNameProposer.createPackageJson(mappings);
MojmapNameProposer.writePackageJson(tempFile, entries);

String expected = Files.readString(Path.of("./src/test/resources/mojmap_test/example_mappings_empty.json"));
String actual = Files.readString(tempFile);

Assertions.assertEquals(expected, actual);
}

@Test
void testPackageNameOverrideGenerationMojmap() throws IOException {
Path tempFile = Files.createTempFile("temp_package_overrides_moj", "json");

var entries = MojmapNameProposer.createPackageJson(MojmapNameProposer.mojmaps);
MojmapNameProposer.writePackageJson(tempFile, entries);

String expected = Files.readString(Path.of("./src/test/resources/mojmap_test/expected.json"));
String actual = Files.readString(tempFile);

Assertions.assertEquals(expected, actual);
}

@Test
void testOverrideUpdating() throws IOException, MappingParseException {
Path tempFile = Files.createTempFile("temp_package_overrides_update", "json");
Path mappingPath = Path.of("./src/test/resources/mojmap_test/example_mappings.mapping");

Path oldOverrides = Path.of("./src/test/resources/mojmap_test/update_test/example_mappings.json");
Path newOverrides = Path.of("./src/test/resources/mojmap_test/update_test/example_mappings_expected.json");

var mappings = project.getEnigma().readMappings(mappingPath).get();

var oldPackageJson = MojmapNameProposer.readPackageJson(oldOverrides.toString());
var updated = MojmapNameProposer.updatePackageJson(oldPackageJson, mappings);
MojmapNameProposer.writePackageJson(tempFile, updated);

String expected = Files.readString(newOverrides);
String actual = Files.readString(tempFile);

Assertions.assertEquals(expected, actual);
}

private void assertMapping(Entry<?> entry, String name, TokenType type) {
var mapping = project.getRemapper().getMapping(entry);
assertEquals(entry, name, mapping.targetName());
Expand Down
7 changes: 7 additions & 0 deletions src/test/resources/mojmap_test/example_mappings.mapping
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CLASS a a/A
CLASS b a/b/B
CLASS c b/a/A
CLASS d c/a/A
CLASS e b/b/B
CLASS f b/b/c/C
CLASS g a/b/c/A
40 changes: 40 additions & 0 deletions src/test/resources/mojmap_test/example_mappings_empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[
{
"obf": "a",
"deobf": "",
"children": [
{
"obf": "b",
"deobf": "",
"children": []
}
]
},
{
"obf": "b",
"deobf": "",
"children": [
{
"obf": "a",
"deobf": "",
"children": []
},
{
"obf": "b",
"deobf": "",
"children": []
}
]
},
{
"obf": "c",
"deobf": "",
"children": [
{
"obf": "a",
"deobf": "",
"children": []
}
]
}
]
29 changes: 29 additions & 0 deletions src/test/resources/mojmap_test/update_test/example_mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"obf": "a",
"deobf": "wawa",
"children": []
},
{
"obf": "b",
"deobf": "gaming",
"children": [
{
"obf": "a",
"deobf": "",
"children": []
}
]
},
{
"obf": "c",
"deobf": "",
"children": [
{
"obf": "a",
"deobf": "listen to the 2016 album aura obelisk by octagrape it goes crazy",
"children": []
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[
{
"obf": "a",
"deobf": "wawa",
"children": [
{
"obf": "b",
"deobf": "",
"children": []
}
]
},
{
"obf": "b",
"deobf": "gaming",
"children": [
{
"obf": "a",
"deobf": "",
"children": []
},
{
"obf": "b",
"deobf": "",
"children": []
}
]
},
{
"obf": "c",
"deobf": "",
"children": [
{
"obf": "a",
"deobf": "listen to the 2016 album aura obelisk by octagrape it goes crazy",
"children": []
}
]
}
]

0 comments on commit 838be24

Please sign in to comment.