mirror of
https://github.com/djdevin/recflare.git
synced 2026-10-27 16:08:58 -07:00
[studio] prefix assets
This commit is contained in:
@@ -3,42 +3,54 @@
|
||||
*
|
||||
* `unity_asset` holds one row per BUILD of a unity asset: the asset's GUID, the `Target`
|
||||
* it was built for (0 Windows, 2 Android/Quest — the same numbers a custom avatar's
|
||||
* `unityAssetTarget` uses), its `Version`, and the storage blob the client downloads
|
||||
* (`Filename`, a `<date>/<guid>` path, and its base64 SHA-256 `Hash`). The client asks for
|
||||
* these in bulk from `POST /unity_assets/baked/bulk` (the `rooms` worker) while loading a
|
||||
* room — the step Player.log times as `AssetBundle_Download` — and downloads `Filename`
|
||||
* from the CDN; a lookup that answers no row, or a row without a `Filename`, simply
|
||||
* downloads nothing, with no error anywhere.
|
||||
* `unityAssetTarget` uses), its `Version`, the `Kind` of bundle (`main`, the scene the
|
||||
* client loads; `stripped`, the editor-only variant the Studio upload also stores), and
|
||||
* the storage blob the client downloads (`Filename`, fetched as `/room/<Filename>` straight
|
||||
* from the shared `recflare-cdn` bucket, and its base64 SHA-256 `Hash`).
|
||||
*
|
||||
* This is a separate store from the Rec Room Studio builds in `studio_unity_asset_file`
|
||||
* (see studio-unity-assets.ts), which the studio worker writes per platform and kind and
|
||||
* which `GET /rooms/{id}` folds onto `CurrentSave.UnitySubAssets`. Nothing writes this
|
||||
* table yet; it is the record the bulk lookup reads.
|
||||
* Two things read it. The client asks for main bundles in bulk from
|
||||
* `POST /unity_assets/baked/bulk` (the `rooms` worker) while loading a room — the step
|
||||
* Player.log times as `AssetBundle_Download` — and `GET /rooms/{id}` folds the same main
|
||||
* bundles onto `CurrentSave.UnitySubAssets`; either way the client then downloads
|
||||
* `/room/<Filename>` from the bucket, and a lookup that answers no row,
|
||||
* or a row without a `Filename`, simply downloads nothing, with no error anywhere.
|
||||
*
|
||||
* The `rooms` worker owns the table and its migration — see
|
||||
* apps/rooms/migrations/0024_unity_asset.sql.
|
||||
* The `studio` worker writes it: a Rec Room Studio build posts a Windows + Android bundle
|
||||
* pair (plus optional stripped ones), stored here at {@link STUDIO_ASSET_VERSION} with the
|
||||
* cloud-build record in `studio_cloud_build` (see studio-unity-assets.ts).
|
||||
*
|
||||
* The `rooms` worker owns the table and its migrations — see
|
||||
* apps/rooms/migrations/0024_unity_asset.sql and 0026_unity_asset_kind.sql.
|
||||
*/
|
||||
|
||||
import { bindPlaceholders, chunkForBinds } from './d1-binds'
|
||||
|
||||
/** Schema DDL (mirror of apps/rooms/migrations/0024_unity_asset.sql) — tests apply this. */
|
||||
/** Schema DDL (mirror of apps/rooms/migrations/0026_unity_asset_kind.sql) — tests apply this. */
|
||||
export const UNITY_ASSET_SCHEMA_DDL: string[] = [
|
||||
// A unity asset is built once per target, and may be rebuilt at a new version, so the
|
||||
// three together name a row. `unity_asset_id` is stored lowercase: a GUID's case is not
|
||||
// part of its identity and the client is not consistent about it.
|
||||
// A unity asset is built once per target and kind, and may be rebuilt at a new version,
|
||||
// so the four together name a row. `unity_asset_id` is stored lowercase: a GUID's case
|
||||
// is not part of its identity and the client is not consistent about it.
|
||||
`CREATE TABLE IF NOT EXISTS unity_asset (
|
||||
unity_asset_id TEXT NOT NULL,
|
||||
target INTEGER NOT NULL,
|
||||
version INTEGER NOT NULL,
|
||||
kind TEXT NOT NULL DEFAULT 'main',
|
||||
filename TEXT NOT NULL,
|
||||
hash TEXT NOT NULL,
|
||||
PRIMARY KEY (unity_asset_id, target, version)
|
||||
PRIMARY KEY (unity_asset_id, target, version, kind)
|
||||
)`,
|
||||
]
|
||||
|
||||
/**
|
||||
* Which bundle of a build a row is. `main` is the scene the game loads; `stripped` is the
|
||||
* editor-only variant Rec Room Studio uploads beside it, stored but never listed to the
|
||||
* game — a client that loaded every row would replace the scene with the stripped one.
|
||||
*/
|
||||
export type UnityAssetKind = 'main' | 'stripped'
|
||||
|
||||
/**
|
||||
* One baked bundle as the client reads it — its five-key DTO, in this order. An element of
|
||||
* the bare array `POST /unity_assets/baked/bulk` answers.
|
||||
* the bare array `POST /unity_assets/baked/bulk` answers, and of a save's `UnitySubAssets`.
|
||||
*/
|
||||
export interface BakedUnityAsset {
|
||||
/** GUID. */
|
||||
@@ -46,16 +58,22 @@ export interface BakedUnityAsset {
|
||||
/** 0 Windows, 2 Android/Quest. */
|
||||
Target: number
|
||||
Version: number
|
||||
/** The storage blob path the client downloads, `<date>/<guid>`. */
|
||||
/** The storage blob the client downloads, as `/room/<Filename>` from the bucket. */
|
||||
Filename: string
|
||||
/** Base64 SHA-256 of the blob. */
|
||||
Hash: string
|
||||
}
|
||||
|
||||
/** One stored row: a baked bundle and which kind of bundle it is. */
|
||||
export interface UnityAssetBuild extends BakedUnityAsset {
|
||||
Kind: UnityAssetKind
|
||||
}
|
||||
|
||||
interface UnityAssetRow {
|
||||
unity_asset_id: string
|
||||
target: number
|
||||
version: number
|
||||
kind: string
|
||||
filename: string
|
||||
hash: string
|
||||
}
|
||||
@@ -68,19 +86,54 @@ const toBakedUnityAsset = (row: UnityAssetRow): BakedUnityAsset => ({
|
||||
Hash: row.hash,
|
||||
})
|
||||
|
||||
/** Record a build. Replaces the row for the same (asset, target, version). */
|
||||
export async function putUnityAsset(db: D1Database, asset: BakedUnityAsset): Promise<void> {
|
||||
await db
|
||||
const toUnityAssetBuild = (row: UnityAssetRow): UnityAssetBuild => ({
|
||||
...toBakedUnityAsset(row),
|
||||
Kind: row.kind === 'stripped' ? 'stripped' : 'main',
|
||||
})
|
||||
|
||||
/**
|
||||
* The statement that records a build, for callers that batch it with other writes.
|
||||
* Replaces the row for the same (asset, target, version, kind).
|
||||
*/
|
||||
export function unityAssetUpsert(
|
||||
db: D1Database,
|
||||
asset: BakedUnityAsset,
|
||||
kind: UnityAssetKind = 'main'
|
||||
): D1PreparedStatement {
|
||||
return db
|
||||
.prepare(
|
||||
`INSERT OR REPLACE INTO unity_asset (unity_asset_id, target, version, filename, hash)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5)`
|
||||
`INSERT OR REPLACE INTO unity_asset (unity_asset_id, target, version, kind, filename, hash)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6)`
|
||||
)
|
||||
.bind(asset.UnityAssetId.toLowerCase(), asset.Target, asset.Version, asset.Filename, asset.Hash)
|
||||
.bind(
|
||||
asset.UnityAssetId.toLowerCase(),
|
||||
asset.Target,
|
||||
asset.Version,
|
||||
kind,
|
||||
asset.Filename,
|
||||
asset.Hash
|
||||
)
|
||||
}
|
||||
|
||||
/** Record a build. Replaces the row for the same (asset, target, version, kind). */
|
||||
export async function putUnityAsset(
|
||||
db: D1Database,
|
||||
asset: BakedUnityAsset,
|
||||
kind: UnityAssetKind = 'main'
|
||||
): Promise<void> {
|
||||
await unityAssetUpsert(db, asset, kind).run()
|
||||
}
|
||||
|
||||
/** Forget every build of `unityAssetId`, all targets and kinds. The blobs stay in R2. */
|
||||
export async function deleteUnityAssetBuilds(db: D1Database, unityAssetId: string): Promise<void> {
|
||||
await db
|
||||
.prepare('DELETE FROM unity_asset WHERE unity_asset_id = ?1')
|
||||
.bind(unityAssetId.toLowerCase())
|
||||
.run()
|
||||
}
|
||||
|
||||
/**
|
||||
* The baked bundles for `unityAssetIds` on `target` — one entry per asset that has one, in
|
||||
* The main bundles for `unityAssetIds` on `target` — one entry per asset that has one, in
|
||||
* the order asked, assets with no build left out. An asset built more than once is served
|
||||
* ONCE: the build at the requested `version` when there is one, otherwise the newest. The
|
||||
* request's version is a preference, not a filter, because the one capture pairing the two
|
||||
@@ -102,8 +155,9 @@ export async function getBakedUnityAssets(
|
||||
chunkForBinds(ids, 2).map((chunk) =>
|
||||
db
|
||||
.prepare(
|
||||
`SELECT unity_asset_id, target, version, filename, hash FROM unity_asset
|
||||
WHERE target = ?1 AND unity_asset_id IN (${bindPlaceholders(chunk, 2)})
|
||||
`SELECT unity_asset_id, target, version, kind, filename, hash FROM unity_asset
|
||||
WHERE target = ?1 AND kind = 'main'
|
||||
AND unity_asset_id IN (${bindPlaceholders(chunk, 2)})
|
||||
ORDER BY (version = ?2) DESC, version DESC`
|
||||
)
|
||||
.bind(target, version, ...chunk)
|
||||
@@ -117,3 +171,39 @@ export async function getBakedUnityAssets(
|
||||
}
|
||||
return ids.flatMap((id) => best.get(id) ?? [])
|
||||
}
|
||||
|
||||
/**
|
||||
* Every stored build of the given unity assets — all targets, versions and kinds. Ids are
|
||||
* matched case-insensitively and served as stored (lowercase). An empty id list reads
|
||||
* nothing.
|
||||
*/
|
||||
export async function listUnityAssetBuilds(
|
||||
db: D1Database,
|
||||
unityAssetIds: string[]
|
||||
): Promise<UnityAssetBuild[]> {
|
||||
const ids = [...new Set(unityAssetIds.filter((id) => id !== '').map((id) => id.toLowerCase()))]
|
||||
if (ids.length === 0) return []
|
||||
const pages = await Promise.all(
|
||||
chunkForBinds(ids).map((chunk) =>
|
||||
db
|
||||
.prepare(
|
||||
`SELECT unity_asset_id, target, version, kind, filename, hash FROM unity_asset
|
||||
WHERE unity_asset_id IN (${bindPlaceholders(chunk)})`
|
||||
)
|
||||
.bind(...chunk)
|
||||
.all<UnityAssetRow>()
|
||||
)
|
||||
)
|
||||
return pages.flatMap((page) => page.results).map(toUnityAssetBuild)
|
||||
}
|
||||
|
||||
/**
|
||||
* The main bundles among `builds` as a save lists them: Windows then Android, the client's
|
||||
* five-key DTO. Stripped bundles are left off — see {@link UnityAssetKind}.
|
||||
*/
|
||||
export function bakedUnityAssets(builds: UnityAssetBuild[]): BakedUnityAsset[] {
|
||||
return builds
|
||||
.filter((build) => build.Kind === 'main')
|
||||
.map(({ Kind: _kind, ...baked }) => baked)
|
||||
.sort((a, b) => a.Target - b.Target || a.Filename.localeCompare(b.Filename))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user