Advertisement
apieceoffruit

CloudSave

Oct 15th, 2023 (edited)
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Unity.Services.CloudSave;
  5. using UnityEngine;
  6. using static Newtonsoft.Json.JsonConvert;
  7. namespace JS
  8. {
  9.  
  10.     public class CloudSave : ISaveClient
  11.     {
  12.        
  13.         public async Task Save(string key, object value) =>
  14.             await Call( CloudSaveService.Instance.Data.Player.SaveAsync(
  15.                 new Dictionary<string, object> { { key, value } }));
  16.  
  17.         public async Task Save(params (string key, object value)[] values) =>
  18.             await Call( CloudSaveService.Instance.Data.Player.SaveAsync(
  19.                 values.ToDictionary(item => item.key, item => item.value)));
  20.  
  21.         public async Task<T> Load<T>(string key)
  22.         {
  23.             var query = await Call( CloudSaveService.Instance.Data.Player.LoadAsync(new HashSet<string> { key }));
  24.             return query.TryGetValue(key, out var value) ? Deserialize<T>(value.Value.GetAsString()) : default;
  25.         }
  26.  
  27.         public async Task<IEnumerable<T>> Load<T>(params string[] keys)
  28.         {
  29.             var query = await Call( CloudSaveService.Instance.Data.Player.LoadAsync(keys.ToHashSet()));
  30.             return keys.Select(k =>
  31.             {
  32.                 if (query.TryGetValue(k, out var value))
  33.                     return value != null ? Deserialize<T>(value.Value.GetAsString()) : default;
  34.                 return default;
  35.             });
  36.         }
  37.  
  38.         public async Task Delete(string key) => await Call( CloudSaveService.Instance.Data.Player.DeleteAsync(key));
  39.  
  40.         public async Task DeleteAll()
  41.         {
  42.             var keys = await Call(   CloudSaveService.Instance.Data.Player.ListAllKeysAsync());
  43.             var tasks = keys.Select(k =>  CloudSaveService.Instance.Data.Player.DeleteAsync(k.Key)).ToList();
  44.             await Call(Task.WhenAll(tasks));
  45.         }
  46.  
  47.         static T Deserialize<T>(string input)
  48.         {
  49.             if (typeof(T) == typeof(string)) return (T)(object)input;
  50.             return DeserializeObject<T>(input);
  51.         }
  52.  
  53.         static async Task Call(Task action)
  54.         {
  55.             try
  56.             {
  57.                 await action;
  58.             }
  59.             catch (CloudSaveValidationException e)
  60.             {
  61.                 Debug.LogError(e);
  62.             }
  63.             catch (CloudSaveRateLimitedException e)
  64.             {
  65.                 Debug.LogError(e);
  66.             }
  67.             catch (CloudSaveException e)
  68.             {
  69.                 Debug.LogError(e);
  70.             }
  71.         }
  72.  
  73.         static async Task<T> Call<T>(Task<T> action)
  74.         {
  75.             try
  76.             {
  77.                 return await action;
  78.             }
  79.             catch (CloudSaveValidationException e)
  80.             {
  81.                 Debug.LogError(e);
  82.             }
  83.             catch (CloudSaveRateLimitedException e)
  84.             {
  85.                 Debug.LogError(e);
  86.             }
  87.             catch (CloudSaveException e)
  88.             {
  89.                 Debug.LogError(e);
  90.             }
  91.  
  92.             return default;
  93.         }
  94.     }
  95.  
  96.     public interface ISaveClient
  97.     {
  98.         Task Save(string key, object value);
  99.  
  100.         Task Save(params (string key, object value)[] values);
  101.  
  102.         Task<T> Load<T>(string key);
  103.  
  104.         Task<IEnumerable<T>> Load<T>(params string[] keys);
  105.  
  106.         Task Delete(string key);
  107.  
  108.         Task DeleteAll();
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement