Advertisement
JustinSamaKun

Untitled

Apr 20th, 2020
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. package net.eterniamc.extramixinstuffs;
  2.  
  3. import com.google.gson.GsonBuilder;
  4. import com.google.gson.stream.JsonReader;
  5. import net.minecraft.launchwrapper.LaunchClassLoader;
  6. import net.minecraftforge.fml.common.FMLLog;
  7. import net.minecraftforge.fml.relauncher.CoreModManager;
  8. import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;
  9. import org.apache.commons.io.FileUtils;
  10. import org.spongepowered.asm.launch.MixinBootstrap;
  11. import org.spongepowered.asm.mixin.MixinEnvironment;
  12. import org.spongepowered.asm.mixin.Mixins;
  13.  
  14. import javax.annotation.Nullable;
  15. import java.io.*;
  16. import java.util.Collection;
  17. import java.util.Map;
  18. import java.util.zip.ZipEntry;
  19. import java.util.zip.ZipInputStream;
  20.  
  21. @IFMLLoadingPlugin.MCVersion("1.12.2")
  22. @IFMLLoadingPlugin.DependsOn("pixelmon")
  23. public class MixinsCoreMod implements IFMLLoadingPlugin {
  24. static File modFile;
  25.  
  26. public MixinsCoreMod() {
  27.  
  28. //TODO nasty hack to get pixelmon up at the same time as the mixins
  29. try {
  30. findAndLoadPixelmon();
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34.  
  35. MixinBootstrap.init();
  36.  
  37. //TODO debug options
  38. MixinEnvironment.getDefaultEnvironment().setOption(MixinEnvironment.Option.DEBUG_EXPORT_DECOMPILE, true);
  39. MixinEnvironment.getDefaultEnvironment().setOption(MixinEnvironment.Option.DEBUG_EXPORT, true);
  40.  
  41. Mixins.addConfiguration("mixins.mymixins.json");
  42. try {
  43. File file = new File("./config/mixins.json");
  44. if (!file.exists()) {
  45. ExtraMixinStuffs.config = new Config();
  46. file.createNewFile();
  47. PrintWriter pw = new PrintWriter(file);
  48. String json = new GsonBuilder().setPrettyPrinting().create().toJson(ExtraMixinStuffs.config);
  49. pw.print(json);
  50. pw.flush();
  51. pw.close();
  52. } else
  53. ExtraMixinStuffs.config = new GsonBuilder().setPrettyPrinting().create().fromJson(new JsonReader(new FileReader(file)), Config.class);
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58.  
  59. private void loadModJar(File jar) throws Exception{
  60. ((LaunchClassLoader) this.getClass().getClassLoader()).addURL(jar.toURI().toURL());
  61. CoreModManager.getReparseableCoremods().add(jar.getName());
  62. }
  63.  
  64. @Override
  65. public String[] getASMTransformerClass() {
  66. return new String[] {};
  67. }
  68.  
  69. @Override
  70. public String getModContainerClass() {
  71. return null;
  72. }
  73.  
  74. @Nullable
  75. @Override
  76. public String getSetupClass() {
  77. return null;
  78. }
  79.  
  80. @Override
  81. public void injectData(Map<String, Object> data) {
  82. modFile = (File) data.get("coremodLocation");
  83. if (modFile == null) {
  84. modFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
  85. }
  86. }
  87.  
  88. @Override
  89. public String getAccessTransformerClass() {
  90. return null;
  91. }
  92.  
  93. /**
  94. * NOTE: this can be used for any other mod as well.
  95. *
  96. * @author happyzleaf (finding the jar)
  97. * @author clienthax (loading the jar)
  98. */
  99. public void findAndLoadPixelmon() {
  100. try {
  101. FMLLog.log.info("Trying to load pixelmon...");
  102. File modsFolder = new File(System.getProperty("user.dir"), "mods");
  103. if (!modsFolder.exists()) {
  104. FMLLog.log.error("The mods folder couldn't be found.\nFolder: ", modsFolder.toString());
  105. }
  106.  
  107. //Scanning through /mods/ searching for jars
  108. Collection<File> jars = FileUtils.listFiles(modsFolder, new String[]{"jar"}, false);
  109. File pixelmon = null;
  110. for (File jar : jars) {
  111. ZipInputStream zip = new ZipInputStream(new FileInputStream(jar));
  112. ZipEntry entry;
  113. while ((entry = zip.getNextEntry()) != null) {
  114. zip.closeEntry();
  115.  
  116. //Checking if the jar is the pixelmon
  117. if (entry.getName().equals("com/pixelmonmod/pixelmon/Pixelmon.class")) {
  118. pixelmon = jar;
  119. break;
  120. }
  121. }
  122. zip.close();
  123. if (pixelmon != null) break;
  124. }
  125.  
  126. //Opsie dopsie, no pixelmon jars found :(
  127. if (pixelmon == null) {
  128. FMLLog.log.error("Pixelmon's jar cannot be found, the program will not continue.");
  129. return;
  130. }
  131.  
  132. //Once we know the file that contains the pixelmon, it's better to check if another coremod already loaded it
  133. if (!CoreModManager.getReparseableCoremods().contains(pixelmon.getName())) {
  134.  
  135. //Now we're safe to load the jar (thanks to clienthax)
  136. ((LaunchClassLoader) this.getClass().getClassLoader()).addURL(pixelmon.toURI().toURL());
  137. CoreModManager.getReparseableCoremods().add(pixelmon.getName());
  138. }
  139. FMLLog.log.info("Loaded!");
  140. } catch (Exception e) {
  141. FMLLog.log.error("There was a problem trying to find Pixelmon's jar, the program will not continue.", e);
  142. return;
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement