1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-11 09:11:50 +00:00
tobspr_shapez.io/src/js/platform/storage.ts
Даниїл Григор'єв fc33cc2fbf
Add dedicated saves storage, new fs job types
Keep track of the storage ID in each renderer Storage instance and pass
it to the IPC bridge. Jobs are dispatched to the relevant handler (only
saves/ for now) and all (de)compression is handled there.

Add dedicated fs-job types to read or write and (de)compress data
from/to the file picked by the user. Remove redundant utility functions
that used web APIs instead.
2025-04-15 01:57:00 +03:00

75 lines
2.1 KiB
TypeScript

import { Application } from "@/application";
import { FsError } from "./fs_error";
export const STORAGE_SAVES = "saves";
export const STORAGE_MOD_PREFIX = "mod/";
export class Storage {
readonly app: Application;
readonly id: string;
constructor(app: Application, id: string) {
this.app = app;
this.id = id;
}
/**
* Initializes the storage
*/
initialize(): Promise<void> {
return this.invokeFsJob({ type: "initialize" });
}
/**
* Writes a string to a file asynchronously
*/
writeFileAsync(filename: string, contents: unknown): Promise<void> {
return this.invokeFsJob({ type: "write", filename, contents });
}
/**
* Reads a string asynchronously
*/
readFileAsync(filename: string): Promise<unknown> {
return this.invokeFsJob({ type: "read", filename });
}
/**
* Tries to delete a file
*/
deleteFileAsync(filename: string): Promise<void> {
return this.invokeFsJob({ type: "delete", filename });
}
/**
* Displays the "Open File" dialog to let user pick a file. Returns the
* decompressed file contents, or undefined if the operation was canceled
*/
requestOpenFile(extension: string): Promise<unknown> {
return this.invokeFsJob({ type: "open-external", extension });
}
/**
* Displays the "Save File" dialog to let user pick a file. If the user
* picks a file, the passed contents will be compressed and written to
* that file.
*/
requestSaveFile(filename: string, contents: unknown): Promise<unknown> {
return this.invokeFsJob({ type: "save-external", filename, contents });
}
private invokeFsJob(data: object) {
return ipcRenderer
.invoke("fs-job", {
id: this.id,
...data,
})
.catch(e => this.wrapError(e));
}
private wrapError(err: unknown): Promise<never> {
const message = err instanceof Error ? err.message : err.toString();
return Promise.reject(new FsError(message, { cause: err }));
}
}