Beispiel #1
0
 private static void HttpPrintAll(string url)
 {
     using (PartialHttpStream stream = new PartialHttpStream(url))
     {
         AtomPrintAll(stream);
         Console.WriteLine($"#http requests: {stream.HttpRequestsCount}");
     }
 }
Beispiel #2
0
 private static void HttpGet(string url, string atomTypeName)
 {
     using (PartialHttpStream stream = new PartialHttpStream(url))
     {
         var    mp4Reader = new AtomReader(stream);
         string value     = mp4Reader.GetMetaAtomValue(atomTypeName);
         Console.WriteLine($"{atomTypeName}: {value}");
         Console.WriteLine($"#http requests: {stream.HttpRequestsCount}");
     }
 }
Beispiel #3
0
        public async Task <GF> PrepareGFByTGAName(string tgaName)
        {
            FileStruct file = fileList.FirstOrDefault(f => f.TGAName.ToLower().Replace('/', '\\').Equals(tgaName.ToLower().Replace('/', '\\')));

            if (file == null)
            {
                return(null);
            }
            Reader            reader     = readers[file.fileNum];
            PartialHttpStream httpStream = reader.BaseStream as PartialHttpStream;

            if (httpStream != null)
            {
                Controller c = MapLoader.Loader.controller;
                readers[file.fileNum].BaseStream.Seek(file.pointer, SeekOrigin.Begin);
                await httpStream.FillCacheForRead(file.size);
            }
            byte[] bytes = GetFileBytes(file);
            //Util.ByteArrayToFile("textures/" + file.FullName, bytes);
            return(new GF(bytes));
        }
        private async Task LoadFromDAT()
        {
            Reader            reader     = dat.reader;
            PartialHttpStream httpStream = reader.BaseStream as PartialHttpStream;

            int levelIndex = 0;

            for (int i = 0; i < dat.gameDsb.levels.Count; i++)
            {
                if (dat.gameDsb.levels[i].ToLower().Equals(name.ToLower()))
                {
                    levelIndex = i;
                    break;
                }
            }
            RelocationTableReference rtref = new RelocationTableReference((byte)levelIndex, (byte)type);
            //R3Loader.Loader.print("RtRef Pre  (" + rtref.levelId + "," + rtref.relocationType + ")");
            uint mask = dat.GetMask(rtref);
            await dat.GetOffset(rtref);

            uint offset = dat.lastOffset;

            //R3Loader.Loader.print("RtRef Post (" + rtref.levelId + "," + rtref.relocationType + ")");

            /*dat.reader.BaseStream.Seek(offset, SeekOrigin.Begin);
             * reader.SetMask(mask);
             * byte[] dataNew = reader.ReadBytes(1000000);
             * Util.ByteArrayToFile(name + "_" + type + ".data", dataNew);*/
            dat.reader.BaseStream.Seek(offset, SeekOrigin.Begin);
            if (httpStream != null)
            {
                await httpStream.FillCacheForRead(4);
            }
            reader.SetMask(mask);
            reader.ReadUInt32();
            await Read(reader);
        }
        async Task Read(Reader reader)
        {
            MapLoader         l          = MapLoader.Loader;
            PartialHttpStream httpStream = reader.BaseStream as PartialHttpStream;

            if (httpStream != null)
            {
                await httpStream.FillCacheForRead(5);
            }
            byte count = reader.ReadByte();

            if (Settings.s.game != Settings.Game.R2Demo &&
                Settings.s.engineVersion > Settings.EngineVersion.Montreal)
            {
                reader.ReadUInt32();
            }
            pointerBlocks = new RelocationPointerList[count];
            for (int i = 0; i < count; i++)
            {
                if (reader.BaseStream.Position >= reader.BaseStream.Length)
                {
                    Array.Resize(ref pointerBlocks, i);
                    break;
                }
                if (httpStream != null)
                {
                    await httpStream.FillCacheForRead(6);
                }
                // A pointer list contains pointers located in SNA part with matching module & block
                pointerBlocks[i]        = new RelocationPointerList();
                pointerBlocks[i].module = reader.ReadByte();
                pointerBlocks[i].id     = reader.ReadByte();
                l.print("Parsing pointer block for (" + pointerBlocks[i].module + "," + pointerBlocks[i].id + ")");
                //l.print("Module: " + parts[i].module + " - Block: " + parts[i].block);
                pointerBlocks[i].count    = reader.ReadUInt32();
                pointerBlocks[i].pointers = new RelocationPointerInfo[pointerBlocks[i].count];
                if (pointerBlocks[i].count > 0)
                {
                    if (Settings.s.snaCompression)
                    {
                        if (httpStream != null)
                        {
                            await httpStream.FillCacheForRead(5 *4);
                        }
                        uint isCompressed         = reader.ReadUInt32();
                        uint compressedSize       = reader.ReadUInt32();
                        uint compressedChecksum   = reader.ReadUInt32();
                        uint decompressedSize     = reader.ReadUInt32();
                        uint decompressedChecksum = reader.ReadUInt32();
                        if (httpStream != null)
                        {
                            await httpStream.FillCacheForRead((int)compressedSize);
                        }
                        byte[] compressedData = reader.ReadBytes((int)compressedSize);
                        if (isCompressed != 0)
                        {
                            using (var compressedStream = new MemoryStream(compressedData))
                                using (var lzo = new LzoStream(compressedStream, CompressionMode.Decompress))
                                    using (Reader lzoReader = new Reader(lzo, Settings.s.IsLittleEndian)) {
                                        ReadPointerBlock(lzoReader, pointerBlocks[i]);
                                    }
                        }
                        else
                        {
                            using (var uncompressedStream = new MemoryStream(compressedData))
                                using (Reader unCompressedReader = new Reader(uncompressedStream, Settings.s.IsLittleEndian)) {
                                    ReadPointerBlock(unCompressedReader, pointerBlocks[i]);
                                }
                        }
                    }
                    else
                    {
                        if (httpStream != null)
                        {
                            await httpStream.FillCacheForRead((int)pointerBlocks[i].count * 8);
                        }
                        ReadPointerBlock(reader, pointerBlocks[i]);
                    }
                }
            }
        }
Beispiel #6
0
        public async Task Init(int readerIndex, Reader reader)
        {
            PartialHttpStream httpStream = reader.BaseStream as PartialHttpStream;

            if (httpStream != null)
            {
                await httpStream.FillCacheForRead(11);
            }
            int localDirCount  = reader.ReadInt32();
            int localFileCount = reader.ReadInt32();

            directoryCount            += localDirCount;
            fileCount                 += localFileCount;
            directoryList[readerIndex] = new string[localDirCount];
            byte isXor       = reader.ReadByte();
            byte isChecksum  = reader.ReadByte();
            byte xorKey      = reader.ReadByte();
            byte curChecksum = 0;

            // Load directories
            //Debug.Log("directories");
            if (httpStream != null)
            {
                await httpStream.FillCacheForRead(300 *localDirCount);
            }
            await MapLoader.WaitIfNecessary();

            for (int i = 0; i < localDirCount; i++)
            {
                //if (httpStream != null) yield return c.StartCoroutine(httpStream.FillCacheForRead(4));
                int    strLen    = reader.ReadInt32();
                string directory = "";

                if (isXor != 0 || isChecksum != 0)
                {
                    for (int j = 0; j < strLen; j++)
                    {
                        byte b = reader.ReadByte();
                        if (isXor != 0)
                        {
                            b = (byte)(xorKey ^ b);
                        }
                        if (isChecksum != 0)
                        {
                            curChecksum = (byte)((curChecksum + b) % 256);
                        }
                        directory += (char)b;
                    }
                }
                else
                {
                    directory = reader.ReadString(strLen);
                }

                directoryList[readerIndex][i] = directory;
            }

            // Load and check version
            //if (httpStream != null) yield return c.StartCoroutine(httpStream.FillCacheForRead(1));
            byte directoryChecksum = reader.ReadByte();

            if (directoryChecksum != curChecksum)
            {
                Debug.LogWarning("CNT: Directory checksum failed");
            }

            // Read files
            //Debug.Log("files");
            if (httpStream != null)
            {
                await httpStream.FillCacheForRead(300 *localFileCount);
            }
            for (int i = 0; i < localFileCount; i++)
            {
                //if (httpStream != null) yield return c.StartCoroutine(httpStream.FillCacheForRead(8));
                int dirIndex = reader.ReadInt32();
                int strLen   = reader.ReadInt32();

                string file = "";

                //if (httpStream != null) yield return c.StartCoroutine(httpStream.FillCacheForRead(size + 16));
                if (isXor != 0)
                {
                    for (int j = 0; j < strLen; j++)
                    {
                        byte b = reader.ReadByte();
                        b     = (byte)(xorKey ^ b);
                        file += (char)b;
                    }
                }
                else
                {
                    file = reader.ReadString(strLen);
                }

                byte[] fileXorKey = new byte[4];
                reader.Read(fileXorKey, 0, 4);

                uint fileChecksum = reader.ReadUInt32();

                int dataPointer = reader.ReadInt32();
                int fileSize    = reader.ReadInt32();

                string dir = dirIndex != -1 ? directoryList[readerIndex][dirIndex] : "";

                fileList.Add(new FileStruct()
                {
                    directory = dir,
                    name      = file,
                    pointer   = dataPointer,
                    size      = fileSize,
                    xorKey    = fileXorKey,
                    checksum  = fileChecksum,
                    fileNum   = readerIndex
                });
            }
        }
Beispiel #7
0
        public async Task GetOffset(RelocationTableReference rtref)
        {
            PartialHttpStream httpStream = reader.BaseStream as PartialHttpStream;

            reader.MaskingOff();
            reader.BaseStream.Seek(0, SeekOrigin.Begin);

            if (httpStream != null)
            {
                await httpStream.FillCacheForRead(12 *4);
            }
            DATHeader header = new DATHeader();

            header.field_0 = reader.ReadInt32();
            header.field_4 = reader.ReadInt32();
            header.field_8 = reader.ReadInt32();
            header.field_C = reader.ReadInt32();

            int number;

            number          = reader.ReadInt32();
            header.field_0 += header.field_8;
            header.field_4 += header.field_C;
            number          = reader.ReadInt32();
            uint levels0DatValue_0 = (uint)(header.field_4 ^ (number - header.field_0));

            header.field_0 += header.field_8;
            header.field_4 += header.field_C;
            number          = reader.ReadInt32();
            uint levels0DatValue_1 = (uint)((header.field_4 ^ (uint)(number - header.field_0)) >> 2);

            header.field_0 += header.field_8;
            header.field_4 += header.field_C;
            number          = reader.ReadInt32();
            header.field_0 += header.field_8;
            header.field_4 += header.field_C;
            number          = reader.ReadInt32();
            int v4 = header.field_4 ^ (number - header.field_0);

            header.field_0 += header.field_8;
            header.field_4 += header.field_C;
            uint levels0DatValue_2 = (uint)v4;

            number = reader.ReadInt32();
            int v5 = header.field_4 ^ (number - header.field_0);

            header.field_0 += header.field_8;
            uint levels0DatValue_3 = (uint)v5;

            header.field_4 += header.field_C;
            number          = reader.ReadInt32();
            int v6 = header.field_4 ^ (number - header.field_0);

            header.field_0 += header.field_8;
            header.field_4 += header.field_C;
            uint levels0DatValue_4 = (uint)v6;

            number = reader.ReadInt32();
            int levels0DatValue_5 = header.field_4 ^ (number - header.field_0);

            header.field_0 += header.field_8;
            header.field_4 += header.field_C;

            // Get offset with sinus header - SNA_fn_hGetOffSetInBigFileWithSinusHeader

            rtref.levelId         = (byte)(rtref.levelId % levels0DatValue_1);
            rtref.relocationType &= 3; // can only be 0, 1, 2 or 3. No RTL? or another relocation type?

            v6 = 4 * rtref.levelId;
            int v28 = 4 * rtref.levelId;

            int v7 = v28;

            switch (rtref.relocationType)
            {
            case 1:
                v7  = v6 + 1;
                v28 = v7;
                break;

            case 2:
                v7  = v6 + 2;
                v28 = v7;
                break;

            case 3:
                v7  = v6 + 3;
                v28 = v7;
                break;

            default:
                break;
            }

            uint  v8  = rtref.byte2 % levels0DatValue_2;
            float v9  = 1.06913f;
            float v30 = 1.06913f;

            rtref.byte2 = (byte)v8;

            if (v8 != 0)
            {
                uint   v10 = 0;
                double v11 = 0;

                do
                {
                    v30 = v10;
                    v11 = v10;
                    v10++;
                    v9 = v9 - Math.Abs((float)Math.Sin(v11 * v11 * 1.69314f)) * -0.69314f - -0.52658f;
                }while (v10 < v8);

                v30 = v9;
            }

            //float v23 = (float)Math.Truncate(v30);
            //double v12 = v30 - v23;
            double v12 = v30 % 1f;
            double v13 = Math.Floor(v12 * 1000000.0) / 1000000.0;
            //R3Loader.Loader.print("Double v13: " + v13);
            ulong v24 = levels0DatValue_0;
            long  v14 = (long)Math.Floor(levels0DatValue_0 * v13);

            reader.BaseStream.Seek(levels0DatValue_4 + levels0DatValue_5 * v14, SeekOrigin.Begin);
            if (httpStream != null)
            {
                await httpStream.FillCacheForRead(4 * 4);
            }

            header.field_0 = reader.ReadInt32();
            header.field_4 = reader.ReadInt32();
            header.field_8 = reader.ReadInt32();
            header.field_C = reader.ReadInt32();

            if (v28 != 0)
            {
                int v15, v16;

                header.field_0 += v28 * header.field_8;
                v15             = header.field_4;
                v16             = v28;
                do
                {
                    v15 += header.field_C;
                    --v16;
                }while (v16 != 0);
                header.field_4 = v15;
            }

            reader.BaseStream.Seek(4 * v28, SeekOrigin.Current);
            if (httpStream != null)
            {
                await httpStream.FillCacheForRead(4);
            }
            uint value1 = reader.ReadUInt32();

            uint dataOffset = (uint)(header.field_4 ^ (value1 - header.field_0));

            lastOffset = dataOffset;
        }
Beispiel #8
0
        public async Task Init(int readerIndex, Reader reader)
        {
            PartialHttpStream httpStream = reader.BaseStream as PartialHttpStream;

            if (httpStream != null)
            {
                await httpStream.FillCacheForRead(11);
            }
            int localDirCount  = reader.ReadInt32();
            int localFileCount = reader.ReadInt32();

            directoryCount            += localDirCount;
            fileCount                 += localFileCount;
            directoryList[readerIndex] = new string[localDirCount];
            // Check signature
            if (reader.ReadInt16() != 257)
            {
                throw new FormatException("This is not a valid CNT archive!");
            }

            byte xorKey = reader.ReadByte();

            // Load directories
            //Debug.Log("directories");
            if (httpStream != null)
            {
                await httpStream.FillCacheForRead(300 *localDirCount);
            }
            await MapLoader.WaitIfNecessary();

            for (int i = 0; i < localDirCount; i++)
            {
                //if (httpStream != null) yield return c.StartCoroutine(httpStream.FillCacheForRead(4));
                int    strLen    = reader.ReadInt32();
                string directory = "";

                //if (httpStream != null) yield return c.StartCoroutine(httpStream.FillCacheForRead(strLen));
                for (int j = 0; j < strLen; j++)
                {
                    directory += (char)(xorKey ^ reader.ReadByte());
                }

                directoryList[readerIndex][i] = directory;
            }

            // Load and check version
            //if (httpStream != null) yield return c.StartCoroutine(httpStream.FillCacheForRead(1));
            byte verId = reader.ReadByte();

            switch (verId)
            {
            case 246:
                version = CNTVersion.Rayman2; break;

            default:
                version = CNTVersion.Rayman2Vignette; break;
            }

            // Read files
            //Debug.Log("files");
            if (httpStream != null)
            {
                await httpStream.FillCacheForRead(300 *localFileCount);
            }
            for (int i = 0; i < localFileCount; i++)
            {
                //if (httpStream != null) yield return c.StartCoroutine(httpStream.FillCacheForRead(8));
                int dirIndex = reader.ReadInt32();
                int size     = reader.ReadInt32();

                string file = "";

                //if (httpStream != null) yield return c.StartCoroutine(httpStream.FillCacheForRead(size + 16));
                for (int j = 0; j < size; j++)
                {
                    file += (char)(xorKey ^ reader.ReadByte());
                }

                byte[] fileXorKey = new byte[4];
                reader.Read(fileXorKey, 0, 4);

                uint magic2 = reader.ReadUInt32();

                int dataPointer = reader.ReadInt32();
                int fileSize    = reader.ReadInt32();

                string dir = dirIndex != -1 ? directoryList[readerIndex][dirIndex] : "";

                fileList.Add(new FileStruct()
                {
                    directory = dir,
                    name      = file,
                    pointer   = dataPointer,
                    size      = fileSize,
                    xorKey    = fileXorKey,
                    magic2    = magic2,
                    fileNum   = readerIndex
                });
            }
        }