Esempio n. 1
0
  /** Initialise mod hooks */
  private void initHooks() {
    try {
      // Chat hook
      if ((chatListeners.size() > 0 || chatFilters.size() > 0) && !chatHooked) {
        chatHooked = true;
        HookChat.Register();
        HookChat.RegisterPacketHandler(this);
      }

      // Login hook
      if ((preLoginListeners.size() > 0 || loginListeners.size() > 0) && !loginHooked) {
        loginHooked = true;
        ModUtilities.registerPacketOverride(1, HookLogin.class);
        HookLogin.loader = this;
      }

      // Plugin channels hook
      if (pluginChannelListeners.size() > 0 && !pluginChannelHooked) {
        pluginChannelHooked = true;
        HookPluginChannels.Register();
        HookPluginChannels.RegisterPacketHandler(this);
      }

      // Tick hook
      if (!tickHooked) {
        tickHooked = true;
        PrivateFields.minecraftProfiler.SetFinal(minecraft, new HookProfiler(this, logger));
      }
    } catch (Exception ex) {
      logger.log(Level.WARNING, "Error creating hooks", ex);
      ex.printStackTrace();
    }
  }
Esempio n. 2
0
  /**
   * 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;
  }
Esempio n. 3
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);
  }