1
This commit is contained in:
78
Packages/Runtime/Components/SaveToFile.cs
Normal file
78
Packages/Runtime/Components/SaveToFile.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Sirenix.OdinInspector;
|
||||
using System.IO;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using BITKit.IO;
|
||||
namespace BITKit
|
||||
{
|
||||
public class SaveToFile : Provider, IAction
|
||||
{
|
||||
public bool isPersistentData;
|
||||
public bool crypto;
|
||||
[SerializeReference, SubclassSelector] public References extensions;
|
||||
[SerializeReference, SubclassSelector] public References path;
|
||||
[SerializeReference, SubclassSelector] public NameProvider nameProvider;
|
||||
|
||||
public void Excute()
|
||||
{
|
||||
string path;
|
||||
if (isPersistentData)
|
||||
{
|
||||
path = PathHelper.GetFolderPath(Utility.Path.persistentDataPath, this.path.Get());
|
||||
}
|
||||
else
|
||||
{
|
||||
path = PathHelper.GetFolderPath(Utility.Path.dataPath, this.path);
|
||||
}
|
||||
BITApp.Open(path);
|
||||
}
|
||||
|
||||
public override async void Set<T>(T obj)
|
||||
{
|
||||
await UniTask.SwitchToThreadPool();
|
||||
|
||||
var path = GetPath();
|
||||
BIT4Log.Log<SaveToFile>($"正在保存文件为:{path}");
|
||||
switch (obj)
|
||||
{
|
||||
case string _string:
|
||||
try
|
||||
{
|
||||
if (crypto)
|
||||
{
|
||||
_string = GZipHelper.GZipCompressString(_string);
|
||||
}
|
||||
File.WriteAllText(path, _string);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
Debug.LogWarning(e);
|
||||
}
|
||||
break;
|
||||
case BITAssets assets:
|
||||
assets.Build(path);
|
||||
break;
|
||||
case byte[] _bytes:
|
||||
File.WriteAllBytes(path, _bytes);
|
||||
break;
|
||||
}
|
||||
}
|
||||
string GetPath()
|
||||
{
|
||||
string path;
|
||||
var fileName = nameProvider.GetName(null, extensions);
|
||||
if (isPersistentData)
|
||||
{
|
||||
path = PathHelper.GetFilePath(Utility.Path.persistentDataPath, this.path.Get(), fileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
path = PathHelper.GetFilePath(Utility.Path.dataPath, this.path, fileName);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user