diff --git a/src/mantle/books/BookData.java b/src/mantle/books/BookData.java index ba1e850..bd7102f 100644 --- a/src/mantle/books/BookData.java +++ b/src/mantle/books/BookData.java @@ -17,6 +17,7 @@ public class BookData //font can be left null if so, the default from mantle will be used public SmallFontRenderer font; public Boolean isTranslatable = false; + public boolean isFromZip = false; public Document getDoc () { diff --git a/src/mantle/books/BookDataStore.java b/src/mantle/books/BookDataStore.java index bbf8f89..de7f13f 100644 --- a/src/mantle/books/BookDataStore.java +++ b/src/mantle/books/BookDataStore.java @@ -1,17 +1,23 @@ package mantle.books; +import java.util.List; + import com.google.common.collect.HashBiMap; +import com.google.common.collect.Lists; public class BookDataStore { private static HashBiMap data = HashBiMap.create(); private static HashBiMap intMap = HashBiMap.create(); - + private static List zipBooks= Lists.newArrayList(); public static void addBook (BookData bd) { int insertOrder = data.size(); data.put(bd.getFullUnlocalizedName(), bd); intMap.put(insertOrder, bd.getFullUnlocalizedName()); + if(bd.isFromZip) + zipBooks.add(bd.getFullUnlocalizedName()); + } public static BookData getBookfromName (String ModID, String unlocalizedName) @@ -38,4 +44,8 @@ public class BookDataStore { return getIDFromName(data.inverse().get(b)); } + + public static List getZipBooks(){ + return zipBooks; + } } diff --git a/src/mantle/books/ManualReader.java b/src/mantle/books/ManualReader.java index d845e8a..5e9a204 100644 --- a/src/mantle/books/ManualReader.java +++ b/src/mantle/books/ManualReader.java @@ -21,10 +21,7 @@ public class ManualReader { logger.info("Loading Manual XML from: " + location); InputStream stream = Mantle.class.getResourceAsStream(location); - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); - Document doc = dBuilder.parse(stream); - doc.getDocumentElement().normalize(); - return doc; + return readManual(stream, location); } catch (Exception e) { @@ -32,5 +29,22 @@ public class ManualReader return null; } } + public static Document readManual (InputStream is, String filenameOrLocation) + { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + try + { + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(is); + doc.getDocumentElement().normalize(); + return doc; + } + catch (Exception e) + { + logger.error("Failed to Load Manual XML from: " + filenameOrLocation); + e.printStackTrace(); + return null; + } + } } diff --git a/src/mantle/books/client/BookImage.java b/src/mantle/books/client/BookImage.java new file mode 100644 index 0000000..8482568 --- /dev/null +++ b/src/mantle/books/client/BookImage.java @@ -0,0 +1,17 @@ +package mantle.books.client; + +import net.minecraft.client.renderer.texture.DynamicTexture; +import net.minecraft.util.ResourceLocation; + +public class BookImage { + public int height, width; + public DynamicTexture texture; + public ResourceLocation resource; + + public BookImage(int width, int height, DynamicTexture texture, ResourceLocation resource) { + this.width = width; + this.height = height; + this.texture = texture; + this.resource = resource; + } +} diff --git a/src/mantle/books/external/ZipLoader.java b/src/mantle/books/external/ZipLoader.java new file mode 100644 index 0000000..b2348cf --- /dev/null +++ b/src/mantle/books/external/ZipLoader.java @@ -0,0 +1,133 @@ +package mantle.books.external; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import javax.imageio.ImageIO; + +import static mantle.lib.CoreRepo.logger; +import mantle.books.BookData; +import mantle.books.BookDataStore; +import mantle.books.ManualReader; +import mantle.books.client.BookImage; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.texture.DynamicTexture; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.StringUtils; + +import org.apache.commons.io.FilenameUtils; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; + +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.relauncher.Side; + +public class ZipLoader +{ + public BookData loadZip (File f) + { + String flName = new String(); + String flExt = new String(); + String lImg = new String(); + String rImg = new String(); + String bIcon = new String(); + String unlocName = new String(); + String toolTip = new String(); + Boolean translatable = false; + BookData b = new BookData(); + boolean isClient = FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT; + if (f.exists()) + { + if (FilenameUtils.getExtension(f.getPath()).equalsIgnoreCase("zip")) + { + try + { + ZipFile zipfile = new ZipFile(f); + Enumeration e = zipfile.entries(); + while (e.hasMoreElements()) + { + ZipEntry entry = (ZipEntry) e.nextElement(); + flName = entry.getName(); + flExt = FilenameUtils.getExtension(flName); + if (!entry.isDirectory()) + { + if (isClient && isImage(flExt)) + { + String id = FilenameUtils.getBaseName(flName); + BufferedImage img = ImageIO.read(zipfile.getInputStream(entry)); + DynamicTexture dynTexture = new DynamicTexture(img); + ResourceLocation rsLoc = Minecraft.getMinecraft().getTextureManager().getDynamicTextureLocation(id, dynTexture); + BookImage bI = new BookImage(img.getWidth(), img.getHeight(), dynTexture, rsLoc); + + } + if (flExt.equalsIgnoreCase("xml")) + { + b.doc = ManualReader.readManual(zipfile.getInputStream(entry), flName); + } + if (flExt.equalsIgnoreCase("bookconfig")) + { + Document config = ManualReader.readManual(zipfile.getInputStream(entry), flName); + NodeList nodes = config.getElementsByTagName("UnlocalizedName"); + if (nodes != null) + unlocName = nodes.item(0).getTextContent(); + nodes = config.getElementsByTagName("Tooltip"); + if (nodes != null) + toolTip = nodes.item(0).getTextContent(); + nodes = config.getElementsByTagName("Translatable"); + if (nodes != null) + translatable = StringUtils.isNullOrEmpty(nodes.item(0).getTextContent()) ? false : nodes.item(0).getTextContent().equalsIgnoreCase("true") ? true : false; + //if these 3 are null or empty or invalid then the book will use the mantle defaults + nodes = config.getElementsByTagName("BookIcon"); + if (nodes != null) + bIcon = nodes.item(0).getTextContent(); + nodes = config.getElementsByTagName("LeftImage"); + if (nodes != null) + lImg = nodes.item(0).getTextContent(); + nodes = config.getElementsByTagName("RightImage"); + if (nodes != null) + lImg = nodes.item(0).getTextContent(); + //TODO 1.7.2 add in ability to register stuff w/ client registry via this file + } + + } + + } + zipfile.close(); + b.unlocalizedName = unlocName; + b.toolTip = toolTip; + b.isTranslatable = translatable; + b.leftImage = new ResourceLocation("mantle", "textures/gui/bookleft.png"); + b.rightImage = new ResourceLocation("mantle", "textures/gui/bookright.png"); + b.itemImage = new ResourceLocation("mantle", "textures/items/mantlebook_blue.png"); + b.isFromZip = true; + BookDataStore.addBook(b); + } + catch (Exception e) + { + } + return null; + + } + else + { + logger.error("Attempted to load non-zip file for mantle book. File will be skipped"); + } + } + else + { + logger.error("Attempted to load non-existant file for mantle book. File will be skipped"); + + } + return null; + } + + public boolean isImage (String ext) + { + if (ext.equalsIgnoreCase("png")) + return true; + return false; + } +} diff --git a/src/mantle/client/pages/PicturePage.java b/src/mantle/client/pages/PicturePage.java index 8c6bbef..a5ade10 100644 --- a/src/mantle/client/pages/PicturePage.java +++ b/src/mantle/client/pages/PicturePage.java @@ -1,5 +1,6 @@ package mantle.client.pages; +import mantle.lib.client.MantleClientRegistry; import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; @@ -17,12 +18,18 @@ public class PicturePage extends BookPage NodeList nodes = element.getElementsByTagName("text"); if (nodes != null) text = nodes.item(0).getTextContent(); - + nodes = element.getElementsByTagName("bookImage"); + if (nodes != null) + { + //TODO 1.7 null check all of this + background = MantleClientRegistry.imageCache.get(nodes.item(0).getTextContent()).resource; + } nodes = element.getElementsByTagName("location"); if (nodes != null) + { location = nodes.item(0).getTextContent(); - - background = new ResourceLocation(location); + background = new ResourceLocation(location); + } } @Override diff --git a/src/mantle/lib/client/MantleClientRegistry.java b/src/mantle/lib/client/MantleClientRegistry.java index 2c358b0..a90089a 100644 --- a/src/mantle/lib/client/MantleClientRegistry.java +++ b/src/mantle/lib/client/MantleClientRegistry.java @@ -3,13 +3,18 @@ package mantle.lib.client; import java.util.HashMap; import java.util.Map; +import com.google.common.collect.Maps; + +import mantle.books.client.BookImage; import net.minecraft.init.Items; import net.minecraft.item.ItemStack; public class MantleClientRegistry { - public static Map manualIcons = new HashMap(); - public static Map recipeIcons = new HashMap(); + public static Map manualIcons = Maps.newHashMap(); + public static Map recipeIcons = Maps.newHashMap(); + public static HashMap imageCache = Maps.newHashMap(); + public static ItemStack defaultStack = new ItemStack(Items.iron_ingot); public static void registerManualIcon (String name, ItemStack stack)