#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-only """Build private classic-plus-1 data from a supplied German installation and OBW donor. No data are downloaded. Pillow and CairoSVG are build-time image tools, not game dependencies. Original installations and source archives are never modified. The output must not exist. """ import argparse import collections import datetime import hashlib import io import json from pathlib import Path import shutil import struct import unicodedata import zipfile SIZES = (18, 18, 18, 18, 18, 18, 20, 18, 18) OLD_SIZES = (18, 18, 18, 18, 18, 12, 12, 12, 12) def sha(data): return hashlib.sha256(data).hexdigest() def u16(data): return list(struct.unpack('<' + 'H' * (len(data) // 2), data)) def words(values, width=2): return struct.pack('<' + ('H' if width == 2 else 'I') * len(values), *values) def read_master(data, clubs=419, players=2574, records=168, extra=3096): at = 0 def take(n): nonlocal at if at + n > len(data): raise ValueError('Master database is truncated or has unsupported dimensions') result = data[at:at+n] at += n return result lengths = u16(take(8)) result = {'bonus': take(extra+50), 'pools': [take(n) for n in lengths]} for key, n in [('clubs', clubs*2), ('names', players*2), ('meta', 100), ('aux', records*2), ('badges', clubs*2), ('strength', 251), ('league_aux', 189), ('refs', 50), ('kits', clubs), ('short', records), ('squads', (extra+50)*2), ('form', players*4), ('skills', players*4), ('identity', players*4)]: result[key] = take(n) if at != len(data): raise ValueError('Unsupported master database dimensions/trailing bytes') return result def write_master(db): return words([len(p) for p in db['pools']]) + db['bonus'] + b''.join(db['pools']) + b''.join( db[k] for k in ('clubs', 'names', 'meta', 'aux', 'badges', 'strength', 'league_aux', 'refs', 'kits', 'short', 'squads', 'form', 'skills', 'identity')) def legacy_name(name): # Preserve Original ISO 646-DE and use a documented transliteration for other letters. aliases = str.maketrans({'ø':'o', 'Ø':'O', 'ł':'l', 'Ł':'L', 'đ':'d', 'Đ':'D', 'ß':'~', 'ä':'{', 'ö':'|', 'ü':'}', 'Ä':'[', 'Ö':'\\', 'Ü':']'}) value = unicodedata.normalize('NFKD', name.translate(aliases)) return ''.join(c for c in value if not unicodedata.combining(c)).upper().encode('ascii', errors='replace') def add_string(pool, name): data = legacy_name(name) + b'\0' if len(pool) + len(data) > 65535: raise ValueError('String pool exceeds Original u16 offsets') offset = len(pool) pool.extend(data) return offset def decode_vga(data): width, height, length = struct.unpack_from('= 128: pixels.extend([data[at]] * (c-127)); at += 1 else: pixels.extend(data[at:at+c+1]); at += c+1 if len(pixels) != width*height or at+1 != 8+length: raise ValueError('Malformed VGA input') return width, height, pixels def encode_vga(width, height, pixels): if len(pixels) != width*height: raise ValueError('VGA dimensions do not match pixels') # Valid literal packets, including the one trailing byte required by the Original reader. payload = b''.join(bytes([len(pixels[i:i+128])-1]) + bytes(pixels[i:i+128]) for i in range(0, len(pixels), 128)) + b'\0' return struct.pack('>5}) db['names']=words(names);db['pools'][1]=bytes(pool) for k in new_words: db[k]+=words(new_words[k],4) bonus=bytearray(4154);bonus[:2574]=db['bonus'][:2574] for i,p in enumerate(player_additions): nat=field_provenance[i]['nationality_code'];bonus[2574+i]=(int(p['attributes'].get('bonus',0))&127)|((nat>>5)<<7) db['bonus']=bytes(bonus) # Keep Original national flags/domestic crests. Assign private foreign crests to free cells. used={b&511 for i,b in enumerate(old_badges) if i<90 or 138<=i<168 or i>=357} free=iter(i for i in range(512) if i not in used) palette=find_file(source,'M3PC.COL').read_bytes() rgb=[tuple(((v&63)*255+31)//63 for v in palette[i:i+3]) for i in range(0,768,3)] sheets={}; crest_report=[]; cache={} w_dir=find_file(source,'W') def sheet(n): if n not in sheets: file=next((p for p in w_dir.iterdir() if p.name.upper()==str(n)+'.VGA'),None) sheets[n]=decode_vga(file.read_bytes()) if file else (304,38,bytearray([0]*11552)) if sheets[n][:2]!=(304,38):raise ValueError('Unexpected crest sheet geometry') return sheets[n][2] for index,club in enumerate(new_clubs): if not (90 <= index < 164 or 194 <= index < 383): continue cell=next(free,None) if cell is None: raise ValueError('512-cell badge space exhausted') asset=assets.get(club.get('crest'),{});files=asset.get('files',{}); image=None; error=None for kind in ('svg','png','native'): path=files.get(kind) if not path:continue try: data=archive.read(path) if path.lower().endswith('.svg'):data=cairosvg.svg2png(bytestring=data,output_width=152,output_height=152) image=Image.open(io.BytesIO(data)).convert('RGBA');break except Exception as exc:error=type(exc).__name__ fictional=image is None if fictional: image=Image.new('RGBA',(38,38));d=ImageDraw.Draw(image);d.polygon([(6,3),(31,3),(30,24),(19,35),(7,24)],fill=rgb[64+index%16],outline='white');d.text((12,11),str(index%100),fill='white') image=ImageOps.contain(image,(36,36),method=Image.Resampling.LANCZOS) canvas=Image.new('RGBA',(38,38));canvas.alpha_composite(image,((38-image.width)//2,(38-image.height)//2)) pixels=bytearray() for r,g,b,a in canvas.getdata(): if a<128:pixels.append(0);continue key=(r,g,b) if key not in cache:cache[key]=min(range(1,256),key=lambda n:sum((x-y)**2 for x,y in zip(rgb[n],key))) pixels.append(cache[key]) dst=sheet(cell//8) for y in range(38):dst[y*304+(cell%8)*38:y*304+(cell%8)*38+38]=pixels[y*38:(y+1)*38] badges[index]=(badges[index]&~511)|cell crest_report.append({'club':club['id'],'runtime_team':index,'cell':cell,'asset':club.get('crest'),'source':asset.get('source'),'license':asset.get('license'),'fictional':fictional,'last_decode_error':error}) db['badges']=words(badges) # Every Original player word and all German starting rosters remain unchanged. original_db = read_master(master) for key in ('form', 'skills', 'identity'): assert db[key][:2574*4] == original_db[key] for team in range(90): assert new_squads[team*24:(team+1)*24] == runtime_squads[team*24:(team+1)*24] occupied = [v & 4095 for v in new_squads[:3936] if (v & 4095) != 4095] if len(occupied) != 3280 or len(set(occupied)) != len(occupied): raise ValueError('Expanded club rosters contain missing or duplicated players') encoded=write_master(db) parsed=read_master(encoded,445,3334,194,4104) assert write_master(parsed)==encoded shutil.copytree(source,output,ignore=shutil.ignore_patterns('.git','.openeddi','*.FBM','*.fbm')) find_file(output,'M3D.MAN').write_bytes(encoded) out_w=find_file(output,'W') for n,(width,height,pix) in sheets.items(): target=next((p for p in out_w.iterdir() if p.name.upper()==str(n)+'.VGA'),out_w/(str(n)+'.VGA')) vga=encode_vga(width,height,pix); assert decode_vga(vga)[2]==pix;target.write_bytes(vga) (output/'OBMHPROF.INI').write_text('[OpenBMH]\nprofile=classic-plus-1\n',encoding='ascii') report={'profile':'classic-plus-1','base_sha256':sha(master),'donor_sha256':sha(donor.read_bytes()),'output_sha256':sha(encoded),'league_sizes':SIZES,'club_count':445,'player_count':3334,'source_to_runtime':old_new,'selected_additions':selected,'rosters':roster_report,'fictional_club_fields':{'new_clubs':['/kit/selector','/short_name_offset'],'policy':'deterministic existing Classic kit palette and automatic stadium initialization'},'additional_players':field_provenance,'crests':crest_report,'content_license':json.loads(archive.read('manifest.json')).get('content_license'),'distribution':'Private prepared data; content rights/provenance unchanged. No game content belongs in public source/binary artifacts.'} (output/'IMPORT-REPORT.json').write_text(json.dumps(report,ensure_ascii=False,indent=2)+'\n') print(json.dumps({'output':str(output),'clubs':445,'players':3334,'foreign_crests':len(crest_report),'fictional_crests':sum(c['fictional'] for c in crest_report),'sha256':sha(encoded)})) if __name__=='__main__': p=argparse.ArgumentParser(description=__doc__);p.add_argument('--source',required=True,type=Path);p.add_argument('--supplement',required=True,type=Path);p.add_argument('--output',required=True,type=Path);a=p.parse_args();build(a.source,a.supplement,a.output)