Skip to content

Commit

Permalink
Merge pull request #85 from WatWowMap/feat-extract-apk
Browse files Browse the repository at this point in the history
feat: extract texts from apk
  • Loading branch information
TurtIeSocks authored Jul 27, 2023
2 parents 81bcac5 + 26bf2b6 commit b8e8cd5
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 56 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pogo-data-generator",
"version": "1.13.0",
"version": "1.14.0",
"description": "Pokemon GO project data generator",
"author": "TurtIeSocks",
"license": "Apache-2.0",
Expand Down Expand Up @@ -37,6 +37,7 @@
},
"dependencies": {
"@na-ji/pogo-protos": "<3.0.0",
"jszip": "^3.10.1",
"node-fetch": "2.6.7"
},
"engines": {
Expand Down
90 changes: 90 additions & 0 deletions src/classes/Apk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import JSZip from 'jszip'

export default class ApkReader {
texts: Record<string, Record<string, string>>
codeMap: Record<string, string>
files: JSZip | null

constructor() {
this.texts = {}
this.codeMap = {
brazilianportuguese: 'pt-br',
chinesetraditional: 'zh-tw',
english: 'en',
french: 'fr',
german: 'de',
hindi: 'hi',
indonesian: 'id',
italian: 'it',
japanese: 'ja',
korean: 'ko',
russian: 'ru',
spanish: 'es',
thai: 'th',
turkish: 'tr',
}
this.files = null
}

removeEscapes(str: string) {
return str.replace(/\r/g, '').replace(/\n/g, '').replace(/\"/g, '”')
}

async fetchApk() {
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()
const first = data[0].filename

const response = await fetch(`https://mirror.unownhash.com/apks/${first}`)
const apk = await response.arrayBuffer()
const zip = new JSZip()
this.files = await zip.loadAsync(apk)
} 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 += 2) {
this.texts[code][data[i]] = this.removeEscapes(data[i + 1])
}
} catch (e) {
console.error('Unknown language code', e)
}
}),
)
} catch (e) {
console.warn(e, 'Issue with extracting texts')
}
}

cleanup() {
if (this.files) delete this.files
}
}
12 changes: 9 additions & 3 deletions src/classes/Item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ export default class Item extends Masterfile {
typeof itemId === 'string' ? Rpc.Item[itemId as ItemProto] : itemId
this.parsedItems[id] = {
itemId: id,
itemName: this.capitalize(templateId.replace('ITEM_', '')),
itemName: templateId
? this.capitalize(templateId.replace('ITEM_', ''))
: '',
proto: templateId,
type: this.capitalize(itemType.replace('ITEM_TYPE_', '')),
category: this.capitalize(category.replace('ITEM_CATEGORY_', '')),
type: typeof itemType === 'string'
? this.capitalize(itemType.replace('ITEM_TYPE_', ''))
: '',
category: category
? this.capitalize(category.replace('ITEM_CATEGORY_', ''))
: '',
minTrainerLevel: dropTrainerLevel,
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/classes/Translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ export default class Translations extends Masterfile {
}
}

set fromApk(values: TranslationKeys) {
this.rawTranslations = values
}

removeEscapes(str: string) {
return str.replace(/\r/g, '').replace(/\n/g, '').replace(/\"/g, '”')
}
Expand Down Expand Up @@ -210,9 +214,11 @@ export default class Translations extends Masterfile {

splitText.forEach((line: string, i: number) => {
if (line?.startsWith('RESOURCE ID')) {
this.rawTranslations[locale][
this.removeEscapes(line.replace('RESOURCE ID: ', ''))
] = this.removeEscapes(splitText[i + 1].replace('TEXT: ', ''))
const key = this.removeEscapes(line.replace('RESOURCE ID: ', ''))
const value = this.removeEscapes(
splitText[i + 1].replace('TEXT: ', ''),
)
this.rawTranslations[locale][key] = value
}
})
} catch (e) {
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { AllInvasions, FinalResult } from './typings/dataTypes'
import { InvasionInfo } from './typings/pogoinfo'
import { NiaMfObj } from './typings/general'
import ApkReader from './classes/Apk'

export async function generate({
template,
Expand Down Expand Up @@ -59,6 +60,12 @@ export async function generate({
const AllWeather = new Weather()
const AllTranslations = new Translations(translations.options, translationApkUrl, translationRemoteUrl)
const AllPokeApi = new PokeApi()
const apk = new ApkReader()

await apk.fetchApk()
await apk.extractTexts()
apk.cleanup()
AllTranslations.fromApk = apk.texts

const data: NiaMfObj[] = await AllPokemon.fetch(urlToFetch)

Expand Down
2 changes: 1 addition & 1 deletion src/typings/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export interface NiaMfObj {
}
itemSettings?: {
itemId: string | number
itemType: string
itemType: string | number
category: string
dropTrainerLevel: number
}
Expand Down
116 changes: 76 additions & 40 deletions tests/defaultValues.json
Original file line number Diff line number Diff line change
Expand Up @@ -396,78 +396,100 @@
"name": "Zygarde",
"pokedex_id": 718,
"default_form_id": 2591,
"types": [
"Ground",
"Dragon"
],
"quick_moves": [
"Bite",
"Zen Headbutt",
"Dragon Tail"
],
"charged_moves": [
"Hyper Beam",
"Earthquake",
"Bulldoze",
"Outrage",
"Crunch"
],
"gen_id": 6,
"generation": "Kalos",
"forms": {
"2591": {
"name": "Ten Percent",
"proto": "ZYGARDE_TEN_PERCENT"
"proto": "ZYGARDE_TEN_PERCENT",
"attack": 205,
"defense": 173,
"stamina": 144,
"height": 1.2,
"weight": 33.5,
"evolutions": [
{
"pokemon": 718,
"form": 2592
}
]
},
"2592": {
"name": "Fifty Percent",
"proto": "ZYGARDE_FIFTY_PERCENT"
"proto": "ZYGARDE_FIFTY_PERCENT",
"evolutions": [
{
"pokemon": 718,
"form": 2593
}
]
},
"2593": {
"name": "Complete",
"proto": "ZYGARDE_COMPLETE"
"proto": "ZYGARDE_COMPLETE",
"attack": 184,
"defense": 207,
"stamina": 389,
"height": 4.5,
"weight": 610
},
"2823": {
"name": "Complete Ten Percent",
"proto": "ZYGARDE_COMPLETE_TEN_PERCENT"
"proto": "ZYGARDE_COMPLETE_TEN_PERCENT",
"attack": 205,
"defense": 173,
"stamina": 144,
"height": 1.2,
"weight": 33.5
},
"2824": {
"name": "Complete Fifty Percent",
"proto": "ZYGARDE_COMPLETE_FIFTY_PERCENT"
}
},
"gen_id": 6,
"generation": "Kalos",
"types": [
"Ground",
"Dragon"
],
"quick_moves": [
"Bite",
"Zen Headbutt",
"Dragon Tail"
],
"charged_moves": [
"Hyper Beam",
"Earthquake",
"Bulldoze",
"Outrage",
"Crunch"
],
"family": 718,
"legendary": true,
"attack": 203,
"defense": 232,
"stamina": 239,
"height": 5,
"weight": 305,
"family": 718,
"legendary": true,
"mythic": false,
"buddy_group_number": 2,
"buddy_distance": 20,
"third_move_stardust": 100000,
"third_move_candy": 100,
"attack": 203,
"defense": 232,
"stamina": 239
"evolutions": [
{
"pokemon": 718,
"form": 2592
}
]
},
"719": {
"name": "Diancie",
"pokedex_id": 719,
"default_form_id": 0,
"forms": {
"0": {
"proto": "FORM_UNSET"
}
},
"gen_id": 6,
"generation": "Kalos",
"types": [
"Rock",
"Fairy"
],
"attack": 190,
"defense": 285,
"stamina": 137,
"height": 0.7,
"weight": 8.8,
"quick_moves": [
"Tackle",
"Rock Throw"
Expand All @@ -477,9 +499,22 @@
"Power Gem",
"Moonblast"
],
"gen_id": 6,
"generation": "Kalos",
"forms": {
"0": {
"proto": "FORM_UNSET"
}
},
"attack": 190,
"defense": 285,
"stamina": 137,
"height": 0.7,
"weight": 8.8,
"family": 719,
"legendary": false,
"mythic": true,
"buddy_group_number": 2,
"buddy_distance": 20,
"third_move_stardust": 100000,
"third_move_candy": 100,
Expand All @@ -488,7 +523,8 @@
"attack": 342,
"defense": 235,
"stamina": 137,
"unreleased": true
"height": 1.1,
"weight": 27.8
}
}
}
Expand Down
Loading

0 comments on commit b8e8cd5

Please sign in to comment.