Skip to content

Instantly share code, notes, and snippets.

@khanhduytran0
Last active March 30, 2022 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khanhduytran0/915bb54c7a7edf569e71e72f92d073e5 to your computer and use it in GitHub Desktop.
Save khanhduytran0/915bb54c7a7edf569e71e72f92d073e5 to your computer and use it in GitHub Desktop.
Minecraft 1.17+ assets patcher for PojavLauncher.
/*
* These following steps are not currently automated, so you need to do manually for now.
* download assets-v0.zip at https://cdn.discordapp.com/attachments/724163890803638277/923349783589056522/assets-v0.zip
* extract and copy the shaders folder to the resources folder.
* edit include/light.glsl add `#define texture texture2D` after `#version 100` line
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.jar.JarFile;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.stream.*;
public class Pojav117AssetsPatcher implements Runnable {
public void run() {
File verDir = new File(System.getProperty("user.dir"), "versions");
if (!verDir.exists()) {
JOptionPane.showMessageDialog(null, "Minecraft versions directory at " + verDir.getAbsolutePath() + " did not exists.");
return;
}
String[] versionList = verDir.list((dir, name) -> {
try {
String jsonModded = new String(Files.readAllBytes(new File(dir, name + File.separator + name + ".json").toPath()), StandardCharsets.UTF_8).replace(" ", "");
if (!jsonModded.contains("inheritsFrom\":\"")) {
System.out.println(name + " doesn't contain inheritsFrom");
return jsonModded.contains("javaVersion") && !(jsonModded.contains("jre-legacy") && jsonModded.contains("\"majorVersion\":8"));
}
String inheritsFrom = jsonModded.substring(jsonModded.indexOf("inheritsFrom\":\"") + 15);
inheritsFrom = inheritsFrom.substring(0, inheritsFrom.indexOf("\""));
String jsonVanilla = new String(Files.readAllBytes(new File(dir, inheritsFrom + File.separator + inheritsFrom + ".json").toPath()), StandardCharsets.UTF_8).replace(" ", "");
//System.out.println(jsonVanilla);
return jsonVanilla.contains("javaVersion") && !(jsonVanilla.contains("jre-legacy") && jsonVanilla.contains("\"majorVersion\":8"));
} catch (IOException e) {
System.out.println("Could not read " + name);
e.printStackTrace();
return false;
}
});
JComboBox optionList = new JComboBox(versionList);
JPanel panel = new JPanel();
panel.add(new JLabel("Select installed version:"));
panel.add(optionList);
JOptionPane.showOptionDialog(null, panel, "Minecraft 1.17+ shader patcher", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
int n = optionList.getSelectedIndex();
if (n < 0) {
System.out.println("Invalid selected index " + n);
//JOptionPane.showMessageDialog(null, "");
return;
}
try {
FileSystem sourceFS = FileSystems.newFileSystem(new File(Pojav117AssetsPatcher.class.getProtectionDomain().getCodeSource().getLocation().toURI()).toPath(), null);
Path sourcePath = sourceFS.getPath("shaders");
FileSystem targetFS = FileSystems.newFileSystem(new File(verDir, versionList[n] + File.separator + versionList[n] + ".jar").toPath(), null);
Path targetPath = targetFS.getPath("assets/minecraft");
try (Stream<Path> stream = Files.walk(sourcePath)) {
stream.forEach(source -> {
if (Files.isRegularFile(source)) {
System.out.println(targetPath.resolve(source.toString().substring(1)));
copy(source, targetPath.resolve(source.toString().substring(1)));
}
});
}
// Allow re-patching
try {
Files.delete(targetFS.getPath("META-INF/MOJANGCS.RSA"));
Files.delete(targetFS.getPath("META-INF/MOJANGCS.SF"));
} catch (NoSuchFileException e) {
e.printStackTrace();
}
copy(sourceFS.getPath("manifest.txt"), targetFS.getPath("META-INF/MANIFEST.MF"));
sourceFS.close();
try {
targetFS.close();
} catch (FileSystemException e) {
// dirty workaround for FAT32 sdcards
if (e.getMessage().endsWith(": Operation not permitted")) {
System.err.println("Error setting permission, applying workaround.");
e.printStackTrace();
File file = new File(e.getMessage().substring(0, e.getMessage().indexOf(": Operation not permitted")));
file.renameTo(new File(file.getParentFile(), versionList[n]));
} else throw e;
}
JOptionPane.showMessageDialog(null, "Patched " + versionList[n]);
} catch (Throwable e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
}
private void copy(Path source, Path dest) {
try {
Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Pojav117AssetsPatcher());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment