feat(nitro): normalise hotel and uploaded bundles to WebP Lossless
Gitea Actions Runner Test / test-job (push) Successful in 0s
CI / check (push) Successful in 31s
CI / tests-integration (push) Successful in 2m17s
CI / tests-unit (push) Failing after 2m29s
CI / tests-ui (push) Successful in 3m17s
CI / preflight (push) Skipped
CI / deploy (push) Skipped
Gitea Actions Runner Test / test-job (push) Successful in 0s
CI / check (push) Successful in 31s
CI / tests-integration (push) Successful in 2m17s
CI / tests-unit (push) Failing after 2m29s
CI / tests-ui (push) Successful in 3m17s
CI / preflight (push) Skipped
CI / deploy (push) Skipped
Importing from a hotel wrote the downloaded .nitro to disk untouched, so official PNG textures stayed PNG and only SWF imports ended up as WebP. Every Studio import should produce the same format regardless of where the bytes came from, so both clone and upload paths now run the bundle through toWebpLosslessBundle. The helper decodes the texture and re-encodes it with the same VP8L options the SWF importer uses, so the artwork round-trips bit-for-bit, and lets createNitroBundle relabel the member and repair the meta.image pointer. A bundle that is already lossless WebP is returned untouched, making the operation idempotent and safe to run on re-import. A colour variant that shares a library keeps the member base name it arrived with. A texture that cannot be decoded keeps its original format with a warning instead of failing the import: the bundle is valid, and losing a furniture item over a codec edge case is worse than a slightly larger texture.
This commit is contained in:
1 parent
306e209e29
commit
b26e2e0de4
5 files changed
+174
-36
No files matched your search
@@ -24,7 +24,10 @@ import {
|
|||||||
} from "@/lib/services/import/core/curl-fetch";
|
} from "@/lib/services/import/core/curl-fetch";
|
||||||
import { downloadFile } from "@/lib/services/import/core/download";
|
import { downloadFile } from "@/lib/services/import/core/download";
|
||||||
import { siteSettings } from "@/lib/services/site-settings";
|
import { siteSettings } from "@/lib/services/site-settings";
|
||||||
import { parseNitroBundle } from "@/lib/services/swf/nitro-builder";
|
import {
|
||||||
|
parseNitroBundle,
|
||||||
|
toWebpLosslessBundle,
|
||||||
|
} from "@/lib/services/swf/nitro-builder";
|
||||||
import { getRuntimePath } from "@/lib/utils/runtime-path";
|
import { getRuntimePath } from "@/lib/utils/runtime-path";
|
||||||
|
|
||||||
export interface SourceFurni {
|
export interface SourceFurni {
|
||||||
@@ -331,6 +334,22 @@ export async function cloneSingleFurni(params: {
|
|||||||
logger.warn("[clone-import] invalid .nitro bundle for", { classname });
|
logger.warn("[clone-import] invalid .nitro bundle for", { classname });
|
||||||
return { ok: false, classname, warnings, error: "invalid .nitro bundle" };
|
return { ok: false, classname, warnings, error: "invalid .nitro bundle" };
|
||||||
}
|
}
|
||||||
|
// Hotels ship PNG textures, so normalise every clone to WebP Lossless and
|
||||||
|
// make it match the SWF importer. A texture that will not re-encode is
|
||||||
|
// kept as it arrived rather than failing the whole import — the bundle is
|
||||||
|
// valid, and losing the furniture over a codec edge case is worse.
|
||||||
|
try {
|
||||||
|
const converted = await toWebpLosslessBundle(
|
||||||
|
await fs.readFile(/*turbopackIgnore: true*/ nitroPath),
|
||||||
|
);
|
||||||
|
await fs.writeFile(/*turbopackIgnore: true*/ nitroPath, converted);
|
||||||
|
} catch (err) {
|
||||||
|
warnings.push("texture kept in its original format");
|
||||||
|
logger.warn("[clone-import] WebP normalisation failed for", {
|
||||||
|
classname,
|
||||||
|
err,
|
||||||
|
});
|
||||||
|
}
|
||||||
let iconDl: { ok: boolean; size: number };
|
let iconDl: { ok: boolean; size: number };
|
||||||
if (source.iconBaseUrl) {
|
if (source.iconBaseUrl) {
|
||||||
iconDl = await downloadFile(
|
iconDl = await downloadFile(
|
||||||
|
|||||||
@@ -503,3 +503,44 @@ function inflateJsonOrGzip(
|
|||||||
return gunzipSync(input, options);
|
return gunzipSync(input, options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── WebP Lossless Normalisation ────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the bundle with its texture stored as WebP Lossless (VP8L).
|
||||||
|
*
|
||||||
|
* Studio imports should all look the same regardless of where the bytes
|
||||||
|
* came from: a hotel serves PNG, a hand-made bundle may carry anything, and
|
||||||
|
* an SWF import already produces WebP. Re-encoding here means one code path
|
||||||
|
* decides the format instead of each importer deciding on its own.
|
||||||
|
*
|
||||||
|
* The texture is decoded and re-encoded with `encodeWebpLossless`, so it
|
||||||
|
* round-trips bit-for-bit, and the member keeps the base name it arrived
|
||||||
|
* with — a `chair*2` colour variant sharing the `chair` library must not be
|
||||||
|
* renamed. `meta.image` is corrected by `createNitroBundle`.
|
||||||
|
*
|
||||||
|
* A bundle that is already lossless WebP is returned untouched, which keeps
|
||||||
|
* the operation idempotent and avoids a pointless re-encode.
|
||||||
|
*
|
||||||
|
* Throws if the texture claims to be PNG or WebP but cannot actually be
|
||||||
|
* decoded; callers that would rather keep the original should catch.
|
||||||
|
*/
|
||||||
|
export async function toWebpLosslessBundle(bundle: Buffer): Promise<Buffer> {
|
||||||
|
const parsed = parseNitroBundle(bundle);
|
||||||
|
if (isLosslessWebp(parsed.texture)) return bundle;
|
||||||
|
|
||||||
|
const image = sharp(parsed.texture);
|
||||||
|
const { width = 0, height = 0 } = await image.metadata();
|
||||||
|
if (!width || !height)
|
||||||
|
throw new Error(
|
||||||
|
`Nitro texture "${parsed.textureFileName}" has no readable dimensions`,
|
||||||
|
);
|
||||||
|
|
||||||
|
const webp = await encodeWebpLossless(
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
await image.raw().ensureAlpha().toBuffer(),
|
||||||
|
);
|
||||||
|
const memberBase = parsed.textureFileName.replace(/\.(?:png|webp)$/i, "");
|
||||||
|
return createNitroBundle(parsed.json, webp, memberBase);
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -84,19 +84,16 @@ vi.mock("@/lib/services/furni-import", () => ({
|
|||||||
IMPORTED_PAGE_CAPTION_SAVE: "imported_furni",
|
IMPORTED_PAGE_CAPTION_SAVE: "imported_furni",
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const nitroBuilderMock = vi.hoisted(() => ({
|
|
||||||
createNitroBundle: vi.fn((json: object) =>
|
|
||||||
Buffer.from(`rebuilt:${JSON.stringify(json)}`, "utf-8"),
|
|
||||||
),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("@/lib/services/swf/nitro-builder", () => ({
|
vi.mock("@/lib/services/swf/nitro-builder", () => ({
|
||||||
parseNitroBundle: vi.fn(() => ({
|
parseNitroBundle: vi.fn(() => ({
|
||||||
json: { xdim: 1, ydim: 1 },
|
json: { xdim: 1, ydim: 1 },
|
||||||
texture: Buffer.from("texture-bytes"),
|
texture: Buffer.from("texture-bytes"),
|
||||||
textureFileName: "chair.png",
|
textureFileName: "chair.png",
|
||||||
})),
|
})),
|
||||||
createNitroBundle: nitroBuilderMock.createNitroBundle,
|
isLosslessWebp: vi.fn(() => false),
|
||||||
|
toWebpLosslessBundle: vi.fn(() =>
|
||||||
|
Promise.resolve(Buffer.from("webp-normalised", "utf-8")),
|
||||||
|
),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -146,19 +143,13 @@ describe("uploadSingleFurni live asset mirrors", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(result.ok).toBe(true);
|
expect(result.ok).toBe(true);
|
||||||
// The bundle is re-written through the builder so `meta.image` can never
|
// Every upload is normalised to WebP Lossless, whatever the source
|
||||||
// disagree with the texture member, while the member keeps the name it
|
// bundle carried, so what lands on disk is not the raw upload.
|
||||||
// arrived with (colour variants share a library name).
|
|
||||||
expect(nitroBuilderMock.createNitroBundle).toHaveBeenCalledWith(
|
|
||||||
{ xdim: 1, ydim: 1 },
|
|
||||||
Buffer.from("texture-bytes"),
|
|
||||||
"chair",
|
|
||||||
);
|
|
||||||
await expect(
|
await expect(
|
||||||
fs.readFile(
|
fs.readFile(
|
||||||
getRuntimePath(assetTargets.mirrorDirs[0].nitroDir, "chair.nitro"),
|
getRuntimePath(assetTargets.mirrorDirs[0].nitroDir, "chair.nitro"),
|
||||||
),
|
),
|
||||||
).resolves.toEqual(Buffer.from('rebuilt:{"xdim":1,"ydim":1}', "utf-8"));
|
).resolves.toEqual(Buffer.from("webp-normalised", "utf-8"));
|
||||||
await expect(
|
await expect(
|
||||||
fs.readFile(
|
fs.readFile(
|
||||||
getRuntimePath(assetTargets.mirrorDirs[0].iconDir, "chair_icon.png"),
|
getRuntimePath(assetTargets.mirrorDirs[0].iconDir, "chair_icon.png"),
|
||||||
|
|||||||
@@ -18,8 +18,9 @@ import {
|
|||||||
} from "@/lib/services/furni-import";
|
} from "@/lib/services/furni-import";
|
||||||
import { nitroAnimationStatesCount } from "@/lib/services/furni-real-interaction";
|
import { nitroAnimationStatesCount } from "@/lib/services/furni-real-interaction";
|
||||||
import {
|
import {
|
||||||
createNitroBundle,
|
isLosslessWebp,
|
||||||
parseNitroBundle,
|
parseNitroBundle,
|
||||||
|
toWebpLosslessBundle,
|
||||||
} from "@/lib/services/swf/nitro-builder";
|
} from "@/lib/services/swf/nitro-builder";
|
||||||
import { getRuntimePath, getRuntimeResolve } from "@/lib/utils/runtime-path";
|
import { getRuntimePath, getRuntimeResolve } from "@/lib/utils/runtime-path";
|
||||||
|
|
||||||
@@ -278,19 +279,19 @@ export async function uploadSingleFurni(params: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let nitroMeta: Record<string, unknown> | null = null;
|
let nitroMeta: Record<string, unknown> | null = null;
|
||||||
// The client resolves the spritesheet through `spritesheet.meta.image`, so
|
// Studio imports always land as WebP Lossless, whatever the uploaded
|
||||||
// that pointer must name the member we actually ship. A bundle from a
|
// bundle carried, so a hotel-supplied PNG and an SWF import end up
|
||||||
// third-party tool can arrive with a stale pointer (say `.png` next to a
|
// identical on disk. Re-encoding also repairs a `meta.image` pointer that
|
||||||
// WebP member), which stores fine and then renders nothing. Re-writing
|
// disagrees with the texture member, which would otherwise store fine and
|
||||||
// through the builder repairs it without touching a single texture byte.
|
// then render nothing. The member keeps its own base name, so `chair*2`
|
||||||
// The member keeps its own base name, so `chair*2` colour variants that
|
// colour variants that share the `chair` library are not renamed.
|
||||||
// share the `chair` library are not renamed.
|
|
||||||
let storedNitro = nitroBuffer;
|
let storedNitro = nitroBuffer;
|
||||||
try {
|
try {
|
||||||
const parsed = parseNitroBundle(nitroBuffer);
|
const parsed = parseNitroBundle(nitroBuffer);
|
||||||
nitroMeta = parsed.json;
|
nitroMeta = parsed.json;
|
||||||
const memberBase = parsed.textureFileName.replace(/\.(?:png|webp)$/i, "");
|
storedNitro = await toWebpLosslessBundle(nitroBuffer);
|
||||||
storedNitro = createNitroBundle(parsed.json, parsed.texture, memberBase);
|
if (!isLosslessWebp(parsed.texture))
|
||||||
|
warnings.push("texture converted to WebP Lossless");
|
||||||
} catch {
|
} catch {
|
||||||
return { ok: false, classname, warnings, error: "invalid .nitro bundle" };
|
return { ok: false, classname, warnings, error: "invalid .nitro bundle" };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user