mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2025-12-10 17:52:36 +00:00
[MIRROR] Hardens painting saving against inconsistent state [MDB IGNORE] (#11664)
* Hardens painting saving against inconsistent state (#65024) Should help when paintings lists gets zeroed unintentionally during the round for some reason. Now it only deletes the painting file right before it writes the new file to prevent loss of data if anything runtimes or breaks. * Hardens painting saving against inconsistent state Co-authored-by: AnturK <AnturK@users.noreply.github.com>
This commit is contained in:
@@ -94,6 +94,8 @@ SUBSYSTEM_DEF(persistent_paintings)
|
||||
|
||||
/// A list of painting frames that this controls
|
||||
var/list/obj/structure/sign/painting/painting_frames = list()
|
||||
/// Hashes of paintings deleted this round
|
||||
var/list/deleted_paintings_md5s = list()
|
||||
|
||||
/// A list of /datum/paintings saved or ready to be saved this round.
|
||||
var/list/paintings = list()
|
||||
@@ -196,10 +198,26 @@ SUBSYSTEM_DEF(persistent_paintings)
|
||||
/// Saves all currently tracked painting data to file
|
||||
/datum/controller/subsystem/persistent_paintings/proc/save_to_file()
|
||||
var/json_file = file("data/paintings.json")
|
||||
fdel(json_file)
|
||||
var/list/all_data = list("version" = PAINTINGS_DATA_FORMAT_VERSION)
|
||||
|
||||
var/list/collated_data = list()
|
||||
if(fexists(json_file))
|
||||
var/list/old_data = json_decode(file2text(json_file))
|
||||
for(var/list/painting_data as anything in old_data["paintings"])
|
||||
collated_data[painting_data["md5"]] = painting_data
|
||||
|
||||
var/list/painting_data = list()
|
||||
for(var/datum/painting/painting as anything in paintings)
|
||||
painting_data += list(painting.to_json())
|
||||
collated_data[painting.md5] = painting.to_json() //Current data has priority over old data
|
||||
|
||||
// Remove deleted paintings from the list
|
||||
collated_data -= deleted_paintings_md5s
|
||||
|
||||
// Flatten the resulting list
|
||||
for(var/key in collated_data)
|
||||
painting_data += list(collated_data[key])
|
||||
|
||||
var/list/all_data = list("version" = PAINTINGS_DATA_FORMAT_VERSION)
|
||||
all_data["paintings"] = painting_data
|
||||
WRITE_FILE(json_file, json_encode(all_data))
|
||||
var/payload = json_encode(all_data)
|
||||
fdel(json_file)
|
||||
WRITE_FILE(json_file, payload)
|
||||
|
||||
@@ -47,6 +47,7 @@
|
||||
fdel(png)
|
||||
//Remove entry from paintings list
|
||||
SSpersistent_paintings.paintings -= chosen_painting
|
||||
SSpersistent_paintings.deleted_paintings_md5s |= chosen_painting.md5
|
||||
SSpersistent_paintings.save_to_file() // Save now so we don't have broken variations if this round crashes
|
||||
//Delete any painting instances in the current round
|
||||
for(var/obj/structure/sign/painting/painting as anything in SSpersistent_paintings.painting_frames)
|
||||
|
||||
Reference in New Issue
Block a user