mirror of
https://github.com/badvision/lawless-legends.git
synced 2025-01-18 19:31:49 +00:00
Get rid of temp files when dealing with assembly results
This commit is contained in:
parent
9feb064e83
commit
5e6012dd82
@ -7,6 +7,8 @@ import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -20,10 +22,10 @@ import java.util.stream.Collectors;
|
||||
*
|
||||
* @author blurry
|
||||
*/
|
||||
public class AcmeCompiler implements CompileResult<File> {
|
||||
public class AcmeCompiler implements CompileResult<ByteBuffer> {
|
||||
|
||||
boolean successful = false;
|
||||
File compiledAsset = null;
|
||||
ByteBuffer compiledAsset = null;
|
||||
Map<Integer, String> errors = new LinkedHashMap<>();
|
||||
Map<Integer, String> warnings = new LinkedHashMap<>();
|
||||
List<String> otherWarnings = new ArrayList<>();
|
||||
@ -35,7 +37,7 @@ public class AcmeCompiler implements CompileResult<File> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getCompiledAsset() {
|
||||
public ByteBuffer getCompiledAsset() {
|
||||
return compiledAsset;
|
||||
}
|
||||
|
||||
@ -100,21 +102,26 @@ public class AcmeCompiler implements CompileResult<File> {
|
||||
|
||||
private void invokeAcme(File sourceFile, File workingDirectory) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IOException {
|
||||
String oldPath = System.getProperty("user.dir");
|
||||
File tempFile = null;
|
||||
redirectSystemOutput();
|
||||
try {
|
||||
compiledAsset = File.createTempFile(sourceFile.getName(), "bin", sourceFile.getParentFile());
|
||||
tempFile = File.createTempFile(sourceFile.getName(), "bin", sourceFile.getParentFile());
|
||||
tempFile.deleteOnExit();
|
||||
System.setProperty("user.dir", workingDirectory.getAbsolutePath());
|
||||
AcmeCrossAssembler acme = new AcmeCrossAssembler();
|
||||
String[] params = {"--outfile", normalizeWindowsPath(compiledAsset.getAbsolutePath()), "-f", "cbm", "--maxerrors","16",normalizeWindowsPath(sourceFile.getAbsolutePath())};
|
||||
String[] params = {"--outfile", normalizeWindowsPath(tempFile.getAbsolutePath()), "-f", "cbm", "--maxerrors","16",normalizeWindowsPath(sourceFile.getAbsolutePath())};
|
||||
int status = acme.run("Acme", params);
|
||||
successful = status == 0;
|
||||
if (!successful) {
|
||||
compiledAsset.delete();
|
||||
compiledAsset = null;
|
||||
if (successful) {
|
||||
compiledAsset = ByteBuffer.wrap(Files.readAllBytes(tempFile.toPath()));
|
||||
}
|
||||
tempFile.delete();
|
||||
} finally {
|
||||
restoreSystemOutput();
|
||||
System.setProperty("user.dir", oldPath);
|
||||
if (tempFile != null && tempFile.exists()) {
|
||||
tempFile.delete();
|
||||
}
|
||||
}
|
||||
rawOutput.add("Error output:");
|
||||
extractOutput(baosErr.toString());
|
||||
|
@ -3,6 +3,7 @@ package jace.assembly;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@ -17,14 +18,14 @@ import jace.ide.Program;
|
||||
*
|
||||
* @author blurry
|
||||
*/
|
||||
public class AssemblyHandler implements LanguageHandler<File> {
|
||||
public class AssemblyHandler implements LanguageHandler<ByteBuffer> {
|
||||
@Override
|
||||
public String getNewDocumentContent() {
|
||||
return "\t\t*= $300;\n\t\t!cpu 65c02;\n;--- Insert your code here ---\n";
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompileResult<File> compile(Program proxy) {
|
||||
public CompileResult<ByteBuffer> compile(Program proxy) {
|
||||
AcmeCompiler compiler = new AcmeCompiler();
|
||||
compiler.compile(proxy);
|
||||
return compiler;
|
||||
@ -34,69 +35,51 @@ public class AssemblyHandler implements LanguageHandler<File> {
|
||||
HeadlessProgram prg = new HeadlessProgram(Program.DocumentType.assembly);
|
||||
prg.setValue(code);
|
||||
|
||||
CompileResult<File> lastResult = compile(prg);
|
||||
CompileResult<ByteBuffer> lastResult = compile(prg);
|
||||
if (lastResult.isSuccessful()) {
|
||||
Emulator.withComputer(c -> {
|
||||
RAM memory = c.getMemory();
|
||||
try {
|
||||
FileInputStream input = new FileInputStream(lastResult.getCompiledAsset());
|
||||
int startLSB = input.read();
|
||||
int startMSB = input.read();
|
||||
int start = startLSB + startMSB << 8;
|
||||
System.out.printf("Storing assembled code to $%s%n", Integer.toHexString(start));
|
||||
c.getCpu().whileSuspended(() -> {
|
||||
try {
|
||||
int pos = start;
|
||||
int next;
|
||||
while ((next=input.read()) != -1) {
|
||||
memory.write(pos++, (byte) next, false, true);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(AssemblyHandler.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
});
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(AssemblyHandler.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
ByteBuffer input = lastResult.getCompiledAsset();
|
||||
input.rewind();
|
||||
int startLSB = input.get();
|
||||
int startMSB = input.get();
|
||||
int start = startLSB + startMSB << 8;
|
||||
System.out.printf("Storing assembled code to $%s%n", Integer.toHexString(start));
|
||||
c.getCpu().whileSuspended(() -> {
|
||||
int pos = start;
|
||||
while (input.hasRemaining()) {
|
||||
memory.write(pos++, input.get(), false, true);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(CompileResult<File> lastResult) {
|
||||
public void execute(CompileResult<ByteBuffer> lastResult) {
|
||||
if (lastResult.isSuccessful()) {
|
||||
Emulator.withComputer(c -> {
|
||||
RAM memory = c.getMemory();
|
||||
try {
|
||||
FileInputStream input = new FileInputStream(lastResult.getCompiledAsset());
|
||||
int startLSB = input.read();
|
||||
int startMSB = input.read();
|
||||
int start = startLSB + startMSB << 8;
|
||||
System.out.printf("Issuing JSR to $%s%n", Integer.toHexString(start));
|
||||
c.getCpu().whileSuspended(() -> {
|
||||
try {
|
||||
int pos = start;
|
||||
int next;
|
||||
while ((next=input.read()) != -1) {
|
||||
memory.write(pos++, (byte) next, false, true);
|
||||
}
|
||||
c.getCpu().JSR(start);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(AssemblyHandler.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
});
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(AssemblyHandler.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
ByteBuffer input = lastResult.getCompiledAsset();
|
||||
input.rewind();
|
||||
int startLSB = input.get();
|
||||
int startMSB = input.get();
|
||||
int start = startLSB + startMSB << 8;
|
||||
System.out.printf("Issuing JSR to $%s%n", Integer.toHexString(start));
|
||||
c.getCpu().whileSuspended(() -> {
|
||||
int pos = start;
|
||||
while (input.hasRemaining()) {
|
||||
memory.write(pos++, input.get(), false, true);
|
||||
}
|
||||
c.getCpu().JSR(start);
|
||||
});
|
||||
});
|
||||
}
|
||||
clean(lastResult);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clean(CompileResult<File> lastResult) {
|
||||
if (lastResult.getCompiledAsset() != null) {
|
||||
lastResult.getCompiledAsset().delete();
|
||||
}
|
||||
public void clean(CompileResult<ByteBuffer> lastResult) {
|
||||
// Nothing to do here
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user