Remove Nashorn usage, fix configuration swap for video/memory so it works correctly.

This commit is contained in:
Brendan Robert 2024-03-09 10:49:34 -06:00
parent 264f317c90
commit f0b87d07ec
6 changed files with 18 additions and 46 deletions

View File

@ -201,6 +201,11 @@ public class Apple2e extends Computer {
return getDesiredMemoryConfiguration().isInstance((RAM128k) getMemory()); return getDesiredMemoryConfiguration().isInstance((RAM128k) getMemory());
} }
private boolean isVideoConfigurationCorrect() {
VideoImpls videoSelection = videoRenderer.getValue();
return videoSelection != null && videoSelection.isInstance(getVideo());
}
@Override @Override
protected RAM createMemory() { protected RAM createMemory() {
return getDesiredMemoryConfiguration().create(); return getDesiredMemoryConfiguration().create();
@ -291,7 +296,7 @@ public class Apple2e extends Computer {
clock = null; clock = null;
} }
if (videoRenderer.getValue() != null && !videoRenderer.getValue().isInstance(getVideo())) { if (!isVideoConfigurationCorrect()) {
boolean resumeVideo = false; boolean resumeVideo = false;
if (getVideo() != null) { if (getVideo() != null) {
resumeVideo = getVideo().suspend(); resumeVideo = getVideo().suspend();

View File

@ -71,7 +71,7 @@ abstract public class RAM128k extends RAM {
@Override @Override
public boolean isInstance(RAM128k card) { public boolean isInstance(RAM128k card) {
return card != null && clazz.isInstance(card); return card != null && clazz.equals(card.getClass());
} }
} }

View File

@ -1,7 +1,5 @@
package jace.cheat; package jace.cheat;
import javax.script.ScriptException;
import jace.core.RAMEvent; import jace.core.RAMEvent;
import jace.core.RAMListener; import jace.core.RAMListener;
import javafx.beans.property.BooleanProperty; import javafx.beans.property.BooleanProperty;
@ -25,20 +23,20 @@ public class DynamicCheat extends RAMListener {
String cheatName; String cheatName;
Callback<RAMEvent, Integer> expressionCallback; Callback<RAMEvent, Integer> expressionCallback;
public DynamicCheat(String cheatName, int address, String expr) { public DynamicCheat(String cheatName, int address, int holdValue) {
super(cheatName, RAMEvent.TYPE.ANY, RAMEvent.SCOPE.ADDRESS, RAMEvent.VALUE.ANY); super(cheatName, RAMEvent.TYPE.ANY, RAMEvent.SCOPE.ADDRESS, RAMEvent.VALUE.ANY);
id = (int) (Math.random() * 10000000); id = (int) (Math.random() * 10000000);
addr = new SimpleIntegerProperty(address); addr = new SimpleIntegerProperty(address);
expression = new SimpleStringProperty(expr); expression = new SimpleStringProperty(String.valueOf(holdValue));
isHold = true;
active = new SimpleBooleanProperty(false); active = new SimpleBooleanProperty(false);
name = new SimpleStringProperty("Untitled"); name = new SimpleStringProperty("Untitled");
expression.addListener((param, oldValue, newValue) -> { expressionCallback = (RAMEvent e) -> holdValue;
expressionCallback = parseExpression(newValue);
});
expressionCallback = parseExpression(expr);
doConfig(); doConfig();
} }
boolean isHold = false;
@Override @Override
protected void doConfig() { protected void doConfig() {
if (addr != null) { if (addr != null) {
@ -75,29 +73,6 @@ public class DynamicCheat extends RAMListener {
return expression; return expression;
} }
private Callback<RAMEvent, Integer> parseExpression(String expr) {
String functionName = "processCheat" + id;
String functionBody = "function " + functionName + "(old,val){" + (expr.contains("return") ? expr : "return " + expr) + "}";
try {
MetaCheat.NASHORN_ENGINE.eval(functionBody);
return (RAMEvent e) -> {
try {
Object result = MetaCheat.NASHORN_INVOCABLE.invokeFunction(functionName, e.getOldValue(), e.getNewValue());
if (result instanceof Number) {
return ((Number) result).intValue();
} else {
System.err.println("Not able to handle non-numeric return value: " + result.getClass());
return null;
}
} catch (ScriptException | NoSuchMethodException ex) {
return null;
}
};
} catch (ScriptException ex) {
return null;
}
}
public static String escape(String in) { public static String escape(String in) {
return in.replaceAll(";", "~~").replaceAll("\n","\\n"); return in.replaceAll(";", "~~").replaceAll("\n","\\n");
} }
@ -120,7 +95,7 @@ public class DynamicCheat extends RAMListener {
Integer addr = Integer.parseInt(parts[2].substring(1), 16); Integer addr = Integer.parseInt(parts[2].substring(1), 16);
String expr = unescape(parts[3]); String expr = unescape(parts[3]);
DynamicCheat out = new DynamicCheat(cheatName, addr, expr); DynamicCheat out = new DynamicCheat(cheatName, addr, Integer.parseInt(expr));
out.name.set(name); out.name.set(name);
return out; return out;
} }

View File

@ -11,10 +11,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import jace.Emulator; import jace.Emulator;
import jace.LawlessLegends; import jace.LawlessLegends;
import jace.core.CPU; import jace.core.CPU;
@ -33,10 +29,6 @@ import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
public class MetaCheat extends Cheats { public class MetaCheat extends Cheats {
static final ScriptEngine NASHORN_ENGINE = new ScriptEngineManager().getEngineByName("nashorn");
static Invocable NASHORN_INVOCABLE = (Invocable) NASHORN_ENGINE;
public enum SearchType { public enum SearchType {
VALUE, TEXT, CHANGE VALUE, TEXT, CHANGE
} }

View File

@ -35,6 +35,6 @@ public enum VideoImpls implements DeviceEnum<Video> {
@Override @Override
public boolean isInstance(Video video) { public boolean isInstance(Video video) {
return clazz.isInstance(video); return video != null && clazz.equals(video.getClass());
} }
} }

View File

@ -157,7 +157,7 @@ public class MetacheatUI {
@FXML @FXML
void addCheat(ActionEvent event) { void addCheat(ActionEvent event) {
cheatEngine.addCheat(new DynamicCheat(event.toString(), 0, "?")); cheatEngine.addCheat(new DynamicCheat(event.toString(), 0, 0));
} }
@FXML @FXML
@ -562,7 +562,7 @@ public class MetacheatUI {
} }
private void addCheat(String name, int addr, int val) { private void addCheat(String name, int addr, int val) {
cheatEngine.addCheat(new DynamicCheat(name, addr, String.valueOf(val))); cheatEngine.addCheat(new DynamicCheat(name, addr, val));
} }
int currentlyInspecting = 0; int currentlyInspecting = 0;