SWFProcessing.SWFModeller.Characters.Shapes.IO.ShapeParser.ReadShapeRecordsInto C# (CSharp) Method

ReadShapeRecordsInto() private method

private ReadShapeRecordsInto ( ShapeDef sws, SWFDataTypeReader shapeReader, int &fillBits, int &lineBits, Tag format ) : void
sws ShapeDef
shapeReader SWFDataTypeReader
fillBits int
lineBits int
format Tag
return void
        private void ReadShapeRecordsInto(ShapeDef sws, SWFDataTypeReader shapeReader, ref int fillBits, ref int lineBits, Tag format)
        {
            List<IShapeRecord> records = new List<IShapeRecord>();

            int currentX = 0;
            int currentY = 0;

            while (true)
            {
                bool isEdgeRecord = shapeReader.ReadBit();
                if (isEdgeRecord)
                {
                    if (shapeReader.ReadBit())
                    {
                        /* StraightEdgeRecord */
                        int bpv = 2 + (int)shapeReader.ReadUBits(4);

                        bool isGeneralLine = shapeReader.ReadBit();
                        bool isVertical = false;
                        if (!isGeneralLine)
                        {
                            isVertical = shapeReader.ReadBit();
                        }

                        int dx = 0;
                        int dy = 0;

                        if (isGeneralLine || !isVertical)
                        {
                            dx = shapeReader.ReadSBits(bpv);
                        }

                        if (isGeneralLine || isVertical)
                        {
                            dy = shapeReader.ReadSBits(bpv);
                        }

                        currentX += dx;
                        currentY += dx;

                        records.Add(new StraightEdge() { DX = dx, DY = dy });
                    }
                    else
                    {
                        /* CurvedEdgeRecord */
                        int bpv = 2 + (int)shapeReader.ReadUBits(4);

                        int ctrlDX = shapeReader.ReadSBits(bpv);
                        int ctrlDY = shapeReader.ReadSBits(bpv);
                        int anchorDX = shapeReader.ReadSBits(bpv);
                        int anchorDY = shapeReader.ReadSBits(bpv);

                        currentX += ctrlDX + anchorDX;
                        currentY += ctrlDY + anchorDY;

                        records.Add(new CurvedEdge() { AnchorDX = anchorDX, AnchorDY = anchorDY, CtrlDX = ctrlDX, CtrlDY = ctrlDY });
                    }
                }
                else
                {
                    uint flags = shapeReader.ReadUBits(5);

                    if (flags == 0)
                    {
                        /* EndShapeRecord */
                        break;
                    }

                    /* StyleChangeRecord */

                    bool stateMoveTo = (flags & 1) == 1;
                    flags >>= 1;
                    bool stateFillStyle0 = (flags & 1) == 1;
                    flags >>= 1;
                    bool stateFillStyle1 = (flags & 1) == 1;
                    flags >>= 1;
                    bool stateLineStyle = (flags & 1) == 1;
                    flags >>= 1;
                    bool stateNewStyles = (flags & 1) == 1;
                    flags >>= 1;

                    StyleChange sc = new StyleChange();

                    if (stateMoveTo)
                    {
                        int moveBits = (int)shapeReader.ReadUBits(5);

                        sc.DX = shapeReader.ReadSBits(moveBits);
                        sc.DY = shapeReader.ReadSBits(moveBits);

                        currentX = sc.DX.Value;
                        currentY = sc.DY.Value;
                    }

                    if (stateFillStyle0)
                    {
                        sc.FillStyle0 = sws.FillFromIndex((int)shapeReader.ReadUBits(fillBits));
                    }

                    if (stateFillStyle1)
                    {
                        sc.FillStyle1 = sws.FillFromIndex((int)shapeReader.ReadUBits(fillBits));
                    }

                    if (stateLineStyle)
                    {
                        sc.LineStyle = (int)shapeReader.ReadUBits(lineBits);
                    }

                    if (stateNewStyles)
                    {
                        sc.NewFillStyles = this.ReadFillStyleArray(shapeReader, format);
                        sc.NewLineStyles = this.ReadLineStyleArray(shapeReader, format);

                        fillBits = (int)shapeReader.ReadUBits(4);
                        lineBits = (int)shapeReader.ReadUBits(4);

                        /* ISSUE 21: We're storing new styles defined in shape records in two places and
                         * in such a way that we can't figure out where we got them from. This makes
                         * it impossible to reconstruct the SWF data. Kinda need to work out how to
                         * find styles. */

                        sws.FillStyles.AddRange(sc.NewFillStyles);
                        sws.LineStyles.AddRange(sc.NewLineStyles);
                    }

                    records.Add(sc);
                }
            }

            shapeReader.Align8();

            sws.Records = records.ToArray();
        }