Beispiel #1
0
        /// <summary>Loads data of <typeparamref name="T"/> as a string from given path</summary>
        /// <typeparam name="T">The type of object to load from text</typeparam>
        public static bool LoadPlainText <T>(string path, out LoadResult <string> loadResult)
        {
            string filePath = Path.Combine(rootPath, path);

            if (!File.Exists(filePath))
            {
                loadResult.data   = string.Empty;
                loadResult.result = LoadResult.FileNotFound;
                return(false);
            }
            else
            {
                loadResult.data = File.ReadAllText(filePath);

                string key = $"{typeof(T).Name}@{path}";
                if (PlayerPrefs.HasKey(key))
                {
                    //if a hash was stored for this loadable object in player prefs, check if it matches the loaded one
                    string storedHash = PlayerPrefs.GetString(key);
                    string loadedHash = EncryptionSystem.Hash(loadResult.data);
                    if (storedHash != loadedHash)
                    {
                        Debug.LogWarning($"Loaded data {loadResult.data} @ {filePath} path isn't the same as the saved data :: this is not intended");
                        loadResult.result = LoadResult.FileChanged;
                        return(true);
                    }
                }

                loadResult.result = LoadResult.Succesfull;
                return(true);
            }
        }
Beispiel #2
0
        /// <summary>Loads json string at given path from root directory and converts it into T. Can also be used for loading monobehaviours and scriptableobjects</summary>
        public static bool LoadFromJSON <T>(string path, out LoadResult <T> loadResult) where T : new()
        {
            string filePath = Path.Combine(rootPath, path);

            if (!File.Exists(filePath))
            {
                loadResult.data   = default;
                loadResult.result = LoadResult.FileNotFound;
                return(false);
            }
            else
            {
                T      data = new T();
                string json = File.ReadAllText(filePath);
                JsonUtility.FromJsonOverwrite(json, data);

                loadResult.data = data;

                string key = $"{typeof(T).Name}@{path}";
                if (PlayerPrefs.HasKey(key))
                {
                    string storedHash = PlayerPrefs.GetString(key);
                    string loadedHash = EncryptionSystem.Hash(json);
                    if (storedHash != loadedHash)
                    {
                        Debug.LogWarning($"Loaded data {loadResult.data} @ {filePath} path isn't the same as the saved data :: this is not intended");
                        loadResult.result = LoadResult.FileChanged;
                        return(true);
                    }
                }

                loadResult.result = LoadResult.Succesfull;
                return(true);
            }
        }
        /// <summary>Saves the given data as plain text at given path</summary>
        public static void SaveAsPlainText <T>(string path, T data, SaveMode mode = SaveMode.UnSafe)
        {
            string filePath = Path.Combine(rootPath, path);

            VerifyFileDirectoryPath(filePath);

            string text = data.ToString();

            if (mode == SaveMode.Hashed)
            {
                //store the hashed text in player prefs to use for verification on load
                string hash = EncryptionSystem.Hash(text);
                string key  = $"{typeof(T).Name}@{path}";
                PlayerPrefs.SetString(key, hash);
            }

            lock (saveLock)
            {
                File.WriteAllText(filePath, text);
            }
        }
        /// <summary>Saves value as json string at given path from root directory. Can also be used for saving monobehaviours and scriptableobjects</summary>
        public static void SaveAsJSON <T>(string path, T value, SaveMode mode = SaveMode.UnSafe)
        {
            string filePath = Path.Combine(rootPath, path);

            VerifyFileDirectoryPath(filePath);

            string json = JsonUtility.ToJson(value);

            if (mode == SaveMode.Hashed)
            {
                //store the hashed json in player prefs to use for verification on load
                string hash = EncryptionSystem.Hash(json);
                string key  = $"{typeof(T).Name}@{path}";
                PlayerPrefs.SetString(key, hash);
            }

            lock (saveLock)
            {
                File.WriteAllText(filePath, json);
            }
        }