using System;
using System.IO;
using UnityEngine;
using Framework.Services;
namespace Framework.Storage {
/// Manages the save slots in which game states can be stored
[
ServiceScope(Scope.Global)
]
public class SaveSlotManager : Service, ISaveSlotManager {
/// Checks whether the slot with the specified index contains save data
/// Index of the slot that will be checked
/// True if the specified save slot is filled
public bool IsSlotFilled(int slotIndex) {
string saveSlotPath = getSaveSlotPath(slotIndex);
return Directory.Exists(saveSlotPath);
}
/// Deletes the save slot with the specified index
/// Index of the slot that will be deleted
public void DeleteSaveSlot(int slotIndex) {
string saveSlotPath = getSaveSlotPath(slotIndex);
Directory.Delete(saveSlotPath, recursive: true);
}
/// Returns the absolute path of the slot with the specified index
/// Index of the slot whose path will be returned
/// Whether to create the slot if it doesn't exist yet
///
/// The absolute path fo the slot with the specified index or null if the slot
/// does not exist and was set to false.
///
public string GetSaveSlotPath(int slotIndex, bool create = false) {
string saveSlotPath = getSaveSlotPath(slotIndex);
if(!Directory.Exists(saveSlotPath) && create) {
Directory.CreateDirectory(saveSlotPath);
}
return saveSlotPath;
}
/// Returns the absolute path of a save slot
/// Index of the save slot whose absolute path will be returned
/// The absolute path of the save slot with the specified index
private string getSaveSlotPath(int slotIndex) {
//Debug.Log(StandardDirectories.SaveGamePath);
//Debug.Log(getSaveSlotDirectoryName(slotIndex));
return Path.Combine(
StandardDirectories.SaveGamePath, getSaveSlotDirectoryName(slotIndex)
);
}
/// Builds the name of the directory to use for a save slot
/// Index of the slot for which the directory name is built
/// The directory name for the save slot with the specified index
private string getSaveSlotDirectoryName(int slotIndex) {
return "SaveSlot." + slotIndex.ToString();
}
}
} // namespace Framework.Storage