diff --git a/tests/wiki/test_export.py b/tests/wiki/test_export.py new file mode 100644 index 00000000..411471aa --- /dev/null +++ b/tests/wiki/test_export.py @@ -0,0 +1,40 @@ +import importlib.util +import tempfile +import unittest +from pathlib import Path +from unittest.mock import patch + +MODULE_PATH=Path(__file__).resolve().parents[2]/'tools/wiki/export_public_tables.py' +spec=importlib.util.spec_from_file_location('export_wiki',MODULE_PATH) +module=importlib.util.module_from_spec(spec);spec.loader.exec_module(module) + + +class ExportTests(unittest.TestCase): + def test_only_public_inputs_and_readonly_query(self): + with tempfile.TemporaryDirectory() as folder: + base=Path(folder);conf=base/'server/share/conf';conf.mkdir(parents=True) + for name in ('item_proto.txt','item_names.txt','mob_proto.txt','mob_names.txt'): + (conf/name).write_text('public\n') + (conf/'CONFIG').write_text('DO NOT EXPORT') + def run(args,stdout,check): + self.assertTrue(check) + self.assertTrue(args[-1].startswith('SELECT id,cost,prob,')) + self.assertNotIn('account',args[-1]) + stdout.write(b'1\t600\t90\n') + with patch.object(module.subprocess,'run',side_effect=run): + result=module.export(base/'server',base/'out','player','mysql') + self.assertEqual(len(result),5) + self.assertFalse((base/'out/CONFIG').exists()) + self.assertTrue((base/'out/source-manifest.json').exists()) + with self.assertRaises(ValueError):module.export(base/'server',base/'out','player','mysql') + + def test_invalid_database_and_incomplete_source(self): + with tempfile.TemporaryDirectory() as folder: + base=Path(folder) + with self.assertRaises(ValueError):module.export(base,base/'out','player;DROP TABLE','mysql') + self.assertFalse((base/'out').exists()) + with self.assertRaises(FileNotFoundError):module.export(base,base/'out','player','mysql') + self.assertFalse((base/'out').exists()) + + +if __name__=='__main__':unittest.main() diff --git a/tools/wiki/README.md b/tools/wiki/README.md new file mode 100644 index 00000000..29c785c3 --- /dev/null +++ b/tools/wiki/README.md @@ -0,0 +1,13 @@ +# 公开模板资料导出 + +本工具供 metin2-client 百科生成器使用,只读取四张公开配置表和 refine_proto 的配方字段。 +不读取账号、角色、物品实例或运行 CONFIG,不启动/停止服务,不修改数据库。 + +在服务器上使用现有 MySQL 客户端凭据(如 root 的 socket/option file): + +```sh +python3 tools/wiki/export_public_tables.py /tmp/wiki-export-NEW +``` + +目标目录必须不存在。将导出目录通过 SSH 复制到客户端忽略的 build/wiki-source/,再运行客户端的 tools/wiki/build_catalogue.py。 +本工具与客户端生成器分别保留输入哈希。运行中的数据库和磁盘配置可能存在未重载差异,导出时应确认 DB/game 已加载该版本;此静态资料不能声明服务器实时掉落或最终精炼概率。 diff --git a/tools/wiki/export_public_tables.py b/tools/wiki/export_public_tables.py new file mode 100644 index 00000000..c48d09bd --- /dev/null +++ b/tools/wiki/export_public_tables.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +"""Read-only export of public template tables; run on the FreeBSD game host. +Usage: python3 tools/wiki/export_public_tables.py /tmp/wiki-export +mysql uses the operator's existing option file/socket credentials; no password is printed/stored. +""" +import argparse +import hashlib +import json +import os +import re +import shutil +import subprocess +from pathlib import Path + + +def export(server, output, database, mysql): + if not re.fullmatch(r'[A-Za-z0-9_]+', database): + raise ValueError('invalid database identifier') + if output.exists(): + raise ValueError('choose a fresh output directory') + conf=server/'share/conf' + output.mkdir(parents=True,mode=0o700) + try: + for name in ('item_proto.txt','item_names.txt','mob_proto.txt','mob_names.txt'): + shutil.copyfile(conf/name,output/name) + query=('SELECT id,cost,prob,vnum0,count0,vnum1,count1,vnum2,count2,' + 'vnum3,count3,vnum4,count4 FROM refine_proto ORDER BY id') + with (output/'refine.tsv').open('wb') as stream: + subprocess.run([mysql,'-N','-B',database,'-e',query],stdout=stream,check=True) + report={p.name:hashlib.sha256(p.read_bytes()).hexdigest() for p in output.iterdir()} + (output/'source-manifest.json').write_text(json.dumps(report,indent=2)+'\n') + except Exception: + shutil.rmtree(output) + raise + return report + + +if __name__=='__main__': + parser=argparse.ArgumentParser(description=__doc__) + parser.add_argument('output',type=Path) + parser.add_argument('--server',type=Path,default=Path('/usr/metin2/server')) + parser.add_argument('--database',default='player') + parser.add_argument('--mysql',default='mysql') + args=parser.parse_args() + print(json.dumps(export(args.server,args.output,args.database,args.mysql),indent=2))