Created
April 10, 2011 00:57
-
-
Save agrif/911946 to your computer and use it in GitHub Desktop.
a simple MCR chunk extractor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
usage = "python contrib/%prog [-v] [-f file] [-o output/]" | |
description = """ | |
Extracts the given MCR file into output/. If -o is not provided, it | |
will be derived from the input file name. | |
""" | |
from optparse import OptionParser | |
import sys | |
import os.path | |
# sys.path wrangling, so we can access Overviewer code | |
overviewer_dir = os.path.split(os.path.split(os.path.abspath(__file__))[0])[0] | |
sys.path.insert(0, overviewer_dir) | |
import c_overviewer | |
import nbt | |
from world import base36encode | |
def main(): | |
parser = OptionParser(usage=usage, description=description) | |
parser.add_option("-v", "--verbose", dest="verbose", action="store_true", | |
help="list every file extracted.") | |
parser.add_option("-f", "--file", dest="file", default=None, metavar="FILE", | |
help="the MCR file to extract") | |
parser.add_option("-o", "--output", dest="output", default=None, metavar="OUT", | |
help="the output directory") | |
opt, args = parser.parse_args() | |
if not len(args) == 0: | |
parser.print_help() | |
sys.exit(1) | |
if opt.file is None: # and opt.output is None: | |
parser.print_help() | |
print >>sys.stderr, "you must provide -f" | |
sys.exit(1) | |
filename = opt.file | |
if filename: | |
if not filename.lower().endswith('.mcr'): | |
print >>sys.stderr, "error: not a MCR file" | |
sys.exit(1) | |
if not os.path.exists(filename): | |
print >>sys.stderr, "error: cannot open '%s'" % (filename,) | |
sys.exit(1) | |
outdir = opt.output | |
if outdir is None: | |
assert filename | |
outdir = os.path.splitext(filename)[0] | |
outdir = os.path.split(outdir)[1] | |
if not (os.path.exists(outdir) and os.path.isdir(outdir)): | |
try: | |
os.makedirs(outdir) | |
except: | |
print >>sys.stderr, "error: cannot create directory '%s'" % (outdir,) | |
sys.exit(1) | |
## start extraction | |
mcr = nbt.MCRFileReader(filename) | |
for x, y in mcr.get_chunks(): | |
chunk = mcr.load_chunk(x, y) | |
outfile = "chunk.%s.%s.nbt" % (base36encode(x), base36encode(y)) | |
outfile = os.path.join(outdir, outfile) | |
if opt.verbose: | |
print outfile | |
fout = open(outfile, 'wb') | |
fout.write(chunk._file.read()) | |
fout.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment