ResourceWriter(ModuleDef module, Stream stream, ResourceElementSet resources)
 {
     this.module    = module;
     typeCreator    = new ResourceDataFactory(module);
     writer         = new BinaryWriter(stream);
     this.resources = 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();
예제 #3
0
        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 && 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 =
                    version == 1 ? ReadResourceDataV1(userTypes, size) : ReadResourceDataV2(userTypes, size);
                element.ResourceData.StartOffset = baseFileOffset + (FileOffset)info.offset;
                element.ResourceData.EndOffset   = baseFileOffset + (FileOffset)reader.Position;

                resources.Add(element);
            }

            return(resources);
        }