package ring.compiler; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.util.HashSet; import java.util.Properties; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import ring.util.UserUtilities; /** * Object representation of a .mud file. Allows easy adding and removal of * entries, as well as writing it out to a file on the filesystem. * @author projectmoon * */ public class MUDFile { private Set<FileEntry> entries = new HashSet<FileEntry>(); //List of properties for this mud. The defaults are: //name: the name of the mud. //version: the version of the mud. //author: the author of the mud. private Properties mudProperties = new Properties(); //Used for detecting if a new deployment is necessary. private String mudHash = ""; public MUDFile() {} public MUDFile(Properties mudProperties) { this.mudProperties = mudProperties; } public String getName() { return mudProperties.getProperty("name", ""); } public void setName(String name) { mudProperties.setProperty("name", name); } public String getVersion() { return mudProperties.getProperty("version", ""); } public void setVersion(String version) { mudProperties.setProperty("version", version); } public String getAuthor() { return mudProperties.getProperty("author", ""); } public void setAuthor(String author) { mudProperties.setProperty("author", author); } public String getHash() { return mudHash; } public void setProperties(Properties props) { mudProperties = props; } public void addEntry(FileEntry entry) { entries.add(entry); } public boolean removeEntry(FileEntry entry) { return entries.remove(entry); } public Set<FileEntry> getEntries() { return entries; } public Set<FileEntry> getEntries(String prefix) { Set<FileEntry> results = new HashSet<FileEntry>(); for (FileEntry entry : getEntries()) { if (entry.getEntryName().startsWith(prefix)) { results.add(entry); } } return results; } public void clear() { entries.clear(); } private Properties getProperties() { return mudProperties; } private String createComments() { return "Generated by RMC 0.1"; } private void generateHash() { String textToHash = getName() + getAuthor() + getVersion(); for (FileEntry entry : getEntries()) { textToHash += entry.toString(); } mudHash = UserUtilities.sha1Hash(textToHash); } public void writeTo(OutputStream stream) throws IOException { ZipOutputStream out = new ZipOutputStream(stream); BufferedInputStream fileInput = null; //buffer for reading and writing stuff to the zip file. byte[] buf = new byte[2048]; //First, write the MUD's properties to the zip file. generateHash(); Properties props = getProperties(); props.setProperty("hash", mudHash); ZipEntry propsEntry = new ZipEntry("mud/mud.properties"); out.putNextEntry(propsEntry); props.store(out, createComments()); //Now, write all of its entries. for (FileEntry entry : getEntries()) { File file = entry.getFile(); //Sets up the entry in the .mud file. ZipEntry zipEntry = new ZipEntry("mud/" + entry.getEntryName()); out.putNextEntry(zipEntry); //Write the data of the file to this zip. fileInput = new BufferedInputStream(new FileInputStream(file), buf.length); int bytesRead = 0; while ((bytesRead = fileInput.read(buf, 0, buf.length)) != - 1) { out.write(buf, 0, bytesRead); } fileInput.close(); } out.close(); stream.close(); } public static MUDFile read(String path) { return read(new File(path)); } /** * Reads in a MUDFile from its file form. Note that no data is loaded but the entries that reside in * the MUDFile as a result of this operation. * @param file * @return */ public static MUDFile read(File file) { return null; } }