示例#1
0
        private bool ReadField(ObjectReader objectReader, Dictionary <MidiField, int> fieldValues, MidiField field)
        {
            switch (field)
            {
            case MidiField.End:
                return(false);

            case MidiField.MidiData:
                ushort chunks = 2;
                for (int i = 0; i <= chunks; i++)
                {
                    var header = objectReader.ReadBytes(8);

                    Array.Reverse(header);
                    var dataLength = (int)BitConverter.ToUInt32(header, 0);
                    var data       = objectReader.ReadBytes(dataLength);

                    if (i == 0)
                    {
                        Array.Reverse(data);
                        chunks = BitConverter.ToUInt16(data, 2);
                    }
                }
                return(true);

            default:
                fieldValues[field] = objectReader.ReadInt32();
                return(true);
            }
        }
示例#2
0
        private static void ReadBitArray(ObjectReader objectReader, StringBuilder destination, AnimationField field, List <int> counts, int count)
        {
            var bits = new BitArray(objectReader.ReadBytes((int)Math.Ceiling(count / 8.0)));

            bits.Length = count;
            counts.Add(count);

            destination.AppendLine(field.ToString());
            destination.AppendLine($"    [{bits.Length}]: {string.Join(string.Empty, bits.Cast<bool>().Select(b => b ? "1" : "0"))}");
        }
示例#3
0
        // TODO needed?
        //protected bool HasStructMember(string name)
        //{
        //    return serializedType?.m_Nodes != null && serializedType.m_Nodes.Any(x => x.m_Name == name);
        //}

        //public string Dump()
        //{
        //    reader.Reset();
        //    if (serializedType?.m_Nodes != null)
        //    {
        //        var sb = new StringBuilder();
        //        TypeTreeHelper.ReadTypeString(sb, serializedType.m_Nodes, reader);
        //        return sb.ToString();
        //    }
        //    return null;
        //}

        public byte[] GetRawData()
        {
            reader.Reset();
            return(reader.ReadBytes((int)byteSize));
        }
示例#4
0
        private bool ReadField(ObjectReader objectReader, Dictionary <TextureField, int> fieldValues, TextureField field)
        {
            switch (field)
            {
            case TextureField.End:
                return(false);

            case TextureField.JpegData:
                using (var jpeg = new Bitmap(new MemoryStream(objectReader.ReadBytes(objectReader.ReadInt32()))))
                {
                    m_Width  = jpeg.Width;
                    m_Height = jpeg.Height;

                    var data    = jpeg.LockBits(new Rectangle(0, 0, m_Width, m_Height), ImageLockMode.ReadOnly, jpeg.PixelFormat);
                    var strides = new byte[data.Stride * jpeg.Height];
                    Marshal.Copy(data.Scan0, strides, 0, strides.Length);
                    jpeg.UnlockBits(data);

                    var rgb   = new byte[m_Width * m_Height * 4];
                    var index = 0;
                    for (int row = 0; row < m_Height; row++)
                    {
                        for (int column = 0; column < m_Width; column++)
                        {
                            var offset = (m_Height - row - 1) * data.Stride;

                            switch (jpeg.PixelFormat)
                            {
                            case PixelFormat.Format24bppRgb:
                                offset      += column * 3;
                                rgb[index++] = strides[offset++];
                                rgb[index++] = strides[offset++];
                                rgb[index++] = strides[offset];
                                rgb[index++] = 255;
                                break;

                            case PixelFormat.Format8bppIndexed:
                                var colour = jpeg.Palette.Entries[strides[offset + column]];
                                rgb[index++] = colour.B;
                                rgb[index++] = colour.G;
                                rgb[index++] = colour.R;
                                rgb[index++] = colour.A;
                                break;

                            default:
                                throw new NotImplementedException();
                            }
                        }
                    }

                    _pixels    = new MemoryStream(rgb);
                    image_data = new ResourceReader(new BinaryReader(_pixels), 0, rgb.Length);
                }
                return(true);

            case TextureField.BmpColourTable:
                _colours = objectReader.ReadUInt32Array(fieldValues[TextureField.Colours]);
                return(true);

            case TextureField.Tranparent:
                _transparent = objectReader.ReadUInt32() | 0xff000000;
                return(true);

            case TextureField.BmpPixelData:
                using (var memoryWriter = new BinaryWriter(_pixels = new MemoryStream(), Encoding.Default, true))
                {
                    m_Width  = fieldValues[TextureField.Width];
                    m_Height = fieldValues[TextureField.Height];

                    var bitsPerPixel = fieldValues[TextureField.BitsPerPixel];
                    var rowBytes     = (int)Math.Ceiling((m_Width * bitsPerPixel) / 8.0) % 4;

                    for (int row = 0; row < m_Height; row++)
                    {
                        for (int column = 0; column < m_Width; column++)
                        {
                            switch (bitsPerPixel)
                            {
                            case 4:
                                var b = objectReader.ReadByte();
                                WriteColour(memoryWriter, _colours[b >> 4]);
                                if (++column < m_Width)
                                {
                                    WriteColour(memoryWriter, _colours[b & 0x0f]);
                                }
                                break;

                            case 8:
                                WriteColour(memoryWriter, _colours[objectReader.ReadByte()]);
                                break;

                            case 24:
                                WriteColour(memoryWriter, objectReader.ReadUInt32());
                                objectReader.Position--;
                                break;

                            default:
                                throw new NotImplementedException();
                            }
                        }

                        // Align rows to multiples of 32 bits
                        if (rowBytes != 0)
                        {
                            objectReader.ReadBytes(4 - rowBytes);
                        }
                    }

                    image_data = new ResourceReader(new BinaryReader(_pixels), 0, (int)_pixels.Length);
                }
                return(true);

            case TextureField.BitsPerPixel:
            case TextureField.UnknownShort31:
                fieldValues.Add(field, objectReader.ReadUInt16());
                InfoText += $"{field}: {fieldValues[field]}\n";
                return(true);

            case TextureField.UnknownByte22:
            case TextureField.UnknownByte27:
            case TextureField.UnknownByte128:
            case TextureField.UnknownByte129:
                fieldValues.Add(field, objectReader.ReadByte());
                InfoText += $"{field}: {fieldValues[field]}\n";
                return(true);

            case TextureField.UnknownArray99:
                fieldValues.Add(field, objectReader.ReadInt32());
                InfoText += $"{field}: {fieldValues[field]} [{string.Join(", ", Enumerable.Range(0, 12).Select(i => objectReader.ReadByte()))}]\n";
                return(true);

            case TextureField.AlphaMap:
                fieldValues.Add(field, objectReader.ReadInt32());
                var alphaMap = new BitArray(objectReader.ReadBytes(fieldValues[field]));
                var stride   = alphaMap.Length / m_Height;
                for (int row = 0; row < m_Height; row++)
                {
                    for (int column = 0; column < m_Width; column++)
                    {
                        var endianColumn = 8 * (column / 8);
                        endianColumn += 7 - (column % 8);

                        if (!alphaMap[(row * stride) + endianColumn])
                        {
                            _pixels.Position = ((row * m_Width) + column) * 4 + 3;
                            _pixels.WriteByte(0);
                        }
                    }
                }
                return(true);

            default:
                fieldValues.Add(field, objectReader.ReadInt32());
                InfoText += $"{field}: {fieldValues[field]}\n";
                return(true);
            }
        }
示例#5
0
        private bool ReadField(ObjectReader objectReader, Dictionary <ModelField, int> fieldValues, ModelField field)
        {
            switch (field)
            {
            case ModelField.End:
                m_SubMeshes = _subMeshes.ToArray();
                if (_faceGroups == null)
                {
                    m_SubMeshes.Single().indices = _indices;
                }
                else
                {
                    for (int i = 0; i < _faceGroups.Length; i++)
                    {
                        foreach (var j in _indicesByFace[i])
                        {
                            var subMeshIndices = m_SubMeshes[_faceGroups[i]].indices;
                            subMeshIndices.Add(_indices[j + 0]);
                            subMeshIndices.Add(_indices[j + 1]);
                            subMeshIndices.Add(_indices[j + 2]);
                        }
                    }
                }
                return(false);

            case ModelField.UnknownMarker19:
                return(true);

            case ModelField.UnknownShort150:
                _subMesh.Properties.Add(field, objectReader.ReadInt16());
                return(true);

            case ModelField.UnknownArray22:
                var unknown22 = objectReader.ReadBytes((int)_subMesh.Properties[ModelField.VertexCount]);
                _subMesh.Properties.Add(field, unknown22.Count());
                return(true);

            case ModelField.Vertices:
                m_VertexCount = (int)_subMesh.Properties[ModelField.VertexCount];
                m_Vertices    = objectReader.ReadSingleArray(3 * m_VertexCount);
                return(true);

            case ModelField.FacesSingle:
                _subMesh.Properties[field] = objectReader.ReadByte();
                var indices = objectReader.ReadBytes((int)_subMesh.Properties[ModelField.IndexCount]);
                UnpackFaces(indices.Select(b => (ushort)b).ToArray());
                return(true);

            case ModelField.FacesDouble:
                UnpackFaces(Enumerable.Range(0, (int)_subMesh.Properties[ModelField.IndexCount])
                            .Select(i => objectReader.ReadUInt16()).ToArray());
                return(true);

            case ModelField.FaceGroups34:
                if ((int)_subMesh.Properties[ModelField.IndexCount] != 0)
                {
                    _faceGroups = Enumerable.Range(0, (int)_subMesh.Properties[ModelField.FaceCount])
                                  .Select(i => objectReader.ReadInt16())
                                  .ToArray();
                }
                return(true);

            case ModelField.FaceGroups36:
                var bitsPerItem = objectReader.ReadByte();
                var byteCount   = (int)_subMesh.Properties[ModelField.FaceCount] * bitsPerItem / 32.0;
                var faceGroups  = objectReader.ReadBytes((int)Math.Ceiling(byteCount));
                if (bitsPerItem == 16)
                {
                    faceGroups = faceGroups.SelectMany(b => new[]
                    {
                        (byte)((b & 0xF0) >> 4),
                        (byte)(b & 0x0F)
                    })
                                 .ToArray();
                }

                if (byteCount != Math.Ceiling(byteCount))
                {
                    faceGroups = faceGroups.Take(faceGroups.Length - 1).ToArray();
                }
                _faceGroups = faceGroups.Select(b => (short)b).ToArray();
                return(true);

            case ModelField.UnknownArray113:
                _subMesh.Properties.Add(field, objectReader.ReadBytes(12));
                return(true);

            case ModelField.UnknownArray115:
                _subMesh.Properties.Add(field, Enumerable.Range(0, 6)
                                        .Select(i => objectReader.ReadUInt32())
                                        .Where(i => i != uint.MaxValue)
                                        .ToArray());
                return(true);

            case ModelField.MaterialAmbient:
                _subMesh.color = Enumerable.Range(0, 4).Select(i => objectReader.ReadSingle()).ToArray();
                return(true);

            case ModelField.MaterialPower:
            case ModelField.MaterialEmissive:
            case ModelField.MaterialSpecular:
                _subMesh.Properties.Add(field, Enumerable.Range(0, 4)
                                        .Select(i => objectReader.ReadSingle())
                                        .ToArray());
                return(true);

            case ModelField.UnknownShort149:
            case ModelField.UnknownShort151:
                _subMesh.Properties.Add(field, objectReader.ReadInt16());
                return(true);

            case ModelField.FaceCount:
            case ModelField.IndexCount:
            case ModelField.VertexCount:
            case ModelField.UnknownInt21:
            case ModelField.UnknownInt23:
            case ModelField.UnknownInt112:
            case ModelField.UnknownInt114:
            case ModelField.Texture:
            case ModelField.UnknownInt129:
            case ModelField.UnknownInt130:
            case ModelField.TextureGroup:
            case ModelField.UnknownInt152:
            case ModelField.UnknownInt154:
            case ModelField.UnknownInt176:
            case ModelField.UnknownInt178:
            case ModelField.UnknownInt192:
            case ModelField.UnknownInt193:
            case ModelField.UnknownInt194:
            case ModelField.UnknownInt195:
            case ModelField.UnknownInt198:
            case ModelField.UnknownInt199:
            case ModelField.UnknownInt200:
            case ModelField.UnknownInt201:
            case ModelField.UnknownInt202:
                if (_subMesh == null || _subMesh.Properties.ContainsKey(field))
                {
                    _subMeshes.Add(_subMesh = new MxrSubMesh());
                }
                _subMesh.Properties.Add(field, objectReader.ReadInt32());
                return(true);

            case ModelField.UnknownFloat148:
            case ModelField.TextureDivisionU:
            case ModelField.TextureDivisionV:
            case ModelField.TexturePositionU:
            case ModelField.TexturePositionV:
            case ModelField.TextureRotateX:
            case ModelField.TextureRotateY:
            case ModelField.TextureRotateZ:
            case ModelField.UnknownFloat196:
            case ModelField.UnknownFloat197:
                _subMesh.Properties.Add(field, objectReader.ReadSingle());
                return(true);

            case ModelField.GroupName:
                _subMesh.Properties.Add(field, _subMeshes.Count + " " + MxrObjectReader.ReadString(objectReader));
                return(true);

            default:
                throw new NotImplementedException();
            }
        }
示例#6
0
        private bool ReadField(ObjectReader objectReader, Dictionary <TableField, int> fieldValues, TableField field)
        {
            var address = 0;
            var value   = field.ToString() + " = ";

            switch (field)
            {
            case TableField.End:
                return(false);

            case TableField.Unknown128:
                var header     = objectReader.ReadBytes(2);
                var dataLength = 0;

                switch (header[0])
                {
                case 3:
                    if (header[1] == 32)
                    {
                        header     = header.Concat(objectReader.ReadBytes(12)).ToArray();
                        dataLength = header[6] * 4;
                    }
                    else
                    {
                        dataLength = 4;
                    }
                    break;

                case 5:
                    dataLength = header[1] == 32 ? 24 : 8;
                    break;

                case 8:
                    dataLength = 16;
                    break;

                case 11:
                    if (header[1] == 32)
                    {
                        header     = header.Concat(objectReader.ReadBytes(9)).ToArray();
                        dataLength = header[6];
                    }
                    else
                    {
                        dataLength = 1;
                    }
                    break;
                }

                value += string.Join(", ", header.Concat(objectReader.ReadBytes(dataLength)));
                break;

            case TableField.Unknown129:
                value += string.Join(", ", objectReader.ReadBytes(10));
                break;

            case TableField.Unknown130:
                address = objectReader.ReadInt32();
                value  += $"{address.ToString("X8")}, {objectReader.ReadInt32()}, {objectReader.ReadInt32()}, {objectReader.ReadInt32()}";
                break;

            case TableField.Unknown131:
            case TableField.Unknown132:
                address = objectReader.ReadInt32();
                value  += $"{address.ToString("X8")}, {objectReader.ReadInt32()}, {objectReader.ReadInt32()}";
                break;

            default:
                throw new NotImplementedException();
            }

            var entry = MxrObjectReader.ReadString(objectReader);

            if (address != 0)
            {
                Strings.Add(address, entry);
            }

            InfoText += $"{value}, {entry}\n";

            if (objectReader.ReadUInt16() != 0)
            {
                objectReader.BaseStream.Position--;
            }

            return(true);
        }
示例#7
0
        private bool ReadField(ObjectReader objectReader, Dictionary <WaveField, int> fieldValues, WaveField field)
        {
            switch (field)
            {
            case WaveField.End:
                return(false);

            case WaveField.RiffData:
                var unknown1       = objectReader.ReadBytes(6);
                var format         = objectReader.ReadUInt16();
                var channels       = objectReader.ReadUInt16();
                var samplesPerSec  = objectReader.ReadUInt32();
                var avgBytesPerSec = objectReader.ReadUInt32();
                var blockAlign     = objectReader.ReadUInt16();
                var bitsPerSample  = objectReader.ReadUInt16();
                var extraSize      = objectReader.ReadUInt16();

                var unknown2   = objectReader.ReadBytes(6);
                var fmtLength  = format == WAVE_FORMAT_PCM ? 16 : (18 + extraSize);
                var dataLength = (int)objectReader.ReadUInt32() - extraSize;

                var audioData = new MemoryStream();
                using (var destination = new BinaryWriter(audioData, Encoding.ASCII, true))
                {
                    destination.Write(Encoding.ASCII.GetBytes("RIFF"));
                    destination.Write(dataLength + 20 + fmtLength);
                    destination.Write(Encoding.ASCII.GetBytes("WAVEfmt "));
                    destination.Write(fmtLength);
                    destination.Write(format);
                    destination.Write(channels);
                    destination.Write(samplesPerSec);
                    destination.Write(avgBytesPerSec);
                    destination.Write(blockAlign);
                    destination.Write(bitsPerSample);

                    if (format == WAVE_FORMAT_DVI_ADPCM)
                    {
                        // See https://icculus.org/SDL_sound/downloads/external_documentation/wavecomp.htm
                        var samplesPerBlock = objectReader.ReadUInt16();
                        destination.Write(extraSize);
                        destination.Write(samplesPerBlock);
                        destination.Write(Encoding.ASCII.GetBytes("fact"));
                        destination.Write(4);
                        destination.Write(samplesPerBlock * dataLength / blockAlign);
                    }

                    destination.Write(Encoding.ASCII.GetBytes("data"));
                    destination.Write(dataLength);
                    destination.Write(objectReader.ReadBytes(dataLength));
                }

                m_Type      = AudioType.WAV;
                m_Size      = audioData.Length;
                m_AudioData = new ResourceReader(new BinaryReader(audioData), 0, (int)audioData.Length);
                return(true);

            case WaveField.UnknownByte:
                fieldValues.Add(field, objectReader.ReadByte());
                return(true);

            default:
                fieldValues.Add(field, objectReader.ReadInt32());
                return(true);
            }
        }
示例#8
0
 public void Unpack(ObjectReader reader)
 {
     rect = new RectangleR2(reader.ReadDouble(), reader.ReadDouble(), reader.ReadDouble(), reader.ReadDouble());
     body = reader.ReadBytes(reader.ReadInt32());
 }