From dacd45e5d32aab58258a0722495c6b86c9dc0534 Mon Sep 17 00:00:00 2001 From: Aaron Griffith Date: Fri, 13 May 2011 17:21:48 -0400 Subject: [PATCH] configurable settings.py, web_assets, textures paths --- configParser.py | 11 +++++- googlemap.py | 34 +++++++++++-------- overviewer.py | 4 ++- rendernode.py | 5 +-- textures.py | 29 +++++++++------- .../overviewerConfig.js | 0 6 files changed, 52 insertions(+), 31 deletions(-) rename overviewerConfig.js => web_assets/overviewerConfig.js (100%) diff --git a/configParser.py b/configParser.py index 5b275f0..c779fc2 100644 --- a/configParser.py +++ b/configParser.py @@ -22,6 +22,9 @@ class ConfigOptionParser(object): self.customArgs = ["required", "commandLineOnly", "default", "listify", "listdelim", "choices"] self.requiredArgs = [] + + # add the *very* special config-file path option + self.add_option("--config-file", dest="config_file", help="Specifies a configuration file to load, by name. This file's format is discussed in the README.", metavar="PATH", type="string", commandLineOnly=True) def display_config(self): logging.info("Using the following settings:") @@ -68,8 +71,14 @@ class ConfigOptionParser(object): # if this has a default, use that to seed the globals dict if a.get("default", None): g[n] = a['default'] g['args'] = args - + try: + if options.config_file: + self.configFile = options.config_file + elif os.path.exists(self.configFile): + # warn about automatic loading + logging.warning("Automatic settings.py loading is DEPRECATED, and may be removed in the future. Please use --config-file instead.") + if os.path.exists(self.configFile): execfile(self.configFile, g, l) except NameError, ex: diff --git a/googlemap.py b/googlemap.py index 331a4ea..daf0e0e 100644 --- a/googlemap.py +++ b/googlemap.py @@ -68,6 +68,7 @@ class MapGen(object): self.skipjs = configInfo.get('skipjs', None) self.web_assets_hook = configInfo.get('web_assets_hook', None) + self.web_assets_path = configInfo.get('web_assets_path', None) self.bg_color = configInfo.get('bg_color') if not len(quadtrees) > 0: @@ -81,14 +82,28 @@ class MapGen(object): raise ValueError("all the given quadtrees must have the same destdir and world") self.quadtrees = quadtrees - + def go(self, procs): """Writes out config.js, marker.js, and region.js Copies web assets into the destdir""" zoomlevel = self.p - configpath = os.path.join(util.get_program_path(), "overviewerConfig.js") - config = open(configpath, 'r').read() + bgcolor = (int(self.bg_color[1:3],16), int(self.bg_color[3:5],16), int(self.bg_color[5:7],16), 0) + blank = Image.new("RGBA", (1,1), bgcolor) + # Write a blank image + for quadtree in self.quadtrees: + tileDir = os.path.join(self.destdir, quadtree.tiledir) + if not os.path.exists(tileDir): os.mkdir(tileDir) + blank.save(os.path.join(tileDir, "blank."+quadtree.imgformat)) + + # copy web assets into destdir: + mirror_dir(os.path.join(util.get_program_path(), "web_assets"), self.destdir) + # do the same with the local copy, if we have it + if self.web_assets_path: + mirror_dir(self.web_assets_path, self.destdir) + + # replace the config js stuff + config = open(os.path.join(self.destdir, 'overviewerConfig.js'), 'r').read() config = config.replace( "{minzoom}", str(0)) config = config.replace( @@ -111,18 +126,7 @@ class MapGen(object): with open(os.path.join(self.destdir, "overviewerConfig.js"), 'w') as output: output.write(config) - bgcolor = (int(self.bg_color[1:3],16), int(self.bg_color[3:5],16), int(self.bg_color[5:7],16), 0) - blank = Image.new("RGBA", (1,1), bgcolor) - # Write a blank image - for quadtree in self.quadtrees: - tileDir = os.path.join(self.destdir, quadtree.tiledir) - if not os.path.exists(tileDir): os.mkdir(tileDir) - blank.save(os.path.join(tileDir, "blank."+quadtree.imgformat)) - - # copy web assets into destdir: - mirror_dir(os.path.join(util.get_program_path(), "web_assets"), self.destdir) - - # Add time in index.html + # Add time and version in index.html indexpath = os.path.join(self.destdir, "index.html") index = open(indexpath, 'r').read() diff --git a/overviewer.py b/overviewer.py index bd4ef7b..396cee8 100755 --- a/overviewer.py +++ b/overviewer.py @@ -101,6 +101,8 @@ def main(): parser.add_option("--bg_color", dest="bg_color", help="Configures the background color for the GoogleMap output. Specify in #RRGGBB format", configFileOnly=True, type="string", default="#1A1A1A") parser.add_option("--optimize-img", dest="optimizeimg", help="If using png, perform image file size optimizations on the output. Specify 1 for pngcrush, 2 for pngcrush+optipng+advdef. This may double (or more) render times, but will produce up to 30% smaller images. NOTE: requires corresponding programs in $PATH or %PATH%", configFileOnly=True) parser.add_option("--web-assets-hook", dest="web_assets_hook", help="If provided, run this function after the web assets have been copied, but before actual tile rendering begins. It should accept a QuadtreeGen object as its only argument.", action="store", metavar="SCRIPT", type="function", configFileOnly=True) + parser.add_option("--web-assets-path", dest="web_assets_path", help="Specifies a non-standard web_assets directory to use. Files here will overwrite the default web assets.", metavar="PATH", type="string", configFileOnly=True) + parser.add_option("--textures-path", dest="textures_path", help="Specifies a non-standard textures path, from which terrain.png and other textures are loaded.", metavar="PATH", type="string", configFileOnly=True) parser.add_option("-q", "--quiet", dest="quiet", action="count", default=0, help="Print less output. You can specify this option multiple times.") parser.add_option("-v", "--verbose", dest="verbose", action="count", default=0, help="Print more output. You can specify this option multiple times.") parser.add_option("--skip-js", dest="skipjs", action="store_true", help="Don't output marker.js or regions.js") @@ -242,7 +244,7 @@ def main(): qtree.go(options.procs) # create the distributed render - r = rendernode.RenderNode(q) + r = rendernode.RenderNode(q, options) # write out the map and web assets m = googlemap.MapGen(q, configInfo=options) diff --git a/rendernode.py b/rendernode.py index f99c087..6f6c76e 100644 --- a/rendernode.py +++ b/rendernode.py @@ -65,7 +65,7 @@ def pool_initializer(rendernode): # make sure textures are generated for this process # and initialize c_overviewer - textures.generate() + textures.generate(path=rendernode.options.get('textures_path', None)) c_overviewer.init_chunk_render() # load biome data in each process, if needed @@ -94,12 +94,13 @@ def roundrobin(iterables): class RenderNode(object): - def __init__(self, quadtrees): + def __init__(self, quadtrees, options): """Distributes the rendering of a list of quadtrees.""" if not len(quadtrees) > 0: raise ValueError("there must be at least one quadtree to work on") + self.options = options self.quadtrees = quadtrees #bind an index value to the quadtree so we can find it again #and figure out which worlds are where diff --git a/textures.py b/textures.py index 8a63d3c..611353a 100644 --- a/textures.py +++ b/textures.py @@ -26,11 +26,14 @@ from PIL import Image, ImageEnhance, ImageOps, ImageDraw import util import composite +_find_file_local_path = None def _find_file(filename, mode="rb"): """Searches for the given file and returns an open handle to it. This searches the following locations in this order: + * the textures_path given in the config file (if present) * The program dir (same dir as this file) + * The program dir / textures * On Darwin, in /Applications/Minecraft * Inside minecraft.jar, which is looked for at these locations @@ -38,14 +41,21 @@ def _find_file(filename, mode="rb"): * On Darwin, at $HOME/Library/Application Support/minecraft/bin/minecraft.jar * at $HOME/.minecraft/bin/minecraft.jar - * The current working directory - * The program dir / textures - """ + + if _find_file_local_path: + path = os.path.join(_find_file_local_path, filename) + if os.path.exists(path): + return open(path, mode) + programdir = util.get_program_path() path = os.path.join(programdir, filename) if os.path.exists(path): return open(path, mode) + + path = os.path.join(programdir, "textures", filename) + if os.path.exists(path): + return open(path, mode) if sys.platform == "darwin": path = os.path.join("/Applications/Minecraft", filename) @@ -73,14 +83,6 @@ def _find_file(filename, mode="rb"): except (KeyError, IOError): pass - path = filename - if os.path.exists(path): - return open(path, mode) - - path = os.path.join(programdir, "textures", filename) - if os.path.exists(path): - return open(path, mode) - raise IOError("Could not find the file {0}. Is Minecraft installed? If so, I couldn't find the minecraft.jar file.".format(filename)) def _load_image(filename): @@ -1661,7 +1663,10 @@ biome_grass_texture = None biome_leaf_texture = None specialblockmap = None -def generate(): +def generate(path=None): + global _find_file_local_path + _find_file_local_path = path + # This maps terainids to 16x16 images global terrain_images terrain_images = _split_terrain(_get_terrain_image()) diff --git a/overviewerConfig.js b/web_assets/overviewerConfig.js similarity index 100% rename from overviewerConfig.js rename to web_assets/overviewerConfig.js