Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement error handling for save data unserialization. #3270

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions flixel/util/FlxSave.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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".');
Expand Down Expand Up @@ -459,19 +470,52 @@ 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);
}

return all.get(id);
}


static function resolveEnum(name:String):Enum<Dynamic> {
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<Dynamic> {
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)
{
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down
Loading