PageRenderTime 29ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

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