using UnityEngine;
using XGame.Framework.Json;

namespace XGame.Framework.Persistence
{
    /// <summary>
    /// 数据持久化
    /// </summary>
    public static class Persistence
    {
        public static bool Write(string key, object val)
        {
            PlayerPrefs.SetString(key, XJson.ToJson(val));
            return true;
        }

        public static T Read<T>(string key, T defaultValue)
        {
            if (!PlayerPrefs.HasKey(key))
            {
                return defaultValue;
            }
            string str = PlayerPrefs.GetString(key);
            var val = XJson.ToObject<T>(str);
            return val;
        }

        public static void Delete(string key)
        {
            PlayerPrefs.DeleteKey(key);
        }

        public static void Clear()
        {
            PlayerPrefs.DeleteAll();
        }

        public static void Save()
        {
            PlayerPrefs.Save();
        }
    }
}

