Fixed typos and code cleanup

This commit is contained in:
simon 2018-01-05 20:14:38 -05:00
parent ce0584a49f
commit 8b0460f5f1
15 changed files with 68 additions and 182 deletions

View File

@ -24,7 +24,7 @@ public class Assembler {
private RegisterSet registerSet;
private static final int MEM_SIZE = 0x10000; // Size in words
private static final int MEM_SIZE = 0x10000; // Size in words todo load from config
public Assembler(InstructionSet instructionSet, RegisterSet registerSet, ServerConfiguration config) {
this.instructionSet = instructionSet;
@ -256,25 +256,25 @@ public class Assembler {
}
/**
* Check for and handle segment declarations (.text & .data)
* Check for and handle section declarations (.text & .data)
*
* @param line Current line
*/
private static void checkForSegmentDeclaration(String line, AssemblyResult result,
private static void checkForSectionDeclaration(String line, AssemblyResult result,
int currentLine, int currentOffset) throws AssemblyException {
String[] tokens = line.split("\\s+");
if (tokens[0].toUpperCase().equals(".TEXT")) {
result.defineSegment(Segment.TEXT, currentLine, currentOffset);
result.defineSecton(Section.TEXT, currentLine, currentOffset);
throw new PseudoInstructionException(currentLine);
} else if (tokens[0].toUpperCase().equals(".DATA")) {
LogManager.LOGGER.fine("DEBUG: .data @" + currentLine);
result.defineSegment(Segment.DATA, currentLine, currentOffset);
result.defineSecton(Section.DATA, currentLine, currentOffset);
throw new PseudoInstructionException(currentLine);
}
}
@ -385,7 +385,7 @@ public class Assembler {
}
//Check for pseudo instructions
checkForSegmentDeclaration(line, result, currentLine, currentOffset);
checkForSectionDeclaration(line, result, currentLine, currentOffset);
checkForEQUInstruction(line, result.labels, currentLine);
checkForORGInstruction(line, result, currentLine);

View File

@ -2,7 +2,7 @@ package net.simon987.server.assembly;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.exception.AssemblyException;
import net.simon987.server.assembly.exception.DuplicateSegmentException;
import net.simon987.server.assembly.exception.DuplicateSectionException;
import net.simon987.server.logging.LogManager;
import java.nio.ByteBuffer;
@ -31,11 +31,11 @@ public class AssemblyResult {
/**
* Offset of the code segment
*/
private int codeSegmentOffset;
private int codeSectionOffset;
/**
* Line of the code segment definition
*/
private int codeSegmentLine;
private int codeSectionLine;
/**
* The encoded user code (will be incomplete or invalid if the
@ -45,60 +45,60 @@ public class AssemblyResult {
/**
* Offset of the data segment
*/
private int dataSegmentOffset;
private int dataSectionOffset;
/**
* Line of the data segment definition
*/
private int dataSegmentLine;
private int dataSectionLine;
/**
* Whether or not the code segment is set
*/
private boolean codeSegmentSet = false;
private boolean codeSectionSet = false;
/**
* Whether or not the data segment is set
*/
private boolean dataSegmentSet = false;
private boolean dataSectionSet = false;
AssemblyResult(ServerConfiguration config) {
origin = config.getInt("org_offset");
}
/**
* Define a segment.
* Define a section.
*
* @param segment Segment to define
* @param currentOffset Current offset, in bytes of the segment
* @param currentLine Line number of the segment declaration
* @throws DuplicateSegmentException when a segment is defined twice
* @param section Section to define
* @param currentOffset Current offset, in bytes of the section
* @param currentLine Line number of the section declaration
* @throws DuplicateSectionException when a section is defined twice
*/
void defineSegment(Segment segment, int currentLine, int currentOffset) throws DuplicateSegmentException {
void defineSecton(Section section, int currentLine, int currentOffset) throws DuplicateSectionException {
if (segment == Segment.TEXT) {
//Code segment
if (section == Section.TEXT) {
//Code section
if (!codeSegmentSet) {
codeSegmentOffset = origin + currentOffset;
codeSegmentLine = currentLine;
if (!codeSectionSet) {
codeSectionOffset = origin + currentOffset;
codeSectionLine = currentLine;
LogManager.LOGGER.fine("DEBUG: .text offset @" + codeSegmentOffset);
LogManager.LOGGER.fine("DEBUG: .text offset @" + codeSectionOffset);
codeSegmentSet = true;
codeSectionSet = true;
} else {
throw new DuplicateSegmentException(currentLine);
throw new DuplicateSectionException(currentLine);
}
} else {
//Data segment
if (!dataSegmentSet) {
dataSegmentOffset = origin + currentOffset;
dataSegmentLine = currentLine;
//Data section
if (!dataSectionSet) {
dataSectionOffset = origin + currentOffset;
dataSectionLine = currentLine;
LogManager.LOGGER.fine("DEBUG: .data offset @" + dataSegmentOffset);
LogManager.LOGGER.fine("DEBUG: .data offset @" + dataSectionOffset);
dataSegmentSet = true;
dataSectionSet = true;
} else {
throw new DuplicateSegmentException(currentLine);
throw new DuplicateSectionException(currentLine);
}
}
@ -113,9 +113,9 @@ public class AssemblyResult {
return assembledCode;
}
public int getCodeSegmentOffset() {
if (codeSegmentSet) {
return codeSegmentOffset;
public int getCodeSectionOffset() {
if (codeSectionSet) {
return codeSectionOffset;
} else {
return origin;
}

View File

@ -43,10 +43,10 @@ public class CPU implements MongoSerialisable {
private RegisterSet registerSet;
/**
* Offset of the code segment. The code starts to get
* Offset of the code section. The code starts to get
* executed at this address each tick. Defaults to org_offset@config.properties
*/
private int codeSegmentOffset;
private int codeSectionOffset;
/**
* Instruction pointer, always points to the next instruction
@ -73,7 +73,7 @@ public class CPU implements MongoSerialisable {
instructionSet = new DefaultInstructionSet();
registerSet = new DefaultRegisterSet();
attachedHardware = new HashMap<>();
codeSegmentOffset = config.getInt("org_offset");
codeSectionOffset = config.getInt("org_offset");
instructionSet.add(new JmpInstruction(this));
instructionSet.add(new JnzInstruction(this));
@ -112,7 +112,7 @@ public class CPU implements MongoSerialisable {
public void reset() {
status.clear();
ip = codeSegmentOffset;
ip = codeSectionOffset;
}
public int execute(int timeout) {
@ -352,7 +352,7 @@ public class CPU implements MongoSerialisable {
dbObject.put("memory", memory.mongoSerialise());
dbObject.put("registerSet", registerSet.mongoSerialise());
dbObject.put("codeSegmentOffset", codeSegmentOffset);
dbObject.put("codeSegmentOffset", codeSectionOffset);
BasicDBList hardwareList = new BasicDBList();
@ -375,7 +375,7 @@ public class CPU implements MongoSerialisable {
CPU cpu = new CPU(GameServer.INSTANCE.getConfig(), user);
cpu.codeSegmentOffset = (int) obj.get("codeSegmentOffset");
cpu.codeSectionOffset = (int) obj.get("codeSegmentOffset");
BasicDBList hardwareList = (BasicDBList) obj.get("hardware");
@ -416,8 +416,8 @@ public class CPU implements MongoSerialisable {
this.ip = ip;
}
public void setCodeSegmentOffset(int codeSegmentOffset) {
this.codeSegmentOffset = codeSegmentOffset;
public void setCodeSectionOffset(int codeSectionOffset) {
this.codeSectionOffset = codeSectionOffset;
}
public void attachHardware(CpuHardware hardware, int address) {

View File

@ -6,7 +6,6 @@ import com.mongodb.DBObject;
import net.simon987.server.GameServer;
import net.simon987.server.io.MongoSerialisable;
import net.simon987.server.logging.LogManager;
import org.json.simple.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@ -157,36 +156,6 @@ public class Memory implements Target, MongoSerialisable {
return memory;
}
public static Memory deserialize(JSONObject json) {
Memory memory = new Memory(0);
String zipBytesStr = (String) json.get("zipBytes");
if (zipBytesStr != null) {
byte[] compressedBytes = Base64.getDecoder().decode((String) json.get("zipBytes"));
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Inflater decompressor = new Inflater(true);
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(baos, decompressor);
inflaterOutputStream.write(compressedBytes);
inflaterOutputStream.close();
memory.setBytes(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
} else {
LogManager.LOGGER.severe("Memory was manually deleted");
memory = new Memory(GameServer.INSTANCE.getConfig().getInt("memory_size"));
}
return memory;
}
public void setBytes(byte[] bytes) {
this.words = new char[bytes.length / 2];
ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asCharBuffer().get(this.words);

View File

@ -3,10 +3,9 @@ package net.simon987.server.assembly;
/**
* Section of a user-created program.
* The execution will start at the beginning of the code
* segment and a warning message will be displayed when execution
* reached the data segment during debugging
* segment.
*/
public enum Segment {
public enum Section {
/**
* Code section of the program. Contains executable code

View File

@ -3,17 +3,17 @@ package net.simon987.server.assembly.exception;
/**
* Threw when a user attempts to define the same section twice
*/
public class DuplicateSegmentException extends AssemblyException {
public class DuplicateSectionException extends AssemblyException {
/**
* Message of the exception
*/
private static final String message = "Segments can only be defined once";
private static final String message = "Sections can only be defined once";
/**
* Create a new Duplicate Segment Exception
* Create a new Duplicate Section Exception
*/
public DuplicateSegmentException(int line) {
public DuplicateSectionException(int line) {
super(message, line);
}
}

View File

@ -11,7 +11,7 @@ public class FatalAssemblyException extends AssemblyException {
private static final String message = "A fatal assembly error has occurred";
/**
* Create a new Duplicate Segment Exception
* Create a new Duplicate Section Exception
*/
public FatalAssemblyException(String msg, int line) {
super(msg, line);

View File

@ -7,12 +7,14 @@ public class ObjectDeathEvent extends GameEvent {
/**
* The GameObject type ID of object that init this event
*/
private int sourceObjectId;
private long sourceObjectId;
public ObjectDeathEvent(Object source, int sourceObjectId) {
setSource(source);
this.sourceObjectId = sourceObjectId;
}
public int getSourceObjectId() { return sourceObjectId; }
public long getSourceObjectId() {
return sourceObjectId;
}
}

View File

@ -1,26 +0,0 @@
package net.simon987.server.game;
/**
* Types of GameEffects
*/
public enum EffectType {
/**
* Warning icon
*/
WARNING,
/**
* Error icon
*/
ERROR,
/**
* Dig particle effect
*/
DIG,
/**
* 'A' Icon
*/
A_EMOTE
}

View File

@ -1,64 +0,0 @@
package net.simon987.server.game;
import net.simon987.server.io.JSONSerialisable;
import org.json.simple.JSONObject;
/**
* Represents a game effect in a World (e.g. Particles made when digging, Error animation, Attack effects etc..)
* <br>
* These effects are purely visual and could be changed or ignored by the client
*/
public class GameEffect implements JSONSerialisable {
/**
* Type of the effect
*/
private EffectType type;
private int x;
private int y;
public GameEffect(EffectType type, int x, int y) {
this.type = type;
this.x = x;
this.y = y;
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("x", x);
json.put("y", y);
json.put("type", type);
return json;
}
public EffectType getType() {
return type;
}
public void setType(EffectType type) {
this.type = type;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}

View File

@ -110,7 +110,7 @@ public class GameUniverse {
char[] assembledCode = ar.getWords();
user.getCpu().getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length);
user.getCpu().setCodeSegmentOffset(ar.getCodeSegmentOffset());
user.getCpu().setCodeSectionOffset(ar.getCodeSectionOffset());
} else {

View File

@ -8,7 +8,7 @@ import java.util.Collections;
/**
* Class to compute paths in the game universe. It supports
* paths between within the same World.
* paths within the same World.
*/
public class Pathfinder {

View File

@ -31,7 +31,7 @@ public class CodeUploadHandler implements MessageHandler {
char[] assembledCode = ar.getWords();
user.getUser().getCpu().getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length);
user.getUser().getCpu().setCodeSegmentOffset(ar.getCodeSegmentOffset());
user.getUser().getCpu().setCodeSectionOffset(ar.getCodeSectionOffset());
//Clear keyboard buffer
if (user.getUser().getControlledUnit() != null &&

View File

@ -2,6 +2,7 @@ package net.simon987.server.webserver;
import net.simon987.server.logging.LogManager;
import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@ -28,9 +29,13 @@ public class MessageEventDispatcher {
try {
JSONObject json = (JSONObject) parser.parse(message);
if (json.containsKey("t")) {
if (json.containsKey("t") && user.getWebSocket().isOpen()) {
for (MessageHandler handler : handlers) {
handler.handle(user, json);
try {
handler.handle(user, json);
} catch (WebsocketNotConnectedException e) {
LogManager.LOGGER.fine("Catched WebsocketNotConnectedException");
}
}
} else {
LogManager.LOGGER.severe("Malformed JSON sent by " + user.getUser().getUsername());

View File

@ -1,9 +1,10 @@
package net.simon987.server.webserver;
import org.java_websocket.exceptions.WebsocketNotConnectedException;
import org.json.simple.JSONObject;
public interface MessageHandler {
void handle(OnlineUser user, JSONObject json);
void handle(OnlineUser user, JSONObject json) throws WebsocketNotConnectedException;
}