using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using Cysharp.Threading.Tasks; using BITKit.IO; using System.IO.Compression; 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 Execute() { 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 obj) { await UniTask.SwitchToThreadPool(); var path = GetPath(); BIT4Log.Log($"正在保存文件为:{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.Get()); if (isPersistentData) { path = PathHelper.GetFilePath(Utility.Path.persistentDataPath, this.path.Get(), fileName); } else { path = PathHelper.GetFilePath(Utility.Path.dataPath, this.path, fileName); } return path; } } }