Beispiel #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, 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);
        }
Beispiel #2
0
        /// <summary>
        /// The entry point for the bootstrapping program.
        /// </summary>
        /// <param name="args">The arguments passed in on the command line.</param>
        public static void Main(string[] args)
        {
            // This is a small, bootstrapping application that extracts
            // the LZMA compressed Protobuild.Internal library assembly
            // from an embedded resource and then loads it, calling
            // the internal library's Program.Main method instead.
            //
            // This is done so that we can significantly reduce the size
            // of the Protobuild executable shipped in repositories,
            // because most of the application code ends up being LZMA
            // compressed.

            var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Protobuild.Internal.dll.lzma");
            var memory = new MemoryStream();

            LzmaHelper.Decompress(stream, memory);

            var bytes = new byte[memory.Position];

            memory.Seek(0, SeekOrigin.Begin);
            memory.Read(bytes, 0, bytes.Length);
            memory.Close();

            var loaded      = Assembly.Load(bytes);
            var realProgram = loaded.GetType("Protobuild.MainClass");
            var main        = realProgram.GetMethod("Main", BindingFlags.Public | BindingFlags.Static);

            main.Invoke(null, new object[] { (string)Environment.CurrentDirectory, (string[])args });
        }
Beispiel #3
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);
                    }
                }
            }
        }
    static void PackFile(string fileSrc, string fileDst, string name, ThreadParam param)
    {
        if (!IGG.FileUtil.CheckFileExist(fileDst))
        {
            Debug.LogFormat("-->PackFile: {0} -> {1}", fileSrc, fileDst);

            IGG.FileUtil.CreateDirectoryFromFile(fileDst);
            IGG.FileUtil.DeleteFile(fileDst);

            LzmaHelper.Compress(fileSrc, fileDst);
        }

        if (ConstantData.enableMd5Name)
        {
            string md5 = IGG.FileUtil.CalcFileMd5(fileSrc);

            FileInfo fi = new FileInfo(fileDst);

            VersionData.VersionItem item = new VersionData.VersionItem();
            item.Name = name.Replace(ConstantData.assetBundleExt, "");
            item.Md5  = md5;
            item.Size = fi.Length;

            lock (param.lockd)
            {
                param.list.Add(item);
            }
        }
    }
Beispiel #5
0
        public static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("No input or output file specified for compression.");
                Environment.Exit(1);
            }

            if (args.Length == 1)
            {
                Console.WriteLine("No output file specified for compression.");
                Environment.Exit(1);
            }

            var srcFile  = args[0];
            var destFile = args[1];

            using (var reader = new FileStream(srcFile, FileMode.Open, FileAccess.Read))
            {
                using (var writer = new FileStream(destFile, FileMode.Create, FileAccess.Write))
                {
                    LzmaHelper.Compress(reader, writer);
                }
            }

            Console.WriteLine(srcFile + " compressed as " + destFile);
        }
Beispiel #6
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);
        }
Beispiel #7
0
        private byte[] Decompress(byte[] bytes)
        {
            byte[] result = new byte[1];
            int    size   = LzmaHelper.Decompress(bytes, ref result);

            if (size == 0)
            {
                UnityEngine.Debug.LogError("Uncompress Failed");
                return(null);
            }

            return(result);
        }
        /// <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)));
                }
            }
        }
Beispiel #9
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);
        }
Beispiel #10
0
 /// <summary>
 /// Compress all included objects and store it into temporary directory.
 /// </summary>
 /// <returns>Dictionary with packed files locations.</returns>
 private static Dictionary<IncludedObjectConfigBase, string> packIncludedObjects(BuildConfiguration configuration, string tempDirectoryName)
 {
     Dictionary<IncludedObjectConfigBase, string> includedObjectsPackedFiles = new Dictionary<IncludedObjectConfigBase, string>();
     // Packing the data to the temp directory
     try {
         foreach (IncludedObjectConfigBase configBase in configuration.OutputConfig.GetAllIncludedObjects()) {
             string sourceFilePath = configurePathByConfigurationVariables(configBase.Path, configuration);
             //
             string packedFilePath = Path.Combine(tempDirectoryName, Guid.NewGuid() + ".packed");
             //
             if (logger.IsInfoEnabled) {
                 logger.Info(String.Format("Reading {0} file.", sourceFilePath));
             }
             byte[] bytes = File.ReadAllBytes(sourceFilePath);
             //
             if (logger.IsInfoEnabled) {
                 logger.Info(String.Format("Readed {0} bytes.", bytes.Length));
             }
             //
             if (logger.IsInfoEnabled) {
                 logger.Info(String.Format("Compressing file and storing to {0}.", packedFilePath));
             }
             //
             long encodedLength;
             byte[] compressed = LzmaHelper.Encode(configBase.CompressionConfig, bytes, bytes.LongLength, out encodedLength);
             using (FileStream fileStream = File.OpenWrite(packedFilePath)) {
                 fileStream.Write(compressed, 0, unchecked((int)encodedLength));
             }
             //
             if (logger.IsInfoEnabled) {
                 logger.Info(String.Format("Compressing OK. Compressed to {0} bytes.", encodedLength));
             }
             //
             includedObjectsPackedFiles.Add(configBase, packedFilePath);
         }
         //
         return (includedObjectsPackedFiles);
     } catch (IOException exc) {
         if (logger.IsErrorEnabled) {
             logger.Error(String.Format("Error during packing source files. Message : {0}.", exc.Message), exc);
         }
         throw;
     }
 }
Beispiel #11
0
 public void Decompress(byte[] data)
 {
     Loom.RunAsync(new LoomBase(), delegate(LoomBase param)
     {
         try
         {
             m_Bytes  = new byte[1];
             int size = LzmaHelper.Decompress(data, ref m_Bytes);
             if (size == 0)
             {
                 error = "Compress Failed";
             }
         }
         catch (Exception e)
         {
             error = e.Message;
         }
         finally
         {
             Loom.QueueOnMainThread(param, OnDone);
         }
     });
 }
Beispiel #12
0
        public void Conduction()
        {
            try
            {
                Status = 0;
                Directory.CreateDirectory(System.IO.Path.GetDirectoryName(_dataPath));
                Directory.CreateDirectory(System.IO.Path.GetDirectoryName(_metaPath));
                // open the binary output file
                using (var binaryOutput = File.OpenWrite(_dataPath + ActionUtils.TempFileExtention))
                {
                    Status = 1;
                    MarerHook hook;
                    if (_configuaration.True("rounding"))
                    {
                        if (typeof(TAlgorithm) == typeof(string))
                        {
                            hook = new MarerHook <string>(new RoundingDigitsAlgorithm <string>((IAlgorithm <string>)_algorithm), NoDataConverter.Instance, binaryOutput, _configuaration);
                        }
                        // if not we have to use the converting algorithm
                        else
                        {
                            hook = new MarerHook <string>(new RoundingDigitsAlgorithm <TAlgorithm>(new StringSourceAlgorithm <TAlgorithm>(_converter, _algorithm)), NoDataConverter.Instance, binaryOutput, _configuaration);
                        }
                    }
                    else
                    {
                        hook = new MarerHook <TAlgorithm>(_algorithm, _converter, binaryOutput, _configuaration);
                    }

                    Status = 2;
                    //var hook = new MarerHook<OfcNumber>(algorithm, converter, binaryOutput, _configuaration);

                    Status = 3;
                    var f = false;
                    using (var source = new FileInputStream(_sourcePath))
                    {
                        Status = 4;
                        var lexer  = new OfcLexer(source);
                        var parser = new OfcParser(lexer, hook);
                        hook.PositionProvider = parser;
                        Status = 5;
                        try
                        {
                            parser.Parse();
                            _generatedDataFile = hook.CompressedDataSections.Count != 0;
                        }
                        catch (Exception)
                        {
                            f = true;
                        }
                    }
                    Status = 6;
                    if (f || hook.CompressedDataSections.Count == 0)
                    {
                        _generatedDataFile = false;
                        Directory.CreateDirectory(System.IO.Path.GetDirectoryName(_lzmaPath));
                        Status = 100;
                        using (var source = File.OpenRead(_sourcePath))
                        {
                            Status = 101;
                            using (var outp = File.OpenWrite(_lzmaPath))
                            {
                                Status = 102;
                                LzmaHelper.CompressLzma(source, outp);
                                Status = 103;
                            }
                        }
                        Status = 104;
                    }
                    else
                    {
                        Status = 200;
                        using (var source = File.OpenText(_sourcePath))
                        {
                            Status = 201;
                            using (var outp = File.CreateText(_metaPath + ActionUtils.TempFileExtention))
                            {
                                Status = 202;
                                using (var writer = new MarerWriter(source, outp, hook.CompressedDataSections))
                                {
                                    Status = 203;
                                    writer.Do();
                                    Status = 204;
                                }
                            }
                        }
                        Status = 205;
                        using (var source = File.OpenRead(_metaPath + ActionUtils.TempFileExtention))
                        {
                            Status = 206;
                            using (var outp = File.OpenWrite(_metaPath))
                            {
                                Status = 207;
                                LzmaHelper.CompressLzma(source, outp);
                                Status = 208;
                            }
                        }
                        Status = 209;
                        File.Delete(_metaPath + ActionUtils.TempFileExtention);
                        Status = 210;
                    }
                }

                if (_generatedDataFile)
                {
                    Status = 212;
                    using (var input = File.OpenRead(_dataPath + ActionUtils.TempFileExtention))
                    {
                        Status = 213;
                        using (var output = File.OpenWrite(_dataPath))
                        {
                            Status = 214;
                            LzmaHelper.CompressLzma(input, output);
                            Status = 215;
                        }
                        Status = 216;
                    }
                    Status = 217;
                }
                File.Delete(_dataPath + ActionUtils.TempFileExtention);
            }
            catch (UnauthorizedAccessException)
            {
                Message = "Access failed.";
                throw;
            }
            catch (Exception exception)
            {
                Message = "Internal: " + exception.Message;
                throw;
            }
        }
Beispiel #13
0
        /// <summary>
        /// Retrieves decompressed data block for specified config of included object.
        /// </summary>
        private static byte[] loadRawData(IncludedObjectConfigBase configBase, out long rawDataLength)
        {
            byte[] bytes;
            //
            switch (configBase.IncludeMethod.IncludeMethodKind)
            {
            case IncludeMethodKind.File: {
                string fullPath = configurePath(configBase.IncludeMethod.FileLoadFromPath);
                bytes = File.ReadAllBytes(fullPath);
                break;
            }

            case IncludeMethodKind.Overlay: {
                Assembly executingAssembly = Assembly.GetExecutingAssembly();
                if (executingAssembly.Location == null)
                {
                    throw new InvalidOperationException("Cannot get location of executing assembly.");
                }
                //
                using (FileStream stream = File.OpenRead(executingAssembly.Location)) {
                    if (inititalOverlayOffset == 0)
                    {
                        stream.Seek(-4, SeekOrigin.End);
                        for (int i = 0; i < 3; i++)
                        {
                            inititalOverlayOffset |= (stream.ReadByte() << (8 * (3 - i)));
                        }
                        stream.Seek(0, SeekOrigin.Begin);
                    }
                    //
                    stream.Seek(inititalOverlayOffset + configBase.IncludeMethod.OverlayOffset, SeekOrigin.Begin);
                    bytes = new byte[configBase.IncludeMethod.OverlayLength];
                    if (stream.Read(bytes, 0, bytes.Length) != configBase.IncludeMethod.OverlayLength)
                    {
                        throw new InvalidOperationException("Cannot read overlay. Image may be corrupted.");
                    }
                }
                break;
            }

            case IncludeMethodKind.Resource: {
                string   resourceName      = configBase.IncludeMethod.ResourceName;
                Assembly executingAssembly = Assembly.GetExecutingAssembly();
                Stream   stream            = executingAssembly.GetManifestResourceStream(resourceName);
                if (stream == null)
                {
                    stream = executingAssembly.GetManifestResourceStream(executingAssembly.GetName().Name + "." + resourceName);
                    if (stream == null)
                    {
                        throw new InvalidOperationException("Cannot load resource by name.");
                    }
                }
                using (stream) {
                    bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, unchecked ((int)stream.Length));
                }
                break;
            }

            default: {
                throw new InvalidOperationException("Unknown type.");
            }
            }
            return(LzmaHelper.Decode(bytes, bytes.LongLength, out rawDataLength));
        }
Beispiel #14
0
        public void Conduction()
        {
            try
            {
                if (_isLzma)
                {
                    Status = 1;
                    using (var source = File.OpenRead(_metaPath))
                    {
                        Status = 2;
                        using (var destination = File.OpenWrite(_destination))
                        {
                            Status = 3;
                            LzmaHelper.DecompressFileLzma(source, destination);
                            Status = 4;
                        }
                    }
                    Status = 5;
                }
                else
                {
                    Status = 100;
                    using (var source = File.OpenRead(_metaPath))
                    {
                        Status = 101;
                        using (var destination = File.OpenWrite(_metaPath + ActionUtils.TempFileExtention))
                        {
                            Status = 102;
                            LzmaHelper.DecompressFileLzma(source, destination);
                            Status = 103;
                        }
                    }

                    Status = 104;
                    if (_hasData)
                    {
                        Status = 105;
                        using (var source = File.OpenRead(_dataPath))
                        {
                            Status = 106;
                            using (var destination = File.OpenWrite(_dataPath + ActionUtils.TempFileExtention))
                            {
                                Status = 107;
                                LzmaHelper.DecompressFileLzma(source, destination);
                                Status = 108;
                            }
                        }
                    }

                    Status = 109;
                    using (var output = File.OpenWrite(_destination))
                    {
                        Status = 110;
                        using (var outputText = new StreamWriter(output))
                        {
                            Status = 111;

                            Status = 112;
                            using (var meta = File.OpenText(_metaPath + ActionUtils.TempFileExtention))
                            {
                                Status = 113;
                                if (_hasData)
                                {
                                    Status = 114;
                                    using (var data = File.OpenRead(_dataPath + ActionUtils.TempFileExtention))
                                    {
                                        Status = 115;
                                        using (var reader = new MarerReader <TAlgorithm>(meta, outputText, _algorithm, _converter, data))
                                        {
                                            Status = 116;
                                            reader.Do();
                                            Status = 117;
                                        }
                                        Status = 118;
                                    }
                                    Status = 119;
                                }
                                else
                                {
                                    Status = 130;
                                    using (var reader = new MarerReader <TAlgorithm>(meta, outputText, _algorithm, _converter, null))
                                    {
                                        Status = 131;
                                        reader.Do();
                                        Status = 132;
                                    }
                                    Status = 133;
                                }
                            }
                            Status = 140;
                        }
                        Status = 141;
                    }
                    Status = 142;
                }
            }
            catch (Exception exception)
            {
                Message = "Internal: " + exception.Message;
                throw;
            }
        }