From 6854d9a20d6c2b4f4e63f09f7df73412e79eeef8 Mon Sep 17 00:00:00 2001 From: EliteMasterEric Date: Fri, 18 Oct 2024 16:47:23 -0400 Subject: [PATCH] Implement error handling for save data unserialization. --- flixel/util/FlxSave.hx | 60 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/flixel/util/FlxSave.hx b/flixel/util/FlxSave.hx index f2164833ac..c04faa7b4f 100644 --- a/flixel/util/FlxSave.hx +++ b/flixel/util/FlxSave.hx @@ -167,6 +167,17 @@ class FlxSave implements IFlxDestroyable _sharedObject = FlxSharedObject.getLocal(name, path); status = BOUND(name, path); } + catch (s:FlxSaveStatus) { + this.status = s; + switch (this.status) { + case EMPTY: + return false; + case ERROR(_): + return false; + case BOUND(_, _): + return true; + } + } catch (e:Error) { FlxG.log.error('Error:${e.message} name:"$name", path:"$path".'); @@ -459,11 +470,14 @@ private class FlxSharedObject extends SharedObject try { final unserializer = new haxe.Unserializer(encodedData); - final resolver = { resolveEnum: Type.resolveEnum, resolveClass: SharedObject.__resolveClass }; + final resolver = { resolveEnum: FlxSharedObject.resolveEnum, resolveClass: FlxSharedObject.resolveClass }; unserializer.setResolver(cast resolver); sharedObject.data = unserializer.unserialize(); } - catch (e:Dynamic) {} + catch (e:Dynamic) { + trace('Error loading shared object "' + name + '" from local path "' + localPath + '"'); + throw FlxSaveStatus.ERROR("There was a problem parsing the save data."); + } } all.set(id, sharedObject); @@ -471,7 +485,37 @@ private class FlxSharedObject extends SharedObject return all.get(id); } - + + static function resolveEnum(name:String):Enum { + try + { + return Type.resolveEnum(name); + } + catch (e) + { + FlxG.log.error('Found invalid enum type ${name} in save data, indicates partial save corruption.'); + throw e; + } + } + + static function resolveClass(name:String):Class { + if (name == 'Dynamic') + { + FlxG.log.warn('Found invalid class type ${name} in save data, indicates partial save corruption.'); + return null; + } + + try + { + return SharedObject.__resolveClass(name); + } + catch (e) + { + FlxG.log.error('Found invalid class type ${name} in save data, indicates partial save corruption.'); + throw e; + } + } + #if (js && html5) static function getData(name:String, ?localPath:String) { @@ -622,7 +666,7 @@ private class FlxSharedObject extends SharedObject if (!sys.FileSystem.exists(directory)) SharedObject.__mkdir(directory); - + var output = sys.io.File.write(path, false); output.writeString(encodedData); output.close(); @@ -631,18 +675,18 @@ private class FlxSharedObject extends SharedObject { return SharedObjectFlushStatus.PENDING; } - + return SharedObjectFlushStatus.FLUSHED; } - + override function clear() { data = {}; - + try { var path = getPath(__localPath, __name); - + if (sys.FileSystem.exists(path)) sys.FileSystem.deleteFile(path); }