From 66abe4bf45b748dee3da7b1de2f386f73a57d58c Mon Sep 17 00:00:00 2001 From: Karan Gurazada Date: Sun, 8 Jul 2018 16:10:36 -0700 Subject: [PATCH] added file-based registry --- src/tanks/Game.java | 47 ++++++++++---- src/tanks/Registry.java | 141 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 166 insertions(+), 22 deletions(-) diff --git a/src/tanks/Game.java b/src/tanks/Game.java index aa00c5df..b9a00507 100644 --- a/src/tanks/Game.java +++ b/src/tanks/Game.java @@ -2,7 +2,15 @@ import java.awt.Color; import java.awt.Toolkit; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Date; import javax.swing.SwingUtilities; @@ -53,21 +61,34 @@ public enum Menu {none, paused, title, options, interlevel} static String currentLevel = ""; + public static PrintStream logger = System.err; + + public static void initScript () { + System.out.println(System.getProperty("java.class.path")); + String homedir = System.getProperty("user.home"); + if (!Files.exists(Paths.get(homedir+"/.tanks.d"))) { + new File (homedir+"/.tanks.d").mkdir(); + try { + new File (homedir+"/.tanks.d/logfile.txt").createNewFile(); + Game.logger = new PrintStream (new FileOutputStream (homedir+"/.tanks.d/logfile.lmao", true)); + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } + Registry.initRegistry (homedir); + } + try { + Game.logger = new PrintStream (new FileOutputStream (homedir+"/.tanks.d/logfile.lmao", true)); + } catch (FileNotFoundException e) { + Game.logger = System.err; + Game.logger.println(new Date().toString() + " (syswarn) logfile not found despite existence of tanks.d! using stderr instead."); + } + Registry.loadRegistry (homedir); + } + public static void main(String[] args) { - new Registry.TankRegistry(registry, EnemyTankBrown.class, "brown", 1); - new Registry.TankRegistry(registry, EnemyTankGray.class, "gray", 1); - new Registry.TankRegistry(registry, EnemyTankMint.class, "mint", 1.0 / 2); - new Registry.TankRegistry(registry, EnemyTankYellow.class, "yellow", 1.0 / 2); - new Registry.TankRegistry(registry, EnemyTankMagenta.class, "magenta", 1.0 / 3); - new Registry.TankRegistry(registry, EnemyTankRed.class, "red", 1.0 / 3); - new Registry.TankRegistry(registry, EnemyTankGreen.class, "green", 1.0 / 4); - new Registry.TankRegistry(registry, EnemyTankPurple.class, "purple", 1.0 / 4); - new Registry.TankRegistry(registry, EnemyTankWhite.class, "white", 1.0 / 4); - new Registry.TankRegistry(registry, EnemyTankOrange.class, "orange", 1.0 / 6); - new Registry.TankRegistry(registry, EnemyTankDarkGreen.class, "darkgreen", 1.0 / 9); - new Registry.TankRegistry(registry, EnemyTankBlack.class, "black", 1.0 / 10); - new Registry.TankRegistry(registry, EnemyTankPink.class, "pink", 1.0 / 15); + initScript(); SwingUtilities.invokeLater ( diff --git a/src/tanks/Registry.java b/src/tanks/Registry.java index 68ff6220..e097b564 100644 --- a/src/tanks/Registry.java +++ b/src/tanks/Registry.java @@ -1,14 +1,136 @@ package tanks; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; +import java.util.Date; +import java.util.Scanner; public class Registry { - public ArrayList tankRegistries = new ArrayList(); + + public ArrayList tankRegistries = new ArrayList(); protected double maxTankWeight = 0; - static class TankRegistry + public static void loadRegistry (String homedir) { + String path = homedir + "/.tanks.d/tank-registry.lmao"; + try { + Scanner in = new Scanner (new File (path)); + while (in.hasNextLine()) { + String[] tankLine = in.nextLine().split(","); + if (tankLine[0].charAt(0) == '#') { continue; } + if (tankLine[2].toLowerCase().equals("default")) { + switch (tankLine[0]) { + case "brown": + new Registry.TankEntry(Game.registry, EnemyTankBrown.class, "brown", Double.parseDouble(tankLine[1])); + break; + case "gray": + new Registry.TankEntry(Game.registry, EnemyTankGray.class, "gray", Double.parseDouble(tankLine[1])); + break; + case "mint": + new Registry.TankEntry(Game.registry, EnemyTankMint.class, "mint", Double.parseDouble(tankLine[1])); + break; + case "yellow": + new Registry.TankEntry(Game.registry, EnemyTankYellow.class, "yellow", Double.parseDouble(tankLine[1])); + break; + case "magenta": + new Registry.TankEntry(Game.registry, EnemyTankMagenta.class, "magenta", Double.parseDouble(tankLine[1])); + break; + case "red": + new Registry.TankEntry(Game.registry, EnemyTankRed.class, "red", Double.parseDouble(tankLine[1])); + break; + case "green": + new Registry.TankEntry(Game.registry, EnemyTankGreen.class, "green", Double.parseDouble(tankLine[1])); + break; + case "purple": + new Registry.TankEntry(Game.registry, EnemyTankPurple.class, "purple", Double.parseDouble(tankLine[1])); + break; + case "white": + new Registry.TankEntry(Game.registry, EnemyTankWhite.class, "white", Double.parseDouble(tankLine[1])); + break; + case "orange": + new Registry.TankEntry(Game.registry, EnemyTankOrange.class, "orange", Double.parseDouble(tankLine[1])); + break; + case "darkgreen": + new Registry.TankEntry(Game.registry, EnemyTankDarkGreen.class, "darkgreen", Double.parseDouble(tankLine[1])); + break; + case "black": + new Registry.TankEntry(Game.registry, EnemyTankBlack.class, "black", Double.parseDouble(tankLine[1])); + break; + case "pink": + new Registry.TankEntry(Game.registry, EnemyTankPink.class, "pink", Double.parseDouble(tankLine[1])); + break; + default: + Game.logger.println (new Date().toString() + " (syswarn) no such default tank '" + tankLine[0] + "' exists. ignoring."); + break; + } + } else { + try { + @SuppressWarnings("resource") + ClassLoader loader = new URLClassLoader ( new URL[] { new File(tankLine[3]).toURI().toURL() }); // super messy + @SuppressWarnings("unchecked") + Class clasz = (Class) loader.loadClass(tankLine[4]); + new Registry.TankEntry(Game.registry, clasz, tankLine[0], Double.parseDouble(tankLine[1])); + } catch (Exception e) { + e.printStackTrace(); + Game.logger.println (new Date().toString() + " (syswarn) error loading custom tank '" + tankLine[3] + "'. try adding the path to your jvm classpath. ignoring."); + } + } + } + in.close(); + } catch (Exception e) { + Game.logger.println (new Date().toString() + " (syswarn) tank-registry.lmao nonexistent or broken, using default."); + new Registry.TankEntry(Game.registry, EnemyTankBrown.class, "brown", 1); + new Registry.TankEntry(Game.registry, EnemyTankGray.class, "gray", 1); + new Registry.TankEntry(Game.registry, EnemyTankMint.class, "mint", 1.0 / 2); + new Registry.TankEntry(Game.registry, EnemyTankYellow.class, "yellow", 1.0 / 2); + new Registry.TankEntry(Game.registry, EnemyTankMagenta.class, "magenta", 1.0 / 3); + new Registry.TankEntry(Game.registry, EnemyTankRed.class, "red", 1.0 / 3); + new Registry.TankEntry(Game.registry, EnemyTankGreen.class, "green", 1.0 / 4); + new Registry.TankEntry(Game.registry, EnemyTankPurple.class, "purple", 1.0 / 4); + new Registry.TankEntry(Game.registry, EnemyTankWhite.class, "white", 1.0 / 4); + new Registry.TankEntry(Game.registry, EnemyTankOrange.class, "orange", 1.0 / 6); + new Registry.TankEntry(Game.registry, EnemyTankDarkGreen.class, "darkgreen", 1.0 / 9); + new Registry.TankEntry(Game.registry, EnemyTankBlack.class, "black", 1.0 / 10); + new Registry.TankEntry(Game.registry, EnemyTankPink.class, "pink", 1.0 / 15); + } + } + + public static void initRegistry (String homedir) { + String path = homedir + "/.tanks.d/tank-registry.lmao"; + try { + new File (path).createNewFile(); + } catch (IOException e) { + Game.logger.println (new Date().toString() + " (syserr) file permissions are screwed up! cannot initialize tank registry."); + System.exit(1); + } + try { + PrintStream writer = new PrintStream (new File (path)); + writer.println("brown,"+String.valueOf(1)+",default"); + writer.println("gray,"+String.valueOf(1)+",default"); + writer.println("mint,"+String.valueOf(1.0/2)+",default"); + writer.println("yellow,"+String.valueOf(1.0/2)+",default"); + writer.println("magenta,"+String.valueOf(1.0/3)+",default"); + writer.println("red,"+String.valueOf(1.0/3)+",default"); + writer.println("green,"+String.valueOf(1.0/4)+",default"); + writer.println("purple,"+String.valueOf(1.0/4)+",default"); + writer.println("white,"+String.valueOf(1.0/4)+",default"); + writer.println("orange,"+String.valueOf(1.0/6)+",default"); + writer.println("darkgreen,"+String.valueOf(1.0/9)+",default"); + writer.println("black,"+String.valueOf(1.0/10)+",default"); + writer.println("pink,"+String.valueOf(1.0/15)+",default"); + } catch (Exception e) { + Game.logger.println (new Date().toString() + " (syserr) something broke. cannot initialize tank registry."); + System.exit(1); + } + + } + + static class TankEntry { public final Class tank; public final String name; @@ -17,7 +139,7 @@ static class TankRegistry protected double startWeight; protected double endWeight; - public TankRegistry(Registry r, Class tank, String name, double weight) + public TankEntry(Registry r, Class tank, String name, double weight) { this.tank = tank; this.name = name; @@ -44,14 +166,15 @@ public Tank getTank(double x, double y, double a) } } - public TankRegistry getRandomTank() + public TankEntry getRandomTank() { double random = Math.random() * maxTankWeight; - + System.out.println(random); for (int i = 0; i < tankRegistries.size(); i++) { - TankRegistry r = tankRegistries.get(i); - + TankEntry r = tankRegistries.get(i); + System.out.println(r.name); + System.out.println(r.startWeight + " " + r.endWeight); if (random >= r.startWeight && random < r.endWeight) { return r; @@ -61,11 +184,11 @@ public TankRegistry getRandomTank() return null; } - public TankRegistry getRegistry(String name) + public TankEntry getRegistry(String name) { for (int i = 0; i < tankRegistries.size(); i++) { - TankRegistry r = tankRegistries.get(i); + TankEntry r = tankRegistries.get(i); if (r.name.equals(name)) {