Esempio n. 1
0
        public static ConstantData FromFileSystemEntry(FileSystemEntry entry)
        {
            using (var stream = entry.Open())
                using (var reader = new BinaryReader(stream))
                {
                    var data = new ConstantData();

                    //validate that this is a correct const
                    var magic = reader.ReadFixedLengthString(17);
                    if (magic != "Apt constant file")
                    {
                        throw new InvalidDataException($"Not a supported const file: {magic}");
                    }

                    reader.BaseStream.Seek(3, SeekOrigin.Current);

                    data.AptDataEntryOffset = reader.ReadUInt32();
                    var numEntries = reader.ReadUInt32();
                    var headerSize = reader.ReadUInt32();

                    if (headerSize != 32)
                    {
                        throw new InvalidDataException("Constant header must be 32 bytes");
                    }

                    for (var i = 0; i < numEntries; i++)
                    {
                        var constEntry = new ConstantEntry
                        {
                            Type = reader.ReadUInt32AsEnum <ConstantEntryType>()
                        };

                        //read the number/ string offset


                        switch (constEntry.Type)
                        {
                        case ConstantEntryType.Undef:
                            throw new InvalidDataException("Undefined const entry");

                        case ConstantEntryType.String:
                            var strOffset = reader.ReadUInt32();
                            var pos       = reader.BaseStream.Position;
                            reader.BaseStream.Seek(strOffset, SeekOrigin.Begin);
                            constEntry.Value = reader.ReadNullTerminatedString();
                            reader.BaseStream.Seek(pos, SeekOrigin.Begin);
                            break;

                        case ConstantEntryType.Register:
                            constEntry.Value = reader.ReadUInt32();
                            break;

                        case ConstantEntryType.Boolean:
                            constEntry.Value = reader.ReadBooleanUInt32Checked();
                            break;

                        case ConstantEntryType.Float:
                            constEntry.Value = reader.ReadSingle();
                            break;

                        case ConstantEntryType.Integer:
                            constEntry.Value = reader.ReadInt32();
                            break;

                        case ConstantEntryType.Lookup:
                            constEntry.Value = reader.ReadUInt32();
                            break;

                        default:
                            throw new InvalidDataException();
                        }

                        data.Entries.Add(constEntry);
                    }

                    return(data);
                }
        }
Esempio n. 2
0
        public static ConstantData FromFileSystemEntry(FileSystemEntry entry)
        {
            using (var stream = entry.Open())
                using (var reader = new BinaryReader(stream))
                {
                    var data = new ConstantData();

                    //validate that this is a correct const
                    var magic = reader.ReadFixedLengthString(17);
                    if (magic != "Apt constant file")
                    {
                        throw new InvalidDataException($"Not a supported const file: {magic}");
                    }

                    reader.BaseStream.Seek(3, SeekOrigin.Current);

                    data.AptDataEntryOffset = reader.ReadUInt32();
                    var numEntries = reader.ReadUInt32();
                    reader.BaseStream.Seek(4, SeekOrigin.Current);

                    for (var i = 0; i < numEntries; i++)
                    {
                        var constEntry = new ConstantEntry
                        {
                            Type = reader.ReadUInt32AsEnum <ConstantEntryType>()
                        };

                        //read the number/ string offset
                        var entryValue = reader.ReadUInt32();

                        switch (constEntry.Type)
                        {
                        case ConstantEntryType.Undef:
                            throw new InvalidDataException("Undefined const entry");

                        case ConstantEntryType.String:
                            var pos = reader.BaseStream.Position;
                            reader.BaseStream.Seek(entryValue, SeekOrigin.Begin);
                            constEntry.Value = reader.ReadNullTerminatedString();
                            reader.BaseStream.Seek(pos, SeekOrigin.Begin);
                            break;

                        case ConstantEntryType.Number:
                            constEntry.Value = entryValue;
                            break;

                        case ConstantEntryType.Unknown1:
                            // TODO
                            break;

                        case ConstantEntryType.Unknown2:
                            // TODO
                            break;

                        case ConstantEntryType.Unknown3:
                            // TODO
                            break;

                        case ConstantEntryType.Unknown4:
                            // TODO
                            break;

                        case ConstantEntryType.Unknown5:
                            // TODO
                            break;

                        default:
                            throw new InvalidDataException();
                        }

                        data.Entries.Add(constEntry);
                    }

                    return(data);
                }
        }