Esempio n. 1
0
 public static TypeList load(String gamePath, String dataName) {
   System.out.println("load:" + dataName);
   InputStream is = null;
   Savable sav = null;
   try {
     File file =
         new File(
             System.getProperty("user.dir")
                 + File.separator
                 + gamePath
                 + File.separator
                 + dataName);
     if (!file.exists()) {
       return null;
     }
     is = new BufferedInputStream(new FileInputStream(file));
     // is = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
     XMLImporter imp = XMLImporter.getInstance();
     // if (manager != null) {
     //     imp.setAssetManager(manager);
     // }
     sav = imp.load(is);
   } catch (IOException ex) {
     Logger.getLogger(Type.class.getName()).log(Level.SEVERE, "Error loading data: {0}", ex);
     ex.printStackTrace();
   } finally {
     if (is != null) {
       try {
         is.close();
       } catch (IOException ex) {
         Logger.getLogger(Type.class.getName()).log(Level.SEVERE, "Error loading data: {0}", ex);
         ex.printStackTrace();
       }
     }
   }
   return (TypeList) sav;
 }
Esempio n. 2
0
  public void save(String gamePath, String dataName) {
    XMLExporter ex = XMLExporter.getInstance();
    OutputStream os = null;
    try {

      File daveFolder = new File(System.getProperty("user.dir") + File.separator + gamePath);
      if (!daveFolder.exists() && !daveFolder.mkdirs()) {
        Logger.getLogger(Type.class.getName()).log(Level.SEVERE, "Error creating save file!");
        throw new IllegalStateException("SaveGame dataset cannot be created");
      }
      File saveFile = new File(daveFolder.getAbsolutePath() + File.separator + dataName);
      if (!saveFile.exists()) {
        if (!saveFile.createNewFile()) {
          Logger.getLogger(Type.class.getName()).log(Level.SEVERE, "Error creating save file!");
          throw new IllegalStateException("SaveGame dataset cannot be created");
        }
      }
      os = new BufferedOutputStream(new FileOutputStream(saveFile));
      // os = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(saveFile)));
      ex.save(this, os);
    } catch (IOException ex1) {
      Logger.getLogger(Type.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
      ex1.printStackTrace();
      throw new IllegalStateException("SaveGame dataset cannot be saved");
    } finally {
      try {
        if (os != null) {
          os.close();
        }
      } catch (IOException ex1) {
        Logger.getLogger(Type.class.getName()).log(Level.SEVERE, "Error saving data: {0}", ex1);
        ex1.printStackTrace();
        throw new IllegalStateException("SaveGame dataset cannot be saved");
      }
    }
  }