// *********************************************************************** // Company : // Author : Kimch // Created : // // Last Modified By : Name // Last Modified On : // *********************************************************************** //#define COMPRESS_DATA namespace G { using System.IO; //using System.IO.Compression; using System.Threading.Tasks; using UnityEngine; public static class KFileUtils { /// The g_local documents path public static string LocalDocumentsPath { get { #if !UNITY_EDITOR && SDK_WECHAT_WASM return WeChatWASM.WX.env.USER_DATA_PATH; #else return Application.persistentDataPath; #endif } } /// Combines the specified path1.合并两个路径字符串. /// The path1. /// The path2. /// public static string Combine(string path1, string path2) { return Path.Combine(path1, path2); } /// Gets the extension.返回指定的路径字符串的扩展名. /// The path. /// public static string GetExtension(string path) { return Path.GetExtension(path); } /// Gets the name of the file.返回指定路径字符串的文件名和扩展名. /// The path. /// public static string GetFileName(string path) { return Path.GetFileName(path); } /// Gets the file name without extension.返回指定路径字符串的文件名. /// The path. /// public static string GetFileNameWithoutExtension(string path) { return Path.GetFileNameWithoutExtension(path); } /// Gets the files.返回指定目录中的文件的名称. /// The path. /// The search pattern. /// public static string[] GetFiles(string path, string searchPattern) { return Directory.GetFiles(path, searchPattern); } /// Gets the creation time.返回指定文件的创建日期和时间. /// The path. /// public static long GetCreationTime(string path) { return File.GetCreationTimeUtc(path).Ticks; } /// Gets the last access time.返回上次访问指定文件的日期和时间. /// The path. /// public static long GetLastAccessTime(string path) { return File.GetLastAccessTimeUtc(path).Ticks; } /// Gets the last write time.返回上次写入指定的文件的日期和时间. /// The path. /// public static long GetLastWriteTime(string path) { return File.GetLastWriteTimeUtc(path).Ticks; } /// Compares the last write time. /// The path1. /// The path2. /// public static long CompareLastWriteTime(string path1, string path2) { return (File.GetLastWriteTimeUtc(path1) - File.GetLastWriteTimeUtc(path2)).Ticks; } /// Deletes the specified path. /// The path. public static void DeleteFile(string path) { File.Delete(path); } /// Moves the specified source file name. /// Name of the sour file. /// Name of the dest file. public static void MoveFile(string sourFileName, string destFileName) { File.Move(sourFileName, destFileName); } /// /// /// /// /// public static void CopyFile(string sourFileName, string destFileName) { #if !UNITY_EDITOR && SDK_WECHAT_WASM var fs = WeChatWASM.WX.GetFileSystemManager(); fs.CopyFileSync(sourFileName, destFileName); #else File.Copy(sourFileName, destFileName, true); #endif } /// Existses the specified path. /// The path. /// public static bool ExistsFile(string path) { #if !UNITY_EDITOR && SDK_WECHAT_WASM try { var fs = WeChatWASM.WX.GetFileSystemManager(); return fs.AccessSync(path) == "access:ok"; } catch (System.Exception ex) { Debug.Log(ex); return false; } #else return File.Exists(path); #endif } /// Reads all bytes. /// The path. /// public static bool ReadAllBytes(string path, out byte[] bytes) { bytes = null; try { using (FileStream fs = File.OpenRead(path)) { #if COMPRESS_DATA using (GZipStream gz = new GZipStream(fs, CompressionMode.Decompress)) { bytes = new byte[gz.Length]; gz.Read(bytes, 0, bytes.Length); } #else bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); #endif } return true; } catch (System.Exception) { #if DEBUG_MY || UNITY_EDITOR UnityEngine.Debug.LogError("Failed read file: " + path); #endif return false; } } /// /// /// /// /// /// public static bool ReadAllBytes(string path, Stream stream) { #if !UNITY_EDITOR && SDK_WECHAT_WASM try { var fileSystem = WeChatWASM.WX.GetFileSystemManager(); var resultBytes = fileSystem.ReadFileSync(path); if (resultBytes != null && resultBytes.Length > 0) { stream.Write(resultBytes, 0, resultBytes.Length); return true; } return false; } catch (System.Exception ex) { Debug.Log(ex); return false; } #else try { using (var fs = File.OpenRead(path)) { if (fs.Length == 0) { return false; } #if COMPRESS_DATA using (GZipStream gz = new GZipStream(fs, CompressionMode.Decompress)) { gz.CopyTo(stream); } #else fs.CopyTo(stream); #endif } return true; } catch (System.Exception) { #if DEBUG_MY || UNITY_EDITOR UnityEngine.Debug.LogError("Failed read file: " + path); #endif return false; } #endif } /// /// /// /// /// public static async Task ReadAllBytesAsync(string path) { byte[] bytes = null; try { using (FileStream fs = File.OpenRead(path)) { #if COMPRESS_DATA using (GZipStream gz = new GZipStream(fs, CompressionMode.Decompress)) { bytes = new byte[gz.Length]; await gz.ReadAsync(bytes, 0, bytes.Length); } #else bytes = new byte[fs.Length]; await fs.ReadAsync(bytes, 0, bytes.Length); #endif } } catch (System.Exception) { #if DEBUG_MY || UNITY_EDITOR UnityEngine.Debug.LogError("Failed read file: " + path); #endif } return bytes; } /// /// /// /// /// /// public static async Task ReadAllBytesAsync(string path, Stream stream) { try { using (var fs = File.OpenRead(path)) { if (fs.Length == 0) { return false; } #if COMPRESS_DATA using (GZipStream gz = new GZipStream(fs, CompressionMode.Decompress)) { await gz.CopyToAsync(stream); } #else await fs.CopyToAsync(stream); #endif } UnityEngine.Debug.Log("read file: " + path + " " + stream.Length); return true; } catch (System.Exception) { #if DEBUG_MY || UNITY_EDITOR UnityEngine.Debug.LogError("Failed read file: " + path); #endif return false; } } /// /// /// /// /// /// /// /// public static bool WriteBytes(string path, byte[] bytes, int offset, int count) { try { using (var fs = File./*OpenWrite(path)*/Open(path, FileMode.Create)) { #if COMPRESS_DATA using (GZipStream gz = new GZipStream(fs, CompressionMode.Compress)) { gz.Write(bytes, offset, count); } #else fs.Write(bytes, offset, count); #endif } return true; } catch (System.Exception) { #if DEBUG_MY || UNITY_EDITOR UnityEngine.Debug.LogError("Failed write file: " + path); #endif return false; } } /// /// /// /// /// /// public static bool WriteBytes(string path, Stream stream) { #if !UNITY_EDITOR && SDK_WECHAT_WASM try { var memS = (MemoryStream)stream; var fileSystem = WeChatWASM.WX.GetFileSystemManager(); var result = "ok"; var data = memS.ToArray(); if (data != null && data.Length > 0) result = fileSystem.WriteFileSync(path, data); return result == "ok"; } catch (System.Exception ex) { Debug.Log(ex); return false; } #else try { using (var fs = File./*OpenWrite(path)*/Open(path, FileMode.Create)) { #if COMPRESS_DATA using (GZipStream gz = new GZipStream(fs, CompressionMode.Compress)) { stream.CopyTo(gz); } #else stream.CopyTo(fs); #endif fs.Flush(true); } return true; } catch (System.Exception) { #if DEBUG_MY || UNITY_EDITOR UnityEngine.Debug.LogError("Failed write file: " + path); #endif return false; } #endif } /// /// /// /// /// /// /// /// public static async Task WriteAllBytesAsync(string path, byte[] bytes, int offset, int count) { try { using (var fs = File./*OpenWrite(path)*/Open(path, FileMode.Create)) { #if COMPRESS_DATA using (GZipStream gz = new GZipStream(fs, CompressionMode.Compress)) { await gz.WriteAsync(bytes, offset, count); } #else await fs.WriteAsync(bytes, offset, count); #endif } return true; } catch (System.Exception) { #if DEBUG_MY || UNITY_EDITOR UnityEngine.Debug.LogError("Failed write file: " + path); #endif } return false; } /// /// /// /// /// /// public static async Task WriteAllBytesAsync(string path, Stream stream) { #if !UNITY_EDITOR && SDK_WECHAT_WASM try { WeChatWASM.WriteFileParam param = new WeChatWASM.WriteFileParam(); param.filePath = path; param.data = ((MemoryStream)stream).ToArray(); var tcs = new TaskCompletionSource(); param.complete += response => { tcs.SetResult(response.errCode == 0); }; //param.success += response => { tcs.SetResult(true); }; //param.fail += response => { tcs.SetResult(false); }; var fileSystem = WeChatWASM.WX.GetFileSystemManager(); fileSystem.WriteFile(param); return await tcs.Task; } catch (System.Exception ex) { Debug.Log(ex); return false; } #else try { using (var fs = File./*OpenWrite(path)*/Open(path, FileMode.Create)) { #if COMPRESS_DATA using (GZipStream gz = new GZipStream(fs, CompressionMode.Compress)) { await stream.CopyToAsync(gz); } #else await stream.CopyToAsync(fs); #endif } return true; } catch (System.Exception ex) { #if DEBUG_MY || UNITY_EDITOR UnityEngine.Debug.LogError($"Failed write file: {path} + ex:{ex.Message}"); #endif return false; } #endif } } }