Skip to content

Commit

Permalink
build with pathfinder
Browse files Browse the repository at this point in the history
  • Loading branch information
leijurv committed Jun 16, 2023
1 parent a9e9cd9 commit e01093e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
30 changes: 18 additions & 12 deletions buildSrc/src/main/java/baritone/gradle/task/ProguardTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,18 @@ public class ProguardTask extends BaritoneGradleTask {
private List<String> requiredLibraries;

private File mixin;
private File pathfinder;

@TaskAction
protected void exec() throws Exception {
super.verifyArtifacts();

// "Haha brady why don't you make separate tasks"
processArtifact();
downloadProguard();
extractProguard();
generateConfigs();
acquireDependencies();
processArtifact();
proguardApi();
proguardStandalone();
cleanup();
Expand All @@ -89,7 +90,7 @@ private void processArtifact() throws Exception {
Files.delete(this.artifactUnoptimizedPath);
}

Determinizer.determinize(this.artifactPath.toString(), this.artifactUnoptimizedPath.toString(), Optional.empty());
Determinizer.determinize(this.artifactPath.toString(), this.artifactUnoptimizedPath.toString(), Arrays.asList(pathfinder), false);
}

private void downloadProguard() throws Exception {
Expand All @@ -114,25 +115,23 @@ private String getJavaBinPathForProguard() throws Exception {
try {
path = findJavaPathByGradleConfig();
if (path != null) return path;
}
catch (Exception ex) {
} catch (Exception ex) {
System.err.println("Unable to find java by javaCompile options");
ex.printStackTrace();
}

try {
path = findJavaByJavaHome();
if (path != null) return path;
}
catch(Exception ex) {
} catch (Exception ex) {
System.err.println("Unable to find java by JAVA_HOME");
ex.printStackTrace();
}


path = findJavaByGradleCurrentRuntime();
if (path != null) return path;

throw new Exception("Unable to find java to determine ProGuard libraryjars. Please specify forkOptions.executable in javaCompile," +
" JAVA_HOME environment variable, or make sure to run Gradle with the correct JDK (a v1.8 only)");
}
Expand Down Expand Up @@ -281,13 +280,19 @@ private void acquireDependencies() throws Exception {
if (lib.contains("mixin")) {
mixin = file;
}
if (lib.contains("nether-pathfinder")) {
pathfinder = file;
}
Files.copy(file.toPath(), getTemporaryFile("tempLibraries/" + lib + ".jar"), REPLACE_EXISTING);
}
}
}
if (mixin == null) {
throw new IllegalStateException("Unable to find mixin jar");
}
if (pathfinder == null) {
throw new IllegalStateException("Unable to find pathfinder jar");
}
}

// a bunch of epic stuff to get the path to the cached jar
Expand Down Expand Up @@ -375,14 +380,14 @@ private List<MappingType> getUsedMappingTypes(Set<Object> reobf) {

private void proguardApi() throws Exception {
runProguard(getTemporaryFile(PROGUARD_API_CONFIG));
Determinizer.determinize(this.proguardOut.toString(), this.artifactApiPath.toString(), Optional.empty());
Determinizer.determinize(this.proguardOut.toString(), this.artifactForgeApiPath.toString(), Optional.of(mixin));
Determinizer.determinize(this.proguardOut.toString(), this.artifactApiPath.toString(), Arrays.asList(pathfinder), false);
Determinizer.determinize(this.proguardOut.toString(), this.artifactForgeApiPath.toString(), Arrays.asList(pathfinder, mixin), true);
}

private void proguardStandalone() throws Exception {
runProguard(getTemporaryFile(PROGUARD_STANDALONE_CONFIG));
Determinizer.determinize(this.proguardOut.toString(), this.artifactStandalonePath.toString(), Optional.empty());
Determinizer.determinize(this.proguardOut.toString(), this.artifactForgeStandalonePath.toString(), Optional.of(mixin));
Determinizer.determinize(this.proguardOut.toString(), this.artifactStandalonePath.toString(), Arrays.asList(pathfinder), false);
Determinizer.determinize(this.proguardOut.toString(), this.artifactForgeStandalonePath.toString(), Arrays.asList(pathfinder, mixin), true);
}

private void cleanup() {
Expand All @@ -409,7 +414,7 @@ private void runProguard(Path config) throws Exception {
Path workingDirectory = getTemporaryFile("");
Path proguardJar = workingDirectory.relativize(getTemporaryFile(PROGUARD_JAR));
config = workingDirectory.relativize(config);

// Honestly, if you still have spaces in your path at this point, you're SOL.

Process p = new ProcessBuilder("java", "-jar", proguardJar.toString(), "@" + config.toString())
Expand All @@ -423,6 +428,7 @@ private void runProguard(Path config) throws Exception {
// Halt the current thread until the process is complete, if the exit code isn't 0, throw an exception
int exitCode = p.waitFor();
if (exitCode != 0) {
Thread.sleep(1000);
throw new IllegalStateException("Proguard exited with code " + exitCode);
}
}
Expand Down
15 changes: 10 additions & 5 deletions buildSrc/src/main/java/baritone/gradle/util/Determinizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import com.google.gson.stream.JsonWriter;

import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
Expand All @@ -36,10 +39,11 @@
*/
public class Determinizer {

public static void determinize(String inputPath, String outputPath, Optional<File> toInclude) throws IOException {
public static void determinize(String inputPath, String outputPath, List<File> toInclude, boolean doForgeReplacementOfMetaInf) throws IOException {
System.out.println("Running Determinizer");
System.out.println(" Input path: " + inputPath);
System.out.println(" Output path: " + outputPath);
System.out.println(" Shade: " + toInclude);

try (
JarFile jarFile = new JarFile(new File(inputPath));
Expand All @@ -63,7 +67,7 @@ public static void determinize(String inputPath, String outputPath, Optional<Fil
if (entry.getName().endsWith(".refmap.json")) {
JsonObject object = new JsonParser().parse(new InputStreamReader(jarFile.getInputStream(entry))).getAsJsonObject();
jos.write(writeSorted(object).getBytes());
} else if (entry.getName().equals("META-INF/MANIFEST.MF") && toInclude.isPresent()) { // only replace for forge jar
} else if (entry.getName().equals("META-INF/MANIFEST.MF") && doForgeReplacementOfMetaInf) { // only replace for forge jar
ByteArrayOutputStream cancer = new ByteArrayOutputStream();
copy(jarFile.getInputStream(entry), cancer);
String manifest = new String(cancer.toByteArray());
Expand All @@ -76,8 +80,8 @@ public static void determinize(String inputPath, String outputPath, Optional<Fil
copy(jarFile.getInputStream(entry), jos);
}
}
if (toInclude.isPresent()) {
try (JarFile mixin = new JarFile(toInclude.get())) {
for (File file : toInclude) {
try (JarFile mixin = new JarFile(file)) {
for (JarEntry entry : mixin.stream().sorted(Comparator.comparing(JarEntry::getName)).collect(Collectors.toList())) {
if (entry.getName().startsWith("META-INF") && !entry.getName().startsWith("META-INF/services")) {
continue;
Expand All @@ -89,6 +93,7 @@ public static void determinize(String inputPath, String outputPath, Optional<Fil
}
jos.finish();
}
System.out.println("Done with determinizer");
}

private static void copy(InputStream is, OutputStream os) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion scripts/proguard.pro
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
-libraryjars 'tempLibraries/mixin-0.7.11-SNAPSHOT.jar'
-libraryjars 'tempLibraries/launchwrapper-1.11.jar' # TODO why does only 1.11.jar exist?


-libraryjars 'tempLibraries/nether-pathfinder-.jar'


# Keep - Applications. Keep all application classes, along with their 'main'
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/baritone/Elytra.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class Elytra extends Behavior implements Helper {
public List<BetterBlockPos> path = new ArrayList<>();

public void path(BlockPos destination) {
playerNear = 0;
goingTo = 0;
path = Arrays.stream(NetherPathfinder.pathFind(146008555100680L, false, false, ctx.playerFeet().x, ctx.playerFeet().y, ctx.playerFeet().z, destination.getX(), destination.getY(), destination.getZ())).mapToObj(BlockPos::fromLong).map(BetterBlockPos::new).collect(Collectors.toList());
removeBacktracks();
}
Expand Down

0 comments on commit e01093e

Please sign in to comment.