Improve saving and loading classes

This commit is contained in:
Stone_Red
2023-10-05 12:28:16 +02:00
parent 15a6d9d2c5
commit ea6445a4ea
2 changed files with 17 additions and 4 deletions
@@ -20,9 +20,22 @@ internal class WorldLoader
public Task<Result<WorldData>> LoadWorld(string saveName, IProgress<WorldSaveLoadProgress> progress) public Task<Result<WorldData>> LoadWorld(string saveName, IProgress<WorldSaveLoadProgress> progress)
{ {
BinaryReader binaryReader = new BinaryReader(File.OpenRead(Paths.GetWorldSaveFilePath(saveName))); ushort fileVersion;
ushort fileVersion = binaryReader.ReadUInt16(); BinaryReader? binaryReader = null;
binaryReader.Close();
try
{
binaryReader = new BinaryReader(File.OpenRead(Paths.GetWorldSaveFilePath(saveName)));
fileVersion = binaryReader.ReadUInt16();
}
catch (IOException ex)
{
return Task.FromResult(Result.Fail<WorldData>(ex.Message));
}
finally
{
binaryReader?.Close();
}
IWorldReader? worldReader = fileVersion switch IWorldReader? worldReader = fileVersion switch
{ {
@@ -115,7 +115,7 @@ internal class WorldReaderV1 : IWorldReader
} }
finally finally
{ {
reader?.Dispose(); reader?.Close();
} }
} }
} }