Ejemplo n.º 1
0
        void SerializeTitleMemory(IO.EndianStream gpdStream, long tmOffset, IO.IEndianStreamSerializable tm)
        {
            gpdStream.Seek(tmOffset);

            if (gpdStream.IsReading)
            {
                byte[] uncompressed_data = CompressedStream.DecompressFromStream(gpdStream);
                using (var ms = new MemoryStream(uncompressed_data, false))
                    using (var s = new IO.EndianStream(ms, FileAccess.Read))
                    {
                        s.StreamMode = FileAccess.Read;
                        s.Stream(tm);
                    }
            }
            else if (gpdStream.IsWriting)
            {
                Contract.Assert(false, "TODO");

                using (var ms = new MemoryStream())
                    using (var s = new IO.EndianStream(ms, FileAccess.Write))
                    {
                        s.StreamMode = FileAccess.Write;
                        s.Stream(tm);

                        ms.Seek(0, SeekOrigin.Begin);
                        uint stream_adlr;
                        int  stream_size;
                        CompressedStream.CompressFromStream(gpdStream.Writer, ms, out stream_adlr, out stream_size);
                    }
            }
        }
Ejemplo n.º 2
0
        static void Stream(IO.EndianStream s, bool crypt, IO.IEndianStreamSerializable obj,
                           long size = 0, ulong userKey = 0, Action <IO.EndianStream> streamLeftovers = null)
        {
            if (!crypt)
            {
                obj.Serialize(s);

                if (s.IsWriting && streamLeftovers != null)
                {
                    streamLeftovers(s);
                }
            }
            else
            {
                using (var ms = new System.IO.MemoryStream())
                    using (var crypted = new IO.EndianStream(ms, s.ByteOrder))
                    {
                        crypted.StreamMode = s.StreamMode;

                        if (s.IsReading)
                        {
                            var tea = new Security.Cryptography.PhxTEA(s.Reader, crypted.Writer);
                            tea.InitializeKey(Security.Cryptography.PhxTEA.kKeyGameFile, userKey);
                            tea.Decrypt(size);

                            crypted.Seek(0);
                        }

                        obj.Serialize(crypted);

                        if (streamLeftovers != null)
                        {
                            streamLeftovers(crypted);
                        }

                        if (s.IsWriting)
                        {
                            crypted.Seek(0);

                            var tea = new Security.Cryptography.PhxTEA(crypted.Reader, s.Writer);
                            tea.InitializeKey(Security.Cryptography.PhxTEA.kKeyGameFile, userKey);
                            tea.Encrypt(size);
                        }
                    }
            }
        }