Skip to content

Commit

Permalink
Merge pull request #3 from friend8/master
Browse files Browse the repository at this point in the history
rework the way cli arguments are fetched
  • Loading branch information
cniemira committed Mar 1, 2015
2 parents c42208e + faa90ab commit 875be33
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 128 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

Expand All @@ -14,3 +13,7 @@ hs_err_pid*
# Eclipse Stuff
*.classpath
*.project
/nbproject/
/src/nbproject/private/
/build/
/dist/
58 changes: 31 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,46 @@ This package contains a utility to bootstrap a standalone MapTool server.
To use, you need MapTool from rptools.net:
http://www.rptools.net/index.php?page=downloads#MapTool

Download a .zip file, extract, and then drop the provided standalone.jar file into this folder alongside
the maptool-1.3bXX.jar (as of this writing, b91 is current and is the only version this server has been
tested with). You can then start the standalone server like so:
Download a .zip file, extract, and then drop the provided standalone.jar and the files in the lib folder
into this folder alongside the maptool-1.3bXX.jar (as of this writing, b91 is current and is the only
version this server has been tested with).

java -cp standalone.jar:maptool-1.3.bXX.jar -Dserver.port="51234" net.rptools.maptool.server.StandaloneServer
You can then start the standalone server like so:

java -cp standalone.jar:maptool-1.3.bXX.jar -port="51234"

You can also rename (or better, `ln -s`) maptool-1.3.bXX.jar -> maptool-1.3.jar and run it this way:

java -jar standalone.jar

You can declare any of the following properties to configure the server:

server.name (registers at rptools.net if set)
server.port (defaults to 51234)
server.gmPassword
server.playerPassword
campaign.file
Because the MapTool server must host a saved campaign, you load a .cmpgn file in the standalone server, but keep in mind that changes will have to be saved on one of the clients. You can also declare the following. The actual value doesn't matter, if you set them at all, they're on.
name (registers at rptools.net if set)
port (defaults to 51234)
gmPassword
playerPassword
campaign

Because the MapTool server must host a saved campaign, you load a .cmpgn file in the standalone server, but keep in mind that changes will have to be saved on one of the clients. You can also declare the following.

log.debug
server.useStrictTokenManagement
server.playersCanRevealVision
server.useIndividualViews
server.playersReceiveCampaignMacros
server.useToolTipsForDefaultRollFormat
server.restrictedImpersonation
logDebug
useStrictTokenManagement
playersCanRevealVision
useIndividualViews
playersReceiveCampaignMacros
useToolTipsForDefaultRollFormat
restrictedImpersonation

You can get a help with all possible arguments simply by calling

java -jar standalone.jar

Something like this should do you fine:

java -cp standalone.jar:maptool-1.3.b91.jar \
-Dserver.gmPassword="secret" \
-Dserver.playerPassword="******" \
-Dserver.useStrictTokenManagement="on" \
-Dserver.useIndividualViews="on" \
-Dserver.playersReceiveCampaignMacros="on" \
-Dserver.restrictedImpersonation="on" \
net.rptools.maptool.server.StandaloneServer
java -jar standalone.jar \
--gmPassword="secret" \
--playerPassword="******" \
--useStrictTokenManagement \
--useIndividualViews \
--playersReceiveCampaignMacros \
--restrictedImpersonation
Binary file added lib/commons-cli-1.2-javadoc.jar
Binary file not shown.
Binary file added lib/commons-cli-1.2.jar
Binary file not shown.
3 changes: 3 additions & 0 deletions manifest.mf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: net.rptools.maptool.server.StandaloneServer
Class-Path: maptool-1.3.jar lib/commons-cli-1.2.jar
235 changes: 135 additions & 100 deletions src/net/rptools/maptool/server/StandaloneServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,52 @@
import net.rptools.maptool.model.AssetManager;
import net.rptools.maptool.model.Campaign;
import net.rptools.maptool.model.CampaignFactory;
import net.rptools.maptool.model.Zone;
import net.rptools.maptool.model.transform.campaign.AssetNameTransform;
import net.rptools.maptool.model.transform.campaign.PCVisionTransform;
import net.rptools.maptool.server.MapToolServer;
import net.rptools.maptool.server.ServerConfig;
import net.rptools.maptool.server.ServerPolicy;
import net.rptools.maptool.transfer.AssetTransferManager;
import net.rptools.maptool.util.PersistenceUtil.PersistedCampaign;
import net.rptools.maptool.util.StringUtil;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class StandaloneServer {
private static final Logger log = Logger.getLogger(StandaloneServer.class);

private static final ModelVersionManager campaignVersionManager = new ModelVersionManager();
private static final ModelVersionManager assetnameVersionManager = new ModelVersionManager();

private static MapToolServer server;
private static ServerCommand serverCommand;

static {
PackedFile.init(AppUtil.getAppHome("tmp"));
campaignVersionManager.registerTransformation("1.3.51", new PCVisionTransform());
assetnameVersionManager.registerTransformation("1.3.51", new AssetNameTransform("^(.*)\\.(dat)?$", "$1"));
}

private static boolean haveProp(String name) {
if(System.getProperty(name) != null) {
return true;
}
return false;

}


private static void loadAssets(Collection<MD5Key> assetIds, PackedFile pakFile) throws IOException {
pakFile.getXStream().processAnnotations(Asset.class);
String campVersion = (String)pakFile.getProperty("campaignVersion");

for (MD5Key key : assetIds) {
if (key == null) {
continue;
}

if (!AssetManager.hasAsset(key)) {
String pathname = "assets/" + key;
Asset asset;
asset = (Asset) pakFile.getFileObject(pathname);

if (asset == null) {
log.error("Referenced asset '" + pathname + "' not found while loading?!");
continue;
Expand All @@ -84,108 +79,148 @@ private static void loadAssets(Collection<MD5Key> assetIds, PackedFile pakFile)
asset.setImage(IOUtils.toByteArray(is));
is.close();
}

AssetManager.putAsset(asset);
serverCommand.putAsset(asset);
}
}
}

public static void loadCampaignFile(String filename) throws IOException {
File campaignFile = new File(filename);
PackedFile pakfile = new PackedFile(campaignFile);
pakfile.setModelVersionManager(campaignVersionManager);

String version = (String)pakfile.getProperty("campaignVersion");
version = version == null ? "1.3.50" : version;
PersistedCampaign persistedCampaign = (PersistedCampaign) pakfile.getContent(version);

if (persistedCampaign != null) {
server.setCampaign(persistedCampaign.campaign);

Set<MD5Key> allAssetIds = persistedCampaign.assetMap.keySet();
loadAssets(allAssetIds, pakfile);
for (Zone zone : persistedCampaign.campaign.getZones()) {
zone.optimize();
}
}

private static void startServer(String id, ServerConfig config, ServerPolicy policy, Campaign campaign) throws IOException {
AssetTransferManager assetTransferManager = new AssetTransferManager();
assetTransferManager.addConsumerListener(new AssetTransferHandler());
assetTransferManager.flush();

server = new MapToolServer(config, policy);
server.setCampaign(campaign);

if (config.isServerRegistered()) {
log.debug("Attempting To Register Server");
try {
int result = MapToolRegistry.registerInstance(config.getServerName(), config.getPort());
if (result == 3) {
log.error("Already Registered", null);
System.exit(1);
}
} catch (Exception e) {
log.error("Could Not Register Server", e);
System.exit(1);
}
} else {
log.debug("Will Not Register Server");
}
}

private static void startServer(String id, ServerConfig config, ServerPolicy policy, Campaign campaign) throws IOException {
AssetTransferManager assetTransferManager = new AssetTransferManager();
assetTransferManager.addConsumerListener(new AssetTransferHandler());
assetTransferManager.flush();

server = new MapToolServer(config, policy);
server.setCampaign(campaign);

if (config.isServerRegistered()) {
log.debug("Attempting To Register Server");
try {
int result = MapToolRegistry.registerInstance(config.getServerName(), config.getPort());
if (result == 3) {
log.error("Already Registered", null);
System.exit(1);
}
} catch (Exception e) {
log.error("Could Not Register Server", e);
System.exit(1);
}
} else {
log.debug("Will Not Register Server");
}
}


public static void main(String [] args) {
org.apache.log4j.BasicConfigurator.configure();

if(haveProp("log.debug")) {
Logger.getRootLogger().setLevel(Level.DEBUG);
} else {
Logger.getRootLogger().setLevel(Level.INFO);
}

serverCommand = new ServerCommandClientImpl();
Campaign campaign = CampaignFactory.createBasicCampaign();

String port_number = System.getProperty("server.port");
int port;
if (port_number == null) {
port = 51234;
} else {
port = Integer.parseInt(port_number);
}

ServerConfig config = new ServerConfig("StandaloneServer",
System.getProperty("server.gmPassword"),
System.getProperty("server.playerPassword"),
port,
System.getProperty("server.name"));

ServerPolicy policy = new ServerPolicy();
policy.setUseStrictTokenManagement(haveProp("server.useStrictTokenManagement"));
policy.setPlayersCanRevealVision(haveProp("server.playersCanRevealVision"));
policy.setUseIndividualViews(haveProp("server.useIndividualViews"));
policy.setPlayersReceiveCampaignMacros(haveProp("server.playersReceiveCampaignMacros"));
policy.setUseToolTipsForDefaultRollFormat(haveProp("server.useToolTipsForDefaultRollFormat"));
policy.setRestrictedImpersonation(haveProp("server.restrictedImpersonation"));

try {
startServer(null, config, policy, campaign);
} catch (Exception e) {
log.error("Could not start server", e);
System.exit(1);
}

log.info("Started on port " + port);

if (haveProp("campaign.file")) {
try {
loadCampaignFile(System.getProperty("campaign.file"));


public static void main(String [] args) {
org.apache.log4j.BasicConfigurator.configure();

Options options = new Options();
options.addOption("h", "help", false, "Show this help");
Option name = new Option("n", "name", true, "The name of the maptools server and registers at rptools.net if set");
name.setValueSeparator('=');
name.setArgName("SERVERNAME");
options.addOption(name);
Option port = new Option("p", "port", true, "The port of the maptools server, defaults to 51234");
port.setValueSeparator('=');
port.setArgName("PORT");
options.addOption(port);
Option gm = new Option("g", "gmPassword", true, "The GM password a user has to enter, if he wants to be a GM");
gm.setValueSeparator('=');
gm.setArgName("PASSWORD");
options.addOption(gm);
Option password = new Option("a", "playerPassword", true, "The player password a user has to enter");
password.setValueSeparator('=');
password.setArgName("PASSWORD");
options.addOption(password);
options.addOption("s", "useStrictTokenManagement", false, "Set the strict token management");
options.addOption("v", "playersCanRevealVision", false, "The users can reveal the vision");
options.addOption("i", "useIndividualViews", false, "Use individual views for each player");
options.addOption("m", "playersReceiveCampaignMacros", false, "Send the campaign macros to the users");
options.addOption("t", "useToolTipsForDefaultRollFormat", false, "");
options.addOption("r", "restrictedImpersonation", false, "Restrict the impersonation of a token to only one user");
options.addOption("c", "campaign", true, "The campaign file to load");
options.addOption("d", "logDebug", false, "Show debug information");
CommandLineParser parser = new BasicParser();
CommandLine cmd = null;

try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
log.error("Something went wrong fetching the cli arguments", e);
System.exit(1);
}

if (cmd.hasOption("h") || args.length == 0) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("test", options);
System.exit(0);
}

if(cmd.hasOption("l")) {
Logger.getRootLogger().setLevel(Level.DEBUG);
} else {
Logger.getRootLogger().setLevel(Level.INFO);
}

serverCommand = new ServerCommandClientImpl();
Campaign campaign = CampaignFactory.createBasicCampaign();

String portString = cmd.getOptionValue("p");
int portNumber;
if (portString == null) {
portNumber = 51234;
} else {
portNumber = Integer.parseInt(portString);
}

ServerConfig config = new ServerConfig("StandaloneServer",
cmd.getOptionValue("g"),
cmd.getOptionValue("a"),
portNumber,
cmd.getOptionValue("n"));

ServerPolicy policy = new ServerPolicy();
policy.setUseStrictTokenManagement(cmd.hasOption("s"));
policy.setPlayersCanRevealVision(cmd.hasOption("v"));
policy.setUseIndividualViews(cmd.hasOption("i"));
policy.setPlayersReceiveCampaignMacros(cmd.hasOption("m"));
policy.setUseToolTipsForDefaultRollFormat(cmd.hasOption("t"));
policy.setRestrictedImpersonation(cmd.hasOption("r"));

try {
startServer(null, config, policy, campaign);
} catch (Exception e) {
log.error("Could not start server", e);
System.exit(1);
}

log.info("Started on port " + port);

if (cmd.hasOption("c")) {
try {
loadCampaignFile(cmd.getOptionValue("c"));
log.info("Loaded campaign " + cmd.getOptionValue("c"));
} catch (IOException e) {
log.error("Unable to load campaign", e);
}
}
}
}
}
}

0 comments on commit 875be33

Please sign in to comment.