Skip to content

Commit

Permalink
start working on a unit test for the mojmap proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Nov 28, 2024
1 parent 8d504b8 commit eca067b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dependencies {
testImplementation libs.hamcrest
testRuntimeOnly libs.junit.engine
testImplementation libs.enigma.swing
testImplementation libs.launchermeta.parser

testInputsImplementation libs.dfu
enigmaRuntime libs.dfu
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ annotations = "23.0.0"

junit = "5.9.3"
hamcrest = "2.2"
launchermeta_parser = "1.0.0"

dfu = "4.1.27"

Expand All @@ -26,6 +27,7 @@ annotations = { module = "org.jetbrains:annotations", version.ref = "annotations
junit = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" }
junit_engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit" }
hamcrest = { module = "org.hamcrest:hamcrest", version.ref = "hamcrest" }
launchermeta_parser = { module = "org.quiltmc:launchermeta-parser", version.ref = "launchermeta_parser" }

dfu = { module = "com.mojang:datafixerupper", version.ref = "dfu" }

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
109 changes: 109 additions & 0 deletions src/test/java/org/quiltmc/enigma_plugin/MojmapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package org.quiltmc.enigma_plugin;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.quiltmc.enigma.api.Enigma;
import org.quiltmc.enigma.api.EnigmaProfile;
import org.quiltmc.enigma.api.EnigmaProject;
import org.quiltmc.enigma.api.ProgressListener;
import org.quiltmc.enigma.api.class_provider.ClasspathClassProvider;
import org.quiltmc.enigma.api.translation.mapping.serde.MappingParseException;
import org.quiltmc.enigma.api.translation.representation.entry.ClassEntry;
import org.quiltmc.launchermeta.version.v1.DownloadableFile;
import org.quiltmc.launchermeta.version.v1.Version;
import org.quiltmc.launchermeta.version_manifest.VersionEntry;
import org.quiltmc.launchermeta.version_manifest.VersionManifest;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.nio.file.Path;
import java.util.Optional;

public class MojmapTest {
private static final Path cacheDir = Path.of("build/mojmap_cache");
private static final File cachedServerJar = cacheDir.resolve("server.jar").toFile();
private static final File cachedServerMappings = cacheDir.resolve("server-mappings.txt").toFile();

private static EnigmaProject project;

@BeforeAll
static void downloadMojmaps() throws IOException {
if (!cachedServerJar.exists() || !cachedServerMappings.exists()) {
System.out.println("Could not find server jar or mappings, downloading...");
String mcVersion = "1.21";

InputStreamReader reader;
URL manifestUrl = new URL("https://launchermeta.mojang.com/mc/game/version_manifest_v2.json");
InputStreamReader manifestReader = new InputStreamReader(new BufferedInputStream(manifestUrl.openStream()));
VersionManifest manifest = VersionManifest.fromReader(manifestReader);
Optional<VersionEntry> entry = manifest.getVersions().stream().filter(e -> e.getId().equals(mcVersion)).findAny();
if (entry.isPresent()) {
reader = new InputStreamReader(new BufferedInputStream(new URL(entry.get().getUrl()).openStream()));
} else {
throw new RuntimeException("could not download " + mcVersion + " from manifest!");
}

Version version = Version.fromReader(reader);
Optional<DownloadableFile> serverJar = version.getDownloads().getServer();
Optional<DownloadableFile> serverMappings = version.getDownloads().getServerMappings();

if (serverJar.isEmpty() || serverMappings.isEmpty()) {
throw new RuntimeException("could not download " + mcVersion + " from manifest!");
}

System.out.println("Downloading server jar...");
download(serverJar.get().getUrl(), cachedServerJar);
System.out.println("Downloading server mappings...");
download(serverMappings.get().getUrl(), cachedServerMappings);
}
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private static void download(String url, File file) throws IOException {
file.toPath().resolve("..").toFile().mkdirs();
file.createNewFile();

try (BufferedInputStream in = new BufferedInputStream(new URL(url).openStream()); FileOutputStream fileOutputStream = new FileOutputStream(file)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer, 0, 1024)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
}
}
}

@BeforeEach
@SuppressWarnings("OptionalGetWithoutIsPresent")
void setupEnigma() throws IOException, MappingParseException {
var profile = EnigmaProfile.parse(new StringReader("""
{
"services": {
"name_proposal": [
{
"id": "quiltmc:mojang_name_proposal"
}
]
}
}
"""));


var enigma = Enigma.builder().setProfile(profile).build();
project = enigma.openJar(cachedServerJar.toPath(), new ClasspathClassProvider(), ProgressListener.createEmpty());

// todo should be proposed instead of manually inserted!
project.setMappings(enigma.getReadWriteService(cachedServerMappings.toPath()).get().read(cachedServerMappings.toPath()), ProgressListener.createEmpty());
}

@Test
void test() {
ClassEntry a = new ClassEntry("a");
assert project.getRemapper().getMapping(a).targetName().equals("com/mojang/math/Axis");
}
}

0 comments on commit eca067b

Please sign in to comment.