2023-05-23 18:53:53 +00:00
|
|
|
import { SpacePrimitives } from "./space_primitives.ts";
|
2022-10-12 09:47:13 +00:00
|
|
|
import { AssetBundle } from "../../plugos/asset_bundle/bundle.ts";
|
2023-08-20 15:51:00 +00:00
|
|
|
import { FileMeta } from "$sb/types.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
|
|
|
|
export class AssetBundlePlugSpacePrimitives implements SpacePrimitives {
|
|
|
|
constructor(
|
|
|
|
private wrapped: SpacePrimitives,
|
|
|
|
private assetBundle: AssetBundle,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchFileList(): Promise<FileMeta[]> {
|
2023-01-13 14:41:29 +00:00
|
|
|
const files = await this.wrapped.fetchFileList();
|
2023-05-23 18:53:53 +00:00
|
|
|
return this.assetBundle.listFiles()
|
2022-10-12 09:47:13 +00:00
|
|
|
.map((p) => ({
|
|
|
|
name: p,
|
2023-06-13 18:47:05 +00:00
|
|
|
contentType: this.assetBundle.getMimeType(p),
|
|
|
|
lastModified: this.assetBundle.getMtime(p),
|
2022-10-12 09:47:13 +00:00
|
|
|
perm: "ro",
|
|
|
|
size: -1,
|
2023-01-13 14:41:29 +00:00
|
|
|
} as FileMeta)).concat(files);
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readFile(
|
|
|
|
name: string,
|
2023-05-23 18:53:53 +00:00
|
|
|
): Promise<{ data: Uint8Array; meta: FileMeta }> {
|
2022-10-12 09:47:13 +00:00
|
|
|
if (this.assetBundle.has(name)) {
|
|
|
|
const data = this.assetBundle.readFileSync(name);
|
2022-10-10 12:50:21 +00:00
|
|
|
// console.log("Requested encoding", encoding);
|
|
|
|
return Promise.resolve({
|
2023-05-23 18:53:53 +00:00
|
|
|
data,
|
2022-10-10 12:50:21 +00:00
|
|
|
meta: {
|
2023-06-13 18:47:05 +00:00
|
|
|
contentType: this.assetBundle.getMimeType(name),
|
|
|
|
lastModified: this.assetBundle.getMtime(name),
|
2022-10-10 12:50:21 +00:00
|
|
|
size: data.byteLength,
|
|
|
|
perm: "ro",
|
|
|
|
} as FileMeta,
|
|
|
|
});
|
|
|
|
}
|
2023-05-23 18:53:53 +00:00
|
|
|
return this.wrapped.readFile(name);
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getFileMeta(name: string): Promise<FileMeta> {
|
2022-10-12 09:47:13 +00:00
|
|
|
if (this.assetBundle.has(name)) {
|
|
|
|
const data = this.assetBundle.readFileSync(name);
|
2022-10-10 12:50:21 +00:00
|
|
|
return Promise.resolve({
|
2023-06-13 18:47:05 +00:00
|
|
|
contentType: this.assetBundle.getMimeType(name),
|
|
|
|
lastModified: this.assetBundle.getMtime(name),
|
2022-10-10 12:50:21 +00:00
|
|
|
size: data.byteLength,
|
|
|
|
perm: "ro",
|
|
|
|
} as FileMeta);
|
|
|
|
}
|
|
|
|
return this.wrapped.getFileMeta(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeFile(
|
|
|
|
name: string,
|
2023-05-23 18:53:53 +00:00
|
|
|
data: Uint8Array,
|
2023-01-13 14:41:29 +00:00
|
|
|
selfUpdate?: boolean,
|
2023-07-02 09:25:32 +00:00
|
|
|
meta?: FileMeta,
|
2022-10-10 12:50:21 +00:00
|
|
|
): Promise<FileMeta> {
|
2023-05-23 18:53:53 +00:00
|
|
|
if (this.assetBundle.has(name)) {
|
|
|
|
console.warn("Attempted to write to read-only asset file", name);
|
|
|
|
return this.getFileMeta(name);
|
|
|
|
}
|
|
|
|
return this.wrapped.writeFile(
|
|
|
|
name,
|
|
|
|
data,
|
|
|
|
selfUpdate,
|
2023-07-02 09:25:32 +00:00
|
|
|
meta,
|
2023-05-23 18:53:53 +00:00
|
|
|
);
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteFile(name: string): Promise<void> {
|
2022-10-12 09:47:13 +00:00
|
|
|
if (this.assetBundle.has(name)) {
|
2022-10-10 12:50:21 +00:00
|
|
|
// Quietly ignore
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return this.wrapped.deleteFile(name);
|
|
|
|
}
|
|
|
|
}
|