private static void ReadBlocks(IReader reader, ContainerReader containerFile, TagContainer tags) { while (containerFile.NextBlock()) { switch (containerFile.BlockName) { case "data": // Data block tags.AddDataBlock(ReadDataBlock(reader, containerFile.BlockVersion)); break; case "tag!": // Extracted tag tags.AddTag(ReadTag(reader, containerFile.BlockVersion)); break; case "ersp": // Extracted Raw Resource Page tags.AddExtractedResourcePage(ReadExtractedResourcePage(reader, containerFile.BlockVersion)); break; case "rspg": // Resource page tags.AddResourcePage(ReadResourcePage(reader, containerFile.BlockVersion)); break; case "rsrc": // Resource info tags.AddResource(ReadResource(reader, containerFile.BlockVersion)); break; } } }
private void contextExtract_Click(object sender, RoutedEventArgs e) { // Get the menu item and the tag var item = e.Source as MenuItem; if (item == null) return; var tag = item.DataContext as TagEntry; if (tag == null) return; // Ask where to save the extracted tag collection var sfd = new SaveFileDialog { Title = "Save Tag Set", Filter = "Tag Container Files|*.tagc" }; bool? result = sfd.ShowDialog(); if (!result.Value) return; // Make a tag container var container = new TagContainer(); // Recursively extract tags var tagsToProcess = new Queue<ITag>(); var tagsProcessed = new HashSet<ITag>(); var resourcesToProcess = new Queue<DatumIndex>(); var resourcesProcessed = new HashSet<DatumIndex>(); var resourcePagesProcessed = new HashSet<ResourcePage>(); tagsToProcess.Enqueue(tag.RawTag); ResourceTable resources = null; using (IReader reader = _mapManager.OpenRead()) { while (tagsToProcess.Count > 0) { ITag currentTag = tagsToProcess.Dequeue(); if (tagsProcessed.Contains(currentTag)) continue; // Get the plugin path string className = VariousFunctions.SterilizeTagClassName(CharConstant.ToString(currentTag.Class.Magic)).Trim(); string pluginPath = string.Format("{0}\\{1}\\{2}.xml", VariousFunctions.GetApplicationLocation() + @"Plugins", _buildInfo.Settings.GetSetting<string>("plugins"), className); // Extract dem data blocks var blockBuilder = new DataBlockBuilder(reader, currentTag.MetaLocation, _cacheFile, _buildInfo); using (XmlReader pluginReader = XmlReader.Create(pluginPath)) AssemblyPluginLoader.LoadPlugin(pluginReader, blockBuilder); foreach (DataBlock block in blockBuilder.DataBlocks) container.AddDataBlock(block); // Add data for the tag that was extracted string tagName = _cacheFile.FileNames.GetTagName(currentTag) ?? currentTag.Index.ToString(); var extractedTag = new ExtractedTag(currentTag.Index, currentTag.MetaLocation.AsPointer(), currentTag.Class.Magic, tagName); container.AddTag(extractedTag); // Mark the tag as processed and then enqueue all of its child tags and resources tagsProcessed.Add(currentTag); foreach (DatumIndex tagRef in blockBuilder.ReferencedTags) tagsToProcess.Enqueue(_cacheFile.Tags[tagRef]); foreach (DatumIndex resource in blockBuilder.ReferencedResources) resourcesToProcess.Enqueue(resource); } // Load the resource table in if necessary if (resourcesToProcess.Count > 0) resources = _cacheFile.Resources.LoadResourceTable(reader); } // Extract resource info if (resources != null) { while (resourcesToProcess.Count > 0) { DatumIndex index = resourcesToProcess.Dequeue(); if (resourcesProcessed.Contains(index)) continue; // Add the resource Resource resource = resources.Resources[index.Index]; container.AddResource(new ExtractedResourceInfo(resource)); // Add data for its pages if (resource.Location == null) continue; if (resource.Location.PrimaryPage != null && !resourcePagesProcessed.Contains(resource.Location.PrimaryPage)) { container.AddResourcePage(resource.Location.PrimaryPage); resourcePagesProcessed.Add(resource.Location.PrimaryPage); } if (resource.Location.SecondaryPage == null || resourcePagesProcessed.Contains(resource.Location.SecondaryPage)) continue; container.AddResourcePage(resource.Location.SecondaryPage); resourcePagesProcessed.Add(resource.Location.SecondaryPage); } } // Write it to a file using (var writer = new EndianWriter(File.Open(sfd.FileName, FileMode.Create, FileAccess.Write), Endian.BigEndian)) TagContainerWriter.WriteTagContainer(container, writer); // YAY! MetroMessageBox.Show("Extraction Successful", "Extracted " + container.Tags.Count + " tag(s), " + container.DataBlocks.Count + " data block(s), " + container.ResourcePages.Count + " resource page pointer(s), and " + container.Resources.Count + " resource pointer(s)."); }
private void extractTags(bool withRaw, TagEntry tag) { // Ask where to save the extracted tag collection var sfd = new SaveFileDialog { Title = "Save Tag Set", Filter = "Tag Container Files|*.tagc" }; bool? result = sfd.ShowDialog(); if (!result.Value) return; // Make a tag container var container = new TagContainer(); // Recursively extract tags var tagsToProcess = new Queue<ITag>(); var tagsProcessed = new HashSet<ITag>(); var resourcesToProcess = new Queue<DatumIndex>(); var resourcesProcessed = new HashSet<DatumIndex>(); var resourcePagesProcessed = new HashSet<ResourcePage>(); tagsToProcess.Enqueue(tag.RawTag); ResourceTable resources = null; using (var reader = _mapManager.OpenRead()) { while (tagsToProcess.Count > 0) { var currentTag = tagsToProcess.Dequeue(); if (tagsProcessed.Contains(currentTag)) continue; // Get the plugin path var className = VariousFunctions.SterilizeTagClassName(CharConstant.ToString(currentTag.Class.Magic)).Trim(); var pluginPath = string.Format("{0}\\{1}\\{2}.xml", VariousFunctions.GetApplicationLocation() + @"Plugins", _buildInfo.Settings.GetSetting<string>("plugins"), className); // Extract dem data blocks var blockBuilder = new DataBlockBuilder(reader, currentTag, _cacheFile, _buildInfo); using (var pluginReader = XmlReader.Create(pluginPath)) AssemblyPluginLoader.LoadPlugin(pluginReader, blockBuilder); foreach (var block in blockBuilder.DataBlocks) container.AddDataBlock(block); // Add data for the tag that was extracted var tagName = _cacheFile.FileNames.GetTagName(currentTag) ?? currentTag.Index.ToString(); var extractedTag = new ExtractedTag(currentTag.Index, currentTag.MetaLocation.AsPointer(), currentTag.Class.Magic, tagName); container.AddTag(extractedTag); // Mark the tag as processed and then enqueue all of its child tags and resources tagsProcessed.Add(currentTag); foreach (var tagRef in blockBuilder.ReferencedTags) tagsToProcess.Enqueue(_cacheFile.Tags[tagRef]); foreach (var resource in blockBuilder.ReferencedResources) resourcesToProcess.Enqueue(resource); } // Load the resource table in if necessary if (resourcesToProcess.Count > 0) resources = _cacheFile.Resources.LoadResourceTable(reader); } // Extract resource info if (resources != null) { while (resourcesToProcess.Count > 0) { var index = resourcesToProcess.Dequeue(); if (resourcesProcessed.Contains(index)) continue; // Add the resource var resource = resources.Resources[index.Index]; container.AddResource(new ExtractedResourceInfo(index, resource)); // Add data for its pages if (resource.Location == null) continue; if (resource.Location.PrimaryPage != null && !resourcePagesProcessed.Contains(resource.Location.PrimaryPage)) { container.AddResourcePage(resource.Location.PrimaryPage); resourcePagesProcessed.Add(resource.Location.PrimaryPage); if (withRaw) { using (var fileStream = File.OpenRead(_cacheLocation)) { var resourceFile = _cacheFile; Stream resourceStream = fileStream; if (resource.Location.PrimaryPage.FilePath != null) { var resourceCacheInfo = App.AssemblyStorage.AssemblySettings.HalomapResourceCachePaths.FirstOrDefault( r => r.EngineName == _buildInfo.Name); var resourceCachePath = (resourceCacheInfo != null) ? resourceCacheInfo.ResourceCachePath : Path.GetDirectoryName(_cacheLocation); resourceCachePath = Path.Combine(resourceCachePath ?? "", Path.GetFileName(resource.Location.PrimaryPage.FilePath)); if (!File.Exists(resourceCachePath)) { MetroMessageBox.Show("Unable to extract tag", "Unable to extract tag, because a resource it relies on is in a external cache '{0}' that could not be found. Check Assembly's settings and set the file path to resource caches."); return; } resourceStream = File.OpenRead(resourceCachePath); resourceFile = new ThirdGenCacheFile(new EndianReader(resourceStream, Endian.BigEndian), _buildInfo, _cacheFile.BuildString); } var extractor = new ResourcePageExtractor(resourceFile); byte[] pageData; using (var pageStream = new MemoryStream()) { extractor.ExtractPage(resource.Location.PrimaryPage, resourceStream, pageStream); pageData = new byte[pageStream.Length]; Buffer.BlockCopy(pageStream.GetBuffer(), 0, pageData, 0, (int) pageStream.Length); } container.AddExtractedResourcePage(new ExtractedPage(pageData, resource.Location.PrimaryPage.Index)); } } } if (resource.Location.SecondaryPage == null || resourcePagesProcessed.Contains(resource.Location.SecondaryPage)) continue; container.AddResourcePage(resource.Location.SecondaryPage); resourcePagesProcessed.Add(resource.Location.SecondaryPage); if (withRaw) { using (var fileStream = File.OpenRead(_cacheLocation)) { var resourceFile = _cacheFile; Stream resourceStream = fileStream; if (resource.Location.SecondaryPage.FilePath != null) { var resourceCacheInfo = App.AssemblyStorage.AssemblySettings.HalomapResourceCachePaths.FirstOrDefault( r => r.EngineName == _buildInfo.Name); var resourceCachePath = (resourceCacheInfo != null) ? resourceCacheInfo.ResourceCachePath : Path.GetDirectoryName(_cacheLocation); resourceCachePath = Path.Combine(resourceCachePath ?? "", Path.GetFileName(resource.Location.SecondaryPage.FilePath)); if (!File.Exists(resourceCachePath)) { MetroMessageBox.Show("Unable to extract tag", "Unable to extract tag, because a resource it relies on is in a external cache '{0}' that could not be found. Check Assembly's settings and set the file path to resource caches."); return; } resourceStream = File.OpenRead(resourceCachePath); resourceFile = new ThirdGenCacheFile(new EndianReader(resourceStream, Endian.BigEndian), _buildInfo, _cacheFile.BuildString); } var extractor = new ResourcePageExtractor(resourceFile); byte[] pageData; using (var pageStream = new MemoryStream()) { extractor.ExtractPage(resource.Location.SecondaryPage, resourceStream, pageStream); pageData = new byte[pageStream.Length]; Buffer.BlockCopy(pageStream.GetBuffer(), 0, pageData, 0, (int)pageStream.Length); } container.AddExtractedResourcePage(new ExtractedPage(pageData, resource.Location.SecondaryPage.Index)); } } } } // Write it to a file using (var writer = new EndianWriter(File.Open(sfd.FileName, FileMode.Create, FileAccess.Write), Endian.BigEndian)) TagContainerWriter.WriteTagContainer(container, writer); // YAY! MetroMessageBox.Show("Extraction Successful", "Extracted " + container.Tags.Count + " tag(s), " + container.DataBlocks.Count + " data block(s), " + container.ResourcePages.Count + " resource page pointer(s), " + container.ExtractedResourcePages.Count + " extracted resource page(s), and " + container.Resources.Count + " resource pointer(s)."); }
private static void ReadBlocks(IReader reader, ContainerReader containerFile, TagContainer tags) { while (containerFile.NextBlock()) { switch (containerFile.BlockName) { case "data": // Data block tags.AddDataBlock(ReadDataBlock(reader, containerFile.BlockVersion)); break; case "tag!": // Extracted tag tags.AddTag(ReadTag(reader, containerFile.BlockVersion)); break; case "ersp": // Extracted Raw Resource Page tags.AddExtractedResourcePage(ReadExtractedResourcePage(reader, containerFile.BlockVersion)); break; case "rspg": // Resource page tags.AddResourcePage(ReadResourcePage(reader, containerFile.BlockVersion)); break; case "rsrc": // Resource info tags.AddResource(ReadResource(reader, containerFile.BlockVersion)); break; case "pdct": // Prediction info tags.AddPrediction(ReadPrediction(reader, containerFile.BlockVersion)); break; case "sndc": // Sound platform codec tags.AddSoundCodec(ReadSoundCodec(reader, containerFile.BlockVersion)); break; case "snpr": // Sound pitch range tags.AddSoundPitchRange(ReadSoundPitchRange(reader, containerFile.BlockVersion)); break; case "snld": // Sound language pitch range tags.AddSoundLanguageDuration(ReadSoundLanguageDuration(reader, containerFile.BlockVersion)); break; case "snpb": // Sound playback parameter tags.AddSoundPlayback(ReadSoundPlayback(reader, containerFile.BlockVersion)); break; case "snsc": // Sound scale tags.AddSoundScale(ReadSoundScale(reader, containerFile.BlockVersion)); break; case "spro": // Sound promotion tags.AddSoundPromotion(ReadSoundPromotion(reader, containerFile.BlockVersion)); break; case "scpb": // Sound custom playback tags.AddSoundCustomPlayback(ReadSoundCustomPlayback(reader, containerFile.BlockVersion)); break; case "snex": // Sound extra info tags.AddSoundExtraInfo(ReadSoundExtraInfo(reader, containerFile.BlockVersion)); break; } } }