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

https://bitbucket.org/SevenBits/mcpatcher · Java · 184 lines · 161 code · 23 blank · 0 comment · 23 complexity · 27834038a3cf42408e8f9731257be039 MD5 · raw file

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