PageRenderTime 36ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/SevenBits/mcpatcher
Java | 278 lines | 239 code | 28 blank | 11 comment | 66 complexity | cc9928bf9f57d02a0ae8c4c74d798cbe MD5 | raw file
  1. package com.prupe.mcpatcher;
  2. import com.prupe.mcpatcher.mal.resource.ResourceLocationWithSource;
  3. import net.minecraft.client.Minecraft;
  4. import net.minecraft.src.*;
  5. import org.lwjgl.opengl.GL11;
  6. import javax.imageio.ImageIO;
  7. import java.awt.image.BufferedImage;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.*;
  11. import java.util.regex.Pattern;
  12. public class TexturePackAPI {
  13. private static final MCLogger logger = MCLogger.getLogger("Texture Pack");
  14. public static final String DEFAULT_NAMESPACE = "minecraft";
  15. public static final String MCPATCHER_SUBDIR = "mcpatcher/";
  16. public static TexturePackAPI instance = new TexturePackAPI();
  17. public static List<ResourcePack> getResourcePacks(String namespace) {
  18. List<ResourcePack> list = new ArrayList<ResourcePack>();
  19. ResourceManager resourceManager = getResourceManager();
  20. if (resourceManager instanceof SimpleReloadableResourceManager) {
  21. for (Map.Entry<String, FallbackResourceManager> entry : ((SimpleReloadableResourceManager) resourceManager).namespaceMap.entrySet()) {
  22. if (namespace == null || namespace.equals(entry.getKey())) {
  23. List<ResourcePack> packs = entry.getValue().resourcePacks;
  24. if (packs != null) {
  25. list.removeAll(packs);
  26. list.addAll(packs);
  27. }
  28. }
  29. }
  30. }
  31. return list;
  32. }
  33. public static Set<String> getNamespaces() {
  34. Set<String> set = new HashSet<String>();
  35. ResourceManager resourceManager = getResourceManager();
  36. if (resourceManager instanceof SimpleReloadableResourceManager) {
  37. set.addAll(((SimpleReloadableResourceManager) resourceManager).namespaceMap.keySet());
  38. }
  39. return set;
  40. }
  41. public static ResourceManager getResourceManager() {
  42. return Minecraft.getInstance().getResourceManager();
  43. }
  44. public static boolean isDefaultTexturePack() {
  45. return getResourcePacks(DEFAULT_NAMESPACE).size() <= 1;
  46. }
  47. public static InputStream getInputStream(ResourceLocation resource) {
  48. return resource == null ? null : instance.getInputStreamImpl(resource);
  49. }
  50. public static boolean hasResource(ResourceLocation resource) {
  51. if (resource == null) {
  52. return false;
  53. } else if (resource.getPath().endsWith(".png")) {
  54. return getImage(resource) != null;
  55. } else if (resource.getPath().endsWith(".properties")) {
  56. return getProperties(resource) != null;
  57. } else {
  58. InputStream is = getInputStream(resource);
  59. MCPatcherUtils.close(is);
  60. return is != null;
  61. }
  62. }
  63. public static boolean hasCustomResource(ResourceLocation resource) {
  64. InputStream jar = null;
  65. InputStream pack = null;
  66. try {
  67. String path = "assets/" + resource.getNamespace() + "/" + resource.getPath();
  68. pack = getInputStream(resource);
  69. if (pack == null) {
  70. return false;
  71. }
  72. jar = Minecraft.class.getResourceAsStream(path);
  73. if (jar == null) {
  74. return true;
  75. }
  76. byte[] buffer1 = new byte[4096];
  77. byte[] buffer2 = new byte[4096];
  78. int read1;
  79. int read2;
  80. while ((read1 = pack.read(buffer1)) > 0) {
  81. read2 = jar.read(buffer2);
  82. if (read1 != read2) {
  83. return true;
  84. }
  85. for (int i = 0; i < read1; i++) {
  86. if (buffer1[i] != buffer2[i]) {
  87. return true;
  88. }
  89. }
  90. }
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. } finally {
  94. MCPatcherUtils.close(jar);
  95. MCPatcherUtils.close(pack);
  96. }
  97. return false;
  98. }
  99. public static BufferedImage getImage(ResourceLocation resource) {
  100. return resource == null ? null : instance.getImageImpl(resource);
  101. }
  102. public static Properties getProperties(ResourceLocation resource) {
  103. Properties properties = new Properties();
  104. if (getProperties(resource, properties)) {
  105. return properties;
  106. } else {
  107. return null;
  108. }
  109. }
  110. public static boolean getProperties(ResourceLocation resource, Properties properties) {
  111. return resource != null && instance.getPropertiesImpl(resource, properties);
  112. }
  113. public static ResourceLocation transformResourceLocation(ResourceLocation resource, String oldExt, String newExt) {
  114. return new ResourceLocation(resource.getNamespace(), resource.getPath().replaceFirst(Pattern.quote(oldExt) + "$", newExt));
  115. }
  116. public static ResourceLocation parseResourceLocation(ResourceLocation baseResource, String path) {
  117. if (path == null || path.equals("")) {
  118. return null;
  119. }
  120. boolean absolute = false;
  121. if (path.startsWith("%blur%")) {
  122. path = path.substring(6);
  123. }
  124. if (path.startsWith("%clamp%")) {
  125. path = path.substring(7);
  126. }
  127. if (path.startsWith("/")) {
  128. path = path.substring(1);
  129. absolute = true;
  130. }
  131. if (path.startsWith("assets/minecraft/")) {
  132. path = path.substring(17);
  133. absolute = true;
  134. }
  135. // Absolute path, including namespace:
  136. // namespace:path/filename -> assets/namespace/path/filename
  137. int colon = path.indexOf(':');
  138. if (colon >= 0) {
  139. return new ResourceLocation(path.substring(0, colon), path.substring(colon + 1));
  140. }
  141. ResourceLocation resource;
  142. if (path.startsWith("~/")) {
  143. // Relative to namespace mcpatcher dir:
  144. // ~/path -> assets/(namespace of base file)/mcpatcher/path
  145. resource = new ResourceLocation(baseResource.getNamespace(), MCPATCHER_SUBDIR + path.substring(2));
  146. } else if (path.startsWith("./")) {
  147. // Relative to properties file:
  148. // ./path -> (dir of base file)/path
  149. resource = new ResourceLocation(baseResource.getNamespace(), baseResource.getPath().replaceFirst("[^/]+$", "") + path.substring(2));
  150. } else if (!absolute && !path.contains("/")) {
  151. // Relative to properties file:
  152. // filename -> (dir of base file)/filename
  153. resource = new ResourceLocation(baseResource.getNamespace(), baseResource.getPath().replaceFirst("[^/]+$", "") + path);
  154. } else {
  155. // Absolute path, w/o namespace:
  156. // path/filename -> assets/(namespace of base file)/path/filename
  157. resource = new ResourceLocation(baseResource.getNamespace(), path);
  158. }
  159. if (baseResource instanceof ResourceLocationWithSource) {
  160. resource = new ResourceLocationWithSource(((ResourceLocationWithSource) baseResource).getSource(), resource);
  161. }
  162. return resource;
  163. }
  164. public static ResourceLocation newMCPatcherResourceLocation(String path) {
  165. return new ResourceLocation(MCPATCHER_SUBDIR + path);
  166. }
  167. public static int getTextureIfLoaded(ResourceLocation resource) {
  168. if (resource == null) {
  169. return -1;
  170. }
  171. TextureObject texture = Minecraft.getInstance().getTextureManager().getTexture(resource);
  172. return texture instanceof AbstractTexture ? ((AbstractTexture) texture).glTextureId : -1;
  173. }
  174. public static boolean isTextureLoaded(ResourceLocation resource) {
  175. return getTextureIfLoaded(resource) >= 0;
  176. }
  177. public static TextureObject getTextureObject(ResourceLocation resource) {
  178. return Minecraft.getInstance().getTextureManager().getTexture(resource);
  179. }
  180. public static void bindTexture(ResourceLocation resource) {
  181. Minecraft.getInstance().getTextureManager().bindTexture(resource);
  182. }
  183. public static void bindTexture(int texture) {
  184. if (texture >= 0) {
  185. GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
  186. }
  187. }
  188. public static void unloadTexture(ResourceLocation resource) {
  189. TextureManager textureManager = Minecraft.getInstance().getTextureManager();
  190. TextureObject texture = textureManager.getTexture(resource);
  191. if (texture != null && !(texture instanceof TextureAtlas) && !(texture instanceof DynamicTexture)) {
  192. if (texture instanceof AbstractTexture) {
  193. ((AbstractTexture) texture).unloadGLTexture();
  194. }
  195. logger.finer("unloading texture %s", resource);
  196. textureManager.texturesByName.remove(resource);
  197. }
  198. }
  199. public static void deleteTexture(int texture) {
  200. if (texture >= 0) {
  201. GL11.glDeleteTextures(texture);
  202. }
  203. }
  204. protected InputStream getInputStreamImpl(ResourceLocation resource) {
  205. if (resource instanceof ResourceLocationWithSource) {
  206. try {
  207. return ((ResourceLocationWithSource) resource).getSource().getInputStream(resource);
  208. } catch (IOException e) {
  209. // nothing
  210. }
  211. }
  212. try {
  213. return Minecraft.getInstance().getResourceManager().getResource(resource).getInputStream();
  214. } catch (IOException e) {
  215. return null;
  216. }
  217. }
  218. protected BufferedImage getImageImpl(ResourceLocation resource) {
  219. InputStream input = getInputStream(resource);
  220. BufferedImage image = null;
  221. if (input != null) {
  222. try {
  223. image = ImageIO.read(input);
  224. } catch (IOException e) {
  225. logger.error("could not read %s", resource);
  226. e.printStackTrace();
  227. } finally {
  228. MCPatcherUtils.close(input);
  229. }
  230. }
  231. return image;
  232. }
  233. protected boolean getPropertiesImpl(ResourceLocation resource, Properties properties) {
  234. if (properties != null) {
  235. InputStream input = getInputStream(resource);
  236. try {
  237. if (input != null) {
  238. properties.load(input);
  239. return true;
  240. }
  241. } catch (IOException e) {
  242. logger.error("could not read %s", resource);
  243. e.printStackTrace();
  244. } finally {
  245. MCPatcherUtils.close(input);
  246. }
  247. }
  248. return false;
  249. }
  250. }