Skip to content

Commit

Permalink
read bspdir, fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
illwieckz committed Jul 16, 2015
1 parent a58cc42 commit f3cd31e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ Currently, the `-il` option for `bsp_cutter.py` (to read lightmaps from a direct

```
$ ./bsp_cutter.py -h
usage: bsp_cutter.py [-h] [-D] [-ib FILENAME] [-ob FILENAME] [-od DIRNAME]
[-ie FILENAME] [-oe FILENAME] [-it FILENAME]
[-ot FILENAME] [-il DIRNAME] [-ol DIRNAME] [-sl] [-la]
[-lL] [-le] [-ls] [-lt] [-ll] [-pe]
usage: bsp_cutter.py [-h] [-D] [-ib FILENAME] [-id DIRNAME] [-ob FILENAME]
[-od DIRNAME] [-ie FILENAME] [-oe FILENAME]
[-it FILENAME] [-ot FILENAME] [-il DIRNAME] [-ol DIRNAME]
[-sl] [-la] [-lL] [-le] [-ls] [-lt] [-ll] [-pe]
bsp_cutter.py is a BSP parser for my lovely granger.
Expand All @@ -30,6 +30,8 @@ optional arguments:
-D, --debug print debug information
-ib FILENAME, --input-bsp FILENAME
read from .bsp file FILENAME
-id DIRNAME, --input-bsp-dir DIRNAME
read from .bspdir directory DIRNAME
-ob FILENAME, --output-bsp FILENAME
write to .bsp file FILENAME
-od DIRNAME, --output-bsp-dir DIRNAME
Expand Down Expand Up @@ -96,7 +98,3 @@ Copyright
---------

This script is distributed under the highly permissive and laconic [ISC License](COPYING.md).
pyright
---------

This script is distributed under the highly permissive and laconic [ISC License](COPYING.md).
61 changes: 58 additions & 3 deletions bsp_cutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ def print_list(self):
return True

def read_dir(self, dir_name):
# TODO: check if a dir, perhaps argparse can do
self.lightmap_list = []
file_list = sorted(glob.glob(dir_name + "/lm_*.tga"))
for file_name in file_list:
Expand All @@ -250,7 +251,7 @@ def read_dir(self, dir_name):

def write_dir(self, dir_name):
if not os.path.exists(dir_name):
os.makedirs(dir_name)
os.makedirs(dir_name)

for i in range(0, len(self.lightmap_list)):
file_name = "lm_" + str(i).zfill(4) + ".tga"
Expand Down Expand Up @@ -366,6 +367,60 @@ def read_file(self, bsp_file_name):
self.bsp_file.close()
return True

def read_dir(self, dir_name):
# TODO: check if a dir, perhapas argparse can do
for lump_name in lump_name_list:
file_list = glob.glob(dir_name + "/" + lump_name + ".*")

if len(file_list) > 1:
error("more than one " + lump_name + " lump in bspdir")
# TODO: handling
return

file_path = file_list[0]
file_ext = file_path.split(".")[-1]
file_name = file_path.split("/")[-1][: -(len(file_ext) +1)]

if file_ext == "bin":
if file_name in lump_name_list:
lump_file = open( file_path, "rb")
# TODO: check
self.lump_dict[file_name] = lump_file.read()
else:
error("unknown lump format: " + filename)
return

elif file_ext == "csv":
if file_name == "textures":
textures = Textures()
textures.read_file(file_path)
self.lump_dict[file_name] = textures.export_lump()
else:
error("unknown lump format: " + file_path)
return

elif file_ext == "txt":
if file_name == "entities":
entities = Entities()
entities.read_file(file_path)
self.lump_dict[file_name] = entities.export_lump()
else:
error("unknown lump format: " + file_path)
return

elif file_ext == "d":
if file_name == "lightmaps":
lightmaps = Lightmaps()
lightmaps.read_dir(file_path)
self.lump_dict[file_name] = lightmaps.export_lump()
else:
error("unknown lump format: " + file_path)
return

else:
error("unknown lump format: " + file_path)
return

def print_file_name(self):
print("*** File:")
print(self.bsp_file_name)
Expand Down Expand Up @@ -468,7 +523,7 @@ def write_file(self, bsp_file_name):

def write_dir(self, dir_name):
if not os.path.exists(dir_name):
os.makedirs(dir_name)
os.makedirs(dir_name)

for entity in lump_name_list:
if entity == "entities":
Expand Down Expand Up @@ -501,7 +556,7 @@ def main(argv):
args = argparse.ArgumentParser(description="%(prog)s is a BSP parser for my lovely granger.")
args.add_argument("-D", "--debug", help="print debug information", action="store_true")
args.add_argument("-ib", "--input-bsp", dest="input_bsp_file", metavar="FILENAME", help="read from .bsp file %(metavar)s")
# args.add_argument("-id", "--input-bsp-dir", dest="input_bsp_dir", metavar="DIRNAME", help="read from .bspdir directory %(metavar)s")
args.add_argument("-id", "--input-bsp-dir", dest="input_bsp_dir", metavar="DIRNAME", help="read from .bspdir directory %(metavar)s")
args.add_argument("-ob", "--output-bsp", dest="output_bsp_file", metavar="FILENAME", help="write to .bsp file %(metavar)s")
args.add_argument("-od", "--output-bsp-dir", dest="output_bsp_dir", metavar="DIRNAME", help="write to .bspdir directory %(metavar)s")
args.add_argument("-ie", "--input-entities", dest="input_entities_file", metavar="FILENAME", help="read from entities .txt file %(metavar)s")
Expand Down

0 comments on commit f3cd31e

Please sign in to comment.