Improved Voice.current output

This commit is contained in:
Observer KRypt0n_ 2022-01-02 19:00:32 +02:00
parent 21ec639405
commit 9a2666b8b3
No known key found for this signature in database
GPG key ID: DC5D4EC1303465DA
2 changed files with 38 additions and 16 deletions

View file

@ -1,5 +1,5 @@
import type { VoicePack } from './types/GameData'; import type { VoicePack } from './types/GameData';
import type { InstalledVoice, VoiceLang } from './types/Voice'; import type { VoiceInfo, InstalledVoiceInfo, VoiceLang } from './types/Voice';
import constants from './Constants'; import constants from './Constants';
import Game from './Game'; import Game from './Game';
@ -23,10 +23,11 @@ export default class Voice
/** /**
* Get current installed voice data info * Get current installed voice data info
*/ */
public static get current(): Promise<InstalledVoice> public static get current(): Promise<VoiceInfo>
{ {
return new Promise(async (resolve) => { return new Promise(async (resolve) => {
const persistentPath = `${await constants.paths.gameDataDir}/Persistent/audio_lang_14`; const persistentPath = `${await constants.paths.gameDataDir}/Persistent/audio_lang_14`;
const voiceDir = await constants.paths.voiceDir;
const langs = { const langs = {
'English(US)': 'en-us', 'English(US)': 'en-us',
@ -35,21 +36,29 @@ export default class Voice
'Chinese': 'zn-cn' 'Chinese': 'zn-cn'
}; };
let installedVoice: InstalledVoice = { let installedVoice: VoiceInfo = {
installed: [], installed: [],
active: null active: null
}; };
// Parse installed voice packages // Parse installed voice packages
Neutralino.filesystem.readDirectory(await constants.paths.voiceDir) Neutralino.filesystem.readDirectory(voiceDir)
.then((files) => { .then(async (files) => {
files = files.filter((file) => file.type == 'DIRECTORY') files = files.filter((file) => file.type == 'DIRECTORY')
.map((file) => file.entry); .map((file) => file.entry);
Object.keys(langs).forEach((folder) => { for (const folder of Object.keys(langs))
if (files.includes(folder)) if (files.includes(folder))
installedVoice.installed.push(langs[folder]); {
}); const voiceFiles: { entry: string, type: string }[] = await Neutralino.filesystem.readDirectory(`${voiceDir}/${folder}`);
const latestVoiceFile = voiceFiles.sort((a, b) => a.entry < b.entry ? -1 : 1).pop();
installedVoice.installed.push({
lang: langs[folder],
version: latestVoiceFile ? `${/_([\d]*\.[\d]*)_/.exec(latestVoiceFile.entry)![1]}.0` : null
} as InstalledVoiceInfo);
}
parseActiveVoice(); parseActiveVoice();
}) })
@ -58,14 +67,21 @@ export default class Voice
// Parse active voice package // Parse active voice package
const parseActiveVoice = () => { const parseActiveVoice = () => {
Neutralino.filesystem.readFile(persistentPath) Neutralino.filesystem.readFile(persistentPath)
.then((lang) => { .then(async (lang) => {
installedVoice.active = langs[lang] ?? null; const voiceFiles: { entry: string, type: string }[] = await Neutralino.filesystem.readDirectory(`${voiceDir}/${lang}`);
const latestVoiceFile = voiceFiles.sort((a, b) => a.entry < b.entry ? -1 : 1).pop();
installedVoice.active = {
lang: langs[lang] ?? null,
version: latestVoiceFile ? `${/_([\d]*\.[\d]*)_/.exec(latestVoiceFile.entry)![1]}.0` : null
} as InstalledVoiceInfo;
Debug.log({ Debug.log({
function: 'Voice.current', function: 'Voice.current',
message: { message: {
'active voice': installedVoice.active, 'active voice': `${installedVoice.active.lang} (${installedVoice.active.version})`,
'installed voices': installedVoice.installed.join(', ') 'installed voices': installedVoice.installed.map((voice) => `${voice.lang} (${voice.version})`).join(', ')
} }
}); });

View file

@ -4,12 +4,18 @@ type VoiceLang =
| 'ja-jp' | 'ja-jp'
| 'ko-kr'; | 'ko-kr';
type InstalledVoice = { type InstalledVoiceInfo = {
installed: VoiceLang[]; lang: VoiceLang;
active: VoiceLang|null; version: string|null;
};
type VoiceInfo = {
installed: InstalledVoiceInfo[];
active: InstalledVoiceInfo|null;
}; };
export type { export type {
VoiceLang, VoiceLang,
InstalledVoice InstalledVoiceInfo,
VoiceInfo
}; };