diff --git a/README.md b/README.md index 01e5a7a3783e8e4616f4cc941b3ec8c601c9a7ce..739547c5038ea1132efbe1d4829d87bb85ad75e8 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,4 @@ Known issues: This generates a table that counts all the occurrences of each Thing type in a map. These tables appear in the "Technical data" sections of the level pages, e.g., [Greenflower Zone Act 1](wiki.srb2.org/wiki/Greenflower_Zone_Act_1#Technical_data). The list of Thing types is read from `thing_names.txt`, which is a slightly cleaned-up version of the [Thing types](wiki.srb2.org/wiki/Thing_types) page. Before running the script, check that the Thing type list is up to date. -This script was designed to be used with `zones.pk3` (although any other PK3 with maps will work just as well). Before running the script, extract all the map WADs from `zones.pk3` and place them in the same folder as the script. Then run `python3 thing_counter.py`. For each map file `MAPxx.wad` in the folder, this will generate a text file `MAPxx.txt` with the Thing table. Alternatively, if you specify map names via the command line, it will only generate tables for those maps. For example, `python3 MAP04 MAP50 MAPM0` will generate tables for `MAP04.wad`, `MAP50.wad` and `MAPM0.wad`. - -Known issues: -* Currently, the script doesn't support reading more than one map from a WAD file. Only the first `THINGS` lump in the file is read. \ No newline at end of file +This script cannot read WADs inside of PK3s. If you want to generate Thing tables for all maps inside a PK3, you need to unzip it first and place all the map files in the same folder as the script. Then run `python3 thing_counter.py`. For each WAD file `example.wad` in the folder and each map `MAPxx` inside the WAD, the script will generate a text file `example.wad_MAPxx.txt` with the Thing table. Alternatively, if you specify filenames via the command line (e.g., `python3 example1.wad example2.wad example3.wad`), it will only generate tables for the maps in those files. \ No newline at end of file diff --git a/thing_counter.py b/thing_counter.py index 6fad43b0a0526709a47988fb67bb663589caa958..5ec708332cede77c6eaa98a785dc983b3770be43 100644 --- a/thing_counter.py +++ b/thing_counter.py @@ -22,7 +22,7 @@ def parse_thing_names(): thing_names[type] = name thing_categories[-1][1].append(type) -def parse_things(map, data, lump_start, lump_size): +def parse_things(filename, mapname, data, lump_start, lump_size): thing_count = [0 for x in range(4096)] for i in range(lump_size//10): @@ -34,7 +34,7 @@ def parse_things(map, data, lump_start, lump_size): for type, value in ring_values.items(): total_rings += value * thing_count[type] - with open(map + '.txt', 'w') as output_file: + with open(filename + '_' + mapname + '.txt', 'w') as output_file: output_file.write('{| class="wikitable collapsible collapsed"\n') output_file.write('|-\n') output_file.write('! colspan="2" | [[Thing types|Things]]\n') @@ -58,30 +58,32 @@ def parse_things(map, data, lump_start, lump_size): output_file.write('| ' + str(total_rings) + '\n') output_file.write('|}\n') -def read_wad(map): - print('Parsing ' + map + '...') - with open(map + '.wad', 'rb') as input_file: +def read_wad(filename): + print('Parsing ' + filename + '...') + with open(filename, 'rb') as input_file: header_size = 12 type = input_file.read(4) assert type == b'PWAD', 'Input file is not a WAD file with PWAD header!' num_lumps = int.from_bytes(input_file.read(4), "little") directory_offset = int.from_bytes(input_file.read(4), "little") data = input_file.read(directory_offset - header_size) - + mapname = '' + for i in range(num_lumps): lump_start = int.from_bytes(input_file.read(4), "little") - header_size lump_size = int.from_bytes(input_file.read(4), "little") lump_name = input_file.read(8).decode('UTF-8').rstrip('\0') + if lump_size == 0: + mapname = lump_name if lump_name == 'THINGS': - parse_things(map, data, lump_start, lump_size) - break + parse_things(filename, mapname, data, lump_start, lump_size) parse_thing_names() if len(sys.argv) < 2: #Parse all WAD files in the directory for file in glob.glob("*.wad"): - read_wad(file.rstrip('.wad')) + read_wad(file) else: #Parse specified files for i in range(1, len(sys.argv)):