Skip to content

Commit

Permalink
✨ save files can be deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
codeimpossible committed Aug 30, 2021
1 parent 1d4df58 commit ca5f5b9
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 12 deletions.
31 changes: 30 additions & 1 deletion src/Chonks/Depots/FileSaveDepot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ public class FileSaveDepot : ISaveDepot {
return _emptySaves;
}

public bool TryClearSave(string name, out Exception ex) {
EnsureWorkingDirectory();
ex = null;
try {
var path = Path.Combine(_workingDirectoryPath, $"{name}.sav");
if (File.Exists(path)) {
File.Delete(path);
return true;
}
return true;
} catch (Exception e) {
ex = e;
return false;
}
}

public bool TryLoadSave(string name, out SaveChunk[] chunks, out Exception ex) {
EnsureWorkingDirectory();
ex = null;
Expand Down Expand Up @@ -104,8 +120,14 @@ public class FileSaveDepot : ISaveDepot {
public bool TryWriteSave(string name, SaveChunk[] chunks, out Exception ex) {
EnsureWorkingDirectory();
ex = null;
bool wasTempSave = false;
try {
using (var fs = new FileStream(Path.Combine(_workingDirectoryPath, $"{name}.sav"), FileMode.OpenOrCreate, FileAccess.Write)) {
var saveFilePath = Path.Combine(_workingDirectoryPath, $"{name}.sav");
if (File.Exists(saveFilePath)) {
saveFilePath = Path.Combine(_workingDirectoryPath, $"{name}_tmp.sav");
wasTempSave = true;
}
using (var fs = new FileStream(saveFilePath, FileMode.OpenOrCreate, FileAccess.Write)) {
using (var writer = new StreamWriter(fs)) {
foreach (var chunk in chunks) {
writer.WriteLine(CHUNK_START_MARKER);
Expand All @@ -119,6 +141,13 @@ public class FileSaveDepot : ISaveDepot {
}
}
}

if (wasTempSave) {
var prevSave = Path.Combine(_workingDirectoryPath, $"{name}.sav");
File.Delete(prevSave);
File.Move(saveFilePath, prevSave);
}

return true;
} catch (Exception e) {
ex = e;
Expand Down
9 changes: 9 additions & 0 deletions src/Chonks/Depots/InMemorySaveDepot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public class InMemorySaveDepot : ISaveDepot {
return false;
}

public bool TryClearSave(string name, out Exception ex) {
ex = null;
if (!_chunks.ContainsKey(name)) {
return true;
}
_chunks.Remove(name);
return true;
}

public bool TryWriteSave(string name, SaveChunk[] chunks, out Exception ex) {
ex = null;
if (!_chunks.ContainsKey(name)) {
Expand Down
7 changes: 7 additions & 0 deletions tests/Chonks.Tests/Fakes/FakeDepot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public class FakeDepot : ISaveDepot {
return false;
}

public bool TryClearSave(string name, out Exception ex) {
ex = null;
if (!_storage.ContainsKey(name)) return true;
Storage.Remove(name);
return true;
}

public bool TryWriteSave(string name, SaveChunk[] chunks, out Exception ex) {
ex = null;
if (_storage.TryAdd(name, chunks)) {
Expand Down
46 changes: 35 additions & 11 deletions tests/Chonks.Tests/FileSaveDepotTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,39 @@

namespace Chonks.Tests {
public class FileSaveDepotTests {
private FileSaveDepot _depot;

private string _temporaryWorkingDirectory;

public FileSaveDepotTests() {
_temporaryWorkingDirectory = $"{Path.GetTempPath()}{Path.DirectorySeparatorChar}{System.Guid.NewGuid()}{Path.DirectorySeparatorChar}";
if (!Directory.Exists(_temporaryWorkingDirectory)) {
Directory.CreateDirectory(_temporaryWorkingDirectory);
}
_depot = new FileSaveDepot(_temporaryWorkingDirectory);
}

~FileSaveDepotTests() {
Directory.Delete(_temporaryWorkingDirectory);
}

public class TryClearSae : FileSaveDepotTests {
[InlineData("SaveFile1")]
[InlineData("MultipleChunks")]
[Theory]
public void SavedFilesCanBeDeleted(string fixtureName) {
using (var fixtures = new TemporaryFixture("Fixtures")) {
var depot = new FileSaveDepot(fixtures.Path);
var saveFilePath = fixtures.GetFilePath(fixtureName + ".sav");
Assert.True(File.Exists(saveFilePath));

var result = depot.TryClearSave(fixtureName, out var ex);

Assert.Null(ex);
Assert.True(result);

Assert.False(File.Exists(saveFilePath));
}
}
}

public class TryLoadSave : FileSaveDepotTests {
[InlineData("SaveFile1")]
[InlineData("MultipleChunks")]
Expand All @@ -33,9 +50,9 @@ public class TryLoadSave : FileSaveDepotTests {

var expectedChunks = JsonConvert.DeserializeObject<SaveChunk[]>(json);

File.WriteAllText(Path.Combine(_temporaryWorkingDirectory, fixtureName + ".sav"), savFileData);
var depot = new FileSaveDepot(fixtures.Path);

var result = _depot.TryLoadSave(fixtureName, out var chunks, out var ex);
var result = depot.TryLoadSave(fixtureName, out var chunks, out var ex);

Assert.True(result);
Assert.Null(ex);
Expand Down Expand Up @@ -70,11 +87,13 @@ public class TryWriteSave : FileSaveDepotTests {

var chunks = JsonConvert.DeserializeObject<SaveChunk[]>(json);

var result = _depot.TryWriteSave(fixtureName, chunks, out var ex);
var depot = new FileSaveDepot(fixtures.Path);

var result = depot.TryWriteSave(fixtureName, chunks, out var ex);
Assert.True(result);
Assert.Null(ex);

var savFile = File.ReadAllText(Path.Combine(_temporaryWorkingDirectory, fixtureName + ".sav"));
var savFile = File.ReadAllText(fixtures.GetFilePath(fixtureName + ".sav"));
Assert.Equal(expectedSavFile, savFile);
}
}
Expand All @@ -84,7 +103,8 @@ public class ListSaves : FileSaveDepotTests {

[Fact]
public void GivenNoSavesExist_ReturnsAnEmptyListOfSaves() {
var saves = _depot.ListSaves();
var depot = new FileSaveDepot(_temporaryWorkingDirectory);
var saves = depot.ListSaves();

Assert.Empty(saves);
}
Expand All @@ -96,20 +116,24 @@ public class ListSaves : FileSaveDepotTests {
}
Assert.False(Directory.Exists(_temporaryWorkingDirectory));

var saves = _depot.ListSaves();
var depot = new FileSaveDepot(_temporaryWorkingDirectory);

var saves = depot.ListSaves();

Assert.Empty(saves);
Assert.True(Directory.Exists(_temporaryWorkingDirectory));

_depot.Cleanup();
depot.Cleanup();
}

[Fact]
public void GivenSavesExist_ReturnsAMatchingArrayOfSaves() {
Directory.CreateDirectory(_temporaryWorkingDirectory);
File.WriteAllText(Path.Combine(_temporaryWorkingDirectory, "save1"), "test");

var saves = _depot.ListSaves();
var depot = new FileSaveDepot(_temporaryWorkingDirectory);

var saves = depot.ListSaves();

Assert.Single(saves);
}
Expand Down

0 comments on commit ca5f5b9

Please sign in to comment.