Merge branch 'master' into vaults

# Conflicts:
#	Server/src/main/java/net/simon987/server/GameServer.java
#	Server/src/main/java/net/simon987/server/game/World.java
This commit is contained in:
simon
2018-01-17 20:29:37 -05:00
46 changed files with 698 additions and 643 deletions

View File

@@ -9,7 +9,6 @@ import net.simon987.server.event.TickEvent;
import net.simon987.server.game.DayNightCycle;
import net.simon987.server.game.GameUniverse;
import net.simon987.server.game.World;
import net.simon987.server.io.FileUtils;
import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.PluginManager;
import net.simon987.server.user.User;
@@ -18,8 +17,6 @@ import net.simon987.server.crypto.CryptoProvider;
import java.io.File;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class GameServer implements Runnable {
@@ -39,11 +36,20 @@ public class GameServer implements Runnable {
private CryptoProvider cryptoProvider;
private MongoClient mongo = null;
public GameServer() {
this.config = new ServerConfiguration("config.properties");
try{
mongo = new MongoClient("localhost", 27017);
} catch (UnknownHostException e) {
e.printStackTrace();
}
gameUniverse = new GameUniverse(config);
gameUniverse.setMongo(mongo);
pluginManager = new PluginManager();
maxExecutionTime = config.getInt("user_timeout");
@@ -130,8 +136,7 @@ public class GameServer implements Runnable {
//Process user code
ArrayList<User> users_ = new ArrayList<>(gameUniverse.getUsers());
for (User user : users_) {
for (User user : gameUniverse.getUsers()) {
if (user.getCpu() != null) {
try {
@@ -151,10 +156,8 @@ public class GameServer implements Runnable {
}
//Process each worlds
//Avoid concurrent modification
ArrayList<World> worlds = new ArrayList<>(gameUniverse.getWorlds());
int updatedWorlds = 0;
for (World world : worlds) {
for (World world : gameUniverse.getWorlds()) {
if (world.shouldUpdate()) {
world.update();
updatedWorlds++;
@@ -166,115 +169,100 @@ public class GameServer implements Runnable {
save();
}
// Clean up history files
if (gameUniverse.getTime() % config.getInt("clean_interval") == 0) {
FileUtils.cleanHistory(config.getInt("history_size"));
}
socketServer.tick();
LogManager.LOGGER.info("Processed " + gameUniverse.getWorlds().size() + " worlds (" + updatedWorlds +
LogManager.LOGGER.info("Processed " + gameUniverse.getWorldCount() + " worlds (" + updatedWorlds +
") updated");
}
void load() {
LogManager.LOGGER.info("Loading from MongoDB");
MongoClient mongo;
try {
mongo = new MongoClient("localhost", 27017);
LogManager.LOGGER.info("Loading all data from MongoDB");
DB db = mongo.getDB("mar");
DB db = mongo.getDB("mar");
DBCollection worlds = db.getCollection("world");
DBCollection users = db.getCollection("user");
DBCollection server = db.getCollection("server");
DBCollection worlds = db.getCollection("world");
DBCollection users = db.getCollection("user");
DBCollection server = db.getCollection("server");
//Load worlds
DBCursor cursor = worlds.find();
while (cursor.hasNext()) {
GameServer.INSTANCE.getGameUniverse().getWorlds().add(World.deserialize(cursor.next()));
}
//Load users
cursor = users.find();
while (cursor.hasNext()) {
try {
GameServer.INSTANCE.getGameUniverse().getUsers().add(User.deserialize(cursor.next()));
} catch (CancelledException e) {
e.printStackTrace();
}
}
//Load misc server info
cursor = server.find();
if (cursor.hasNext()) {
DBObject serverObj = cursor.next();
gameUniverse.setTime((long) serverObj.get("time"));
gameUniverse.setNextObjectId((long) serverObj.get("nextObjectId"));
}
LogManager.LOGGER.info("Done loading! W:" + GameServer.INSTANCE.getGameUniverse().getWorlds().size() +
" | U:" + GameServer.INSTANCE.getGameUniverse().getUsers().size());
} catch (UnknownHostException e) {
e.printStackTrace();
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("shouldUpdate", true);
DBCursor cursor = worlds.find(whereQuery);
GameUniverse universe = GameServer.INSTANCE.getGameUniverse();
while (cursor.hasNext()) {
World w = World.deserialize(cursor.next());
universe.addWorld(w);
}
//Load users
cursor = users.find();
while (cursor.hasNext()) {
try {
universe.addUser(User.deserialize(cursor.next()));
} catch (CancelledException e) {
e.printStackTrace();
}
}
//Load misc server info
cursor = server.find();
if (cursor.hasNext()) {
DBObject serverObj = cursor.next();
gameUniverse.setTime((long) serverObj.get("time"));
gameUniverse.setNextObjectId((long) serverObj.get("nextObjectId"));
}
LogManager.LOGGER.info("Done loading! W:" + GameServer.INSTANCE.getGameUniverse().getWorldCount() +
" | U:" + GameServer.INSTANCE.getGameUniverse().getUserCount());
}
private void save() {
LogManager.LOGGER.info("Saving to MongoDB | W:" + gameUniverse.getWorlds().size() + " | U:" + gameUniverse.getUsers().size());
LogManager.LOGGER.info("Saving to MongoDB | W:" + gameUniverse.getWorldCount() + " | U:" + gameUniverse.getUserCount());
try{
DB db = mongo.getDB("mar");
MongoClient mongo;
try {
mongo = new MongoClient("localhost", 27017);
int unloaded_worlds = 0;
DB db = mongo.getDB("mar");
DBCollection worlds = db.getCollection("world");
DBCollection users = db.getCollection("user");
DBCollection server = db.getCollection("server");
db.dropDatabase(); //Todo: Update database / keep history instead of overwriting
DBCollection worlds = db.getCollection("world");
DBCollection users = db.getCollection("user");
DBCollection server = db.getCollection("server");
List<DBObject> worldDocuments = new ArrayList<>();
int perBatch = 35;
int insertedWorlds = 0;
ArrayList<World> worlds_ = new ArrayList<>(GameServer.INSTANCE.getGameUniverse().getWorlds());
for (World w : worlds_) {
worldDocuments.add(w.mongoSerialise());
int insertedWorlds = 0;
GameUniverse universe = GameServer.INSTANCE.getGameUniverse();
for (World w : universe.getWorlds()) {
// LogManager.LOGGER.fine("Saving world "+w.getId()+" to mongodb");
insertedWorlds++;
worlds.save(w.mongoSerialise());
if (worldDocuments.size() >= perBatch || insertedWorlds >= GameServer.INSTANCE.getGameUniverse().getWorlds().size()) {
worlds.insert(worldDocuments);
worldDocuments.clear();
}
}
// If the world should unload, it is removed from the Universe after having been saved.
if (w.shouldUnload()){
unloaded_worlds++;
// LogManager.LOGGER.fine("Unloading world "+w.getId()+" from universe");
universe.removeWorld(w);
}
}
List<DBObject> userDocuments = new ArrayList<>();
int insertedUsers = 0;
ArrayList<User> users_ = new ArrayList<>(GameServer.INSTANCE.getGameUniverse().getUsers());
for (User u : users_) {
for (User u : GameServer.INSTANCE.getGameUniverse().getUsers()) {
insertedUsers++;
if (!u.isGuest()) {
users.save(u.mongoSerialise());
}
if (!u.isGuest()) {
userDocuments.add(u.mongoSerialise());
}
}
if (userDocuments.size() >= perBatch || insertedUsers >= GameServer.INSTANCE.getGameUniverse().getUsers().size()) {
users.insert(userDocuments);
userDocuments.clear();
}
}
BasicDBObject serverObj = new BasicDBObject();
serverObj.put("_id","serverinfo"); // a constant id ensures only one entry is kept and updated, instead of a new entry created every save.
serverObj.put("time", gameUniverse.getTime());
serverObj.put("nextObjectId", gameUniverse.getNextObjectId());
server.save(serverObj);
BasicDBObject serverObj = new BasicDBObject();
serverObj.put("time", gameUniverse.getTime());
serverObj.put("nextObjectId", gameUniverse.getNextObjectId());
server.insert(serverObj);
LogManager.LOGGER.info("Done!");
} catch (UnknownHostException e) {
LogManager.LOGGER.info(""+insertedWorlds+" worlds saved, "+unloaded_worlds+" unloaded");
LogManager.LOGGER.info("Done!");
} catch (Exception e) {
LogManager.LOGGER.severe("Problem happened during save function");
e.printStackTrace();
}
}

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;
@@ -54,7 +54,7 @@ public class Assembler {
*/
private static String removeLabel(String line) {
return line.replaceAll("\\b\\w*\\b:", "");
return line.replaceAll("^\\s*\\b\\w*\\b:", "");
}
@@ -97,11 +97,11 @@ public class Assembler {
line = removeComment(line);
//Check for labels
Pattern pattern = Pattern.compile("\\b\\w*\\b:");
Pattern pattern = Pattern.compile("^\\s*\\b\\w*\\b:");
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
String label = matcher.group(0).substring(0, matcher.group(0).length() - 1);
String label = matcher.group(0).substring(0, matcher.group(0).length() - 1).trim();
LogManager.LOGGER.fine("DEBUG: Label " + label + " @ " + (result.origin + currentOffset));
result.labels.put(label, (char) (result.origin + currentOffset));
@@ -175,7 +175,18 @@ public class Assembler {
out.writeChar(0);
} else {
throw new InvalidOperandException("Invalid operand \"" + value + '"', currentLine);
//Integer.decode failed, try binary
if (value.startsWith("0b")) {
try {
out.writeChar(Integer.parseInt(value.substring(2), 2));
} catch (NumberFormatException e2) {
throw new InvalidOperandException("Invalid operand \"" + value + '"', currentLine);
}
} else {
throw new InvalidOperandException("Invalid operand \"" + value + '"', currentLine);
}
}
}
}
@@ -256,25 +267,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);
}
}
@@ -296,7 +307,7 @@ public class Assembler {
String[] tokens = line.split("\\s+");
if (line.toUpperCase().contains(" EQU ")) {
if (line.toUpperCase().matches(".*\\bEQU\\b.*")) {
if (tokens[1].toUpperCase().equals("EQU") && tokens.length == 3) {
try {
//Save value as a label
@@ -385,7 +396,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;
@@ -27,15 +27,15 @@ public class AssemblyResult {
* List of exceptions encountered during the assembly attempt,
* they will be displayed in the editor
*/
ArrayList<AssemblyException> exceptions = new ArrayList<>(50);
public ArrayList<AssemblyException> exceptions = new ArrayList<>(50);
/**
* 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

@@ -44,6 +44,8 @@ public class DefaultInstructionSet implements InstructionSet {
add(new RclInstruction());
add(new RcrInstruction());
add(new SarInstruction());
add(new IncInstruction());
add(new DecInstruction());
}
/**

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

@@ -224,11 +224,30 @@ public class Operand {
}
//label is invalid
data = Integer.decode(expr);
value += registerSet.size() * 2; //refers to memory with disp
return true;
} catch (NumberFormatException e) {
//Integer.decode failed, try binary
if (expr.startsWith("+0b")) {
try {
data = Integer.parseInt(expr.substring(3), 2);
value += registerSet.size() * 2; //refers to memory with disp
return true;
} catch (NumberFormatException e2) {
return false;
}
} else if (expr.startsWith("-0b")) {
try {
data = -Integer.parseInt(expr.substring(3), 2);
value += registerSet.size() * 2; //refers to memory with disp
return true;
} catch (NumberFormatException e2) {
return false;
}
}
return false;
}
}

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

@@ -0,0 +1,32 @@
package net.simon987.server.assembly.instruction;
import net.simon987.server.assembly.Instruction;
import net.simon987.server.assembly.Status;
import net.simon987.server.assembly.Target;
import net.simon987.server.assembly.Util;
public class DecInstruction extends Instruction {
public static final int OPCODE = 0x2B;
public DecInstruction() {
super("dec", OPCODE);
}
@Override
public Status execute(Target dst, int dstIndex, Status status) {
char a = (char) dst.get(dstIndex);
int result = a - 1;
// Like x86 Carry flag is preserved during INC/DEC
// (Use ADD x, 1 to have carry flag change)
// Other flags set according to result
status.setSignFlag(Util.checkSign16(result));
status.setZeroFlag((char) result == 0);
status.setOverflowFlag(Util.checkOverFlowSub16(a, 1));
dst.set(dstIndex, result);
return status;
}
}

View File

@@ -0,0 +1,32 @@
package net.simon987.server.assembly.instruction;
import net.simon987.server.assembly.Instruction;
import net.simon987.server.assembly.Status;
import net.simon987.server.assembly.Target;
import net.simon987.server.assembly.Util;
public class IncInstruction extends Instruction {
public static final int OPCODE = 0x2A;
public IncInstruction() {
super("inc", OPCODE);
}
@Override
public Status execute(Target dst, int dstIndex, Status status) {
char a = (char) dst.get(dstIndex);
int result = a + 1;
// Like x86 Carry flag is preserved during INC/DEC
// (Use ADD x, 1 to have carry flag change)
// Other flags set according to result
status.setSignFlag(Util.checkSign16(result));
status.setZeroFlag((char) result == 0);
status.setOverflowFlag(Util.checkOverFlowAdd16(a, 1));
dst.set(dstIndex, result);
return status;
}
}

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

@@ -84,9 +84,9 @@ public abstract class GameObject implements JSONSerialisable, MongoSerialisable
}
if (leftWorld != null) {
world.getGameObjects().remove(this);
world.removeObject(this);
world.decUpdatable();
leftWorld.getGameObjects().add(this);
leftWorld.addObject(this);
leftWorld.incUpdatable();
setWorld(leftWorld);
@@ -103,9 +103,9 @@ public abstract class GameObject implements JSONSerialisable, MongoSerialisable
}
if (rightWorld != null) {
world.getGameObjects().remove(this);
world.removeObject(this);
world.decUpdatable();
rightWorld.getGameObjects().add(this);
rightWorld.addObject(this);
rightWorld.incUpdatable();
setWorld(rightWorld);
@@ -123,9 +123,9 @@ public abstract class GameObject implements JSONSerialisable, MongoSerialisable
}
if (upWorld != null) {
world.getGameObjects().remove(this);
world.removeObject(this);
world.decUpdatable();
upWorld.getGameObjects().add(this);
upWorld.addObject(this);
upWorld.incUpdatable();
setWorld(upWorld);
@@ -143,9 +143,9 @@ public abstract class GameObject implements JSONSerialisable, MongoSerialisable
if (downWorld != null) {
world.getGameObjects().remove(this);
world.removeObject(this);
world.decUpdatable();
downWorld.getGameObjects().add(this);
downWorld.addObject(this);
downWorld.incUpdatable();
setWorld(downWorld);

View File

@@ -1,5 +1,6 @@
package net.simon987.server.game;
import com.mongodb.*;
import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.Assembler;
@@ -9,14 +10,20 @@ import net.simon987.server.assembly.exception.CancelledException;
import net.simon987.server.logging.LogManager;
import net.simon987.server.user.User;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
public class GameUniverse {
private ArrayList<World> worlds;
private ArrayList<User> users;
//private ArrayList<World> worlds;
private ConcurrentHashMap<String, World> worlds;
//username:user
private ConcurrentHashMap<String, User> users;
private WorldGenerator worldGenerator;
private MongoClient mongo = null;
private long time;
private long nextObjectId = 0;
@@ -25,64 +32,144 @@ public class GameUniverse {
public GameUniverse(ServerConfiguration config) {
worlds = new ArrayList<>(32);
users = new ArrayList<>(16);
worlds = new ConcurrentHashMap<>(256);
users = new ConcurrentHashMap<>(16);
worldGenerator = new WorldGenerator(config);
}
public void setMongo(MongoClient mongo){
this.mongo = mongo;
}
public long getTime() {
return time;
}
public World getWorld(int x, int y, boolean createNew) {
/**
* Attempts loading a world from mongoDB by coordinates
*
* @param x the x coordinate of the world
* @param y the y coordinate of the world
*
* @return World, null if not found
*/
private World loadWorld(int x, int y){
DB db = mongo.getDB("mar");
DBCollection worlds = db.getCollection("world");
for (World world : worlds) {
if (world.getX() == x && world.getY() == y) {
return world;
}
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("_id", World.idFromCoordinates(x,y));
DBCursor cursor = worlds.find(whereQuery);
if (cursor.hasNext()) {
World w = World.deserialize(cursor.next());
return w;
}
if (x >= 0 && x <= maxWidth && y >= 0 && y <= maxWidth) {
if (createNew) {
//World does not exist
World world = createWorld(x, y);
worlds.add(world);
return world;
} else {
return null;
}
} else {
else{
return null;
}
}
public World createWorld(int x, int y) {
/**
* Get a world by coordinates, attempts to load from mongoDB if not found.
*
* @param x the x coordinate of the world
* @param y the y coordinate of the world
* @param createNew if true, a new world is created when a world with given coordinates is not found
*
* @return World, null if not found and not created.
*/
public World getWorld(int x, int y, boolean createNew) {
// Wrapping coordinates around cyclically
x %= maxWidth+1;
y %= maxWidth+1;
// Looks for a previously loaded world
World world = getLoadedWorld(x,y);
if (world != null){
return world;
}
// Tries loading the world from the database
world = loadWorld(x,y);
if (world != null){
addWorld(world);
LogManager.LOGGER.fine("Loaded world "+(World.idFromCoordinates(x,y))+" from mongodb.");
return world;
}
// World does not exist
if (createNew) {
// Creates a new world
world = createWorld(x, y);
addWorld(world);
LogManager.LOGGER.fine("Created new world "+(World.idFromCoordinates(x,y))+".");
return world;
} else {
return null;
}
}
public World getLoadedWorld(int x, int y) {
// Wrapping coordinates around cyclically
x %= maxWidth+1;
y %= maxWidth+1;
return worlds.get(World.idFromCoordinates(x,y));
}
/**
* Adds a new or freshly loaded world to the universe (if not already present).
*
* @param world the world to be added
*/
public void addWorld(World world){
World w = worlds.get(world.getId());
if (w == null){
world.setUniverse(this);
worlds.put(world.getId(),world);
}
}
/**
* Removes the world with given coordinates from the universe.
*
* @param x the x coordinate of the world to be removed
* @param y the y coordinate of the world to be removed
*/
public void removeWorld(int x, int y){
World w = worlds.remove(World.idFromCoordinates(x,y));
if (w != null){
w.setUniverse(null);
}
}
/**
* Removes the given world from the universe.
*
* @param world the world to be removed.
*/
public void removeWorld(World world){
World w = worlds.remove(world.getId());
if (w != null){
w.setUniverse(null);
}
}
private World createWorld(int x, int y) {
World world = null;
try {
world = worldGenerator.generateWorld(x, y);
} catch (CancelledException e) {
e.printStackTrace();
}
return world;
}
public User getUser(String username) {
for (User user : users) {
if (user.getUsername().equals(username)) {
return user;
}
}
return null;
return users.get(username);
}
public User getOrCreateUser(String username, boolean makeControlledUnit) {
@@ -110,7 +197,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 {
@@ -119,7 +206,7 @@ public class GameUniverse {
user.setUsername(username);
users.add(user);
addUser(user);
return user;
@@ -141,12 +228,11 @@ public class GameUniverse {
*/
public GameObject getObject(long id) {
//
for (World world : worlds) {
for (GameObject object : world.getGameObjects()) {
if (object.getObjectId() == id) {
return object;
}
for (World world : getWorlds()) {
GameObject obj = world.findObject(id);
if (obj != null) {
return obj;
}
}
@@ -159,12 +245,20 @@ public class GameUniverse {
time++;
}
public ArrayList<World> getWorlds() {
return worlds;
public Collection<World> getWorlds() {
return worlds.values();
}
public ArrayList<User> getUsers() {
return users;
public int getWorldCount() {
return worlds.size();
}
public Collection<User> getUsers() {
return users.values();
}
public int getUserCount() {
return users.size();
}
public long getNextObjectId() {
@@ -187,8 +281,12 @@ public class GameUniverse {
}
public void addUser(User user) {
users.put(user.getUsername(), user);
}
public void removeUser(User user) {
users.remove(user);
users.remove(user.getUsername());
}
public int getMaxWidth() {

View File

@@ -11,7 +11,9 @@ import net.simon987.server.io.MongoSerialisable;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
public class World implements MongoSerialisable {
@@ -29,7 +31,7 @@ public class World implements MongoSerialisable {
private TileMap tileMap;
private ArrayList<GameObject> gameObjects = new ArrayList<>(16);
private ConcurrentHashMap<Long, GameObject> gameObjects = new ConcurrentHashMap<>(8);
/**
* If this number is greater than 0, the World will be updated.
@@ -58,7 +60,28 @@ public class World implements MongoSerialisable {
public boolean isTileBlocked(int x, int y) {
return getGameObjectsBlockingAt(x, y).size() > 0 || tileMap.getTileAt(x, y) == TileMap.WALL_TILE;
}
/**
* Computes the world's unique id from its coordinates.
*
* @param x the x coordinate of the world
* @param y the y coordinate of the world
*
* @return long
*/
public static String idFromCoordinates(int x, int y){
return "w-"+"0x"+Integer.toHexString(x)+"-"+"0x"+Integer.toHexString(y);
//return ((long)x)*(((long)maxWidth)+1)+((long)y);
}
/**
* Returns the world's unique id, computed with idFromCoordinates.
*
* @return long
*/
public String getId(){
return World.idFromCoordinates(x,y);
}
public int getX() {
@@ -69,26 +92,48 @@ public class World implements MongoSerialisable {
return y;
}
/**
* Get all the game objects that are instances of the specified class
*/
public ArrayList getGameObjects(Class<? extends GameObject> clazz) {
public ArrayList<GameObject> findObjects(Class clazz) {
ArrayList<GameObject> objects = new ArrayList<>(gameObjects.size());
ArrayList<GameObject> matchingObjects = new ArrayList<>(2);
for (GameObject object : gameObjects) {
if (object.getClass().equals(clazz)) {
objects.add(object);
for (GameObject obj : gameObjects.values()) {
if (obj.getClass().equals(clazz)) {
matchingObjects.add(obj);
}
}
return objects;
return matchingObjects;
}
public ArrayList<GameObject> getGameObjects() {
return gameObjects;
public ArrayList<GameObject> findObjects(int mapInfo) {
ArrayList<GameObject> matchingObjects = new ArrayList<>(2);
for (GameObject obj : gameObjects.values()) {
if ((obj.getMapInfo() & mapInfo) == mapInfo) {
matchingObjects.add(obj);
}
}
return matchingObjects;
}
public void addObject(GameObject object) {
gameObjects.put(object.getObjectId(), object);
}
public void removeObject(GameObject object) {
gameObjects.remove(object.getObjectId());
}
public GameObject findObject(long objectId) {
return gameObjects.get(objectId);
}
/**
* Update this World and its GameObjects
* <br>
@@ -100,13 +145,11 @@ public class World implements MongoSerialisable {
GameEvent event = new WorldUpdateEvent(this);
GameServer.INSTANCE.getEventDispatcher().dispatch(event); //Ignore cancellation
ArrayList<GameObject> gameObjects_ = new ArrayList<>(gameObjects);
for (GameObject object : gameObjects_) {
for (GameObject object : gameObjects.values()) {
//Clean up dead objects
if (object.isDead()) {
object.onDeadCallback();
gameObjects.remove(object);
removeObject(object);
//LogManager.LOGGER.fine("Removed object " + object + " id: " + object.getObjectId());
} else if (object instanceof Updatable) {
((Updatable) object).update();
@@ -120,11 +163,13 @@ public class World implements MongoSerialisable {
BasicDBObject dbObject = new BasicDBObject();
BasicDBList objects = new BasicDBList();
ArrayList<GameObject> gameObjects_ = new ArrayList<>(gameObjects);
for (GameObject obj : gameObjects_) {
for (GameObject obj : gameObjects.values()) {
objects.add(obj.mongoSerialise());
}
dbObject.put("_id", getId());
dbObject.put("objects", objects);
dbObject.put("terrain", tileMap.mongoSerialise());
@@ -133,7 +178,7 @@ public class World implements MongoSerialisable {
dbObject.put("size", worldSize);
dbObject.put("updatable", updatable);
dbObject.put("shouldUpdate",shouldUpdate());
return dbObject;
}
@@ -171,7 +216,7 @@ public class World implements MongoSerialisable {
GameObject object = GameObject.deserialize((DBObject) obj);
object.setWorld(world);
world.gameObjects.add(object);
world.addObject(object);
}
return world;
@@ -209,7 +254,7 @@ public class World implements MongoSerialisable {
}
//Objects
for (GameObject obj : this.gameObjects) {
for (GameObject obj : gameObjects.values()) {
mapInfo[obj.getX()][obj.getY()] |= obj.getMapInfo();
}
@@ -263,17 +308,16 @@ public class World implements MongoSerialisable {
*/
public ArrayList<GameObject> getGameObjectsBlockingAt(int x, int y) {
ArrayList<GameObject> gameObjects = new ArrayList<>(2);
for (GameObject obj : this.gameObjects) {
ArrayList<GameObject> objectsLooking = new ArrayList<>(2);
for (GameObject obj : gameObjects.values()) {
if (obj.isAt(x, y)) {
gameObjects.add(obj);
objectsLooking.add(obj);
}
}
return gameObjects;
return objectsLooking;
}
/**
@@ -287,17 +331,15 @@ public class World implements MongoSerialisable {
* @return the list of game objects at a location
*/
public ArrayList<GameObject> getGameObjectsAt(int x, int y) {
ArrayList<GameObject> gameObjects = new ArrayList<>(2);
ArrayList<GameObject> objectsAt = new ArrayList<>(2);
for (GameObject obj : gameObjects.values()) {
for (GameObject obj : this.gameObjects) {
if (obj.getX() == x && obj.getY() == y) {
gameObjects.add(obj);
if (obj.isAt(x, y)) {
objectsAt.add(obj);
}
}
return gameObjects;
return objectsAt;
}
public void incUpdatable() {
@@ -315,4 +357,76 @@ public class World implements MongoSerialisable {
public int getWorldSize() {
return worldSize;
}
private GameUniverse universe = null;
public void setUniverse(GameUniverse universe){
this.universe = universe;
}
public ArrayList<World> getNeighbouringLoadedWorlds(){
ArrayList<World> neighbouringWorlds = new ArrayList<>();
if (universe == null){
return neighbouringWorlds;
}
for (int dx=-1; dx<=+1; dx+=2){
World nw = universe.getLoadedWorld(x+dx,y);
if (nw != null){
neighbouringWorlds.add(nw);
}
}
for (int dy=-1; dy<=+1; dy+=2){
World nw = universe.getLoadedWorld(x,y+dy);
if (nw != null){
neighbouringWorlds.add(nw);
}
}
return neighbouringWorlds;
}
public ArrayList<World> getNeighbouringExistingWorlds(){
ArrayList<World> neighbouringWorlds = new ArrayList<>();
if (universe == null){
return neighbouringWorlds;
}
for (int dx=-1; dx<=+1; dx+=2){
World nw = universe.getWorld(x+dx,y,false);
if (nw != null){
neighbouringWorlds.add(nw);
}
}
for (int dy=-1; dy<=+1; dy+=2){
World nw = universe.getWorld(x,y+dy,false);
if (nw != null){
neighbouringWorlds.add(nw);
}
}
return neighbouringWorlds;
}
public boolean canUnload(){
return updatable==0;
}
public boolean shouldUnload(){
boolean res = canUnload();
for (World nw : getNeighbouringLoadedWorlds() ){
res &= nw.canUnload();
}
return res;
}
public Collection<GameObject> getGameObjects() {
return gameObjects.values();
}
}

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

@@ -1,35 +1,12 @@
package net.simon987.server.io;
import net.simon987.server.logging.LogManager;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileUtils {
private static final int BUFFER_SIZE = 1024;
private static final String STR_ENCODING = "UTF-8";
private static final String DATE_FORMAT = "yyyyMMddHHmmss";
private static final String FILE_TYPE = ".zip";
private static final Path ROOT_DIR;
private static final String DIR_NAME = "history";
public static final Path DIR_PATH;
static {
ROOT_DIR = Paths.get(".").normalize();
DIR_PATH = ROOT_DIR.resolve(DIR_NAME);
}
//Private constructor
private FileUtils() {
}
/**
* Creates a new stamp containing the current date and time
@@ -42,143 +19,4 @@ public class FileUtils {
return f.format(millisToDate);
}
/**
* Created a directory if none exists with the specified name
*
* @param directory folder to create
* @return true is the file exists or create operation is successful
*/
public static boolean prepDirectory(Path directory) {
File file = directory.toFile();
//If the directory exists or the directory created successfully return true
if (file.exists() || file.mkdir()) {
return true;
} else {
System.out.println("Error creating directory: " + file.toString());
return false;
}
}
/**
* Converts a file into an array of bytes
*
* @param path the file to be converted into bytes
* @return the byte array of the given file
*/
public static byte[] bytifyFile(Path path) {
byte[] bytes = null;
try {
bytes = Files.readAllBytes(path);
} catch (IOException e) {
System.out.println("Failed to extract bytes from: " + path);
e.printStackTrace();
}
return bytes;
}
/**
* Takes in a file that had been converted to a byte[] to be written to a new
* zip file
*
* @param data
* contains data in byte array form to be written, typically a file
* that has been converted with bytifyFile()
* @throws IOException
* if an error occurs during the write process
*/
public static void writeSaveToZip(String name, byte[] data) throws IOException {
String newFile = DIR_PATH.resolve(getDateTimeStamp() + FILE_TYPE).toString();
FileOutputStream output = new FileOutputStream(newFile);
ZipOutputStream stream = new ZipOutputStream(output);
byte[] buffer = new byte[BUFFER_SIZE];
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
while ((bais.read(buffer)) > -1) {
// File name
ZipEntry entry = new ZipEntry(name);
// Set to start of next entry in the stream.
stream.putNextEntry(entry);
// Data to write.
stream.write(data);
// Close the current entry.
stream.closeEntry();
}
stream.close();
output.close();
}
public static void cleanHistory(int size) {
File[] files = new File(DIR_PATH.toString()).listFiles();
File[] sorted = new File[size];
File currentFile;
boolean changed;
if (files != null) {
for (int i = 0; i < files.length / 2; i++) {
currentFile = files[i];
files[i] = files[files.length - i - 1];
files[files.length - i - 1] = currentFile;
}
for (int f = 0; f < files.length; f++) {
changed = false;
try {
long dirFile = Long.parseLong(files[f].getName().substring(0, (files[f].getName().length() - 4)));
if (f < size && sorted[f] == null) {
sorted[f] = files[f];
} else {
for (int s = 0; s < sorted.length; s++) {
long sortedFile = Long.parseLong(sorted[s].getName().substring(0, (sorted[s].getName().length() - 4)));
if (dirFile > sortedFile) {
if (s == sorted.length - 1) {
sorted[s] = files[f];
} else {
sorted[s] = files[f];
}
changed = true;
}
}
if (!changed) {
files[f].delete();
}
}
} catch (NumberFormatException e) {
LogManager.LOGGER.info("Non-save file in history directory: " + files[f].getName());
}
}
}
}
/**
* Converts a byte array into human readable format using the provided encoding
*
* @param bytes data to be encoded to String
* @return a String containing the encoded bytes
*/
public static String byteArrAsString(byte[] bytes) throws UnsupportedEncodingException {
return new String(bytes, STR_ENCODING);
}
}

View File

@@ -19,9 +19,30 @@ public class LogManager {
public static void initialize() {
LOGGER.setUseParentHandlers(false);
Handler handler = new ConsoleHandler();
/*
* Having warning/error directed to stderr
*/
Handler errHandler = new ConsoleHandler();
errHandler.setFormatter(new GenericFormatter());
errHandler.setLevel(Level.WARNING);
/*
* Only have info and below directed to stdout
*/
Handler handler = new StreamHandler(System.out, new GenericFormatter()) {
@Override
public synchronized void publish(LogRecord record) {
super.publish(record);
flush();
}
};
handler.setFilter(new Filter() {
@Override
public boolean isLoggable(LogRecord record) {
return record.getLevel().intValue() <= Level.INFO.intValue();
}
});
handler.setLevel(Level.ALL);
handler.setFormatter(new GenericFormatter());
try {
Handler fileHandler = new FileHandler("mar.log");
@@ -29,6 +50,7 @@ public class LogManager {
fileHandler.setFormatter(new GenericFormatter());
LOGGER.addHandler(handler);
LOGGER.addHandler(errHandler);
LOGGER.addHandler(fileHandler);
LOGGER.setLevel(Level.ALL);
@@ -36,6 +58,5 @@ public class LogManager {
e.printStackTrace();
}
}
}

View File

@@ -44,6 +44,7 @@ public class User implements MongoSerialisable {
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("_id", username); // a constant id ensures only one entry per user is kept and updated, instead of a new entry created every save for every user.
dbObject.put("username", username);
dbObject.put("code", userCode);
dbObject.put("controlledUnit", controlledUnit.getObjectId());

View File

@@ -21,30 +21,35 @@ public class CodeUploadHandler implements MessageHandler {
//TODO Should we wait at the end of the tick to modify the CPU ?
user.getUser().setUserCode((String) json.get("code"));
AssemblyResult ar = new Assembler(user.getUser().getCpu().getInstructionSet(),
user.getUser().getCpu().getRegisterSet(),
GameServer.INSTANCE.getConfig()).parse(user.getUser().getUserCode());
if (user.getUser().getUserCode() != null) {
AssemblyResult ar = new Assembler(user.getUser().getCpu().getInstructionSet(),
user.getUser().getCpu().getRegisterSet(),
GameServer.INSTANCE.getConfig()).parse(user.getUser().getUserCode());
user.getUser().getCpu().getMemory().clear();
user.getUser().getCpu().getMemory().clear();
//Write assembled code to mem
char[] assembledCode = ar.getWords();
//Write assembled code to mem
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().getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length);
user.getUser().getCpu().setCodeSectionOffset(ar.getCodeSectionOffset());
//Clear keyboard buffer
if (user.getUser().getControlledUnit() != null &&
user.getUser().getControlledUnit().getKeyboardBuffer() != null) {
user.getUser().getControlledUnit().getKeyboardBuffer().clear();
//Clear keyboard buffer
if (user.getUser().getControlledUnit() != null &&
user.getUser().getControlledUnit().getKeyboardBuffer() != null) {
user.getUser().getControlledUnit().getKeyboardBuffer().clear();
}
//Clear registers
user.getUser().getCpu().getRegisterSet().clear();
JSONObject response = new JSONObject();
response.put("t", "codeResponse");
response.put("bytes", ar.bytes.length);
response.put("exceptions", ar.exceptions.size());
user.getWebSocket().send(response.toJSONString());
}
JSONObject response = new JSONObject();
response.put("t", "codeResponse");
response.put("bytes", ar.bytes.length);
user.getWebSocket().send(response.toJSONString());
}

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,16 @@ 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");
} catch (Exception e1) {
LogManager.LOGGER.severe(e1.getMessage());
e1.printStackTrace();
}
}
} 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;
}

View File

@@ -7,8 +7,6 @@ import net.simon987.server.logging.LogManager;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.util.ArrayList;
public class ObjectsRequestHandler implements MessageHandler {
@@ -29,13 +27,12 @@ public class ObjectsRequestHandler implements MessageHandler {
World world = GameServer.INSTANCE.getGameUniverse().getWorld(x, y, false);
if (world != null) {
ArrayList<GameObject> gameObjects = world.getGameObjects();
JSONObject response = new JSONObject();
JSONArray objects = new JSONArray();
for (GameObject object : gameObjects) {
for (GameObject object : world.getGameObjects()) {
objects.add(object.serialise());
}

View File

@@ -0,0 +1,39 @@
package net.simon987.server.assembly;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.exception.CancelledException;
import net.simon987.server.user.User;
import org.junit.Test;
import java.util.Random;
public class CPUTest {
@Test
public void executeInstruction() throws CancelledException {
ServerConfiguration config = new ServerConfiguration("config.properties");
User user = new User();
CPU cpu = new CPU(config, user);
for(int i = 0 ; i < 3 ; i++){
//Execute every possible instruction with random values in memory
cpu.reset();
cpu.getMemory().clear();
Random random = new Random();
byte[] randomBytes = new byte[cpu.getMemory().getWords().length * 2];
random.nextBytes(randomBytes);
for (int machineCode = Character.MIN_VALUE; machineCode < Character.MAX_VALUE; machineCode++) {
Instruction instruction = cpu.getInstructionSet().get(machineCode & 0x03F); // 0000 0000 00XX XXXX
int source = (machineCode >> 11) & 0x001F; // XXXX X000 0000 0000
int destination = (machineCode >> 6) & 0x001F; // 0000 0XXX XX00 0000
cpu.executeInstruction(instruction, source, destination);
}
}
}
}

View File

@@ -0,0 +1,49 @@
package net.simon987.server.assembly;
import net.simon987.server.ServerConfiguration;
import org.junit.Test;
import static org.junit.Assert.*;
public class MemoryTest {
@Test
public void getSet() {
ServerConfiguration config = new ServerConfiguration("config.properties");
int memorySize = config.getInt("memory_size");
Memory memory = new Memory(memorySize);
memory.set(1, 1);
assertEquals(1, memory.get(1));
memory.set(memorySize / 2 - 1, 1);
assertEquals(1, memory.get(memorySize / 2 - 1));
memory.get(memorySize / 2);
memory.get(-1);
memory.set(memorySize / 2, 1);
memory.set(-1, 1);
}
@Test
public void write() {
ServerConfiguration config = new ServerConfiguration("config.properties");
int memorySize = config.getInt("memory_size");
Memory memory = new Memory(memorySize);
assertTrue(memory.write(0, new char[memorySize], 0, memorySize));
assertFalse(memory.write(0, new char[memorySize], 0, memorySize + 1));
assertFalse(memory.write(0, new char[memorySize], 0, -1));
assertFalse(memory.write(-1, new char[memorySize], 0, 10));
assertFalse(memory.write(memorySize, new char[15], 0, 1));
assertFalse(memory.write((memorySize) - 5, new char[11], 0, 6));
assertTrue(memory.write((memorySize) - 5, new char[11], 0, 5));
}
}

View File

@@ -0,0 +1,151 @@
package net.simon987.server.assembly;
import net.simon987.server.assembly.exception.InvalidOperandException;
import org.junit.Test;
import java.util.HashMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class OperandTest {
@Test
public void Operand() {
RegisterSet registerSet = new DefaultRegisterSet();
HashMap<String, Character> labels = new HashMap<>(10);
labels.put("label1", (char) 10);
labels.put("label2", (char) 20);
labels.put("label3", (char) 30);
labels.put("label4", (char) 40);
//Valid operands
try {
//Register
Operand reg1 = new Operand("a", labels, registerSet, 0);
assertEquals(OperandType.REGISTER16, reg1.getType());
assertEquals(1, reg1.getValue());
assertEquals(0, reg1.getData());
Operand reg2 = new Operand("a ", labels, registerSet, 0);
assertEquals(OperandType.REGISTER16, reg2.getType());
assertEquals(1, reg2.getValue());
assertEquals(0, reg2.getData());
Operand reg3 = new Operand(" A ", labels, registerSet, 0);
assertEquals(OperandType.REGISTER16, reg3.getType());
assertEquals(1, reg3.getValue());
assertEquals(0, reg3.getData());
Operand reg4 = new Operand("B", labels, registerSet, 0);
assertEquals(OperandType.REGISTER16, reg4.getType());
assertEquals(2, reg4.getValue());
assertEquals(0, reg4.getData());
//Immediate
Operand imm1 = new Operand(" +12", labels, registerSet, 0);
assertEquals(OperandType.IMMEDIATE16, imm1.getType());
assertEquals(Operand.IMMEDIATE_VALUE, imm1.getValue());
assertEquals(12, imm1.getData());
Operand imm2 = new Operand(" -12", labels, registerSet, 0);
assertEquals(OperandType.IMMEDIATE16, imm2.getType());
assertEquals(Operand.IMMEDIATE_VALUE, imm2.getValue());
assertEquals(-12, imm2.getData());
Operand imm3 = new Operand(" 0xABCD", labels, registerSet, 0);
assertEquals(OperandType.IMMEDIATE16, imm3.getType());
assertEquals(Operand.IMMEDIATE_VALUE, imm3.getValue());
assertEquals(0xABCD, imm3.getData());
Operand imm4 = new Operand(" label1", labels, registerSet, 0);
assertEquals(OperandType.IMMEDIATE16, imm4.getType());
assertEquals(Operand.IMMEDIATE_VALUE, imm4.getValue());
assertEquals(10, imm4.getData());
//Memory Immediate
Operand mem1 = new Operand("[+12]", labels, registerSet, 0);
assertEquals(OperandType.MEMORY_IMM16, mem1.getType());
assertEquals(Operand.IMMEDIATE_VALUE_MEM, mem1.getValue());
assertEquals(12, mem1.getData());
Operand mem2 = new Operand("[-12 ]", labels, registerSet, 0);
assertEquals(OperandType.MEMORY_IMM16, mem2.getType());
assertEquals(Operand.IMMEDIATE_VALUE_MEM, mem2.getValue());
assertEquals(-12, mem2.getData());
Operand mem3 = new Operand(" [ 0xABCD]", labels, registerSet, 0);
assertEquals(OperandType.MEMORY_IMM16, mem3.getType());
assertEquals(Operand.IMMEDIATE_VALUE_MEM, mem3.getValue());
assertEquals(0xABCD, mem3.getData());
Operand mem4 = new Operand("[ label1 ]", labels, registerSet, 0);
assertEquals(OperandType.MEMORY_IMM16, mem4.getType());
assertEquals(Operand.IMMEDIATE_VALUE_MEM, mem4.getValue());
assertEquals(10, mem4.getData());
//Memory Reg
Operand mem5 = new Operand("[ A ]", labels, registerSet, 0);
assertEquals(OperandType.MEMORY_REG16, mem5.getType());
assertEquals(1 + registerSet.size(), mem5.getValue());
assertEquals(0, mem5.getData());
Operand mem6 = new Operand("[ B ]", labels, registerSet, 0);
assertEquals(OperandType.MEMORY_REG16, mem6.getType());
assertEquals(2 + registerSet.size(), mem6.getValue());
assertEquals(0, mem6.getData());
//Memory Reg + displacement
Operand mem7 = new Operand("[ A + 1 ]", labels, registerSet, 0);
assertEquals(OperandType.MEMORY_REG_DISP16, mem7.getType());
assertEquals(1 + 2 * registerSet.size(), mem7.getValue());
assertEquals(1, mem7.getData());
Operand mem8 = new Operand("[ B + label1 ]", labels, registerSet, 0);
assertEquals(OperandType.MEMORY_REG_DISP16, mem8.getType());
assertEquals(2 + 2 * registerSet.size(), mem8.getValue());
assertEquals(10, mem8.getData());
Operand mem9 = new Operand("[ BP + 1 ]", labels, registerSet, 0);
assertEquals(OperandType.MEMORY_REG_DISP16, mem9.getType());
assertEquals(8 + 2 * registerSet.size(), mem9.getValue());
assertEquals(1, mem9.getData());
} catch (InvalidOperandException e) {
fail("Failed trying to parse a valid operand");
}
//Invalid operands
try{ new Operand("aa", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("a1", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("a_", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("_a", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("_1", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("S", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("label1_", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("+label1", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("[- 12]", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("[12+1]", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("[+label1", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("[*12]", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("[-A]", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("[A B]", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("[A + B]", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("[A + -1]", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("[A + ]", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("[]", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("[A+A+]", labels, registerSet, 0); } catch (InvalidOperandException e){}
try{ new Operand("[A+[1]]", labels, registerSet, 0); } catch (InvalidOperandException e){}
}
}

View File

@@ -0,0 +1,78 @@
package net.simon987.server.assembly;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RegisterSetTest {
@Test
public void getIndex() {
RegisterSet registerSet = new RegisterSet();
Register r1 = new Register("R1");
Register r2 = new Register("R2");
registerSet.put(1, r1);
registerSet.put(2, r2);
assertEquals(1, registerSet.getIndex("R1"));
assertEquals(2, registerSet.getIndex("R2"));
assertEquals(-1, registerSet.getIndex("Unknown register name"));
}
@Test
public void getRegister() {
RegisterSet registerSet = new RegisterSet();
Register r1 = new Register("R1");
Register r2 = new Register("R2");
registerSet.put(1, r1);
registerSet.put(2, r2);
assertEquals(r1, registerSet.getRegister("R1"));
assertEquals(r1, registerSet.getRegister(1));
assertEquals(r2, registerSet.getRegister("R2"));
assertEquals(r2, registerSet.getRegister(2));
//Test unknown registers
assertEquals(null, registerSet.getRegister("Unknown"));
assertEquals(null, registerSet.getRegister(3));
}
@Test
public void get() {
RegisterSet registerSet = new RegisterSet();
Register r1 = new Register("R1");
registerSet.put(1, r1);
r1.setValue(10);
assertEquals(10, registerSet.get(1));
//Test unknown indexes
assertEquals(0, registerSet.get(2));
}
@Test
public void set() {
RegisterSet registerSet = new RegisterSet();
Register r1 = new Register("R1");
registerSet.put(1, r1);
registerSet.set(1, 10);
assertEquals(10, r1.getValue());
//Test unknown indexes
registerSet.set(3, 10);
}
}

View File

@@ -0,0 +1,161 @@
package net.simon987.server.assembly.instruction;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.Memory;
import net.simon987.server.assembly.Register;
import net.simon987.server.assembly.RegisterSet;
import net.simon987.server.assembly.Status;
import org.junit.Test;
import static org.junit.Assert.*;
public class AddInstructionTest {
/**
* ADD mem/reg, mem/reg
*/
@Test
public void addTargetTarget() {
ServerConfiguration config = new ServerConfiguration("config.properties");
int memorySize = config.getInt("memory_size");
//Memory
Memory memory = new Memory(memorySize);
memory.clear();
//RegisterSet
RegisterSet registerSet = new RegisterSet();
registerSet.put(1, new Register("T1"));
registerSet.put(2, new Register("T2"));
registerSet.clear();
//Status
Status status = new Status();
status.clear();
AddInstruction addInstruction = new AddInstruction();
//ADD mem, mem
//Positive numbers
memory.set(0, 10);
memory.set(1, 10);
addInstruction.execute(memory, 0, memory, 1, status);
assertEquals(20, memory.get(0));
assertEquals(10, memory.get(1));
assertEquals(10, memory.get(1));
//FLAGS Should be CF=0 ZF=0 SF=0 OF=0
assertFalse(status.isSignFlag());
assertFalse(status.isZeroFlag());
assertFalse(status.isCarryFlag());
assertFalse(status.isOverflowFlag());
assertFalse(status.isBreakFlag());
memory.clear();
memory.set(memorySize - 1, 10);
memory.set(1, 10);
addInstruction.execute(memory, memorySize - 1, memory, 1, status);
assertEquals(20, memory.get(memorySize - 1));
assertEquals(10, memory.get(1));
//FLAGS Should be CF=0 ZF=0 SF=0 OF=0
assertFalse(status.isSignFlag());
assertFalse(status.isZeroFlag());
assertFalse(status.isCarryFlag());
assertFalse(status.isOverflowFlag());
assertFalse(status.isBreakFlag());
//Large positive numbers
memory.clear();
memory.set(0, 2);
memory.set(1, 0xFFFF);
addInstruction.execute(memory, memorySize, memory, 1, status);
assertEquals(1, memory.get(0));
assertEquals(0xFFFF, memory.get(1));
//FLAGS Should be CF=1 ZF=0 SF=0 OF=0
assertFalse(status.isSignFlag());
assertFalse(status.isZeroFlag());
assertTrue(status.isCarryFlag());
assertFalse(status.isOverflowFlag());
assertFalse(status.isBreakFlag());
//Zero result
memory.clear();
memory.set(0, 1);
memory.set(1, 0xFFFF);
addInstruction.execute(memory, memorySize, memory, 1, status);
assertEquals(0, memory.get(0));
assertEquals(0xFFFF, memory.get(1));
//FLAGS Should be CF=1 ZF=1 SF=0 OF=0
assertTrue(status.isCarryFlag());
assertTrue(status.isZeroFlag());
assertFalse(status.isSignFlag());
assertFalse(status.isOverflowFlag());
assertFalse(status.isBreakFlag());
//Overflow
memory.clear();
memory.set(0, 0x8000);
memory.set(1, 0xFFFF);
addInstruction.execute(memory, 0, memory, 1, status);
assertEquals(0x7FFF, memory.get(0));
assertEquals(0xFFFF, memory.get(1));
//FLAGS Should be CF=1 ZF=0 SF=0 OF=1
assertTrue(status.isCarryFlag());
assertFalse(status.isZeroFlag());
assertFalse(status.isSignFlag());
assertTrue(status.isOverflowFlag());
assertFalse(status.isBreakFlag());
//ADD reg, reg
//Positive numbers
registerSet.set(1, 10);
registerSet.set(2, 10);
addInstruction.execute(registerSet, 1, registerSet, 2, status);
assertEquals(20, registerSet.get(1));
assertEquals(10, registerSet.get(2));
//FLAGS Should be CF=0 ZF=0 SF=0 OF=0
assertFalse(status.isSignFlag());
assertFalse(status.isZeroFlag());
assertFalse(status.isCarryFlag());
assertFalse(status.isOverflowFlag());
assertFalse(status.isBreakFlag());
}
/**
* ADD mem/reg, imm
*/
@Test
public void addTargetImm() {
ServerConfiguration config = new ServerConfiguration("config.properties");
int memorySize = config.getInt("memory_size");
//Memory
Memory memory = new Memory(memorySize);
memory.clear();
//Status
Status status = new Status();
status.clear();
AddInstruction addInstruction = new AddInstruction();
//Positive number
memory.clear();
memory.set(0, 10);
addInstruction.execute(memory, 0, 10, status);
assertEquals(20, memory.get(0));
//FLAGS Should be CF=0 ZF=0 SF=0 OF=0
assertFalse(status.isSignFlag());
assertFalse(status.isZeroFlag());
assertFalse(status.isCarryFlag());
assertFalse(status.isOverflowFlag());
assertFalse(status.isBreakFlag());
//The rest is assumed to work since it is tested with addTargetTarget() and behavior shouldn't be different;
}
}

View File

@@ -0,0 +1,82 @@
package net.simon987.server.assembly.instruction;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.Memory;
import net.simon987.server.assembly.Status;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class AndInstructionTest {
@Test
public void executeTargetTarget() {
ServerConfiguration config = new ServerConfiguration("config.properties");
int memorySize = config.getInt("memory_size");
//Memory
Memory memory = new Memory(memorySize);
memory.clear();
//Status
Status status = new Status();
status.clear();
AndInstruction andInstruction = new AndInstruction();
//mem mem
memory.clear();
memory.set(0, 0xF010);
memory.set(1, 0xF111);
andInstruction.execute(memory, 0, memory, 1, status);
assertEquals(0xF010, memory.get(0));
assertEquals(true, status.isSignFlag());
assertEquals(false, status.isZeroFlag());
assertEquals(false, status.isOverflowFlag());
assertEquals(false, status.isCarryFlag());
//mem mem
memory.clear();
memory.set(0, 0x1010);
memory.set(1, 0x0101);
andInstruction.execute(memory, 0, memory, 1, status);
assertEquals(0, memory.get(0));
assertEquals(false, status.isSignFlag());
assertEquals(true, status.isZeroFlag());
assertEquals(false, status.isOverflowFlag());
assertEquals(false, status.isCarryFlag());
}
@Test
public void executeTargetImm() {
ServerConfiguration config = new ServerConfiguration("config.properties");
int memorySize = config.getInt("memory_size");
//Memory
Memory memory = new Memory(memorySize);
memory.clear();
//Status
Status status = new Status();
status.clear();
AndInstruction andInstruction = new AndInstruction();
//mem imm
memory.clear();
memory.set(0, 0x1010);
andInstruction.execute(memory, 0, 0x0101, status);
assertEquals(0, memory.get(0));
assertEquals(false, status.isSignFlag());
assertEquals(true, status.isZeroFlag());
assertEquals(false, status.isOverflowFlag());
assertEquals(false, status.isCarryFlag());
}
}

View File

@@ -0,0 +1,24 @@
package net.simon987.server.assembly.instruction;
import net.simon987.server.assembly.Status;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class BrkInstructionTest {
@Test
public void execute() throws Exception {
//Status
Status status = new Status();
status.clear();
BrkInstruction brkInstruction = new BrkInstruction();
brkInstruction.execute(status);
assertEquals(true, status.isBreakFlag());
}
}

View File

@@ -0,0 +1,45 @@
package net.simon987.server.assembly.instruction;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.*;
import net.simon987.server.user.User;
import org.junit.Test;
public class CallInstructionTest {
@Test
public void execute() throws Exception {
ServerConfiguration config = new ServerConfiguration("config.properties");
int memorySize = config.getInt("memory_size");
//Memory
Memory memory = new Memory(memorySize);
memory.clear();
//RegisterSet
RegisterSet registerSet = new RegisterSet();
registerSet.put(1, new Register("T1"));
registerSet.put(2, new Register("T2"));
registerSet.clear();
//Status
Status status = new Status();
status.clear();
CPU cpu = new CPU(config, new User());
CallInstruction callInstruction = new CallInstruction(cpu);
//We have to check if IP is 'pushed' correctly (try to pop it), the
}
@Test
public void execute1() throws Exception {
}
}