no longer validates images, runs MUCH faster to scan existing chunks.
If a chunk image can't be loaded, it re-generates it on the fly.
This commit is contained in:
21
chunk.py
21
chunk.py
@@ -60,17 +60,6 @@ def render_and_save(chunkfile, cave=False):
|
|||||||
# propagate the exception back to the parent process.
|
# propagate the exception back to the parent process.
|
||||||
raise Exception()
|
raise Exception()
|
||||||
|
|
||||||
def valid_image(filename):
|
|
||||||
"""Returns true if the file is valid, false if it can't be loaded (is
|
|
||||||
corrupt or something)
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
img = Image.open(filename)
|
|
||||||
img.load()
|
|
||||||
except Exception, e:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
class ChunkRenderer(object):
|
class ChunkRenderer(object):
|
||||||
def __init__(self, chunkfile):
|
def __init__(self, chunkfile):
|
||||||
if not os.path.exists(chunkfile):
|
if not os.path.exists(chunkfile):
|
||||||
@@ -140,11 +129,7 @@ class ChunkRenderer(object):
|
|||||||
if os.path.getmtime(self.chunkfile) < os.path.getmtime(oldimg_path):
|
if os.path.getmtime(self.chunkfile) < os.path.getmtime(oldimg_path):
|
||||||
# chunkfile is older than the image, don't even bother checking
|
# chunkfile is older than the image, don't even bother checking
|
||||||
# the hash
|
# the hash
|
||||||
if valid_image(oldimg_path):
|
|
||||||
return oldimg_path
|
return oldimg_path
|
||||||
else:
|
|
||||||
os.unlink(oldimg_path)
|
|
||||||
oldimg = None
|
|
||||||
|
|
||||||
# Reasons for the code to get to this point:
|
# Reasons for the code to get to this point:
|
||||||
# 1) An old image doesn't exist
|
# 1) An old image doesn't exist
|
||||||
@@ -163,7 +148,7 @@ class ChunkRenderer(object):
|
|||||||
dest_path = os.path.join(destdir, dest_filename)
|
dest_path = os.path.join(destdir, dest_filename)
|
||||||
|
|
||||||
if oldimg:
|
if oldimg:
|
||||||
if dest_filename == oldimg and valid_image(dest_path):
|
if dest_filename == oldimg:
|
||||||
# There is an existing file, the chunk has a newer mtime, but the
|
# There is an existing file, the chunk has a newer mtime, but the
|
||||||
# hashes match.
|
# hashes match.
|
||||||
return dest_path
|
return dest_path
|
||||||
@@ -175,7 +160,11 @@ class ChunkRenderer(object):
|
|||||||
# Render the chunk
|
# Render the chunk
|
||||||
img = self.chunk_render(cave=cave)
|
img = self.chunk_render(cave=cave)
|
||||||
# Save it
|
# Save it
|
||||||
|
try:
|
||||||
img.save(dest_path)
|
img.save(dest_path)
|
||||||
|
except:
|
||||||
|
os.unlink(dest_path)
|
||||||
|
raise
|
||||||
# Return its location
|
# Return its location
|
||||||
return dest_path
|
return dest_path
|
||||||
|
|
||||||
|
|||||||
2
gmap.py
2
gmap.py
@@ -44,6 +44,8 @@ def main():
|
|||||||
if 1000 % i == 0 or i % 1000 == 0:
|
if 1000 % i == 0 or i % 1000 == 0:
|
||||||
print "{0}/{1} chunks rendered".format(i, len(chunks))
|
print "{0}/{1} chunks rendered".format(i, len(chunks))
|
||||||
|
|
||||||
|
results['pool'].join()
|
||||||
|
|
||||||
print "Writing out html file"
|
print "Writing out html file"
|
||||||
if not os.path.exists(destdir):
|
if not os.path.exists(destdir):
|
||||||
os.mkdir(destdir)
|
os.mkdir(destdir)
|
||||||
|
|||||||
25
world.py
25
world.py
@@ -299,7 +299,22 @@ def render_worldtile(chunkmap, colstart, colend, rowstart, rowend, oldhash):
|
|||||||
# col colstart will get drawn on the image starting at x coordinates -(384/2)
|
# col colstart will get drawn on the image starting at x coordinates -(384/2)
|
||||||
# row rowstart will get drawn on the image starting at y coordinates -(192/2)
|
# row rowstart will get drawn on the image starting at y coordinates -(192/2)
|
||||||
for col, row, chunkfile in tilelist:
|
for col, row, chunkfile in tilelist:
|
||||||
|
try:
|
||||||
chunkimg = Image.open(chunkfile)
|
chunkimg = Image.open(chunkfile)
|
||||||
|
except IOError, e:
|
||||||
|
print "Error opening file", chunkfile
|
||||||
|
print "Attempting to re-generate it"
|
||||||
|
os.unlink(chunkfile)
|
||||||
|
# Do some string manipulation to determine what the chunk file is
|
||||||
|
# that goes with this image. Then call chunk.render_and_save
|
||||||
|
dirname, imagename = os.path.split(chunkfile)
|
||||||
|
parts = imagename.split(".")
|
||||||
|
datafile = "c.{0}.{1}.dat".format(parts[1],parts[2])
|
||||||
|
print "Chunk came from data file", datafile
|
||||||
|
# XXX Don't forget to set cave mode here when it gets implemented!
|
||||||
|
chunk.render_and_save(os.path.join(dirname, datafile), False)
|
||||||
|
chunkimg = Image.open(chunkfile)
|
||||||
|
print "Success"
|
||||||
|
|
||||||
xpos = -192 + (col-colstart)*192
|
xpos = -192 + (col-colstart)*192
|
||||||
ypos = -96 + (row-rowstart)*96
|
ypos = -96 + (row-rowstart)*96
|
||||||
@@ -635,13 +650,21 @@ class ReturnableProcess(multiprocessing.Process):
|
|||||||
multiprocessing.Process.__init__(self, *args, **kwargs)
|
multiprocessing.Process.__init__(self, *args, **kwargs)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
try:
|
||||||
results = self._target(*self._args, **self._kwargs)
|
results = self._target(*self._args, **self._kwargs)
|
||||||
|
except BaseException, e:
|
||||||
|
self._respipe_in.send(e)
|
||||||
|
else:
|
||||||
self._respipe_in.send(results)
|
self._respipe_in.send(results)
|
||||||
|
finally:
|
||||||
self.__sem.release()
|
self.__sem.release()
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
self.join()
|
self.join()
|
||||||
return self._respipe_out.recv()
|
ret = self._respipe_out.recv()
|
||||||
|
if isinstance(ret, BaseException):
|
||||||
|
raise ret
|
||||||
|
return ret
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self._respipe_out, self._respipe_in = multiprocessing.Pipe()
|
self._respipe_out, self._respipe_in = multiprocessing.Pipe()
|
||||||
|
|||||||
Reference in New Issue
Block a user