1
23
24package org.jzuul.tools;
25
26import java.util.Iterator;
27import java.util.List;
28import java.util.Vector;
29
30import org.jzuul.engine.CommandList;
31import org.jzuul.engine.Engine;
32import org.jzuul.engine.exceptions.GuiNotInitializedException;
33import org.jzuul.engine.gui.GuiInterface;
34
35
42public class GameFileRunner {
43
46 private static final String guiBase = "org.jzuul.engine.gui";
47
48
51 private String filename;
52
55 private boolean withPlayerName = true;
56
59 private boolean historyRun = false;
60
63 private boolean threaded = false;
64
67 private int numberOfPlayers = 0;
68
71 private String histFileName;
72
75 private String guiName = "TextUi";
76
77
80 private Engine engine;
81
82
87 public static void main(String[] args) {
88 if (args.length == 0) {
89 GameFileRunner.usage();
90 }
91 Vector v = new Vector(args.length);
92 for (int i = 0; i < args.length; i++) {
93 v.add(args[i].replaceFirst("-+", ""));
94 }
95
96 GameFileRunner runner = new GameFileRunner(v);
97 runner.run();
98 }
99
00
05 public GameFileRunner(List arguments) {
06 for (Iterator argIter = arguments.iterator(); argIter.hasNext();) {
08 String element = (String) argIter.next();
09 if (element.endsWith(".xml") && filename == null) {
10 if (element.startsWith("/")) {
11 filename = element;
12 } else {
13 filename = "/" + element;
14 }
15 argIter.remove();
16 }
17 if (element.equals("players")) {
18 argIter.remove();
19 numberOfPlayers = Integer.parseInt((String) argIter.next());
20 argIter.remove();
21 }
22 if (element.equals("history")) {
23 historyRun = true;
24 argIter.remove();
25 histFileName = (String) argIter.next();
26 argIter.remove();
27 if (!histFileName.startsWith("/")) {
28 histFileName = "/" + histFileName;
29 }
30 }
31 if (element.equals("gui")) {
32 argIter.remove();
33 guiName = (String) argIter.next();
34 argIter.remove();
35 }
36 if (element.equals("debug")) {
37 argIter.remove();
38 Engine.DEBUG = Integer.parseInt((String) argIter.next());
39 argIter.remove();
40 }
41 }
42 withPlayerName = arguments.contains("name");
43 arguments.remove("name");
44 threaded = arguments.contains("threaded");
45 arguments.remove("threaded");
46
47 if (!arguments.isEmpty()) {
48 System.out.println();
49 for (Iterator argIter = arguments.iterator(); argIter.hasNext();) {
50 String element = (String) argIter.next();
51 System.out.println("ERROR: No such argument: " + element);
52 }
53 System.out.println();
54 usage();
55 }
56
57 GuiInterface gui = createGui();
58 this.engine = new Engine(filename, CommandList.defaultList(), gui, threaded, numberOfPlayers);
59 }
60
61
66 protected GuiInterface createGui() {
67 Object guiObj = null;
68 try {
69 Class classDefinition = Class.forName(guiBase + "." + guiName);
70 guiObj = classDefinition.newInstance();
71 } catch (InstantiationException e) {
72 System.out.println(e);
73 } catch (IllegalAccessException e) {
74 System.out.println(e);
75 } catch (ClassNotFoundException e) {
76 System.out.println("ERROR: There is no " + guiName + " GUI");
77 System.exit(2);
78 }
79
80 if (guiObj != null) {
81 return (GuiInterface) guiObj;
82 } else {
83 throw new GuiNotInitializedException();
84 }
85 }
86
87
91 protected void run() {
92 if (historyRun) {
93 engine.runHistory(histFileName);
94 } else {
95 engine.run(withPlayerName);
96 }
97 }
98
99
02 public static void usage() {
03 System.out.println(" This is the JZuul Gamefile runner $Revision: 1.5 $ ");
04 System.out.println();
05 System.out.println(" JZuul is free software, you can distribute and modify ");
06 System.out.println(" it under the terms of the GPL (see LICENSE)");
07 System.out.println();
08 System.out.println(
09 " Usage: [java] GameFileRunner gamefile.xml [-name] [-history <file.xml>]");
10 System.out.println("\t [-players <number>] [-threaded] [-gui GuiImplementation] ");
11 System.out.println("\t [-debug value]");
12 System.out.println();
13 System.out.println("\t-name\t\tdoes do a named Player game");
14 System.out.println("\t-history\tdoes a history run on the given file");
15 System.out.println("\t-players\tcreates a game with number of players");
16 System.out.println("\t-threaded\tlet the NPCs actions run in an own Thread");
17 System.out.println("\t-debug\t\tsets the Engine debug level to Value");
18 System.out.println();
19 System.exit(1);
20 }
21
22}
23