public static void LoadToTemp(byte[] data, string path, EndLoadToTemp endcall, bool removeImpurity) { TempData td; if (TryGetValueTempDic(path, out td)) { if (td != null) { td.time = SDateTime.TotalMilliseconds; endcall(td.path); return; } else { RemoveFromTempDic(path); UnityEngine.Debug.LogError("missing path temp path->" + path); } } ClearTemp(); if (removeImpurity) { ResLoader.RemoveImpurity(data, (tdata) => { if (TryGetValueTempDic(path, out td)) { if (td != null) { endcall(td.path); } else { UnityEngine.Debug.LogError("temp dic contains key, but value is null->" + path); } } else { string tp = GenerateTempPath(); td = new TempData(); td.path = tp; td.time = SDateTime.TotalMilliseconds; AddToTempDic(path, td); using (FileStream f = File.OpenWrite(tp)) { f.Write(tdata, 0, tdata.Length); f.Flush(); f.Close(); } endcall(tp); } }); } else { if (TryGetValueTempDic(path, out td)) { if (td != null) { endcall(td.path); } else { UnityEngine.Debug.LogError("temp dic contains key, but value is null->" + path); } } else { string tp = GenerateTempPath(); td = new TempData(); td.path = tp; td.time = SDateTime.TotalMilliseconds; AddToTempDic(path, td); using (FileStream f = File.OpenWrite(tp)) { f.Write(data, 0, data.Length); f.Flush(); f.Close(); } endcall(tp); } } }
public static void LoadToTemp(string path, bool removeImpurity, EndLoadToTemp endcall) { string rePath = null; ThreadTask.RunAsync(() => { try { using (FileStream fs = File.OpenRead(path)) { byte[] buffer; int bufferID; lock (lockBuffer) { bufferID = cachedBuffer.GetByteBuffer(out buffer); } if (bufferID >= 0) { FileStream tfs = LoadToTempAdditive_Begin(path, out rePath); try { int bufferLen = buffer.Length; int remain = (int)fs.Length; int total = remain; int pindex = 0; int readlen = 0; int remainTime = 0; while (remain > 0) { remainTime++; if (remainTime > 1000) { Debugger.LogError("error read file temp large"); rePath = null; break; } if (remain > bufferLen) { readlen = bufferLen; } else { readlen = remain; } if (readlen > 0) { fs.Read(buffer, 0, readlen); LoadToTempAdditive(rePath, tfs, buffer, readlen, total, removeImpurity, ref pindex); } remain -= readlen; if (remain <= 0) { break; } } } catch (Exception e) { Debugger.LogError("error read file temp->" + e.ToString()); rePath = null; } lock (lockBuffer) { cachedBuffer.RecycleByteBuffer(bufferID); } LoadToTempAdditive_End(rePath, tfs); } else { Debugger.LogError("load to temp get cached buffer failed->" + bufferID); } fs.Close(); } } catch (System.Exception e) { Debugger.LogError("read file exception->" + path + "^" + e.ToString()); } }, () => { if (endcall != null) { endcall(rePath); } }); }