using System; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; namespace OCES.Audio { /// /// 文件操作类 /// public static class FileManager { public static bool CreateDir(string dirPath) { if (string.IsNullOrEmpty(dirPath)) return false; if (Directory.Exists(dirPath)) { Directory.Delete(dirPath, true); } Directory.CreateDirectory(dirPath); return true; } /// /// 将数据写入二进制文件 /// /// /// 继承自IBinarySerialize的数据 public static bool WriteBinaryDataToFile(string filePath, IBinarySerializable data) { if (string.IsNullOrEmpty(filePath)) return false; if (File.Exists(filePath)) { File.Delete(filePath); } using (var fileStream = new FileStream(filePath, FileMode.Create)) { using (var bw = new BinaryWriter(fileStream)) { data.Serialize(bw); bw.Flush(); bw.Close(); } fileStream.Close(); } return true; } /// /// 将数据写入二进制文件 /// /// /// 类型(小写)和value的字符串键值对 /// public static bool WriteBinaryDatasToFile(string filePath, List> datas) { try { if (string.IsNullOrEmpty(filePath)) return false; if (File.Exists(filePath)) { File.Delete(filePath); } using (var fileStream = new FileStream(filePath, FileMode.Create)) { using (var bw = new BinaryWriter(fileStream)) { foreach (var data in datas) { if (data.Item1.Equals("int")) { if (string.IsNullOrEmpty(data.Item2)) { bw.Write(Convert.ToInt32(0)); } else { bw.Write(Convert.ToInt32(data.Item2)); } } else if (data.Item1.Equals("uint")) { if (string.IsNullOrEmpty(data.Item2)) { bw.Write(Convert.ToUInt32(0)); } else { bw.Write(Convert.ToUInt32(data.Item2)); } }else if (data.Item1.Equals("short")) { bw.Write(string.IsNullOrEmpty(data.Item2) ? Convert.ToInt16(0) : Convert.ToInt16(data.Item2)); } else if (data.Item1.Equals("ushort")) { bw.Write(string.IsNullOrEmpty(data.Item2) ? Convert.ToUInt16(0) : Convert.ToUInt16(data.Item2)); } else if (data.Item1.Equals("sbyte")) { if (string.IsNullOrEmpty(data.Item2)) { bw.Write(Convert.ToSByte(0)); } else { bw.Write(Convert.ToSByte(data.Item2)); } } else if (data.Item1.Equals("byte")) { if (string.IsNullOrEmpty(data.Item2)) { bw.Write(Convert.ToByte(0)); } else { bw.Write(Convert.ToByte(data.Item2)); } } else if (data.Item1.Equals("bool")) { if (string.IsNullOrEmpty(data.Item2)) { bw.Write(Convert.ToBoolean(false)); } else { bw.Write(Convert.ToBoolean(data.Item2)); } } else if (data.Item1.Equals("float")) { if (string.IsNullOrEmpty(data.Item2)) { bw.Write(Convert.ToSingle(0)); } else { bw.Write(Convert.ToSingle(data.Item2)); } } else if (data.Item1.Equals("double")) { if (string.IsNullOrEmpty(data.Item2)) { bw.Write(Convert.ToDouble(0)); } else { bw.Write(Convert.ToDouble(data.Item2)); } } else if (data.Item1.Equals("string")) { bw.Write(string.IsNullOrEmpty(data.Item2) ? "" : data.Item2.ToString()); } else if (data.Item1.Equals("long")) { if (string.IsNullOrEmpty(data.Item2)) { bw.Write(Convert.ToInt64(0)); } else { bw.Write(Convert.ToInt64(data.Item2)); } } else if (data.Item1.Equals("vector")) { //[1.2,3.4,5.6] var str = data.Item2.ToString(); if (string.IsNullOrEmpty(str)) { bw.Write(Convert.ToInt32(0)); } else { str = str.Replace("]", "").Replace("[", ""); var numStrs = str.Split(','); int vectorCount = 3; bw.Write(vectorCount); for (int i = 0; i < vectorCount; i++) { float v = Convert.ToSingle(numStrs[i]); bw.Write(v); } } } else if (data.Item1.Equals("vectorlist")) //List类型 { //[[1.2,3.4,5.6],[2.2,3.4,5.6],[3.2,3.4,5.6]] string str = data.Item2.ToString(); if (string.IsNullOrEmpty(str)) { bw.Write(Convert.ToInt32(0)); } else { str = str.Replace("]", "").Replace("[", ""); var numStrs = str.Split(','); bw.Write(Convert.ToInt32(numStrs.Length / 3)); for (int i = 0; i < numStrs.Length; i++) { if (i % 3 == 0) bw.Write(Convert.ToInt32(3)); bw.Write(Convert.ToSingle(numStrs[i])); } } } else if (data.Item1.Equals("intlist")) { string str = data.Item2.ToString(); if (string.IsNullOrEmpty(str)) { bw.Write(Convert.ToInt32(0)); } else { var numStrs = str.Split(','); bw.Write(numStrs.Length); for (int i = 0; i < numStrs.Length; i++) { bw.Write(Convert.ToInt32(numStrs[i])); } } } else if (data.Item1.Equals("floatlist")) { string str = data.Item2.ToString(); if (string.IsNullOrEmpty(str)) { bw.Write(Convert.ToInt32(0)); } else { var numStrs = str.Split(','); bw.Write(numStrs.Length); for (int i = 0; i < numStrs.Length; i++) { bw.Write(Convert.ToSingle(numStrs[i])); } } } else if (data.Item1.Equals("boollist")) { string str = data.Item2.ToString(); if (string.IsNullOrEmpty(str)) { bw.Write(Convert.ToInt32(0)); } else { var numStrs = str.Split(','); bw.Write(numStrs.Length); for (int i = 0; i < numStrs.Length; i++) { bw.Write(Convert.ToBoolean(numStrs[i])); } } } else if (data.Item1.Equals("stringlist")) { string str = data.Item2.ToString(); if (string.IsNullOrEmpty(str)) { bw.Write(Convert.ToInt32(0)); } else { var numStrs = str.Split(','); bw.Write(numStrs.Length); for (int i = 0; i < numStrs.Length; i++) { bw.Write(numStrs[i]); } } } else if (data.Item1.Equals("longlist")) { string str = data.Item2.ToString(); if (string.IsNullOrEmpty(str)) { bw.Write(Convert.ToInt32(0)); } else { var numStrs = str.Split(','); bw.Write(numStrs.Length); for (int i = 0; i < numStrs.Length; i++) { bw.Write(Convert.ToInt64(numStrs[i])); } } } else if (data.Item1.Contains("list<")) //泛型数组类型 { string str = data.Item2.ToString(); if (string.IsNullOrEmpty(str)) { bw.Write(Convert.ToInt32(0)); } else { var numStrs = str.Split(','); bw.Write(numStrs.Length); var tempS = data.Item1.Substring(5); var listType = tempS.Substring(0, tempS.Length - 1); if (listType.Equals("int")) { for (int i = 0; i < numStrs.Length; i++) { bw.Write(Convert.ToInt32(numStrs[i])); } } else if (listType.Equals("uint")) { for (int i = 0; i < numStrs.Length; i++) { bw.Write(Convert.ToUInt32(numStrs[i])); } } else if (listType.Equals("short")) { foreach (string t in numStrs) { bw.Write(Convert.ToInt16(t)); } } else if (listType.Equals("ushort")) { foreach (string t in numStrs) { bw.Write(Convert.ToUInt16(t)); } } else if (listType.Equals("sbyte")) { for (int i = 0; i < numStrs.Length; i++) { bw.Write(Convert.ToSByte(numStrs[i])); } } else if (listType.Equals("byte")) { for (int i = 0; i < numStrs.Length; i++) { bw.Write(Convert.ToByte(numStrs[i])); } } else if (listType.Equals("bool")) { for (int i = 0; i < numStrs.Length; i++) { bw.Write(Convert.ToBoolean(numStrs[i])); } } else if (listType.Equals("float")) { for (int i = 0; i < numStrs.Length; i++) { bw.Write(Convert.ToSingle(numStrs[i])); } } else if (listType.Equals("long")) { for (int i = 0; i < numStrs.Length; i++) { bw.Write(Convert.ToInt64(numStrs[i])); } } else if (listType.Equals("string")) { for (int i = 0; i < numStrs.Length; i++) { bw.Write(Convert.ToString(numStrs[i])); } } else { Debug.LogError("数组类型List,T不是支持的Int,Float,String这三种类型,需要扩展类型"); } } } else { Debug.LogError($"写入二进制文件,数据类型{data.Item1}没有适配"); return false; } } bw.Flush(); bw.Close(); } fileStream.Close(); } return true; } catch (Exception ex) { Debug.LogError(ex.ToString()); return false; } } /// /// 从内存流中读取二进制 /// /// /// /// public static bool ReadBinaryDataFromBytes(byte[] bytes, ref IBinarySerializable data) { if (bytes == null) return false; using (var memoryStream = new MemoryStream(bytes)) { using (var br = new BinaryReader(memoryStream)) { data.DeSerialize(br); br.Close(); } memoryStream.Close(); } return true; } /// /// 读取二进制文件 /// /// /// /// public static bool ReadBinaryDataFromFile(string filePath, ref IBinarySerializable data) { if (string.IsNullOrEmpty(filePath)) { return false; } using (var fileStream = new FileStream(filePath, FileMode.Open)) { using (var br = new BinaryReader(fileStream)) { data.DeSerialize(br); br.Close(); } fileStream.Close(); } return true; } public static bool WriteBytesToFile(string filePath, byte[] data) { if (string.IsNullOrEmpty(filePath)) return false; if (File.Exists(filePath)) { File.Delete(filePath); } var file = new FileInfo(filePath); using (Stream sw = file.Create()) { sw.Write(data, 0, data.Length); sw.Flush(); sw.Close(); } return true; } /// /// 将字符串写入文件 /// /// /// /// public static bool WriteToFile(string filePath, string context) { return WriteToFile(filePath, context, Encoding.Default); } public static bool WriteToFile(string filePath, string context, Encoding encoding) { if (string.IsNullOrEmpty(filePath)) return false; if (File.Exists(filePath)) { File.Delete(filePath); } using (FileStream fs = new FileStream(filePath, FileMode.Create)) { var data = encoding.GetBytes(context); fs.Write(data, 0, data.Length); fs.Flush(); fs.Close(); } return true; } /// /// 按行读取 /// /// /// public static string ReadAllByLine(string path) { if (string.IsNullOrEmpty(path) || !File.Exists(path)) { return string.Empty; } StringBuilder sb = new StringBuilder(); using (StreamReader sr = new StreamReader(path, Encoding.Default)) { string line; while ((line = sr.ReadLine()) != null) { sb.AppendLine(line); } sr.Close(); } return sb.ToString(); } public static byte[] ReadAllBytes(string path) { if (string.IsNullOrEmpty(path) || !File.Exists(path)) { return null; } return File.ReadAllBytes(path); } /// /// 修改文件内容 /// /// /// /// public static void ReplaceContent(string path, string normalStr, string newStr) { if (string.IsNullOrEmpty(path) || !File.Exists(path)) { return; } string strContent = File.ReadAllText(path); strContent = strContent.Replace(normalStr, newStr); File.WriteAllText(path, strContent); } /// /// 批量修改文件内容 /// /// /// /// public static void ReplaceContent(string path, string newStr, params string[] normalStrs) { if (string.IsNullOrEmpty(path) || !File.Exists(path)) { return; } string strContent = File.ReadAllText(path); for (int i = 0; i < normalStrs.Length; i++) { strContent = strContent.Replace(normalStrs[i], newStr); } File.WriteAllText(path, strContent); } } }