#!/usr/bin/env python3 """Install the AVA2 maps stage (make_maps.py output/server) into a server locale + conf. - mobs-server.tsv -> conf/mob_proto.txt + mob_names.txt, rows replaced by vnum. - group.txt / group_group.txt: blocks whose Vnum is missing are appended; existing vnums are kept. - map/ copied (an existing real dir is moved to the backup first; a symlink is refused). - map/index: missing " " lines appended; an index used by another map is refused. - data/monster/ (optional 5th arg = the core's data dir): only folders it lacks are added. - mob_drop_item.txt (make_drops_maps.py output): the group of each monster that has no drop group yet is appended; every item vnum must be in conf/item_proto.txt (the core refuses the whole file on an unknown item). - Then every regen group and mob reference is checked against the installed tables. Rerunning is idempotent. Runs on 203 (python3.9). The core reads the locale at boot and the db reads conf at boot: restart db and the core afterwards. Usage: install_maps.py [] """ import re,shutil,sys from pathlib import Path locale,conf,stage,backup=map(Path,sys.argv[1:5]) data=Path(sys.argv[5]) if len(sys.argv)>5 else None if backup.exists():raise SystemExit('use a fresh backup directory') backup.mkdir(parents=True) def lines(raw):return [l for l in raw.replace(b'\r\n',b'\n').split(b'\n') if l.strip()] def newline(raw):return b'\r\n' if b'\r\n' in raw else b'\n' def write(path,data): tmp=path.with_name(path.name+'.tmp');tmp.write_bytes(data);tmp.replace(path) def save(path): dest=backup/path.relative_to(path.parents[1]) if path.parent.name=='map' else backup/path.name dest.parent.mkdir(parents=True,exist_ok=True);shutil.copy2(path,dest) # mob_proto.txt + mob_names.txt rows={int(l.split(b'\t',1)[0]):l for l in lines((stage/'mobs-server.tsv').read_bytes())} for name,row in (('mob_proto.txt',lambda v:rows[v]),('mob_names.txt',lambda v:str(v).encode()+b'\t'+rows[v].split(b'\t')[1])): path=conf/name;raw=path.read_bytes();save(path) kept=[l for l in lines(raw) if not (l.split(b'\t',1)[0].isdigit() and int(l.split(b'\t',1)[0]) in rows)] write(path,newline(raw).join(kept+[row(v) for v in sorted(rows)])+newline(raw)) mobs={int(l.split(b'\t',1)[0]) for l in lines((conf/'mob_proto.txt').read_bytes()) if l.split(b'\t',1)[0].isdigit()} # group.txt / group_group.txt BLOCK=re.compile(rb'Group[ \t]+\S+\s*\{.*?\}',re.S) def blocks(raw):return {int(re.search(rb'Vnum\s+(\d+)',b).group(1)):b for b in BLOCK.findall(raw)} appended={} for name in ('group.txt','group_group.txt'): path=locale/name if path.is_symlink():raise SystemExit('%s is a symlink (shared with the live locale?)'%path) raw=path.read_bytes();have=blocks(raw);new=blocks((stage/name).read_bytes()) missing=[new[v] for v in sorted(new) if v not in have];appended[name]=len(missing) if missing: save(path);nl=newline(raw) write(path,raw.rstrip(b'\r\n')+nl+nl.join(b.replace(b'\r\n',b'\n').replace(b'\n',nl) for b in missing)+nl) groups=blocks((locale/'group.txt').read_bytes());ggroups=blocks((locale/'group_group.txt').read_bytes()) # map dirs + index mapdir=locale/'map';index_path=mapdir/'index' if index_path.is_symlink() or mapdir.is_symlink():raise SystemExit('%s is shared with the live locale'%mapdir) raw=index_path.read_bytes();index={} for l in lines(raw): t=l.split() if len(t)>=2 and t[0].isdigit():index[int(t[0])]=t[1].decode() add=[] for l in lines((stage/'index.txt').read_bytes()): i,m=int(l.split()[0]),l.split()[1].decode() if index.get(i,m)!=m:raise SystemExit('map index %d is %s on the server, not %s'%(i,index[i],m)) if i not in index:add.append(b'%d %s'%(i,m.encode())) dst=mapdir/m if dst.is_symlink():raise SystemExit('%s is a symlink'%dst) if dst.exists():(backup/'map').mkdir(exist_ok=True);shutil.move(str(dst),str(backup/'map'/m)) shutil.copytree(str(stage/'map'/m),str(dst)) if add:save(index_path);write(index_path,raw.rstrip(b'\r\n')+newline(raw)+newline(raw).join(add)+newline(raw)) # server motion data motion_added=0 if data: if (data/'monster').is_symlink() or data.is_symlink():raise SystemExit('%s is shared with the live data'%data) for f in sorted((stage/'data/monster').iterdir()): dst=data/'monster'/f.name if not (dst.exists() or dst.is_symlink()):shutil.copytree(str(f),str(dst));motion_added+=1 # mob drops drops_added=0;errors=[] if (stage/'mob_drop_item.txt').exists(): path=locale/'mob_drop_item.txt' if path.is_symlink():raise SystemExit('%s is a symlink (shared with the live locale?)'%path) raw=path.read_bytes();nl=newline(raw) DROP=re.compile(rb'Group[ \t]+(\S+)\s*\{(.*?)\}',re.S) names={n.lower() for n,_ in DROP.findall(raw)} have={int(m.group(1)) for n,b in DROP.findall(raw) if re.search(rb'Type\s+drop',b,re.I) for m in [re.search(rb'Mob\s+(\d+)',b)] if m} items={int(l.split(b'\t',1)[0]) for l in lines((conf/'item_proto.txt').read_bytes()) if l.split(b'\t',1)[0].isdigit()} add_drops=[] for m in DROP.finditer((stage/'mob_drop_item.txt').read_bytes()): mob=int(re.search(rb'Mob\s+(\d+)',m.group(2)).group(1)) if mob in have:continue if m.group(1).lower() in names:raise SystemExit('drop group name %s already used'%m.group(1).decode()) for v in re.findall(rb'\n\s*\d+\s+(\d+)\s+\d+\s+[\d.]+',m.group(0)): if int(v) not in items:errors.append('drop group %s: item %s not in item_proto'%(m.group(1).decode(),v.decode())) if mob not in mobs:errors.append('drop group %s: mob %d missing'%(m.group(1).decode(),mob)) add_drops.append(m.group(0).replace(b'\r\n',b'\n').replace(b'\n',nl)) if errors:raise SystemExit('\n'.join(sorted(set(errors)))+'\nmob_drop_item.txt not changed') if add_drops: save(path);write(path,raw.rstrip(b'\r\n')+nl+nl.join(add_drops)+nl);drops_added=len(add_drops) # checks for v in map(int,(stage/'mob_refs.txt').read_text().split()): if v not in mobs:errors.append('mob %d missing from mob_proto'%v) for l in lines((stage/'index.txt').read_bytes()): m=l.split()[1].decode() for f in ('regen.txt','npc.txt','boss.txt','stone.txt','dungeon.txt'): for r in lines((mapdir/m/f).read_bytes()): t=r.split() if not t or t[0].startswith(b'/'):continue kind=t[0][:1].lower();v=int(t[-1]) if len(t)>=11 and t[-1].isdigit() else None if v is None:continue if kind==b'g' and v not in groups:errors.append('%s/%s: group %d missing'%(m,f,v)) if kind==b'r' and v not in ggroups:errors.append('%s/%s: group_group %d missing'%(m,f,v)) if kind in (b'm',b's') and v not in mobs:errors.append('%s/%s: mob %d missing'%(m,f,v)) for v,b in blocks((stage/'group.txt').read_bytes()).items(): for mob in re.findall(rb'\n\s*(?:Leader|\d+)\s+\S+\s+(\d+)',groups[v]): if int(mob) not in mobs:errors.append('group %d: mob %s missing'%(v,mob.decode())) print('AVA2 maps installed: mobs=%d groups+=%d group_groups+=%d index+=%d motion+=%d drops+=%d backup=%s'%(len(rows),appended['group.txt'],appended['group_group.txt'],len(add),motion_added,drops_added,backup)) if errors: print('\n'.join(sorted(set(errors))));raise SystemExit('%d reference errors'%len(set(errors))) print('references OK')