public void Run() { ProcessBinder(); if (!PopulateResourcesOnly) { foreach (var p in PendingResources) { var f = Binder.ReadFile(p.Item3); var task = new LoadResourceFromBytesTask(p.Item1, f, AccessLevel, ResourceManager.Locator.Type); task.Run(); } foreach (var t in PendingTPFs) { var f = TPF.Read(Binder.ReadFile(t.Item2)); var task = new LoadTPFResourcesTask(t.Item1, f, AccessLevel, ResourceManager.Locator.Type); task.Run(); } } PendingResources.Clear(); Binder = null; }
public Task RunAsync(IProgress <int> progress) { return(BinderTaskFactory.StartNew(() => { ProcessBinder(); if (!PopulateResourcesOnly) { bool doasync = (PendingResources.Count() + PendingTPFs.Count()) > 1; int i = 0; foreach (var p in PendingResources) { var f = Binder.ReadFile(p.Item3); var task = new LoadResourceFromBytesTask(p.Item1, f, AccessLevel, ResourceManager.Locator.Type); var size = task.GetEstimateTaskSize(); TotalSize += size; if (doasync) { var progress1 = new Progress <int>(); TaskSizes.Add(size); lock (ProgressLock) { TaskProgress.Add(0); } int bindi = i; progress1.ProgressChanged += (x, e) => { lock (ProgressLock) { TaskProgress[bindi] = e; } UpdateProgress(progress); }; LoadingTasks.Add(task.RunAsync(progress1)); i++; } else { task.Run(); i++; progress.Report(i); } } foreach (var t in PendingTPFs) { var f = TPF.Read(Binder.ReadFile(t.Item2)); var task = new LoadTPFResourcesTask(t.Item1, f, AccessLevel, ResourceManager.Locator.Type); var size = task.GetEstimateTaskSize(); TotalSize += size; if (doasync) { var progress1 = new Progress <int>(); TaskSizes.Add(size); lock (ProgressLock) { TaskProgress.Add(0); } int bindi = i; progress1.ProgressChanged += (x, e) => { lock (ProgressLock) { TaskProgress[bindi] = e; } UpdateProgress(progress); }; LoadingTasks.Add(task.RunAsync(progress1)); i++; } else { task.Run(); i++; progress.Report(i); } } } PendingResources.Clear(); // Wait for all the tasks to complete while (LoadingTasks.Count() > 0) { int idx = Task.WaitAny(LoadingTasks.ToArray()); LoadingTasks.RemoveAt(idx); } Binder = null; })); }
public void ProcessBinder() { if (Binder == null) { string o; var path = ResourceManager.Locator.VirtualToRealPath(BinderVirtualPath, out o); Binder = InstantiateBinderReaderForFile(path, ResourceManager.Locator.Type); if (Binder == null) { return; } } for (int i = 0; i < Binder.Files.Count(); i++) { var f = Binder.Files[i]; if (BinderLoadMask != null && !BinderLoadMask.Contains(i)) { continue; } var binderpath = f.Name; var filevirtpath = ResourceManager.Locator.GetBinderVirtualPath(BinderVirtualPath, binderpath); if (AssetWhitelist != null && !AssetWhitelist.Contains(filevirtpath)) { continue; } IResourceHandle handle = null; /*if (ResourceMan.ResourceDatabase.ContainsKey(filevirtpath)) * { * handle = ResourceMan.ResourceDatabase[filevirtpath]; * }*/ if (filevirtpath.ToUpper().EndsWith(".TPF") || filevirtpath.ToUpper().EndsWith(".TPF.DCX")) { string virt = BinderVirtualPath; if (virt.StartsWith($@"map/tex")) { var regex = new Regex(@"\d{4}$"); if (regex.IsMatch(virt)) { virt = virt.Substring(0, virt.Length - 5); } else if (virt.EndsWith("tex")) { virt = virt.Substring(0, virt.Length - 4); } } PendingTPFs.Add(new Tuple <string, BinderFileHeader>(virt, f)); } else { if (ResourceMask.HasFlag(ResourceType.Flver) && (filevirtpath.ToUpper().EndsWith(".FLVER") || filevirtpath.ToUpper().EndsWith(".FLV") || filevirtpath.ToUpper().EndsWith(".FLV.DCX"))) { //handle = new ResourceHandle<FlverResource>(); handle = ResourceManager.GetResource <FlverResource>(filevirtpath); } else if (ResourceMask.HasFlag(ResourceType.CollisionHKX) && (filevirtpath.ToUpper().EndsWith(".HKX") || filevirtpath.ToUpper().EndsWith(".HKX.DCX"))) { handle = ResourceManager.GetResource <HavokCollisionResource>(filevirtpath); } else if (ResourceMask.HasFlag(ResourceType.Navmesh) && filevirtpath.ToUpper().EndsWith(".NVM")) { handle = ResourceManager.GetResource <NVMNavmeshResource>(filevirtpath); } else if (ResourceMask.HasFlag(ResourceType.NavmeshHKX) && (filevirtpath.ToUpper().EndsWith(".HKX") || filevirtpath.ToUpper().EndsWith(".HKX.DCX"))) { handle = ResourceManager.GetResource <HavokNavmeshResource>(filevirtpath); } if (handle != null) { PendingResources.Add(new Tuple <IResourceHandle, string, BinderFileHeader>(handle, filevirtpath, f)); } } } }
public static void WriteBinderFiles(BinderReader bnd, XmlWriter xw, string targetDir, IProgress <float> progress) { xw.WriteStartElement("files"); var pathCounts = new Dictionary <string, int>(); for (int i = 0; i < bnd.Files.Count; i++) { BinderFileHeader file = bnd.Files[i]; string root = ""; string path; if (Binder.HasNames(bnd.Format)) { path = YBUtil.UnrootBNDPath(file.Name, out root); } else if (Binder.HasIDs(bnd.Format)) { path = file.ID.ToString(); } else { path = i.ToString(); } xw.WriteStartElement("file"); xw.WriteElementString("flags", file.Flags.ToString()); if (Binder.HasIDs(bnd.Format)) { xw.WriteElementString("id", file.ID.ToString()); } if (root != "") { xw.WriteElementString("root", root); } xw.WriteElementString("path", path); string suffix = ""; if (pathCounts.ContainsKey(path)) { pathCounts[path]++; suffix = $" ({pathCounts[path]})"; xw.WriteElementString("suffix", suffix); } else { pathCounts[path] = 1; } if (file.CompressionType != DCX.Type.Zlib) { xw.WriteElementString("compression_type", file.CompressionType.ToString()); } xw.WriteEndElement(); byte[] bytes = bnd.ReadFile(file); string outPath = $@"{targetDir}\{Path.GetDirectoryName(path)}\{Path.GetFileNameWithoutExtension(path)}{suffix}{Path.GetExtension(path)}"; Directory.CreateDirectory(Path.GetDirectoryName(outPath)); File.WriteAllBytes(outPath, bytes); progress.Report((float)i / bnd.Files.Count); } xw.WriteEndElement(); }