移除alternate特性

This commit is contained in:
kuangshen04 2024-05-23 13:46:49 +08:00
parent 0e0fefc811
commit f02724ea2c
2 changed files with 35 additions and 54 deletions

View File

@ -1459,21 +1459,16 @@ export class Game {
* @type {string} * @type {string}
*/ */
let audio, let audio,
list = get.Audio.copy(audioList), list = audioList.slice(),
refresh = false; // 当前audioList是否有可播放的音频 refresh = false; // 当前audioList是否有可播放的音频
const check = () => { const check = () => {
if (list.length) return true; if (list.length) return true;
if (refresh) { if (refresh) {
list = get.Audio.copy(audioList); list = audioList.slice();
return true; return true;
} }
//@ts-ignore return false;
if (!list.alternate) return false;
//@ts-ignore
audioList = list.alternate;
list = get.Audio.copy(audioList);
return check();
}; };
/** /**
@ -1494,7 +1489,7 @@ export class Game {
if (autoplay) return play(); if (autoplay) return play();
return () => { return () => {
if (random) list = get.Audio.copy(audioList); if (random) list = audioList.slice();
return play(); return play();
}; };
} }

View File

@ -49,13 +49,13 @@ export class Audio {
}; };
const getInfo = name => get.info(name); const getInfo = name => get.info(name);
const isExist = name => get.info(name); const isExist = name => !!get.info(name);
const getAudioInfo = (name, info, data, options) => { const getAudioInfo = (name, info, data, options) => {
let audioInfo = info.audio; let audioInfo = info.audio;
if (Array.isArray(info.audioname)) data.audioname.addArray(info.audioname); if (Array.isArray(info.audioname)) data.audioname.addArray(info.audioname);
data._audioname = getName(i => data.audioname.includes(i)); data._audioname = getName(i => data.audioname.includes(i));
if (info.audioname2) audioInfo = info.audioname2[getName(i => info.audioname2[i])] || audioInfo; if (info.audioname2) audioInfo = info.audioname2[getName(i => info.audioname2[i])] || audioInfo;
return { audioInfo, isDefault: false }; return audioInfo;
} }
const getName = filter => { const getName = filter => {
@ -111,18 +111,32 @@ export class Audio {
const getAudioInfo = (name, info, data, options) => { const getAudioInfo = (name, info, data, options) => {
let audioInfo = info.dieAudios; let audioInfo = info.dieAudios;
if (audioInfo.length === 0) audioInfo = void 0; if (audioInfo.length === 0) audioInfo = void 0;
return { audioInfo, isDefault: false }; return audioInfo;
} }
return this.#parse({ name, info: skinInfo, options, getInfo, isExist, getAudioInfo }); return this.#parse({ name, info: skinInfo, options, getInfo, isExist, getAudioInfo });
} }
/** /**
* @typedef { {
* type:string,
* defaultPath:string,
* defaultInfo:audioInfo
* } } Options
* @this {typeof get.Audio} * @this {typeof get.Audio}
* @param {object} arg
* @param { string } arg.name
* @param { any } arg.info 这是技能角色等的整个信息不是audioInfo
* @param {object} [arg.data={}] 可变信息
* @param {Options} arg.options 不变信息
* @param {(name:string)=>any} arg.getInfo
* @param {(name:string)=>boolean} arg.isExist
* @param {(name:string,info:any,data:object,options:Options)=>boolean} arg.getAudioInfo
* @returns { textMap[] } * @returns { textMap[] }
*/ */
#parse = function ({ name, info, data: originData = {}, options, getInfo, isExist, getAudioInfo }) { #parse = function ({ name, info, data: originData = {}, options, getInfo, isExist, getAudioInfo }) {
const { type, defaultPath } = options; const { type, defaultPath } = options;
//@ts-ignore
originData.history = []; originData.history = [];
const check = (name, history) => { const check = (name, history) => {
@ -130,40 +144,35 @@ export class Audio {
if (!history.includes(name)) return true; if (!history.includes(name)) return true;
if (history[0] === name) return false; if (history[0] === name) return false;
//deadlock //deadlock
throw new RangeError(`parseAudio: ${name} in ${history} forms a deadlock`); throw new RangeError(`${name} in ${history} forms a deadlock`);
} }
const getInfoAudio = (name, data, { info = void 0, defaultInfo = options.defaultInfo } = {},) => { const getAudio = (name, data, { info = void 0, defaultInfo = options.defaultInfo } = {},) => {
data = JSON.parse(JSON.stringify(data)); data = JSON.parse(JSON.stringify(data));
if (info === void 0 || info === null) { if (info === void 0 || info === null) {
if (!check(name, data.history)) { if (!check(name, data.history)) {
// console.warn(`parse: Cannot find ${name} when parsing ${type} audio.`); console.error(new ReferenceError(`Cannot find ${name} when parsing ${type} audio.`));
return parseAudioWithCache(name, defaultInfo, data, true); return parseAudioWithCache(name, defaultInfo, data);
} }
data.history.unshift(name); data.history.unshift(name);
info = getInfo(name); info = getInfo(name);
} }
const { audioInfo, isDefault } = getAudioInfo(name, info, data, options); const audioInfo = getAudioInfo(name, info, data, options);
if (isDefault || audioInfo === null || audioInfo === void 0) if (audioInfo === null || audioInfo === void 0) return parseAudioWithCache(name, defaultInfo, data);
return parseAudioWithCache(name, defaultInfo, data, true);
return parseAudioWithCache(name, audioInfo, data); return parseAudioWithCache(name, audioInfo, data);
} }
const parseAudioWithCache = (name, audioInfo, data, isDefault = false) => { const parseAudioWithCache = (name, audioInfo, data) => {
const key = this.#getCacheKey(options, name, audioInfo, data); const key = this.#getCacheKey(options, name, audioInfo, data);
const result = this.#Cache[key]; const result = this.#Cache[key];
if (result !== void 0) return this.copy(result); if (result !== void 0) return JSON.parse(JSON.stringify(result));
else { else {
const result = parseAudio(name, audioInfo, data); const result = parseAudio(name, audioInfo, data);
if (isDefault && name.includes("_")) {
name = name.slice(name.indexOf("_") + 1);
result.alternate = getInfoAudio(name, originData);
}
this.#Cache[key] = result; this.#Cache[key] = result;
return this.copy(result); return JSON.parse(JSON.stringify(result));
} }
} }
@ -172,7 +181,7 @@ export class Audio {
if (type === "skill") {//skill的屎山 if (type === "skill") {//skill的屎山
if (audioInfo.length === 2 && typeof audioInfo[0] === "string" && typeof audioInfo[1] === "number") { if (audioInfo.length === 2 && typeof audioInfo[0] === "string" && typeof audioInfo[1] === "number") {
const [newName, number] = audioInfo; const [newName, number] = audioInfo;
return getInfoAudio(newName, data, { defaultInfo: number }).slice(0, number); return getAudio(newName, data, { defaultInfo: number }).slice(0, number);
} }
} }
const map = {}; const map = {};
@ -223,11 +232,11 @@ export class Audio {
audioInfo = audioInfo.slice(0, extIndex); audioInfo = audioInfo.slice(0, extIndex);
} }
if (pathIndex === -1 && extIndex === -1) return getInfoAudio(audioInfo, data); if (pathIndex === -1 && extIndex === -1) return getAudio(audioInfo, data);
return [this.#textMap({ path, name: audioInfo, ext, type, defaultPath })]; return [this.#textMap({ path, name: audioInfo, ext, type, defaultPath })];
} }
return getInfoAudio(name, originData, { info }); return getAudio(name, originData, { info });
} }
/** /**
@ -257,30 +266,12 @@ export class Audio {
} }
} }
/**
* @template {textMap | string} T
* @param {T[]} list
* @this {typeof get.Audio}
* @returns {T[]}
*/
copy(list) {
const result = JSON.parse(JSON.stringify(list));
//@ts-ignore
if (list.alternate) result.alternate = this.copy(list.alternate);
return result;
}
/** /**
* @param {textMap[]} list * @param {textMap[]} list
* @returns {string[]} * @returns {string[]}
*/ */
toFile(list) { toFile(list) {
return (function getResult(textMap) { return list.map(data => data.file);
const result = textMap.map(data => data.file);
//@ts-ignore
if (textMap.alternate) result.alternate = getResult(textMap.alternate);
return result;
})(list);
} }
/** /**
@ -288,12 +279,7 @@ export class Audio {
* @returns {string[]} * @returns {string[]}
*/ */
toText(list) { toText(list) {
return (function getResult(textMap) { return list.map(data => data.text).filter(text => text !== void 0);
const result = textMap.map(data => data.text).filter(text => text !== void 0);
//@ts-ignore
if (textMap.alternate) result.alternate = getResult(textMap.alternate);
return result;
})(list);
} }
} }