fix: preserve db-specific dungeon rows when installing item patches

This commit is contained in:
shen
2026-10-17 05:02:58 -07:00
parent 108fe17127
commit 090dab1fc1
3 changed files with 20 additions and 13 deletions
+12 -9
View File
@@ -13,18 +13,21 @@ load=lambda p:{int(l.split(b'\t',1)[0]):l for l in p.read_bytes().splitlines() i
backup.mkdir(parents=True)
conf=server/'share/conf'
def merge(path,saved,rows,row):
raw=path.read_bytes();saved.parent.mkdir(parents=True,exist_ok=True);shutil.copy2(path,saved)
newline=b'\r\n' if b'\r\n' in raw else b'\n'
kept=[l for l in raw.replace(b'\r\n',b'\n').split(b'\n') if l and not (l.split(b'\t',1)[0].isdigit() and int(l.split(b'\t',1)[0]) in rows)]
tmp=path.with_name(path.name+'.tmp');tmp.write_bytes(newline.join(kept+[row(v) for v in sorted(rows)])+newline);tmp.replace(path)
def install(proto,names,rows):
for filename,row in ((proto,lambda v:rows[v]),(names,lambda v:str(v).encode()+b'\t'+rows[v].split(b'\t')[1])):
path=conf/filename;raw=path.read_bytes();shutil.copy2(path,backup/filename)
newline=b'\r\n' if b'\r\n' in raw else b'\n'
kept=[l for l in raw.replace(b'\r\n',b'\n').split(b'\n') if l and not (l.split(b'\t',1)[0].isdigit() and int(l.split(b'\t',1)[0]) in rows)]
tmp=path.with_name(filename+'.tmp');tmp.write_bytes(newline.join(kept+[row(v) for v in sorted(rows)])+newline);tmp.replace(path)
# Isolated clusters may use copies rather than links to share/conf.
path=conf/filename
# Merge the requested rows into each independent table. A db copy can
# contain dungeon items absent from conf; never replace it wholesale.
dbpath=server/'db'/filename
if dbpath.is_file() and dbpath.resolve()!=path.resolve():
dbbackup=backup/'db'/filename;dbbackup.parent.mkdir(parents=True,exist_ok=True)
shutil.copy2(dbpath,dbbackup)
dbtmp=dbpath.with_name(filename+'.tmp');shutil.copy2(path,dbtmp);dbtmp.replace(dbpath)
separate=dbpath.is_file() and dbpath.resolve()!=path.resolve()
merge(path,backup/filename,rows,row)
if separate:merge(dbpath,backup/'db'/filename,rows,row)
items=load(rows);install('item_proto.txt','item_names.txt',items)
print('AVA2 items installed:',len(items),'backup:',backup)
+3 -3
View File
@@ -97,15 +97,15 @@ class ConvertTests(unittest.TestCase):
d=Path(d);conf=d/'share/conf';conf.mkdir(parents=True)
(conf/'item_proto.txt').write_bytes(b'VNUM\tNAME\r\n71502\told\r\n');(conf/'item_names.txt').write_bytes(b'VNUM\tNAME\r\n')
(conf/'mob_proto.txt').write_bytes(b'VNUM\tNAME\n20110\tboar\n');(conf/'mob_names.txt').write_bytes(b'20110\tboar\n')
(d/'db').mkdir();(d/'db/item_proto.txt').write_bytes(b'old db copy\n')
(d/'db').mkdir();(d/'db/item_proto.txt').write_bytes(b'VNUM\tNAME\n25170\tdungeon-only\n')
(d/'i.tsv').write_bytes(b'71502\tnew\n');(d/'m.tsv').write_bytes(b'32012\tbari\tKING\n')
subprocess.run([sys.executable,str(HERE/'install_items.py'),str(d),str(d/'i.tsv'),str(d/'bk'),str(d/'m.tsv')],check=True,capture_output=True)
self.assertEqual((conf/'item_proto.txt').read_bytes(),b'VNUM\tNAME\r\n71502\tnew\r\n')
self.assertEqual((conf/'mob_proto.txt').read_bytes(),b'VNUM\tNAME\n20110\tboar\n32012\tbari\tKING\n')
self.assertEqual((conf/'mob_names.txt').read_bytes(),b'20110\tboar\n32012\tbari\n')
self.assertTrue((d/'bk/mob_proto.txt').is_file())
self.assertEqual((d/'db/item_proto.txt').read_bytes(),(conf/'item_proto.txt').read_bytes())
self.assertEqual((d/'bk/db/item_proto.txt').read_bytes(),b'old db copy\n')
self.assertEqual((d/'db/item_proto.txt').read_bytes(),b'VNUM\tNAME\n25170\tdungeon-only\n71502\tnew\n')
self.assertEqual((d/'bk/db/item_proto.txt').read_bytes(),b'VNUM\tNAME\n25170\tdungeon-only\n')
def test_sash_effect_closure(self):
import tempfile
with tempfile.TemporaryDirectory() as d: