UNPKG

pogo-data-generator

Version:
116 lines (115 loc) 3.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jszip_1 = __importDefault(require("jszip")); class ApkReader { texts; codeMap; files; apkFilename; constructor() { this.texts = {}; this.codeMap = { 'pt-br': 'pt-br', 'zh-tw': 'zh-tw', 'en-us': 'en', 'fr-fr': 'fr', 'de-de': 'de', 'hi-in': 'hi', 'id-id': 'id', 'it-it': 'it', 'ja-jp': 'ja', 'ko-kr': 'ko', 'ru-ru': 'ru', 'es-es': 'es', 'es-mx': 'es-mx', 'th-th': 'th', 'tr-tr': 'tr', }; this.files = null; this.apkFilename = null; } removeEscapes(str) { return str.replace(/\r/g, '').replace(/\n/g, '').replace(/"/g, '”'); } async getLatestApkFilename() { try { const index = await fetch('https://mirror.unownhash.com/index.json'); if (!index.ok) { throw new Error('Unable to fetch index'); } const data = await index.json(); return data[0].filename; } catch (e) { console.warn(e, 'Issue with downloading APK index'); return null; } } async fetchApk(filename) { this.apkFilename = null; this.files = null; try { const first = filename || (await this.getLatestApkFilename()); if (!first) { throw new Error('Unable to determine latest APK filename'); } const response = await fetch(`https://mirror.unownhash.com/apks/${first}`); if (!response.ok) { throw new Error('Unable to fetch APK'); } const apk = await response.arrayBuffer(); const raw = await new jszip_1.default().loadAsync(apk); const file = raw.files['base.apk']; if (!file) { throw new Error('Missing base.apk in APK bundle'); } const buffer = await file.async('nodebuffer'); this.files = await new jszip_1.default().loadAsync(buffer); this.apkFilename = first; } catch (e) { console.warn(e, 'Issue with downloading APK'); } } async extractTexts() { if (!this.files) return; try { const textFiles = Object.keys(this.files.files).filter((file) => file.startsWith('assets/text')); await Promise.all(textFiles.map(async (file) => { try { const text = await this.files.file(file)?.async('text'); const { data } = JSON.parse(text); const relativePath = file .replace('assets/text/', '') .replace('.json', '') .replace('i18n_', ''); const code = this.codeMap[relativePath]; if (!code) { throw new Error(relativePath); } this.texts[code] = {}; for (let i = 0; i < data.length; i++) { this.texts[code][data[i]] = this.removeEscapes(data[++i]); } } catch (e) { if (e instanceof Error) { console.error('Unknown language code', e.message); } } })); } catch (e) { console.warn(e, 'Issue with extracting texts'); } } cleanup() { if (this.files) delete this.files; } } exports.default = ApkReader;