|
| 1 | +// **************************************************************** // |
| 2 | +// |
| 3 | +// Copyright (c) RimuruDev. All rights reserved. |
| 4 | +// Contact me: |
| 5 | +// - Gmail: rimuru.dev@gmail.com |
| 6 | +// - GitHub: https://github.com/RimuruDev |
| 7 | +// - LinkedIn: https://www.linkedin.com/in/rimuru/ |
| 8 | +// - GitHub Organizations: https://github.com/Rimuru-Dev |
| 9 | +// |
| 10 | +// **************************************************************** // |
| 11 | + |
| 12 | +using System; |
| 13 | +using System.IO; |
| 14 | +using UnityEngine; |
| 15 | +using Newtonsoft.Json; |
| 16 | + |
| 17 | +#if UNITY_EDITOR |
| 18 | +using UnityEditor; |
| 19 | +#endif |
| 20 | + |
| 21 | +namespace AbyssMoth.Internal.Codebase.Infrastructure.StorageService |
| 22 | +{ |
| 23 | + public sealed class JsonSaveLoadService : ISaveLoadService |
| 24 | + { |
| 25 | + private const string FileFormat = ".json"; |
| 26 | + private static string _customPathInEditor = "Assets/Saves"; |
| 27 | + |
| 28 | + public void Save<TData>(TData data, string key) |
| 29 | + { |
| 30 | + if (string.IsNullOrWhiteSpace(key)) |
| 31 | + { |
| 32 | + Debug.LogError("Save operation failed: Key is null or whitespace."); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + if (data == null) |
| 37 | + { |
| 38 | + Debug.LogError("Save operation failed: Data is null."); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + try |
| 43 | + { |
| 44 | + var filePath = GetFilePath(key); |
| 45 | + |
| 46 | + var directoryPath = Path.GetDirectoryName(filePath); |
| 47 | + if (!Directory.Exists(directoryPath)) |
| 48 | + { |
| 49 | + Directory.CreateDirectory(directoryPath ?? |
| 50 | + throw new InvalidOperationException("Directory path is null.")); |
| 51 | + } |
| 52 | + |
| 53 | + var jsonData = JsonConvert.SerializeObject(data, Formatting.Indented); |
| 54 | + File.WriteAllText(filePath, jsonData); |
| 55 | + } |
| 56 | + catch (Exception ex) |
| 57 | + { |
| 58 | + Debug.LogError($"Error when saving data: {ex.Message}"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public TData Load<TData>(string key, TData defaultValue = default) |
| 63 | + { |
| 64 | + if (string.IsNullOrWhiteSpace(key)) |
| 65 | + { |
| 66 | + Debug.LogError("Load operation failed: Key is null or whitespace."); |
| 67 | + return defaultValue; |
| 68 | + } |
| 69 | + |
| 70 | + try |
| 71 | + { |
| 72 | + var filePath = GetFilePath(key); |
| 73 | + if (File.Exists(filePath)) |
| 74 | + { |
| 75 | + var jsonData = File.ReadAllText(filePath); |
| 76 | + return JsonConvert.DeserializeObject<TData>(jsonData); |
| 77 | + } |
| 78 | + } |
| 79 | + catch (Exception ex) |
| 80 | + { |
| 81 | + Debug.LogError($"Error when loading data: {ex.Message}"); |
| 82 | + } |
| 83 | + |
| 84 | + return defaultValue; |
| 85 | + } |
| 86 | + |
| 87 | + private static string GetFilePath(string key) |
| 88 | + { |
| 89 | +#if UNITY_EDITOR |
| 90 | + return Path.Combine(_customPathInEditor, $"{key}{FileFormat}"); |
| 91 | +#else |
| 92 | + return Path.Combine(Application.persistentDataPath, $"{key}{FileFormat}"); |
| 93 | +#endif |
| 94 | + } |
| 95 | + |
| 96 | +#if UNITY_EDITOR |
| 97 | + public void SetCustomPathInEditor(string customPath) |
| 98 | + { |
| 99 | + _customPathInEditor = customPath; |
| 100 | + |
| 101 | + if (!Directory.Exists(_customPathInEditor)) |
| 102 | + Directory.CreateDirectory(_customPathInEditor); |
| 103 | + } |
| 104 | +#endif |
| 105 | + } |
| 106 | +} |
0 commit comments