123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758 |
- using System;
- using System.IO;
- using UnityEngine;
- using System.Text;
- using System.Collections;
- using UnityEngine.Networking;
- using System.Threading.Tasks;
- using System.Threading;
- using System.Collections.Generic;
- namespace XRTool.Util
- {
-
-
-
-
-
-
- public class DataFileUtil
- {
-
-
-
- public const float WriteSpeed = 1024 * 1024 * 150f;
-
-
-
-
-
-
-
- public static string[] TexturesType = new string[] { "BMP", "EXR", "GIF", "HDR", "IFF", "JPG", "PICT", "PNG", "PSD", "TGA", "TIFF" };
-
-
-
- public static string[] MoviesType = new string[] { "MOV", "MPG", "MP4", "AVI", "ASF" };
-
-
-
- public static string[] AudiosType = new string[] { "ACC", "AIFF", "IT", "MOD", "MPEG", "OGGVORBIS", "S3M",
- "AIF", "WAV", "MP3", "OGG","XM","XMA","VAG","AUDIOQUEUE" };
- public static int maxTryCount = 5;
- public const string Extension = ".downing";
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static IEnumerator DownLoadData(string url, string path,
- Action<string, UnityWebRequest> completeData, DownloadHandler hander = null, Action<float, float> downLoadProcess = null, bool isAsync = false)
- {
- UnityWebRequest web = null;
-
- yield return RequestDownData(url, (string error, UnityWebRequest req) =>
- {
-
- if (string.IsNullOrEmpty(error))
- {
-
- if (!isAsync)
- {
- SaveDataToFile(path, req.downloadHandler.data);
- downLoadProcess?.Invoke(req.downloadHandler.data.Length, 1);
- completeData?.Invoke(path, req);
- return;
- }
- else
- {
- web = req;
-
- completeData?.Invoke(null, req);
- }
- }
- else
- {
-
- completeData?.Invoke(null, null);
-
- }
- }, hander, (float allLeng, float process) =>
- {
-
- downLoadProcess?.Invoke(allLeng, process / 1.12f);
- });
-
- if (isAsync && web != null)
- {
-
- yield return SaveDataToFile(path, web.downloadHandler.data, (float saveProcess) =>
- {
-
- downLoadProcess?.Invoke(web.downloadHandler.data.Length, saveProcess * 0.1f + 0.9f);
- }, (bool isSuccess, string filePath) =>
- {
- completeData?.Invoke(filePath, web);
- });
- }
- web.Dispose();
- }
-
-
-
-
- public static IEnumerator RequestData(string url, Action<string> reqComplete, bool isPost = true,
- Dictionary<string, string> header = null, Dictionary<string, string> form = null, Action complete = null)
- {
-
- using (UnityWebRequest web = isPost ? UnityWebRequest.Post(url, form) : UnityWebRequest.Get(url))
- {
- if (header != null)
- {
- foreach (var item in header)
- {
- web.SetRequestHeader(item.Key, item.Value);
- }
- }
- yield return web.SendWebRequest();
- if (web.isNetworkError || web.isHttpError)
- {
- UnityLog.LogError("RequestData:" + url + web.error);
- reqComplete?.Invoke(null);
- complete?.Invoke();
- yield break;
- }
- else
- {
- reqComplete?.Invoke(web.downloadHandler.text);
- complete?.Invoke();
- }
- }
- }
-
-
-
-
-
-
- public static IEnumerator GetDataSize(string url, Action<long> getLen)
- {
- var headRequest = UnityWebRequest.Head(url);
- yield return headRequest.SendWebRequest();
- getLen?.Invoke(long.Parse(headRequest.GetResponseHeader("Content-Length")));
- headRequest.Dispose();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static IEnumerator DownLoadDataAsync(string url, string path,
- Action<string, UnityWebRequest> completeData, DownloadHandler hander = null, Action<float, float> downLoadProcess = null)
- {
-
- FileStream fs = null;
- long curLen = 0;
- string tmpPath = path + Extension;
-
- try
- {
- string dir = Path.GetDirectoryName(tmpPath);
- if (!Directory.Exists(dir))
- {
- Directory.CreateDirectory(dir);
- }
- fs = new FileStream(tmpPath, FileMode.OpenOrCreate);
- curLen = fs.Length;
- UnityLog.Log("当前文件大小" + curLen, 2);
- }
- catch (Exception ex)
- {
- if (fs != null)
- {
- fs.Dispose();
- }
- fs = null;
- UnityLog.LogError(tmpPath + ex.ToString());
- downLoadProcess.Invoke(curLen, 0);
- completeData?.Invoke(null, null);
- yield break;
- }
-
-
- long totalLength = 0;
- yield return GetDataSize(url, (size) =>
- {
- totalLength = size;
- });
-
-
- if (curLen == totalLength)
- {
- fs.Dispose();
- fs.Close();
- downLoadProcess.Invoke(totalLength, 1);
- ReNameFile(tmpPath,path);
- completeData?.Invoke(path, null);
- yield break;
- }
-
-
- else if (curLen > totalLength)
- {
- UnityLog.LogError("文件大小异常" + curLen + "__" + totalLength);
- fs.Dispose();
- fs.Close();
- File.Delete(tmpPath);
- fs = new FileStream(tmpPath, FileMode.OpenOrCreate, FileAccess.Write);
- curLen = fs.Length;
- }
- fs.Seek(curLen, SeekOrigin.Begin);
- bool isSuccess = true;
- yield return RequestDownData(url, (byte[] data, int len) =>
- {
- if (isSuccess && data.Length > 0)
- {
- try
- {
- fs.Write(data, 0, len);
- fs.Flush();
- curLen += len;
- downLoadProcess?.Invoke(curLen, curLen * 1f / totalLength);
- }
- catch (Exception ex)
- {
- UnityLog.LogError(ex.ToString());
- isSuccess = false;
- return;
- }
- }
- else
- {
-
-
- UnityLog.LogError("Down Error:" + url);
- }
- }, totalLength, curLen, (web) =>
- {
- fs.Dispose();
- fs.Close();
- downLoadProcess.Invoke(totalLength, 1);
- ReNameFile(tmpPath,path);
- completeData?.Invoke(path, web);
- });
- }
-
-
-
-
-
-
-
-
- public static IEnumerator RequestDownData(string url, Action<byte[], int> downData, long allLen, long downedLength = 0,
- Action<UnityWebRequest> complete = null)
- {
-
- using (UnityWebRequest web = UnityWebRequest.Get(url))
- {
- web.disposeDownloadHandlerOnDispose = true;
- DataDownLoadHandler ddh = new DataDownLoadHandler();
- web.downloadHandler = ddh;
- ddh.OnReceiveData += downData;
-
- web.SetRequestHeader("Range", "bytes=" + downedLength + "-" + allLen);
- yield return web.SendWebRequest();
- if (web.isNetworkError || web.isHttpError)
- {
- UnityLog.LogError("RequestDownData" + url);
- complete?.Invoke(null);
- }
- else
- {
- complete?.Invoke(web);
- }
- }
- }
-
-
-
-
-
-
-
-
- public static IEnumerator RequestDownData(string url, Action<string, UnityWebRequest> complete, DownloadHandler hander = null, Action<float, float> updateProcess = null, WWWForm form = null)
- {
- UnityWebRequest web;
- Debug.Log("RequestDownData" + url);
- if (form == null)
- {
- web = UnityWebRequest.Get(url);
- }
- else
- {
- web = UnityWebRequest.Post(url, form);
- }
- if (hander != null)
- {
- web.downloadHandler = hander;
- }
- web.SendWebRequest();
- while (!web.isDone)
- {
- updateProcess?.Invoke(web.downloadedBytes, web.downloadProgress);
- yield return 0;
- }
- if (web.isDone)
- {
- updateProcess?.Invoke(web.downloadedBytes, 1);
- }
- if (web.isNetworkError || web.isHttpError)
- {
- complete?.Invoke(web.error, web);
- Debug.LogError(web.error + url);
- }
- else
- {
- complete?.Invoke(null, web);
- }
- web.Dispose();
- }
-
-
-
-
-
-
-
-
-
- public static IEnumerator RequestUpLoadData(string url, WWWForm form, Action<float, float> updateProcess, Action<string, UnityWebRequest> complete)
- {
- UnityWebRequest web = UnityWebRequest.Post(url, form);
- web.SendWebRequest();
- while (!web.isDone)
- {
- updateProcess?.Invoke(web.uploadedBytes, web.uploadProgress);
- yield return 0;
- }
- if (web.isDone)
- {
- updateProcess?.Invoke(web.uploadedBytes, 1);
- }
- if (web.isNetworkError || web.isHttpError)
- {
- complete?.Invoke(web.error, web);
- }
- else
- {
- complete?.Invoke(null, web);
- }
- web.Dispose();
- }
-
-
-
-
-
- public static bool isIgnoreExtension(string ignoreExtension, string suffix)
- {
- string[] igs = ignoreExtension.Split('|', ' ', ',');
- for (int i = 0; i < igs.Length; i++)
- {
- if (igs[i] == suffix)
- {
- return true;
- }
- }
- return false;
- }
-
-
-
-
- public static bool SaveDataToFile(string path, string txt)
- {
- try
- {
- if (!string.IsNullOrEmpty(txt))
- {
- File.WriteAllText(path, txt);
- return true;
- }
- }
- catch (Exception ex)
- {
- UnityLog.LogError(path + ex.ToString());
- }
- return false;
- }
-
-
-
-
-
- public static bool SaveDataToFile(string path, byte[] data)
- {
- try
- {
- string dir = Path.GetDirectoryName(path);
- if (!Directory.Exists(dir))
- {
- Directory.CreateDirectory(dir);
- }
- FileStream fs = new FileStream(path, FileMode.Create);
- fs.Write(data, 0, data.Length);
- fs.Dispose();
- fs.Close();
- return true;
- }
- catch (Exception ex)
- {
- UnityLog.LogError(path + ex.ToString());
- }
- return false;
- }
-
-
-
-
-
-
-
- public static IEnumerator SaveDataToFile(string path, string txt, Action<float> saveProcess, Action<bool, string> onComplete)
- {
- yield return SaveDataToFile(path, Encoding.Default.GetBytes(txt), saveProcess, onComplete);
- }
-
-
-
-
-
- private static IEnumerator SaveDataToFile(string path, byte[] data, Action<float> saveProcess, Action<bool, string> onComplete)
- {
- bool isSuccess = false;
- if (data != null && data.Length > 0)
- {
- Task task = null;
- FileStream fs = null;
- float allData = data.Length;
- try
- {
- fs = new FileStream(path, FileMode.Create);
- task = fs.WriteAsync(data, 0, data.Length);
- isSuccess = true;
- }
- catch (Exception ex)
- {
- UnityLog.LogError(path + ex.ToString());
- path = ex.ToString();
- isSuccess = false;
- }
- float tmp = 0;
- float allTime = data.Length / WriteSpeed;
-
- while (task != null && !task.IsCompleted)
- {
- if (tmp < allTime)
- {
- tmp += Time.deltaTime;
- }
- saveProcess?.Invoke(tmp / allTime);
- yield return 0;
- }
- saveProcess(1);
- fs.Dispose();
- fs.Close();
- }
- onComplete?.Invoke(isSuccess, path);
- }
-
-
-
-
-
- public static void FindFileBreadth(string path, Action<string> callBack)
- {
- try
- {
- DirectoryInfo di = new DirectoryInfo(path);
- FileInfo[] fis = di.GetFiles();
- for (int i = 0; i < fis.Length; i++)
- {
- callBack?.Invoke(fis[i].FullName);
- }
- DirectoryInfo[] dis = di.GetDirectories();
- for (int j = 0; j < dis.Length; j++)
- {
- FindFileBreadth(dis[j].FullName, callBack);
- }
- }
- catch (Exception ex)
- {
- UnityLog.LogError(ex.ToString());
- }
- }
-
-
-
-
- public static void FindFileByDepth(string dir, Action<string> callBack)
- {
- try
- {
- DirectoryInfo d = new DirectoryInfo(dir);
- FileSystemInfo[] fsinfos = d.GetFileSystemInfos();
- foreach (FileSystemInfo fsinfo in fsinfos)
- {
- if (fsinfo is DirectoryInfo)
- {
- FindFileByDepth(fsinfo.FullName, callBack);
- }
- else
- {
- callBack?.Invoke(fsinfo.FullName);
- }
- }
- }
- catch (Exception ex)
- {
- UnityLog.LogError(ex.ToString());
- }
- }
-
-
-
-
-
- public static string GetMD5HashFromFile(string fileName)
- {
- try
- {
- FileStream file = new FileStream(fileName, FileMode.Open);
- System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
- byte[] retVal = md5.ComputeHash(file);
- file.Close();
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < retVal.Length; i++)
- {
- sb.Append(retVal[i].ToString("x2"));
- }
- return sb.ToString();
- }
- catch (Exception ex)
- {
- throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
- }
- }
-
-
-
-
-
- public static void ReNameFile(string file, string newFile)
- {
- if (File.Exists(file))
- {
- try
- {
- string dir = Path.GetDirectoryName(newFile);
- if (!Directory.Exists(dir))
- {
- Directory.CreateDirectory(dir);
- }
- if (File.Exists(newFile))
- {
- File.Delete(newFile);
- }
- FileInfo info = new FileInfo(file);
- info.MoveTo(newFile);
- }
- catch (Exception ex)
- {
- UnityLog.LogError(file + " is error" + ex.ToString());
- }
- }
- }
-
-
-
-
-
- public static void DelFile(string file)
- {
- UnityLog.Log(file + " is Del", 3);
- if (File.Exists(file))
- {
- try
- {
- File.Delete(file);
- }
- catch (Exception ex)
- {
- UnityLog.LogError(file + " is error" + ex.ToString());
- }
- }
- }
-
-
-
-
-
-
-
- public static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
- {
- bool ret = false;
- try
- {
- SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
- DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";
- if (Directory.Exists(SourcePath))
- {
- if (Directory.Exists(DestinationPath) == false)
- Directory.CreateDirectory(DestinationPath);
- foreach (string fls in Directory.GetFiles(SourcePath))
- {
- FileInfo flinfo = new FileInfo(fls);
- flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
- }
- foreach (string drs in Directory.GetDirectories(SourcePath))
- {
- DirectoryInfo drinfo = new DirectoryInfo(drs);
- if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
- ret = false;
- }
- }
- ret = true;
- }
- catch (Exception ex)
- {
- ret = false;
- UnityLog.LogError(ex.ToString());
- }
- return ret;
- }
-
-
-
-
-
- public static bool IsMoviesFile(string fileName)
- {
- if (!string.IsNullOrEmpty(fileName))
- {
- string exten = Path.GetExtension(fileName);
- return IsMediaType(exten.ToUpper(), MoviesType);
- }
- return false;
- }
- public static bool IsTextureFile(string fileName)
- {
- if (!string.IsNullOrEmpty(fileName))
- {
- string exten = Path.GetExtension(fileName);
- return IsMediaType(exten.ToUpper(), TexturesType);
- }
- return false;
- }
-
-
-
-
-
-
- public static bool IsAudioFile(string fileName)
- {
- if (!string.IsNullOrEmpty(fileName))
- {
- string exten = Path.GetExtension(fileName);
- return IsMediaType(exten.ToUpper(), AudiosType);
- }
- return false;
- }
-
-
-
-
-
-
- public static bool IsMediaType(string fileName, string[] types)
- {
- if (!string.IsNullOrEmpty(fileName) && types != null)
- {
- for (int i = 0; i < types.Length; i++)
- {
- if (TexturesType[i] == fileName)
- {
- return true;
- }
- }
- }
- return false;
- }
-
-
-
-
-
-
- public static bool IsMediaType(string fileName)
- {
- if (!string.IsNullOrEmpty(fileName))
- {
- string exten = Path.GetExtension(fileName);
- exten = exten.ToUpper();
- return IsMediaType(exten, TexturesType) || IsMediaType(exten, MoviesType) || IsMediaType(exten, AudiosType);
- }
- return false;
- }
-
-
-
-
-
- public static bool Exists(string file)
- {
- return File.Exists(file);
- }
- }
- }
|