"""make-emote.py [name] [--slot=N] [--black-background] Adds an emote of our own to swf/HUDAssets.swf, from a picture: the classes "" (the emote over an animal's head and in a chat bubble) and "_sml" (the small one in the emote picker and in chat lines), each a clip holding the picture, the size and place the game's own emotes have. The default name is emo_temp, the face with a thermometer. The game makes an emote by its class name (gui.EmoticonUtility), so the name also has to be in that class's lists: swf-src/EmoticonUtility.cinit.pcode, which tools/build-client.sh puts into the client. Each emote of ours has a slot (--slot=N, 0 by default: emo_temp's), its own character ids and its own code: running it again for a slot replaces what an earlier run put there, and leaves the other slots as they are. The picture must be an 8-bit RGBA PNG, not interlaced; --black-background is for one that came on black where it should be see-through. Needs nothing but Python. """ import lzma import struct import sys import zlib from pathlib import Path ROOT = Path(__file__).resolve().parent.parent HUD = ROOT / "swf" / "HUDAssets.swf" # character ids of ours, far above the file's own (which end near 4,000): # picture, the two shapes, the two clips. FIRST_ID = 60001 ABC_NAME = b"ours_emote" # the game's emotes are drawn about their middle: the large ones 35 by 38 # pixels (emo_sick's shape is 706 by 763 twips), the small ones 19 by 20. LARGE_WIDTH = 706 SMALL_WIDTH = 373 SHRINK = 2 # the picture is kept at half its size, which is still four times the large emote def read_png(path): data = path.read_bytes() if data[:8] != b"\x89PNG\r\n\x1a\n": sys.exit("not a PNG") pos, idat, head = 8, b"", None while pos < len(data): length, kind = struct.unpack(">I4s", data[pos:pos + 8]) body = data[pos + 8:pos + 8 + length] if kind == b"IHDR": head = struct.unpack(">IIBBBBB", body) elif kind == b"IDAT": idat += body pos += 12 + length width, height, depth, colour, _, _, interlace = head if (depth, colour, interlace) != (8, 6, 0): sys.exit("the picture must be 8-bit RGBA and not interlaced") raw = zlib.decompress(idat) stride = width * 4 rows, previous = [], bytearray(stride) for y in range(height): kind = raw[y * (stride + 1)] line = bytearray(raw[y * (stride + 1) + 1:(y + 1) * (stride + 1)]) for i in range(stride): left = line[i - 4] if i >= 4 else 0 up = previous[i] corner = previous[i - 4] if i >= 4 else 0 if kind == 1: line[i] = (line[i] + left) & 255 elif kind == 2: line[i] = (line[i] + up) & 255 elif kind == 3: line[i] = (line[i] + (left + up) // 2) & 255 elif kind == 4: estimate = left + up - corner nearest = min((abs(estimate - left), 0, left), (abs(estimate - up), 1, up), (abs(estimate - corner), 2, corner))[2] line[i] = (line[i] + nearest) & 255 rows.append(line) previous = line return width, height, rows def without_background(width, height, rows, darkest=28): """Makes the black around the drawing see-through: every pixel that dark which can be reached from the picture's edge. Black inside the drawing (eyes, a mouth) is not reached and stays.""" dark = lambda x, y: max(rows[y][x * 4:x * 4 + 3]) <= darkest and rows[y][x * 4 + 3] != 0 todo = [(x, y) for x in range(width) for y in (0, height - 1)] + [(x, y) for y in range(height) for x in (0, width - 1)] while todo: x, y = todo.pop() if 0 <= x < width and 0 <= y < height and dark(x, y): rows[y][x * 4 + 3] = 0 todo += [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)] return width, height, rows def shrunk(width, height, rows, by): """The picture at 1/by of its size, as SWF wants it: ARGB with the colours already multiplied by the alpha.""" out_width, out_height = width // by, height // by out = bytearray() for y in range(out_height): for x in range(out_width): red = green = blue = alpha = 0 for dy in range(by): line = rows[y * by + dy] for dx in range(by): i = (x * by + dx) * 4 a = line[i + 3] red += line[i] * a green += line[i + 1] * a blue += line[i + 2] * a alpha += a count = by * by out += bytes((alpha // count, red // (255 * count), green // (255 * count), blue // (255 * count))) return out_width, out_height, bytes(out) class Bits: def __init__(self): self.bits = [] def unsigned(self, value, count): self.bits += [(value >> (count - 1 - i)) & 1 for i in range(count)] def signed(self, value, count): self.unsigned(value & ((1 << count) - 1), count) def bytes(self): bits = self.bits + [0] * (-len(self.bits) % 8) return bytes(sum(bit << (7 - i) for i, bit in enumerate(bits[n:n + 8])) for n in range(0, len(bits), 8)) def needed(*values): """Bits for the widest of these signed numbers.""" return max(max(abs(value) for value in values).bit_length() + 1, 2) def rect(x1, x2, y1, y2): bits = Bits() count = needed(x1, x2, y1, y2) bits.unsigned(count, 5) for value in (x1, x2, y1, y2): bits.signed(value, count) return bits.bytes() def tag(code, body): if len(body) < 63 and code not in (2, 36, 39): return struct.pack("> 3) + 7) // 8 + 4 tags, pos = [], header while pos < len(body): short = struct.unpack_from("> 6, body[pos:start + length], body[start:start + length])) pos = start + length return body[:header], tags def main(): if not [word for word in sys.argv[1:] if not word.startswith("--")]: sys.exit(__doc__) words = [word for word in sys.argv[1:] if not word.startswith("--")] name = words[1] if len(words) > 1 else "emo_temp" slots = [word for word in sys.argv[1:] if word.startswith("--slot=")] slot = int(slots[0].split("=", 1)[1]) if slots else 0 first_id = FIRST_ID + slot * 5 # slot 0 keeps the name it always had, so that a run for it replaces it abc_name = ABC_NAME if slot == 0 else ABC_NAME + b"_" + str(slot).encode() picture = read_png(Path(words[0])) if "--black-background" in sys.argv: picture = without_background(*picture) width, height, pixels = shrunk(*picture, SHRINK) bitmap, large_shape, small_shape, large, small = range(first_id, first_id + 5) ours = [ tag(36, struct.pack("