public static System.IO.Stream LoadFileRelative(string path) { if (string.IsNullOrEmpty(path)) { return(null); } if (path[0] != '\\' && path[0] != '/') { path = "/" + path; } var spath = ThreadSafeValues.UpdatePath + path; try { if (PlatDependant.IsFileExist(spath)) { return(PlatDependant.OpenRead(spath)); } } catch (Exception e) { PlatDependant.LogError(e); } spath = ThreadSafeValues.AppStreamingAssetsPath + path; try { if (PlatDependant.IsFileExist(spath)) { return(PlatDependant.OpenRead(spath)); } } catch (Exception e) { PlatDependant.LogError(e); } return(null); }
public static TaskProgress UnzipAsync(string zip, string destdir) { var prog = new TaskProgress(); try { if (IsFileExist(zip)) { var taskcnt = Environment.ProcessorCount; int entryIndex = 0; int finishCnt = 0; Action <TaskProgress> UnzipWork = p => { try { using (var stream = PlatDependant.OpenRead(zip)) { using (var zipa = new ZipArchive(stream, ZipArchiveMode.Read)) { var entries = zipa.Entries; if (entries != null) { var index = System.Threading.Interlocked.Increment(ref entryIndex) - 1; if (index == 0) { prog.Total = entries.Count; } while (index < entries.Count) { System.Threading.Interlocked.Increment(ref prog.Length); try { var entry = entries[index]; var name = entry.FullName; var dest = System.IO.Path.Combine(destdir, name); using (var srcs = entry.Open()) { using (var dsts = PlatDependant.OpenWrite(dest)) { srcs.CopyTo(dsts); } } } catch (Exception e) { LogError(e); } index = System.Threading.Interlocked.Increment(ref entryIndex) - 1; } } } } } catch (Exception e) { LogError(e); prog.Error = e.Message; } finally { if (System.Threading.Interlocked.Increment(ref finishCnt) >= taskcnt) { prog.Done = true; } } }; for (int i = 0; i < taskcnt; ++i) { RunBackground(UnzipWork); } return(prog); } } catch (Exception e) { LogError(e); prog.Error = e.Message; } prog.Done = true; return(prog); }