using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Cysharp.Threading.Tasks;
using MoralisUnity.Platform.Abstractions;
using MoralisUnity.Platform.Objects;
using MoralisUnity.Platform.Utilities;
using UnityEngine;
using static MoralisUnity.Platform.ResourceWrapper;
#pragma warning disable CS1998 // This async method lacks 'await' operators and will run synchronously
namespace MoralisUnity.Platform.Services.Infrastructure
{
///
/// Implements `IStorageController` for PCL targets, based off of PCLStorage.
///
public class MoralisCacheService : IDiskFileCacheService where TUser : MoralisUser
{
class FileBackedCache : IDataCache
{
public FileBackedCache(FileInfo file) => File = file;
internal void Save()
{
File.WriteContent(JsonUtilities.Encode(Storage));
}
internal async UniTask LoadAsync()
{
Storage = new Dictionary { };
#if !UNITY_WEBGL
if (File.Exists)
{
string data = string.Empty;
try
{
data = await File.ReadAllTextAsync();
}
catch (Exception exp)
{
Debug.Log($"File read error: {exp.Message}");
}
lock (Mutex)
{
try
{
Storage = JsonUtilities.Parse(data) as Dictionary;
}
catch
{
Storage = new Dictionary { };
}
}
}
else
{
Storage = new Dictionary { };
using (File.Create())
{
}
}
#endif
}
internal void Update(IDictionary contents) => Lock(() => Storage = contents.ToDictionary(element => element.Key, element => element.Value));
public async UniTask AddAsync(string key, object value)
{
Storage[key] = value;
Save();
}
public async UniTask RemoveAsync(string key)
{
Storage.Remove(key);
Save();
}
public void Add(string key, object value) => throw new NotSupportedException(FileBackedCacheSynchronousMutationNotSupportedMessage);
public bool Remove(string key) => throw new NotSupportedException(FileBackedCacheSynchronousMutationNotSupportedMessage);
public void Add(KeyValuePair item) => throw new NotSupportedException(FileBackedCacheSynchronousMutationNotSupportedMessage);
public bool Remove(KeyValuePair item) => throw new NotSupportedException(FileBackedCacheSynchronousMutationNotSupportedMessage);
public bool ContainsKey(string key) => Lock(() => Storage.ContainsKey(key));
public bool TryGetValue(string key, out object value)
{
lock (Mutex)
{
return (Result: Storage.TryGetValue(key, out object found), value = found).Result;
}
}
public void Clear() => Lock(() => Storage.Clear());
public bool Contains(KeyValuePair item) => Lock(() => Elements.Contains(item));
public void CopyTo(KeyValuePair[] array, int arrayIndex) => Lock(() => Elements.CopyTo(array, arrayIndex));
public IEnumerator> GetEnumerator() => Storage.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => Storage.GetEnumerator();
public FileInfo File { get; set; }
public object Mutex { get; set; } = new object { };
// ALTNAME: Operate
TResult Lock(Func operation)
{
lock (Mutex)
{
return operation.Invoke();
}
}
void Lock(Action operation)
{
lock (Mutex)
{
operation.Invoke();
}
}
ICollection> Elements => Storage as ICollection>;
Dictionary Storage { get; set; } = new Dictionary { };
public ICollection Keys => Storage.Keys;
public ICollection