Example #1
0
  public SpongeCoremod() {
    // This MUST be the first line in the constructor, to prevent
    // JavaVersionCheckUtils from being passed through the transformer
    // chain, and thereby triggering Mixin to switch to PREINIT.
    Launch.classLoader.addTransformerExclusion("org.spongepowered.launch.JavaVersionCheckUtils");

    try {
      JavaVersionCheckUtils.ensureJava8();
    } catch (Exception e) {
      e.printStackTrace();
      this.clearSecurityManager();
      Runtime.getRuntime().exit(1);
    }

    // Let's get this party started
    MixinBootstrap.init();
    MixinEnvironment.setCompatibilityLevel(MixinEnvironment.CompatibilityLevel.JAVA_8);

    // Add pre-init mixins
    MixinEnvironment.getEnvironment(Phase.PREINIT)
        .addConfiguration("mixins.forge.preinit.json")
        .registerTokenProviderClass("org.spongepowered.mod.SpongeCoremod$TokenProvider");

    SpongeLaunch.initialize();
    SpongeImpl.getGlobalConfig(); // Load config

    MixinEnvironment.getEnvironment(Phase.INIT)
        .addConfiguration("mixins.forge.init.json")
        .registerTokenProviderClass("org.spongepowered.mod.SpongeCoremod$TokenProvider");

    // Add default mixins
    MixinEnvironment.getDefaultEnvironment()
        .addConfiguration("mixins.common.api.json")
        .addConfiguration("mixins.common.core.json")
        .addConfiguration("mixins.common.bungeecord.json")
        .addConfiguration("mixins.common.eulashutdown.json")
        .addConfiguration("mixins.common.timings.json")
        .addConfiguration("mixins.forge.core.json")
        .addConfiguration("mixins.forge.entityactivation.json")
        .addConfiguration("mixins.forge.bungeecord.json")
        .registerTokenProviderClass("org.spongepowered.mod.SpongeCoremod$TokenProvider");

    Launch.classLoader.addClassLoaderExclusion("org.spongepowered.api.event.cause.Cause");
    Launch.classLoader.addClassLoaderExclusion("org.spongepowered.api.event.cause.NamedCause");
    Launch.classLoader.addClassLoaderExclusion("org.spongepowered.api.event.Cancellable");
    Launch.classLoader.addClassLoaderExclusion(
        "org.spongepowered.api.eventgencore.annotation.PropertySettings");

    // Transformer exclusions
    Launch.classLoader.addTransformerExclusion("ninja.leaping.configurate.");
    Launch.classLoader.addTransformerExclusion("org.apache.commons.lang3.");
    Launch.classLoader.addTransformerExclusion("org.spongepowered.mod.interfaces.IMixinEvent");
    Launch.classLoader.addTransformerExclusion("org.spongepowered.common.launch.");

    SpongeSuperclassRegistry.registerSuperclassModification(
        "org.spongepowered.api.entity.ai.task.AbstractAITask",
        "org.spongepowered.common.entity.ai.SpongeEntityAICommonSuperclass");
    SpongeSuperclassRegistry.registerSuperclassModification(
        "org.spongepowered.api.event.cause.entity.damage.source.common.AbstractDamageSource",
        "org.spongepowered.common.event.damage.SpongeCommonDamageSource");
    SpongeSuperclassRegistry.registerSuperclassModification(
        "org.spongepowered.api.event.cause.entity.damage.source.common.AbstractEntityDamageSource",
        "org.spongepowered.common.event.damage.SpongeCommonEntityDamageSource");
    SpongeSuperclassRegistry.registerSuperclassModification(
        "org.spongepowered.api.event.cause.entity.damage.source.common.AbstractIndirectEntityDamageSource",
        "org.spongepowered.common.event.damage.SpongeCommonIndirectEntityDamageSource");
  }
  public static void findPlugins(boolean scanClasspath) throws IOException {
    SetMultimap<Object, String> found = LinkedHashMultimap.create();
    Set<String> pluginClasses = null;

    VanillaLaunch.getLogger().info("Searching for plugins...");

    if (scanClasspath) {
      VanillaLaunch.getLogger().info("Scanning classpath for plugins...");

      ClassLoader loader = VanillaLaunch.class.getClassLoader();
      if (loader instanceof URLClassLoader) {
        pluginClasses = PluginScanner.scanClassPath((URLClassLoader) loader);
        found.putAll("classpath", pluginClasses);
      } else {
        VanillaLaunch.getLogger()
            .error(
                "Cannot search for plugins on classpath: Unsupported class loader: {}",
                loader.getClass());
      }
    }

    Path pluginsDir = SpongeLaunch.getPluginsDir();
    if (Files.exists(pluginsDir)) {
      if (pluginClasses == null) {
        pluginClasses = new HashSet<>();
      }

      List<Path> paths;
      try (DirectoryStream<Path> dir =
          Files.newDirectoryStream(SpongeLaunch.getPluginsDir(), ARCHIVE_FILTER)) {
        paths = Lists.newArrayList(dir);
      }

      Collections.sort(paths);

      for (Path path : paths) {
        // Search for plugins in the JAR
        try (JarFile jar = new JarFile(path.toFile())) {
          Set<String> plugins = PluginScanner.scanZip(path, jar);

          Iterator<String> itr = plugins.iterator();
          while (itr.hasNext()) {
            String plugin = itr.next();
            if (!pluginClasses.add(plugin)) {
              VanillaLaunch.getLogger()
                  .warn("Skipping duplicate plugin class {} from {}", plugin, path);
              itr.remove();
            }
          }

          if (!plugins.isEmpty()) {
            found.putAll(path, plugins);

            // Look for access transformers
            PluginAccessTransformers.register(path, jar);
          }
        } catch (IOException e) {
          VanillaLaunch.getLogger().error("Failed to scan plugin JAR: {}", path, e);
        }
      }
    } else {
      // Create plugin folder
      Files.createDirectories(pluginsDir);
    }

    plugins = ImmutableSetMultimap.copyOf(found);
    VanillaLaunch.getLogger().info("{} plugin(s) found", plugins.size());
  }