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

@ -22,6 +22,15 @@
<jar jarfile="${dist}/${pluginname}.jar">
<fileset dir="${bin}"/>
<fileset file="${src}/main/resources/plugin.yml"/>
<fileset file="${src}/main/resources/configuration.txt"/>
<fileset file="${src}/main/resources/custom-lightings.txt"/>
<fileset file="${src}/main/resources/custom-perspectives.txt"/>
<fileset file="${src}/main/resources/custom-shaders.txt"/>
<fileset file="${src}/main/resources/lightings.txt"/>
<fileset file="${src}/main/resources/models.txt"/>
<fileset file="${src}/main/resources/perspectives.txt"/>
<fileset file="${src}/main/resources/texture.txt"/>
<fileset file="${src}/main/resources/worlds.txt"/>
<zipfileset file="${src}/main/resources/colors.txt"/>
</jar>
</target>

View File

@ -23,21 +23,9 @@
<directory>${project.basedir}/colorschemes</directory>
<outputDirectory>/dynmap/colorschemes</outputDirectory>
</fileSet>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/dynmap/</outputDirectory>
<includes>
<include>configuration.default</include>
<include>shaders.txt</include>
<include>perspectives.txt</include>
<include>lightings.txt</include>
<include>worlds.txt.sample</include></includes></fileSet>
<fileSet>
<directory>${project.basedir}/texturepacks</directory>
<outputDirectory>/dynmap/texturepacks</outputDirectory></fileSet>
<fileSet>
<directory>${project.basedir}/templates</directory>
<outputDirectory>/dynmap/templates</outputDirectory></fileSet>
<fileSet>
<directory>${project.basedir}/renderdata</directory>
<outputDirectory>/dynmap/renderdata</outputDirectory></fileSet>

View File

@ -6,6 +6,7 @@ import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.net.InetAddress;
import java.net.UnknownHostException;
@ -130,11 +131,22 @@ public class DynmapPlugin extends JavaPlugin {
}
}
}
/* Table of default templates - all are resources in dynmap.jar unnder templates/, and go in templates directory when needed */
private static final String[] stdtemplates = { "normal.txt", "nether.txt", "skylands.txt", "normal-lowres.txt",
"nether-lowres.txt", "skylands-lowres.txt", "normal-hires.txt", "nether-hires.txt", "skyands-hires.txt"
};
private static final String CUSTOM_PREFIX = "custom-";
/* Load templates from template folder */
private void loadTemplates() {
File templatedir = new File(dataDirectory, "templates");
templatedir.mkdirs();
/* First, prime the templates directory with default standard templates, if needed */
for(String stdtemplate : stdtemplates) {
File f = new File(templatedir, stdtemplate);
createDefaultFileFromResource("/templates/" + stdtemplate, f);
}
/* Now process files */
String[] templates = templatedir.list();
/* Go through list - process all ones not starting with 'custom' first */
for(String tname: templates) {
@ -174,49 +186,27 @@ public class DynmapPlugin extends JavaPlugin {
/* Load texture mappings */
TexturePack.loadTextureMapping(dataDirectory);
/* Initialize confguration.txt if needed */
File f = new File(this.getDataFolder(), "configuration.txt");
/* If configuration.txt not found, copy the default one */
if(f.exists() == false) {
Log.info("configuration.txt not found - creating default");
File deffile = new File(this.getDataFolder(), "configuration.default");
try {
FileInputStream fis = new FileInputStream(deffile);
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[512];
int len;
while((len = fis.read(buf)) > 0) {
fos.write(buf, 0, len);
}
fos.close();
fis.close();
} catch (IOException iox) {
Log.severe("ERROR CREATING DEFAULT CONFIGURATION.TXT!");
this.setEnabled(false);
return;
}
if(!createDefaultFileFromResource("/configuration.txt", f)) {
this.setEnabled(false);
return;
}
/* Load configuration.txt */
org.bukkit.util.config.Configuration bukkitConfiguration = new org.bukkit.util.config.Configuration(f);
bukkitConfiguration.load();
configuration = new ConfigurationNode(bukkitConfiguration);
/* Now, process worlds.txt - merge it in as an override of existing values (since it is only user supplied values) */
f = new File(this.getDataFolder(), "worlds.txt");
if(f.exists()) {
org.bukkit.util.config.Configuration cfg = new org.bukkit.util.config.Configuration(f);
cfg.load();
ConfigurationNode cn = new ConfigurationNode(cfg);
mergeConfigurationBranch(cn, "worlds", true, true);
}
else {
try {
FileWriter fw = new FileWriter(f);
fw.write("# This file is intended to allow the user to define world-specific settings\n");
fw.write("# Dynmap's install will not overwrite it\n");
fw.write("worlds:\n");
fw.close();
} catch (IOException iox) {
}
if(!createDefaultFileFromResource("/worlds.txt", f)) {
this.setEnabled(false);
return;
}
org.bukkit.util.config.Configuration cfg = new org.bukkit.util.config.Configuration(f);
cfg.load();
ConfigurationNode cn = new ConfigurationNode(cfg);
mergeConfigurationBranch(cn, "worlds", true, true);
/* Now, process templates */
loadTemplates();
@ -672,4 +662,35 @@ public class DynmapPlugin extends JavaPlugin {
public static void setIgnoreChunkLoads(boolean ignore) {
ignore_chunk_loads = ignore;
}
/* Uses resource to create default file, if file does not yet exist */
public boolean createDefaultFileFromResource(String resourcename, File deffile) {
if(deffile.canRead())
return true;
Log.info(deffile.getPath() + " not found - creating default");
InputStream in = getClass().getResourceAsStream(resourcename);
if(in == null) {
Log.severe("Unable to find default resource - " + resourcename);
return false;
}
else {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(deffile);
byte[] buf = new byte[512];
int len;
while((len = in.read(buf)) > 0) {
fos.write(buf, 0, len);
}
} catch (IOException iox) {
Log.severe("ERROR creatomg default for " + deffile.getPath());
return false;
} finally {
if(fos != null)
try { fos.close(); } catch (IOException iox) {}
if(in != null)
try { in.close(); } catch (IOException iox) {}
}
return true;
}
}
}

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 {

View File

@ -0,0 +1,3 @@
# The user is free to add new and custom lightings here, including replacements for standard ones
# Dynmap's install will not overwrite it
lightings:

View File

@ -0,0 +1,3 @@
# The user is free to add new and custom perspectives here, including replacements for standard ones
# Dynmap's install will not overwrite it
perspectives:

View File

@ -0,0 +1,3 @@
# The user is free to add new and custom shaders here, including replacements for standard ones
# Dynmap's install will not overwrite it
shaders:

View File

@ -1,6 +1,7 @@
version: 0.20
#
# This file contains default standard lighting profiles. The contents of this file are replaced and updated
# during upgrades, so new or updated lighing definitions should be done in the custom-lightings.txt file
# This file contains default standard lighting profiles. The contents of this file CAN need to be replaced and updated
# during upgrades, so new or updated lighting definitions should be done in the custom-lightings.txt file
#
lightings:
# Default lighting - no effects, shadows, day/night

View File

@ -1,5 +1,6 @@
version: 0.20
#
# This file contains default standard perspective definitions. The contents of this file are replaced and updated
# This file contains default standard perspective definitions. The contents of this file CAN need to be replaced and updated
# during upgrades, so new or updated perspective definitions should be done in the custom-perspectives.txt file
#
perspectives:

View File

@ -1,8 +1,9 @@
version: 0.20
#
# Default template for "Nether" environment worlds (deftemplatesuffix="hires")
# Uses the HDMap renderer with view from the SE with the "hires" resolution (16 pixels per block edge)
#
# To customize without losing updates during upgrade, rename file to 'custom-nether-hires.txt'
# This file MAY need to be replaced during an upgrade - rename file to 'custom-nether-hires.txt' if you wish to customize it
#
templates:
# Nether world template (HDMap hires)

View File

@ -1,8 +1,9 @@
version: 0.20
#
# Default template for "Nether" environment worlds (deftemplatesuffix="lowres")
# Uses the HDMap renderer with view from the SE with the "lowres" resolution (4 pixels per block edge)
#
# To customize without losing updates during upgrade, rename file to 'custom-nether-lowres.txt'
# This file MAY need to be replaced during an upgrade - rename file to 'custom-nether-lowres.txt' if you wish to customize it
#
templates:
# Nether world template (HDMap lowres)

View File

@ -1,6 +1,8 @@
version: 0.20
#
# Default template for "Nether" environment worlds (deftemplatesuffix="")
# To customize without losing updates during upgrade, rename file to 'custom-nether.txt'
#
# This file MAY need to be replaced during an upgrade - rename file to 'custom-nether.txt' if you wish to customize it
#
templates:
# Nether world template (classic render)

View File

@ -1,8 +1,9 @@
version: 0.20
#
# Default template for "Normal" environment worlds (deftemplatesuffix="lhires")
# Uses the HDMap renderer with view from the SE with the "hires" resolution (16 pixels per block edge)
#
# To customize without losing updates during upgrade, rename file to 'custom-normal-lowres.txt'
# This file MAY need to be replaced during an upgrade - rename file to 'custom-normal-hires.txt' if you wish to customize it
#
templates:
# Template for normal world (HDMap hires)

View File

@ -1,8 +1,9 @@
version: 0.20
#
# Default template for "Normal" environment worlds (deftemplatesuffix="lowres")
# Uses the HDMap renderer with view from the SE with the "lowres" resolution (4 pixels per block edge)
#
# To customize without losing updates during upgrade, rename file to 'custom-normal-lowres.txt'
# This file MAY need to be replaced during an upgrade - rename file to 'custom-normal-lowres.txt' if you wish to customize it
#
templates:
# Template for normal world (HDMap lowres)

View File

@ -1,6 +1,8 @@
version: 0.20
#
# Default template for "Normal" environment worlds (deftemplatesuffix="")
# To customize without losing updates during upgrade, rename file to 'custom-normal.txt'
#
# This file MAY need to be replaced during an upgrade - rename file to 'custom-normal.txt' if you wish to customize it
#
templates:
# Template for normal world (classic render)

View File

@ -1,8 +1,9 @@
version: 0.20
#
# Default template for "Skylands" environment worlds (deftemplatesuffix="hires")
# Uses the HDMap renderer with view from the SE with the "hires" resolution (16 pixels per block edge)
#
# To customize without losing updates during upgrade, rename file to 'custom-skylands-hires.txt'
# This file MAY need to be replaced during an upgrade - rename file to 'custom-skylands-hires.txt' if you wish to customize it
#
templates:
# Skylands world template (HDMap hires)

View File

@ -1,8 +1,9 @@
version: 0.20
#
# Default template for "Skylands" environment worlds (deftemplatesuffix="lowres")
# Uses the HDMap renderer with view from the SE with the "lowres" resolution (4 pixels per block edge)
#
# To customize without losing updates during upgrade, rename file to 'custom-skylands-lowres.txt'
# This file MAY need to be replaced during an upgrade - rename file to 'custom-skylands-lowres.txt' if you wish to customize it
#
templates:
# Skylands world template (HDMap lowres)

View File

@ -1,6 +1,8 @@
version: 0.20
#
# Default template for "Skylands" environment worlds (deftemplatesuffix="")
# To customize without losing updates during upgrade, rename file to 'custom-skylands.txt'
#
# This file MAY need to be replaced during an upgrade - rename file to 'custom-skylands.txt' if you wish to customize it
#
templates:
# Skylands world template (classic render)

View File

@ -1,8 +1,9 @@
# These are examples of world-specific settings, which can be defined in the worlds.txt file
# These are examples of world-specific settings - customize your content as you see fit
#
# NOTE:
# This file only contains samples: actual world customizations must be done in the worlds.txt file (or the worlds: section of
# configuration.txt). All lines here are commented with the # symbol - delete the # symbol on copied lines you wish to enable
# NOTES:
# All lines here are commented with the # symbol - delete the # symbol on copied lines you wish to enable
# Definitions of a world made here will superecede any world definition with the same name in configuration.txt
# Deleting this file will result in a fresh copy being produced by dynmap.jar on the next startup.
#
worlds:
# Worlds can be handled by templates, based on world type