-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmigrate.js
189 lines (182 loc) · 7.69 KB
/
migrate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const fs = require('fs');
const path = require('path');
const POGOProtos = require('pogo-protos');
const gameMaster = require('pokemongo-game-master');
function pokeFilename(pokemonId, evolution = 0, form = 0, costume = 0, gender = 0, shiny = false) {
let result = String(pokemonId);
if (evolution) {
result += '-e' + evolution;
}
if (form) {
result += '-f' + form;
}
if (costume) {
result += '-c' + costume;
}
if (gender) {
result += '-g' + gender;
}
if (shiny) {
result += '-shiny';
}
return result;
}
function extractForms(pokemon, template, pokemonId, computeSuffix, field = 'forms') {
const formSettings = template.data[Object.keys(template.data)[1]];
let forms = formSettings[Object.keys(formSettings)[1]];
if (forms === undefined) {
const pokemonName = template.templateId.split('_POKEMON_', 2)[1];
forms = [{
"form": pokemonName + '_NORMAL'
}, {
"form": pokemonName + '_SHADOW'
}, {
"form": pokemonName + '_PURIFIED'
}];
}
const result = [];
for (const formData of forms) {
const form = formData[Object.keys(formData)[0]];
const formId = computeSuffix(form);
if (formId) {
result.push(formId);
} else {
console.warn('Unrecognized form/temp evolution', form);
}
}
if (pokemon[pokemonId] === undefined) {
pokemon[pokemonId] = {};
}
pokemon[pokemonId][field] = result;
}
const converters = {
pmsf: async function (inDir, outDir, pokemon) {
const outputs = {};
const maxCostume = Math.max.apply(null, Object.values(POGOProtos.Enums.Costume));
for (const filename of await fs.promises.readdir(inDir)) {
let match = /^pokemon_icon_(\d{3,})(?:_00)?(?:_([1-9]\d*))?(?:_([1-9]\d*))?(_shiny)?\.png$/.exec(filename);
if (match === null) {
if (filename.startsWith('pokemon_icon_') && filename.endsWith('.png')) {
console.warn('Unrecognized file', filename);
}
continue;
}
let pokemonId = parseInt(match[1]);
const field1 = parseInt(match[2]);
const field2 = parseInt(match[3]);
let pokemonEntry = pokemon[pokemonId];
let output;
let overrideActive = '';
if (pokemonEntry) {
let evolution = 0;
let form = 0;
let costume = 0;
if (!isNaN(field1)) {
if ((pokemonEntry.forms || []).indexOf(field1) >= 0) {
form = field1;
if (!isNaN(field2)) {
if ((pokemonEntry.evolutions || []).indexOf(field2) >= 0) {
evolution = field2;
} else if (field2 <= maxCostume) {
costume = field2;
} else {
console.warn('Unrecognized field', field2, 'in', filename);
continue;
}
}
} else if ((pokemonEntry.evolutions || []).indexOf(field1) >= 0) {
evolution = field1;
if (!isNaN(field2)) {
if (field2 <= maxCostume) {
costume = field2;
} else {
console.warn('Unrecognized field', field2, 'in', filename);
continue;
}
}
} else if (field1 <= maxCostume) {
costume = field1;
} else {
console.warn('Unrecognized field', field1, 'in', filename);
continue;
}
}
if (form !== 0 && form === pokemonEntry.forms[0]) {
form = 0;
overrideActive = 'pokemon_icon_' + match[1] + '_00';
if (match[3]) {
overrideActive += '_' + match[3];
}
if (match[4]) {
overrideActive += match[4];
}
overrideActive += '.png';
}
output = pokeFilename(pokemonId, evolution, form, costume, 0, match[4] !== undefined);
} else if (isNaN(field1) && isNaN(field2)) {
output = pokeFilename(pokemonId);
} else {
console.warn('Unrecognized pokemon', filename);
continue;
}
if (outputs[output] && outputs[output] !== overrideActive) {
console.warn('Duplicate found for', output, ':', outputs[output], '!=', filename, overrideActive);
} else {
outputs[output] = filename;
}
await fs.promises.copyFile(path.join(inDir, filename), path.join(outDir, output + '.png'));
}
},
rdm: async function (inDir, outDir, pokemon) {
console.error('Not supported yet lol');
process.exit(1);
},
};
(async () => {
let format = converters[process.argv[2]];
let inDir = process.argv[3];
let outDir = process.argv[4];
if (!format || !inDir || !outDir) {
console.error('Usage: node migrate.js <pmsf|rdm> <input dir> <output dir>');
process.exit(1);
}
inDir = path.resolve(inDir);
outDir = path.resolve(outDir);
console.log('Reading game master...');
const pokemon = {};
const gameMasterContent = await gameMaster.getVersion('latest', 'json');
for (const template of gameMasterContent.template) {
if (template.templateId.startsWith('FORMS_V')) {
const pokemonId = parseInt(template.templateId.substr(7, 4));
if (!pokemonId) {
console.warn('Unrecognized templateId', template.templateId);
} else {
extractForms(pokemon, template, pokemonId, (form) => POGOProtos.Enums.Form[form]);
}
} else if (template.templateId.startsWith('TEMPORARY_EVOLUTION_V')) {
const pokemonId = parseInt(template.templateId.substr(21, 4));
if (!pokemonId) {
console.warn('Unrecognized templateId', template.templateId);
} else {
extractForms(pokemon, template, pokemonId, (evolution) => {
// <RANDOMIZED_NAME>_TEMP_EVOLUTION_<NAME> => EVOLUTION_<NAME>
return POGOProtos.Enums.PokemonEvolution[evolution.split('_TEMP_', 2)[1]];
}, 'evolutions');
}
}
}
if (pokemon[18].evolutions) {
console.warn('Pidgeotto mega fixed')
} else {
pokemon[18].evolutions = [POGOProtos.Enums.PokemonEvolution.EVOLUTION_MEGA];
}
pokemon[POGOProtos.Enums.PokemonId.PONYTA].forms.push(POGOProtos.Enums.Form.PONYTA_GALARIAN);
pokemon[POGOProtos.Enums.PokemonId.RAPIDASH].forms.push(POGOProtos.Enums.Form.RAPIDASH_GALARIAN);
pokemon[POGOProtos.Enums.PokemonId.SLOWPOKE].forms.push(POGOProtos.Enums.Form.SLOWPOKE_GALARIAN);
pokemon[POGOProtos.Enums.PokemonId.SLOWBRO].forms.push(POGOProtos.Enums.Form.SLOWBRO_GALARIAN);
pokemon[POGOProtos.Enums.PokemonId.MR_MIME].forms.push(POGOProtos.Enums.Form.MR_MIME_GALARIAN);
pokemon[POGOProtos.Enums.PokemonId.CORSOLA].forms.push(POGOProtos.Enums.Form.CORSOLA_GALARIAN);
pokemon[POGOProtos.Enums.PokemonId.YAMASK].forms.push(POGOProtos.Enums.Form.YAMASK_GALARIAN);
await fs.promises.mkdir(outDir, { recursive: true });
await format(inDir, outDir, pokemon);
})();