diff --git a/Content.Tests/DMProject/Tests/Savefile/BasicReadAndWrite.dm b/Content.Tests/DMProject/Tests/Savefile/BasicReadAndWrite.dm index 7aff9c89bb..58042c79af 100644 --- a/Content.Tests/DMProject/Tests/Savefile/BasicReadAndWrite.dm +++ b/Content.Tests/DMProject/Tests/Savefile/BasicReadAndWrite.dm @@ -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) diff --git a/OpenDreamRuntime/Objects/Types/DreamObjectSavefile.cs b/OpenDreamRuntime/Objects/Types/DreamObjectSavefile.cs index 5a538742d0..3e95dbaaf5 100644 --- a/OpenDreamRuntime/Objects/Types/DreamObjectSavefile.cs +++ b/OpenDreamRuntime/Objects/Types/DreamObjectSavefile.cs @@ -106,7 +106,13 @@ public override void Initialize(DreamProcArguments args) { var data = Resource.ReadAsString(); if (!string.IsNullOrEmpty(data)) { - CurrentDir = _rootNode = JsonSerializer.Deserialize(data)!; + try{ + CurrentDir = _rootNode = JsonSerializer.Deserialize(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