From 8ea34a4a2e89a1814f7bfb56dc43ab0800e11319 Mon Sep 17 00:00:00 2001 From: Sofistico Date: Sat, 6 Sep 2025 00:07:39 -0300 Subject: [PATCH] some fixes on how it's handled the saving service --- src/MagusEngine/Services/SavingService.cs | 29 ++++++++++------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/MagusEngine/Services/SavingService.cs b/src/MagusEngine/Services/SavingService.cs index 971c71af..1c53cc25 100644 --- a/src/MagusEngine/Services/SavingService.cs +++ b/src/MagusEngine/Services/SavingService.cs @@ -18,9 +18,8 @@ namespace MagusEngine.Services /// public sealed class SavingService { - private const string _folderName = "saves"; private const int chunkPartition = 500; - private static readonly string dir = AppDomain.CurrentDomain.BaseDirectory; + private readonly static string _dirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "saves"); private readonly JsonSerializerSettings settings; [DataMember] @@ -46,9 +45,9 @@ public SavingService() /// private static void CreateNewSaveFolder() { - if (!Directory.Exists(dir + $@"\{_folderName}")) + if (!Directory.Exists(_dirPath)) { - Directory.CreateDirectory(_folderName); + Directory.CreateDirectory(_dirPath); } } @@ -69,11 +68,11 @@ public void SaveGameToFolder(Universe gameState, string saveName) try { - Serializer.Save(gameState, @$"{_folderName}\{saveName}\game_state.mr", true); + Serializer.Save(gameState, Path.Combine(_dirPath, saveName, "game_state.mr"), true); } catch (Exception) { - Directory.Delete(Path.Combine(dir, _folderName, saveName), true); + Directory.Delete(Path.Combine(_dirPath, saveName), true); throw; } } @@ -86,7 +85,7 @@ private void CreateSaveNameFolder(string saveName) { if (SavePathName?.Equals(saveName) == true) return; SavePathName = saveName; - string path = dir + $@"{_folderName}\{saveName}"; + string path = Path.Combine(_dirPath, saveName); if (!Directory.Exists(path)) { @@ -120,9 +119,9 @@ private void SaveChuncksToFile(RegionChunk[] chunks, int indexToStart = 0) if (i % chunkPartition == 0 || i != 0 || i == chunks.Length) { if (indexToStart == 0) - Serializer.Save(newChunks, @$"{_folderName}\{SavePathName}\chunks_{i / chunkPartition}.mr", true); + Serializer.Save(newChunks, Path.Combine(_dirPath, SavePathName!, $"chunks_{i / chunkPartition}.mr"), true); else - Serializer.Save(newChunks, @$"{_folderName}\{SavePathName}\chunks_{indexToStart / chunkPartition}.mr", true); + Serializer.Save(newChunks, Path.Combine(_dirPath, SavePathName!, $"chunks_{indexToStart / chunkPartition}.mr"), true); break; } } @@ -151,7 +150,7 @@ public static Universe LoadGame(string saveName) { try { - string path = $@"{_folderName}\{saveName}\game_state.mr"; + string path = Path.Combine(_dirPath, saveName, "game_state.mr"); return Serializer.Load(path, true); } catch (Exception ex) @@ -215,16 +214,14 @@ public void SaveChunkInPos(RegionChunk chunk, int index) private string? GetChunckFile(int index) { // need to remake, so that it will only load the relevant chunk - string path = $@"{SaveDir}\chunks_{index / chunkPartition}.mr"; + string path = Path.Combine(SaveDir!, $"chunks_{index / chunkPartition}.mr"); string pattern = Path.GetFileName(path); - string realDir = path[..^pattern.Length]; - string fullPath = Path.Combine(dir, realDir); - return Directory.GetFiles(fullPath, pattern, SearchOption.TopDirectoryOnly).FirstOrDefault(); + return Directory.GetFiles(_dirPath, pattern, SearchOption.TopDirectoryOnly).FirstOrDefault(); } private string GetChunckRelativeFile(int index) { - return $@"{_folderName}\{SavePathName}\chunks_{index / chunkPartition}.mr"; + return Path.Combine(_dirPath, SavePathName!, $"chunks_{index / chunkPartition}.mr"); } /// @@ -281,7 +278,7 @@ public static void DeleteSave(string saveName) { try { - Directory.Delete(Path.Combine(dir, _folderName, saveName), true); + Directory.Delete(Path.Combine(_dirPath, saveName), true); } catch (Exception) {