Пример #1
0
 public TarWriter(Stream destination, CompressionInfo compressionInfo)
     : base(ArchiveType.Tar)
 {
     if (!destination.CanWrite)
     {
         throw new ArgumentException("Tars require writable streams.");
     }
     switch (compressionInfo.Type)
     {
         case CompressionType.None:
             break;
         case CompressionType.BZip2:
             {
                 destination = new BZip2Stream(destination, CompressionMode.Compress, false);
             }
             break;
         case CompressionType.GZip:
             {
                 destination = new GZipStream(destination, CompressionMode.Compress, false);
             }
             break;
         default:
             {
                 throw new InvalidFormatException("Tar does not support compression: " + compressionInfo.Type);
             }
     }
     InitalizeStream(destination, false);
 }
        public void SaveGame()
        {
            if (isSaving()) {
                Log.Debug("Already doing a save: " + tempSavePath);
            }

            String name = Guid.NewGuid().ToString();

            Log.Debug("Request save: " + name);

            currentSaveData = new MemoryStream();
            BZip2Stream bzip = new BZip2Stream(currentSaveData, CompressionMode.Compress);

            saveWriter = new EndianBinaryWriter(new BigEndianBitConverter(),bzip, Encoding.UTF8);

            saveWriter.Write((byte)formatVersion);

            saveWriter.Write(Singleton<SimulationManager>.instance.m_metaData.m_CityName);
            saveWriter.Write(Singleton<EconomyManager>.instance.LastCashAmount);
            saveWriter.Write(Singleton<EconomyManager>.instance.LastCashDelta);

            int population = (int)Singleton<DistrictManager>.instance.m_districts.m_buffer[0].m_populationData.m_finalCount;
            saveWriter.Write(population);

            var timeSpan = (Singleton<SimulationManager>.instance.m_currentGameTime - new DateTime(1970, 1, 1, 0, 0, 0));
            saveWriter.Write(timeSpan.TotalSeconds);

            SavePanel savePanel = UIView.library.Get<SavePanel>("SavePanel");
            tempSavePath = Path.Combine(DataLocation.saveLocation, name + ".crp");

            savePanel.SaveGame(name);
            WaitForSave();
        }
Пример #3
0
        /// <summary>
        /// Opens a Reader for Non-seeking usage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IReader Open(Stream stream, Options options = Options.KeepStreamsOpen)
        {
            stream.CheckNotNull("stream");

            RewindableStream rewindableStream = new RewindableStream(stream);
            rewindableStream.StartRecording();
            if (ZipArchive.IsZipFile(rewindableStream, null))
            {
                rewindableStream.Rewind(true);
                return ZipReader.Open(rewindableStream, null, options);
            }
            rewindableStream.Rewind(false);
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return new TarReader(rewindableStream, CompressionType.GZip, options);
                }
                rewindableStream.Rewind(true);
                return GZipReader.Open(rewindableStream, options);
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return new TarReader(rewindableStream, CompressionType.BZip2, options);
                }
            }

            rewindableStream.Rewind(false);
            if (TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                return TarReader.Open(rewindableStream, options);
            }
            rewindableStream.Rewind(false);
            if (RarArchive.IsRarFile(rewindableStream, options))
            {
                rewindableStream.Rewind(true);
                return RarReader.Open(rewindableStream, options);
            }

            throw new InvalidOperationException("Cannot determine compressed stream type.");
        }
Пример #4
0
 public static IReader Open(Stream stream,  Options options)
 {
     Utility.CheckNotNull(stream, "stream");
     RewindableStream stream2 = new RewindableStream(stream);
     stream2.StartRecording();
     if (ZipArchive.IsZipFile(stream2, null))
     {
         stream2.Rewind(true);
         return ZipReader.Open(stream2, null, options);
     }
     stream2.Rewind(false);
     if (GZipArchive.IsGZipFile(stream2))
     {
         stream2.Rewind(false);
         GZipStream stream3 = new GZipStream(stream2, CompressionMode.Decompress);
         if (TarArchive.IsTarFile(stream3))
         {
             stream2.Rewind(true);
             return new TarReader(stream2, CompressionType.GZip, options);
         }
         stream2.Rewind(true);
         return GZipReader.Open(stream2, options);
     }
     stream2.Rewind(false);
     if (BZip2Stream.IsBZip2(stream2))
     {
         stream2.Rewind(false);
         BZip2Stream stream4 = new BZip2Stream(stream2, CompressionMode.Decompress, false, false);
         if (TarArchive.IsTarFile(stream4))
         {
             stream2.Rewind(true);
             return new TarReader(stream2, CompressionType.BZip2, options);
         }
     }
     stream2.Rewind(false);
     if (TarArchive.IsTarFile(stream2))
     {
         stream2.Rewind(true);
         return TarReader.Open(stream2, options);
     }
     stream2.Rewind(false);
     if (!RarArchive.IsRarFile(stream2, options))
     {
         throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar");
     }
     stream2.Rewind(true);
     return RarReader.Open(stream2, options);
 }
Пример #5
0
        internal Stream GetStream()
        {
            factory.BaseStream.Seek(factory.BaseOffset + (long)PackedStreams[0].StartPosition, SeekOrigin.Begin);
            Stream stream = null;
            foreach (var type in GetCompressions())
            {
                switch (type.CompressionType)
                {
                    case CompressionType.LZMA:
                        {
                            if (type.Coder.Method.Length == 3
                                && type.Coder.Method[0] == 3
                                && type.Coder.Method[1] == 1
                                && type.Coder.Method[2] == 1)
                            {
                                stream = new LzmaStream(type.Coder.Properties, factory.BaseStream, (long)PackedStreams[0].PackedSize, (long)UnpackedStreamSizes[0]);
                            }
                            else if (type.Coder.Method.Length == 1
                                && type.Coder.Method[0] == 33)
                            {
                                stream = new LzmaStream(type.Coder.Properties, factory.BaseStream, (long)PackedStreams[0].PackedSize, (long)UnpackedStreamSizes[0]);
                            }
                        }
                        break;
                    case CompressionType.PPMd:
                        {
                            stream = new PpmdStream(new PpmdProperties(type.Coder.Properties), factory.BaseStream, false);
                        }
                        break;
                    case CompressionType.BZip2:
                        {
                            stream = new BZip2Stream(factory.BaseStream, CompressionMode.Decompress, true);
                        }
                        break;
                    case CompressionType.BCJ:
                        {
                            stream = new BCJFilter(false, stream);
                        }
                        break;
                    case CompressionType.BCJ2:
                        {
                            long pos = factory.BaseStream.Position;

                            byte[] data1 = new byte[PackedStreams[1].PackedSize];
                            factory.BaseStream.Seek(factory.BaseOffset + (long)PackedStreams[1].StartPosition, SeekOrigin.Begin);
                            factory.BaseStream.Read(data1, 0, data1.Length);
                            byte[] data2 = new byte[PackedStreams[2].PackedSize];
                            factory.BaseStream.Seek(factory.BaseOffset + (long)PackedStreams[2].StartPosition, SeekOrigin.Begin);
                            factory.BaseStream.Read(data2, 0, data2.Length);
                            byte[] control = new byte[PackedStreams[3].PackedSize];
                            factory.BaseStream.Seek(factory.BaseOffset + (long)PackedStreams[3].StartPosition, SeekOrigin.Begin);
                            factory.BaseStream.Read(control, 0, control.Length);

                            factory.BaseStream.Seek(pos, SeekOrigin.Begin);
                            stream = new BCJ2Filter(control, data1, data2, stream);
                        }
                        break;
                    default:
                        {
                            throw new NotSupportedException("Unknown coder.");
                        }
                }
            }
            return stream;
        }
        public void BeginLoadGame(string[] args)
        {
            Log.Debug("Recived request to load game");

            if (loadingGuildGame)
            {
                Log.Debug("Game already doing a load");
                return;
            }

            Log.Debug("Begin load game:" + args[1]);

            loadingGuildGame = true;
            currentCityId = args[0];

            downloadPath = Path.GetTempPath() + "\\Temp.crp.bzip2";

            using (WebClient wc = new WebClient())
            {

                ProgresView progress = ProgresView.ShowModal("Loading guild game", "Connecting to server");

                progress.OnCanceled += (UIComponent ui) =>
                {
                    wc.CancelAsync();
                };

                wc.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) =>
                {

                    ThreadHelper.dispatcher.Dispatch(() =>
                    {
                        progress.SetMessage("Recieved " + e.BytesReceived + " of " + e.TotalBytesToReceive + " bytes");
                    });
                };
                wc.DownloadFileCompleted += (object sender, System.ComponentModel.AsyncCompletedEventArgs e) =>
                {
                    if (e.Error != null)
                    {
                        Log.Debug("Error loading game: " + e.Error.ToString());
                    }
                    else
                    {

                        ThreadHelper.dispatcher.Dispatch(() =>
                        {
                            progress.SetMessage("Starting game");
                        });

                        BZip2Stream bzip = new BZip2Stream(new FileStream(downloadPath, FileMode.Open), SharpCompress.Compressor.CompressionMode.Decompress);

                        string decompressFile = Path.GetTempPath() + "\\Temp.crp";
                        FileStream decompressedFile = new FileStream(decompressFile, FileMode.OpenOrCreate, FileAccess.Write);

                        StreamUtil.CopyStream(bzip, decompressedFile);

                        decompressedFile.Close();

                        progress.Close();

                        LoadSaveGameFromFile(decompressFile);
                    }
                };

                wc.DownloadFileAsync(new Uri(args[1]), downloadPath);
            }
        }