0

specifying a directory name for a dimension should work now

This commit is contained in:
Aaron Griffith
2013-01-06 00:43:52 -05:00
parent a49a05a2ee
commit 77fff9fa25
2 changed files with 42 additions and 35 deletions

View File

@@ -182,8 +182,20 @@ def validateStr(s):
return str(s)
def validateDimension(d):
if d in ["nether", "overworld", "end", "default"]:
return d
# these are provided as arguments to RegionSet.get_type()
pretty_names = {
"nether": "DIM-1",
"overworld": None,
"end": "DIM1",
"default": 0,
}
try:
return pretty_names[d]
except KeyError:
if d.startswith("DIM"):
return d
raise ValidationException("%r is not a valid dimension" % d)
def validateOutputDir(d):

View File

@@ -121,8 +121,8 @@ class World(object):
mcas = [x for x in files if x.endswith(".mca")]
if mcas:
# construct a regionset object for this
rset = RegionSet(root)
if not rset.is_valid(): continue
rel = os.path.relpath(root, self.worlddir)
rset = RegionSet(root, rel)
if root == os.path.join(self.worlddir, "region"):
self.regionsets.insert(0, rset)
else:
@@ -151,15 +151,12 @@ class World(object):
def get_regionset(self, index):
if type(index) == int:
return self.regionsets[index]
else: # assume a string constant
if index == "default":
return self.regionsets[0]
else: # assume a get_type() value
candids = [x for x in self.regionsets if x.get_type() == index]
if len(candids) > 0:
return candids[0]
else:
candids = [x for x in self.regionsets if x.get_type() == index]
if len(candids) > 0:
return candids[0]
else:
return None
return None
def get_level_dat_data(self):
@@ -236,17 +233,28 @@ class RegionSet(object):
"""
def __init__(self, regiondir):
def __init__(self, regiondir, rel):
"""Initialize a new RegionSet to access the region files in the given
directory.
regiondir is a path to a directory containing region files.
rel is the relative path of this directory, with respect to the
world directory.
cachesize, if specified, is the number of chunks to keep parsed and
in-memory.
"""
self.regiondir = os.path.normpath(regiondir)
self.rel = os.path.normpath(rel)
# we want to get rid of /regions, if it exists
if self.rel.endswith(os.path.normpath("/region")):
self.type = self.rel[0:-len(os.path.normpath("/region"))]
if self.rel == "region":
# this is the main world
self.type = None
logging.debug("Scanning regions")
@@ -265,34 +273,21 @@ class RegionSet(object):
# Re-initialize upon unpickling
def __getstate__(self):
return self.regiondir
__setstate__ = __init__
return (self.regiondir, self.rel)
def __setstate__(self, state):
return self.__init__(*state)
def __repr__(self):
return "<RegionSet regiondir=%r>" % self.regiondir
def is_valid(self):
"""If this region set isn't one of the three known regions, then
return False"""
try:
self.get_type()
return True
except Exception:
return False
def get_type(self):
"""Attempts to return a string describing the dimension represented by
this regionset. Either "nether", "end" or "overworld"
"""Attempts to return a string describing the dimension
represented by this regionset. Usually this is the relative
path of the regionset within the world, minus the suffix
/region, but for the main world it's None.
"""
# path will be normalized in __init__
if self.regiondir.endswith(os.path.normpath("/DIM-1/region")):
return "nether"
elif self.regiondir.endswith(os.path.normpath("/DIM1/region")):
return "end"
elif self.regiondir.endswith(os.path.normpath("/region")):
return "overworld"
else:
raise Exception("Woah, what kind of dimension is this?! %r" % self.regiondir)
return self.type
def _get_regionobj(self, regionfilename):
# Check the cache first. If it's not there, create the