This is a Protobuf serializer that serializes and deserializes the CompiledAsset class.
This code is generated from the precompile tool, decompiled with ILSpy and copied back into the Protogame code base. This is done so that we can serialize and deserialize on mobile platforms and so that we don't introduce a new DLL from the result of precompile. If the structure of CompiledAsset, PlatformData or TargetPlatform ever changes, this code will need to be regenerated.
Inheritance: ProtoBuf.Meta.TypeModel
Exemplo n.º 1
0
        /// <summary>
        /// The attempt load of file.
        /// </summary>
        /// <param name="file">
        /// The file.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        /// <exception cref="InvalidDataException">
        /// </exception>
        private IRawAsset AttemptLoadOfFile(FileInfo file, string name)
        {
            if (file.Exists)
            {
                using (var stream = new FileStream(file.FullName, FileMode.Open))
                {
                    using (var reader = new BinaryReader(stream))
                    {
                        if (reader.ReadByte() != 0)
                        {
                            throw new InvalidDataException();
                        }

                        var start = DateTime.Now;
                        using (var memory = new MemoryStream())
                        {
                            LzmaHelper.Decompress(reader.BaseStream, memory);
                            memory.Seek(0, SeekOrigin.Begin);
                            var serializer = new CompiledAssetSerializer();
                            var result     = (CompiledAsset)serializer.Deserialize(memory, null, typeof(CompiledAsset));
                            return(result);
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// The attempt load of stream.
        /// </summary>
        /// <param name="stream">
        /// The stream.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        /// <exception cref="InvalidDataException">
        /// </exception>
        private IRawAsset AttemptLoadOfStream(Stream stream, string name)
        {
            if (stream == null)
            {
                return(null);
            }

            using (var copy = new MemoryStream())
            {
                // The reader.BaseStream won't support the Length property as it
                // is a stream from an Android asset; we have to copy the stream
                // to memory and then decompress it.
                stream.CopyTo(copy);
                copy.Seek(0, SeekOrigin.Begin);

                using (var reader = new BinaryReader(copy))
                {
                    if (reader.ReadByte() != 0)
                    {
                        throw new InvalidDataException();
                    }

                    var start = DateTime.Now;
                    using (var memory = new MemoryStream())
                    {
                        LzmaHelper.Decompress(reader.BaseStream, memory);
                        memory.Seek(0, SeekOrigin.Begin);
                        var serializer = new CompiledAssetSerializer();
                        var result     = (CompiledAsset)serializer.Deserialize(memory, null, typeof(CompiledAsset));
                        return(result);
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// The attempt load of file.
        /// </summary>
        /// <param name="file">
        /// The file.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        /// <exception cref="InvalidDataException">
        /// </exception>
        private IRawAsset AttemptLoadOfFile(FileInfo file, string name)
        {
            if (file.Exists)
            {
                using (var stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (var reader = new BinaryReader(stream))
                    {
                        switch (reader.ReadByte())
                        {
                        case CompiledAsset.FORMAT_LZMA_COMPRESSED:
                            using (var memory = new MemoryStream())
                            {
                                LzmaHelper.Decompress(reader.BaseStream, memory);
                                memory.Seek(0, SeekOrigin.Begin);
                                var serializer = new CompiledAssetSerializer();
                                var result     = (CompiledAsset)serializer.Deserialize(memory, null, typeof(CompiledAsset));
                                return(result);
                            }

                        case CompiledAsset.FORMAT_UNCOMPRESSED:
                            var ucserializer = new CompiledAssetSerializer();
                            var ucresult     = (CompiledAsset)ucserializer.Deserialize(reader.BaseStream, null, typeof(CompiledAsset));
                            return(ucresult);

                        default:
                            throw new InvalidDataException();
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 4
0
        /// <summary>
        /// The _2.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <param name="protoReader">
        /// The proto reader.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        private static object _2(object obj, ProtoReader protoReader)
        {
            if (obj != null)
            {
                return(Read((TargetPlatform)obj, protoReader));
            }

            return(CompiledAssetSerializer.Read(TargetPlatform.Windows, protoReader));
        }
Exemplo n.º 5
0
        /// <summary>
        /// The save compiled asset.
        /// </summary>
        /// <param name="rootPath">
        /// The root path.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="isCompiled">
        /// The is compiled.
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        public void SaveCompiledAsset(string rootPath, string name, IRawAsset data, bool isCompiled, string embedPlatform = null)
        {
            var extension = "asset";

            if (isCompiled)
            {
                extension = "bin";
            }

            var filename = name.Replace('.', Path.DirectorySeparatorChar) + "." + extension;

            if (!string.IsNullOrWhiteSpace(embedPlatform))
            {
                filename = name + "-" + embedPlatform + "." + extension;
            }

            var file = new FileInfo(Path.Combine(rootPath, filename));

            this.CreateDirectories(file.Directory);
            if (isCompiled)
            {
                if (!(data is CompiledAsset))
                {
                    throw new InvalidOperationException();
                }

                var compiledData = (CompiledAsset)data;
                using (var stream = new FileStream(file.FullName, FileMode.Create))
                {
                    stream.WriteByte(0);
                    using (var memory = new MemoryStream())
                    {
                        var serializer = new CompiledAssetSerializer();
                        serializer.Serialize(memory, compiledData);
                        memory.Seek(0, SeekOrigin.Begin);
                        LzmaHelper.Compress(memory, stream);
                    }
                }
            }
            else
            {
                using (var writer = new StreamWriter(file.FullName, false, Encoding.UTF8))
                {
                    writer.Write(JsonConvert.SerializeObject(data.Properties.ToDictionary(k => k.Key, v => v.Value)));
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// The attempt load.
        /// </summary>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        /// <exception cref="InvalidDataException">
        /// </exception>
        public IRawAsset AttemptLoad(string path, string name, ref DateTime?lastModified, bool noTranslate = false)
        {
            lastModified = new DateTime(1970, 1, 1, 0, 0, 0);

            var embedded = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
                            where !assembly.IsDynamic
                            from resource in assembly.GetManifestResourceNames()
                            where
                            resource
                            == assembly.GetName().Name + "." + name + "-"
                            + TargetPlatformUtility.GetExecutingPlatform() + ".bin" ||
                            resource
                            == assembly.GetName().Name + ".Resources." + name + "-"
                            + TargetPlatformUtility.GetExecutingPlatform() + ".bin"
                            select assembly.GetManifestResourceStream(resource)).ToList();

            if (embedded.Any())
            {
                using (var reader = new BinaryReader(embedded.First()))
                {
                    switch (reader.ReadByte())
                    {
                    case CompiledAsset.FORMAT_LZMA_COMPRESSED:
                        using (var memory = new MemoryStream())
                        {
                            LzmaHelper.Decompress(reader.BaseStream, memory);
                            memory.Seek(0, SeekOrigin.Begin);
                            var serializer = new CompiledAssetSerializer();
                            var result     = (CompiledAsset)serializer.Deserialize(memory, null, typeof(CompiledAsset));
                            return(result);
                        }

                    case CompiledAsset.FORMAT_UNCOMPRESSED:
                        var ucserializer = new CompiledAssetSerializer();
                        var ucresult     = (CompiledAsset)ucserializer.Deserialize(reader.BaseStream, null, typeof(CompiledAsset));
                        return(ucresult);

                    default:
                        throw new InvalidDataException();
                    }
                }
            }

            return(null);
        }
Exemplo n.º 7
0
        /// <summary>
        /// The save compiled asset.
        /// </summary>
        /// <param name="rootPath">
        /// The root path.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="isCompiled">
        /// The is compiled.
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        public void SaveCompiledAsset(string rootPath, string name, IRawAsset data, bool isCompiled, string embedPlatform = null)
        {
            var extension = "asset";

            if (isCompiled)
            {
                extension = "bin";
            }

            var filename = name.Replace('.', Path.DirectorySeparatorChar) + "." + extension;

            if (!string.IsNullOrWhiteSpace(embedPlatform))
            {
                filename = name + "-" + embedPlatform + "." + extension;
            }

            var file = new FileInfo(Path.Combine(rootPath, filename));

            this.CreateDirectories(file.Directory);
            if (isCompiled)
            {
                if (!(data is CompiledAsset))
                {
                    throw new InvalidOperationException();
                }

                var compiledData = (CompiledAsset)data;
                using (var stream = new FileStream(file.FullName, FileMode.Create))
                {
                    // LZMA compression is proving too expensive to decompress.  We'll look into
                    // LZ4 in the future, but for now, just store assets uncompressed.
                    stream.WriteByte(CompiledAsset.FORMAT_UNCOMPRESSED);

                    var serializer = new CompiledAssetSerializer();
                    serializer.Serialize(stream, compiledData);
                }
            }
            else
            {
                using (var writer = new StreamWriter(file.FullName, false, Encoding.UTF8))
                {
                    writer.Write(JsonConvert.SerializeObject(data.Properties.ToDictionary(k => k.Key, v => v.Value)));
                }
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// The save compiled asset.
        /// </summary>
        /// <param name="rootPath">
        /// The root path.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="isCompiled">
        /// The is compiled.
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        public void SaveCompiledAsset(string rootPath, string name, IRawAsset data, bool isCompiled, string embedPlatform = null)
        {
            var extension = "asset";
            if (isCompiled)
            {
                extension = "bin";
            }

            var filename = name.Replace('.', Path.DirectorySeparatorChar) + "." + extension;
            if (!string.IsNullOrWhiteSpace(embedPlatform))
            {
                filename = name + "-" + embedPlatform + "." + extension;
            }

            var file = new FileInfo(Path.Combine(rootPath, filename));
            this.CreateDirectories(file.Directory);
            if (isCompiled)
            {
                if (!(data is CompiledAsset))
                {
                    throw new InvalidOperationException();
                }

                var compiledData = (CompiledAsset)data;
                using (var stream = new FileStream(file.FullName, FileMode.Create))
                {
                    stream.WriteByte(0);
                    using (var memory = new MemoryStream())
                    {
                        var serializer = new CompiledAssetSerializer();
                        serializer.Serialize(memory, compiledData);
                        memory.Seek(0, SeekOrigin.Begin);
                        LzmaHelper.Compress(memory, stream);
                    }
                }
            }
            else
            {
                using (var writer = new StreamWriter(file.FullName, false, Encoding.UTF8))
                {
                    writer.Write(JsonConvert.SerializeObject(data.Properties.ToDictionary(k => k.Key, v => v.Value)));
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// The save compiled asset.
        /// </summary>
        /// <param name="rootPath">
        /// The root path.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="data">
        /// The data.
        /// </param>
        /// <param name="isCompiled">
        /// The is compiled.
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        public void SaveCompiledAsset(string rootPath, string name, IRawAsset data, bool isCompiled, string embedPlatform = null)
        {
            var extension = "asset";
            if (isCompiled)
            {
                extension = "bin";
            }

            var filename = name.Replace('.', Path.DirectorySeparatorChar) + "." + extension;
            if (!string.IsNullOrWhiteSpace(embedPlatform))
            {
                filename = name + "-" + embedPlatform + "." + extension;
            }

            var file = new FileInfo(Path.Combine(rootPath, filename));
            this.CreateDirectories(file.Directory);
            if (isCompiled)
            {
                if (!(data is CompiledAsset))
                {
                    throw new InvalidOperationException();
                }

                var compiledData = (CompiledAsset)data;
                using (var stream = new FileStream(file.FullName, FileMode.Create))
                {
                    // LZMA compression is proving too expensive to decompress.  We'll look into
                    // LZ4 in the future, but for now, just store assets uncompressed.
                    stream.WriteByte(CompiledAsset.FORMAT_UNCOMPRESSED);

                    var serializer = new CompiledAssetSerializer();
                    serializer.Serialize(stream, compiledData);
                }
            }
            else
            {
                using (var writer = new StreamWriter(file.FullName, false, Encoding.UTF8))
                {
                    writer.Write(JsonConvert.SerializeObject(data.Properties.ToDictionary(k => k.Key, v => v.Value)));
                }
            }
        }
        /// <summary>
        /// The attempt load.
        /// </summary>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        /// <exception cref="InvalidDataException">
        /// </exception>
        public IRawAsset AttemptLoad(string path, string name, ref DateTime? lastModified, bool noTranslate = false)
        {
            lastModified = new DateTime(1970, 1, 1, 0, 0, 0);

            var embedded = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
                            where !assembly.IsDynamic
                            from resource in assembly.GetManifestResourceNames()
                            where
                                resource
                                    == assembly.GetName().Name + "." + name + "-"
                                    + TargetPlatformUtility.GetExecutingPlatform() + ".bin" ||
                                resource
                                    == assembly.GetName().Name + ".Resources." + name + "-"
                                    + TargetPlatformUtility.GetExecutingPlatform() + ".bin"
                            select assembly.GetManifestResourceStream(resource)).ToList();
            if (embedded.Any())
            {
                using (var reader = new BinaryReader(embedded.First()))
                {
                    switch (reader.ReadByte())
                    {
                        case CompiledAsset.FORMAT_LZMA_COMPRESSED:
                            using (var memory = new MemoryStream())
                            {
                                LzmaHelper.Decompress(reader.BaseStream, memory);
                                memory.Seek(0, SeekOrigin.Begin);
                                var serializer = new CompiledAssetSerializer();
                                var result = (CompiledAsset)serializer.Deserialize(memory, null, typeof(CompiledAsset));
                                return result;
                            }
                        case CompiledAsset.FORMAT_UNCOMPRESSED:
                            var ucserializer = new CompiledAssetSerializer();
                            var ucresult = (CompiledAsset)ucserializer.Deserialize(reader.BaseStream, null, typeof(CompiledAsset));
                            return ucresult;
                        default:
                            throw new InvalidDataException();
                    }
                }
            }

            return null;
        }
Exemplo n.º 11
0
        /// <summary>
        /// The attempt load of file.
        /// </summary>
        /// <param name="file">
        /// The file.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        /// <exception cref="InvalidDataException">
        /// </exception>
        private IRawAsset AttemptLoadOfFile(FileInfo file, string name)
        {
            if (file.Exists)
            {
                using (var stream = new FileStream(file.FullName, FileMode.Open))
                {
                    using (var reader = new BinaryReader(stream))
                    {
                        if (reader.ReadByte() != 0)
                        {
                            throw new InvalidDataException();
                        }

                        var start = DateTime.Now;
                        using (var memory = new MemoryStream())
                        {
                            LzmaHelper.Decompress(reader.BaseStream, memory);
                            memory.Seek(0, SeekOrigin.Begin);
                            var serializer = new CompiledAssetSerializer();
                            var result = (CompiledAsset)serializer.Deserialize(memory, null, typeof(CompiledAsset));
                            return result;
                        }
                    }
                }
            }

            return null;
        }
        /// <summary>
        /// The attempt load of file.
        /// </summary>
        /// <param name="file">
        /// The file.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        /// <exception cref="InvalidDataException">
        /// </exception>
        private IRawAsset AttemptLoadOfFile(FileInfo file, string name)
        {
            if (file.Exists)
            {
                using (var stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (var reader = new BinaryReader(stream))
                    {
                        switch (reader.ReadByte())
                        {
                            case CompiledAsset.FORMAT_LZMA_COMPRESSED:
                                using (var memory = new MemoryStream())
                                {
                                    LzmaHelper.Decompress(reader.BaseStream, memory);
                                    memory.Seek(0, SeekOrigin.Begin);
                                    var serializer = new CompiledAssetSerializer();
                                    var result = (CompiledAsset)serializer.Deserialize(memory, null, typeof(CompiledAsset));
                                    return result;
                                }
                            case CompiledAsset.FORMAT_UNCOMPRESSED:
                                var ucserializer = new CompiledAssetSerializer();
                                var ucresult = (CompiledAsset)ucserializer.Deserialize(reader.BaseStream, null, typeof(CompiledAsset));
                                return ucresult;
                            default:
                                throw new InvalidDataException();
                        }
                    }
                }
            }

            return null;
        }
        /// <summary>
        /// The attempt load of stream.
        /// </summary>
        /// <param name="stream">
        /// The stream.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        /// <exception cref="InvalidDataException">
        /// </exception>
        private IRawAsset AttemptLoadOfStream(Stream stream, string name)
        {
            if (stream == null)
            {
                return null;
            }

            using (var copy = new MemoryStream())
            {
                // The reader.BaseStream won't support the Length property as it
                // is a stream from an Android asset; we have to copy the stream
                // to memory and then decompress it.
                stream.CopyTo(copy);
                copy.Seek(0, SeekOrigin.Begin);

                using (var reader = new BinaryReader(copy))
                {
                    switch (reader.ReadByte())
                    {
                        case CompiledAsset.FORMAT_LZMA_COMPRESSED:
                            using (var memory = new MemoryStream())
                            {
                                LzmaHelper.Decompress(reader.BaseStream, memory);
                                memory.Seek(0, SeekOrigin.Begin);
                                var serializer = new CompiledAssetSerializer();
                                var result = (CompiledAsset)serializer.Deserialize(memory, null, typeof(CompiledAsset));
                                return result;
                            }
                        case CompiledAsset.FORMAT_UNCOMPRESSED:
                            var ucserializer = new CompiledAssetSerializer();
                            var ucresult = (CompiledAsset)ucserializer.Deserialize(reader.BaseStream, null, typeof(CompiledAsset));
                            return ucresult;
                        default:
                            throw new InvalidDataException();
                    }
                }
            }
        }