♻️ 重构xml解析&处理,支持scene切换

This commit is contained in:
BTMuli
2025-12-22 15:38:22 +08:00
parent bd8ee19b79
commit 35851532a5
4 changed files with 107 additions and 125 deletions

View File

@@ -18,15 +18,13 @@ export function parseBirthSrc(data: Document): TGApp.Archive.Birth.GalSrcFull {
const child = resElements.children.item(ci);
if (child === null) continue;
const tmpResItem: TGApp.Archive.Birth.GalSrcRes = {
id: child.getAttribute("id") ?? "",
id: child.id ?? "",
group: child.getAttribute("group") ?? "",
type: child.tagName,
rel: child.getAttribute("rel") ?? "",
src: child.getAttribute("src") ?? "",
};
// 检测是否都是空
const isNotEmpty = Object.values(tmpResItem).every((v) => v !== "");
if (isNotEmpty) tmpRes.push(tmpResItem);
tmpRes.push(tmpResItem);
}
res.resource = tmpRes;
}
@@ -39,9 +37,7 @@ export function parseBirthSrc(data: Document): TGApp.Archive.Birth.GalSrcFull {
name: child.getAttribute("name") ?? "",
key: child.getAttribute("key") ?? undefined,
};
// 检测是否都是空
const isNotEmpty = Object.values(tmpRoleItem).every((v) => v !== "");
if (isNotEmpty) tmpRoles.push(tmpRoleItem);
tmpRoles.push(tmpRoleItem);
}
res.roles = tmpRoles;
}
@@ -51,14 +47,19 @@ export function parseBirthSrc(data: Document): TGApp.Archive.Birth.GalSrcFull {
/**
* 解析Gal数据
* @since Beta v0.9.1
* @todo 结合资源数据,补全图片路径
* @param {Document} data - XML数据
* @param {TGApp.Archive.Birth.GalSrcFull} src - 解析的资源数据
* @return {TGApp.Archive.Birth.GalScenes}
*/
export function parseBirthGal(data: Document): TGApp.Archive.Birth.GalScenes {
export function parseBirthGal(
data: Document,
src: TGApp.Archive.Birth.GalSrcFull,
): TGApp.Archive.Birth.GalScenes {
const scenes: TGApp.Archive.Birth.GalScenes = [];
const sceneElements = data.querySelectorAll("scene");
if (sceneElements.length > 1) console.log(sceneElements);
for (let si = 0; si < sceneElements.length; si++) {
const sceneData = parseBirthScenes(sceneElements.item(si));
const sceneData = parseBirthScenes(sceneElements.item(si), src);
scenes.push(sceneData);
}
return scenes;
@@ -67,30 +68,46 @@ export function parseBirthGal(data: Document): TGApp.Archive.Birth.GalScenes {
/**
* 解析场景数据
* @since Beta v0.9.1
* @param {Document} data - XML数据
* @param {TGApp.Archive.Birth.GalSrcFull} src - 解析的资源数据
* @return {TGApp.Archive.Birth.GalScriptScene}
*/
function parseBirthScenes(data: Element): TGApp.Archive.Birth.GalScriptScene {
function parseBirthScenes(
data: Element,
src: TGApp.Archive.Birth.GalSrcFull,
): TGApp.Archive.Birth.GalScriptScene {
const res: TGApp.Archive.Birth.GalScriptScene = {
id: data.getAttribute("id") ?? "",
title: data.getAttribute("title") ?? "",
prev: data.getAttribute("prev") ?? undefined,
bg: "",
scripts: [],
comments: [],
};
const bgElement = data.querySelector("bg");
if (bgElement !== null) res.bg = bgElement.getAttribute("img") ?? "";
if (bgElement !== null) {
const bgKey = bgElement.getAttribute("img") ?? "";
res.bg = src.resource.find((i) => i.id === bgKey)?.src ?? "";
}
const dialogElements = data.querySelectorAll("simple_dialog");
const tmpScripts: TGApp.Archive.Birth.GalDialog[] = [];
for (let di = 0; di < dialogElements.length; di++) {
const dialogEl = dialogElements.item(di);
const imgKey = dialogEl.getAttribute("img") ?? "";
let img2Src = "";
const imgSrc = src.resource.find((i) => i.id === imgKey)?.src ?? "";
if (imgKey.startsWith("aether")) {
img2Src = src.resource.find((i) => i.id === imgKey.replace("aether", "lumine"))?.src ?? "";
}
const scriptItem: TGApp.Archive.Birth.GalDialog = {
key: dialogEl.getAttribute("key") ?? "",
role: dialogEl.getAttribute("chara") ?? undefined,
img: dialogEl.getAttribute("img") ?? undefined,
img: imgSrc,
img2: img2Src,
pos: dialogEl.getAttribute("pos") ?? undefined,
text: dialogEl.textContent ?? "",
};
tmpScripts.push(scriptItem);
}
res.scripts = tmpScripts;
res.comments = tmpScripts;
return res;
}