This commit is contained in:
simon987 2019-01-15 21:02:25 -05:00
parent 993838651f
commit 762bad758b
23 changed files with 212 additions and 45 deletions

View File

@ -1,7 +1,7 @@
package net.simon987.cubotplugin; package net.simon987.cubotplugin;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration; import net.simon987.server.IServerConfiguration;
import net.simon987.server.assembly.CPU; import net.simon987.server.assembly.CPU;
import net.simon987.server.assembly.HardwareModule; import net.simon987.server.assembly.HardwareModule;
import net.simon987.server.assembly.Memory; import net.simon987.server.assembly.Memory;
@ -128,7 +128,7 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
shield = document.getInteger("shield"); shield = document.getInteger("shield");
setDirection(Direction.getDirection(document.getInteger("direction"))); setDirection(Direction.getDirection(document.getInteger("direction")));
ServerConfiguration config = GameServer.INSTANCE.getConfig(); IServerConfiguration config = GameServer.INSTANCE.getConfig();
maxHp = config.getInt("cubot_max_hp"); maxHp = config.getInt("cubot_max_hp");
maxShield = config.getInt("cubot_max_shield"); maxShield = config.getInt("cubot_max_shield");
@ -278,7 +278,7 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Me
this.getWorld().removeObject(this); this.getWorld().removeObject(this);
this.getWorld().decUpdatable(); this.getWorld().decUpdatable();
ServerConfiguration config = GameServer.INSTANCE.getConfig(); IServerConfiguration config = GameServer.INSTANCE.getConfig();
Random random = new Random(); Random random = new Random();
int spawnX = config.getInt("new_user_worldX") + random.nextInt(5); int spawnX = config.getInt("new_user_worldX") + random.nextInt(5);

View File

@ -3,7 +3,7 @@ package net.simon987.cubotplugin.event;
import net.simon987.cubotplugin.Cubot; import net.simon987.cubotplugin.Cubot;
import net.simon987.cubotplugin.CubotStatus; import net.simon987.cubotplugin.CubotStatus;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration; import net.simon987.server.IServerConfiguration;
import net.simon987.server.assembly.Assembler; import net.simon987.server.assembly.Assembler;
import net.simon987.server.assembly.AssemblyResult; import net.simon987.server.assembly.AssemblyResult;
import net.simon987.server.assembly.CPU; import net.simon987.server.assembly.CPU;
@ -33,7 +33,7 @@ public class UserCreationListener implements GameEventListener {
Cubot cubot = new Cubot(); Cubot cubot = new Cubot();
cubot.addStatus(CubotStatus.FACTORY_NEW); cubot.addStatus(CubotStatus.FACTORY_NEW);
cubot.setObjectId(new ObjectId()); cubot.setObjectId(new ObjectId());
ServerConfiguration config = GameServer.INSTANCE.getConfig(); IServerConfiguration config = GameServer.INSTANCE.getConfig();
Point point = null; Point point = null;
while (point == null || cubot.getWorld() == null) { while (point == null || cubot.getWorld() == null) {

View File

@ -7,7 +7,7 @@ import net.simon987.npcplugin.event.WorldCreationListener;
import net.simon987.npcplugin.world.TileVaultFloor; import net.simon987.npcplugin.world.TileVaultFloor;
import net.simon987.npcplugin.world.TileVaultWall; import net.simon987.npcplugin.world.TileVaultWall;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration; import net.simon987.server.IServerConfiguration;
import net.simon987.server.game.objects.GameRegistry; import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.ServerPlugin; import net.simon987.server.plugin.ServerPlugin;
@ -27,7 +27,7 @@ public class NpcPlugin extends ServerPlugin {
@Override @Override
public void init(GameServer gameServer) { public void init(GameServer gameServer) {
ServerConfiguration configuration = gameServer.getConfig(); IServerConfiguration configuration = gameServer.getConfig();
GameRegistry registry = gameServer.getRegistry(); GameRegistry registry = gameServer.getRegistry();
listeners.add(new WorldCreationListener(configuration.getInt("settlement_spawn_rate"))); listeners.add(new WorldCreationListener(configuration.getInt("settlement_spawn_rate")));

View File

@ -2,7 +2,7 @@ package net.simon987.npcplugin;
import net.simon987.npcplugin.world.TileVaultFloor; import net.simon987.npcplugin.world.TileVaultFloor;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration; import net.simon987.server.IServerConfiguration;
import net.simon987.server.game.objects.Direction; import net.simon987.server.game.objects.Direction;
import net.simon987.server.game.world.Location; import net.simon987.server.game.world.Location;
import net.simon987.server.game.world.World; import net.simon987.server.game.world.World;
@ -49,7 +49,7 @@ public class VaultDimension {
* as a set of coordinates + a list of opening directions, then they are actually generated * as a set of coordinates + a list of opening directions, then they are actually generated
*/ */
ServerConfiguration config = GameServer.INSTANCE.getConfig(); IServerConfiguration config = GameServer.INSTANCE.getConfig();
int minLayerCount = config.getInt("vault_wg_min_layer_count"); int minLayerCount = config.getInt("vault_wg_min_layer_count");
int maxLayerCount = config.getInt("vault_wg_max_layer_count"); int maxLayerCount = config.getInt("vault_wg_max_layer_count");

View File

@ -3,7 +3,7 @@ package net.simon987.npcplugin.event;
import net.simon987.npcplugin.ElectricBox; import net.simon987.npcplugin.ElectricBox;
import net.simon987.npcplugin.VaultWorldUtils; import net.simon987.npcplugin.VaultWorldUtils;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration; import net.simon987.server.IServerConfiguration;
import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener; import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.WorldUpdateEvent; import net.simon987.server.event.WorldUpdateEvent;
@ -36,7 +36,7 @@ public class VaultWorldUpdateListener implements GameEventListener {
*/ */
private static int electricBoxThreshold; private static int electricBoxThreshold;
public VaultWorldUpdateListener(ServerConfiguration config) { public VaultWorldUpdateListener(IServerConfiguration config) {
minElectricBoxCount = config.getInt("min_electric_box_respawn_count"); minElectricBoxCount = config.getInt("min_electric_box_respawn_count");
maxElectricBoxCount = config.getInt("max_electric_box_respawn_count"); maxElectricBoxCount = config.getInt("max_electric_box_respawn_count");

View File

@ -4,7 +4,7 @@ import net.simon987.biomassplugin.event.ObjectDeathListener;
import net.simon987.biomassplugin.event.WorldCreationListener; import net.simon987.biomassplugin.event.WorldCreationListener;
import net.simon987.biomassplugin.event.WorldUpdateListener; import net.simon987.biomassplugin.event.WorldUpdateListener;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration; import net.simon987.server.IServerConfiguration;
import net.simon987.server.game.objects.GameRegistry; import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.ServerPlugin; import net.simon987.server.plugin.ServerPlugin;
@ -16,7 +16,7 @@ public class BiomassPlugin extends ServerPlugin {
@Override @Override
public void init(GameServer gameServer) { public void init(GameServer gameServer) {
ServerConfiguration config = gameServer.getConfig(); IServerConfiguration config = gameServer.getConfig();
GameRegistry registry = gameServer.getRegistry(); GameRegistry registry = gameServer.getRegistry();
listeners.add(new WorldCreationListener()); listeners.add(new WorldCreationListener());

View File

@ -1,7 +1,7 @@
package net.simon987.biomassplugin.event; package net.simon987.biomassplugin.event;
import net.simon987.biomassplugin.BiomassBlob; import net.simon987.biomassplugin.BiomassBlob;
import net.simon987.server.ServerConfiguration; import net.simon987.server.IServerConfiguration;
import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener; import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.ObjectDeathEvent; import net.simon987.server.event.ObjectDeathEvent;
@ -16,7 +16,7 @@ public class ObjectDeathListener implements GameEventListener {
private int biomassDropCount; private int biomassDropCount;
public ObjectDeathListener(ServerConfiguration config) { public ObjectDeathListener(IServerConfiguration config) {
biomassDropCount = config.getInt("harvester_biomass_drop_count"); biomassDropCount = config.getInt("harvester_biomass_drop_count");
} }

View File

@ -3,7 +3,7 @@ package net.simon987.biomassplugin.event;
import net.simon987.biomassplugin.BiomassBlob; import net.simon987.biomassplugin.BiomassBlob;
import net.simon987.biomassplugin.WorldUtils; import net.simon987.biomassplugin.WorldUtils;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration; import net.simon987.server.IServerConfiguration;
import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener; import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.WorldUpdateEvent; import net.simon987.server.event.WorldUpdateEvent;
@ -23,7 +23,7 @@ public class WorldUpdateListener implements GameEventListener {
private static int waitTime; private static int waitTime;
private static int blobThreshold; private static int blobThreshold;
public WorldUpdateListener(ServerConfiguration config) { public WorldUpdateListener(IServerConfiguration config) {
minBlobCount = config.getInt("minBiomassRespawnCount"); minBlobCount = config.getInt("minBiomassRespawnCount");
maxBlobCount = config.getInt("maxBiomassRespawnCount"); maxBlobCount = config.getInt("maxBiomassRespawnCount");

View File

@ -33,7 +33,7 @@ public class GameServer implements Runnable {
private GameEventDispatcher eventDispatcher; private GameEventDispatcher eventDispatcher;
private PluginManager pluginManager; private PluginManager pluginManager;
private ServerConfiguration config; private IServerConfiguration config;
private SocketServer socketServer; private SocketServer socketServer;
@ -308,7 +308,7 @@ public class GameServer implements Runnable {
} }
} }
public ServerConfiguration getConfig() { public IServerConfiguration getConfig() {
return config; return config;
} }

View File

@ -0,0 +1,12 @@
package net.simon987.server;
public interface IServerConfiguration {
int getInt(String key);
String getString(String key);
void setInt(String key, int value);
void setString(String key, String value);
}

View File

@ -9,11 +9,15 @@ import java.util.Properties;
/** /**
* Wrapper for Java Properties class * Wrapper for Java Properties class
*/ */
public class ServerConfiguration { public class ServerConfiguration implements IServerConfiguration {
private Properties properties; private Properties properties;
private String fileName; private String fileName;
public ServerConfiguration() {
this.properties = new Properties();
}
public ServerConfiguration(String fileName) { public ServerConfiguration(String fileName) {
this.fileName = fileName; this.fileName = fileName;

View File

@ -1,7 +1,6 @@
package net.simon987.server.assembly; package net.simon987.server.assembly;
import net.simon987.server.GameServer; import net.simon987.server.IServerConfiguration;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.exception.*; import net.simon987.server.assembly.exception.*;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import org.apache.commons.text.StringEscapeUtils; import org.apache.commons.text.StringEscapeUtils;
@ -19,18 +18,22 @@ import java.util.regex.Pattern;
*/ */
public class Assembler { public class Assembler {
private ServerConfiguration config; private IServerConfiguration config;
private InstructionSet instructionSet; private InstructionSet instructionSet;
private RegisterSet registerSet; private RegisterSet registerSet;
private static final int MEM_SIZE = GameServer.INSTANCE.getConfig().getInt("memory_size"); private static int MEM_SIZE;
private static String labelPattern = "^\\s*[a-zA-Z_]\\w*:";
private static Pattern commentPattern = Pattern.compile("\"[^\"]*\"|(;)");
public Assembler(InstructionSet instructionSet, RegisterSet registerSet, ServerConfiguration config) { public Assembler(InstructionSet instructionSet, RegisterSet registerSet, IServerConfiguration config) {
this.instructionSet = instructionSet; this.instructionSet = instructionSet;
this.registerSet = registerSet; this.registerSet = registerSet;
this.config = config; this.config = config;
Assembler.MEM_SIZE = config.getInt("memory_size");
} }
/** /**
@ -40,11 +43,17 @@ public class Assembler {
* @return The line without its comment part * @return The line without its comment part
*/ */
private static String removeComment(String line) { private static String removeComment(String line) {
if (line.indexOf(';') != -1) {
return line.substring(0, line.indexOf(';')); Matcher m = commentPattern.matcher(line);
} else {
return line; while (m.find()) {
try {
return line.substring(0, m.start(1));
} catch (IndexOutOfBoundsException ignored) {
}
} }
return line;
} }
/** /**
@ -55,8 +64,7 @@ public class Assembler {
*/ */
private static String removeLabel(String line) { private static String removeLabel(String line) {
return line.replaceAll("^\\s*\\b\\w*\\b:", ""); return line.replaceAll(labelPattern, "");
} }
/** /**
@ -98,7 +106,7 @@ public class Assembler {
line = removeComment(line); line = removeComment(line);
//Check for labels //Check for labels
Pattern pattern = Pattern.compile("^\\s*\\b\\w*\\b:"); Pattern pattern = Pattern.compile(labelPattern);
Matcher matcher = pattern.matcher(line); Matcher matcher = pattern.matcher(line);
if (matcher.find()) { if (matcher.find()) {
@ -161,7 +169,7 @@ public class Assembler {
string = StringEscapeUtils.unescapeJava(string); string = StringEscapeUtils.unescapeJava(string);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new InvalidOperandException( throw new InvalidOperandException(
"Invalid string operand \"" + string + "\": " + e.getMessage(), "Invalid string operand \"" + string + "\": " + e.getMessage(),
currentLine); currentLine);
} }

View File

@ -1,6 +1,6 @@
package net.simon987.server.assembly; package net.simon987.server.assembly;
import net.simon987.server.ServerConfiguration; import net.simon987.server.IServerConfiguration;
import net.simon987.server.assembly.exception.AssemblyException; import net.simon987.server.assembly.exception.AssemblyException;
import net.simon987.server.assembly.exception.DuplicateSectionException; import net.simon987.server.assembly.exception.DuplicateSectionException;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
@ -59,7 +59,7 @@ public class AssemblyResult {
*/ */
private boolean dataSectionSet = false; private boolean dataSectionSet = false;
AssemblyResult(ServerConfiguration config) { AssemblyResult(IServerConfiguration config) {
origin = config.getInt("org_offset"); origin = config.getInt("org_offset");
} }

View File

@ -1,7 +1,7 @@
package net.simon987.server.assembly; package net.simon987.server.assembly;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration; import net.simon987.server.IServerConfiguration;
import net.simon987.server.assembly.exception.CancelledException; import net.simon987.server.assembly.exception.CancelledException;
import net.simon987.server.assembly.instruction.*; import net.simon987.server.assembly.instruction.*;
import net.simon987.server.event.CpuInitialisationEvent; import net.simon987.server.event.CpuInitialisationEvent;
@ -98,7 +98,7 @@ public class CPU implements MongoSerializable {
/** /**
* Creates a new CPU * Creates a new CPU
*/ */
public CPU(ServerConfiguration config, ControllableUnit unit) throws CancelledException { public CPU(IServerConfiguration config, ControllableUnit unit) throws CancelledException {
instructionSet = new DefaultInstructionSet(); instructionSet = new DefaultInstructionSet();
registerSet = new DefaultRegisterSet(); registerSet = new DefaultRegisterSet();
codeSectionOffset = config.getInt("org_offset"); codeSectionOffset = config.getInt("org_offset");

View File

@ -5,7 +5,7 @@ import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration; import net.simon987.server.IServerConfiguration;
import net.simon987.server.assembly.exception.CancelledException; import net.simon987.server.assembly.exception.CancelledException;
import net.simon987.server.game.objects.GameObject; import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World; import net.simon987.server.game.world.World;
@ -32,7 +32,7 @@ public class GameUniverse {
private int maxWidth = 0xFFFF; private int maxWidth = 0xFFFF;
public GameUniverse(ServerConfiguration config) { public GameUniverse(IServerConfiguration config) {
worlds = new ConcurrentHashMap<>(256); worlds = new ConcurrentHashMap<>(256);
users = new ConcurrentHashMap<>(16); users = new ConcurrentHashMap<>(16);

View File

@ -1,7 +1,7 @@
package net.simon987.server.game.world; package net.simon987.server.game.world;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration; import net.simon987.server.IServerConfiguration;
import net.simon987.server.assembly.exception.CancelledException; import net.simon987.server.assembly.exception.CancelledException;
import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEvent;
import net.simon987.server.event.WorldGenerationEvent; import net.simon987.server.event.WorldGenerationEvent;
@ -48,7 +48,7 @@ public class WorldGenerator {
private HashMap<Point, Integer> centerPointsMap; private HashMap<Point, Integer> centerPointsMap;
public WorldGenerator(ServerConfiguration config) { public WorldGenerator(IServerConfiguration config) {
dimension = config.getString("new_user_dimension"); dimension = config.getString("new_user_dimension");

View File

@ -1,6 +1,6 @@
package net.simon987.server.web; package net.simon987.server.web;
import net.simon987.server.ServerConfiguration; import net.simon987.server.IServerConfiguration;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import net.simon987.server.websocket.SocketServer; import net.simon987.server.websocket.SocketServer;
import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.app.VelocityEngine;
@ -14,7 +14,7 @@ public class WebServer {
private SocketServer socketServer; private SocketServer socketServer;
private GuestPolicy guestPolicy; private GuestPolicy guestPolicy;
public WebServer(ServerConfiguration config) { public WebServer(IServerConfiguration config) {
//Velocity config //Velocity config
Properties properties = new Properties(); Properties properties = new Properties();
@ -63,7 +63,7 @@ public class WebServer {
* Load guest policy from config. * Load guest policy from config.
* If no valid policy is specified in the config, the default <code>GuestPolicy.ALLOW</code> is used * If no valid policy is specified in the config, the default <code>GuestPolicy.ALLOW</code> is used
*/ */
private void initGuestPolicy(ServerConfiguration config) { private void initGuestPolicy(IServerConfiguration config) {
String guestPolicyStr = config.getString("guest_policy"); String guestPolicyStr = config.getString("guest_policy");
try { try {
this.guestPolicy = GuestPolicy.valueOf(guestPolicyStr); this.guestPolicy = GuestPolicy.valueOf(guestPolicyStr);

View File

@ -28,7 +28,7 @@ function checkForLabel(line, result) {
line = removeComment(line); line = removeComment(line);
var match; var match;
if ((match = /\b\w*\b:/.exec(line)) !== null) { if ((match = /^[a-zA-Z_]\w*:/.exec(line)) !== null) {
result.labels.push(match[0].substring(0, match[0].length - 1)); result.labels.push(match[0].substring(0, match[0].length - 1));
} }

View File

@ -0,0 +1,33 @@
package net.simon987.server;
import java.util.Properties;
public class FakeConfiguration implements IServerConfiguration {
private Properties properties;
public FakeConfiguration() {
this.properties = new Properties();
}
@Override
public int getInt(String key) {
return Integer.parseInt(properties.getProperty(key));
}
@Override
public String getString(String key) {
return properties.getProperty(key);
}
@Override
public void setInt(String key, int value) {
properties.setProperty(key, String.valueOf(value));
}
@Override
public void setString(String key, String value) {
properties.setProperty(key, value);
}
}

View File

@ -0,0 +1,24 @@
package net.simon987.server.assembly;
import net.simon987.server.FakeConfiguration;
import net.simon987.server.IServerConfiguration;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class DWTest {
@Test
public void TestSemiColonInString() {
IServerConfiguration configuration = new FakeConfiguration();
configuration.setInt("memory_size", 1000);
configuration.setInt("org_offset", 400);
Assembler assembler = new Assembler(new DefaultInstructionSet(), new DefaultRegisterSet(), configuration);
AssemblyResult ar = assembler.parse("DW \";\"");
assertEquals(0, ar.exceptions.size());
}
}

View File

@ -0,0 +1,30 @@
package net.simon987.server.assembly;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class LabelTest {
@Test
public void TestNumericLabel() {
Assembler asm = TestHelper.getTestAsm();
AssemblyResult ar = asm.parse("999:");
assertEquals(ar.labels.size(), 0);
}
@Test
public void TestValidLabel() {
Assembler asm = TestHelper.getTestAsm();
AssemblyResult ar = asm.parse("\ttest_label: dw 1 ; comment");
assertNotNull(ar.labels.get("test_label"));
}
}

View File

@ -0,0 +1,38 @@
package net.simon987.server.assembly;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestComment {
@Test
public void TestCommentInQuotes() {
Assembler asm = TestHelper.getTestAsm();
AssemblyResult r1 = asm.parse("dw \";\", 12");
assertEquals(r1.bytes.length, 4);
}
@Test
public void TestRegularComment() {
Assembler asm = TestHelper.getTestAsm();
AssemblyResult r1 = asm.parse("register_SP: DW \"SP=\",0 ; register_A + 28");
assertEquals(8, r1.bytes.length);
assertEquals(0, r1.exceptions.size());
}
@Test
public void TestStandaloneComment() {
Assembler asm = TestHelper.getTestAsm();
AssemblyResult r1 = asm.parse("; Set display_mode to DECIMAL_MODE");
assertEquals(0, r1.bytes.length);
assertEquals(0, r1.exceptions.size());
}
}

View File

@ -0,0 +1,18 @@
package net.simon987.server.assembly;
import net.simon987.server.FakeConfiguration;
import net.simon987.server.IServerConfiguration;
class TestHelper {
static Assembler getTestAsm() {
IServerConfiguration configuration = new FakeConfiguration();
configuration.setInt("memory_size", 1000);
configuration.setInt("org_offset", 400);
return new Assembler(new DefaultInstructionSet(), new DefaultRegisterSet(), configuration);
}
}