From 2fcf7612a6fe906a95b03d235d43d8cf22e46adf Mon Sep 17 00:00:00 2001 From: Wirlaburla Date: Sat, 13 Jul 2024 02:45:49 -0500 Subject: [PATCH] Add patching functionality for mods --- firestar/build.gradle | 1 + firestar/src/main/java/Gonzo.java | 38 +++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/firestar/build.gradle b/firestar/build.gradle index cd19ac5..d0dedbc 100644 --- a/firestar/build.gradle +++ b/firestar/build.gradle @@ -17,6 +17,7 @@ configurations { dependencies { implementation group: 'net.lingala.zip4j', name: 'zip4j', version: '2.11.5' implementation group: 'org.json', name: 'json', version: '20240303' + implementation "io.github.java-diff-utils:java-diff-utils:4.12" implementation 'com.jetbrains.intellij.java:java-gui-forms-rt:203.7148.30' antTask 'com.jetbrains.intellij.java:java-compiler-ant-tasks:203.7148.30' diff --git a/firestar/src/main/java/Gonzo.java b/firestar/src/main/java/Gonzo.java index d2bf659..de03328 100644 --- a/firestar/src/main/java/Gonzo.java +++ b/firestar/src/main/java/Gonzo.java @@ -16,8 +16,10 @@ * along with this program. If not, see https://www.gnu.org/licenses/. */ +import com.github.difflib.DiffUtils; +import com.github.difflib.UnifiedDiffUtils; +import com.github.difflib.patch.Patch; import net.lingala.zip4j.ZipFile; -import net.lingala.zip4j.util.FileUtils; import javax.imageio.ImageIO; import javax.swing.*; @@ -35,6 +37,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.stream.Stream; public class Gonzo { JFrame frame = new JFrame(); @@ -158,10 +163,39 @@ public class Gonzo { consoleDisplay.append("Firestar is extracting " + m.friendlyName + " by " + m.author + "\n"); new ZipFile(System.getProperty("user.home") + "/.firestar/mods/" + m.path).extractAll(System.getProperty("user.home") + "/.firestar/temp/"); + try (Stream walkStream = Files.walk(Paths.get(System.getProperty("user.home") + "/.firestar/temp/"))) { + walkStream.filter(p -> p.toFile().isFile()).forEach(f -> { + if (f.toString().endsWith(".patch")) { + System.out.println("Patching " + f.toString() + "..."); + consoleDisplay.append("Patching " + f.toString() + "...\n"); + String mainFile = f.toString().substring(0, f.toString().length() - 6); + + try { + List original = Files.readAllLines(new File(mainFile).toPath()); + List patched = Files.readAllLines(f); + + Patch patch = UnifiedDiffUtils.parseUnifiedDiff(patched); + List result = DiffUtils.patch(original, patch); + + try (FileWriter fileWriter = new FileWriter(mainFile)) { + for (String str : result) { + fileWriter.write(str + "\r\n"); + } + } + new File(f.toString()).delete(); + } catch (Exception ex) { + Logger.getLogger(Gonzo.class.getName()).log(Level.SEVERE, null, ex); + System.out.println(ex.getMessage()); + consoleDisplay.append("CRITICAL FAILURE: " + ex.getMessage()); + } + } + }); + } + if (new File(System.getProperty("user.home") + "/.firestar/temp/delete.txt").isFile()) { System.out.println("Firestar is deleting files that conflict with " + m.friendlyName + " by " + m.author); consoleDisplay.append("Firestar is deleting files that conflict with " + m.friendlyName + " by " + m.author + "\n"); - + String deleteQueue = new String(Files.readAllBytes(Paths.get(System.getProperty("user.home") + "/.firestar/temp/delete.txt"))); if (Main.windows) {deleteQueue = new String(Files.readAllBytes(Paths.get(System.getProperty("user.home") + "\\.firestar\\temp\\delete.txt")));} // might be unnecessary String[] dQarray = deleteQueue.split("\n");