# codecs/text/KeyValues + data/vdf fixtures: KeyValues text parsed by srctools (Keyvalues.parse, flags $WIN32 on), # random trees written by the python vdf module (vdf.dumps) and binary KeyValues written / read by vdf.binary_dumps. # tests/gen/keyvalues.luau { text = { { data, canon } }, objects = { { data, canon } }, written = { { data, canon } }, binary = { { data, canon } } } # canon: a pair list as "{" .. := ... "}", a value s: | i | f<%.17g> | a block; # binary types prefix their letter (i32 / f32 / ptr / wstr / col / u64 / i64). objects: vdf.loads of the written # trees, in tests/data/Canon form. import importlib.util, json, os _spec = importlib.util.spec_from_file_location("_data_common", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_data_common.py")) C = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(C) TEXT = [ '"root"\n{\n\t"a"\t"1"\n\t"b"\t"two words"\n}\n', '// comment\n"root" { "k" "v" "sub" { "x" "y" } }\n', 'root\n{\n\tkey value\n\tother\t"quoted value"\n\tnested\n\t{\n\t\tdeep 1\n\t}\n}\n', '"r"\n{\n\t"dup"\t"1"\n\t"dup"\t"2"\n\t"blk" { "a" "1" }\n\t"blk" { "a" "2" }\n}\n', '"r"\n{\n\t"x"\t"1"\n\t"x"\t"2"\t[$X360]\n\t"y"\t"pc"\t[$WIN32]\n\t"z"\t"not console"\t[!$X360]\n}\n', '"r"\n{\n\t"esc"\t"a\\tb\\nc\\\\d\\"e"\n}\n', '"r"\n{\n\t"first" "0"\n\t"blk" [$X360]\n\t{\n\t\t"a" "1"\n\t}\n\t"blk2" [$WIN32]\n\t{\n\t\t"b" "2"\n\t}\n}\n', '"r" { "empty" "" "emptyblock" { } }\n', '"a" { "k" "1" }\n"b" { "k" "2" }\n', '"r"\r\n{\r\n\t"crlf"\t"yes"\r\n}\r\n', '"r"\n{\n\t"unicode"\t"é中"\n\t"slash"\t"models/props/x.mdl"\n}\n', '"r"\n{\n\t"c" "1" // trailing comment\n\t// "hidden" "1"\n}\n', '"r"{"tight""1""t2"{"a""b"}}', ] CODE = r''' import json, sys, random from srctools.keyvalues import Keyvalues import vdf FLAGS = {"$win32": True, "win32": True, "$x360": False} inp = json.loads(sys.stdin.read()) def esc(b): return b"%d:" % len(b) + b def canon(kvs): out = bytearray(b"{") for q in kvs: out += esc(q.real_name.encode()) out += b"=" if q.has_children() or isinstance(q.value, list): out += canon(q) else: out += b"s" + esc(q.value.encode()) return bytes(out + b"}") def vcanon(d): out = bytearray(b"{") for k, v in d.items(): out += esc(k.encode()) + b"=" if isinstance(v, dict): out += vcanon(v) elif isinstance(v, vdf.UINT_64): out += b"u64i%d" % int(v) elif isinstance(v, vdf.INT_64): out += b"i64i%d" % int(v) elif isinstance(v, vdf.POINTER): out += b"ptri%d" % int(v) elif isinstance(v, vdf.COLOR): out += b"coli%d" % int(v) elif isinstance(v, int): out += b"i32i%d" % int(v) elif isinstance(v, float): out += b"f32f" + ("%.17g" % v).encode() else: out += b"s" + esc(v.encode()) return bytes(out + b"}") res = {"text": [], "written": [], "binary": []} for t in inp["text"]: kv = Keyvalues.parse(t, flags=FLAGS, single_line=True) res["text"].append({"data": t, "canon": canon(kv).decode("latin1")}) rng = random.Random(5) ALPH = "abcXYZ019_ .-/\\\"\té" def word(n=8): return "".join(rng.choice(ALPH) for _ in range(rng.randrange(1, n))) def tree(d): out = vdf.VDFDict() if False else {} for i in range(rng.randrange(0, 5)): k = word() + str(i) if d < 3 and rng.random() < 0.3: out[k] = tree(d + 1) else: out[k] = word(14) return out for i in range(40): t = {"root%d" % i: tree(0)} s = vdf.dumps(t, pretty=bool(i % 2)) kv = Keyvalues.parse(s, single_line=True) res["written"].append({"data": s, "canon": canon(kv).decode("latin1"), "obj": vdf.loads(s)}) import struct def btree(d): out = {} for i in range(rng.randrange(0, 6)): k = "k%d_%s" % (i, rng.choice(["", "x", "é"])) r = rng.random() if d < 3 and r < 0.2: out[k] = btree(d + 1) elif r < 0.35: out[k] = rng.randrange(-2**31, 2**31) elif r < 0.45: out[k] = struct.unpack("