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; this.unloaded = false;
// Platform stuff
this.storage = new Storage(this);
this.platformWrapper = new PlatformWrapperImplElectron(this);
// Global stuff // Global stuff
this.settings = new ApplicationSettings(this); this.settings = new ApplicationSettings(this, this.storage);
this.ticker = new AnimationFrame(); this.ticker = new AnimationFrame();
this.stateMgr = new StateManager(this); 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.inputMgr = new InputDistributor(this);
this.backgroundResourceLoader = new BackgroundResourcesLoader(this); this.backgroundResourceLoader = new BackgroundResourcesLoader(this);
this.clientApi = new ClientAPI(this); this.clientApi = new ClientAPI(this);
// Platform dependent stuff
this.storage = new Storage(this);
this.platformWrapper = new PlatformWrapperImplElectron(this);
this.sound = new Sound(this); this.sound = new Sound(this);
// Track if the window is focused (only relevant for browser) // Track if the window is focused (only relevant for browser)

View File

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

View File

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

View File

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

View File

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