Brought up to date with the private repository (a06d3c5)

This commit is contained in:
S committed 2026-10-14 19:03:13 +03:00
commit eda992fcff
144 files changed
+31026

No files matched your search

+64
View File
@@ -0,0 +1,64 @@
#!/bin/sh
# rebuilds swf/ajclient.swf: the untouched client (swf/ajclient.before-finder.swf)
# with every class in swf-src/ compiled over its original, by JPEXS (tools/ffdec,
# needs Java). the result is the same bytes every time.
#
# tools/build-client.sh [output.swf] (default: swf/ajclient.swf)
#
# A class added to swf-src/ needs a line below: its full name, then its file.
#
# one thing is not a whole class: gui.EmoticonUtility's static initialiser,
# as P-code, with our emote added to its lists (tools/make-emote.py). the
# decompiler gets two of that class's functions wrong, so the class cannot
# be compiled from its source. 4839 is that method body's place in the
# untouched client.
set -e
cd "$(dirname "$(readlink -f "$0")")/.."
out=${1:-swf/ajclient.swf}
set -- \
AJClient AJClient.as \
GlobalErrorCatch GlobalErrorCatch.as \
XtReplyDemuxer XtReplyDemuxer.as \
loader.MediaHelper MediaHelper.as \
com.sbi.graphics.PaletteHelper PaletteHelper.as \
gui.ColorTable ColorTable.as \
gui.MySettings MySettings.as \
gui.itemWindows.ItemWindowToggle ItemWindowToggle.as \
gui.ReportAPlayer ReportAPlayer.as \
gui.ECardCreation ECardCreation.as \
gui.GuiManager GuiManager.as \
gui.PlayerSearch PlayerSearch.as \
avatar.NPCView NPCView.as \
gui.ChatHistory ChatHistory.as \
gui.PredictiveTextManager PredictiveTextManager.as \
avatar.UserCommXtCommManager UserCommXtCommManager.as \
buddy.BuddyCard BuddyCard.as \
gui.itemWindows.ItemWindowPets ItemWindowPets.as \
gui.itemWindows.ItemWindowAvtPetSml ItemWindowAvtPetSml.as \
gui.itemWindows.ItemWindowBuddyList ItemWindowBuddyList.as \
shop.Shop Shop.as \
shop.ShopManager ShopManager.as \
avatar.Avatar Avatar.as \
gui.ChatBalloon ChatBalloon.as \
room.RoomManagerView RoomManagerView.as \
gui.itemWindows.ItemWindowOriginal ItemWindowOriginal.as \
gui.itemWindows.ItemWindowAnimal ItemWindowAnimal.as \
gui.DenAndClothesItemSelect DenAndClothesItemSelect.as \
gui.RecycleItems RecycleItems.as \
gui.AvatarEditor AvatarEditor.as \
gui.PetInventory PetInventory.as \
gui.DenEditor DenEditor.as \
gui.TradeManager TradeManager.as \
avatar.AvatarWorldView AvatarWorldView.as \
room.RoomManagerWorld RoomManagerWorld.as \
gskinner.motion.GTween GTween.as \
playerWall.PostMessage PostMessage.as
args=""
while [ $# -gt 0 ]; do
args="$args $1 swf-src/$2"
shift 2
done
# shellcheck disable=SC2086
java -jar tools/ffdec/ffdec-cli.jar -replace swf/ajclient.before-finder.swf "$out" \
gui.EmoticonUtility swf-src/EmoticonUtility.cinit.pcode 4839 $args | grep -v "^Replac\|EXPERIMENTAL" || true
ls -l "$out"
+27
View File
@@ -0,0 +1,27 @@
// drives the custom client (~/aj-private-client) through a real login and
// saves a screenshot of the game view. it opens a window on the desktop.
//
// AJ_USER=name AJ_PASS=password [WAIT=55000] node tools/client-test.js shot.png
//
// extra arguments go to Electron. uses the Chrome DevTools Protocol.
const {spawn} = require("child_process"); const os = require("os"); const fs = require("fs");
const PORT = 9336; const extra = process.argv.slice(3); const shotPath = process.argv[2];
const child = spawn(os.homedir() + "/aj-private-client/run.sh", [`--remote-debugging-port=${PORT}`, "--enable-logging", ...extra], {stdio: ["ignore", "pipe", "pipe"]});
let log = ""; child.stdout.on("data", d => log += d); child.stderr.on("data", d => log += d);
const sleep = ms => new Promise(r => setTimeout(r, ms));
const connect = async target => { const ws = new WebSocket(target.webSocketDebuggerUrl); await new Promise((res, rej) => { ws.onopen = res; ws.onerror = rej; }); let id = 0; const pending = new Map(); ws.onmessage = m => { const d = JSON.parse(m.data); if (d.id && pending.has(d.id)) { pending.get(d.id)(d); pending.delete(d.id); } }; return (method, params = {}) => new Promise(res => { const i = ++id; pending.set(i, res); ws.send(JSON.stringify({id: i, method, params})); }); };
const targets = async () => (await fetch(`http://127.0.0.1:${PORT}/json`)).json();
const evalIn = async (send, expr) => { const r = await send("Runtime.evaluate", {expression: expr, awaitPromise: true, returnByValue: true}); return r.result.exceptionDetails ? "EXC" : r.result.result.value; };
(async () => {
let list = []; for (let i = 0; i < 40; i++) { try { list = await targets(); if (list.some(t => t.url.includes("index.html"))) break; } catch (e) {} await sleep(300); }
const host = await connect(list.find(t => t.url.includes("index.html")));
await sleep(800);
await evalIn(host, `document.getElementById('username').value=${JSON.stringify(process.env.AJ_USER)}; document.getElementById('password').value=${JSON.stringify(process.env.AJ_PASS)}; document.getElementById('log-in').click()`);
await sleep(Number(process.env.WAIT || 12000));
console.log("game visible:", await evalIn(host, "!document.getElementById('game').hidden"), "status:", await evalIn(host, "document.getElementById('status').textContent"));
const g = (await targets()).find(x => x.type === "webview");
if (g && shotPath !== "-") { const guest = await connect(g); const sh = await guest("Page.captureScreenshot", {format: "png"}); if (sh.result && sh.result.data) fs.writeFileSync(shotPath, Buffer.from(sh.result.data, "base64")); }
child.kill(); await sleep(300);
console.log(log.split("\n").filter(l => /ERROR|plugin|Plugin|flash|Flash|sandbox/i.test(l) && !/GetVSync|Security Warning/.test(l)).slice(0, 12).join("\n").slice(0, 1800));
process.exit(0);
})().catch(e => { console.log("FAILED", e.message); child.kill(); process.exit(1); });
+87
View File
@@ -0,0 +1,87 @@
#!/usr/bin/env python3
"""Finds artwork among the CDN's media files that the game used to be told
about by lists on the official servers: Jam-A-Gram cards, Jammer Wall
wallpapers and wall stickers. Prints the media ids of each.
python3 tools/find-art.py [firstId] [lastId] (default 1 to 9500)
A media file is a SWF, zlib ("CWS") or LZMA ("ZWS") compressed. What tells
them apart is the names of the clips inside:
card a "msgTxt" text field and a "stamp" clip (ecard.ECardImageBase)
wallpaper a "postStatus" clip (playerWall.PlayerWallGui.onWallDecorItemLoaded)
sticker the ones found carry "sticker" in a symbol name
That overshoots a little (a few windows of the game have the same names
inside), so the results were looked at, one picture each, before going into
lib/ecard-art.json and lib/wall-art.json. Two things had to be done by eye
altogether: stamps are plain pictures with nothing to tell them by (ids
10-23 are what sits next to the first cards), and each wallpaper's button in
the wall's "customize" popup is a separate file, usually numbered right after
or right before its batch of wallpapers.
It downloads every file in the range (about 250 MB) without keeping any.
"""
import concurrent.futures
import hashlib
import lzma
import struct
import sys
import urllib.request
import zlib
CONTENT = "https://ajcontent.akamaized.net/"
DEPLOY_VERSION = "1830"
def hashed(name):
"""LoaderCache.hashItV2, as in lib/cdn.js."""
salted = "W3 7r4Ck h4X0r3rs" + name
out = ""
for index, char in enumerate(salted):
out = out + char if index % 2 == 0 else char + out
return hashlib.md5(out.encode()).hexdigest()
def inflate(data):
if data[:3] == b"CWS":
return zlib.decompress(data[8:])
if data[:3] == b"ZWS":
# ZWS: "ZWS", version, uncompressed length, compressed length, LZMA
# properties, data. the .lzma format wants properties, length, data.
size = struct.unpack("<I", data[4:8])[0] - 8
return lzma.decompress(data[12:17] + struct.pack("<Q", size) + data[17:], format=lzma.FORMAT_ALONE)
return data
def kinds(media_id):
url = "%s%s/media/%s?v=3" % (CONTENT, DEPLOY_VERSION, hashed(str(media_id)))
try:
request = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
body = inflate(urllib.request.urlopen(request, timeout=60).read())
except Exception:
return media_id, []
found = []
if b"postStatus" in body:
found.append("wallpaper")
elif b"msgTxt" in body and b"stamp" in body:
found.append("card")
if b"sticker" in body and b"postStatus" not in body:
found.append("sticker")
return media_id, found
def main():
first = int(sys.argv[1]) if len(sys.argv) > 1 else 1
last = int(sys.argv[2]) if len(sys.argv) > 2 else 9500
results = {"card": [], "wallpaper": [], "sticker": []}
with concurrent.futures.ThreadPoolExecutor(16) as pool:
for media_id, found in pool.map(kinds, range(first, last + 1)):
for kind in found:
results[kind].append(media_id)
for kind, ids in results.items():
print(kind, ids)
if __name__ == "__main__":
main()
+85
View File
@@ -0,0 +1,85 @@
import sys, pickle, os
import xml.etree.ElementTree as ET
here=os.path.dirname(os.path.abspath(__file__))
pk=os.path.join(here,"hud.pickle")
if os.path.exists(pk):
D=pickle.load(open(pk,"rb"))
else:
root=ET.parse(os.path.join(here,"hud.xml")).getroot()
tags=root.find("tags")
D={"sprites":{}, "shapes":{}, "texts":{}, "names":{}}
def rect(e):
return tuple(int(e.get(k))/20 for k in ("Xmin","Ymin","Xmax","Ymax"))
for it in tags:
t=it.get("type")
if t=="SymbolClassTag":
ids=[int(x.text) for x in it.find("tags")]
names=[x.text for x in it.find("names")]
for i,n in zip(ids,names): D["names"][n]=i
elif t=="DefineSpriteTag":
sid=int(it.get("spriteId"))
frames=[]; cur={}; labels={}
disp={}
fi=0
for st in it.find("subTags"):
tt=st.get("type")
if tt in ("PlaceObject2Tag","PlaceObject3Tag"):
depth=int(st.get("depth"))
prev=disp.get(depth,{})
o=dict(prev)
if st.get("placeFlagHasCharacter")=="true": o["cid"]=int(st.get("characterId"))
if st.get("placeFlagHasName")=="true": o["name"]=st.get("name")
m=st.find("matrix")
if st.get("placeFlagHasMatrix")=="true" and m is not None:
o["x"]=int(m.get("translateX"))/20; o["y"]=int(m.get("translateY"))/20
o["sx"]=float(m.get("scaleX")) if m.get("hasScale")=="true" else 1.0
o["sy"]=float(m.get("scaleY")) if m.get("hasScale")=="true" else 1.0
disp[depth]=o
elif tt=="RemoveObject2Tag":
disp.pop(int(st.get("depth")),None)
elif tt=="FrameLabelTag":
labels[st.get("name")]=fi
elif tt=="ShowFrameTag":
frames.append({d:dict(o) for d,o in disp.items()}); fi+=1
D["sprites"][sid]={"frames":frames,"labels":labels}
elif t and t.startswith("DefineShape"):
D["shapes"][int(it.get("shapeId"))]=rect(it.find("shapeBounds"))
elif t=="DefineEditTextTag":
D["texts"][int(it.get("characterID"))]=(rect(it.find("bounds")), it.get("variableName"), it.get("initialText"), it.get("fontHeight"))
pickle.dump(D,open(pk,"wb"))
def bounds(cid, frame=0, seen=()):
if cid in D["shapes"]: return D["shapes"][cid]
if cid in D["texts"]: return D["texts"][cid][0]
sp=D["sprites"].get(cid)
if not sp or not sp["frames"] or cid in seen: return None
b=None
for o in sp["frames"][min(frame,len(sp["frames"])-1)].values():
if "cid" not in o: continue
cb=bounds(o["cid"],0,seen+(cid,))
if not cb: continue
x=o.get("x",0); y=o.get("y",0); sx=o.get("sx",1); sy=o.get("sy",1)
xs=sorted([x+cb[0]*sx,x+cb[2]*sx]); ys=sorted([y+cb[1]*sy,y+cb[3]*sy])
r=(xs[0],ys[0],xs[1],ys[1])
b=r if b is None else (min(b[0],r[0]),min(b[1],r[1]),max(b[2],r[2]),max(b[3],r[3]))
return b
def fmt(b): return "?" if not b else "[%.0f,%.0f %.0fx%.0f]"%(b[0],b[1],b[2]-b[0],b[3]-b[1])
def dump(cid, depth, maxd, frame=0, ind=""):
sp=D["sprites"].get(cid)
if not sp: return
if sp["labels"]: print(ind+" labels:", ",".join(f"{k}@{v}" for k,v in list(sp["labels"].items())[:30]), "frames", len(sp["frames"]))
for d,o in sorted(sp["frames"][min(frame,len(sp["frames"])-1)].items()):
c=o.get("cid"); kind="sprite" if c in D["sprites"] else "text" if c in D["texts"] else "shape"
extra=""
if kind=="text": extra=" text=%r h=%s"%(D["texts"][c][2], D["texts"][c][3])
print(f'{ind} d{d} {o.get("name","-")} ({kind} {c}) at {o.get("x",0):.0f},{o.get("y",0):.0f} scale {o.get("sx",1):.2f},{o.get("sy",1):.2f} bounds {fmt(bounds(c))}{extra}')
if kind=="sprite" and depth<maxd: dump(c, depth+1, maxd, 0, ind+" ")
if __name__=="__main__":
name=sys.argv[1]; maxd=int(sys.argv[2]) if len(sys.argv)>2 else 2; frame=sys.argv[3] if len(sys.argv)>3 else "0"
if name=="--list":
for n in sorted(D["names"]):
if maxd==0 or sys.argv[3].lower() in n.lower(): print(n, D["names"][n], fmt(bounds(D["names"][n])))
sys.exit()
cid=D["names"][name]; sp=D["sprites"][cid]
f=sp["labels"].get(frame, int(frame) if frame.isdigit() else 0)
print(name,"id",cid,"bounds",fmt(bounds(cid,f)),"frame",f)
dump(cid,0,maxd,f)
+168
View File
@@ -0,0 +1,168 @@
"""transplant-nametag.py old.xml now.xml out.xml
Puts the name tags of the March 2014 HUDAssets.swf into today's. Both files
are given as the XML `ffdec-cli -swf2xml` writes; the result goes back
through `-xml2swf`. Everything is found by symbol and instance name, so it
does not depend on either file's character ids.
What changes, and only this:
member tag The 2014 left cap and bar ("e" and "n" of memberNameBar, a
clip each with one hand-drawn frame per colour) are copied
in under new ids and added as a child named "old" on top of
what today's "e" and "m" already hold. Nothing of today's is
taken out: avatar.NameBar reaches into those two for a child
called "mouse", and no login survives its absence. The old
clips carry the same offsets inside as today's parts, so
they go in at 0,0. swf-src/hud/memberNameBar.as shows the
right frame and hides today's drawing.
picker The ten samples in the animal editor (namebar_<colour>Cont):
the "up" and "mouse" drawings inside each one's "mouse" clip
are swapped for the 2014 ones, which sit at the same places.
free tag Its drawing has not changed since 2014, pixel for pixel; the
name on it was in the regular weight then and is bold now.
The text field goes back to the regular font.
"""
import re
import sys
import xml.etree.ElementTree as ET
old_path, now_path, out_path = sys.argv[1:4]
COLOURS = ["black", "blue", "brown", "gold", "green", "pink", "purple", "red", "teal", "white"]
OLD_DEPTH = 40 # above the parts already in today's cap and bar
ID_ATTRS = ("spriteId", "shapeId", "characterId", "characterID", "bitmapId", "fontId", "buttonId")
def read(path):
"""-> (definitions by character id, character id by symbol name)"""
tags = ET.parse(path).getroot().find("tags")
defs, names = {}, {}
for item in tags:
kind = item.get("type", "")
if kind == "SymbolClassTag":
ids = [entry.text for entry in item.find("tags")]
for cid, name in zip(ids, (entry.text for entry in item.find("names"))):
names[name] = cid
elif kind.startswith("Define"):
for attr in ("spriteId", "shapeId", "characterID", "buttonId", "fontId"):
if item.get(attr):
defs.setdefault(item.get(attr), item)
return defs, names
def child(defs, sprite_id, name):
"""Character id of the child called `name` on a sprite's first frame."""
for tag in defs[sprite_id].find("subTags"):
if tag.get("type") == "ShowFrameTag":
break
if tag.get("name") == name and tag.get("characterId"):
return tag.get("characterId")
raise SystemExit(f"sprite {sprite_id} has no child called {name}")
old, old_names = read(old_path)
now, now_names = read(now_path)
wanted = {}
old_tag, now_tag = old_names["memberNameBar"], now_names["memberNameBar"]
add_old = { # today's sprite -> the 2014 clip it gets as "old"
child(now, now_tag, "e"): child(old, old_tag, "e"),
child(now, now_tag, "m"): child(old, old_tag, "n"),
}
retarget = {} # (today's sprite, child name) -> the 2014 character to place there
for colour in COLOURS:
symbol = f"namebar_{colour}Cont"
old_states = child(old, old_names[symbol], "mouse")
now_states = child(now, now_names[symbol], "mouse")
for state in ("up", "mouse"):
retarget[(now_states, state)] = child(old, old_states, state)
for cid in list(add_old.values()) + list(retarget.values()):
wanted[cid] = True
def references(element):
found = set()
for sub in element.iter():
if sub is element:
continue
for attr in ("characterId", "bitmapId", "fontId"):
value = sub.get(attr)
if value and value != "65535":
found.add(value)
return found
order, seen = [], set()
def visit(cid):
if cid in seen:
return
if cid not in old:
raise SystemExit(f"the 2014 file has no definition for character {cid}")
seen.add(cid)
for ref in sorted(references(old[cid]), key=int):
visit(ref)
order.append(cid) # what a thing is made of comes before it
for cid in wanted:
visit(cid)
kinds = {}
for cid in order:
kinds[old[cid].get("type")] = kinds.get(old[cid].get("type"), 0) + 1
print("copying", len(order), "definitions:", kinds)
text = open(now_path, encoding="utf8").read()
highest = max(int(value) for value in re.findall(r'(?:spriteId|shapeId|characterID|buttonId|fontId|characterId)="(\d+)"', text) if int(value) < 60000)
new_id = {cid: str(highest + 1 + index) for index, cid in enumerate(order)}
chunks = []
for cid in order:
element = old[cid]
for sub in element.iter():
for attr in ID_ATTRS:
if sub.get(attr) in new_id:
sub.set(attr, new_id[sub.get(attr)])
chunks.append(ET.tostring(element, encoding="unicode").strip())
def sprite_span(source, sprite_id):
"""(start, end) of a sprite's definition in the XML text."""
start = re.search(r'[ \t]*<item type="DefineSpriteTag"[^>]*spriteId="%s"[^>]*>' % sprite_id, source)
assert start, f"sprite {sprite_id} not found in today's file"
indent = re.match(r'[ \t]*', start.group(0)).group(0)
closing = "\n" + indent + "</item>"
return start.start(), source.index(closing, start.end()) + len(closing)
free_text = child(now, child(now, now_names["FreeNameBar"], "c"), "txt")
field = re.search(r'<item type="DefineEditTextTag"[^>]*characterID="%s"[^>]*>' % free_text, text)
assert field and 'fontClass="CCDigitalDeliveryBold"' in field.group(0)
plain = field.group(0).replace('fontClass="CCDigitalDeliveryBold"', 'fontClass="CCDigitalDeliveryRegular"').replace("&lt;b&gt;", "").replace("&lt;/b&gt;", "")
text = text[:field.start()] + plain + text[field.end():]
for (sprite_id, name), old_cid in retarget.items():
start, end = sprite_span(text, sprite_id)
sprite = text[start:end]
pattern = re.compile(r'(<item type="PlaceObject[23]Tag"[^>]*?)characterId="\d+"([^>]*?name="%s")' % name)
sprite, count = pattern.subn(lambda match: f'{match.group(1)}characterId="{new_id[old_cid]}"{match.group(2)}', sprite)
assert count == 1, (sprite_id, name, count)
text = text[:start] + sprite + text[end:]
for sprite_id, old_cid in add_old.items():
start, end = sprite_span(text, sprite_id)
show = text.index('<item type="ShowFrameTag"', start)
assert show < end
place = ('<item type="PlaceObject2Tag" characterId="%s" depth="%d" forceWriteAsLong="true" name="old" '
'placeFlagHasCharacter="true" placeFlagHasClipActions="false" placeFlagHasClipDepth="false" '
'placeFlagHasColorTransform="false" placeFlagHasMatrix="true" placeFlagHasName="true" '
'placeFlagHasRatio="false" placeFlagMove="false">'
'<matrix type="MATRIX" hasRotate="false" hasScale="false" nRotateBits="0" nScaleBits="0" '
'nTranslateBits="0" translateX="0" translateY="0"/></item>\n ') % (new_id[old_cid], OLD_DEPTH)
text = text[:show] + place + text[show:]
first = min(sprite_span(text, sprite_id)[0] for sprite_id in list(add_old) + [key[0] for key in retarget])
indent = re.match(r'[ \t]*', text[first:]).group(0)
text = text[:first] + "".join(indent + chunk + "\n" for chunk in chunks) + text[first:]
open(out_path, "w", encoding="utf8").write(text)
print("written", out_path)
+362
View File
@@ -0,0 +1,362 @@
#!/usr/bin/env python3
"""Makes the pictures of the badges the admin gives (lib/badges.js), both
forms of each from one outline:
swf/badges/<id>.swf the name tag badge: the outline alone, in grey,
which the name tag tints with its own colour
public/badges/<id>.png the badge on player cards: the outline in white on
a round disc of the badge's colour
python3 tools/make-badges.py <workdir>
Needs Java, for tools/ffdec, and nothing else. The workdir gets the files
in between and sheet.png, every badge at a size to look at.
A name tag badge is a small media file: an unseen square to point at
("iconBA") and one shape. This takes the game's star badge (media 700,
from cache/), swaps the star's outline for another and names the symbol
after it.
An outline is a list of closed paths of straight edges, drawn below in any
units with y downwards; each is a solid piece or a hole in one. Paths must
not cross or overlap each other.
"""
import hashlib
import math
import os
import re
import struct
import subprocess
import sys
import zlib
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
FFDEC = os.path.join(HERE, "ffdec", "ffdec-cli.jar")
DEPLOY_VERSION = "1830"
STAR = "700"
TAG_HALF = 6.0 # how far a name tag badge reaches from its middle, in pixels: 80% of the star's 7.5
CARD_SIZE = 48 # the card's picture, drawn at a third of this
CARD_HALF = 12.5 # how far the outline reaches from the middle of that
def hashed(name):
"""LoaderCache.hashItV2, as in lib/cdn.js."""
salted = "W3 7r4Ck h4X0r3rs" + name
out = ""
for index, char in enumerate(salted):
out = out + char if index % 2 == 0 else char + out
return hashlib.md5(out.encode()).hexdigest()
def arc(cx, cy, radius, start, end, steps):
"""Points along a circle, angles in degrees, y downwards."""
return [(cx + radius * math.cos(math.radians(start + (end - start) * step / steps)),
cy + radius * math.sin(math.radians(start + (end - start) * step / steps))) for step in range(steps + 1)]
def circle(cx, cy, radius, steps=16):
return arc(cx, cy, radius, 0, 360, steps)[:-1]
def curve(start, control, end, steps):
"""Points along a quadratic curve, without its start."""
points = []
for step in range(1, steps + 1):
t = step / steps
points.append(tuple((1 - t) ** 2 * a + 2 * (1 - t) * t * b + t ** 2 * c for a, b, c in zip(start, control, end)))
return points
def mirrored(points):
return [(-x, y) for x, y in reversed(points)]
def turned(points, degrees):
turn = math.radians(degrees)
return [(x * math.cos(turn) - y * math.sin(turn), x * math.sin(turn) + y * math.cos(turn)) for x, y in points]
def bar(x1, y1, x2, y2, width):
"""A straight stroke from one point to another."""
length = math.hypot(x2 - x1, y2 - y1)
nx, ny = -(y2 - y1) / length * width / 2, (x2 - x1) / length * width / 2
return [(x1 + nx, y1 + ny), (x2 + nx, y2 + ny), (x2 - nx, y2 - ny), (x1 - nx, y1 - ny)]
# each returns [(points, is_hole), ...].
def crown():
return [([(-6.4, 5.6), (-7.5, -3.6), (-3.4, 0.2), (0, -6), (3.4, 0.2), (7.5, -3.6), (6.4, 5.6)], False)]
def shield():
right = [(0, -7.2), (6.2, -5), (6.2, 0.5)] + curve((6.2, 0.5), (6, 5), (0, 7.4), 6)
return [(right + [(-x, y) for x, y in reversed(right[1:-1])], False)]
def wrench():
# an open-ended spanner standing upright, then turned: a round head
# with a slot in the top, and a handle with a round end.
head, slot, grip = 4.3, 1.7, 1.75
reach = math.degrees(math.asin(slot / head)) # where the slot's sides meet the head's edge
join = math.degrees(math.asin(grip / head))
points = [(-slot, -4.2)]
points += arc(0, -4, head, 270 - reach, 90 + join, 10)
points += arc(0, 7.2, grip, 180, 0, 6)
points += arc(0, -4, head, 90 - join, -90 + reach, 10)
points += [(slot, -4.2)]
return [(turned(points, 45), False)]
def brackets():
# "</>"
left = [(-3.4, -5.4), (-8.4, 0), (-3.4, 5.4), (-3.4, 2.6), (-5.8, 0), (-3.4, -2.6)]
return [(left, False), (mirrored(left), False), ([(0.5, -6.4), (2.3, -6.4), (-0.5, 6.4), (-2.3, 6.4)], False)]
def brush():
handle = [(-1.2, -9), (1.2, -9), (1.5, 0.4), (2.1, 0.4), (2.1, 3), (-2.1, 3), (-2.1, 0.4), (-1.5, 0.4)]
tip = [(-2.1, 3.9), (2.1, 3.9)] + curve((2.1, 3.9), (3, 7), (0, 9.4), 5) + curve((0, 9.4), (-3, 7), (-2.1, 3.9), 5)[:-1]
return [(turned(handle, 40), False), (turned(tip, 40), False)]
def question():
cx, cy, outer, inner = 0, -3.4, 4.6, 2.2
hook = arc(cx, cy, outer, 180, 405, 14) + [(1.2, 1.2), (1.2, 3.2), (-1.2, 3.2), (-1.2, 0.2)] + arc(cx, cy, inner, 405, 180, 12)
return [(hook, False), (circle(0, 6.3, 1.5, 10), False)]
def bug():
body = [(x, 1.2 + y) for x, y in zip([4.2 * math.cos(math.radians(a)) for a in range(0, 360, 20)],
[5.4 * math.sin(math.radians(a)) for a in range(0, 360, 20)])]
head = arc(0, -4.9, 2.7, 180, 360, 8)
legs = [bar(4.9, -0.8, 7.9, -2.8, 1.4), bar(5.2, 1.6, 8.3, 1.6, 1.4), bar(4.9, 4, 7.9, 6, 1.4)]
feelers = [bar(1.4, -7.8, 3.2, -9.6, 1.2)]
pieces = [body, head] + legs + [mirrored(leg) for leg in legs] + feelers + [mirrored(feeler) for feeler in feelers]
return [(piece, False) for piece in pieces]
def trophy():
right = [(0, -7), (4.6, -7), (4.6, -3.4)] + curve((4.6, -3.4), (4.4, 1.2), (1.2, 2.4), 5) + [(1.2, 4.4), (3.8, 4.4), (3.8, 7), (0, 7)]
cup = right + [(-x, y) for x, y in reversed(right[1:-1])]
handle = arc(5.5, -4.2, 2.6, -90, 90, 8) + arc(5.5, -4.2, 1.3, 90, -90, 6)
return [(cup, False), (handle, False), (mirrored(handle), False)]
def flask():
glass = [(-2.6, -7.6), (2.6, -7.6), (2.6, -6.2), (1.7, -6.2), (1.7, -1.8), (6.4, 5.8), (5.4, 7.6), (-5.4, 7.6), (-6.4, 5.8),
(-1.7, -1.8), (-1.7, -6.2), (-2.6, -6.2)]
return [(glass, False), (circle(1.2, 3.4, 1.4, 10), True), (circle(-2, 5.2, 0.9, 8), True)]
def smiley():
smile = arc(0, -0.6, 5.2, 20, 160, 10) + arc(0, -0.6, 3.6, 160, 20, 8)
return [(circle(0, 0, 7.5, 28), False), (circle(-2.7, -2.4, 1.25, 10), True), (circle(2.7, -2.4, 1.25, 10), True), (smile, True)]
def heart_points(scale, dy=0.0):
right = [(0, -3.2)] + curve((0, -3.2), (1.2, -6.8), (4.4, -6.6), 5) + curve((4.4, -6.6), (7.8, -6.2), (7.4, -2.2), 5) \
+ curve((7.4, -2.2), (6.8, 2.4), (0, 7.2), 7)
points = right + [(-x, y) for x, y in reversed(right[1:-1])]
return [(x * scale, y * scale + dy) for x, y in points]
def heart():
# an outlined heart: the game has a solid one among its own badges.
return [(heart_points(1), False), (heart_points(0.5, -0.2), True)]
def tick():
return [([(-7.4, 0.6), (-4.8, -2.2), (-2, 0.8), (5, -6.4), (7.6, -3.8), (-2, 6.2)], False)]
def sprout():
stem = [(-0.8, 2.3), (0.8, 2.3), (0.8, 8), (-0.8, 8)]
small = [(-0.4, 1.7)] + curve((-0.4, 1.7), (-1.4, -4.4), (-7.4, -3.6), 7) + curve((-7.4, -3.6), (-6.6, 1.9), (-0.4, 1.7), 7)[:-1]
large = [(0.4, 1.7)] + curve((0.4, 1.7), (7.6, 1.6), (7.8, -7.6), 8) + curve((7.8, -7.6), (0, -7.4), (0.4, 1.7), 8)[:-1]
return [(stem, False), (small, False), (large, False)]
# id, outline, and the card disc's colours: top, bottom, rim.
BADGES = [
("owner", crown, "#ffd95a", "#f0a11c", "#96600a"),
("moderator", shield, "#5cb2ff", "#1f6fd6", "#12458a"),
("contributor", wrench, "#6fd878", "#2f9e3a", "#19621f"),
("developer", brackets, "#8a9ab0", "#4d5b70", "#283241"),
("artist", brush, "#ffa066", "#ea6524", "#8f3a10"),
("helper", question, "#5fd9cf", "#1fa398", "#0f615a"),
("bughunter", bug, "#f77873", "#d03b36", "#7f1c19"),
("eventwinner", trophy, "#d9a273", "#9c5f2a", "#5e3613"),
("tester", flask, "#c6e45e", "#8fb81f", "#55700d"),
("friend", smiley, "#7fd6ff", "#2ba6e0", "#136a94"),
("supporter", heart, "#ff8fa8", "#e8476d", "#93203c"),
("partner", tick, "#b28cff", "#7a4fe0", "#44288f"),
("early", sprout, "#58b874", "#1f7a3d", "#0f4a22"),
]
def fitted(paths, half):
"""Centred, and reaching `half` from the middle."""
xs = [x for points, _ in paths for x, _ in points]
ys = [y for points, _ in paths for _, y in points]
cx, cy = (min(xs) + max(xs)) / 2, (min(ys) + max(ys)) / 2
scale = half / max(max(xs) - cx, max(ys) - cy)
return [([((x - cx) * scale, (y - cy) * scale) for x, y in points], hole) for points, hole in paths]
def area(points):
return sum(x1 * y2 - x2 * y1 for (x1, y1), (x2, y2) in zip(points, points[1:] + points[:1]))
def records(paths, clockwise):
"""The outline as SWF shape records, in twips. A solid piece goes round
the way the star does and a hole the other way, which puts the fill on
the same side of every edge."""
out, xs, ys = [], [], []
for points, hole in fitted(paths, TAG_HALF):
twips = [(round(x * 20), round(y * 20)) for x, y in points]
if (area(twips) > 0) != (clockwise != hole):
twips.reverse()
out.append('<item type="StyleChangeRecord" fillStyle1="1" moveBits="15" moveDeltaX="%d" moveDeltaY="%d" stateFillStyle0="false" '
'stateFillStyle1="true" stateLineStyle="false" stateMoveTo="true" stateNewStyles="false"/>' % twips[0])
for (x1, y1), (x2, y2) in zip(twips, twips[1:] + twips[:1]):
if (x1, y1) != (x2, y2):
out.append('<item type="StraightEdgeRecord" deltaX="%d" deltaY="%d" generalLineFlag="true" numBits="13" vertLineFlag="false"/>'
% (x2 - x1, y2 - y1))
xs += [x for x, _ in twips]
ys += [y for _, y in twips]
out.append('<item type="EndShapeRecord" endOfShape="0"/>')
bounds = '<shapeBounds type="RECT" Xmax="%d" Xmin="%d" Ymax="%d" Ymin="%d" nbits="15"/>' % (max(xs), min(xs), max(ys), min(ys))
return bounds, "\n".join(out)
def star_points(shape):
"""The star's own outline, to learn which way round it goes."""
move = re.search(r'moveDeltaX="(-?\d+)" moveDeltaY="(-?\d+)"', shape)
x, y = int(move.group(1)), int(move.group(2))
points = [(x, y)]
for edge in re.findall(r'<item type="StraightEdgeRecord"[^>]*>', shape):
dx, dy = re.search(r'deltaX="(-?\d+)"', edge), re.search(r'deltaY="(-?\d+)"', edge)
x, y = x + (int(dx.group(1)) if dx else 0), y + (int(dy.group(1)) if dy else 0)
points.append((x, y))
return points[:-1]
def inside(paths, x, y):
"""Is the point in the outline? An odd number of crossings is in, which
makes a hole of a path inside another."""
crossings = 0
for points, _ in paths:
for (x1, y1), (x2, y2) in zip(points, points[1:] + points[:1]):
if (y1 > y) != (y2 > y) and x < x1 + (y - y1) * (x2 - x1) / (y2 - y1):
crossings += 1
return crossings % 2 == 1
def rgb(colour):
return tuple(int(colour[i:i + 2], 16) for i in (1, 3, 5))
def card_pixels(paths, top, bottom, rim):
"""CARD_SIZE square of (r, g, b, a): a disc shaded from `top` to
`bottom` with a `rim`, the outline in white on it. Sixteen samples to a
pixel."""
paths = fitted(paths, CARD_HALF)
top, bottom, rim = rgb(top), rgb(bottom), rgb(rim)
middle, fill_radius, rim_radius = CARD_SIZE / 2, 20.0, 23.0
rows = []
for py in range(CARD_SIZE):
row = []
for px in range(CARD_SIZE):
total = [0.0, 0.0, 0.0, 0.0]
for sy in range(4):
for sx in range(4):
x, y = px + (sx + 0.5) / 4 - middle, py + (sy + 0.5) / 4 - middle
distance = math.hypot(x, y)
if distance > rim_radius:
continue
if distance > fill_radius:
colour = rim
elif inside(paths, x, y):
colour = (255, 255, 255)
else:
shade = (y + fill_radius) / (2 * fill_radius)
colour = tuple(a + (b - a) * shade for a, b in zip(top, bottom))
for channel in range(3):
total[channel] += colour[channel]
total[3] += 1
row.append(tuple(round(total[c] / total[3]) if total[3] else 0 for c in range(3)) + (round(total[3] / 16 * 255),))
rows.append(row)
return rows
def png(rows):
raw = b"".join(b"\x00" + bytes(value for pixel in row for value in pixel) for row in rows)
def chunk(kind, data):
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data))
return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", len(rows[0]), len(rows), 8, 6, 0, 0, 0)) \
+ chunk(b"IDAT", zlib.compress(raw, 9)) + chunk(b"IEND", b"")
def sheet(cards, outlines):
"""Every badge to look at: the card picture, the same at the size the
card draws it, and the name tag outline on a tag-like colour."""
cell, small = CARD_SIZE + 4, CARD_SIZE // 3
backgrounds = [(119, 119, 119), (233, 228, 212), (43, 43, 51), (242, 201, 76)]
rows = [[backgrounds[(y // cell) % 4] + (255,) for _ in range(cell * len(cards))] for y in range(cell * 4)]
def put(x0, y0, pixels):
for dy, row in enumerate(pixels):
for dx, (r, g, b, a) in enumerate(row):
under = rows[y0 + dy][x0 + dx]
rows[y0 + dy][x0 + dx] = tuple(round((c * a + u * (255 - a)) / 255) for c, u in zip((r, g, b), under)) + (255,)
for index, (card, paths) in enumerate(zip(cards, outlines)):
put(index * cell + 2, 2, card)
shrunk = [[tuple(round(sum(card[y * 3 + j][x * 3 + i][c] * card[y * 3 + j][x * 3 + i][3] for j in range(3) for i in range(3))
/ max(1, sum(card[y * 3 + j][x * 3 + i][3] for j in range(3) for i in range(3)))) for c in range(3))
+ (round(sum(card[y * 3 + j][x * 3 + i][3] for j in range(3) for i in range(3)) / 9),)
for x in range(small)] for y in range(small)]
put(index * cell + 2 + small, cell + small, shrunk)
put(index * cell + 2 + small, cell * 3 + small, shrunk)
# the name tag outline, three times its size, in a light tint.
big = fitted(paths, TAG_HALF * 3)
tag = [[(250, 240, 200, 255 if inside(big, x + 0.5 - cell / 2, y + 0.5 - cell / 2) else 0) for x in range(cell)] for y in range(cell)]
put(index * cell, cell * 2, tag)
return rows
def main():
work = sys.argv[1]
os.makedirs(work, exist_ok=True)
os.makedirs(os.path.join(ROOT, "swf", "badges"), exist_ok=True)
os.makedirs(os.path.join(ROOT, "public", "badges"), exist_ok=True)
star_xml = os.path.join(work, "star.xml")
subprocess.run(["java", "-jar", FFDEC, "-swf2xml", os.path.join(ROOT, "cache", DEPLOY_VERSION, "media", hashed(STAR)), star_xml],
check=True, stdout=subprocess.DEVNULL)
xml = open(star_xml, encoding="utf-8").read()
# the second shape is the star; the first is the square behind it.
shape = re.search(r'<item type="DefineShapeTag"[^>]*shapeId="3">.*?</item>\s*<item type="DefineSpriteTag"', xml, re.S).group(0)
clockwise = area(star_points(shape)) > 0
cards, outlines = [], []
for name, outline, top, bottom, rim in BADGES:
paths = outline()
bounds, edges = records(paths, clockwise)
changed = re.sub(r'<shapeBounds [^>]*/>', bounds, shape, count=1)
changed = re.sub(r'<shapeRecords>.*?</shapeRecords>', "<shapeRecords>\n" + edges + "\n</shapeRecords>", changed, count=1, flags=re.S)
made = xml.replace(shape, changed).replace("iconStar", "icon" + name.capitalize())
made_xml = os.path.join(work, name + ".xml")
open(made_xml, "w", encoding="utf-8").write(made)
out = os.path.join(ROOT, "swf", "badges", name + ".swf")
subprocess.run(["java", "-jar", FFDEC, "-xml2swf", made_xml, out], check=True, stdout=subprocess.DEVNULL)
card = card_pixels(paths, top, bottom, rim)
open(os.path.join(ROOT, "public", "badges", name + ".png"), "wb").write(png(card))
cards.append(card)
outlines.append(paths)
print(name, os.path.getsize(out), "bytes")
open(os.path.join(work, "sheet.png"), "wb").write(png(sheet(cards, outlines)))
if __name__ == "__main__":
main()
+162
View File
@@ -0,0 +1,162 @@
#!/usr/bin/env python3
"""Makes swf/biotab.swf, the artwork of the Bio tab on player cards.
python3 tools/make-bio-tab.py <workdir>
The card (media 4620, from cache/) has two tabs that slide out of its left
side: the buddy games' at the top and the trade list's at the bottom. Each
is a brown frame with an ear on its left edge, and a blue button on the
ear. The Bio tab goes between them, so it needs a frame with its ear in
the middle, and a button with no picture on it (the card code puts
public/bio-icon.png there). This makes both from the card's own artwork
and leaves everything else in the file as it is, so that the card code can
load the result as it loads the card and take the tab out of it, button
class and all:
- the games tab's frame is drawn again with the ear EAR_DOWN pixels
lower, and its top left corner rounded as its bottom left one is;
- the trade list's button loses its arrows: each of its four pictures is
a stack of layers, the arrows being the last six;
- the games tab gets that button in place of its own, on the ear.
Needs Java, for tools/ffdec.
"""
import hashlib
import os
import re
import subprocess
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
FFDEC = os.path.join(HERE, "ffdec", "ffdec-cli.jar")
DEPLOY_VERSION = "1830"
CARD = "4620"
FRAME_SHAPE = 366 # the games tab's frame
GAMES_TAB = 372
GAMES_BUTTON = 294
TRADE_BUTTON = 312
# the trade button's pictures, and how many layers of each are the button itself.
BUTTON_LAYERS = {307: 4, 309: 4, 305: 3, 303: 3} # up, mouse over, down, grey
EAR_DOWN = 66 # the games tab's ear is at the top; this far down is midway to the trade list's
def hashed(name):
"""LoaderCache.hashItV2, as in lib/cdn.js."""
salted = "W3 7r4Ck h4X0r3rs" + name
out = ""
for index, char in enumerate(salted):
out = out + char if index % 2 == 0 else char + out
return hashlib.md5(out.encode()).hexdigest()
def frame_path(inset, d):
"""The frame's outline, `inset` pixels in from its edge, as ("M" | "L",
x, y) and ("Q", control x, control y, x, y). The numbers are the
original's, the ear's with `d` added."""
if inset:
left, ear_left, top, bottom, right = -78, -130.9, -97.5, 97.5, 131
ear_top, ear_bottom = -84.8 + d, -27.9 + d
ear = [("Q", -119.8, ear_top, -125.4, -79.2 + d), ("Q", ear_left, -73.7 + d, ear_left, -65.8 + d), ("L", ear_left, -46.9 + d),
("Q", ear_left, -39.0 + d, -125.4, -33.4 + d), ("Q", -119.8, ear_bottom, -112, ear_bottom)]
corner = [("Q", left, 88.8, -73.5, 93.1), ("Q", -69.2, bottom, -63, bottom)]
else:
left, ear_left, top, bottom, right = -80, -132.9, -99.5, 99.5, 133
ear_top, ear_bottom = -86.8 + d, -25.9 + d
ear = [("Q", -120.7, ear_top, -126.8, -80.7 + d), ("Q", ear_left, -74.5 + d, ear_left, -65.8 + d), ("L", ear_left, -46.9 + d),
("Q", ear_left, -38.1 + d, -126.8, -32.0 + d), ("Q", -120.7, ear_bottom, -112, ear_bottom)]
corner = [("Q", left, 89.5, -75, 94.5), ("Q", -70, bottom, -63, bottom)]
# the top left corner, which the original's ear ran into: the bottom one, upside down.
(_, c1x, c1y, m1x, m1y), (_, c2x, c2y, _, _) = corner
top_corner = [("Q", c2x, -c2y, m1x, -m1y), ("Q", c1x, -c1y, left, -82.5)]
return [("M", left, ear_top), ("L", -112, ear_top)] + ear + [("L", left, ear_bottom), ("L", left, 82.5)] + corner \
+ [("L", right, bottom), ("L", right, top), ("L", -63, top)] + top_corner + [("L", left, ear_top)]
def backwards(path):
points = [(step[-2], step[-1]) for step in path]
out = [("M",) + points[-1]]
for index in range(len(path) - 1, 0, -1):
step = path[index]
out.append(step[:-2] + points[index - 1])
return out
def path_records(path, first):
"""SWF shape records for a path, in twips. `first` is put into the
record that moves to its start."""
out, x, y = [], 0, 0
twips = lambda value: round(value * 20)
for step in path:
if step[0] == "M":
x, y = twips(step[1]), twips(step[2])
out.append('<item type="StyleChangeRecord" %s moveBits="15" moveDeltaX="%d" moveDeltaY="%d" stateLineStyle="false" '
'stateMoveTo="true" stateNewStyles="false"/>' % (first, x, y))
elif step[0] == "L":
nx, ny = twips(step[1]), twips(step[2])
if (nx, ny) != (x, y):
out.append('<item type="StraightEdgeRecord" deltaX="%d" deltaY="%d" generalLineFlag="true" numBits="13" vertLineFlag="false"/>'
% (nx - x, ny - y))
x, y = nx, ny
else:
cx, cy, nx, ny = (twips(value) for value in step[1:])
out.append('<item type="CurvedEdgeRecord" anchorDeltaX="%d" anchorDeltaY="%d" controlDeltaX="%d" controlDeltaY="%d" numBits="13"/>'
% (nx - cx, ny - cy, cx - x, cy - y))
x, y = nx, ny
return out
def shape_tag(xml, shape_id):
return re.search(r'<item type="DefineShape\d?Tag"[^>]*shapeId="%d">.*?</shapes>\s*</item>' % shape_id, xml, re.S).group(0)
def new_frame(xml):
"""The frame as two layers, as the original has it: the brown inside,
then the dark rim around it (the outline, with the inside as a hole)."""
tag = shape_tag(xml, FRAME_SHAPE)
# the record between the layers, which carries the second layer's colour.
new_styles = re.search(r'<item type="StyleChangeRecord"[^>]*stateNewStyles="true">.*?</lineStyles>\s*</item>', tag, re.S).group(0)
fill, keep = 'fillStyle0="1" stateFillStyle0="true" stateFillStyle1="false"', 'stateFillStyle0="false" stateFillStyle1="false"'
inner, outer = frame_path(2, EAR_DOWN), frame_path(0, EAR_DOWN)
records = path_records(inner, fill) + [new_styles] + path_records(outer, fill) + path_records(backwards(inner), keep) \
+ ['<item type="EndShapeRecord" endOfShape="0"/>']
return xml.replace(tag, re.sub(r'<shapeRecords>.*</shapeRecords>', "<shapeRecords>\n" + "\n".join(records) + "\n</shapeRecords>",
tag, count=1, flags=re.S))
def without_picture(xml, shape_id, layers):
"""A button picture with only its first `layers` layers."""
tag = shape_tag(xml, shape_id)
starts = [match.start() for match in re.finditer(r'<item type="StyleChangeRecord"[^>]*stateNewStyles="true"', tag)]
cut = starts[layers - 1] # the record that begins the layer after the last one kept
end = tag.rindex('<item type="EndShapeRecord"')
return xml.replace(tag, tag[:cut] + tag[end:])
def main():
work = sys.argv[1]
os.makedirs(work, exist_ok=True)
card_xml = os.path.join(work, "card.xml")
subprocess.run(["java", "-jar", FFDEC, "-swf2xml", os.path.join(ROOT, "cache", DEPLOY_VERSION, "media", hashed(CARD)), card_xml],
check=True, stdout=subprocess.DEVNULL)
xml = open(card_xml, encoding="utf-8").read()
xml = new_frame(xml)
for shape_id, layers in BUTTON_LAYERS.items():
xml = without_picture(xml, shape_id, layers)
# the games tab's button: the trade list's, on the ear where it is now.
tab = re.search(r'<item type="DefineSpriteTag"[^>]*spriteId="%d">.*?</subTags>' % GAMES_TAB, xml, re.S).group(0)
place = re.search(r'<item type="PlaceObject2Tag" characterId="%d"[^>]*name="gameTabBtn"[^>]*>\s*<matrix [^>]*/>' % GAMES_BUTTON, tab).group(0)
moved = place.replace('characterId="%d"' % GAMES_BUTTON, 'characterId="%d"' % TRADE_BUTTON)
y = int(re.search(r'translateY="(-?\d+)"', place).group(1))
moved = moved.replace('translateY="%d"' % y, 'translateY="%d"' % (y + EAR_DOWN * 20))
xml = xml.replace(tab, tab.replace(place, moved))
made_xml = os.path.join(work, "biotab.xml")
open(made_xml, "w", encoding="utf-8").write(xml)
out = os.path.join(ROOT, "swf", "biotab.swf")
subprocess.run(["java", "-jar", FFDEC, "-xml2swf", made_xml, out], check=True, stdout=subprocess.DEVNULL)
print(out, os.path.getsize(out), "bytes")
if __name__ == "__main__":
main()
+309
View File
@@ -0,0 +1,309 @@
"""make-emote.py <picture.png> [name] [--black-background]
Adds an emote of our own to swf/HUDAssets.swf, from a picture: the classes
"<name>" (the emote over an animal's head and in a chat bubble) and
"<name>_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.
Running it again replaces what an earlier run added. 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("<H", code << 6 | len(body)) + body
return struct.pack("<HI", code << 6 | 63, len(body)) + body
def picture_shape(shape_id, bitmap_id, pixels_wide, pixels_high, width):
"""A rectangle filled with the picture, `width` twips across and with
its middle at 0,0."""
height = round(width * pixels_high / pixels_wide)
left, top = -(width // 2), -(height // 2)
right, bottom = left + width, top + height
bits = Bits()
# the fill's matrix: twips per pixel of the picture, as 16.16, and where
# the picture's corner goes.
scale = round(width / pixels_wide * 65536)
bits.unsigned(1, 1)
bits.unsigned(needed(scale), 5)
bits.signed(scale, needed(scale))
bits.signed(scale, needed(scale))
bits.unsigned(0, 1)
count = needed(left, top)
bits.unsigned(count, 5)
bits.signed(left, count)
bits.signed(top, count)
matrix = bits.bytes()
# one fill style: 0x41, a picture drawn once and smoothed. no lines.
styles = bytes([1, 0x41]) + struct.pack("<H", bitmap_id) + matrix + bytes([0])
bits = Bits()
bits.unsigned(1, 4) # bits in a fill number
bits.unsigned(0, 4) # ... and a line number
# go to the corner, with the fill on the right-hand side.
bits.unsigned(0, 1)
bits.unsigned(0b00101, 5) # fill style 1 and a move
count = needed(left, top)
bits.unsigned(count, 5)
bits.signed(left, count)
bits.signed(top, count)
bits.unsigned(1, 1)
for dx, dy in ((width, 0), (0, height), (-width, 0), (0, -height)):
length = needed(dx, dy)
bits.unsigned(1, 1)
bits.unsigned(1, 1) # a straight edge
bits.unsigned(length - 2, 4)
bits.unsigned(0, 1) # along one axis only
bits.unsigned(1 if dx == 0 else 0, 1)
bits.signed(dy if dx == 0 else dx, length)
bits.unsigned(0, 6)
return tag(2, struct.pack("<H", shape_id) + rect(left, right, top, bottom) + styles + bits.bytes())
def clip(sprite_id, shape_id):
place = tag(26, bytes([0x06]) + struct.pack("<HH", 1, shape_id) + bytes([0])) # the shape at depth 1, where it is
return tag(39, struct.pack("<HH", sprite_id, 1) + place + tag(1, b"") + tag(0, b""))
def abc(names):
"""A class for each name, each `extends flash.display.MovieClip` and
nothing else: what Flash writes for a library symbol with a class."""
strings = ["", *names, "flash.display", "MovieClip", "Object", "flash.events", "EventDispatcher", "DisplayObject", "InteractiveObject",
"DisplayObjectContainer", "Sprite"]
at = {text: index + 1 for index, text in enumerate(strings)}
out = bytearray(struct.pack("<HH", 16, 46))
out += bytes([0, 0, 0]) # no integers, unsigned integers or doubles
out.append(len(strings) + 1)
for text in strings:
out += bytes([len(text)]) + text.encode()
# namespaces: 1 the top level, 2 flash.display, 3 flash.events (0x16 is a package).
out += bytes([4, 0x16, at[""], 0x16, at["flash.display"], 0x16, at["flash.events"]])
out.append(0) # no namespace sets
chain = [(1, "Object"), (3, "EventDispatcher"), (2, "DisplayObject"), (2, "InteractiveObject"), (2, "DisplayObjectContainer"), (2, "Sprite"),
(2, "MovieClip")]
qnames = [(1, name) for name in names] + chain
multiname = {name: index + 1 for index, (_, name) in enumerate(qnames)}
out.append(len(qnames) + 1)
for namespace, name in qnames:
out += bytes([0x07, namespace, at[name]])
# for each class its static initialiser, its constructor and the
# script that defines it; none takes or returns anything.
count = len(names)
out.append(count * 3)
out += bytes([0, 0, 0, 0]) * (count * 3)
out.append(0) # no metadata
out.append(count)
for index, name in enumerate(names):
out += bytes([multiname[name], multiname["MovieClip"], 0, 0, index * 3 + 1, 0]) # dynamic, no interfaces, no traits
for index in range(count):
out += bytes([index * 3, 0])
out.append(count)
for index, name in enumerate(names):
out += bytes([index * 3 + 2, 1, multiname[name], 0x04, 1, index]) # one trait: the class
bodies = []
for index, name in enumerate(names):
bodies.append((index * 3, 1, 9, 10, bytes([0xD0, 0x30, 0x47])))
bodies.append((index * 3 + 1, 1, 10, 11, bytes([0xD0, 0x30, 0xD0, 0x49, 0x00, 0x47]))) # super()
code = bytearray([0xD0, 0x30, 0x65, 0x00])
for _, parent in chain:
code += bytes([0x60, multiname[parent], 0x30]) # each class it comes from, onto the scope
code += bytes([0x60, multiname["MovieClip"], 0x58, index])
code += bytes([0x1D] * len(chain))
code += bytes([0x68, multiname[name], 0x47])
bodies.append((index * 3 + 2, 2, 1, 2 + len(chain), bytes(code)))
out.append(len(bodies))
for method, stack, scope, max_scope, code in bodies:
out += bytes([method, stack, 1, scope, max_scope, len(code)]) + code + bytes([0, 0])
return bytes(out)
def split(body):
"""The header of an unpacked SWF's body, then its tags as (code, bytes)."""
header = (5 + 4 * (body[0] >> 3) + 7) // 8 + 4
tags, pos = [], header
while pos < len(body):
short = struct.unpack_from("<H", body, pos)[0]
length, start = short & 63, pos + 2
if length == 63:
length, start = struct.unpack_from("<I", body, pos + 2)[0], pos + 6
tags.append((short >> 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"
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("<HBHH", bitmap, 5, width, height) + zlib.compress(pixels, 9)),
picture_shape(large_shape, bitmap, width, height, LARGE_WIDTH),
picture_shape(small_shape, bitmap, width, height, SMALL_WIDTH),
clip(large, large_shape),
clip(small, small_shape),
tag(82, struct.pack("<I", 1) + ABC_NAME + b"\0" + abc([name, f"{name}_sml"])),
tag(76, struct.pack("<H", 2) + struct.pack("<H", large) + name.encode() + b"\0" + struct.pack("<H", small) + f"{name}_sml".encode() + b"\0"),
]
packed = HUD.read_bytes()
kind, version = packed[:3], packed[3]
if kind == b"ZWS":
filters = [lzma._decode_filter_properties(lzma.FILTER_LZMA1, packed[12:17])]
body = lzma.LZMADecompressor(format=lzma.FORMAT_RAW, filters=filters).decompress(packed[17:], struct.unpack("<I", packed[4:8])[0] - 8)
elif kind == b"CWS":
body = zlib.decompress(packed[8:])
else:
body = packed[8:]
header, tags = split(body)
def is_ours(code, whole, inner):
if code in (2, 36, 39):
return FIRST_ID <= struct.unpack_from("<H", inner)[0] < FIRST_ID + 5
if code == 82:
return inner[4:4 + len(ABC_NAME) + 1] == ABC_NAME + b"\0"
return code == 76 and struct.unpack_from("<H", inner, 2)[0] == large
kept = [entry for entry in tags if not is_ours(*entry)]
first_frame = next(index for index, (code, _, _) in enumerate(kept) if code == 1)
out = header + b"".join(whole for _, whole, _ in kept[:first_frame]) + b"".join(ours) + b"".join(whole for _, whole, _ in kept[first_frame:])
HUD.write_bytes(b"CWS" + bytes([version]) + struct.pack("<I", len(out) + 8) + zlib.compress(out, 9))
print(f"{name}: a {width} by {height} picture, {len(tags) - len(kept)} old tags of ours taken out, {len(ours)} put in; {HUD.name} is {HUD.stat().st_size} bytes")
main()
+32
View File
@@ -0,0 +1,32 @@
#!/bin/sh
# makes a folder to copy to the machine that will host the server: the code,
# the asset cache, and a clean copy of the database. nothing else of data/
# goes: not the logs (months of game messages, with what players typed and
# enough to work some passwords out), not the recordings of the official
# game, not the old accounts.json.
#
# tools/pack-for-vps.sh <new folder>
#
# the database is copied with SQLite's own VACUUM INTO, so it is whole and
# small whether or not the server is running. tools/ is left out but for
# what the running server may be asked to use.
set -e
cd "$(dirname "$(readlink -f "$0")")/.."
out=${1:?usage: tools/pack-for-vps.sh <new folder>}
[ -e "$out" ] && { echo "$out is already there" >&2; exit 1; }
mkdir -p "$out/data" "$out/tools"
cp -r server.js config.js package.json lib public swf client-updates "$out/"
cp tools/reset-admin-password.js "$out/tools/"
cp -r cache "$out/cache"
node -e '
const {DatabaseSync} = require("node:sqlite");
const db = new DatabaseSync(process.argv[1]);
db.exec("PRAGMA busy_timeout = 5000");
db.prepare("VACUUM INTO ?").run(process.argv[2]);
' data/game.sqlite "$out/data/game.sqlite"
[ -d data/masterpieces ] && cp -r data/masterpieces "$out/data/masterpieces"
chmod -R go-rwx "$out/data"
find "$out" -type d -exec chmod u+rwx {} +
find "$out" -type f -exec chmod u+rw,a-x {} +
echo "Packed into $out:"
du -sh "$out"/* | sed 's/^/ /'
+83
View File
@@ -0,0 +1,83 @@
#!/usr/bin/env bash
# copies this repository as it stands at its last commit, minus what stays
# private, into the public one and pushes it:
#
# tools/publish-public.sh [--no-push]
#
# no markdown file of any kind goes public. the public repository has a
# history of its own: nothing of this one's
# history goes there, so a file that was ever committed here and is private
# never shows up in an old public commit. its .gitignore names the private
# paths too, so they are not added there by hand either.
set -euo pipefail
name=burrow-server
remote=[email protected]:burrow-dev/$name.git
# what stays private: the game's own client and SWFs, the client decompiled,
# the client updates (release files), and the working notes
private=(swf swf-src client-updates HANDOFF.md DISCOVERIES.md GOING-PUBLIC.md)
here=$(cd "$(dirname "$0")/.." && pwd)
public=${BURROW_PUBLIC_DIR:-$HOME/burrow-public/$name}
push=1
[ "${1:-}" = "--no-push" ] && push=0
cd "$here"
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
echo "commit first: the public copy is made from the last commit" >&2
exit 1
fi
source=$(git rev-parse --short HEAD)
ssh_command=$(git config --get core.sshCommand || true)
if [ ! -d "$public/.git" ]; then
mkdir -p "$public"
git -C "$public" init -q -b main
git -C "$public" remote add origin "$remote"
fi
git -C "$public" config user.name "$(git config user.name)"
git -C "$public" config user.email "$(git config user.email)"
[ -n "$ssh_command" ] && git -C "$public" config core.sshCommand "$ssh_command"
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
git archive HEAD | tar -x -C "$work"
for path in "${private[@]}"; do
rm -rf "${work:?}/$path"
done
# no markdown file goes public, of any kind (the owner's rule)
find "$work" -name '*.md' -delete
# a last look before anything leaves: none of the game's code or SWFs
leaked=$(find "$work" \( -name '*.swf' -o -name '*.as' -o -name '*.pem' -o -name '*.md' \) -print)
if [ -n "$leaked" ]; then
echo "refused: these would have gone public:" >&2
echo "$leaked" >&2
exit 1
fi
{
cat "$work/.gitignore"
printf '\n# kept in the private repository, never here\n'
printf '*.md\n'
for path in "${private[@]}"; do
printf '/%s\n' "$path"
done
} > "$work/.gitignore.public"
mv "$work/.gitignore.public" "$work/.gitignore"
# the public tree becomes exactly this
find "$public" -mindepth 1 -maxdepth 1 ! -name .git -exec rm -rf {} +
cp -a "$work/." "$public/"
cd "$public"
git add -A
if git diff --cached --quiet; then
echo "the public copy is already up to date"
exit 0
fi
git commit -q -m "Brought up to date with the private repository ($source)"
echo "committed in $public: $(git log -1 --format='%h %s')"
if [ "$push" = 1 ]; then
git push -q -u origin main
echo "pushed to $remote"
fi
+15
View File
@@ -0,0 +1,15 @@
"use strict";
// forgets the admin panel's password, so the panel asks for a new one the
// next time it is opened. restart the server afterwards: from another machine
// the panel will want the setup code it prints at start.
//
// node tools/reset-admin-password.js
const path = require("path");
const {DatabaseSync} = require("node:sqlite");
const config = require("../config.js");
const db = new DatabaseSync(path.join(config.dataDir, "game.sqlite"));
const result = db.prepare("DELETE FROM settings WHERE key = 'admin_password'").run();
console.log(result.changes ? "The admin password is forgotten. Restart the server, then open /admin." : "There was no admin password.");
+49
View File
@@ -0,0 +1,49 @@
"use strict";
// writes out the files a room file carries inside it.
//
// node tools/room-assets.js <outdir> <room/path>...
// node tools/room-assets.js /tmp/scenes pvp_tictactoe/room_main
//
// A room file (roomDefs/<path>.xroom: zlib, then AMF3) holds its artwork as
// SWFs in `assets`, each with a name and its bytes (`ba`); its layers say
// which asset each one shows. A minigame's scene is such a room, and the
// SWF on its "theGame" layer has code of its own: half the rules of each
// buddy game are in there (lib/buddygames.js). to read it:
//
// java -jar tools/ffdec/ffdec-cli.jar -export script <dir> <the .swf>
const fs = require("fs");
const path = require("path");
const zlib = require("zlib");
const amf3 = require("../lib/amf3.js");
const cdn = require("../lib/cdn.js");
(async () => {
const [outDir, ...rooms] = process.argv.slice(2);
if (!outDir || rooms.length === 0) {
console.log("usage: node tools/room-assets.js <outdir> <room/path>...");
process.exit(1);
}
for (const room of rooms) {
const body = await cdn.get(cdn.hashedPath(`roomDefs/${room}.xroom`));
if (!body) {
console.log(`${room}: no such room file`);
continue;
}
const decoded = amf3.decode(zlib.inflateSync(body));
const dir = path.join(outDir, room.replace(/\//g, "_"));
fs.mkdirSync(dir, {recursive: true});
for (const asset of decoded.assets) {
const name = String(asset.name).replace(/[\\/: ]/g, "_");
if (Buffer.isBuffer(asset.ba)) {
fs.writeFileSync(path.join(dir, name), asset.ba);
}
console.log(`${room}: ${name} ${Buffer.isBuffer(asset.ba) ? `${asset.ba.length} bytes` : "(no bytes)"}`);
}
for (const layer of decoded.actors.ActorLayer || []) {
console.log(`${room}: layer "${layer.name}" shows ${layer.assetName || layer.asset || "?"}`);
}
}
process.exit(0);
})();
+131
View File
@@ -0,0 +1,131 @@
"use strict";
// lists every version of the lands' room files the CDN still has: for each
// room, which builds share a version, the big pictures inside it, and when
// its newest artwork was saved. this is how the older designs in the Theme
// list were found (DISCOVERIES.md, "Older designs of the lands").
//
// node tools/room-history.js <workdir>
//
// downloads about 1.9 GB and keeps the 400 MB that is distinct, in
// <workdir>/files, so a second run only reads. A build is dated by the
// newest sale start in its item pack, which is a few weeks ahead of it.
const crypto = require("crypto");
const fs = require("fs");
const path = require("path");
const zlib = require("zlib");
const amf3 = require("../lib/amf3.js");
const cdn = require("../lib/cdn.js");
const config = require("../config.js");
const OLDEST_BUILD = 1600;
const LANDS = /^(appondale|aussie|balloosh|bahari_bay|canyon_path|coral_canyons|coralpets|crystal_sands|jamaa_township|lost_temple_of_zios|mountains_of_shivveer|oceans|pirate_ship|sarepia|r?holidays_and_celebrations)\//;
const BIG_PICTURE = 60000; // bytes; smaller JPEGs are patches and signs
const WORKERS = 12;
const tail = assetPath => cdn.hashedPath(assetPath).split("/").slice(1).join("/");
const download = async (build, assetPath) => {
const url = new URL(`${build}/${tail(assetPath)}`, config.upstreamContent);
url.searchParams.set("v", config.upstreamBuildVersion);
for (let attempt = 0; attempt < 4; attempt++) {
try {
const res = await fetch(url, {headers: {"User-Agent": "Mozilla/5.0"}});
if (res.status === 404 || res.status === 403) {
return null;
}
if (res.ok) {
return Buffer.from(await res.arrayBuffer());
}
}
catch (err) {
// tried again below
}
}
throw new Error(`could not fetch ${assetPath} of build ${build}`);
};
const day = time => new Date(time).toISOString().slice(0, 10);
(async () => {
const workDir = process.argv[2];
if (!workDir) {
console.log("usage: node tools/room-history.js <workdir>");
process.exit(1);
}
const filesDir = path.join(workDir, "files");
const indexFile = path.join(workDir, "index.json");
fs.mkdirSync(filesDir, {recursive: true});
// {builds: {build: newest sale start}, rooms: {room: {build: hash of its file}}}
let index = fs.existsSync(indexFile) ? JSON.parse(fs.readFileSync(indexFile)) : null;
if (!index) {
index = {builds: {}, rooms: {}};
const jobs = [];
const candidates = Array.from({length: Number(config.deployVersion) - OLDEST_BUILD + 1}, (unused, at) => OLDEST_BUILD + at);
await Promise.all(candidates.map(async build => {
const rooms = await download(build, "defPacks/1011");
if (!rooms) {
return;
}
const items = Object.values(amf3.decode(zlib.inflateRawSync(await download(build, "defPacks/1000"))));
index.builds[build] = day(1000 * Math.max(...items.map(def => Number(def.availabilityStartTime) || 0)));
for (const def of Object.values(amf3.decode(zlib.inflateRawSync(rooms)))) {
if (def && LANDS.test(def.pathName)) {
jobs.push([build, String(def.pathName)]);
}
}
}));
console.log(`${Object.keys(index.builds).length} builds, ${jobs.length} room files`);
const worker = async () => {
while (jobs.length > 0) {
const [build, room] = jobs.pop();
const body = await download(build, `roomDefs/${room}.xroom`);
const hash = body ? crypto.createHash("sha1").update(body).digest("hex").slice(0, 12) : null;
if (body && !fs.existsSync(path.join(filesDir, hash))) {
fs.writeFileSync(path.join(filesDir, hash), body);
}
(index.rooms[room] = index.rooms[room] || {})[build] = hash;
}
};
await Promise.all(Array.from({length: WORKERS}, worker));
fs.writeFileSync(indexFile, JSON.stringify(index));
}
console.log("Builds, by the newest sale start in each:");
console.log(Object.entries(index.builds).map(([build, date]) => ` ${build} ${date}`).join("\n"));
const described = new Map();
const describe = hash => {
if (!described.has(hash)) {
const room = amf3.decode(zlib.inflateSync(fs.readFileSync(path.join(filesDir, hash))));
const pictures = room.assets.filter(asset => /\.jpe?g$/i.test(asset.name) && Buffer.isBuffer(asset.ba) && asset.ba.length > BIG_PICTURE)
.map(asset => String(asset.name).split("\\").slice(-2).join("/")).sort();
const newest = Math.max(0, ...room.assets.map(asset => Number(new Date(asset.timeStamp)) || 0));
described.set(hash, {pictures: pictures.join(" + ") || "(no big picture)", newest: newest ? day(newest) : "?", assets: room.assets.length});
}
return described.get(hash);
};
for (const room of Object.keys(index.rooms).sort()) {
// versions with the same big pictures are one line: they differ in
// decorations, which change monthly.
const looks = new Map();
for (const build of Object.keys(index.rooms[room]).sort()) {
const hash = index.rooms[room][build];
if (hash) {
const {pictures, newest, assets} = describe(hash);
const look = looks.get(pictures) || looks.set(pictures, {builds: [], newest, assets: new Set()}).get(pictures);
look.builds.push(build);
look.newest = newest > look.newest ? newest : look.newest;
look.assets.add(assets);
}
}
console.log(`\n${room}`);
for (const [pictures, look] of looks) {
console.log(` ${pictures}\n builds ${look.builds.join(" ")}; ${Math.min(...look.assets)}-${Math.max(...look.assets)} assets; newest artwork ${look.newest}`);
}
}
})().catch(err => {
console.error(err);
process.exit(1);
});
+11
View File
@@ -0,0 +1,11 @@
const path=require("path");const SRV=process.argv[2];
const cdn=require(path.join(SRV,"lib/cdn.js")),zlib=require("zlib"),amf=require(path.join(SRV,"lib/amf3.js"));
(async()=>{for(const p of process.argv.slice(3)){const b=await cdn.get(cdn.hashedPath("roomDefs/"+p+".xroom"));if(!b){console.log("==",p,"MISSING");continue}const o=amf.decode(zlib.inflateSync(b));
const ts=o.assets.map(a=>Number(a.timeStamp)).filter(Boolean).sort((a,b)=>a-b);const d=t=>new Date(t>1e12?t:t*1000).toISOString().slice(0,10);
console.log("==",p,"assets",o.assets.length,"newest asset",ts.length?d(ts[ts.length-1]):"?", "oldest",ts.length?d(ts[0]):"?");
const L=o.actors.ActorLayer||[];
if(process.env.KEYS){console.log(" layer keys:",Object.keys(L[0]).join(","));console.log(" settings:",JSON.stringify(o.actors.ActorSettings[0],(k,v)=>Buffer.isBuffer(v)?"bytes":v).slice(0,900))}
const names=[...new Set([...o.assets.map(a=>a.name),...L.map(l=>l.name)])].filter(n=>/hallow|spook|pumpk|phantom|night|snow|winter|jamaalid|xmas|christ|holiday|friend|heart|valent|summer|beach|freedom|firework|lucky|clover|feast|leaf|spring|egg|birthday|season/i.test(String(n)));
console.log(" seasonal-looking names:",names.join(", ").slice(0,700)||"(none)");
if(process.env.ALL)console.log(" all layers:",L.map(l=>l.name).join(", ").slice(0,3000));
}})().catch(e=>console.log("ERR",e.stack));
+88
View File
@@ -0,0 +1,88 @@
"use strict";
// collects when each item was on sale, from every build of the defPacks the
// CDN still serves, into lib/sale-dates.json (see lib/sales.js for what
// reads it).
//
// node tools/sale-history.js
//
// an item's def carries one sale window, availabilityStartTime to
// availabilityEndTime, and the official game rewrote it whenever the item
// came back: Halloween items have a window each October, and so on. any one
// build only has the latest. old builds stay on the CDN under their own
// number (35 of them between 1636 and 1830 when this was written), so
// reading them all gives each item's windows over about four years.
//
// downloads about 10 MB and keeps none of it.
const fs = require("fs");
const path = require("path");
const zlib = require("zlib");
const amf3 = require("../lib/amf3.js");
const cdn = require("../lib/cdn.js");
const config = require("../config.js");
const PACKS = {clothing: 1000, denItems: 1030, diamond: 1054};
const OLDEST_BUILD = 1600;
const DAY = 86400;
const OUT = path.join(__dirname, "..", "lib", "sale-dates.json");
const fetchPack = async (build, id) => {
const tail = cdn.hashedPath(`defPacks/${id}`).split("/").slice(1).join("/");
const url = new URL(`${build}/${tail}`, config.upstreamContent);
url.searchParams.set("v", config.upstreamBuildVersion);
for (let attempt = 0; attempt < 3; attempt++) {
try {
const res = await fetch(url, {headers: {"User-Agent": "Mozilla/5.0"}});
if (res.status === 404 || res.status === 403) {
return null;
}
if (res.ok) {
return Object.values(amf3.decode(zlib.inflateRawSync(Buffer.from(await res.arrayBuffer()))));
}
}
catch (err) {
// tried again below
}
}
throw new Error(`could not fetch defPack ${id} of build ${build}`);
};
(async () => {
const candidates = [];
for (let build = OLDEST_BUILD; build <= Number(config.deployVersion); build++) {
candidates.push(build);
}
const found = await Promise.all(candidates.map(async build => (await fetchPack(build, PACKS.clothing) ? build : null)));
const builds = found.filter(Boolean);
console.log(`${builds.length} builds on the CDN: ${builds[0]} to ${builds[builds.length - 1]}`);
const out = {builds: [builds[0], builds[builds.length - 1]]};
for (const [name, id] of Object.entries(PACKS)) {
const windows = new Map(); // def id -> Set of "startDay,endDay"
for (const build of builds) {
for (const def of await fetchPack(build, id) || []) {
const start = Number(def.availabilityStartTime) || 0;
const end = Number(def.availabilityEndTime) || 0;
// only a window with both ends says "this is when it is sold".
if (start > 0 && end > start) {
const key = String(def.id);
windows.set(key, (windows.get(key) || new Set()).add(`${Math.floor(start / DAY)},${Math.ceil(end / DAY)}`));
}
}
}
out[name] = Object.fromEntries([...windows].sort((one, other) => Number(one[0]) - Number(other[0]))
.map(([key, set]) => [key, [...set].map(window => window.split(",").map(Number)).sort((one, other) => one[0] - other[0])]));
const counts = Object.values(out[name]).map(list => list.length);
console.log(`${name}: ${counts.length} items with sale windows, ${counts.reduce((sum, count) => sum + count, 0)} windows`);
}
// one item a line: the file is committed, and this keeps its diffs readable.
const body = Object.entries(out).map(([name, value]) => (Array.isArray(value)
? ` ${JSON.stringify(name)}: ${JSON.stringify(value)}`
: ` ${JSON.stringify(name)}: {\n${Object.entries(value).map(([id, list]) => ` ${JSON.stringify(id)}: ${JSON.stringify(list)}`).join(",\n")}\n }`));
fs.writeFileSync(OUT, `{\n${body.join(",\n")}\n}\n`);
console.log(`wrote ${OUT} (${fs.statSync(OUT).size} bytes)`);
})().catch(err => {
console.error(err.message);
process.exit(1);
});
+321
View File
@@ -0,0 +1,321 @@
#!/usr/bin/env python3
"""Builds lib/shop-stock.json: what each shop sells, from the AJ Classic Wiki.
python3 tools/shop-stock.py <workdir> [<output file>]
The game's files say which shop list stands in which room and nothing about
what is on its shelves: the official server sent that. The wiki
(animaljam.fandom.com) has it. Every item page there carries the game's own
def id (the sort key of "Clothing by ID" / "Den Items by ID"), the category
of each shop selling it now ("Jam Mart Clothing") and of each that used to
("Formerly Jam Mart Clothing"). Shops that are gone, the party shops mostly,
list their items on the shop's own page instead.
LISTS below says which wiki shop is which generic list. Where that is a guess
it says so. <workdir> keeps what was downloaded (wiki.json, pages.json);
delete them to fetch the wiki again.
Written out per list: "now" (on the shelf according to the wiki) and
"former" (sold there once). lib/shopstock.js stocks all of "now" and those
of "former" that have sale dates, which bring them back in their season.
"""
import json
import os
import re
import sys
import time
import urllib.parse
import urllib.request
API = "https://animaljam.fandom.com/api.php"
OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "lib", "shop-stock.json")
def api(**params):
params["format"] = "json"
url = API + "?" + urllib.parse.urlencode(params)
for attempt in range(4):
try:
request = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0 (shop research)"})
return json.load(urllib.request.urlopen(request, timeout=60))
except Exception:
time.sleep(2 + attempt * 3)
raise SystemExit("failed: " + url)
def members(category, sortkey=False):
found, more = [], {}
while True:
reply = api(action="query", list="categorymembers", cmtitle="Category:" + category, cmlimit="500",
cmprop="title|sortkeyprefix" if sortkey else "title", **more)
found += reply["query"]["categorymembers"]
if "continue" not in reply:
return found
more = {"cmcontinue": reply["continue"]["cmcontinue"]}
time.sleep(0.2)
def download(workdir):
wiki_path, pages_path = os.path.join(workdir, "wiki.json"), os.path.join(workdir, "pages.json")
if not os.path.exists(wiki_path):
wiki = {"ids": {}, "shops": {}}
for kind, category in (("clothing", "Clothing by ID"), ("den", "Den Items by ID")):
wiki["ids"][kind] = {row["title"]: row["sortkeyprefix"] for row in members(category, True)}
for shop in [row["title"] for row in members("Shops")]:
if not shop.startswith("Category:"):
wiki["shops"][shop] = {"now": [row["title"] for row in members(shop)],
"former": [row["title"] for row in members("Formerly " + shop)]}
time.sleep(0.2)
json.dump(wiki, open(wiki_path, "w"))
wiki = json.load(open(wiki_path))
if not os.path.exists(pages_path):
pages = {}
for shop in wiki["shops"]:
reply = api(action="parse", page=shop, prop="wikitext", redirects="1")
pages[shop] = reply.get("parse", {}).get("wikitext", {}).get("*", "")
time.sleep(0.15)
json.dump(pages, open(pages_path, "w"))
return wiki, json.load(open(pages_path))
# sections of a shop page that talk about items without listing the shelf.
NOT_THE_SHELF = re.compile(r"trivia|gallery|history|appearance|glitch|previously|formerly|see also", re.I)
def sections(text):
"""[(heading path, body)] of a page, the lead included under ""."""
parts = re.split(r"^(=+)\s*(.*?)\s*=+\s*$", text, flags=re.M)
found, path = [("", parts[0])], []
for at in range(1, len(parts), 3):
level, name = len(parts[at]), parts[at + 1]
path = [step for step in path if step[0] < level] + [(level, name)]
found.append((" / ".join(step[1] for step in path), parts[at + 2]))
return [(head, body) for head, body in found if not NOT_THE_SHELF.search(head)]
def latest_tab(body):
"""Of a tabbed list by year, the first tab: the latest years."""
body = re.sub(r"\{\{Tabber\s*\|[^|]*\|(\{\{[^{}]*\}\}).*?(?=\n==|\Z)", r"\1", body, flags=re.S)
return re.sub(r"<tabber>\s*\|-\|[^=\n]*=(.*?)(?:\|-\|.*?)?</tabber>", r"\1", body, flags=re.S)
def names(body):
"""Item names in a stretch of wikitext: item tables and links."""
found = []
for table in re.finditer(r"\{\{(?:ItemTable|CollectionTable)\s*\|([^{}]*)\}\}", body):
for cell in re.split(r"[|,;]", table.group(1)):
found.append(cell.split("::")[0].strip())
found += [link.group(1).strip() for link in re.finditer(r"\[\[([^\]|#]+)", body)]
return found
# what a list takes from the wiki: (shop, where, heading, name).
# where "category": the shop's item category, now and formerly;
# "page": the items listed on the shop's page.
# heading with "page", only sections whose heading path matches.
# name only items whose name matches (a leading "!" for "does not").
def category(shop, name=None):
return (shop, "category", None, name)
def page(shop, heading=None, name=None):
return (shop, "page", heading, name)
NEW_YEARS = ["Chinese New Year Shop"] + ["Year of the %s Shop" % animal for animal in (
"Dog", "Dragon", "Horse", "Ox", "Pig", "Rabbit", "Rat", "Rooster", "Snake", "Tiger")]
PARTY_MUSIC = r"music"
# generic list id -> what it sells. "music": True keeps only music tracks,
# False keeps them out (a party's furniture stand next to its jukebox).
# "kinds" names the one kind a diamond list sells, which can hold both.
LISTS = {
# the lands. 11, 50, 12, 51, 56, 85 and 234 are named in the client
# (GenericListXtCommManager); the rest follow from the room each list
# stands in.
11: {"from": [category("Jam Mart Clothing")]},
12: {"from": [category("Jam Mart Furniture")], "music": False},
234: {"from": [category("Jam Mart Furniture (New Jammer)")]},
56: {"from": [category("Music Shop")], "music": True},
5: {"from": [category("Shiveer Shoppe")]},
9: {"from": [category("Mystery Emporium")]},
113: {"from": [category("Treetop Gardens")]},
323: {"from": [category("Topiary Shop")]},
589: {"from": [category("Topiary Shop (Lit)")]},
778: {"from": [category("Eco Shop")]},
362: {"from": [category("Theater Shop")]},
184: {"from": [category("Museum Shop")]},
85: {"from": [category("Pet Den Item Shop")]},
50: {"from": [category("Bahari Bargains")]},
51: {"from": [category("Sunken Treasures")]},
626: {"from": [category("Ocean Diamond Shop")], "kinds": ["clothing"]}, # the clam in Bahari Bargains: clothing
625: {"from": [category("Ocean Diamond Shop")], "kinds": ["den"]}, # the shelf in Sunken Treasures: den items
250: {"from": [category("Marine Marvels")]},
212: {"from": [page("Royal Ridge")]}, # "airshop": the one only flying animals reach
59: {"from": [category("Epic Wonders")]},
423: {"from": [category("Epic Wonders")], "kinds": ["den"]}, # the lower orb
420: {"from": [category("Studio Corner")]},
63: {"from": [category("Sol Arcade Shop")]},
374: {"from": [category("Astronomy Shop")]},
177: {"from": [category("Outback Imports")]},
178: {"from": [category("Hospital Shop")]},
179: {"from": [category("Adventure Shop")]},
687: {"from": [category("Ancient Antiquities")]},
# guessed which of the temple's two windows is which, by the order the
# shops opened in (the armory first).
688: {"from": [category("Ancient Armory")]},
713: {"from": [category("Ancient Treasures")]},
801: {"from": [category("Jambassador Plushie Shop")]},
790: {"from": [category("Spooky Shop (Phantom Vortex)")]},
584: {"from": [page(r"??? (Epic Dens)")]},
# the two shops this server filled with what was never sold anywhere
# (ODDITY_SHOPS in lib/world.js) get their real shelves in front of that.
396: {"from": [page(r"??? (Basement of Secrets)")]},
666: {"from": [category("Forgotten Relics")]},
# the Diamond Shop. guessed: 415 as the clothing window (the newer list;
# the two were one window until 2016), 167 as the den items, 168
# ("armor") as the armour among the clothing.
415: {"from": [category("Diamond Shop")], "kinds": ["clothing"]},
168: {"from": [category("Diamond Shop", r"armou?r|helmet|gauntlet|amulet|sword|shield")], "kinds": ["clothing"]},
167: {"from": [category("Diamond Shop", r"!mannequin")], "kinds": ["den"], "music": False},
173: {"from": [category("Diamond Shop")], "kinds": ["den"], "music": True},
684: {"from": [page("Diamond Shop", r"^Mannequins")]},
689: {"from": [page("Diamond Shop", r"^Deep Blue")]},
# alpha Headquarters: six stands, one alpha's armour each. guessed which
# stand is whose (the stands are numbered 1 to 6 in the room file, the
# alphas taken in the wiki's order).
677: {"from": [category("Alpha Headquarters", r"peck")]},
678: {"from": [category("Alpha Headquarters", r"greely")]},
676: {"from": [category("Alpha Headquarters", r"gilbert")]},
680: {"from": [category("Alpha Headquarters", r"liza")]},
679: {"from": [category("Alpha Headquarters", r"graham")]},
681: {"from": [category("Alpha Headquarters", r"cosmo")]},
# hall of Animals. the plushies and the coins are named in the room
# file; which of the three tables has banners, statues and flags is
# guessed.
805: {"from": [page("Hall of Animals", r"Animal Banners")]},
806: {"from": [page("Hall of Animals", r"Alpha Statues")]},
803: {"from": [page("Hall of Animals", r"Flags")]},
807: {"from": [page("Hall of Animals", r"Giant Animal Plushies")]},
817: {"from": [page("Hall of Animals", r"Animal Coins")]},
# parties.
61: {"from": [category("Jamaaliday Shop")], "music": False},
138: {"from": [category("Jamaaliday Shop")], "music": True},
709: {"from": [page("Giant Gift Pile (Shop)")]},
67: {"from": [page("Dinner Shop")]},
68: {"from": [page("Friendship Shop")]},
70: {"from": [page("Heatwave Shop")]},
71: {"from": [page("Leap Year Accessories")]},
72: {"from": [page("Leap Year Den Items")]},
74: {"from": [page("Lucky Shop")]},
81: {"from": [page("Wolves Only Shop")]},
83: {"from": [page("Horses Only Shop")]},
84: {"from": [page("Horses Only Shop")], "music": False},
90: {"from": [page("Horses Only Shop")], "music": True},
82: {"from": [page("April Fools' Shop")]},
216: {"from": [page("April Fools' Shop")]},
87: {"from": [page("Atlantis Shop")]},
88: {"from": [page("Atlantis Shop")]},
92: {"from": [page("Spooky Shop")]},
93: {"from": [page("Spooky Shop")], "music": False},
94: {"from": [page("Spooky Shop")], "music": True},
95: {"from": [page("Cruise Ship Shop")]},
96: {"from": [page("Cruise Ship Shop")]},
99: {"from": [page("Bunnies Only Shop")], "music": False},
100: {"from": [page("Bunnies Only Shop")], "music": True},
104: {"from": [page("Penguins Only Shop")], "music": False},
106: {"from": [page("Penguins Only Shop")], "music": True},
107: {"from": [category("Freedom Shop")]},
108: {"from": [category("Freedom Shop")]},
111: {"from": [page("Pets Only Shop")], "music": False},
112: {"from": [page("Pets Only Shop")], "music": True},
116: {"from": [page("Monkeys Only Shop")], "music": False},
117: {"from": [page("Monkeys Only Shop")], "music": True},
118: {"from": [page("AJ Birthday Shop")], "music": False},
119: {"from": [page("AJ Birthday Shop")], "music": True},
346: {"from": [page("AJ Birthday Shop")]},
122: {"from": [page("Haunted Forest Shop")]},
123: {"from": [page("Haunted Forest Shop")], "music": False},
124: {"from": [page("Haunted Forest Shop")], "music": True},
143: {"from": [page("New Year's Shop")]},
144: {"from": [page("New Year's Shop")], "music": False},
145: {"from": [page("New Year's Shop")], "music": True},
161: {"from": [page("Jam Session Shop")]},
164: {"from": [page("Jam Session Shop")], "music": False},
163: {"from": [page("Jam Session Shop")], "music": True},
191: {"from": [category("Play-As-Your-Pet Shop")], "music": False},
192: {"from": [category("Play-As-Your-Pet Shop")], "music": False},
193: {"from": [category("Play-As-Your-Pet Shop")], "music": True},
238: {"from": [category("Beta Shop")]},
271: {"from": [page("Bounce House Shop")]},
324: {"from": [page("Cloud Shop")]},
325: {"from": [page("Cloud Shop")]},
345: {"from": [page("Schoolhouse Shop")]},
150: {"from": [page("Paradise Shop")]},
400: {"from": [category("Candy Shop")]},
# one party room for every year's animal: all their shops together.
399: {"from": [category(shop) for shop in NEW_YEARS]},
# the Phantom Emporium's three rooms.
816: {"from": [page("Phantom Emporium (Shop)")]},
818: {"from": [page("Phantom Emporium (Shop)")]},
819: {"from": [page("Phantom Emporium (Shop)")]},
}
def main(workdir, out_path=OUT):
wiki, pages = download(workdir)
ids = {}
for kind in ("clothing", "den"):
for title, def_id in wiki["ids"][kind].items():
if def_id.isdigit():
ids.setdefault(title.lower(), []).append((kind, int(def_id)))
unknown = set()
def matching(pattern, text):
if not pattern:
return True
if pattern.startswith("!"):
return not re.search(pattern[1:], text, re.I)
return bool(re.search(pattern, text, re.I))
def take(titles, name):
return [item for title in titles if matching(name, title) for item in ids.get(title.lower(), [])]
lists = {}
for list_id, spec in sorted(LISTS.items()):
now, former, shops = [], [], []
for shop, where, heading, name in spec["from"]:
shops.append(shop)
if where == "category":
now += take(wiki["shops"][shop]["now"], name)
former += take(wiki["shops"][shop]["former"], name)
else:
for head, body in sections(pages[shop]):
if matching(heading, head):
found = names(latest_tab(body))
unknown.update(n for n in found if n and n.lower() not in ids and len(n) < 40)
now += take(found, name)
entry = {"shop": " + ".join(shops) if len(shops) < 3 else shops[0] + " and others"}
if "music" in spec:
entry["music"] = spec["music"]
for label, items in (("now", now), ("former", [item for item in former if item not in now])):
unique = list(dict.fromkeys(items))
entry[label] = {kind: [def_id for item_kind, def_id in unique if item_kind == kind and kind in spec.get("kinds", (kind,))]
for kind in ("clothing", "den")}
lists[str(list_id)] = entry
print("%4d %-34s now %3d clothing %3d den former %4d" % (
list_id, entry["shop"][:34], len(entry["now"]["clothing"]), len(entry["now"]["den"]),
len(entry["former"]["clothing"]) + len(entry["former"]["den"])))
out = {"source": "AJ Classic Wiki (animaljam.fandom.com), read by tools/shop-stock.py", "lists": lists}
with open(out_path, "w") as handle:
handle.write(json.dumps(out, separators=(",", ":")).replace('"lists":{', '"lists":{\n').replace('}},"', '}},\n"') + "\n")
print("\nnames on shop pages that are no item page (links to other pages, mostly):")
print(", ".join(sorted(unknown)))
if __name__ == "__main__":
if len(sys.argv) not in (2, 3):
raise SystemExit(__doc__)
main(*sys.argv[1:])