Example #1
0
  /**
   * Recursive function to enumerate classes inside a classpath folder
   *
   * @param superClass
   * @param classloader
   * @param classes
   * @param packagePath
   * @param packageName
   */
  private static void enumerateDirectory(
      String prefix,
      Class superClass,
      ClassLoader classloader,
      LinkedList<Class> classes,
      File packagePath,
      String packageName) {
    File[] classFiles = packagePath.listFiles();

    for (File classFile : classFiles) {
      if (classFile.isDirectory()) {
        enumerateDirectory(
            prefix,
            superClass,
            classloader,
            classes,
            classFile,
            packageName + classFile.getName() + ".");
      } else {
        if (classFile.getName().endsWith(".class")
            && (prefix == null || classFile.getName().startsWith(prefix))) {
          String classFileName = classFile.getName();
          String className = packageName + classFileName.substring(0, classFileName.length() - 6);
          checkAndAddClass(classloader, superClass, classes, className);
        }
      }
    }
  }
Example #2
0
 private void initModulesDir() throws RuntimeConfigException {
   this.modulesDirPath = null;
   String modulesDirPath = getProperty(modulesDirKey);
   if (modulesDirPath != null) {
     File modulesDir = new File(modulesDirPath);
     if (!modulesDir.isDirectory()) {
       throw createInvalidPropertyValueException(modulesDirKey, modulesDirPath);
     }
     this.modulesDirPath = modulesDir.getPath();
   } else {
     // try default
     File modulesDir = new File(substitute(defaultHomeModulesDirPath));
     if (modulesDir.isDirectory()) {
       this.modulesDirPath = modulesDir.getPath();
     }
   }
 }
Example #3
0
 public void startFile(String logfile) {
   File parent = new File(logfile).getParentFile();
   if (!parent.isDirectory() && !parent.mkdirs()) {
     logger.warning("Could not create log folder: " + parent);
   }
   Handler fileHandler = new RotatingFileHandler(logfile);
   fileHandler.setFormatter(new DateOutputFormatter(FILE_DATE));
   logger.addHandler(fileHandler);
 }
Example #4
0
 private void initLibDirs() throws RuntimeConfigException {
   this.libDirPaths = new String[0];
   String libDirPathsString = getProperty(libDirsKey);
   if (libDirPathsString != null) {
     String[] libDirPaths = splitLibDirPaths(libDirPathsString);
     for (String libDirPath : libDirPaths) {
       File libDir = new File(libDirPath);
       if (!libDir.isDirectory()) {
         throw createInvalidPropertyValueException(libDirsKey, libDirPathsString);
       }
     }
     this.libDirPaths = libDirPaths;
   } else {
     // try default
     libDirPathsString = substitute(defaultHomeLibDirPath);
     File libDir = new File(libDirPathsString);
     if (libDir.isDirectory()) {
       this.libDirPaths = new String[] {libDirPathsString};
     }
   }
 }
Example #5
0
 private void maybeInitHomeDir() throws RuntimeConfigException {
   String homeDirPath = getProperty(homeDirKey);
   if (!isNullOrEmptyString(homeDirPath)) {
     // ok set: must be an existing directory
     File homeDir = new File(homeDirPath);
     if (!homeDir.isDirectory()) {
       throw createInvalidPropertyValueException(homeDirKey, homeDirPath);
     }
     try {
       this.homeDirPath = homeDir.getCanonicalPath();
     } catch (IOException e) {
       throw new RuntimeConfigException(e.getMessage(), e);
     }
   }
 }
Example #6
0
 public static String[] get_filelist(String path, boolean nodirs, boolean nofiles) {
   File folder = new File(path);
   if (folder.isDirectory()) {
     File[] listOfFiles = folder.listFiles();
     java.util.Vector<String> r = new java.util.Vector<String>();
     for (int i = 0; listOfFiles != null && i < listOfFiles.length; i++) {
       if ((listOfFiles[i].isFile() && !nofiles) || (listOfFiles[i].isDirectory() && !nodirs)) {
         r.add(listOfFiles[i].getName());
       }
     }
     return r.toArray(new String[0]);
   } else {
     return null; // A: no existe o no es directorio
   }
 }
Example #7
0
  /** Get the "mods" folder */
  public File getModsFolder() {
    if (modsFolder == null) {
      modsFolder = new File(Minecraft.getMinecraftDir(), "mods");

      if (!modsFolder.exists() || !modsFolder.isDirectory()) {
        try {
          // Attempt to create the "mods" folder if it does not already exist
          modsFolder.mkdirs();
        } catch (Exception ex) {
        }
      }
    }

    return modsFolder;
  }
Example #8
0
  /**
   * Enumerate classes on the classpath which are subclasses of the specified class
   *
   * @param superClass
   * @return
   */
  private static LinkedList<Class> getSubclassesFor(
      File packagePath, ClassLoader classloader, Class superClass, String prefix) {
    LinkedList<Class> classes = new LinkedList<Class>();

    try {
      if (packagePath.isDirectory()) {
        enumerateDirectory(prefix, superClass, classloader, classes, packagePath);
      } else if (packagePath.isFile()
          && (packagePath.getName().endsWith(".jar")
              || packagePath.getName().endsWith(".zip")
              || packagePath.getName().endsWith(".litemod"))) {
        enumerateCompressedPackage(prefix, superClass, classloader, classes, packagePath);
      }
    } catch (Throwable th) {
      logger.log(Level.WARNING, "Enumeration error", th);
    }

    return classes;
  }
  /** Save the bundle to a file. Returns false if save was not successful. */
  public boolean saveBundleToFile(final String localeName) {
    try {
      final File externalBundleDir = new File(EXTERNAL_BUNDLE_DIR);
      if (!externalBundleDir.isDirectory()) {
        externalBundleDir.mkdirs();
      }
      final String filename = EXTERNAL_BUNDLE_DIR + "langres_" + localeName + ".properties";
      final PrintWriter out =
          new PrintWriter(
              new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF-8")));

      final TreeMap<String, String> sorter = new TreeMap<String, String>(bundle);

      for (final String string : sorter.keySet()) {
        String key = string;
        String val = getString(key);
        key = key.trim();
        val = val.trim();
        // change newlines in val into \n
        final StringBuilder sbTmp = new StringBuilder();
        for (int x = 0; x < val.length(); x++) {
          final char c = val.charAt(x);
          if (c == '\n') {
            sbTmp.append("\\n");
          } else {
            sbTmp.append(c);
          }
        }
        val = sbTmp.toString();

        out.println(key + "=" + val);
      }

      out.close();
      return true;
    } catch (final Throwable t) {
      logger.log(Level.SEVERE, "Error saving bundle.", t);
      return false;
    }
  }
Example #10
0
  /** Enumerate the java class path and "mods" folder to find mod classes, then load the classes */
  private void prepareMods() {
    // List of mod files in the "mods" folder
    LinkedList<File> modFiles = new LinkedList<File>();

    // Find and enumerate the "mods" folder
    File modFolder = getModsFolder();
    if (modFolder.exists() && modFolder.isDirectory()) {
      logger.info("Mods folder found, searching " + modFolder.getPath());
      findModFiles(modFolder, modFiles);
      logger.info("Found " + modFiles.size() + " mod file(s)");
    }

    // Find and enumerate classes on the class path
    HashMap<String, Class> modsToLoad = null;
    try {
      logger.info("Enumerating class path...");

      String classPath = System.getProperty("java.class.path");
      String classPathSeparator = System.getProperty("path.separator");
      String[] classPathEntries = classPath.split(classPathSeparator);

      logger.info(String.format("Class path separator=\"%s\"", classPathSeparator));
      logger.info(
          String.format(
              "Class path entries=(\n   classpathEntry=%s\n)",
              classPath.replace(classPathSeparator, "\n   classpathEntry=")));

      logger.info("Loading mods from class path...");

      modsToLoad = findModClasses(classPathEntries, modFiles);

      logger.info("Mod class discovery completed");
    } catch (Throwable th) {
      logger.log(Level.WARNING, "Mod class discovery failed", th);
      return;
    }

    loadMods(modsToLoad);
  }