From 0b68cc4a73731247b3c02c6e2c0fcfd34af31d67 Mon Sep 17 00:00:00 2001 From: Brendan Robert Date: Tue, 25 Aug 2015 23:09:45 -0500 Subject: [PATCH] Cheat load/save now works --- src/main/java/jace/cheat/DynamicCheat.java | 27 ++++++++++- src/main/java/jace/cheat/MetaCheat.java | 52 +++++++++++++++++++++- src/main/java/jace/ui/MetacheatUI.java | 19 ++++++-- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/src/main/java/jace/cheat/DynamicCheat.java b/src/main/java/jace/cheat/DynamicCheat.java index f46cf9e..1f1a1be 100644 --- a/src/main/java/jace/cheat/DynamicCheat.java +++ b/src/main/java/jace/cheat/DynamicCheat.java @@ -28,7 +28,7 @@ public class DynamicCheat extends RAMListener { id = (int) (Math.random() * 10000000); addr = new SimpleIntegerProperty(address); expression = new SimpleStringProperty(expr); - active = new SimpleBooleanProperty(true); + active = new SimpleBooleanProperty(false); name = new SimpleStringProperty("Untitled"); expression.addListener((param, oldValue, newValue) -> { expressionCallback = parseExpression(newValue); @@ -95,5 +95,30 @@ public class DynamicCheat extends RAMListener { return null; } } + + public static String escape(String in) { + return in.replaceAll(";", "~~").replaceAll("\n","\\n"); + } + public static String unescape(String in) { + return in.replaceAll("~~", ";").replaceAll("\\n", "\n"); + } + + public static final String DELIMITER = ";"; + public String serialize() { + return escape(name.get()) + DELIMITER + + escape("$"+Integer.toHexString(addr.get())) + DELIMITER + + escape(expression.get()); + } + + static public DynamicCheat deserialize(String in) { + String[] parts = in.split(DELIMITER); + String name = unescape(parts[0]); + Integer addr = Integer.parseInt(parts[1].substring(1), 16); + String expr = unescape(parts[2]); + + DynamicCheat out = new DynamicCheat(addr, expr); + out.name.set(name); + return out; + } } diff --git a/src/main/java/jace/cheat/MetaCheat.java b/src/main/java/jace/cheat/MetaCheat.java index 9cb5faf..94b22bf 100644 --- a/src/main/java/jace/cheat/MetaCheat.java +++ b/src/main/java/jace/cheat/MetaCheat.java @@ -8,8 +8,15 @@ import jace.core.RAMEvent; import jace.core.RAMListener; import jace.state.State; import jace.ui.MetacheatUI; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; import javafx.beans.property.BooleanProperty; import javafx.beans.property.Property; import javafx.beans.property.SimpleBooleanProperty; @@ -128,7 +135,6 @@ public class MetaCheat extends Cheats { } public void addCheat(DynamicCheat cheat) { - cheat.activeProperty().set(true); cheatList.add(cheat); computer.getMemory().addListener(cheat); cheat.addressProperty().addListener((prop, oldVal, newVal) -> { @@ -137,7 +143,7 @@ public class MetaCheat extends Cheats { computer.getMemory().addListener(cheat); }); } - + public void removeCheat(DynamicCheat cheat) { cheat.active.set(false); computer.getMemory().removeListener(cheat); @@ -347,4 +353,46 @@ public class MetaCheat extends Cheats { cell.value.set(e.getNewValue()); } } + + public void saveCheats(File saveFile) { + FileWriter writer = null; + try { + writer = new FileWriter(saveFile); + for (DynamicCheat cheat : cheatList) { + writer.write(cheat.serialize()); + writer.write("\n"); + } + writer.close(); + } catch (IOException ex) { + Logger.getLogger(MetaCheat.class.getName()).log(Level.SEVERE, null, ex); + } finally { + try { + writer.close(); + } catch (IOException ex) { + Logger.getLogger(MetaCheat.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + + public void loadCheats(File saveFile) { + BufferedReader in = null; + try { + in = new BufferedReader(new FileReader(saveFile)); + StringBuilder guts = new StringBuilder(); + String line; + while ((line=in.readLine()) != null) { + DynamicCheat cheat = DynamicCheat.deserialize(line); + addCheat(cheat); + } + in.close(); + } catch (IOException ex) { + Logger.getLogger(MetaCheat.class.getName()).log(Level.SEVERE, null, ex); + } finally { + try { + in.close(); + } catch (IOException ex) { + Logger.getLogger(MetaCheat.class.getName()).log(Level.SEVERE, null, ex); + } + } + } } diff --git a/src/main/java/jace/ui/MetacheatUI.java b/src/main/java/jace/ui/MetacheatUI.java index 446fe3f..ab7d4bd 100644 --- a/src/main/java/jace/ui/MetacheatUI.java +++ b/src/main/java/jace/ui/MetacheatUI.java @@ -8,8 +8,8 @@ import jace.cheat.MetaCheat; import jace.cheat.MetaCheat.SearchChangeType; import jace.cheat.MetaCheat.SearchResult; import jace.cheat.MetaCheat.SearchType; -import jace.core.RAMListener; import jace.state.State; +import java.io.File; import java.util.HashSet; import java.util.Set; import java.util.concurrent.ConcurrentSkipListSet; @@ -46,6 +46,7 @@ import javafx.scene.input.MouseEvent; import javafx.scene.layout.StackPane; import javafx.scene.layout.TilePane; import javafx.scene.paint.Color; +import javafx.stage.FileChooser; import javafx.util.converter.DefaultStringConverter; import javafx.util.converter.IntegerStringConverter; @@ -153,12 +154,24 @@ public class MetacheatUI { @FXML void loadCheats(ActionEvent event) { - + FileChooser chooser = new FileChooser(); + chooser.setTitle("Load cheats"); + chooser.setInitialFileName("cheat.txt"); + File saveFile = chooser.showOpenDialog(null); + if (saveFile != null) { + cheatEngine.loadCheats(saveFile); + } } @FXML void saveCheats(ActionEvent event) { - + FileChooser chooser = new FileChooser(); + chooser.setTitle("Save current cheats"); + chooser.setInitialFileName("cheat.txt"); + File saveFile = chooser.showSaveDialog(null); + if (saveFile != null) { + cheatEngine.saveCheats(saveFile); + } } @FXML