feat: add cross-engine platform services
This commit is contained in:
@@ -3,8 +3,8 @@ using System.Collections.Concurrent;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace ShrinkDataSaver
|
||||
{
|
||||
@@ -21,7 +21,11 @@ namespace ShrinkDataSaver
|
||||
|
||||
public LocalStorageProvider(string rootPath = null)
|
||||
{
|
||||
_rootPath = rootPath ?? Application.persistentDataPath;
|
||||
_rootPath = rootPath ?? ShrinkDataSaverPlatform.Paths?.PersistentDataPath
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
?? UnityEngine.Application.persistentDataPath
|
||||
#endif
|
||||
?? throw new InvalidOperationException("ShrinkDataSaver persistent path is not configured.");
|
||||
}
|
||||
|
||||
private string Resolve(string path) =>
|
||||
@@ -33,7 +37,7 @@ namespace ShrinkDataSaver
|
||||
var dir = Path.GetDirectoryName(fullPath);
|
||||
if (!string.IsNullOrEmpty(dir))
|
||||
{
|
||||
await UniTask.RunOnThreadPool(() => Directory.CreateDirectory(dir), cancellationToken: ct);
|
||||
await Task.Run(() => Directory.CreateDirectory(dir), ct);
|
||||
}
|
||||
|
||||
var tempPath = fullPath + TempWriteSuffix;
|
||||
@@ -45,7 +49,7 @@ namespace ShrinkDataSaver
|
||||
try
|
||||
{
|
||||
await WriteFileBytesAsync(tempPath, data, ct);
|
||||
await UniTask.RunOnThreadPool(() =>
|
||||
await Task.Run(() =>
|
||||
{
|
||||
if (File.Exists(fullPath))
|
||||
{
|
||||
@@ -65,17 +69,17 @@ namespace ShrinkDataSaver
|
||||
{
|
||||
File.Move(tempPath, fullPath);
|
||||
}
|
||||
}, cancellationToken: ct);
|
||||
}, ct);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await UniTask.RunOnThreadPool(() =>
|
||||
await Task.Run(() =>
|
||||
{
|
||||
if (File.Exists(tempPath))
|
||||
{
|
||||
File.Delete(tempPath);
|
||||
}
|
||||
}, cancellationToken: CancellationToken.None);
|
||||
}, CancellationToken.None);
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
@@ -105,7 +109,7 @@ namespace ShrinkDataSaver
|
||||
}
|
||||
|
||||
public UniTask<bool> ExistsAsync(string path, CancellationToken ct = default) =>
|
||||
UniTask.RunOnThreadPool(() => File.Exists(Resolve(path)), cancellationToken: ct);
|
||||
Task.Run(() => File.Exists(Resolve(path)), ct).AsUniTask();
|
||||
|
||||
public async UniTask DeleteAsync(string path, CancellationToken ct = default)
|
||||
{
|
||||
@@ -114,13 +118,13 @@ namespace ShrinkDataSaver
|
||||
await pathLock.WaitAsync(ct);
|
||||
try
|
||||
{
|
||||
await UniTask.RunOnThreadPool(() =>
|
||||
await Task.Run(() =>
|
||||
{
|
||||
if (File.Exists(fullPath))
|
||||
{
|
||||
File.Delete(fullPath);
|
||||
}
|
||||
}, cancellationToken: ct);
|
||||
}, ct);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -130,7 +134,7 @@ namespace ShrinkDataSaver
|
||||
|
||||
public async UniTask<string[]> ListAsync(string prefix = "", CancellationToken ct = default)
|
||||
{
|
||||
return await UniTask.RunOnThreadPool(() =>
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
var dir = string.IsNullOrEmpty(prefix) ? _rootPath : Path.Combine(_rootPath, prefix);
|
||||
if (!Directory.Exists(dir))
|
||||
@@ -141,7 +145,7 @@ namespace ShrinkDataSaver
|
||||
return Directory.GetFiles(dir)
|
||||
.Select(f => Path.GetRelativePath(_rootPath, f))
|
||||
.ToArray();
|
||||
}, cancellationToken: ct);
|
||||
}, ct);
|
||||
}
|
||||
|
||||
private static SemaphoreSlim GetPathLock(string fullPath)
|
||||
|
||||
Reference in New Issue
Block a user