Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.1 kB
1
Indexable
Never
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public static class JsonUtilityHandler
{
    public static string ConvertObjectToJson (object data) 
    => JsonUtility.ToJson(data);
    
    public static void LoadOverrideFromJson(string json, object data)
    => JsonUtility.FromJsonOverwrite(json, data);
    
    public static T LoadFromJson<T>(string json) 
    =>JsonUtility.FromJson<T>(json);

} 


public static class SaveManager
{
    public enum FileStreamType { Binary = 0, FileStream = 1 , PlayerPref =2 };

    public static void SaveFile<T>(T objectT, string fileName, FileStreamType fileStreamType , string fileType = "json", string PathFolders = "")
    {
        switch (fileStreamType)
        {
            case FileStreamType.Binary:
                SaveToFilePathAndDontConvertItToBinary(objectT, fileName, fileType, PathFolders);
                break;
            case FileStreamType.FileStream:
                SaveToFilePathAndConvertItToBinary(objectT, fileName, fileType, PathFolders);
                break;
            case FileStreamType.PlayerPref:
                SaveToPlayerPref(objectT, fileName);
                break;
            default:
                throw new System.Exception("Save method was not assigned!");
         
        }
    }
    private static void SaveToFilePathAndConvertItToBinary<T>(T objectT, string fileName, string fileType, string PathFolders)
    {
        string path = string.Concat(Application.persistentDataPath, "/", PathFolders, fileName, ".", fileType);

        if (!Directory.Exists(string.Concat(Application.persistentDataPath, "/", PathFolders)))
            Directory.CreateDirectory(string.Concat(Application.persistentDataPath, "/", PathFolders));


        BinaryFormatter binaryFormatter = new BinaryFormatter();
        FileStream fileStream = new FileStream(path, FileMode.Create);
        binaryFormatter.Serialize(fileStream, objectT);
        fileStream.Close();
    }
    private static void SaveToFilePathAndDontConvertItToBinary<T>(T objectT, string fileName, string fileType, string PathFolders)
    {
        string windowPath = string.Concat(Application.dataPath, "/", PathFolders, fileName, ".", fileType);

        if (!Directory.Exists(string.Concat(Application.dataPath, "/", PathFolders)))
            Directory.CreateDirectory(string.Concat(Application.dataPath, "/", PathFolders));

        var streamWriter = File.CreateText(windowPath);
        streamWriter.Write(JsonUtilityHandler.ConvertObjectToJson(objectT));
        streamWriter.Close();
    }

    private static void SaveToPlayerPref<T>(T objectT, string fileName)
    {
        PlayerPrefs.SetString(fileName, JsonUtilityHandler.ConvertObjectToJson(objectT));
        PlayerPrefs.Save();
    }

    public static T Load<T>(string fileName, FileStreamType fileStream, string fileType = "json", string PathFolders = "") where T : class
    {

        T objectLoaded = null;

        switch (fileStream)
        {

            case FileStreamType.Binary:
            objectLoaded = LoadFromBinaryFilePath<T>(fileName, fileType, PathFolders);
                break;
            case FileStreamType.FileStream:
                objectLoaded = LoadFromFilePath<T>(fileName, fileType, PathFolders);
                break;
            case FileStreamType.PlayerPref:
                objectLoaded = LoadFromPlayerPref<T>(fileName);
                break;
            default:
                throw new System.Exception("FileStreamType is not valid!");
        }

       
      
        if (objectLoaded == null)
            Debug.Log("No Save File");

        return objectLoaded;

    }

    private static T LoadFromPlayerPref<T>(string fileName) where T : class
    {
        
        if (PlayerPrefs.HasKey(fileName))
            return JsonUtilityHandler.LoadFromJson<T>(PlayerPrefs.GetString(fileName));
        
        return null;
    }
    private static T LoadFromFilePath<T>(string fileName, string fileType, string PathFolders) where T : class
    {
        string path = string.Concat(Application.dataPath, "/", PathFolders, fileName, ".", fileType);

        if (File.Exists(path))
        {
           string txt =  File.ReadAllText (path);

            T loadedObject = JsonUtilityHandler.LoadFromJson<T>(txt);
   
            return loadedObject;
        }
        return null;
    }
    private static T LoadFromBinaryFilePath<T>(string fileName,  string fileType, string PathFolders) where T : class
    { 
        string path = string.Concat(Application.persistentDataPath, "/", PathFolders, fileName, ".", fileType);

        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream fileStream = new FileStream(path, FileMode.Open);
            T loadedObject =(T)formatter.Deserialize(fileStream) ;
            fileStream.Close();
            return loadedObject;
        }
        return null;
    }
}