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) return str(s)
def validateDimension(d): def validateDimension(d):
if d in ["nether", "overworld", "end", "default"]: # 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 return d
raise ValidationException("%r is not a valid dimension" % d) raise ValidationException("%r is not a valid dimension" % d)
def validateOutputDir(d): def validateOutputDir(d):

View File

@@ -121,8 +121,8 @@ class World(object):
mcas = [x for x in files if x.endswith(".mca")] mcas = [x for x in files if x.endswith(".mca")]
if mcas: if mcas:
# construct a regionset object for this # construct a regionset object for this
rset = RegionSet(root) rel = os.path.relpath(root, self.worlddir)
if not rset.is_valid(): continue rset = RegionSet(root, rel)
if root == os.path.join(self.worlddir, "region"): if root == os.path.join(self.worlddir, "region"):
self.regionsets.insert(0, rset) self.regionsets.insert(0, rset)
else: else:
@@ -151,10 +151,7 @@ class World(object):
def get_regionset(self, index): def get_regionset(self, index):
if type(index) == int: if type(index) == int:
return self.regionsets[index] return self.regionsets[index]
else: # assume a string constant else: # assume a get_type() value
if index == "default":
return self.regionsets[0]
else:
candids = [x for x in self.regionsets if x.get_type() == index] candids = [x for x in self.regionsets if x.get_type() == index]
if len(candids) > 0: if len(candids) > 0:
return candids[0] return candids[0]
@@ -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 """Initialize a new RegionSet to access the region files in the given
directory. directory.
regiondir is a path to a directory containing region files. 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 cachesize, if specified, is the number of chunks to keep parsed and
in-memory. in-memory.
""" """
self.regiondir = os.path.normpath(regiondir) 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") logging.debug("Scanning regions")
@@ -265,34 +273,21 @@ class RegionSet(object):
# Re-initialize upon unpickling # Re-initialize upon unpickling
def __getstate__(self): def __getstate__(self):
return self.regiondir return (self.regiondir, self.rel)
__setstate__ = __init__ def __setstate__(self, state):
return self.__init__(*state)
def __repr__(self): def __repr__(self):
return "<RegionSet regiondir=%r>" % self.regiondir 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): def get_type(self):
"""Attempts to return a string describing the dimension represented by """Attempts to return a string describing the dimension
this regionset. Either "nether", "end" or "overworld" 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__ # path will be normalized in __init__
if self.regiondir.endswith(os.path.normpath("/DIM-1/region")): return self.type
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)
def _get_regionobj(self, regionfilename): def _get_regionobj(self, regionfilename):
# Check the cache first. If it's not there, create the # Check the cache first. If it's not there, create the