private ResourceWriter(ModuleDef module, Stream stream, ResourceElementSet resources) { this.module = module; this.typeCreator = new ResourceDataCreator(module); this.writer = new BinaryWriter(stream); this.resources = resources; }
private 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); element.ResourceData.StartOffset = this.baseFileOffset + (FileOffset)info.offset; element.ResourceData.EndOffset = this.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(); }