-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathltdNativeRendererAdapter.js
More file actions
358 lines (324 loc) · 13.5 KB
/
Copy pathltdNativeRendererAdapter.js
File metadata and controls
358 lines (324 loc) · 13.5 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import {
hasPinnedNativeRenderOrchestrator,
NativeLtdRuntimeError,
NativeLtdRuntimePool
} from "./ltdNativeRendererClient.js";
const MAX_NATIVE_RUNTIME_WORKERS = 2;
const NATIVE_RUNTIME_STARTUP_TIMEOUT_MS = 30_000;
const NATIVE_RUNTIME_REQUEST_TIMEOUT_MS = 120_000;
const NATIVE_PARTS_CATALOG_SHA256 =
"d8d56e7ee1e291e2e4cc213ef88521b594093a83952747f1d3c8ab0ca5b00523";
const SHA256_PATTERN = /^[0-9a-f]{64}$/;
const nativeRuntimeStates = new Map();
function isPlainObject(value) {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
function requireRegularFile(filePath, label) {
let stat;
try {
stat = fs.statSync(filePath);
} catch (error) {
throw new TypeError(`${label} does not exist.`, { cause: error });
}
if (!stat.isFile()) throw new TypeError(`${label} is not a regular file.`);
return path.resolve(filePath);
}
function requireDirectory(directory, label) {
let stat;
try {
stat = fs.statSync(directory);
} catch (error) {
throw new TypeError(`${label} does not exist.`, { cause: error });
}
if (!stat.isDirectory()) throw new TypeError(`${label} is not a directory.`);
return path.resolve(directory);
}
function listMaterialTextureRoots(rendererRoot) {
const candidates = [
["renderer", "assets", "texture_mips"],
["renderer", "assets", "texture_mips_mii2"],
["renderer", "assets", "reference_outfit_texture_mips"]
].map(parts => path.join(rendererRoot, ...parts));
return candidates
.filter(candidate => fs.existsSync(candidate) && fs.statSync(candidate).isDirectory())
.map(candidate => path.resolve(candidate));
}
function listNativeRuntimeRevisionFiles(rendererRoot) {
const candidates = [
["native_runtime", "build", "ltd_native_runtime.exe"],
["native_runtime", "build", "libzstd.dll"],
["native_runtime", "build", "native_png_zlib1.dll"],
["native_runtime", "generated", "native_parts_catalog.bin"],
["native_runtime", "generated", "native_parts_catalog.json"]
].map(parts => path.join(rendererRoot, ...parts));
return candidates
.filter(candidate => fs.existsSync(candidate) && fs.statSync(candidate).isFile())
.map(candidate => path.resolve(candidate));
}
export function resolveNativeLtdRuntimeConfiguration(rendererRoot) {
const root = requireDirectory(path.resolve(String(rendererRoot || "")), "native LTD renderer root");
const executable = path.join(root, "native_runtime", "build", "ltd_native_runtime.exe");
if (!fs.existsSync(executable)) return null;
const configuration = Object.freeze({
root,
executable: requireRegularFile(executable, "native LTD runtime executable"),
commandArgs: Object.freeze([]),
partsCatalog: requireRegularFile(
path.join(root, "native_runtime", "generated", "native_parts_catalog.bin"),
"native LTD Parts catalog"
),
partsCatalogSha256: NATIVE_PARTS_CATALOG_SHA256,
modelRoot: requireDirectory(
path.join(root, "renderer", "assets", "models"),
"native LTD model root"
),
faceTextureRoot: requireDirectory(
path.join(root, "renderer", "assets", "face_sprite_mips"),
"native LTD face texture root"
),
materialTextureRoots: Object.freeze(listMaterialTextureRoots(root)),
posePath: requireRegularFile(
path.join(root, "renderer", "assets", "animations", "IconPose.json"),
"native LTD pose"
),
revisionFiles: Object.freeze(listNativeRuntimeRevisionFiles(root))
});
if (configuration.materialTextureRoots.length < 1) {
throw new TypeError("native LTD material texture roots are unavailable.");
}
return configuration;
}
function configurationIdentity(configuration) {
return [
configuration.root,
configuration.executable,
...configuration.commandArgs,
configuration.partsCatalog,
...configuration.materialTextureRoots
].join("\0");
}
function getRuntimeState(configuration) {
const identity = configurationIdentity(configuration);
let state = nativeRuntimeStates.get(identity);
if (!state) {
const pool = new NativeLtdRuntimePool({
command: configuration.executable,
commandArgs: [...configuration.commandArgs],
cwd: configuration.root,
maxWorkers: MAX_NATIVE_RUNTIME_WORKERS,
startupTimeoutMs: NATIVE_RUNTIME_STARTUP_TIMEOUT_MS
});
state = {
identity,
pool,
readinessPromise: null,
disabled: false
};
nativeRuntimeStates.set(identity, state);
}
return state;
}
function disableRuntimeState(state) {
if (state.disabled) return;
state.disabled = true;
state.pool.close();
}
async function ensureRuntimeReady(state, timeoutMs) {
if (state.disabled) return false;
if (!state.readinessPromise) {
state.readinessPromise = state.pool.run(
{ op: "readiness" },
{ timeoutMs }
).then(result => {
if (
!isPlainObject(result)
|| result.protocol_ready !== true
|| typeof result.activation_ready !== "boolean"
|| !hasPinnedNativeRenderOrchestrator(result.render_modules)
) {
throw new NativeLtdRuntimeError(
"NATIVE_RUNTIME_PROTOCOL_ERROR",
"The native LTD runtime returned invalid readiness metadata.",
{ workerUnavailable: true }
);
}
if (result.activation_ready !== true) {
disableRuntimeState(state);
return false;
}
return true;
}).catch(error => {
disableRuntimeState(state);
if (error instanceof NativeLtdRuntimeError) return false;
throw error;
});
}
return await state.readinessPromise;
}
function validateRenderRequest(request) {
if (!isPlainObject(request)) throw new TypeError("native LTD render request must be an object");
for (const key of ["ltdPath", "outputDirectory", "presentationContextKind", "presentationContextSha256"]) {
if (typeof request[key] !== "string" || !request[key]) {
throw new TypeError(`${key} is required`);
}
}
if (!path.isAbsolute(request.ltdPath) || !path.isAbsolute(request.outputDirectory)) {
throw new TypeError("native LTD render paths must be absolute");
}
if (!Number.isInteger(request.size) || ![128, 512].includes(request.size)) {
throw new TypeError("native LTD output size must be 128 or 512");
}
if (!['portrait', 'full_body'].includes(request.view)) {
throw new TypeError("native LTD view must be portrait or full_body");
}
if (!SHA256_PATTERN.test(request.presentationContextSha256)) {
throw new TypeError("native LTD presentation context hash is invalid");
}
}
async function validateActivatedResult(result, request) {
const expectedImageName = request.view === "full_body" ? "mii_full_body.png" : "mii.png";
const expectedImagePath = path.resolve(request.outputDirectory, expectedImageName);
const expectedReportPath = path.resolve(request.outputDirectory, "render_report.json");
const expectedReportView = request.view === "full_body"
? "posed_full_body"
: "appearance_bust_portrait";
const output = Array.isArray(result?.render_report?.outputs)
&& result.render_report.outputs.length === 1
? result.render_report.outputs[0]
: null;
if (
result?.activation_ready !== true
|| !hasPinnedNativeRenderOrchestrator(result?.modules)
|| result?.pixels_produced !== true
|| result?.png_produced !== true
|| result?.files_written !== true
|| result?.transaction_cleanup_complete !== true
|| ![expectedImageName, expectedImagePath].includes(result?.output_path)
|| !["render_report.json", expectedReportPath].includes(result?.report_path)
|| !SHA256_PATTERN.test(String(result?.input_sha256 || ""))
|| !SHA256_PATTERN.test(String(result?.output_sha256 || ""))
|| !SHA256_PATTERN.test(String(result?.report_sha256 || ""))
|| result?.render_report?.input_sha256 !== result.input_sha256
|| result?.render_report?.presentation_context?.kind !== request.presentationContextKind
|| result?.render_report?.presentation_context?.sha256 !== request.presentationContextSha256
|| output?.path !== expectedImageName
|| output?.view !== expectedReportView
|| output?.size?.[0] !== request.size
|| output?.size?.[1] !== request.size
|| output?.supersampling?.portable_profile !== "native-resolution-v1"
|| output?.supersampling?.raster_size?.[0] !== request.size
|| output?.supersampling?.raster_size?.[1] !== request.size
|| output?.sha256 !== result.output_sha256
) {
throw new NativeLtdRuntimeError(
"NATIVE_RUNTIME_PROTOCOL_ERROR",
"The native LTD runtime returned an unbound render result.",
{ workerUnavailable: true }
);
}
let outputBytes;
let reportBytes;
let storedReport;
try {
const [outputStat, reportStat] = await Promise.all([
fs.promises.lstat(expectedImagePath),
fs.promises.lstat(expectedReportPath)
]);
if (
!outputStat.isFile()
|| outputStat.isSymbolicLink()
|| !reportStat.isFile()
|| reportStat.isSymbolicLink()
) throw new Error("native output paths must be regular files");
[outputBytes, reportBytes] = await Promise.all([
fs.promises.readFile(expectedImagePath),
fs.promises.readFile(expectedReportPath)
]);
storedReport = JSON.parse(reportBytes.toString("utf8"));
} catch (error) {
throw new NativeLtdRuntimeError(
"NATIVE_RUNTIME_PROTOCOL_ERROR",
"The native LTD runtime did not publish a readable output transaction.",
{ workerUnavailable: true, cause: error }
);
}
const storedOutputSha256 = crypto.createHash("sha256").update(outputBytes).digest("hex");
const storedReportSha256 = crypto.createHash("sha256").update(reportBytes).digest("hex");
const declaredReportBytes = Buffer.from(`${JSON.stringify(result.render_report)}\n`, "utf8");
if (
storedOutputSha256 !== result.output_sha256
|| storedReportSha256 !== result.report_sha256
|| !reportBytes.equals(declaredReportBytes)
|| storedReport?.outputs?.[0]?.sha256 !== storedOutputSha256
) {
throw new NativeLtdRuntimeError(
"NATIVE_RUNTIME_PROTOCOL_ERROR",
"The native LTD runtime output files do not match its authenticated result.",
{ workerUnavailable: true }
);
}
return Object.freeze({ outputBytes, reportBytes, storedReport });
}
export async function tryNativeLtdRender(configuration, request, {
timeoutMs = NATIVE_RUNTIME_REQUEST_TIMEOUT_MS
} = {}) {
if (!configuration) return Object.freeze({ activated: false, reason: "not-configured" });
validateRenderRequest(request);
const state = getRuntimeState(configuration);
if (!await ensureRuntimeReady(state, timeoutMs)) {
return Object.freeze({ activated: false, reason: "not-ready" });
}
let result;
try {
result = await state.pool.run({
op: "render_ltd",
repository_root: configuration.root,
ltd_path: path.resolve(request.ltdPath),
catalog_path: configuration.partsCatalog,
model_root: configuration.modelRoot,
face_texture_root: configuration.faceTextureRoot,
material_texture_roots: [...configuration.materialTextureRoots],
pose_path: configuration.posePath,
output_dir: path.resolve(request.outputDirectory),
view: request.view,
output_size: request.size,
supersampling: {
profile: "native-resolution-v1",
raster_size: request.size
},
presentation_context: {
kind: request.presentationContextKind,
sha256: request.presentationContextSha256
}
}, { timeoutMs });
} catch (error) {
if (error instanceof NativeLtdRuntimeError && error.workerUnavailable === true) {
disableRuntimeState(state);
return Object.freeze({ activated: false, reason: "unavailable", error });
}
return Object.freeze({ activated: false, reason: "request-rejected", error });
}
if (
result?.activation_ready !== true
|| result?.pixels_produced !== true
|| result?.png_produced !== true
) {
return Object.freeze({ activated: false, reason: "request-not-supported" });
}
try {
const validatedFiles = await validateActivatedResult(result, request);
result = Object.freeze({ ...result, validatedFiles });
} catch (error) {
disableRuntimeState(state);
return Object.freeze({ activated: false, reason: "invalid-result", error });
}
return Object.freeze({ activated: true, result });
}
export function closeNativeLtdRuntimePools() {
for (const state of nativeRuntimeStates.values()) state.pool.close();
nativeRuntimeStates.clear();
}
process.once("exit", closeNativeLtdRuntimePools);