/newcode/src/com/prupe/mcpatcher/TexturePackChangeHandler.java

https://bitbucket.org/Freso/mcpatcher · Java · 177 lines · 154 code · 23 blank · 0 comment · 21 complexity · 5816dfe91f89bf85a7785790e2567152 MD5 · raw file

  1. package com.prupe.mcpatcher;
  2. import net.minecraft.client.Minecraft;
  3. import net.minecraft.src.*;
  4. import java.util.*;
  5. abstract public class TexturePackChangeHandler {
  6. private static final MCLogger logger = MCLogger.getLogger("Texture Pack");
  7. private static final ArrayList<TexturePackChangeHandler> handlers = new ArrayList<TexturePackChangeHandler>();
  8. private static boolean initializing;
  9. private static boolean changing;
  10. private static long startTime;
  11. private static long startMem;
  12. private boolean updateNeeded;
  13. protected final String name;
  14. protected final int order;
  15. public TexturePackChangeHandler(String name, int order) {
  16. this.name = name;
  17. this.order = order;
  18. }
  19. public void initialize() {
  20. beforeChange();
  21. afterChange();
  22. }
  23. public void refresh() {
  24. beforeChange();
  25. afterChange();
  26. }
  27. abstract public void beforeChange();
  28. abstract public void afterChange();
  29. public void afterChange2() {
  30. }
  31. protected void setUpdateNeeded(boolean updateNeeded) {
  32. this.updateNeeded = updateNeeded;
  33. }
  34. public static void scheduleTexturePackRefresh() {
  35. MCPatcherUtils.getMinecraft().scheduleTexturePackRefresh();
  36. }
  37. public static void register(TexturePackChangeHandler handler) {
  38. if (handler != null) {
  39. if (Minecraft.getInstance().getResourceManager() != null) {
  40. try {
  41. logger.info("initializing %s...", handler.name);
  42. handler.initialize();
  43. } catch (Throwable e) {
  44. e.printStackTrace();
  45. logger.severe("%s initialization failed", handler.name);
  46. }
  47. }
  48. handlers.add(handler);
  49. logger.fine("registered texture pack handler %s, priority %d", handler.name, handler.order);
  50. Collections.sort(handlers, new Comparator<TexturePackChangeHandler>() {
  51. public int compare(TexturePackChangeHandler o1, TexturePackChangeHandler o2) {
  52. return o1.order - o2.order;
  53. }
  54. });
  55. }
  56. }
  57. public static void earlyInitialize(String className, String methodName) {
  58. try {
  59. logger.fine("calling %s.%s", className, methodName);
  60. Class.forName(className).getDeclaredMethod(methodName).invoke(null);
  61. } catch (Throwable e) {
  62. }
  63. }
  64. public static void checkForTexturePackChange() {
  65. for (TexturePackChangeHandler handler : handlers) {
  66. if (handler.updateNeeded) {
  67. handler.updateNeeded = false;
  68. try {
  69. logger.info("refreshing %s...", handler.name);
  70. handler.refresh();
  71. } catch (Throwable e) {
  72. e.printStackTrace();
  73. logger.severe("%s refresh failed", handler.name);
  74. }
  75. }
  76. }
  77. }
  78. public static void beforeChange1(boolean initializing1) {
  79. logger.finer("beforeChange1(%s) initializing=%s changing=%s", initializing1, initializing, changing);
  80. if (initializing1) {
  81. logger.finer("skipping beforeChange1 because we are still initializing");
  82. initializing = true;
  83. return;
  84. }
  85. if (changing && !initializing) {
  86. new RuntimeException("unexpected recursive call to TexturePackChangeHandler").printStackTrace();
  87. return;
  88. }
  89. changing = true;
  90. startTime = System.currentTimeMillis();
  91. Runtime runtime = Runtime.getRuntime();
  92. startMem = runtime.totalMemory() - runtime.freeMemory();
  93. List<ResourcePack> resourcePacks = TexturePackAPI.getResourcePacks(null);
  94. logger.fine("%s resource packs (%d selected):", initializing ? "initializing" : "changing", resourcePacks.size());
  95. for (ResourcePack pack : resourcePacks) {
  96. logger.fine("resource pack: %s", pack);
  97. }
  98. for (TexturePackChangeHandler handler : handlers) {
  99. try {
  100. logger.info("refreshing %s (pre)...", handler.name);
  101. handler.beforeChange();
  102. } catch (Throwable e) {
  103. e.printStackTrace();
  104. logger.severe("%s.beforeChange failed", handler.name);
  105. }
  106. }
  107. TextureManager textureManager = MCPatcherUtils.getMinecraft().getTextureManager();
  108. if (textureManager != null) {
  109. Set<ResourceLocation> texturesToUnload = new HashSet<ResourceLocation>();
  110. for (Map.Entry<ResourceLocation, TextureObject> entry : textureManager.texturesByName.entrySet()) {
  111. ResourceLocation resource = entry.getKey();
  112. TextureObject texture = entry.getValue();
  113. if (texture instanceof SimpleTexture && !TexturePackAPI.hasResource(resource)) {
  114. texturesToUnload.add(resource);
  115. }
  116. }
  117. for (ResourceLocation resource : texturesToUnload) {
  118. TexturePackAPI.unloadTexture(resource);
  119. }
  120. }
  121. }
  122. public static void afterChange1(boolean initializing1) {
  123. logger.finer("afterChange1(%s) initializing=%s changing=%s", initializing1, initializing, changing);
  124. if (initializing && !initializing1) {
  125. logger.finer("deferring afterChange1 because we are still initializing");
  126. return;
  127. }
  128. for (TexturePackChangeHandler handler : handlers) {
  129. try {
  130. logger.info("refreshing %s (post)...", handler.name);
  131. handler.afterChange();
  132. } catch (Throwable e) {
  133. e.printStackTrace();
  134. logger.severe("%s.afterChange failed", handler.name);
  135. }
  136. }
  137. for (int i = handlers.size() - 1; i >= 0; i--) {
  138. TexturePackChangeHandler handler = handlers.get(i);
  139. try {
  140. handler.afterChange2();
  141. } catch (Throwable e) {
  142. e.printStackTrace();
  143. logger.severe("%s.afterChange2 failed", handler.name);
  144. }
  145. }
  146. System.gc();
  147. long timeDiff = System.currentTimeMillis() - startTime;
  148. Runtime runtime = Runtime.getRuntime();
  149. long memDiff = runtime.totalMemory() - runtime.freeMemory() - startMem;
  150. logger.info("done (%.3fs elapsed, mem usage %+.1fMB)\n", timeDiff / 1000.0, memDiff / 1048576.0);
  151. changing = false;
  152. initializing = false;
  153. }
  154. }