-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackfillConsoleFields.js
More file actions
242 lines (203 loc) · 6.66 KB
/
Copy pathbackfillConsoleFields.js
File metadata and controls
242 lines (203 loc) · 6.66 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import "./setEnvs.js";
import miijs from "miijs";
import { cleanupMongoResources, connectionPromise, Miis } from "./database.js";
import { getMiiIdentityHash } from "./miiIdentityHash.js";
const MII_DATA_TOP_LEVEL_KEYS = Object.freeze([
"console",
"meta",
"perms",
"general",
"face",
"hair",
"eyes",
"eyebrows",
"nose",
"mouth",
"beard",
"glasses",
"mole",
"tl",
"mt"
]);
const OFFICIAL_TAG_CONSOLE_PRIORITY = Object.freeze([
{ console: "DS", keys: ["ds", "nintendods"] },
{ console: "Wii", keys: ["wii", "nintendowii"] },
{ console: "3DS", keys: ["3ds", "nintendo3ds"] },
{ console: "Wii U", keys: ["wiiu", "nintendowiiu"] },
{ console: "Switch", keys: ["switch", "nintendoswitch"] }
]);
function getArgValue(name) {
const prefix = `${name}=`;
const match = process.argv.find(arg => arg.startsWith(prefix));
return match ? match.slice(prefix.length) : "";
}
function normalizeTagKey(value) {
return String(value || "")
.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, "");
}
function getOfficialConsoleFromTags(mii) {
if (!mii?.official) return "";
const topLevelCategoryTags = (Array.isArray(mii.officialCategories) ? mii.officialCategories : [])
.map(path => String(path || "").split(/[>/]/)[0]);
const tagKeys = new Set([
...(Array.isArray(mii.tags) ? mii.tags : []),
...topLevelCategoryTags
].map(normalizeTagKey).filter(Boolean));
if (tagKeys.size === 0) return "";
for (const candidate of OFFICIAL_TAG_CONSOLE_PRIORITY) {
if (candidate.keys.some(key => tagKeys.has(key))) {
return candidate.console;
}
}
return "";
}
function toMiiDataOnly(mii) {
if (!mii || typeof mii !== "object") return mii;
const out = {};
for (const key of MII_DATA_TOP_LEVEL_KEYS) {
if (!Object.prototype.hasOwnProperty.call(mii, key)) continue;
out[key] = mii[key];
}
return out;
}
function hasMeaningfulIdentifier(value) {
const normalized = String(value || "")
.trim()
.replace(/[^a-fA-F0-9]/g, "");
return Boolean(normalized && !/^0+$/.test(normalized));
}
function hasSwitchIdentifier(mii) {
return hasMeaningfulIdentifier(mii?.meta?.miiId) || hasMeaningfulIdentifier(mii?.meta?.systemId);
}
function hasTomodachiLifeData(mii) {
const tlData = mii?.tl;
if (tlData === null || tlData === undefined) return false;
if (typeof tlData !== "object") return true;
return Object.keys(tlData).length > 0;
}
function getOriginalDevice(mii) {
const value = Number.parseInt(mii?.meta?.originalDevice, 10);
return Number.isInteger(value) ? value : null;
}
function refine3dsConsole(mii) {
if (getOriginalDevice(mii) === 4) {
return {
console: "Wii U",
reason: "CFCD round-trip hash match with Wii U original device"
};
}
if (hasTomodachiLifeData(mii)) {
return {
console: "TL 3DS",
reason: "CFCD round-trip hash match with Tomodachi Life data"
};
}
return {
console: "3DS",
reason: "CFCD round-trip hash match"
};
}
async function roundTripIdentityMatches(miiData, format, originalHash) {
if (!originalHash) return false;
try {
const sourceMii = await miijs.Mii.create(miiData);
const encoded = await sourceMii.encode(format);
const decodedMii = await miijs.Mii.create(encoded);
const decodedHash = getMiiIdentityHash(decodedMii.fields);
return Boolean(decodedHash && decodedHash === originalHash);
} catch (error) {
return false;
}
}
async function inferConsole(mii) {
const officialConsole = getOfficialConsoleFromTags(mii);
if (officialConsole) {
return {
console: officialConsole,
reason: "official tag"
};
}
const miiData = toMiiDataOnly(mii);
const originalHash = getMiiIdentityHash(miiData);
if (await roundTripIdentityMatches(miiData, "rcd", originalHash)) {
return {
console: "Wii",
reason: "RCD round-trip hash match"
};
}
if (await roundTripIdentityMatches(miiData, "cfcd", originalHash)) {
return refine3dsConsole(miiData);
}
if (hasSwitchIdentifier(miiData)) {
return {
console: "Switch",
reason: "Mii ID or System ID present"
};
}
return {
console: "Mii Studio",
reason: "fallback"
};
}
function getExistingConsole(mii) {
return String(mii?.console || mii?.meta?.console || "").trim();
}
async function main() {
const dryRun = process.argv.includes("--dry-run");
const limit = Number.parseInt(getArgValue("--limit"), 10);
const counters = {
checked: 0,
changed: 0,
unchanged: 0,
failed: 0
};
await connectionPromise;
console.log(`[console-backfill] Starting${dryRun ? " dry run" : ""}.`);
const query = {};
const cursor = Miis.find(query).cursor();
for await (const mii of cursor) {
if (Number.isInteger(limit) && limit > 0 && counters.checked >= limit) {
break;
}
counters.checked += 1;
try {
const { console: consoleValue, reason } = await inferConsole(mii);
const existingConsole = getExistingConsole(mii);
if (existingConsole === consoleValue && mii?.meta?.console === consoleValue) {
counters.unchanged += 1;
continue;
}
counters.changed += 1;
const id = mii.id || String(mii._id);
console.log(`[console-backfill] ${id}: ${existingConsole || "(blank)"} -> ${consoleValue} (${reason})`);
if (!dryRun) {
await Miis.updateOne(
{ _id: mii._id },
{
$set: {
console: consoleValue,
"meta.console": consoleValue
}
}
);
}
} catch (error) {
counters.failed += 1;
console.warn(`[console-backfill] Failed ${mii?.id || mii?._id}: ${error.message}`);
}
}
console.log(`[console-backfill] Done. Checked ${counters.checked}, changed ${counters.changed}, unchanged ${counters.unchanged}, failed ${counters.failed}.`);
if (dryRun) {
console.log("[console-backfill] Dry run only; no records were updated.");
}
}
main()
.catch((error) => {
console.error("[console-backfill] Fatal error:", error);
process.exitCode = 1;
})
.finally(async () => {
await cleanupMongoResources();
});