ResourceWriter(ModuleDef module, Stream stream, ResourceElementSet resources) { this.module = module; this.typeCreator = new ResourceDataCreator(module); this.writer = new BinaryWriter(stream); this.resources = resources; }
ResourceElementSet Read() { ResourceElementSet resources = new ResourceElementSet(); uint sig = reader.ReadUInt32(); if (sig != 0xBEEFCACE) throw new ResourceReaderException(string.Format("Invalid resource sig: {0:X8}", sig)); if (!CheckReaders()) throw new ResourceReaderException("Invalid resource reader"); int version = reader.ReadInt32(); if (version != 2)//TODO: Support version 1 throw new ResourceReaderException(string.Format("Invalid resource version: {0}", version)); int numResources = reader.ReadInt32(); if (numResources < 0) throw new ResourceReaderException(string.Format("Invalid number of resources: {0}", numResources)); int numUserTypes = reader.ReadInt32(); if (numUserTypes < 0) throw new ResourceReaderException(string.Format("Invalid number of user types: {0}", numUserTypes)); var userTypes = new List<UserResourceType>(); for (int i = 0; i < numUserTypes; i++) userTypes.Add(new UserResourceType(reader.ReadString(), ResourceTypeCode.UserTypes + i)); reader.Position = (reader.Position + 7) & ~7; var hashes = new int[numResources]; for (int i = 0; i < numResources; i++) hashes[i] = reader.ReadInt32(); var offsets = new int[numResources]; for (int i = 0; i < numResources; i++) offsets[i] = reader.ReadInt32(); long baseOffset = reader.Position; long dataBaseOffset = reader.ReadInt32(); long nameBaseOffset = reader.Position; long end = reader.Length; var infos = new List<ResourceInfo>(numResources); for (int i = 0; i < numResources; i++) { reader.Position = nameBaseOffset + offsets[i]; var name = reader.ReadString(Encoding.Unicode); long offset = dataBaseOffset + reader.ReadInt32(); infos.Add(new ResourceInfo(name, offset)); } infos.Sort((a, b) => a.offset.CompareTo(b.offset)); for (int i = 0; i < infos.Count; i++) { var info = infos[i]; var element = new ResourceElement(); element.Name = info.name; reader.Position = info.offset; long nextDataOffset = i == infos.Count - 1 ? end : infos[i + 1].offset; int size = (int)(nextDataOffset - info.offset); element.ResourceData = ReadResourceData(userTypes, size); resources.Add(element); } return resources; }
public byte[] Convert() { var resources = new ResourceElementSet(); foreach (var info in infos) resources.Add(Convert(info)); var memStream = new MemoryStream(); ResourceWriter.Write(module, memStream, resources); return memStream.ToArray(); }
ResXProjectFile CreateResXFile(ModuleDef module, EmbeddedResource er, ResourceElementSet set, string filename, string typeFullName, bool isSatellite) { Debug.Assert(options.CreateResX); if (!options.CreateResX) throw new InvalidOperationException(); return new ResXProjectFile(module, filename, typeFullName, er) { IsSatelliteFile = isSatellite, }; }
IEnumerable<ProjectFile> CreateXamlResourceFiles(ModuleDef module, ResourceNameCreator resourceNameCreator, ResourceElementSet set) { bool decompileBaml = options.DecompileXaml && options.DecompileBaml != null; foreach (var e in set.ResourceElements) { Debug.Assert(e.ResourceData.Code == ResourceTypeCode.ByteArray || e.ResourceData.Code == ResourceTypeCode.Stream); var data = (byte[])((BuiltInResourceData)e.ResourceData).Data; if (decompileBaml && e.Name.EndsWith(".baml", StringComparison.OrdinalIgnoreCase)) { string typeFullName; var filename = resourceNameCreator.GetBamlResourceName(e.Name, out typeFullName); yield return new BamlResourceProjectFile(filename, data, typeFullName, (bamlData, stream) => options.DecompileBaml(module, bamlData, options.DecompilationContext.CancellationToken, stream)); } else if (StringComparer.InvariantCultureIgnoreCase.Equals(splashScreenImageName, e.Name)) { var filename = resourceNameCreator.GetXamlResourceFilename(e.Name); yield return new SplashScreenProjectFile(filename, data, e.Name); } else { var filename = resourceNameCreator.GetXamlResourceFilename(e.Name); yield return new ResourceProjectFile(filename, data, e.Name); } } }
bool IsXamlResource(ModuleDef module, string name, ResourceElementSet set) { var asm = module.Assembly; if (asm == null || !module.IsManifestModule) return false; string culture = UTF8String.IsNullOrEmpty(asm.Culture) ? string.Empty : "." + asm.Culture; if (!StringComparer.OrdinalIgnoreCase.Equals(asm.Name + ".g" + culture + ".resources", name)) return false; var elems = set.ResourceElements.ToArray(); if (elems.Length == 0) return false; foreach (var e in elems) { if (!(e.ResourceData.Code == ResourceTypeCode.ByteArray || e.ResourceData.Code == ResourceTypeCode.Stream)) return false; } return true; }
ResourceElementSet Read() { var resources = new ResourceElementSet(); uint sig = reader.ReadUInt32(); if (sig != 0xBEEFCACE) { throw new ResourceReaderException($"Invalid resource sig: {sig:X8}"); } if (!CheckReaders()) { throw new ResourceReaderException("Invalid resource reader"); } int version = reader.ReadInt32(); if (version != 2) //TODO: Support version 1 { throw new ResourceReaderException($"Invalid resource version: {version}"); } int numResources = reader.ReadInt32(); if (numResources < 0) { throw new ResourceReaderException($"Invalid number of resources: {numResources}"); } int numUserTypes = reader.ReadInt32(); if (numUserTypes < 0) { throw new ResourceReaderException($"Invalid number of user types: {numUserTypes}"); } var userTypes = new List <UserResourceType>(); for (int i = 0; i < numUserTypes; i++) { userTypes.Add(new UserResourceType(reader.ReadSerializedString(), ResourceTypeCode.UserTypes + i)); } reader.Position = (reader.Position + 7) & ~7U; var hashes = new int[numResources]; for (int i = 0; i < numResources; i++) { hashes[i] = reader.ReadInt32(); } var offsets = new int[numResources]; for (int i = 0; i < numResources; i++) { offsets[i] = reader.ReadInt32(); } long baseOffset = reader.Position; long dataBaseOffset = reader.ReadInt32(); long nameBaseOffset = reader.Position; long end = reader.Length; var infos = new List <ResourceInfo>(numResources); for (int i = 0; i < numResources; i++) { reader.Position = (uint)(nameBaseOffset + offsets[i]); var name = reader.ReadSerializedString(Encoding.Unicode); long offset = dataBaseOffset + reader.ReadInt32(); infos.Add(new ResourceInfo(name, offset)); } infos.Sort((a, b) => a.offset.CompareTo(b.offset)); for (int i = 0; i < infos.Count; i++) { var info = infos[i]; var element = new ResourceElement(); element.Name = info.name; reader.Position = (uint)info.offset; long nextDataOffset = i == infos.Count - 1 ? end : infos[i + 1].offset; int size = (int)(nextDataOffset - info.offset); element.ResourceData = ReadResourceData(userTypes, size); element.ResourceData.StartOffset = baseFileOffset + (FileOffset)info.offset; element.ResourceData.EndOffset = baseFileOffset + (FileOffset)reader.Position; resources.Add(element); } return(resources); }
/// <summary> /// Write .NET resources /// </summary> /// <param name="module">Owner module</param> /// <param name="stream">Output stream</param> /// <param name="resources">.NET resources</param> public static void Write(ModuleDef module, Stream stream, ResourceElementSet resources) { new ResourceWriter(module, stream, resources).Write(); }
public ResourceElementSetNode(ITreeNodeGroup treeNodeGroup, ModuleDef module, EmbeddedResource resource) : base(treeNodeGroup, resource) { this.module = module; this.resourceElementSet = ResourceReader.Read(module, resource.Data); }
void RegenerateEmbeddedResource(ModuleDef module) { TreeNode.EnsureChildrenLoaded(); var outStream = new MemoryStream(); var resources = new ResourceElementSet(); foreach (IResourceElementNode child in TreeNode.DataChildren) resources.Add(child.ResourceElement); ResourceWriter.Write(module, outStream, resources); this.Resource = new EmbeddedResource(Resource.Name, outStream.ToArray(), Resource.Attributes); }