001package edu.pdx.cs410J.security;
002
003import java.io.*;
004import java.net.*;
005
006/**
007 * This class represents a game console that can play many games.
008 */
009public class GameConsole {
010
011  /**
012   * Called from the Game to write a game's preferences file.
013   *
014   * @return <code>true</code> if the preferences were sucessfully
015   * written (note that we don't want the name of the preferences file
016   * escaping!)
017   */
018  public boolean writePreferences(Game game, String prefs) {
019    // Write to a file in the ${user.home} preferences directory
020    String home = System.getProperty("user.home");
021    try {
022      File dir = new File(home, ".games");
023      if (!dir.exists()) {
024        dir.mkdirs();
025      }
026
027      File file = new File(dir, game.getName());
028      FileWriter fw = new FileWriter(file);
029      fw.write(prefs);
030      fw.flush();
031      fw.close();
032    } catch (IOException ex) {
033      return false;
034    }
035
036    return true;
037  }
038
039  /**
040   * Called from the Game to read a game's preferences file.
041   *
042   * @return <code>null</code> if the preferences could not be read
043   */
044  public String readPreferences(Game game) {
045    String home = System.getProperty("user.home");
046    try {
047      File dir = new File(home, ".games");
048      if (!dir.exists()) {
049        dir.mkdirs();
050      }
051
052      File file = new File(dir, game.getName());
053      if (!file.exists()) {
054        // No preferences
055        return("");
056
057      } else if (file.isDirectory()) {
058        return(null);
059      }
060
061      StringBuffer sb = new StringBuffer();
062      char[] arr = new char[1024];
063      FileReader fr = new FileReader(file);
064      while (fr.ready()) {
065        fr.read(arr, 0, arr.length);
066        sb.append(arr);
067      }
068
069      return sb.toString();
070
071    } catch (IOException ex) {
072      return null;
073    }
074  }
075
076
077  /**
078   * Loads a <code>Game</code> from a given URL.
079   */
080  public Game loadGame(String gameName, String gameURL) 
081    throws Exception {
082
083    // Use a URLClassLoader to load the Game class
084    URL[] urls = { new URL(gameURL) };
085    URLClassLoader loader = new URLClassLoader(urls);
086    Class gameClass = loader.loadClass(gameName);
087
088    // Make an instance of the Game class and return it
089    Game game = (Game) gameClass.newInstance();
090    return game;
091  }
092
093  /**
094   * The command line contains the name of the game and a URL from
095   * where to load it.
096   */
097  public static void main(String[] args) {
098    String gameName = args[0];
099    String gameURL = args[1];
100
101    GameConsole console = new GameConsole();
102
103    try {
104      Game game = console.loadGame(gameName, gameURL);
105      game.play(console);
106
107    } catch (Exception ex) {
108      System.err.println("** Could not load game " + gameName + 
109                         " from " + gameURL);
110      System.err.println("** " + ex);
111    }
112  }
113}