1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-09 16:21:51 +00:00

Pass storage object to ReadWriteProxy

Make it slightly easier to replace the storage interface used for
app_settings.bin and savegames.bin in case it's ever needed. Savegames
always use app.storage for now, but this should be easier to change as
well.
This commit is contained in:
Даниїл Григор'єв 2025-04-14 17:48:35 +03:00
parent c61825e8b3
commit 6b7cfa1b1b
No known key found for this signature in database
GPG Key ID: B890DF16341D8C1D
5 changed files with 38 additions and 24 deletions

View File

@ -54,21 +54,21 @@ export class Application {
this.unloaded = false;
// Platform stuff
this.storage = new Storage(this);
this.platformWrapper = new PlatformWrapperImplElectron(this);
// Global stuff
this.settings = new ApplicationSettings(this);
this.settings = new ApplicationSettings(this, this.storage);
this.ticker = new AnimationFrame();
this.stateMgr = new StateManager(this);
this.savegameMgr = new SavegameManager(this);
// NOTE: SavegameManager uses the passed storage, but savegames always
// use Application#storage
this.savegameMgr = new SavegameManager(this, this.storage);
this.inputMgr = new InputDistributor(this);
this.backgroundResourceLoader = new BackgroundResourcesLoader(this);
this.clientApi = new ClientAPI(this);
// Platform dependent stuff
this.storage = new Storage(this);
this.platformWrapper = new PlatformWrapperImplElectron(this);
this.sound = new Sound(this);
// Track if the window is focused (only relevant for browser)

View File

@ -1,5 +1,5 @@
/* typehints:start */
import { Application } from "../application";
import { Storage } from "@/platform/storage";
/* typehints:end */
import { FsError } from "@/platform/fs_error";
@ -18,9 +18,9 @@ const salt = globalConfig.info.file;
// Helper which only writes / reads if verify() works. Also performs migration
export class ReadWriteProxy {
constructor(app, filename) {
/** @type {Application} */
this.app = app;
constructor(storage, filename) {
/** @type {Storage} */
this.storage = storage;
this.filename = filename;
@ -143,7 +143,7 @@ export class ReadWriteProxy {
return asyncCompressor
.compressObjectAsync(this.currentData)
.then(compressed => {
return this.app.storage.writeFileAsync(this.filename, compressed);
return this.storage.writeFileAsync(this.filename, compressed);
})
.then(() => {
logger.log("📄 Wrote", this.filename);
@ -158,7 +158,7 @@ export class ReadWriteProxy {
readAsync() {
// Start read request
return (
this.app.storage
this.storage
.readFileAsync(this.filename)
// Check for errors during read
@ -297,7 +297,7 @@ export class ReadWriteProxy {
* @returns {Promise<void>}
*/
deleteAsync() {
return this.app.storage.deleteFileAsync(this.filename);
return this.storage.deleteFileAsync(this.filename);
}
// Internal

View File

@ -2,13 +2,13 @@
import { Application } from "../application";
/* typehints:end */
import { ReadWriteProxy } from "../core/read_write_proxy";
import { BoolSetting, EnumSetting, RangeSetting, BaseSetting } from "./setting_types";
import { createLogger } from "../core/logging";
import { ExplainedResult } from "../core/explained_result";
import { createLogger } from "../core/logging";
import { ReadWriteProxy } from "../core/read_write_proxy";
import { THEMES, applyGameTheme } from "../game/theme";
import { T } from "../translations";
import { LANGUAGES } from "../languages";
import { T } from "../translations";
import { BaseSetting, BoolSetting, EnumSetting, RangeSetting } from "./setting_types";
const logger = createLogger("application_settings");
@ -330,8 +330,11 @@ class SettingsStorage {
}
export class ApplicationSettings extends ReadWriteProxy {
constructor(app) {
super(app, "app_settings.bin");
constructor(app, storage) {
super(storage, "app_settings.bin");
/** @type {Application} */
this.app = app;
this.settingHandles = initializeSettings();
}

View File

@ -36,7 +36,11 @@ export class Savegame extends ReadWriteProxy {
* @param {SavegameMetadata} param0.metaDataRef Handle to the meta data
*/
constructor(app, { internalId, metaDataRef }) {
super(app, "savegame-" + internalId + ".bin");
super(app.storage, "savegame-" + internalId + ".bin");
/** @type {Application} */
this.app = app;
this.internalId = internalId;
this.metaDataRef = metaDataRef;

View File

@ -1,3 +1,7 @@
/* typehints:start */
import { Application } from "@/application";
/* typehints:end */
import { globalConfig } from "../core/config";
import { ExplainedResult } from "../core/explained_result";
import { createLogger } from "../core/logging";
@ -17,8 +21,11 @@ export const enumLocalSavegameStatus = {
};
export class SavegameManager extends ReadWriteProxy {
constructor(app) {
super(app, "savegames.bin");
constructor(app, storage) {
super(storage, "savegames.bin");
/** @type {Application} */
this.app = app;
this.currentData = this.getDefaultData();
}