# Build engine reference tools for the art / buildmap fixtures: arttool (Jonathon Fowler, Artistic License 2.0) and # map2stl (Ken Silverman) compiled from the nix eduke32 source (nixpkgs#eduke32.src, never committed) with g++ and a # small compat.h stub written here: stdio and a few B* aliases, a NULL-safe Bfclose, and a DO_FREE_AND_NULL that leaks # (arttool's Bstring copy constructor frees an uninitialised pointer). Binaries are cached in tests/gen/.buildtools/. import os, subprocess STUB = r""" #include #include #include #include #include #include #include #include #define Bassert assert #define Bfclose(f) ((f) ? fclose(f) : 0) #define Bfileno fileno #define Bfopen fopen #define Bfprintf fprintf #define Bfputc fputc #define Bfread fread #define Bfseek fseek #define Bfstat fstat #define Bftell ftell #define Bfwrite fwrite #define Bmalloc malloc #define Bprintf printf #define Brealloc realloc #define Brewind rewind #define Bstat stat #define Bstrcat strcat #define Bstrcmp strcmp #define Bstrdup strdup #define Bstrlen strlen #define BFILE FILE #define DO_FREE_AND_NULL(x) do { (x) = NULL; } while (0) #define EDUKE32_PREDICT_FALSE(x) (x) #define ATTRIBUTE(x) """ def tools(ctx): """{"arttool": path, "map2stl": path}""" out = os.path.join(ctx.gen, ".buildtools") paths = {n: os.path.join(out, n) for n in ("arttool", "map2stl")} if all(os.path.exists(p) for p in paths.values()): return paths os.makedirs(out, exist_ok=True) src = subprocess.run(["nix", "build", "--no-link", "--print-out-paths", "nixpkgs#eduke32.src"], capture_output=True, check=True).stdout.decode().strip().split("\n")[-1] stub = os.path.join(out, "stub") os.makedirs(stub, exist_ok=True) with open(os.path.join(stub, "compat.h"), "w") as f: f.write(STUB) for name, path in paths.items(): ctx.nix("gcc", ["g++", "-O1", "-w", "-funsigned-char", "-I" + stub, os.path.join(src, "source/tools/src/%s.cpp" % name), "-o", path]) return paths def pcx_write(w, h, idx, pal): """8-bit RLE PCX (version 5) of row-major indices with a 256 x RGB palette, as arttool reads it""" bpl = w + (w & 1) hd = bytearray(128) hd[0:4] = bytes([10, 5, 1, 8]) hd[8:10] = (w - 1).to_bytes(2, "little") hd[10:12] = (h - 1).to_bytes(2, "little") hd[65] = 1 hd[66:68] = bpl.to_bytes(2, "little") hd[68] = 1 out = bytearray(hd) for y in range(h): row = list(idx[y * w:(y + 1) * w]) + [0] * (bpl - w) i = 0 while i < bpl: v, n = row[i], 1 while i + n < bpl and n < 63 and row[i + n] == v: n += 1 if n > 1 or v >= 0xC0: out.append(0xC0 | n) out.append(v) i += n out.append(12) out += bytes(pal) return bytes(out) def pcx_read(data): """(w, h, row-major indices, 768-byte palette) of an 8-bit RLE PCX""" w = int.from_bytes(data[8:10], "little") - int.from_bytes(data[4:6], "little") + 1 h = int.from_bytes(data[10:12], "little") - int.from_bytes(data[6:8], "little") + 1 bpl = int.from_bytes(data[66:68], "little") raw, p = bytearray(), 128 while len(raw) < bpl * h: c = data[p] p += 1 if c >= 0xC0: raw += bytes([data[p]]) * (c & 63) p += 1 else: raw.append(c) idx = bytearray() for y in range(h): idx += raw[y * bpl:y * bpl + w] return w, h, bytes(idx), data[-768:]