Dev Improvements (#4)
* Fixed dependency * Updates to NeoDictionary * Changes to NeoDictionary * Started working on a Git implementation to integrate into Game Engines in the future * Added commit functionality to the git library * New changes * Added select for NeoDictionary.cs * Fixed issue with previous new method * Added a Benchmarking Tool to be used mostly in game engines * Added IndexOf to NeoDictionary.cs * Added new method for BenchmarkUtility.cs * added an instacedobject class * Added a new shuffle method for lists * fixed return type * Added new Changelog generator * Added new Game logic library to assist with game projects * Added Web Project * Added Nuget package configs * Add workflow for publishing game library NuGet package * Add README for OmegaLeo's Helper Library * Updated Website to reflect all libraries * Fixed missing library in Changelog * Added Contributing and Code of Conduct
This commit was merged in pull request #4.
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using NetFlow.DocumentationHelper.Library.Attributes;
|
||||
using Newtonsoft.Json;
|
||||
using OmegaLeo.HelperLib.Game.Interfaces;
|
||||
|
||||
namespace OmegaLeo.HelperLib.Game.Services
|
||||
{
|
||||
public class SaveManager
|
||||
{
|
||||
private readonly ISaveFileProvider _fileProvider;
|
||||
private readonly JsonSerializerSettings _jsonOptions;
|
||||
|
||||
public SaveManager(ISaveFileProvider fileProvider)
|
||||
{
|
||||
_fileProvider = fileProvider ?? throw new ArgumentNullException(nameof(fileProvider));
|
||||
_jsonOptions = new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
Formatting = Formatting.Indented
|
||||
};
|
||||
}
|
||||
|
||||
[Documentation(nameof(Save), "Saves data to a JSON file.")]
|
||||
public void Save<T>(string fileName, T data)
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(data, _jsonOptions);
|
||||
string path = _fileProvider.GetSavePath(fileName);
|
||||
string directory = Path.GetDirectoryName(path);
|
||||
|
||||
if (!Directory.Exists(directory))
|
||||
Directory.CreateDirectory(directory);
|
||||
|
||||
File.WriteAllText(path, json);
|
||||
}
|
||||
|
||||
[Documentation(nameof(Load), "Loads data from a JSON file.")]
|
||||
public T Load<T>(string fileName)
|
||||
{
|
||||
string path = _fileProvider.GetSavePath(fileName);
|
||||
|
||||
if (!File.Exists(path))
|
||||
throw new FileNotFoundException($"Save file not found: {fileName}");
|
||||
|
||||
string json = File.ReadAllText(path);
|
||||
return JsonConvert.DeserializeObject<T>(json, _jsonOptions);
|
||||
}
|
||||
|
||||
[Documentation(nameof(TryLoad), "Attempts to load data from a JSON file, returning a success flag.")]
|
||||
public bool TryLoad<T>(string fileName, out T data)
|
||||
{
|
||||
try
|
||||
{
|
||||
data = Load<T>(fileName);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
data = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
[Documentation(nameof(SaveExists), "Checks if a save file exists.")]
|
||||
public bool SaveExists(string fileName)
|
||||
{
|
||||
return File.Exists(_fileProvider.GetSavePath(fileName));
|
||||
}
|
||||
|
||||
[Documentation(nameof(DeleteSave), "Deletes a save file.")]
|
||||
public void DeleteSave(string fileName)
|
||||
{
|
||||
string path = _fileProvider.GetSavePath(fileName);
|
||||
if (File.Exists(path))
|
||||
File.Delete(path);
|
||||
}
|
||||
|
||||
[Documentation(nameof(GetAllSaveFiles), "Retrieves all save files in the save directory.")]
|
||||
public string[] GetAllSaveFiles()
|
||||
{
|
||||
string savePath = _fileProvider.GetSaveDirectory();
|
||||
if (!Directory.Exists(savePath))
|
||||
return Array.Empty<string>();
|
||||
|
||||
return Directory.GetFiles(savePath, "*.json");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user