part 1 of ability to load books from zip files

This commit is contained in:
Progwml6 committed 2014-03-10 18:23:12 -04:00
1 parent f3f6ea2251
commit fddefc1e60
7 files changed
+197 -10

No files matched your search

+1
View File
@@ -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 ()
{
+11 -1
View File
@@ -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<String, BookData> data = HashBiMap.create();
private static HashBiMap<Integer, String> intMap = HashBiMap.create();
private static List<String> 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<String> getZipBooks(){
return zipBooks;
}
}
+18 -4
View File
@@ -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;
}
}
}
+17
View File
@@ -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;
}
}
+133
View File
@@ -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;
}
}
+10 -3
View File
@@ -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
@@ -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<String, ItemStack> manualIcons = new HashMap<String, ItemStack>();
public static Map<String, ItemStack[]> recipeIcons = new HashMap<String, ItemStack[]>();
public static Map<String, ItemStack> manualIcons = Maps.newHashMap();
public static Map<String, ItemStack[]> recipeIcons = Maps.newHashMap();
public static HashMap<String, BookImage> imageCache = Maps.newHashMap();
public static ItemStack defaultStack = new ItemStack(Items.iron_ingot);
public static void registerManualIcon (String name, ItemStack stack)