Skip to content

Commit

Permalink
failsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
amylizzle committed Feb 2, 2024
1 parent b4193f5 commit 3afa639
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Content.Tests/DMProject/Tests/Savefile/BasicReadAndWrite.dm
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,12 @@
ASSERT(S["CBA"] == null)

fdel("savefile.sav")

file("badsavefile.sav") << "hey wait, this isn't json! oh well, better fail miserably and die"

var/savefile/fail
try
fail = new("badsavefile.sav")
catch(var/exception/e)
ASSERT(isnull(fail))
ASSERT(fail == null)
8 changes: 7 additions & 1 deletion OpenDreamRuntime/Objects/Types/DreamObjectSavefile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ public override void Initialize(DreamProcArguments args) {
var data = Resource.ReadAsString();

if (!string.IsNullOrEmpty(data)) {
CurrentDir = _rootNode = JsonSerializer.Deserialize<SFDreamJsonValue>(data)!;
try{
CurrentDir = _rootNode = JsonSerializer.Deserialize<SFDreamJsonValue>(data)!;
} catch (JsonException e) { //only catch JSON exceptions, other exceptions probably mean something else happened
//fail safe, make this null if something goes super fucky. Prevents accidentally overwrite of non-savefile files.
Resource = null;
throw new InvalidDataException($"Error parsing savefile {filename}: Is the savefile corrupted or using a BYOND version? BYOND savefiles are not compatible with OpenDream. Details: {e}");
}
SavefileDirectories.Add(filename, _rootNode);
} else {
//_rootNode is created in constructor
Expand Down

0 comments on commit 3afa639

Please sign in to comment.