From f8cc5a50db87246fcb9f0e6fb7f4db03b278154b Mon Sep 17 00:00:00 2001 From: Connor Date: Thu, 24 Aug 2017 22:57:48 -0700 Subject: [PATCH 1/2] --- bwmap.py | 519 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 519 insertions(+) create mode 100644 bwmap.py diff --git a/bwmap.py b/bwmap.py new file mode 100644 index 0000000..7792520 --- /dev/null +++ b/bwmap.py @@ -0,0 +1,519 @@ +import sys +import struct + +# sorry not sorry +P = struct.pack +U = struct.unpack + +# notes: +# SCMDraft 2 requires, at bare minimum: +scmdraft_req = b"ERA ~DIM ~TILE~UNIT~PUNI~IOWN~THG2~SPRP~WAV ~MASK~STR ~OWNR~SIDE~FORC".split(b"~") + +# Starcraft 1.18+ Melee requires, at bare minimum: +# absolutely required: +# VER TYPE OWNR SIDE COLR ERA DIM MTXM UNIT THG2 SPRP FORC +# not needed at all: +# IVER IOWN ISOM TILE DD2 +# not required for Melee mode (non-UMS): +# PUPx UPGx PUNI MASK MRGN WAV PTEx UNIx TECx TRIG MBRF UPRP UPUS SWNM +# special cases: +# VCOD partly: 34 19 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1F 29 7D A6 D7 B0 00 BB CC 31 24 ED 17 4C 13 0B +# STR kinda (can be empty for melee) +melee_req = b"VER ~TYPE~VCOD~OWNR~SIDE~COLR~ERA ~DIM ~MTXM~UNIT~THG2~STR ~SPRP~FORC".split(b"~") + +#both_req = set(melee_req + scmdraft_req) +#print(b"~".join(both_req)) + +known_keys = [ # in the order we want to write them. + # boring filetype stuff + b"TYPE", + b"VER ", + b"IVER", + b"IVE2", + b"VCOD", + + b"SPRP", # scenario name and description + b"COLR", # player colors + b"FORC", # player forces and settings + b"SIDE", # player races + b"IOWN", # editor player types + b"OWNR", # ingame player types + + b"DIM ", # map dimensions + b"ERA ", # tileset + b"MTXM", # ingame tiles + b"TILE", # editor tiles + b"ISOM", # editor isometric tiles + b"MASK", # fog of war per tile + b"DD2 ", # editor doodads + b"UNIT", # placed units + b"THG2", # placed sprites (e.g. doodad) + + b"MRGN", # locations + + b"UNIS", # unit settings (a bunch of stuff, including names) + b"UNIx", # broodwar override + + b"PUNI", # build unit restrictions + + b"PTEC", # tech restrictions + b"PTEx", # broodwar override + + b"UPGR", # upgrade restrictions (and defaults, max levels...) + b"PUPx", # broodwar override + + b"TECS", # tech settings (costs and times) + b"TECx", # broodwar override + + b"UPGS", # upgrade settings (costs and times) + b"UPGx", # broodwar override + + b"UPRP", # properties of spawn-with-properties + b"UPUS", # booleans of which properties are used + + b"MBRF", # mission briefings (triggers without conditions) + b"TRIG", # triggers + + b"SWNM", # switch names + b"WAV ", # wav paths (editor only) + b"STR ", # string data +] + +skippable_keys = [ + b"ISOM", + b"PTEC", + b"SWNM", + b"TECS", + b"UNIS", + b"UPGR", + b"UPGS", +] + +vcod_valid = b"\ +\x34\x19\xCA\x77\x99\xDC\x68\x71\x0A\x60\xBF\xC3\xA7\xE7\x75\xA7\ +\x1F\x29\x7D\xA6\xD7\xB0\x3A\xBB\xCC\x31\x24\xED\x17\x4C\x13\x0B\ +\x65\x20\xA2\xB7\x91\xBD\x18\x6B\x8D\xC3\x5D\xDD\xE2\x7A\xD5\x37\ +\xF6\x59\x64\xD4\x63\x9A\x12\x0F\x43\x5C\x2E\x46\xE3\x74\xF8\x2A\ +\x08\x6A\x37\x06\x37\xF6\xD6\x3B\x0E\x94\x63\x16\x45\x67\x5C\xEC\ +\xD7\x7B\xF7\xB7\x1A\xFC\xD4\x9E\x73\xFA\x3F\x8C\x2E\xC0\xE1\x0F\ +\xD1\x74\x09\x07\x95\xE3\x64\xD7\x75\x16\x68\x74\x99\xA7\x4F\xDA\ +\xD5\x20\x18\x1F\xE7\xE6\xA0\xBE\xA6\xB6\xE3\x1F\xCA\x0C\xEF\x70\ +\x31\xD5\x1A\x31\x4D\xB8\x24\x35\xE3\xF8\xC7\x7D\xE1\x1A\x58\xDE\ +\xF4\x05\x27\x43\xBA\xAC\xDB\x07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0E\xE9\x3B\x87\xD4\ +\xF4\x41\xC0\xE3\xA8\x05\x44\xBE\xE4\x0F\x8A\x13\x1A\xC4\x37\xF4\ +\x5A\x40\x55\xEF\x9D\x79\x1D\x4B\x4A\x79\x3A\x9C\x76\x85\x37\xCC\ +\x82\x3D\x0F\xB6\x60\xA6\x93\x7E\xBD\x5C\xC2\xC4\x72\xC7\x7F\x90\ +\x4D\x1B\x96\x10\x13\x05\x68\x68\x35\xC0\x7B\xFF\x46\x85\x43\x2A\ +\x01\x04\x05\x06\x02\x01\x05\x02\x00\x03\x07\x07\x05\x04\x06\x03\ +" + +mrgn_default = bytearray(b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" * 255) +# set up the "Anywhere" location correctly. this is always location 64. +mrgn_default[63*20 + 9] = 0x10 +mrgn_default[63*20 + 13] = 0x10 +mrgn_default[63*20 + 16] = 0x03 +mrgn_default = bytes(mrgn_default) + +def readit(f): + f.seek(0, 2) + fs = f.tell() + f.seek(0) + + src = {} + + while f.tell() < fs: + k = f.read(4) + + try: + name = k.decode('utf-8')#, errors="replace") + except UnicodeDecodeError: + print("! skipping invalid section name:", k) + size = U(" 0: + f.read(size) + continue + + size = U(" 0 and type(strings[0]) != bytes: + strings = [str(s).encode("CP1252") for s in strings] + + flat = b"\0" + b"\0".join(strings) + + offsets = [] + o = count * 2 + 3 + for s in strings: + offsets += [o] + # FIXME: offsets are still broken. + o += len(s) + 1 + + out = P("= 12: + print("# ignoring start location for player", pi) + placed_starts[pi] = True + + k = b"ERA " + if k in src: + for ki, v in enumerate(src[k]): + era = U("force mapping + 0,0,0,0, # strings for forces + 1,1,1,1, # bitmasks: randstart, ally, allyvic, vis, unused... + ) + src[b"FORC"] = [b] + assert len(b) == 20, b + + if b"IOWN" not in src: + # FIXME: possibly invalid. + b = b"" + for i in range(8): + b += b"\6" if placed_starts[i] else b"\0" + b += b"\0\0\0\7" + src[b"IOWN"] = [b] + assert len(b) == 12, b + + if b"MRGN" not in src: + src[b"MRGN"] = [mrgn_default] + + if b"THG2" not in src: + src[b"THG2"] = [b""] + + if b"SPRP" not in src: + src[b"SPRP"] = [b"\0\0\0\0"] + + if b"WAV " not in src: + src[b"WAV "] = [b"\0\0\0\0" * 512] + + #if b"ISOM" not in src: + # size = (w // 2 + 1) * (h + 1) * 4 + # src[b"ISOM"] = [b'\0' * size] + + if b"PUNI" not in src: + src[b"PUNI"] = [b"\1" * 5700] + + done = {} + for k in known_keys: + if k in src: + for v in src[k]: + W(k, v) + done[k] = True + + for k, v in src.items(): + if not done[k]: + print("# deferred write:", k) + W(k, v) + + for k in known_keys: + if k not in skippable_keys and k not in wroteonce: + print("# never wrote section:", k) + + for k in scmdraft_req: + if k not in wroteonce: + print("# map might not open in SCMDraft 2. missing", k) + for k in melee_req: + if k not in wroteonce: + print("# map might not open in StarCraft (Melee). missing", k) + +def run(args): + for fn in args: + with open(fn, 'rb') as f: + src = readit(f) + + if src is None: + print("! failed to rewrite", fn) # FIXME: reuse of terminology. + ofn = fn.replace('.chk', '') + '.x.chk' + assert fn != ofn, ofn + rewrite(src) + with open(ofn, 'wb') as f: + writeit(f, src) + +if __name__ == '__main__': + ret = run(sys.argv[1:]) + sys.exit(ret) From 8ebad01de8cbbec82323a0c1a78366a30b803b65 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 30 Aug 2017 21:44:22 -0700 Subject: [PATCH 2/2] . --- bwmap.py | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/bwmap.py b/bwmap.py index 7792520..3db4e6b 100644 --- a/bwmap.py +++ b/bwmap.py @@ -21,8 +21,10 @@ scmdraft_req = b"ERA ~DIM ~TILE~UNIT~PUNI~IOWN~THG2~SPRP~WAV ~MASK~STR ~OWNR~SID # STR kinda (can be empty for melee) melee_req = b"VER ~TYPE~VCOD~OWNR~SIDE~COLR~ERA ~DIM ~MTXM~UNIT~THG2~STR ~SPRP~FORC".split(b"~") -#both_req = set(melee_req + scmdraft_req) -#print(b"~".join(both_req)) +# TODO: try more things to add to this list. +ums_req = melee_req + b"MRGN~PTEx~PUPx~TECx~TRIG~UNIx~UPGx~UPRP".split(b"~") + +both_req = set(melee_req + scmdraft_req) known_keys = [ # in the order we want to write them. # boring filetype stuff @@ -79,15 +81,7 @@ known_keys = [ # in the order we want to write them. b"STR ", # string data ] -skippable_keys = [ - b"ISOM", - b"PTEC", - b"SWNM", - b"TECS", - b"UNIS", - b"UPGR", - b"UPGS", -] +skippable_keys = set(known_keys).symmetric_difference(both_req) vcod_valid = b"\ \x34\x19\xCA\x77\x99\xDC\x68\x71\x0A\x60\xBF\xC3\xA7\xE7\x75\xA7\ @@ -175,7 +169,7 @@ def readit(f): k = f.read(4) try: - name = k.decode('utf-8')#, errors="replace") + name = k.decode('ascii') except UnicodeDecodeError: print("! skipping invalid section name:", k) size = U("