Added short overflow check for schematic saving.

This commit is contained in:
sk89q 2010-10-04 17:00:54 -07:00
parent 713f23d0de
commit 414c3cf33d
3 changed files with 25 additions and 12 deletions

View File

@ -129,12 +129,23 @@ public class RegionClipboard {
* *
* @param path * @param path
* @throws IOException * @throws IOException
* @throws SchematicException
*/ */
public void saveSchematic(String path) throws IOException { public void saveSchematic(String path) throws IOException, SchematicException {
int xs = getWidth(); int xs = getWidth();
int ys = getHeight(); int ys = getHeight();
int zs = getLength(); int zs = getLength();
if (xs > 65535) {
throw new SchematicException("Width of region too large for a .schematic");
}
if (ys > 65535) {
throw new SchematicException("Height of region too large for a .schematic");
}
if (zs > 65535) {
throw new SchematicException("Length of region too large for a .schematic");
}
HashMap<String,Tag> schematic = new HashMap<String,Tag>(); HashMap<String,Tag> schematic = new HashMap<String,Tag>();
schematic.put("Width", new ShortTag("Width", (short)xs)); schematic.put("Width", new ShortTag("Width", (short)xs));
schematic.put("Length", new ShortTag("Length", (short)zs)); schematic.put("Length", new ShortTag("Length", (short)zs));
@ -174,27 +185,27 @@ public class RegionClipboard {
* @param path * @param path
* @param origin * @param origin
* @return * @return
* @throws SchematicLoadException * @throws SchematicException
* @throws IOException * @throws IOException
*/ */
public static RegionClipboard loadSchematic(String path, Point<Integer> origin) public static RegionClipboard loadSchematic(String path, Point<Integer> origin)
throws SchematicLoadException, IOException { throws SchematicException, IOException {
FileInputStream stream = new FileInputStream(path); FileInputStream stream = new FileInputStream(path);
NBTInputStream nbtStream = new NBTInputStream(stream); NBTInputStream nbtStream = new NBTInputStream(stream);
CompoundTag schematicTag = (CompoundTag)nbtStream.readTag(); CompoundTag schematicTag = (CompoundTag)nbtStream.readTag();
if (!schematicTag.getName().equals("Schematic")) { if (!schematicTag.getName().equals("Schematic")) {
throw new SchematicLoadException("Tag \"Schematic\" does not exist or is not first"); throw new SchematicException("Tag \"Schematic\" does not exist or is not first");
} }
Map<String,Tag> schematic = schematicTag.getValue(); Map<String,Tag> schematic = schematicTag.getValue();
if (!schematic.containsKey("Blocks")) { if (!schematic.containsKey("Blocks")) {
throw new SchematicLoadException("Schematic file is missing a \"Blocks\" tag"); throw new SchematicException("Schematic file is missing a \"Blocks\" tag");
} }
short xs = (Short)getChildTag(schematic, "Width", ShortTag.class).getValue(); short xs = (Short)getChildTag(schematic, "Width", ShortTag.class).getValue();
short zs = (Short)getChildTag(schematic, "Length", ShortTag.class).getValue(); short zs = (Short)getChildTag(schematic, "Length", ShortTag.class).getValue();
short ys = (Short)getChildTag(schematic, "Height", ShortTag.class).getValue(); short ys = (Short)getChildTag(schematic, "Height", ShortTag.class).getValue();
String materials = (String)getChildTag(schematic, "Materials", StringTag.class).getValue(); String materials = (String)getChildTag(schematic, "Materials", StringTag.class).getValue();
if (!materials.equals("Alpha")) { if (!materials.equals("Alpha")) {
throw new SchematicLoadException("Schematic file is not an Alpha schematic"); throw new SchematicException("Schematic file is not an Alpha schematic");
} }
byte[] blocks = (byte[])getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue(); byte[] blocks = (byte[])getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue();
@ -223,13 +234,13 @@ public class RegionClipboard {
} }
private static Tag getChildTag(Map<String,Tag> items, String key, Class expected) private static Tag getChildTag(Map<String,Tag> items, String key, Class expected)
throws SchematicLoadException { throws SchematicException {
if (!items.containsKey(key)) { if (!items.containsKey(key)) {
throw new SchematicLoadException("Schematic file is missing a \"" + key + "\" tag"); throw new SchematicException("Schematic file is missing a \"" + key + "\" tag");
} }
Tag tag = items.get(key); Tag tag = items.get(key);
if (!expected.isInstance(tag)) { if (!expected.isInstance(tag)) {
throw new SchematicLoadException( throw new SchematicException(
key + " tag is not of tag type " + expected.getName()); key + " tag is not of tag type " + expected.getName());
} }
return tag; return tag;

View File

@ -467,7 +467,7 @@ public class WorldEdit extends Plugin {
logger.log(Level.INFO, player.getName() + " loaded " + filePath); logger.log(Level.INFO, player.getName() + " loaded " + filePath);
player.sendMessage(Colors.LightPurple + filename + " loaded."); player.sendMessage(Colors.LightPurple + filename + " loaded.");
} }
} catch (SchematicLoadException e) { } catch (SchematicException e) {
player.sendMessage(Colors.Rose + "Load error: " + e.getMessage()); player.sendMessage(Colors.Rose + "Load error: " + e.getMessage());
} catch (IOException e) { } catch (IOException e) {
player.sendMessage(Colors.Rose + "Schematic could not read or it does not exist."); player.sendMessage(Colors.Rose + "Schematic could not read or it does not exist.");
@ -505,6 +505,8 @@ public class WorldEdit extends Plugin {
logger.log(Level.INFO, player.getName() + " saved " + filePath); logger.log(Level.INFO, player.getName() + " saved " + filePath);
player.sendMessage(Colors.LightPurple + filename + " saved."); player.sendMessage(Colors.LightPurple + filename + " saved.");
} }
} catch (SchematicException se) {
player.sendMessage(Colors.Rose + "Save error: " + se.getMessage());
} catch (IOException e) { } catch (IOException e) {
player.sendMessage(Colors.Rose + "Schematic could not written."); player.sendMessage(Colors.Rose + "Schematic could not written.");
} }

View File

@ -23,8 +23,8 @@ package com.sk89q.worldedit;
* *
* @author Albert * @author Albert
*/ */
public class SchematicLoadException extends WorldEditException { public class SchematicException extends WorldEditException {
public SchematicLoadException(String error) { public SchematicException(String error) {
super(error); super(error);
} }
} }