0
0
mirror of https://github.com/sp-tarkov/server.git synced 2025-02-12 21:30:43 -05:00

Add safeguard to prevent a call to save the profile from happening if a save is already underway

This commit is contained in:
Archangel 2025-01-31 01:16:30 +01:00
parent 554159e084
commit 4595c29c15

View File

@ -15,6 +15,7 @@ import { inject, injectAll, injectable } from "tsyringe";
export class SaveServer {
protected profileFilepath = "user/profiles/";
protected profiles: Map<string, ISptProfile> = new Map();
protected profilesBeingSaved: Set<string> = new Set();
protected onBeforeSaveCallbacks: Map<string, (profile: ISptProfile) => Promise<ISptProfile>> = new Map();
protected saveSHA1: { [key: string]: string } = {};
@ -184,6 +185,12 @@ export class SaveServer {
throw new Error(`Profile ${sessionID} does not exist! Unable to save this profile!`);
}
if (this.profilesBeingSaved.has(sessionID)) {
throw new Error(`Profile ${sessionID} is already being saved!`);
}
this.profilesBeingSaved.add(sessionID);
const filePath = `${this.profileFilepath}${sessionID}.json`;
// Run pre-save callbacks before we save into json
@ -201,12 +208,16 @@ export class SaveServer {
this.profiles.get(sessionID),
!this.configServer.getConfig<ICoreConfig>(ConfigTypes.CORE).features.compressProfile,
);
const sha1 = await this.hashUtil.generateSha1ForDataAsync(jsonProfile);
if (typeof this.saveSHA1[sessionID] !== "string" || this.saveSHA1[sessionID] !== sha1) {
this.saveSHA1[sessionID] = sha1;
// save profile to disk
await this.fileSystem.write(filePath, jsonProfile);
}
this.profilesBeingSaved.delete(sessionID);
}
/**