Package more of default data/config in JAR, lay down when needed, to avoid stomping customized configuration when unneeded

This commit is contained in:
Mike Primm 2011-07-23 15:29:47 -05:00
parent 357e46280c
commit ddeded3e9c
25 changed files with 178 additions and 118 deletions

View file

@ -1,10 +1,13 @@
package org.dynmap.hdmap;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.HashMap;
@ -263,10 +266,24 @@ public class HDBlockModels {
*/
public static void loadModels(File datadir) {
/* Load block models */
loadModelFile(new File(datadir, "renderdata/models.txt"));
InputStream in = TexturePack.class.getResourceAsStream("/models.txt");
if(in != null) {
loadModelFile(in, "models.txt");
try { in.close(); } catch (IOException iox) {} in = null;
}
File custom = new File(datadir, "renderdata/custom-models.txt");
if(custom.canRead()) {
loadModelFile(custom);
try {
in = new FileInputStream(custom);
loadModelFile(in, custom.getPath());
} catch (IOException iox) {
Log.severe("Error loading " + custom.getPath());
} finally {
if(in != null) {
try { in.close(); } catch (IOException iox) {}
in = null;
}
}
}
else {
try {
@ -280,7 +297,7 @@ public class HDBlockModels {
/**
* Load models from file
*/
private static void loadModelFile(File modelfile) {
private static void loadModelFile(InputStream in, String fname) {
LineNumberReader rdr = null;
int cnt = 0;
try {
@ -289,7 +306,7 @@ public class HDBlockModels {
int layerbits = 0;
int rownum = 0;
int scale = 0;
rdr = new LineNumberReader(new FileReader(modelfile));
rdr = new LineNumberReader(new InputStreamReader(in));
while((line = rdr.readLine()) != null) {
if(line.startsWith("block:")) {
ArrayList<Integer> blkids = new ArrayList<Integer>();
@ -322,7 +339,7 @@ public class HDBlockModels {
}
}
else {
Log.severe("Block model missing required parameters = line " + rdr.getLineNumber() + " of " + modelfile.getPath());
Log.severe("Block model missing required parameters = line " + rdr.getLineNumber() + " of " + fname);
}
layerbits = 0;
}
@ -401,11 +418,11 @@ public class HDBlockModels {
}
}
}
Log.verboseinfo("Loaded " + cnt + " block models from " + modelfile.getPath());
Log.verboseinfo("Loaded " + cnt + " block models from " + fname);
} catch (IOException iox) {
Log.severe("Error reading models.txt - " + iox.toString());
} catch (NumberFormatException nfx) {
Log.severe("Format error - line " + rdr.getLineNumber() + " of " + modelfile.getPath());
Log.severe("Format error - line " + rdr.getLineNumber() + " of " + fname);
} finally {
if(rdr != null) {
try {

View file

@ -13,6 +13,7 @@ import org.bukkit.World;
import org.bukkit.plugin.Plugin;
import org.dynmap.ConfigurationNode;
import org.dynmap.DynmapChunk;
import org.dynmap.DynmapPlugin;
import org.dynmap.DynmapWorld;
import org.dynmap.Log;
import org.dynmap.MapManager;
@ -28,10 +29,14 @@ public class HDMapManager {
public HashSet<HDMap> maps = new HashSet<HDMap>();
public HashMap<String, ArrayList<HDMap>> maps_by_world_perspective = new HashMap<String, ArrayList<HDMap>>();
public void loadHDShaders(Plugin plugin) {
public void loadHDShaders(DynmapPlugin plugin) {
Log.verboseinfo("Loading shaders...");
org.bukkit.util.config.Configuration bukkitShaderConfig = new org.bukkit.util.config.Configuration(new File(plugin.getDataFolder(), "shaders.txt"));
File f = new File(plugin.getDataFolder(), "shaders.txt");
if(!plugin.createDefaultFileFromResource("/shaders.txt", f)) {
return;
}
org.bukkit.util.config.Configuration bukkitShaderConfig = new org.bukkit.util.config.Configuration(f);
bukkitShaderConfig.load();
ConfigurationNode shadercfg = new ConfigurationNode(bukkitShaderConfig);
@ -40,7 +45,8 @@ public class HDMapManager {
shaders.put(shader.getName(), shader);
}
/* Load custom shaders, if file is defined - or create empty one if not */
File f = new File(plugin.getDataFolder(), "custom-shaders.txt");
f = new File(plugin.getDataFolder(), "custom-shaders.txt");
plugin.createDefaultFileFromResource("/custom-shaders.txt", f);
if(f.exists()) {
bukkitShaderConfig = new org.bukkit.util.config.Configuration(f);
bukkitShaderConfig.load();
@ -50,22 +56,16 @@ public class HDMapManager {
shaders.put(shader.getName(), shader);
}
}
else {
try {
FileWriter fw = new FileWriter(f);
fw.write("# The user is free to add new and custom shaders here, including replacements for standard ones\n");
fw.write("# Dynmap's install will not overwrite it\n");
fw.write("shaders:\n");
fw.close();
} catch (IOException iox) {
}
}
Log.info("Loaded " + shaders.size() + " shaders.");
}
public void loadHDPerspectives(Plugin plugin) {
public void loadHDPerspectives(DynmapPlugin plugin) {
Log.verboseinfo("Loading perspectives...");
org.bukkit.util.config.Configuration bukkitPerspectiveConfig = new org.bukkit.util.config.Configuration(new File(plugin.getDataFolder(), "perspectives.txt"));
File f = new File(plugin.getDataFolder(), "perspectives.txt");
if(!plugin.createDefaultFileFromResource("/perspectives.txt", f)) {
return;
}
org.bukkit.util.config.Configuration bukkitPerspectiveConfig = new org.bukkit.util.config.Configuration(f);
bukkitPerspectiveConfig.load();
ConfigurationNode perspectivecfg = new ConfigurationNode(bukkitPerspectiveConfig);
for(HDPerspective perspective : perspectivecfg.<HDPerspective>createInstances("perspectives", new Class<?>[0], new Object[0])) {
@ -73,7 +73,8 @@ public class HDMapManager {
perspectives.put(perspective.getName(), perspective);
}
/* Load custom perspectives, if file is defined - or create empty one if not */
File f = new File(plugin.getDataFolder(), "custom-perspectives.txt");
f = new File(plugin.getDataFolder(), "custom-perspectives.txt");
plugin.createDefaultFileFromResource("/custom-perspectives.txt", f);
if(f.exists()) {
bukkitPerspectiveConfig = new org.bukkit.util.config.Configuration(f);
bukkitPerspectiveConfig.load();
@ -83,22 +84,16 @@ public class HDMapManager {
perspectives.put(perspective.getName(), perspective);
}
}
else {
try {
FileWriter fw = new FileWriter(f);
fw.write("# The user is free to add new and custom perspectives here, including replacements for standard ones\n");
fw.write("# Dynmap's install will not overwrite it\n");
fw.write("perspectives:\n");
fw.close();
} catch (IOException iox) {
}
}
Log.info("Loaded " + perspectives.size() + " perspectives.");
}
public void loadHDLightings(Plugin plugin) {
public void loadHDLightings(DynmapPlugin plugin) {
Log.verboseinfo("Loading lightings...");
org.bukkit.util.config.Configuration bukkitLightingsConfig = new org.bukkit.util.config.Configuration(new File(plugin.getDataFolder(), "lightings.txt"));
File f = new File(plugin.getDataFolder(), "lightings.txt");
if(!plugin.createDefaultFileFromResource("/lightings.txt", f)) {
return;
}
org.bukkit.util.config.Configuration bukkitLightingsConfig = new org.bukkit.util.config.Configuration(f);
bukkitLightingsConfig.load();
ConfigurationNode lightingcfg = new ConfigurationNode(bukkitLightingsConfig);
@ -107,7 +102,8 @@ public class HDMapManager {
lightings.put(lighting.getName(), lighting);
}
/* Load custom lightings, if file is defined - or create empty one if not */
File f = new File(plugin.getDataFolder(), "custom-lightings.txt");
f = new File(plugin.getDataFolder(), "custom-lightings.txt");
plugin.createDefaultFileFromResource("/custom-lightings.txt", f);
if(f.exists()) {
bukkitLightingsConfig = new org.bukkit.util.config.Configuration(f);
bukkitLightingsConfig.load();
@ -117,16 +113,6 @@ public class HDMapManager {
lightings.put(lighting.getName(), lighting);
}
}
else {
try {
FileWriter fw = new FileWriter(f);
fw.write("# The user is free to add new and custom lightings here, including replacements for standard ones\n");
fw.write("# Dynmap's install will not overwrite it\n");
fw.write("lightings:\n");
fw.close();
} catch (IOException iox) {
}
}
Log.info("Loaded " + lightings.size() + " lightings.");
}

View file

@ -8,6 +8,7 @@ import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Collections;
@ -557,10 +558,24 @@ public class TexturePack {
/* Initialize map with blank map for all entries */
HDTextureMap.initializeTable();
/* Load block models */
loadTextureFile(new File(datadir, "renderdata/texture.txt"));
InputStream in = TexturePack.class.getResourceAsStream("/texture.txt");
if(in != null) {
loadTextureFile(in, "texture.txt");
if(in != null) { try { in.close(); } catch (IOException x) {} in = null; }
}
else
Log.severe("Error loading texture.txt");
File custom = new File(datadir, "renderdata/custom-texture.txt");
if(custom.canRead()) {
loadTextureFile(custom);
try {
in = new FileInputStream(custom);
loadTextureFile(in, custom.getPath());
} catch (IOException iox) {
Log.severe("Error loading renderdata/custom-texture.txt - " + iox);
} finally {
if(in != null) { try { in.close(); } catch (IOException x) {} in = null; }
}
}
else {
try {
@ -575,13 +590,13 @@ public class TexturePack {
/**
* Load texture pack mappings from texture.txt file
*/
private static void loadTextureFile(File txtfile) {
private static void loadTextureFile(InputStream txtfile, String txtname) {
LineNumberReader rdr = null;
int cnt = 0;
try {
String line;
rdr = new LineNumberReader(new FileReader(txtfile));
rdr = new LineNumberReader(new InputStreamReader(txtfile));
while((line = rdr.readLine()) != null) {
if(line.startsWith("block:")) {
ArrayList<Integer> blkids = new ArrayList<Integer>();
@ -642,7 +657,7 @@ public class TexturePack {
trans = BlockTransparency.valueOf(av[1]);
if(trans == null) {
trans = BlockTransparency.OPAQUE;
Log.severe("Texture mapping has invalid transparency setting - " + av[1] + " - line " + rdr.getLineNumber() + " of " + txtfile.getPath());
Log.severe("Texture mapping has invalid transparency setting - " + av[1] + " - line " + rdr.getLineNumber() + " of " + txtname);
}
}
}
@ -655,17 +670,17 @@ public class TexturePack {
cnt++;
}
else {
Log.severe("Texture mapping missing required parameters = line " + rdr.getLineNumber() + " of " + txtfile.getPath());
Log.severe("Texture mapping missing required parameters = line " + rdr.getLineNumber() + " of " + txtname);
}
}
else if(line.startsWith("#") || line.startsWith(";")) {
}
}
Log.verboseinfo("Loaded " + cnt + " texture mappings from " + txtfile.getPath());
Log.verboseinfo("Loaded " + cnt + " texture mappings from " + txtname);
} catch (IOException iox) {
Log.severe("Error reading " + txtfile.getPath() + " - " + iox.toString());
Log.severe("Error reading " + txtname + " - " + iox.toString());
} catch (NumberFormatException nfx) {
Log.severe("Format error - line " + rdr.getLineNumber() + " of " + txtfile.getPath());
Log.severe("Format error - line " + rdr.getLineNumber() + " of " + txtname);
} finally {
if(rdr != null) {
try {