0

Merged in rmccue's cache checking changes

Conflicts:
	chunk.py
This commit is contained in:
Andrew Chin
2010-12-23 02:01:58 -05:00
2 changed files with 71 additions and 38 deletions

View File

@@ -124,12 +124,38 @@ fluid_blocks = set([8,9,10,11])
# (glass, half blocks)
nospawn_blocks = set([20,44])
def render_and_save(chunkfile, cachedir, worldobj, cave=False, queue=None):
def find_oldimage(chunkfile, cached, cave):
destdir, filename = os.path.split(chunkfile)
filename_split = filename.split(".")
blockid = ".".join(filename_split[1:3])
# Get the name of the existing image.
moredirs, dir2 = os.path.split(destdir)
dir1 = os.path.basename(moredirs)
cachename = '/'.join((dir1, dir2))
oldimg = oldimg_path = None
key = ".".join((blockid, "cave" if cave else "nocave"))
if key in cached[cachename]:
oldimg_path = cached[cachename][key]
_, oldimg = os.path.split(oldimg_path)
logging.debug("Found cached image {0}".format(oldimg))
return oldimg, oldimg_path
def check_cache(chunkfile, oldimg):
try:
if oldimg[1] and os.path.getmtime(chunkfile) <= os.path.getmtime(oldimg[1]):
return True
return False
except OSError:
return False
def render_and_save(chunkfile, cachedir, worldobj, oldimg, cave=False, queue=None):
"""Used as the entry point for the multiprocessing workers (since processes
can't target bound methods) or to easily render and save one chunk
Returns the image file location"""
a = ChunkRenderer(chunkfile, cachedir, worldobj, queue)
a = ChunkRenderer(chunkfile, cachedir, worldobj, oldimg, queue)
try:
return a.render_and_save(cave)
except ChunkCorrupt:
@@ -152,7 +178,7 @@ class ChunkCorrupt(Exception):
pass
class ChunkRenderer(object):
def __init__(self, chunkfile, cachedir, worldobj, queue):
def __init__(self, chunkfile, cachedir, worldobj, oldimg, queue):
"""Make a new chunk renderer for the given chunkfile.
chunkfile should be a full path to the .dat file to process
cachedir is a directory to save the resulting chunk images to
@@ -181,6 +207,7 @@ class ChunkRenderer(object):
moredirs, dir2 = os.path.split(destdir)
_, dir1 = os.path.split(moredirs)
self.cachedir = os.path.join(cachedir, dir1, dir2)
self.oldimg, self.oldimg_path = oldimg
if self.world.useBiomeData:
@@ -486,36 +513,12 @@ class ChunkRenderer(object):
self._digest = digest[:6]
return self._digest
def find_oldimage(self, cave):
# Get the name of the existing image. No way to do this but to look at
# all the files
oldimg = oldimg_path = None
for filename in os.listdir(self.cachedir):
if filename.startswith("img.{0}.{1}.".format(self.blockid,
"cave" if cave else "nocave")) and \
filename.endswith(".png"):
oldimg = filename
oldimg_path = os.path.join(self.cachedir, oldimg)
break
return oldimg, oldimg_path
def render_and_save(self, cave=False):
"""Render the chunk using chunk_render, and then save it to a file in
the same directory as the source image. If the file already exists and
is up to date, this method doesn't render anything.
"""
blockid = self.blockid
oldimg, oldimg_path = self.find_oldimage(cave)
if oldimg:
# An image exists? Instead of checking the hash which is kinda
# expensive (for tens of thousands of chunks, yes it is) check if
# the mtime of the chunk file is newer than the mtime of oldimg
if os.path.getmtime(self.chunkfile) <= os.path.getmtime(oldimg_path):
# chunkfile is older than the image, don't even bother checking
# the hash
return oldimg_path
# Reasons for the code to get to this point:
# 1) An old image doesn't exist
@@ -533,18 +536,19 @@ class ChunkRenderer(object):
dest_path = os.path.join(self.cachedir, dest_filename)
if oldimg:
if dest_filename == oldimg:
if self.oldimg:
if dest_filename == self.oldimg:
# There is an existing file, the chunk has a newer mtime, but the
# hashes match.
# Before we return it, update its mtime so the next round
# doesn't have to check the hash
os.utime(dest_path, None)
logging.debug("Using cached image")
return dest_path
else:
# Remove old image for this chunk. Anything already existing is
# either corrupt or out of date
os.unlink(oldimg_path)
os.unlink(self.oldimg_path)
# Render the chunk
img = self.chunk_render(cave=cave)