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

https://bitbucket.org/Freso/mcpatcher · Java · 165 lines · 148 code · 17 blank · 0 comment · 37 complexity · 1c7d608a9fa25f549b7cc9713be5fb03 MD5 · raw file

  1. package com.prupe.mcpatcher;
  2. import net.minecraft.src.Icon;
  3. import net.minecraft.src.Tessellator;
  4. import net.minecraft.src.TextureAtlas;
  5. import java.lang.reflect.Field;
  6. import java.lang.reflect.Modifier;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import java.util.WeakHashMap;
  11. public class TessellatorUtils {
  12. private static final MCLogger logger = MCLogger.getLogger("Tilesheet");
  13. private static final Integer MAGIC_VALUE = 0x12345678;
  14. private static final Map<TextureAtlas, String> textureMapNames = new WeakHashMap<TextureAtlas, String>();
  15. private static final Map<Icon, TextureAtlas> iconMap = new HashMap<Icon, TextureAtlas>();
  16. private static Field[] fieldsToCopy;
  17. public static boolean haveBufferSize;
  18. public static void clearDefaultTextureMap(Tessellator tessellator) {
  19. tessellator.textureMap = null;
  20. }
  21. public static Tessellator getTessellator(Tessellator tessellator, Icon icon) {
  22. TextureAtlas textureMap = iconMap.get(icon);
  23. if (textureMap == null) {
  24. return tessellator;
  25. }
  26. Tessellator newTessellator = tessellator.children.get(textureMap);
  27. if (newTessellator == null) {
  28. String mapName = textureMapNames.get(textureMap);
  29. if (mapName == null) {
  30. mapName = textureMap.toString();
  31. }
  32. logger.fine("new Tessellator for texture map %s gl texture %d", mapName, textureMap.glTextureId);
  33. newTessellator = new Tessellator(0x200000);
  34. copyFields(tessellator, newTessellator, true);
  35. newTessellator.textureMap = textureMap;
  36. tessellator.children.put(textureMap, newTessellator);
  37. } else {
  38. copyFields(tessellator, newTessellator, false);
  39. }
  40. return newTessellator;
  41. }
  42. static void registerTextureMap(TextureAtlas textureMap, String name) {
  43. textureMapNames.put(textureMap, name);
  44. }
  45. static void registerIcon(TextureAtlas textureMap, Icon icon) {
  46. iconMap.put(icon, textureMap);
  47. }
  48. private static Field[] getFieldsToCopy(Tessellator tessellator) {
  49. int saveBufferSize;
  50. if (haveBufferSize) {
  51. saveBufferSize = tessellator.bufferSize;
  52. tessellator.bufferSize = MAGIC_VALUE;
  53. } else {
  54. saveBufferSize = 0;
  55. }
  56. int saveVertexCount = tessellator.vertexCount;
  57. int saveAddedVertices = tessellator.addedVertices;
  58. int saveRawBufferIndex = tessellator.rawBufferIndex;
  59. tessellator.vertexCount = MAGIC_VALUE;
  60. tessellator.addedVertices = MAGIC_VALUE;
  61. tessellator.rawBufferIndex = MAGIC_VALUE;
  62. ArrayList<Field> fields = new ArrayList<Field>();
  63. for (Field f : Tessellator.class.getDeclaredFields()) {
  64. try {
  65. Class<?> type = f.getType();
  66. int modifiers = f.getModifiers();
  67. if (!Modifier.isStatic(modifiers) && type.isPrimitive() && !f.getName().equals("rawBufferSize")) {
  68. f.setAccessible(true);
  69. if (type == Integer.TYPE && MAGIC_VALUE.equals(f.get(tessellator))) {
  70. continue;
  71. }
  72. logger.finest("copy %s %s %s", Modifier.toString(f.getModifiers()), f.getType().toString(), f.getName());
  73. fields.add(f);
  74. }
  75. } catch (Throwable e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. if (!haveBufferSize) {
  80. tessellator.bufferSize = saveBufferSize;
  81. }
  82. tessellator.vertexCount = saveVertexCount;
  83. tessellator.addedVertices = saveAddedVertices;
  84. tessellator.rawBufferIndex = saveRawBufferIndex;
  85. return fields.toArray(new Field[fields.size()]);
  86. }
  87. private static void copyFields(Tessellator a, Tessellator b, boolean isNew) {
  88. if (fieldsToCopy == null) {
  89. fieldsToCopy = getFieldsToCopy(a);
  90. }
  91. for (Field field : fieldsToCopy) {
  92. try {
  93. Object value = field.get(a);
  94. if (isNew) {
  95. logger.finest("copy %s %s %s = %s", Modifier.toString(field.getModifiers()), field.getType(), field.getName(), value);
  96. }
  97. field.set(b, value);
  98. } catch (IllegalAccessException e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. if (a.isDrawing && !b.isDrawing) {
  103. b.startDrawing(a.drawMode);
  104. } else if (!a.isDrawing && b.isDrawing) {
  105. b.reset();
  106. }
  107. }
  108. static void clear(Tessellator tessellator) {
  109. for (Tessellator child : tessellator.children.values()) {
  110. clear(child);
  111. }
  112. tessellator.children.clear();
  113. textureMapNames.clear();
  114. iconMap.clear();
  115. }
  116. public static void resetChildren(Tessellator tessellator) {
  117. for (Tessellator child : tessellator.children.values()) {
  118. child.reset();
  119. }
  120. }
  121. public static int drawChildren(int sum, Tessellator tessellator) {
  122. for (Tessellator child : tessellator.children.values()) {
  123. sum += child.draw();
  124. }
  125. return sum;
  126. }
  127. public static void startDrawingChildren(Tessellator tessellator, int drawMode) {
  128. for (Tessellator child : tessellator.children.values()) {
  129. child.startDrawing(drawMode);
  130. }
  131. }
  132. private static String toString(Tessellator tessellator) {
  133. if (tessellator == null) {
  134. return "Tessellator{null}";
  135. }
  136. String desc = tessellator.toString();
  137. TextureAtlas textureMap = tessellator.textureMap;
  138. if (textureMap != null) {
  139. String mapName = textureMapNames.get(textureMap);
  140. if (mapName == null) {
  141. desc = textureMap.toString();
  142. } else {
  143. desc = mapName;
  144. }
  145. }
  146. return String.format("Tessellator{%s, isDrawing=%s, %d children}", desc, tessellator.isDrawing, tessellator.children.size());
  147. }
  148. }