Last active
September 1, 2023 17:32
-
-
Save RimuruDev/90872ccbd0cc2c2507502da09d40f289 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// **************************************************************** // | |
// | |
// Copyright (c) RimuruDev. All rights reserved. | |
// Contact me: | |
// - Gmail: rimuru.dev@gmail.com | |
// - GitHub: https://github.com/RimuruDev | |
// - LinkedIn: https://www.linkedin.com/in/rimuru/ | |
// - GitHub Organizations: https://github.com/Rimuru-Dev | |
// | |
// **************************************************************** // | |
using System; | |
namespace AbyssMoth.Codebase.Infrastructure.Services.Storage | |
{ | |
public interface IStorageService | |
{ | |
public string GetDataPath { get; } | |
public void Save(string key, object data, Action<bool> onCallback = null); | |
public void Load<TData>(string key, Action<TData> onCallback); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// **************************************************************** // | |
// | |
// Copyright (c) RimuruDev. All rights reserved. | |
// Contact me: | |
// - Gmail: rimuru.dev@gmail.com | |
// - GitHub: https://github.com/RimuruDev | |
// - LinkedIn: https://www.linkedin.com/in/rimuru/ | |
// - GitHub Organizations: https://github.com/Rimuru-Dev | |
// | |
// **************************************************************** // | |
using System; | |
using System.Diagnostics.CodeAnalysis; | |
using System.IO; | |
using UnityEngine; | |
namespace AbyssMoth.Codebase.Infrastructure.Services.Storage | |
{ | |
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] | |
public sealed class JsonToFileStorageService : IStorageService | |
{ | |
public string GetDataPath => Application.persistentDataPath + "/Database"; | |
public void Save(string key, object data, Action<bool> onCallback = null) | |
{ | |
var path = BuildPath(key); | |
if (!Directory.Exists(GetDataPath)) | |
Directory.CreateDirectory(GetDataPath); | |
if (!File.Exists(path)) | |
File.Create(path).Dispose(); | |
var json = JsonUtility.ToJson(data); | |
using var fileStream = new StreamWriter(path); | |
fileStream.Write(json); | |
onCallback?.Invoke(true); | |
} | |
public void Load<TData>(string key, Action<TData> onCallback) | |
{ | |
var path = BuildPath(key); | |
if (!Directory.Exists(GetDataPath)) | |
Directory.CreateDirectory(GetDataPath); | |
if (!File.Exists(path)) | |
{ | |
File.Create(path).Dispose(); | |
onCallback?.Invoke(default); | |
return; | |
} | |
using var fileStream = new StreamReader(path); | |
var json = fileStream.ReadToEnd(); | |
var data = JsonUtility.FromJson<TData>(json); | |
onCallback?.Invoke(data); | |
} | |
private string BuildPath(string key) => | |
Path.Combine(GetDataPath, key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment