feat: readoorDict 打包按服务器环境替换词典 APPID 与服务器地址
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a9e169b00c
commit
11f4035da7
@@ -117,11 +117,78 @@ def _write_export_options(build_dir: Path, config: dict):
|
||||
plistlib.dump(data, f)
|
||||
|
||||
|
||||
# 词典环境宏 ← 服务器环境字段(APPID 取 App 的 AppGuid)
|
||||
DICT_SERVER_MACROS = {
|
||||
"SERVERURL": "DICT_SERVERURL",
|
||||
"SERVERHTTPSURL": "DICT_SERVERHTTPSURL",
|
||||
"UCSERVERURL": "DICT_UCSERVERURL",
|
||||
"RECORDSERVERURL": "DICT_RECORDSERVERURL",
|
||||
"LOGSERVERURL": "DICT_LOGSERVERURL",
|
||||
}
|
||||
_DICT_BLOCK_START = re.compile(r"^\s*#\s*(?:el)?if\s+TARGETTYPE\s*==\s*2\s*$")
|
||||
_PP_OPEN = re.compile(r"^\s*#\s*if")
|
||||
_PP_BRANCH = re.compile(r"^\s*#\s*(?:elif|else)\b")
|
||||
_PP_CLOSE = re.compile(r"^\s*#\s*endif\b")
|
||||
|
||||
|
||||
def _replace_dict_env_macros(content: str, values: dict) -> str:
|
||||
"""只替换 TARGETTYPE == 2 段落中未注释的 #define,其它 TARGETTYPE 的同名宏不动。"""
|
||||
lines = content.splitlines(keepends=True)
|
||||
start = next((i for i, line in enumerate(lines) if _DICT_BLOCK_START.match(line)), None)
|
||||
if start is None:
|
||||
raise ProjectPatchError("STSConstantsMacro.h 中未找到 #elif TARGETTYPE == 2 词典配置段")
|
||||
counts = {name: 0 for name in values}
|
||||
depth = 0
|
||||
for i in range(start + 1, len(lines)):
|
||||
line = lines[i]
|
||||
if _PP_OPEN.match(line):
|
||||
depth += 1
|
||||
continue
|
||||
if _PP_CLOSE.match(line) or _PP_BRANCH.match(line):
|
||||
if depth == 0:
|
||||
break
|
||||
if _PP_CLOSE.match(line):
|
||||
depth -= 1
|
||||
continue
|
||||
for name, value in values.items():
|
||||
if re.match(rf"^\s*#define\s+{name}\b", line):
|
||||
ending = "\n" if line.endswith("\n") else ""
|
||||
lines[i] = f'#define {name} @"{value}"{ending}'
|
||||
counts[name] += 1
|
||||
missing = [name for name, count in counts.items() if count == 0]
|
||||
if missing:
|
||||
raise ProjectPatchError(f"词典配置段中未找到未注释的宏: {', '.join(missing)}")
|
||||
return "".join(lines)
|
||||
|
||||
|
||||
def _dict_env_values(config: dict) -> dict:
|
||||
"""服务器环境中的词典地址;全部未配置返回空(沿用源码),部分配置视为错误。"""
|
||||
server_vars = config.get("SERVER_VARS") or {}
|
||||
values = {macro: str(server_vars.get(key, "") or "").strip() for macro, key in DICT_SERVER_MACROS.items()}
|
||||
if not any(values.values()):
|
||||
return {}
|
||||
missing = [DICT_SERVER_MACROS[macro] for macro, value in values.items() if not value]
|
||||
if missing:
|
||||
raise ProjectPatchError(f"服务器环境 {config.get('SERVER', '')} 的词典地址未配置完整: {', '.join(missing)}")
|
||||
if not str(config.get("APPID", "") or "").strip():
|
||||
raise ProjectPatchError("词典 App 未配置 AppGuid,无法写入 APPID")
|
||||
return {"APPID": str(config["APPID"]).strip(), **values}
|
||||
|
||||
|
||||
def _apply_dictionary_config(build_dir: Path, config: dict, messages: list[str]):
|
||||
macro = build_dir / "readoor" / "BookShelf" / "Classes" / "Macro" / "STSConstantsMacro.h"
|
||||
if not macro.exists():
|
||||
raise ProjectPatchError(f"词典配置文件不存在: {macro}")
|
||||
content = macro.read_text(encoding="utf-8")
|
||||
env_values = _dict_env_values(config)
|
||||
if env_values:
|
||||
content = _replace_dict_env_macros(content, env_values)
|
||||
messages.append(
|
||||
f"已替换词典环境配置({config.get('SERVER', '')}): APPID {env_values['APPID']},"
|
||||
f"SERVERURL {env_values['SERVERURL']}"
|
||||
)
|
||||
else:
|
||||
messages.append(f"服务器环境 {config.get('SERVER', '')} 未配置词典地址,沿用源码中的词典环境配置")
|
||||
version = config.get("VERSION", "")
|
||||
content, count = re.subn(
|
||||
r'^(#define\s+SOURCE_VERSION\s+)@"[^"]*"',
|
||||
|
||||
Reference in New Issue
Block a user