public void preprocess(Run run) { File logFile = new File(getRunDir(run), "genetik.log"); try { SimpleFileHandler fh = new SimpleFileHandler(logFile); fh.setFormatter(new CompactFormatter()); Logger logger = Logger.getLogger(GenetikConstants.LOGGER); logger.setLevel(Level.INFO); logger.setUseParentHandlers(false); Handler handlers[] = logger.getHandlers(); logger.addHandler(fh); for (Handler h : handlers) { logger.removeHandler(h); if (h instanceof SimpleFileHandler) h.close(); // close our old one } } catch (Exception exp) { throw new IllegalArgumentException( "Unable to create log file at " + logFile.getAbsolutePath()); } super.preprocess(run); }
/** * Find mod files in the "mods" folder * * @param modFolder Folder to search * @param modFiles List of mod files to load */ protected void findModFiles(File modFolder, LinkedList<File> modFiles) { List<String> supportedVerions = Arrays.asList(SUPPORTED_VERSIONS); for (File modFile : modFolder.listFiles(this)) { try { // Check for a version file ZipFile modZip = new ZipFile(modFile); ZipEntry version = modZip.getEntry("version.txt"); if (version != null) { // Read the version string InputStream versionStream = modZip.getInputStream(version); BufferedReader versionReader = new BufferedReader(new InputStreamReader(versionStream)); String strVersion = versionReader.readLine(); versionReader.close(); // Only add the mod if the version matches and we were able to successfully add it to the // class path if (supportedVerions.contains(strVersion) && addURLToClassPath(modFile.toURI().toURL())) { modFiles.add(modFile); } } modZip.close(); } catch (Exception ex) { logger.warning( "Error enumerating '" + modFile.getAbsolutePath() + "': Invalid zip file or error reading file"); } } }
/** * Find mod classes in the class path and enumerated mod files list * * @param classPathEntries Java class path split into string entries * @return map of classes to load */ private HashMap<String, Class> findModClasses( String[] classPathEntries, LinkedList<File> modFiles) { // To try to avoid loading the same mod multiple times if it appears in more than one entry in // the class path, we index // the mods by name and hopefully match only a single instance of a particular mod HashMap<String, Class> modsToLoad = new HashMap<String, Class>(); try { logger.info("Searching protection domain code source..."); File packagePath = new File(LiteLoader.class.getProtectionDomain().getCodeSource().getLocation().toURI()); LinkedList<Class> modClasses = getSubclassesFor(packagePath, Minecraft.class.getClassLoader(), LiteMod.class, "LiteMod"); for (Class mod : modClasses) { modsToLoad.put(mod.getSimpleName(), mod); } if (modClasses.size() > 0) logger.info(String.format("Found %s potential matches", modClasses.size())); } catch (Throwable th) { logger.warning("Error loading from local class path: " + th.getMessage()); } // Search through the class path and find mod classes for (String classPathPart : classPathEntries) { logger.info(String.format("Searching %s...", classPathPart)); File packagePath = new File(classPathPart); LinkedList<Class> modClasses = getSubclassesFor(packagePath, Minecraft.class.getClassLoader(), LiteMod.class, "LiteMod"); for (Class mod : modClasses) { modsToLoad.put(mod.getSimpleName(), mod); } if (modClasses.size() > 0) logger.info(String.format("Found %s potential matches", modClasses.size())); } // Search through mod files and find mod classes for (File modFile : modFiles) { logger.info(String.format("Searching %s...", modFile.getAbsolutePath())); LinkedList<Class> modClasses = getSubclassesFor(modFile, Minecraft.class.getClassLoader(), LiteMod.class, "LiteMod"); for (Class mod : modClasses) { modsToLoad.put(mod.getSimpleName(), mod); } if (modClasses.size() > 0) logger.info(String.format("Found %s potential matches", modClasses.size())); } return modsToLoad; }
public void saveRunData(Run run, String name, String data) { File file = new File(getRunDir(run), name); try { BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(data); writer.close(); } catch (Exception exp) { throw new IllegalArgumentException( "Error trying to save data to " + file.getAbsolutePath(), exp); } }
@Timeout public void handletimeout(Timer timer) { // delete temp files for (File f : deleteTempFileList) { dbgLog.fine("file to be deleted: path=" + f.getAbsolutePath() + "\tname=" + f.getName()); if (f.exists()) { boolean sc = f.delete(); if (!sc) { dbgLog.fine( "failed to delete file: path=" + f.getAbsolutePath() + "\tname=" + f.getName()); } else { dbgLog.fine("successfully deleted? let's check its existence"); if (f.exists()) { dbgLog.fine("surprise: actually the File still exists"); } else { dbgLog.fine("The file no longer exists"); } } } } }
public String getRunData(Run run, String name) { File file = new File(getRunDir(run), name); String retVal = null; if (!file.exists()) return null; try { BufferedReader reader = new BufferedReader(new FileReader(file)); StringBuilder builder = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { builder.append(line); } reader.close(); retVal = builder.toString(); } catch (Exception exp) { throw new IllegalArgumentException( "Error trying to read data at " + file.getAbsolutePath(), exp); } return retVal; }
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"); } } }
public static String temp_filePath(String namePattern, String ext) throws IOException, FileNotFoundException { File temp = File.createTempFile("temp-file-name", ".tmp"); return temp.getAbsolutePath(); }