Example #1
0
        // Ok so this is how this works.
        // It starts out by walking the file backwards to find the ending offset.
        // It then sets up the original path and starts scanning along the tiles.
        // When it finds an unknown tile type the program duplicates the current
        // path and makes one follow the path of it being non-important while
        // the other follows the path of being important.
        // When a path turns out not to fit the data it gets removed from the list.
        // All paths get checked in parallel.
        public TileImportance[] ScanWorld(String world, BackgroundWorker worker = null)
        {
            Timer t = null;
            TileImportance[] retList;

            if (worker != null)
            {
                bw = worker;
                t = new Timer(333);
                t.Elapsed += new ElapsedEventHandler(timer_ScanWorld);
                t.Enabled = true;
                t.Start();
            }

            stream = new FileStream(world, FileMode.Open, FileAccess.Read);
            reader = new BinaryReader(stream);
            buffReader = new BufferedBinaryReader(stream, 5000, 1000);

            posChests = new BackwardsScanner(stream, header).SeekToChestsBackwards();

            stream.Seek(0, SeekOrigin.Begin);
            ReadHeader();
            retList = ScanWorldTiles();

            if (bw != null)
            {
                t.Stop();
                bw = null;
            }

            reader.Close();

            return retList;
        }
Example #2
0
        private protected override IEnumerable <float> DoEnumerateDataValues(BufferedBinaryReader reader, DataSection dataSection, long dataPointsNumber)
        {
            var mvm = MissingValueManagement;

            var pmv = PrimaryMissingValue;

            var ng = (int)NumberOfGroups;

            // 6-xx  Get reference values for groups (X1's)
            var x1     = new int[ng];
            var nbBits = NumberOfBits;

            for (var i = 0; i < ng; i++)
            {
                x1[i] = reader.ReadUIntN(nbBits);
            }

            // [xx + 1 ]-yy Get number of bits used to encode each group
            var nb = new int[ng];

            nbBits = BitsGroupWidths;

            reader.NextUIntN();
            for (var i = 0; i < ng; i++)
            {
                nb[i] = reader.ReadUIntN(nbBits);
            }

            // [yy +1 ]-zz Get the scaled group lengths using formula
            //     Ln = ref + Kn * len_inc, where n = 1-NG,
            //          ref = referenceGroupLength, and  len_inc = lengthIncrement

            var l        = new int[ng];
            var rgLength = (int)ReferenceGroupLength;

            var lenInc = LengthIncrement;

            nbBits = BitsScaledGroupLength;

            reader.NextUIntN();
            for (var i = 0; i < ng; i++)
            {
                // NG
                l[i] = rgLength + reader.ReadUIntN(nbBits) * lenInc;
            }

            // [zz +1 ]-nn get X2 values and calculate the results Y using formula
            //                Y * 10**D = R + (X1 + X2) * 2**E

            var d = DecimalScaleFactor;

            var dd = Math.Pow(10, d);

            var r = ReferenceValue;

            var e = BinaryScaleFactor;

            var ee = Math.Pow(2.0, e);

            // used to check missing values when X2 is packed with all 1's
            var bitsmv1 = new int[31];

            for (var i = 0; i < 31; i++)
            {
                bitsmv1[i] = (int)Math.Pow(2, i) - 1;
            }

            int x2;

            reader.NextUIntN();
            for (var i = 0; i < ng - 1; i++)
            {
                for (var j = 0; j < l[i]; j++)
                {
                    if (nb[i] == 0)
                    {
                        if (mvm == 0)
                        {
                            // X2 = 0
                            yield return((float)((r + x1[i] * ee) / dd));
                        }
                        else if (mvm == ComplexPackingMissingValueManagement.Primary)
                        {
                            yield return(pmv);
                        }
                    }
                    else
                    {
                        x2 = reader.ReadUIntN(nb[i]);
                        if (mvm == 0)
                        {
                            yield return((float)((r + (x1[i] + x2) * ee) / dd));
                        }
                        else if (mvm == ComplexPackingMissingValueManagement.Primary)
                        {
                            // X2 is also set to missing value is all bits set to 1's
                            if (x2 == bitsmv1[nb[i]])
                            {
                                yield return(pmv);
                            }
                            else
                            {
                                yield return((float)((r + (x1[i] + x2) * ee) / dd));
                            }
                        }
                    }
                }         // end for j
            }             // end for i

            // process last group
            var last = (int)LastGroupLength;

            for (var j = 0; j < last; j++)
            {
                // last group
                if (nb[ng - 1] == 0)
                {
                    if (mvm == 0)
                    {
                        // X2 = 0
                        yield return((float)((r + x1[ng - 1] * ee) / dd));
                    }
                    else if (mvm == ComplexPackingMissingValueManagement.Primary)
                    {
                        yield return(pmv);
                    }
                }
                else
                {
                    x2 = reader.ReadUIntN(nb[ng - 1]);
                    if (mvm == 0)
                    {
                        yield return((float)((r + (x1[ng - 1] + x2) * ee) / dd));
                    }
                    else if (mvm == ComplexPackingMissingValueManagement.Primary)
                    {
                        // X2 is also set to missing value is all bits set to 1's
                        if (x2 == bitsmv1[nb[ng - 1]])
                        {
                            yield return(pmv);
                        }
                        else
                        {
                            yield return((float)((r + (x1[ng - 1] + x2) * ee) / dd));
                        }
                    }
                }
            }             // end for j
        }
Example #3
0
        public void Clear()
        {
            header = new WorldHeader();
            tiles = null;
            chests = null;
            signs = null;
            footer = null;

            posChests = 0;
            posEnd = 0;
            posFooter = 0;
            posNpcs = 0;
            posSigns = 0;
            posTiles = 0;

            MaxX = 0;
            MaxY = 0;
            progress = 0;

            if (reader != null)
            {
                reader.Close();
                reader = null;
            }

            if (stream != null)
                stream = null;

            if (buffReader != null)
                buffReader = null;

            if (bw != null)
                bw = null;
        }
Example #4
0
        private static Instruction ReadInstruction(IMethod method, IMethodContext context, BufferedBinaryReader reader, long startPosition)
        {
            var instr = new Instruction
            {
                Offset = (int)(reader.Position - startPosition),
                OpCode = OpCodes.Nop
            };

            byte   op = reader.ReadUInt8();
            OpCode?opCode;

            if (op != CIL.MultiBytePrefix)
            {
                opCode = CIL.GetShortOpCode(op);
            }
            else
            {
                op     = reader.ReadUInt8();
                opCode = CIL.GetLongOpCode(op);
            }

            if (!opCode.HasValue)
            {
                throw new BadImageFormatException(string.Format("The format of instruction with code {0} is invalid", op));
            }

            instr.OpCode = opCode.Value;

            //Read operand
            switch (instr.OpCode.OperandType)
            {
            case OperandType.InlineI:
                instr.Value = reader.ReadInt32();
                break;

            case OperandType.ShortInlineI:
                instr.Value = (int)reader.ReadSByte();
                break;

            case OperandType.InlineI8:
                instr.Value = reader.ReadInt64();
                break;

            case OperandType.InlineR:
                instr.Value = reader.ReadDouble();
                break;

            case OperandType.ShortInlineR:
                instr.Value = reader.ReadSingle();
                break;

            case OperandType.InlineBrTarget:
            {
                int offset = reader.ReadInt32();
                instr.Value = (int)(offset + reader.Position - startPosition);
            }
            break;

            case OperandType.ShortInlineBrTarget:
            {
                int offset = reader.ReadSByte();
                instr.Value = (int)(offset + reader.Position - startPosition);
            }
            break;

            case OperandType.InlineSwitch:
            {
                int casesCount     = reader.ReadInt32();
                var switchBranches = new int[casesCount];
                for (int k = 0; k < casesCount; k++)
                {
                    switchBranches[k] = reader.ReadInt32();
                }

                int shift = (int)(reader.Position - startPosition);
                for (int k = 0; k < casesCount; k++)
                {
                    switchBranches[k] += shift;
                }

                instr.Value = switchBranches;
            }
            break;

            case OperandType.InlineVar:
                instr.Value = (int)reader.ReadUInt16();
                break;

            case OperandType.ShortInlineVar:
                instr.Value = reader.ReadByte();
                break;

            case OperandType.InlineString:
            {
                int token = reader.ReadInt32();
                instr.Value = context.ResolveMetadataToken(method, token);
            }
            break;

            case OperandType.InlineField:
            case OperandType.InlineMethod:
            case OperandType.InlineSig:
            case OperandType.InlineTok:
            case OperandType.InlineType:
            {
                int token = reader.ReadInt32();
                instr.MetadataToken = token;

                object val = context.ResolveMetadataToken(method, token);
                if (val is ITypeMember)
                {
                }

                if (val == null)
                {
#if DEBUG
                    if (DebugHooks.BreakInvalidMetadataToken)
                    {
                        Debugger.Break();
                        val = context.ResolveMetadataToken(method, token);
                    }
#endif
                    throw new BadTokenException(token);
                }

                instr.Value = val;
            }
            break;

            case OperandType.InlineNone:
                // no operand
                break;

            case OperandType.InlinePhi:
                throw new BadImageFormatException(@"Obsolete. The InlinePhi operand is reserved and should not be used!");
            }

            return(instr);
        }
Example #5
0
        public MethodBody(IMethod method, IMethodContext context, BufferedBinaryReader reader)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            _method = method;

            int lsb   = reader.ReadUInt8();
            var flags = (MethodBodyFlags)lsb;

            _maxStackSize = 8;

            var             format    = flags & MethodBodyFlags.FormatMask;
            List <SEHBlock> sehBlocks = null;

            switch (format)
            {
            case MethodBodyFlags.FatFormat:
            {
                byte msb = reader.ReadUInt8();
                int  dwordMultipleSize = (msb & 0xF0) >> 4;
                Debug.Assert(dwordMultipleSize == 3);         // the fat header is 3 dwords

                _maxStackSize = reader.ReadUInt16();

                int codeSize = reader.ReadInt32();
                int localSig = reader.ReadInt32();

                flags = (MethodBodyFlags)((msb & 0x0F) << 8 | lsb);

                _code = ReadCode(method, context, reader, codeSize);

                if ((flags & MethodBodyFlags.MoreSects) != 0)
                {
                    sehBlocks = ReadSehBlocks(reader);
                }

                bool hasGenericVars;
                _vars = context.ResolveLocalVariables(method, localSig, out hasGenericVars);

                if (hasGenericVars)
                {
                    _genericFlags |= GenericFlags.HasGenericVars;
                }
            }
            break;

            case MethodBodyFlags.TinyFormat:
            case MethodBodyFlags.TinyFormat1:
            {
                int codeSize = (lsb >> 2);
                _code = ReadCode(method, context, reader, codeSize);
            }
            break;

            default:
                throw new NotSupportedException("Not supported method body format!");
            }

            TranslateOffsets();

            if (sehBlocks != null)
            {
                _protectedBlocks = TranslateSehBlocks(method, context, sehBlocks, _code);
            }

            context.LinkDebugInfo(this);
        }
Example #6
0
        /// <summary>
        /// Przepisuje dane do plików odpowiadających za poszczególne tabele.
        /// </summary>
        /// <param name="filename">Plik z danymi.</param>
        private bool rewriteData(String filename)
        {
            tic("Rewriting data:");

            bool result = true;

            BinaryWriter session   = null;
            BinaryWriter query     = null;
            BinaryWriter queryTerm = null;
            BinaryWriter queryUrl  = null;
            BinaryWriter click     = null;
            BinaryWriter urlFile   = null;

            session   = new BinaryWriter(new FileStream(workDir + sessionTableName, FileMode.CreateNew));
            query     = new BinaryWriter(new FileStream(workDir + queryTableName, FileMode.CreateNew));
            queryTerm = new BinaryWriter(new FileStream(workDir + queryTermTableName, FileMode.CreateNew));
            queryUrl  = new BinaryWriter(new FileStream(workDir + queryUrlTableName, FileMode.CreateNew));
            click     = new BinaryWriter(new FileStream(workDir + clickTableName, FileMode.CreateNew));
            urlFile   = new BinaryWriter(new FileStream(workDir + urlTableName, FileMode.CreateNew));

            BinaryWriter[] writers = new BinaryWriter[] { session, query, queryTerm, queryUrl, click };

            {
                bool any = false;
                foreach (var writer in writers)
                {
                    any = any || (writer != null);
                }

                if (!any)
                {
                    toc();
                    return(true);
                }
            }

            using (BufferedBinaryReader reader = new BufferedBinaryReader(filename))
            {
                int lineCounter = 0;
                int q_idCount   = 0;
                int queryUrl_id = 0;

                /// rzutuje parę (url, serpid) na (result_id, q_id)
                Dictionary <Tuple <int, int>, int> urlInfo = new Dictionary <Tuple <int, int>, int>();
                int           lastTime     = 0;
                HashSet <int> urlsAdded    = new HashSet <int>();
                HashSet <int> queriesAdded = new HashSet <int>();

                while (reader.PeekChar() > -1)
                {
                    lineCounter++;

                    if (LINES_LIMIT > 0 && LINES_LIMIT < lineCounter)
                    {
                        break;
                    }

                    byte type = reader.ReadByte();
                    if (type < 0 || type > 3)
                    {
                        Console.WriteLine("Incorrect file in line " + lineCounter);
                        result = false;
                        break;
                    }

                    int sessionId = reader.ReadInt32();

                    switch (type)
                    {
                    case 0:
                    {
                        int day  = reader.ReadInt32();
                        int user = reader.ReadInt32();

                        session.Write((int)sessionId);
                        session.Write((int)day);
                        session.Write((int)user);

                        urlInfo.Clear();
                        lastTime = 0;
                        break;
                    }

                    case 1:
                    case 2:
                    {
                        // TIME
                        int time = reader.ReadInt32();
                        // SERPID
                        int serpid = reader.ReadInt32();
                        // QUERYID
                        int queryId = reader.ReadInt32();

                        int q_id = q_idCount++;

                        query.Write((int)q_id);
                        query.Write((int)queryId);
                        query.Write((int)sessionId);
                        query.Write((int)serpid);
                        query.Write((int)(time - lastTime));
                        lastTime = time;
                        query.Write((bool)(type == 2));

                        bool processQuery = !queriesAdded.Contains(queryId);
                        if (processQuery)
                        {
                            queriesAdded.Add(queryId);
                        }

                        for (int i = reader.ReadInt32(); i > 0; i--)
                        {
                            int term = reader.ReadInt32();

                            if (processQuery)
                            {
                                queryTerm.Write((int)term);
                                queryTerm.Write((int)queryId);
                            }
                        }

                        for (int i = reader.ReadInt32(); i > 0; i--)
                        {
                            int url    = reader.ReadInt32();
                            int domain = reader.ReadInt32();

                            Tuple <int, int> tuple = new Tuple <int, int>(url, serpid);
                            if (!urlInfo.ContainsKey(tuple))
                            {
                                urlInfo.Add(tuple, q_id);
                            }

                            if (processQuery)
                            {
                                queryUrl.Write((int)queryUrl_id);
                                queryUrl_id++;

                                queryUrl.Write((int)url);
                                queryUrl.Write((int)queryId);

                                if (!urlsAdded.Contains(url))
                                {
                                    urlFile.Write((int)url);
                                    urlFile.Write((int)domain);
                                    urlsAdded.Add(url);
                                }
                            }
                        }
                        break;
                    }

                    case 3:
                    {
                        // TIME
                        int time = reader.ReadInt32();
                        // SERPID
                        int serpid = reader.ReadInt32();
                        // URL
                        int url = reader.ReadInt32();

                        int q_id = urlInfo[new Tuple <int, int>(url, serpid)];

                        click.Write((int)url);
                        click.Write((int)q_id);
                        click.Write((int)(time - lastTime));
                        lastTime = time;

                        break;
                    }
                    }
                }
            }

            foreach (BinaryWriter writer in writers)
            {
                if (writer != null)
                {
                    writer.Dispose();
                }
            }

            toc();

            return(result);
        }
Example #7
0
        /// <summary>
        /// Wrzuca dane do bazy danych.
        /// </summary>
        private bool importData()
        {
            tic("Importing data:", true, true);

            const int FLUSH_ROWS = 200000;

            String[] toImport = new String[]
            {
                sessionTableName, queryTableName, queryTermTableName, queryUrlTableName, clickTableName, urlTableName
            };

            foreach (String tableName in toImport)
            {
                tic(tableName, false, true);

                int[] types = getTableTypes(tableName);

                NpgsqlCommand        cmd        = new NpgsqlCommand(buildInsertCommand(tableName), connection);
                NpgsqlCopySerializer serializer = new NpgsqlCopySerializer(connection);
                NpgsqlCopyIn         copyIn     = new NpgsqlCopyIn(cmd, connection, serializer.ToStream);

                copyIn.Start();

                using (BufferedBinaryReader reader = new BufferedBinaryReader(workDir + tableName))
                {
                    int lineCounter = 0;

                    while (reader.PeekChar() > -1)
                    {
                        lineCounter++;

                        for (int i = 0; i < types.Length; i++)
                        {
                            if (types[i] == 0)
                            {
                                int value = reader.ReadInt32();
                                serializer.AddInt32(value);
                            }
                            if (types[i] == 1)
                            {
                                bool value = reader.ReadBool();
                                serializer.AddBool(value);
                            }
                        }

                        serializer.EndRow();

                        if ((lineCounter + 1) % FLUSH_ROWS == 0)
                        {
                            serializer.Flush();
                        }
                    }

                    Console.Write(String.Format("{0,-15}", String.Format("({0})", lineCounter)));
                }

                serializer.Flush();
                serializer.Close();
                copyIn.End();

                toc();
            }

            toc(true);

            return(true);
        }
Example #8
0
 /// <summary>
 /// Reads the data.
 /// </summary>
 /// <param name="binaryReader">Binary reader.</param>
 public void ReadData(BufferedBinaryReader binaryReader)
 {
     this.pos44      = binaryReader.ReadUInt32();
     this.leftLevel  = binaryReader.ReadUInt16();
     this.rightLevel = binaryReader.ReadUInt16();
 }
Example #9
0
        protected override Boolean read(BinaryReader source, MetaDataIO.ReadTagParams readTagParams)
        {
            var result = true;

            UInt16 nbOrders      = 0;
            UInt16 nbPatterns    = 0;
            UInt16 nbInstruments = 0;

            UInt16 flags;
            UInt16 trackerVersion;

            var comment = new StringBuilder("");

            IList <UInt16> patternPointers    = new List <UInt16>();
            IList <UInt16> instrumentPointers = new List <UInt16>();

            resetData();
            var bSource = new BufferedBinaryReader(source.BaseStream);

            // Title = first 28 chars
            var title = StreamUtils.ReadNullTerminatedStringFixed(bSource, System.Text.Encoding.ASCII, 28);

            if (readTagParams.PrepareForWriting)
            {
                structureHelper.AddZone(0, 28, new Byte[28] {
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                }, ZONE_TITLE);
            }
            tagData.IntegrateValue(TagData.TAG_FIELD_TITLE, title.Trim());
            bSource.Seek(4, SeekOrigin.Current);

            nbOrders      = bSource.ReadUInt16();
            nbInstruments = bSource.ReadUInt16();
            nbPatterns    = bSource.ReadUInt16();

            flags          = bSource.ReadUInt16();
            trackerVersion = bSource.ReadUInt16();

            trackerName = getTrackerName(trackerVersion);

            bSource.Seek(2, SeekOrigin.Current); // sampleType (16b)
            if (!S3M_SIGNATURE.Equals(Utils.Latin1Encoding.GetString(bSource.ReadBytes(4))))
            {
                result = false;
                throw new Exception("Invalid S3M file (file signature mismatch)");
            }
            bSource.Seek(1, SeekOrigin.Current); // globalVolume (8b)

            tagExists = true;

            initialSpeed = bSource.ReadByte();
            initialTempo = bSource.ReadByte();

            bSource.Seek(1, SeekOrigin.Current); // masterVolume (8b)
            bSource.Seek(1, SeekOrigin.Current); // ultraClickRemoval (8b)
            bSource.Seek(1, SeekOrigin.Current); // defaultPan (8b)
            bSource.Seek(8, SeekOrigin.Current); // defaultPan (64b)
            bSource.Seek(2, SeekOrigin.Current); // ptrSpecial (16b)

            // Channel table
            for (var i = 0; i < 32; i++)
            {
                FChannelTable.Add(bSource.ReadByte());
                if (FChannelTable[FChannelTable.Count - 1] < 30)
                {
                    nbChannels++;
                }
            }

            // Pattern table
            for (var i = 0; i < nbOrders; i++)
            {
                FPatternTable.Add(bSource.ReadByte());
            }

            // Instruments pointers
            for (var i = 0; i < nbInstruments; i++)
            {
                instrumentPointers.Add(bSource.ReadUInt16());
            }

            // Patterns pointers
            for (var i = 0; i < nbPatterns; i++)
            {
                patternPointers.Add(bSource.ReadUInt16());
            }

            readInstruments(bSource, instrumentPointers);
            readPatterns(bSource, patternPointers);


            // == Computing track properties

            duration = calculateDuration() * 1000.0;

            foreach (var i in FInstruments)
            {
                var displayName = i.DisplayName.Trim();
                if (displayName.Length > 0)
                {
                    comment.Append(displayName).Append(Settings.InternalValueSeparator);
                }
            }
            if (comment.Length > 0)
            {
                comment.Remove(comment.Length - 1, 1);
            }

            tagData.IntegrateValue(TagData.TAG_FIELD_COMMENT, comment.ToString());
            bitrate = sizeInfo.FileSize / duration;

            return(result);
        }
            public Matrix4x4[] ReadMatrix4x4()
            {
                if (!ValidateAccessorType(type, AccessorType.MAT4))
                {
                    return(new Matrix4x4[count]);
                }

                Func <BufferedBinaryReader, float> floatReader = GetFloatReader(componentType);

                Matrix4x4[] m = new Matrix4x4[count];
                if (bufferView != null)
                {
                    BufferedBinaryReader reader = new BufferedBinaryReader(bufferView.stream, 1024);
                    reader.Position = bufferView.byteOffset + byteOffset;
                    int byteSkip = byteStride.HasValue ? byteStride.Value - GetComponentSize() : 0;
                    for (int i = 0; i < count; i++)
                    {
                        m[i].m00 = floatReader(reader);
                        m[i].m01 = floatReader(reader);
                        m[i].m02 = floatReader(reader);
                        m[i].m03 = floatReader(reader);
                        m[i].m10 = floatReader(reader);
                        m[i].m11 = floatReader(reader);
                        m[i].m12 = floatReader(reader);
                        m[i].m13 = floatReader(reader);
                        m[i].m20 = floatReader(reader);
                        m[i].m21 = floatReader(reader);
                        m[i].m22 = floatReader(reader);
                        m[i].m23 = floatReader(reader);
                        m[i].m30 = floatReader(reader);
                        m[i].m31 = floatReader(reader);
                        m[i].m32 = floatReader(reader);
                        m[i].m33 = floatReader(reader);
                        reader.Skip(byteSkip);
                    }
                }
                if (sparse != null)
                {
                    Func <BufferedBinaryReader, int> indexIntReader = GetIntReader(sparse.indices.componentType);
                    BufferedBinaryReader             indexReader    = new BufferedBinaryReader(sparse.indices.bufferView.stream, 1024);
                    indexReader.Position = sparse.indices.bufferView.byteOffset + sparse.indices.byteOffset;
                    int[] indices = new int[sparse.count];
                    for (int i = 0; i < sparse.count; i++)
                    {
                        indices[i] = indexIntReader(indexReader);
                    }
                    BufferedBinaryReader valueReader = new BufferedBinaryReader(sparse.values.bufferView.stream, 1024);
                    indexReader.Position = sparse.values.bufferView.byteOffset + sparse.values.byteOffset;
                    for (int i = 0; i < sparse.count; i++)
                    {
                        int index = indices[i];
                        m[index].m00 = floatReader(valueReader);
                        m[index].m01 = floatReader(valueReader);
                        m[index].m02 = floatReader(valueReader);
                        m[index].m03 = floatReader(valueReader);
                        m[index].m10 = floatReader(valueReader);
                        m[index].m11 = floatReader(valueReader);
                        m[index].m12 = floatReader(valueReader);
                        m[index].m13 = floatReader(valueReader);
                        m[index].m20 = floatReader(valueReader);
                        m[index].m21 = floatReader(valueReader);
                        m[index].m22 = floatReader(valueReader);
                        m[index].m23 = floatReader(valueReader);
                        m[index].m30 = floatReader(valueReader);
                        m[index].m31 = floatReader(valueReader);
                        m[index].m32 = floatReader(valueReader);
                        m[index].m33 = floatReader(valueReader);
                    }
                }
                return(m);
            }
            public Color[] ReadColor()
            {
                if (!ValidateAccessorTypeAny(type, AccessorType.VEC3, AccessorType.VEC4))
                {
                    return(new Color[count]);
                }

                Func <BufferedBinaryReader, float> floatReader = GetFloatReader(componentType);

                Color[] c = new Color[count];
                if (bufferView != null)
                {
                    BufferedBinaryReader reader = new BufferedBinaryReader(bufferView.stream, 1024);
                    reader.Position = bufferView.byteOffset + byteOffset;
                    int byteSkip = byteStride.HasValue ? byteStride.Value - GetComponentSize() : 0;
                    if (type == AccessorType.VEC3)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            c[i].r = floatReader(reader);
                            c[i].g = floatReader(reader);
                            c[i].b = floatReader(reader);
                            reader.Skip(byteSkip);
                        }
                    }
                    else if (type == AccessorType.VEC4)
                    {
                        for (int i = 0; i < count; i++)
                        {
                            c[i].r = floatReader(reader);
                            c[i].g = floatReader(reader);
                            c[i].b = floatReader(reader);
                            c[i].a = floatReader(reader);
                            reader.Skip(byteSkip);
                        }
                    }
                }
                if (sparse != null)
                {
                    Func <BufferedBinaryReader, int> indexIntReader = GetIntReader(sparse.indices.componentType);
                    BufferedBinaryReader             indexReader    = new BufferedBinaryReader(sparse.indices.bufferView.stream, 1024);
                    indexReader.Position = sparse.indices.bufferView.byteOffset + sparse.indices.byteOffset;
                    int[] indices = new int[sparse.count];
                    for (int i = 0; i < sparse.count; i++)
                    {
                        indices[i] = indexIntReader(indexReader);
                    }
                    BufferedBinaryReader valueReader = new BufferedBinaryReader(sparse.values.bufferView.stream, 1024);
                    indexReader.Position = sparse.values.bufferView.byteOffset + sparse.values.byteOffset;
                    if (type == AccessorType.VEC3)
                    {
                        for (int i = 0; i < sparse.count; i++)
                        {
                            int index = indices[i];
                            c[index].r = floatReader(valueReader);
                            c[index].g = floatReader(valueReader);
                            c[index].b = floatReader(valueReader);
                        }
                    }
                    else if (type == AccessorType.VEC4)
                    {
                        for (int i = 0; i < sparse.count; i++)
                        {
                            int index = indices[i];
                            c[index].r = floatReader(valueReader);
                            c[index].g = floatReader(valueReader);
                            c[index].b = floatReader(valueReader);
                            c[index].a = floatReader(valueReader);
                        }
                    }
                }
                return(c);
            }
Example #12
0
        protected override bool read(BinaryReader source, MetaDataIO.ReadTagParams readTagParams)
        {
            bool result = true;

            ushort nbOrders      = 0;
            ushort nbPatterns    = 0;
            ushort nbSamples     = 0;
            ushort nbInstruments = 0;

            ushort flags;
            ushort special;
            ushort trackerVersion;
            ushort trackerVersionCompatibility;

            bool useSamplesAsInstruments = false;

            ushort messageLength;
            uint   messageOffset;
            String message = "";

            IList <UInt32> patternPointers    = new List <UInt32>();
            IList <UInt32> instrumentPointers = new List <UInt32>();
            IList <UInt32> samplePointers     = new List <UInt32>();

            resetData();
            BufferedBinaryReader bSource = new BufferedBinaryReader(source.BaseStream);


            if (!IT_SIGNATURE.Equals(Utils.Latin1Encoding.GetString(bSource.ReadBytes(4))))
            {
                throw new InvalidDataException(sizeInfo.FileSize + " : Invalid IT file (file signature mismatch)"); // TODO - might be a compressed file -> PK header
            }

            tagExists = true;

            // Title = max first 26 chars after file signature; null-terminated
            string title = StreamUtils.ReadNullTerminatedStringFixed(bSource, Utils.Latin1Encoding, 26);

            if (readTagParams.PrepareForWriting)
            {
                structureHelper.AddZone(4, 26, new byte[26] {
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                }, ZONE_TITLE);
            }
            tagData.IntegrateValue(TagData.TAG_FIELD_TITLE, title.Trim());
            bSource.Seek(2, SeekOrigin.Current); // Pattern row highlight information

            nbOrders      = bSource.ReadUInt16();
            nbInstruments = bSource.ReadUInt16();
            nbSamples     = bSource.ReadUInt16();
            nbPatterns    = bSource.ReadUInt16();

            trackerVersion = bSource.ReadUInt16();
            trackerVersionCompatibility = bSource.ReadUInt16();

            flags = bSource.ReadUInt16();

            useSamplesAsInstruments = (flags & 0x04) <= 0;

            special = bSource.ReadUInt16();

            //            trackerName = "Impulse tracker"; // TODO use TrackerVersion to add version

            bSource.Seek(2, SeekOrigin.Current); // globalVolume (8b), masterVolume (8b)

            initialSpeed = bSource.ReadByte();
            initialTempo = bSource.ReadByte();

            bSource.Seek(2, SeekOrigin.Current); // panningSeparation (8b), pitchWheelDepth (8b)

            messageLength = bSource.ReadUInt16();
            messageOffset = bSource.ReadUInt32();
            bSource.Seek(132, SeekOrigin.Current); // reserved (32b), channel Pan (64B), channel Vol (64B)

            // Orders table
            for (int i = 0; i < nbOrders; i++)
            {
                patternTable.Add(bSource.ReadByte());
            }

            // Instruments pointers
            for (int i = 0; i < nbInstruments; i++)
            {
                instrumentPointers.Add(bSource.ReadUInt32());
            }

            // Samples pointers
            for (int i = 0; i < nbSamples; i++)
            {
                samplePointers.Add(bSource.ReadUInt32());
            }

            // Patterns pointers
            for (int i = 0; i < nbPatterns; i++)
            {
                patternPointers.Add(bSource.ReadUInt32());
            }

            if ((!useSamplesAsInstruments) && (instrumentPointers.Count > 0))
            {
                if (trackerVersionCompatibility < 0x200)
                {
                    readInstrumentsOld(bSource, instrumentPointers);
                }
                else
                {
                    readInstruments(bSource, instrumentPointers);
                }
            }
            else
            {
                readSamples(bSource, samplePointers);
            }
            readPatterns(bSource, patternPointers);

            // IT Message
            if ((special & 0x1) > 0)
            {
                bSource.Seek(messageOffset, SeekOrigin.Begin);
                message = StreamUtils.ReadNullTerminatedStringFixed(bSource, Utils.Latin1Encoding, messageLength);
            }


            // == Computing track properties

            duration = calculateDuration() * 1000.0;

            string commentStr;

            if (messageLength > 0) // Get Comment from the "IT message" field
            {
                commentStr = message;
            }
            else // Get Comment from all the instrument names (common practice in the tracker community)
            {
                StringBuilder comment = new StringBuilder("");
                // NB : Whatever the value of useSamplesAsInstruments, FInstruments contain the right data
                foreach (Instrument i in instruments)
                {
                    if (i.DisplayName.Length > 0)
                    {
                        comment.Append(i.DisplayName).Append(Settings.InternalValueSeparator);
                    }
                }
                if (comment.Length > 0)
                {
                    comment.Remove(comment.Length - 1, 1);
                }
                commentStr = comment.ToString();
            }
            tagData.IntegrateValue(TagData.TAG_FIELD_COMMENT, commentStr);

            bitrate = (double)sizeInfo.FileSize / duration;

            return(result);
        }
Example #13
0
        private void readPatterns(BufferedBinaryReader source, IList <UInt32> patternPointers)
        {
            ushort                  nbRows;
            byte                    rowNum;
            byte                    what;
            byte                    maskVariable = 0;
            IList <Event>           aRow;
            IList <IList <Event> >  aPattern;
            IDictionary <int, byte> maskVariables = new Dictionary <int, byte>();

            foreach (UInt32 pos in patternPointers)
            {
                aPattern = new List <IList <Event> >();
                if (pos > 0)
                {
                    source.Seek(pos, SeekOrigin.Begin);
                    aRow   = new List <Event>();
                    rowNum = 0;
                    source.Seek(2, SeekOrigin.Current); // patternSize
                    nbRows = source.ReadUInt16();
                    source.Seek(4, SeekOrigin.Current); // unused data

                    do
                    {
                        what = source.ReadByte();

                        if (what > 0)
                        {
                            Event theEvent = new Event();
                            theEvent.Channel = (what - 1) & 63;
                            if ((what & 128) > 0)
                            {
                                maskVariable = source.ReadByte();
                                maskVariables[theEvent.Channel] = maskVariable;
                            }
                            else if (maskVariables.ContainsKey(theEvent.Channel))
                            {
                                maskVariable = maskVariables[theEvent.Channel];
                            }
                            else
                            {
                                maskVariable = 0;
                            }

                            if ((maskVariable & 1) > 0)
                            {
                                source.Seek(1, SeekOrigin.Current);                         // Note
                            }
                            if ((maskVariable & 2) > 0)
                            {
                                source.Seek(1, SeekOrigin.Current);                         // Instrument
                            }
                            if ((maskVariable & 4) > 0)
                            {
                                source.Seek(1, SeekOrigin.Current);                         // Volume/panning
                            }
                            if ((maskVariable & 8) > 0)
                            {
                                theEvent.Command = source.ReadByte();
                                theEvent.Info    = source.ReadByte();
                            }

                            aRow.Add(theEvent);
                        }
                        else // what = 0 => end of row
                        {
                            aPattern.Add(aRow);
                            aRow = new List <Event>();
                            rowNum++;
                        }
                    } while (rowNum < nbRows);
                }

                patterns.Add(aPattern);
            }
        }
Example #14
0
 private void readInstrumentsOld(BufferedBinaryReader source, IList <UInt32> instrumentPointers)
 {
     // The fileName and displayName fields have the same offset in the new and old format
     readInstruments(source, instrumentPointers);
 }