public static void SendGameData(GameDataSnapshot data, bool async) { var cache = Multiplayer.session.dataSnapshot; var mapsData = new Dictionary <int, byte[]>(cache.mapData); var gameData = cache.gameData; var semiPersistent = cache.semiPersistentData; void Send() { var writer = new ByteWriter(); writer.WriteInt32(mapsData.Count); foreach (var mapData in mapsData) { writer.WriteInt32(mapData.Key); writer.WritePrefixedBytes(GZipStream.CompressBuffer(mapData.Value)); } writer.WritePrefixedBytes(GZipStream.CompressBuffer(gameData)); writer.WritePrefixedBytes(GZipStream.CompressBuffer(semiPersistent)); byte[] data = writer.ToArray(); OnMainThread.Enqueue(() => Multiplayer.Client?.SendFragmented(Packets.Client_WorldDataUpload, data)); }; if (async) { ThreadPool.QueueUserWorkItem(c => Send()); } else { Send(); } }
public static void SendCurrentGameData(bool async) { var mapsData = new Dictionary <int, byte[]>(OnMainThread.cachedMapData); var gameData = OnMainThread.cachedGameData; void Send() { var writer = new ByteWriter(); writer.WriteInt32(mapsData.Count); foreach (var mapData in mapsData) { writer.WriteInt32(mapData.Key); writer.WritePrefixedBytes(GZipStream.CompressBuffer(mapData.Value)); } writer.WritePrefixedBytes(GZipStream.CompressBuffer(gameData)); byte[] data = writer.ToArray(); OnMainThread.Enqueue(() => Multiplayer.Client.SendFragmented(Packets.Client_AutosavedData, data)); }; if (async) { ThreadPool.QueueUserWorkItem(c => Send()); } else { Send(); } }
public void WriteWorldSave(string path, int playerPosX, int playerPosZ) { Directory.CreateDirectory(path); int y = GetRegionAt(playerPosX, playerPosZ).GetChunk(playerPosX % 512, playerPosZ % 512, false).GetHighestBlock(playerPosX % 16, playerPosZ % 16); levelDat = CreateLevelDAT(playerPosX, y + 1, playerPosZ, true); List <byte> levelDATBytes = new List <byte>(); levelDat.WriteToBytes(levelDATBytes, false); var compressedLevelDAT = GZipStream.CompressBuffer(levelDATBytes.ToArray()); File.WriteAllBytes(Path.Combine(path, "level.dat"), compressedLevelDAT); Directory.CreateDirectory(Path.Combine(path, "region")); var options = new ParallelOptions() { MaxDegreeOfParallelism = 4 }; Parallel.ForEach(regions, options, (KeyValuePair <RegionLocation, Region> region) => { string name = $"r.{region.Key.x}.{region.Key.z}.mca"; using (var stream = new FileStream(Path.Combine(path, "region", name), FileMode.Create)) { RegionSerializer.WriteRegionToStream(region.Value, stream, gameVersion); } }); }
public static byte[] Compress(byte[] data) { try { return(GZipStream.CompressBuffer(data)); } catch (Exception) { return(null); } }
public void SetData(byte[] data, bool compress) { IsCompressed = compress; if (compress) { Data = GZipStream.CompressBuffer(data); } else { Data = data; } }
public void SetString(string data, bool compress) { IsCompressed = compress; if (compress) { Data = GZipStream.CompressBuffer(Context.I.DefaultEncoding.GetBytes(data)); } else { Data = Context.I.DefaultEncoding.GetBytes(data); } }
public static byte[] Compress(byte[] data) { byte[] numArray; try { numArray = GZipStream.CompressBuffer(data); } catch (Exception exception) { numArray = null; } return(numArray); }
public byte[] ToData() { int timeSeriesByteLength; int dataSeriesByteLength; int totalByteLength; byte[] data; int offset; int seriesID; if (m_dataSeries.Count == 0) { return(new byte[0]); } timeSeriesByteLength = m_samples * sizeof(long); dataSeriesByteLength = sizeof(int) + (m_samples * sizeof(double)); totalByteLength = sizeof(int) + timeSeriesByteLength + (dataSeriesByteLength * m_dataSeries.Count); data = new byte[totalByteLength]; offset = 0; offset += LittleEndian.CopyBytes(m_samples, data, offset); foreach (DataPoint dataPoint in m_dataSeries[0].DataPoints) { offset += LittleEndian.CopyBytes(dataPoint.Time.Ticks, data, offset); } foreach (DataSeries series in m_dataSeries) { if ((object)series.SeriesInfo != null) { seriesID = series.SeriesInfo.ID; } else { seriesID = 0; } offset += LittleEndian.CopyBytes(seriesID, data, offset); foreach (DataPoint dataPoint in series.DataPoints) { offset += LittleEndian.CopyBytes(dataPoint.Value, data, offset); } } return(GZipStream.CompressBuffer(data)); }
public override ArraySegment <byte> WriteMessage(System.ServiceModel.Channels.Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset) { ArraySegment <byte> buffer = _innerEncoder.WriteMessage(message, maxMessageSize, bufferManager, messageOffset); buffer = GZipStream.CompressBuffer(buffer, bufferManager, messageOffset); // Encode Message byte[] bytes = buffer.Array; bytes = Encoding.EncodePackage(bytes); byte[] bufferedBytes = bufferManager.TakeBuffer(bytes.Length); Array.Copy(bytes, 0, bufferedBytes, 0, bytes.Length); buffer = new ArraySegment <byte>(bufferedBytes); // End Encode return(buffer); }
public static byte[] Compress(byte[] data, string type) { switch (type) { case CompressionAlgorithm.Zlib: return(ZlibStream.CompressBuffer(data)); case CompressionAlgorithm.Deflate: return(DeflateStream.CompressBuffer(data)); case CompressionAlgorithm.GZip: return(GZipStream.CompressBuffer(data)); case CompressionAlgorithm.Lzma: return(SevenZipHelper.Compress(data)); } return(data); }
public byte[] CompressGZipStream(byte[] bytes, CompressionLevel level = CompressionLevel.Default) { return(GZipStream.CompressBuffer(bytes)); //using (var outputStream = new MemoryStream()) //{ // byte[] lengthBytes = BitConverter.GetBytes(bytes.Length); // outputStream.Write(lengthBytes, 0, 4); // using (var zipStream = new GZipStream(outputStream, CompressionMode.Compress, level)) // { // zipStream.Write(bytes, 0, bytes.Length); // //zipStream.Flush(); // } // return outputStream.ToArray(); //} //using (var outputStream = new MemoryStream()) //{ // byte[] lengthBytes = BitConverter.GetBytes(bytes.Length); // outputStream.Write(lengthBytes, 0, 4); // using (var inputStream = new MemoryStream(bytes)) // using (var zipStream = new GZipStream(outputStream, CompressionMode.Compress, level)) // { // inputStream.CopyTo(zipStream); // //zipStream.Flush(); // } // return outputStream.ToArray(); //} //using (var result = new MemoryStream()) //{ // byte[] lengthBytes = BitConverter.GetBytes(bytes.Length); // result.Write(lengthBytes, 0, 4); // using (var compressionStream = new GZipStream(result, CompressionMode.Compress, level)) // { // compressionStream.Write(bytes, 0, bytes.Length); // //compressionStream.Flush(); // } // return result.ToArray(); //} }
public static byte[] WriteServerData(bool writeConfigs) { var data = new ByteWriter(); data.WriteInt32(activeModsSnapshot.Count()); foreach (var m in activeModsSnapshot) { data.WriteString(m.PackageIdNonUnique); data.WriteString(m.Name); data.WriteULong((ulong)m.GetPublishedFileId()); data.WriteByte((byte)m.Source); } data.WriteInt32(modFilesSnapshot.Count()); foreach (var files in modFilesSnapshot) { data.WriteString(files.Key); data.WriteInt32(files.Value.Count); foreach (var file in files.Value.Values) { data.WriteString(file.relPath); data.WriteInt32(file.hash); } } data.WriteBool(writeConfigs); if (writeConfigs) { var configs = GetSyncableConfigContents(activeModsSnapshot.Select(m => m.PackageIdNonUnique)); data.WriteInt32(configs.Count); foreach (var config in configs) { data.WriteString(config.ModId); data.WriteString(config.FileName); data.WriteString(config.Contents); } } return(GZipStream.CompressBuffer(data.ToArray())); }
public void UploadResource(FileInfo fi, string outname) { totalBytes += fi.Length; byte[] bytes = File.ReadAllBytes(fi.FullName); string fingerprint = Hashdeep.GetFingerprint(bytes); gameData.Add(new IncludeFile(outname, fingerprint)); testForResourceCommand.Parameters["@hashdeep"].Value = fingerprint; DbDataReader dbr = testForResourceCommand.ExecuteReader(); bool fileAlreadyKnown = dbr.HasRows; dbr.Close(); if (fileAlreadyKnown) { skippedFiles++; Console.WriteLine("{0} is already known.", outname); return; } byte[] compressed = GZipStream.CompressBuffer(bytes); double percentage = compressed.Length * 100.0 / bytes.Length; bool useCompression = percentage > 90; byte[] whatToWrite = useCompression ? bytes : compressed; uploadResourceCommand.Parameters["@hashdeep"].Value = fingerprint; uploadResourceCommand.Parameters["@originalFilename"].Value = outname; uploadResourceCommand.Parameters["@data"].Value = whatToWrite; int result = uploadResourceCommand.ExecuteNonQuery(); if (result == 0) { throw new Exception(String.Format("Upload of {0} failed.", outname)); } uploadedFiles++; uploadedBytes += whatToWrite.Length; Console.WriteLine("{0} uploaded sucessfully. {2}", outname, result, useCompression ? "Compressed" : "Uncompressed"); }
private static byte[] ToData(List <DataPoint> data, int seriesID, int samples) { var timeSeries = data.Select(dataPoint => new { Time = dataPoint.Time.Ticks, Compressed = false }).ToList(); for (int i = 1; i < timeSeries.Count; i++) { long previousTimestamp = data[i - 1].Time.Ticks; long timestamp = timeSeries[i].Time; long diff = timestamp - previousTimestamp; if (diff >= 0 && diff <= ushort.MaxValue) { timeSeries[i] = new { Time = diff, Compressed = true } } ; } int timeSeriesByteLength = timeSeries.Sum(obj => obj.Compressed ? sizeof(ushort) : sizeof(int) + sizeof(long)); int dataSeriesByteLength = sizeof(int) + (2 * sizeof(double)) + (samples * sizeof(ushort)); int totalByteLength = sizeof(int) + timeSeriesByteLength + dataSeriesByteLength; byte[] result = new byte[totalByteLength]; int offset = 0; offset += LittleEndian.CopyBytes(samples, result, offset); List <int> uncompressedIndexes = timeSeries .Select((obj, Index) => new { obj.Compressed, Index }) .Where(obj => !obj.Compressed) .Select(obj => obj.Index) .ToList(); for (int i = 0; i < uncompressedIndexes.Count; i++) { int index = uncompressedIndexes[i]; int nextIndex = (i + 1 < uncompressedIndexes.Count) ? uncompressedIndexes[i + 1] : timeSeries.Count; offset += LittleEndian.CopyBytes(nextIndex - index, result, offset); offset += LittleEndian.CopyBytes(timeSeries[index].Time, result, offset); for (int j = index + 1; j < nextIndex; j++) { offset += LittleEndian.CopyBytes((ushort)timeSeries[j].Time, result, offset); } } const ushort NaNValue = ushort.MaxValue; const ushort MaxCompressedValue = ushort.MaxValue - 1; double range = data.Select(item => item.Value).Max() - data.Select(item => item.Value).Min(); double decompressionOffset = data.Select(item => item.Value).Min(); double decompressionScale = range / MaxCompressedValue; double compressionScale = (decompressionScale != 0.0D) ? 1.0D / decompressionScale : 0.0D; offset += LittleEndian.CopyBytes(seriesID, result, offset); offset += LittleEndian.CopyBytes(decompressionOffset, result, offset); offset += LittleEndian.CopyBytes(decompressionScale, result, offset); foreach (DataPoint dataPoint in data) { ushort compressedValue = (ushort)Math.Round((dataPoint.Value - decompressionOffset) * compressionScale); if (compressedValue == NaNValue) { compressedValue--; } if (double.IsNaN(dataPoint.Value)) { compressedValue = NaNValue; } offset += LittleEndian.CopyBytes(compressedValue, result, offset); } byte[] returnArray = GZipStream.CompressBuffer(result); returnArray[0] = 0x44; returnArray[1] = 0x33; return(returnArray); } #endregion }
/// <summary> /// Does the v2 serialization. /// </summary> /// <param name="stream"></param> /// <param name="graph"></param> /// <returns></returns> protected override void DoSerialize(LimitedStream stream, DynamicGraphRouterDataSource <CHEdgeData> graph) { // sort the graph. IDynamicGraph <CHEdgeData> sortedGraph = this.SortGraph(graph); // create the regions. SortedDictionary <ulong, List <uint> > regions = new SortedDictionary <ulong, List <uint> >(); for (uint newVertexId = 1; newVertexId < sortedGraph.VertexCount + 1; newVertexId++) { // add to the CHRegions. float latitude, longitude; sortedGraph.GetVertex(newVertexId, out latitude, out longitude); Tile tile = Tile.CreateAroundLocation(new GeoCoordinate( latitude, longitude), _regionZoom); List <uint> regionVertices; if (!regions.TryGetValue(tile.Id, out regionVertices)) { regionVertices = new List <uint>(); regions.Add(tile.Id, regionVertices); } regionVertices.Add(newVertexId); } // serialize the sorted graph. // [START_OF_BLOCKS][START_OF_TAGS][[SIZE_OF_REGION_INDEX][REGION_INDEX][REGIONS]][[SIZE_OF_BLOCK_INDEX][BLOCK_INDEX][BLOCKS]][TAGS] // STRART_OF_BLOCKS: 4bytes // START_OF_STAGS: 4bytes // SIZE_OF_REGION_INDEX: 4bytes // REGION_INDEX: see SIZE_OF_REGION_INDEX // REGIONS: see START_OF_BLOCKS - 4bytes // SIZE_OF_BLOCK_INDEX: 4bytes // BLOCK_INDEX: see SIZE_OF_BLOCK_INDEX. // BLOCKS: from START_OF_BLOCKS + 4bytes + SIZE_OF_BLOCKS_INDEX until END. // serialize regions and build their index. CHVertexRegionIndex chRegionIndex = new CHVertexRegionIndex(); chRegionIndex.LocationIndex = new int[regions.Count]; chRegionIndex.RegionIds = new ulong[regions.Count]; var memoryStream = new MemoryStream(); int regionIdx = 0; foreach (KeyValuePair <ulong, List <uint> > region in regions) { // serialize. CHVertexRegion chRegion = new CHVertexRegion(); chRegion.Vertices = region.Value.ToArray(); _runtimeTypeModel.Serialize(memoryStream, chRegion); // set index. chRegionIndex.LocationIndex[regionIdx] = (int)memoryStream.Position; chRegionIndex.RegionIds[regionIdx] = region.Key; regionIdx++; } stream.Seek(12, SeekOrigin.Begin); // move to beginning of [REGION_INDEX] _runtimeTypeModel.Serialize(stream, chRegionIndex); // write region index. int sizeRegionIndex = (int)(stream.Position - 12); // now at beginning of [REGIONS] memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.WriteTo(stream); // write regions. memoryStream.Dispose(); int startOfBlocks = (int)stream.Position; // now at beginning of [SIZE_OF_BLOCK_INDEX] // serialize the blocks and build their index. memoryStream = new MemoryStream(); List <int> blockLocations = new List <int>(); uint vertexId = 1; while (vertexId < sortedGraph.VertexCount) { uint blockId = vertexId; List <CHArc> blockArcs = new List <CHArc>(); List <CHVertex> blockVertices = new List <CHVertex>(); while (vertexId < blockId + _blockVertexSize && vertexId < sortedGraph.VertexCount + 1) { // create this block. CHVertex chVertex = new CHVertex(); float latitude, longitude; sortedGraph.GetVertex(vertexId, out latitude, out longitude); chVertex.Latitude = latitude; chVertex.Longitude = longitude; chVertex.ArcIndex = (ushort)(blockArcs.Count); foreach (KeyValuePair <uint, CHEdgeData> sortedArc in sortedGraph.GetArcs(vertexId)) { CHArc chArc = new CHArc(); chArc.TargetId = sortedArc.Key; chArc.ShortcutId = sortedArc.Value.ContractedVertexId; chArc.Weight = sortedArc.Value.Weight; chArc.Direction = sortedArc.Value.Direction; chArc.TagsId = sortedArc.Value.Tags; blockArcs.Add(chArc); } chVertex.ArcCount = (ushort)(blockArcs.Count - chVertex.ArcIndex); blockVertices.Add(chVertex); vertexId++; // move to the next vertex. } // create block. CHBlock block = new CHBlock(); block.Arcs = blockArcs.ToArray(); block.Vertices = blockVertices.ToArray(); // TODO: get rid of the list and create an array to begin with. // write blocks. MemoryStream blockStream = new MemoryStream(); _runtimeTypeModel.Serialize(blockStream, block); byte[] compressed = GZipStream.CompressBuffer(blockStream.ToArray()); blockStream.Dispose(); memoryStream.Write(compressed, 0, compressed.Length); blockLocations.Add((int)memoryStream.Position); } CHBlockIndex blockIndex = new CHBlockIndex(); blockIndex.LocationIndex = blockLocations.ToArray(); stream.Seek(startOfBlocks + 4, SeekOrigin.Begin); // move to beginning of [BLOCK_INDEX] _runtimeTypeModel.Serialize(stream, blockIndex); // write region index. int sizeBlockIndex = (int)(stream.Position - (startOfBlocks + 4)); // now at beginning of [BLOCKS] memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.WriteTo(stream); // write blocks. memoryStream.Dispose(); // write tags after blocks. int tagsPosition = (int)stream.Position; TagIndexSerializer.SerializeBlocks(stream, graph.TagsIndex, 100); stream.Seek(startOfBlocks, SeekOrigin.Begin); // move to [SIZE_OF_BLOCK_INDEX] stream.Write(BitConverter.GetBytes(sizeBlockIndex), 0, 4); // write start position of blocks. Now at [SIZE_OF_REGION_INDEX] // write index. stream.Seek(0, SeekOrigin.Begin); // move to beginning stream.Write(BitConverter.GetBytes(startOfBlocks), 0, 4); // write start position of blocks. Now at [SIZE_OF_REGION_INDEX] stream.Write(BitConverter.GetBytes(tagsPosition), 0, 4); stream.Write(BitConverter.GetBytes(sizeRegionIndex), 0, 4); // write size of region index. stream.Flush(); }
// These two data types are supported in DotNetZip, but only if .NET Framework is targeted. //private SelfExtractorFlavor _selfExtractorFlavor; //private SelfExtractorSaveOptions _selfExtractorSaveOptions; public void CallAll() { // These two apis are supported in DotNetZip, but only if .NET Framework is targeted. //_zipFile.SaveSelfExtractor(_string, _selfExtractorFlavor); //_zipFile.SaveSelfExtractor(_string, _selfExtractorSaveOptions); //Project: Ionic.Zip _bZip2InputStream.Close(); _bZip2InputStream.Flush(); _int = _bZip2InputStream.Read(_bytes, _int, _int); _int = _bZip2InputStream.ReadByte(); _long = _bZip2InputStream.Seek(_long, _seekOrigin); _bZip2InputStream.SetLength(_long); _bZip2InputStream.Write(_bytes, _int, _int); _bZip2OutputStream.Close(); _bZip2OutputStream.Flush(); _int = _bZip2OutputStream.Read(_bytes, _int, _int); _long = _bZip2OutputStream.Seek(_long, _seekOrigin); _bZip2OutputStream.SetLength(_long); _bZip2OutputStream.Write(_bytes, _int, _int); _parallelBZip2OutputStream.Close(); _parallelBZip2OutputStream.Flush(); _int = _parallelBZip2OutputStream.Read(_bytes, _int, _int); _long = _parallelBZip2OutputStream.Seek(_long, _seekOrigin); _parallelBZip2OutputStream.SetLength(_long); _parallelBZip2OutputStream.Write(_bytes, _int, _int); _crc32.Combine(_int, _int); _int = _crc32.ComputeCrc32(_int, _byte); _int = _crc32.GetCrc32(_stream); _int = _crc32.GetCrc32AndCopy(_stream, _stream); _crc32.Reset(); _crc32.SlurpBlock(_bytes, _int, _int); _crc32.UpdateCRC(_byte); _crc32.UpdateCRC(_byte, _int); _crcCalculatorStream.Close(); _crcCalculatorStream.Flush(); _int = _crcCalculatorStream.Read(_bytes, _int, _int); _long = _crcCalculatorStream.Seek(_long, _seekOrigin); _crcCalculatorStream.SetLength(_long); _crcCalculatorStream.Write(_bytes, _int, _int); _zipEntriesCollection = _fileSelector.SelectEntries(_zipFile); _zipEntriesCollection = _fileSelector.SelectEntries(_zipFile, _string); _stringsCollection = _fileSelector.SelectFiles(_string); _stringsReadOnly = _fileSelector.SelectFiles(_string, _bool); _string = _fileSelector.ToString(); _bool = _comHelper.CheckZip(_string); _bool = _comHelper.CheckZipPassword(_string, _string); _comHelper.FixZipDirectory(_string); _string = _comHelper.GetZipLibraryVersion(); _bool = _comHelper.IsZipFile(_string); _bool = _comHelper.IsZipFileWithExtract(_string); _countingStream.Adjust(_long); _countingStream.Flush(); _int = _countingStream.Read(_bytes, _int, _int); _long = _countingStream.Seek(_long, _seekOrigin); _countingStream.SetLength(_long); _countingStream.Write(_bytes, _int, _int); _zipEntry.Extract(); _zipEntry.Extract(_extractExistingFileAction); _zipEntry.Extract(_string); _zipEntry.Extract(_string, _extractExistingFileAction); _zipEntry.Extract(_stream); _zipEntry.ExtractWithPassword(_extractExistingFileAction, _string); _zipEntry.ExtractWithPassword(_string); _zipEntry.ExtractWithPassword(_string, _extractExistingFileAction, _string); _zipEntry.ExtractWithPassword(_string, _string); _zipEntry.ExtractWithPassword(_stream, _string); _crcCalculatorStream = _zipEntry.OpenReader(); _crcCalculatorStream = _zipEntry.OpenReader(_string); _zipEntry.SetEntryTimes(_datetime, _datetime, _datetime); _string = _zipEntry.ToString(); _zipEntry = _zipFile.AddDirectory(_string); _zipEntry = _zipFile.AddDirectory(_string, _string); _zipEntry = _zipFile.AddDirectoryByName(_string); _zipEntry = _zipFile.AddEntry(_string, _bytes); _zipEntry = _zipFile.AddEntry(_string, _openDelegate, _closeDelegate); _zipEntry = _zipFile.AddEntry(_string, _writeDelegate); _zipEntry = _zipFile.AddEntry(_string, _string); _zipEntry = _zipFile.AddEntry(_string, _string, _encoding); _zipEntry = _zipFile.AddEntry(_string, _stream); _zipEntry = _zipFile.AddFile(_string); _zipEntry = _zipFile.AddFile(_string, _string); _zipFile.AddFiles(_strings); _zipFile.AddFiles(_strings, _bool, _string); _zipFile.AddFiles(_strings, _string); _zipEntry = _zipFile.AddItem(_string); _zipEntry = _zipFile.AddItem(_string, _string); _zipFile.AddSelectedFiles(_string); _zipFile.AddSelectedFiles(_string, _bool); _zipFile.AddSelectedFiles(_string, _string); _zipFile.AddSelectedFiles(_string, _string, _bool); _zipFile.AddSelectedFiles(_string, _string, _string); _zipFile.AddSelectedFiles(_string, _string, _string, _bool); _bool = _zipFile.ContainsEntry(_string); _zipFile.Dispose(); _zipFile.ExtractAll(_string); _zipFile.ExtractAll(_string, _extractExistingFileAction); _zipFile.ExtractSelectedEntries(_string); _zipFile.ExtractSelectedEntries(_string, _extractExistingFileAction); _zipFile.ExtractSelectedEntries(_string, _string); _zipFile.ExtractSelectedEntries(_string, _string, _string); _zipFile.ExtractSelectedEntries(_string, _string, _string, _extractExistingFileAction); _enumerator = _zipFile.GetNewEnum(); _zipFile.Initialize(_string); _zipFile.RemoveEntries(_zipEntriesCollection); _zipFile.RemoveEntries(_stringsCollection); _zipFile.RemoveEntry(_zipEntry); _zipFile.RemoveEntry(_string); _int = _zipFile.RemoveSelectedEntries(_string); _int = _zipFile.RemoveSelectedEntries(_string, _string); _zipFile.Save(); _zipFile.Save(_string); _zipFile.Save(_stream); _zipEntriesCollection = _zipFile.SelectEntries(_string); _zipEntriesCollection = _zipFile.SelectEntries(_string, _string); _string = _zipFile.ToString(); _zipEntry = _zipFile.UpdateDirectory(_string); _zipEntry = _zipFile.UpdateDirectory(_string, _string); _zipEntry = _zipFile.UpdateEntry(_string, _bytes); _zipEntry = _zipFile.UpdateEntry(_string, _openDelegate, _closeDelegate); _zipEntry = _zipFile.UpdateEntry(_string, _writeDelegate); _zipEntry = _zipFile.UpdateEntry(_string, _string); _zipEntry = _zipFile.UpdateEntry(_string, _string, _encoding); _zipEntry = _zipFile.UpdateEntry(_string, _stream); _zipEntry = _zipFile.UpdateFile(_string); _zipFile.UpdateFile(_string, _string); _zipFile.UpdateFiles(_strings); _zipFile.UpdateFiles(_strings, _string); _zipFile.UpdateItem(_string); _zipFile.UpdateItem(_string, _string); _zipFile.UpdateSelectedFiles(_string, _string, _string, _bool); _zipInputStream.Flush(); _zipEntry = _zipInputStream.GetNextEntry(); _int = _zipInputStream.Read(_bytes, _int, _int); _long = _zipInputStream.Seek(_long, _seekOrigin); _zipInputStream.SetLength(_long); _string = _zipInputStream.ToString(); _zipInputStream.Write(_bytes, _int, _int); _bool = _zipOutputStream.ContainsEntry(_string); _zipOutputStream.Flush(); _zipEntry = _zipOutputStream.PutNextEntry(_string); _int = _zipOutputStream.Read(_bytes, _int, _int); _long = _zipOutputStream.Seek(_long, _seekOrigin); _zipOutputStream.SetLength(_long); _string = _zipOutputStream.ToString(); _zipOutputStream.Write(_bytes, _int, _int); _deflateStream.Flush(); _int = _deflateStream.Read(_bytes, _int, _int); _long = _deflateStream.Seek(_long, _seekOrigin); _deflateStream.SetLength(_long); _deflateStream.Write(_bytes, _int, _int); _gZipStream.Flush(); _int = _gZipStream.Read(_bytes, _int, _int); _long = _gZipStream.Seek(_long, _seekOrigin); _gZipStream.SetLength(_long); _gZipStream.Write(_bytes, _int, _int); _parallelDeflateOutputStream.Close(); _parallelDeflateOutputStream.Flush(); _int = _parallelDeflateOutputStream.Read(_bytes, _int, _int); _parallelDeflateOutputStream.Reset(_stream); _long = _parallelDeflateOutputStream.Seek(_long, _seekOrigin); _parallelDeflateOutputStream.SetLength(_long); _parallelDeflateOutputStream.Write(_bytes, _int, _int); // Static _bool = ZipFile.CheckZip(_string); _bool = ZipFile.CheckZip(_string, _bool, _textWriter); _bool = ZipFile.CheckZipPassword(_string, _string); ZipFile.FixZipDirectory(_string); _bool = ZipFile.IsZipFile(_string); _bool = ZipFile.IsZipFile(_string, _bool); _bool = ZipFile.IsZipFile(_stream, _bool); _zipFile = ZipFile.Read(_string); _zipFile = ZipFile.Read(_string, _readOptions); _zipFile = ZipFile.Read(_stream); _zipFile = ZipFile.Read(_stream, _readOptions); _uint = Adler.Adler32(_uint, _bytes, _int, _int); _bytes = DeflateStream.CompressBuffer(_bytes); _bytes = DeflateStream.CompressString(_string); _bytes = DeflateStream.UncompressBuffer(_bytes); _string = DeflateStream.UncompressString(_bytes); _bytes = GZipStream.CompressBuffer(_bytes); _bytes = GZipStream.CompressString(_string); _bytes = GZipStream.UncompressBuffer(_bytes); _string = GZipStream.UncompressString(_bytes); _bytes = ZlibStream.CompressBuffer(_bytes); _bytes = ZlibStream.CompressString(_string); _bytes = ZlibStream.UncompressBuffer(_bytes); _string = ZlibStream.UncompressString(_bytes); }
/// <summary> /// Serializes the actual data. /// </summary> /// <param name="typeModel"></param> /// <param name="data"></param> /// <param name="boxes"></param> /// <returns></returns> protected override byte[] Serialize(RuntimeTypeModel typeModel, List <Scene2DEntry> data, List <BoxF2D> boxes) { var icons = new List <Icon2DEntry>(); var images = new List <Image2DEntry>(); var lines = new List <Line2DEntry>(); var points = new List <Point2DEntry>(); var polygons = new List <Polygon2DEntry>(); var texts = new List <Text2DEntry>(); var lineTexts = new List <LineText2DEntry>(); // build the collection object. var collection = new PrimitivesCollection(); for (int idx = 0; idx < data.Count; idx++) { IScene2DPrimitive primitive = data[idx].Scene2DPrimitive; if (primitive is Icon2D) { Icon2D icon = primitive as Icon2D; icons.Add(Icon2DEntry.From(data[idx].Id, icon, _styleIndex.AddStyle(icon, data[idx].Layer))); } else if (primitive is Image2D) { Image2D image = primitive as Image2D; images.Add(Image2DEntry.From(data[idx].Id, image, _styleIndex.AddStyle(image, data[idx].Layer))); } else if (primitive is Line2D) { Line2D line = primitive as Line2D; lines.Add(Line2DEntry.From(data[idx].Id, line, _styleIndex.AddStyle(line, data[idx].Layer))); } else if (primitive is Point2D) { Point2D point = primitive as Point2D; points.Add(Point2DEntry.From(data[idx].Id, point, _styleIndex.AddStyle(point, data[idx].Layer))); } else if (primitive is Polygon2D) { Polygon2D polygon = primitive as Polygon2D; polygons.Add(Polygon2DEntry.From(data[idx].Id, polygon, _styleIndex.AddStyle(polygon, data[idx].Layer))); } else if (primitive is Text2D) { Text2D text = primitive as Text2D; texts.Add(Text2DEntry.From(data[idx].Id, text, _styleIndex.AddStyle(text, data[idx].Layer))); } else if (primitive is LineText2D) { LineText2D lineText = primitive as LineText2D; lineTexts.Add(LineText2DEntry.From(data[idx].Id, lineText, _styleIndex.AddStyle(lineText, data[idx].Layer))); } else { throw new Exception("Primitive type not supported by serializer."); } } collection.Icons = icons.ToArray(); collection.Images = images.ToArray(); collection.Lines = lines.ToArray(); collection.Points = points.ToArray(); collection.Polygons = polygons.ToArray(); collection.Texts = texts.ToArray(); collection.LineTexts = lineTexts.ToArray(); // create the memory stream. var stream = new MemoryStream(); typeModel.Serialize(stream, collection); if (!_compress) { return(stream.ToArray()); } return(GZipStream.CompressBuffer(stream.ToArray())); }
/// <summary> /// Does the v1 serialization. /// </summary> /// <param name="stream"></param> /// <param name="graph"></param> /// <returns></returns> protected override void DoSerialize(LimitedStream stream, DynamicGraphRouterDataSource <LiveEdge> graph) { // create an index per tile. var dataPerTile = new Dictionary <Tile, UnserializedTileData>(); for (uint vertex = 1; vertex < graph.VertexCount + 1; vertex++) { // loop over all vertices and serialize all into the correct tile. float latitude, longitude; if (graph.GetVertex(vertex, out latitude, out longitude)) { // the vertex was found. // build the correct tile. Tile tile = Tile.CreateAroundLocation(new GeoCoordinate(latitude, longitude), Zoom); UnserializedTileData serializableGraphTile; if (!dataPerTile.TryGetValue(tile, out serializableGraphTile)) { // create the new tile. serializableGraphTile = new UnserializedTileData(); serializableGraphTile.Ids = new List <uint>(); serializableGraphTile.Latitude = new List <ushort>(); serializableGraphTile.Longitude = new List <ushort>(); serializableGraphTile.StringTable = new Dictionary <string, int>(); serializableGraphTile.Arcs = new List <SerializableGraphArcs>(); dataPerTile.Add(tile, serializableGraphTile); } // create short latitude/longitude. serializableGraphTile.Ids.Add(vertex); serializableGraphTile.Latitude.Add((ushort)(((tile.TopLeft.Latitude - latitude) / tile.Box.DeltaLat) * ushort.MaxValue)); serializableGraphTile.Longitude.Add((ushort)(((longitude - tile.TopLeft.Longitude) / tile.Box.DeltaLon) * ushort.MaxValue)); // get the arcs. KeyValuePair <uint, LiveEdge>[] arcs = graph.GetArcs(vertex); // serialize the arcs. if (arcs != null && arcs.Length > 0) { var serializableGraphArcs = new SerializableGraphArcs(); serializableGraphArcs.DestinationId = new uint[arcs.Length]; serializableGraphArcs.Forward = new bool[arcs.Length]; serializableGraphArcs.TileX = new int[arcs.Length]; serializableGraphArcs.TileY = new int[arcs.Length]; serializableGraphArcs.Tags = new SerializableTags[arcs.Length]; for (int idx = 0; idx < arcs.Length; idx++) { KeyValuePair <uint, LiveEdge> arc = arcs[idx]; // get destination tile. if (graph.GetVertex(arc.Key, out latitude, out longitude)) { // the destionation was found. Tile destinationTile = Tile.CreateAroundLocation( new GeoCoordinate(latitude, longitude), Zoom); serializableGraphArcs.DestinationId[idx] = arc.Key; serializableGraphArcs.TileX[idx] = destinationTile.X; serializableGraphArcs.TileY[idx] = destinationTile.Y; serializableGraphArcs.Forward[idx] = arc.Value.Forward; // get the tags. TagsCollectionBase tagsCollection = graph.TagsIndex.Get(arc.Value.Tags); if (tagsCollection != null) { serializableGraphArcs.Tags[idx] = new SerializableTags(); serializableGraphArcs.Tags[idx].Keys = new int[tagsCollection.Count]; serializableGraphArcs.Tags[idx].Values = new int[tagsCollection.Count]; int tagsIndex = 0; foreach (var tag in tagsCollection) { int key; if (!serializableGraphTile.StringTable.TryGetValue( tag.Key, out key)) { // string not yet in string table. key = serializableGraphTile.StringTable.Count; serializableGraphTile.StringTable.Add(tag.Key, key); } int value; if (!serializableGraphTile.StringTable.TryGetValue( tag.Value, out value)) { // string not yet in string table. value = serializableGraphTile.StringTable.Count; serializableGraphTile.StringTable.Add(tag.Value, value); } serializableGraphArcs.Tags[idx].Keys[tagsIndex] = key; serializableGraphArcs.Tags[idx].Values[tagsIndex] = value; tagsIndex++; } } } } serializableGraphTile.Arcs.Add(serializableGraphArcs); } } } // LAYOUT OF V2: {HEADER}{compressionflag(1byte)}{#tiles(4byte)}{tilesMetaEnd(8byte)}{tiles-meta-data-xxxxxxx}{tiles-data} // {HEADER} : already written before this method. // {#tiles(4byte)} : the number of tiles in this file (calculate the offset of the {tiles-data} // section using (TileMetaSize * dataPerTile.Count + 4 + 8) // {tilesMetaEnd(8byte)} : the end of the meta tiles. // {tiles-meta-data-xxxxxxx} : the serialized tile metadata. // {tiles-data} : the actual tile data. // calculate the space needed for the tile offset. const long tileMetaOffset = 1 + 4 + 8; long tileOffset = TileMetaSize * dataPerTile.Count + tileMetaOffset; // all tile metadata + a tile count + tags offset. // build the tile metadata while writing the tile data. stream.Seek(tileOffset, SeekOrigin.Begin); var metas = new SerializableGraphTileMetas(); metas.Length = new int[dataPerTile.Count]; metas.Offset = new long[dataPerTile.Count]; metas.TileX = new int[dataPerTile.Count]; metas.TileY = new int[dataPerTile.Count]; int metasIndex = 0; foreach (var unserializedTileData in dataPerTile) { // create the tile meta. metas.TileX[metasIndex] = unserializedTileData.Key.X; metas.TileY[metasIndex] = unserializedTileData.Key.Y; metas.Offset[metasIndex] = stream.Position; // create the tile. var serializableGraphTile = new SerializableGraphTile(); serializableGraphTile.Arcs = unserializedTileData.Value.Arcs.ToArray(); serializableGraphTile.Ids = unserializedTileData.Value.Ids.ToArray(); serializableGraphTile.Latitude = unserializedTileData.Value.Latitude.ToArray(); serializableGraphTile.Longitude = unserializedTileData.Value.Longitude.ToArray(); serializableGraphTile.StringTable = new string[unserializedTileData.Value.StringTable.Count]; foreach (var stringEntry in unserializedTileData.Value.StringTable) { serializableGraphTile.StringTable[stringEntry.Value] = stringEntry.Key; } // serialize the tile. if (!_compress) { // compresses the file. _runtimeTypeModel.Serialize(stream, serializableGraphTile); } else { // first compress the data, then write. var uncompressed = new MemoryStream(); _runtimeTypeModel.Serialize(uncompressed, serializableGraphTile); var uncompressedBuffer = uncompressed.ToArray(); byte[] compressed = GZipStream.CompressBuffer(uncompressedBuffer); stream.Write(compressed, 0, compressed.Length); } // calculate the length of the data that was just serialized. metas.Length[metasIndex] = (int)(stream.Position - metas.Offset[metasIndex]); metasIndex++; } // serialize all tile meta data. stream.Seek(tileMetaOffset, SeekOrigin.Begin); _runtimeTypeModel.Serialize(stream, metas); long tileMetaEnd = stream.Position; // save the meta and. // save all the offsets. stream.Seek(0, SeekOrigin.Begin); byte[] compressionFlag = new[] { (byte)(_compress ? 1 : 0) }; stream.Write(compressionFlag, 0, 1); byte[] tileCountBytes = BitConverter.GetBytes(metas.TileX.Length); stream.Write(tileCountBytes, 0, tileCountBytes.Length); // 4 bytes byte[] tileMetaEndBytes = BitConverter.GetBytes(tileMetaEnd); stream.Write(tileMetaEndBytes, 0, tileMetaEndBytes.Length); // 8 bytes stream.Flush(); }
public IEnumerator SavePlayerPrefs(bool skip = false) { if (!allowSavePrefs || setting.disablePlayerPrefs) { yield break; } var act = win.act; SaveStrings(); if (!skip) { if (guest) { yield break; } } //save= totalSeconds; win.ShowWindow(delegate { win.showBackButton = false; Label("Saving stats "); if (BackButtonLeft()) { ShowWindowNoBack(act); act = null; } }, act, true); yield return(null); StringBuilder sb = new StringBuilder(); byte[] array; using (var ms = new BinaryWriter()) { //#if !UNITY_WP8 ms.Write(_DefinePrefsTime); ms.Write(totalSeconds.ToString()); //#endif var forb = new List <string> { "password", "Enc", _DefinePrefsTime }; var plname = playerName; foreach (string key in playerPrefKeys) { if (!forb.Contains(key)) { if (ResLoader.isEditor && !key.StartsWith(plname)) { continue; } var value = PlayerPrefsGetString(key); //if (Key.EndsWith("reputation")) //print("reputation:" + value); if (value.Length < 200 && isDebug) { sb.AppendLine(key + "\t\t" + value); } if (value.Length > MaxLength || key.Length > MaxLength) { Debug.LogError(string.Format("too big value {0} {1}", key, value)); continue; } ms.Write(key); ms.Write(value); } } array = GZipStream.CompressBuffer(ms.ToArray()); Xor(array); print("saving stats_______ :" + ms.Length + " " + array.Length); if (isDebug) { print(sb.ToString()); } } var w = DownloadAcc("savePrefs2", null, true, "file", array); yield return(w); if (act != null) { win.ShowWindow(act); } if (!w.text.StartsWith("prefs uploaded")) { Popup(_Loader.lastError = (w.text + w.error), MenuWindow); } }
public byte[] zipData(byte[] data) { return(GZipStream.CompressBuffer(data)); }
public static byte[] GZipCompress(byte[] bytes) { return(GZipStream.CompressBuffer(bytes)); }
/// <summary> /// Serializes the actual data. /// </summary> /// <param name="typeModel"></param> /// <param name="data"></param> /// <param name="boxes"></param> /// <returns></returns> protected override byte[] Serialize(RuntimeTypeModel typeModel, List <Scene2DEntry> data, List <BoxF2D> boxes) { var icons = new List <Icon2DEntry>(); var images = new List <Image2DEntry>(); var lines = new List <Line2DEntry>(); var points = new List <Point2DEntry>(); var polygons = new List <Polygon2DEntry>(); var texts = new List <Text2DEntry>(); var lineTexts = new List <LineText2DEntry>(); // build the collection object. var collection = new PrimitivesCollection(); for (int idx = 0; idx < data.Count; idx++) { IScene2DPrimitive primitive = data[idx].Scene2DPrimitive; if (primitive is Icon2D) { icons.Add(new Icon2DEntry() { Primitive = (Icon2D)primitive, Id = data[idx].Id, Layer = data[idx].Layer }); } else if (primitive is Image2D) { images.Add(new Image2DEntry() { Primitive = (Image2D)primitive, Id = data[idx].Id, Layer = data[idx].Layer }); } else if (primitive is Line2D) { lines.Add(new Line2DEntry() { Primitive = (Line2D)primitive, Id = data[idx].Id, Layer = data[idx].Layer }); } else if (primitive is Point2D) { points.Add(new Point2DEntry() { Primitive = (Point2D)primitive, Id = data[idx].Id, Layer = data[idx].Layer }); } else if (primitive is Polygon2D) { polygons.Add(new Polygon2DEntry() { Primitive = (Polygon2D)primitive, Id = data[idx].Id, Layer = data[idx].Layer }); } else if (primitive is Text2D) { texts.Add(new Text2DEntry() { Primitive = (Text2D)primitive, Id = data[idx].Id, Layer = data[idx].Layer }); } else if (primitive is LineText2D) { lineTexts.Add(new LineText2DEntry() { Primitive = (LineText2D)primitive, Id = data[idx].Id, Layer = data[idx].Layer }); } else { throw new Exception("Primitive type not supported by serializer."); } } collection.Icons = icons.ToArray(); collection.Images = images.ToArray(); collection.Lines = lines.ToArray(); collection.Points = points.ToArray(); collection.Polygons = polygons.ToArray(); collection.Texts = texts.ToArray(); collection.LineTexts = lineTexts.ToArray(); // create the memory stream. var stream = new MemoryStream(); typeModel.Serialize(stream, collection); if (!_compress) { return(stream.ToArray()); } return(GZipStream.CompressBuffer(stream.ToArray())); }
/// <summary> /// Serializes one leaf. /// </summary> /// <param name="typeModel"></param> /// <param name="data"></param> /// <param name="boxes"></param> /// <returns></returns> protected override byte[] Serialize(RuntimeTypeModel typeModel, List <SceneObject> data, List <BoxF2D> boxes) { int scaleFactor = _scaleFactor; Dictionary <uint, int> addedPoint = new Dictionary <uint, int>(); Dictionary <uint, int> addedPoints = new Dictionary <uint, int>(); SceneObjectBlock leafData = new SceneObjectBlock(); //leafData.PointsIndexes = new List<int>(); leafData.PointsX = new List <long>(); leafData.PointsY = new List <long>(); leafData.IconPointId = new List <int>(); leafData.IconImageId = new List <uint>(); leafData.LinePointsId = new List <int>(); leafData.LineStyleId = new List <uint>(); leafData.LineTextPointsId = new List <int>(); leafData.LineTextStyleId = new List <uint>(); leafData.LineTextText = new List <string>(); leafData.PointPointId = new List <int>(); leafData.PointStyleId = new List <uint>(); leafData.PolygonPointsId = new List <int>(); leafData.PolygonStyleId = new List <uint>(); leafData.TextPointPointId = new List <int>(); leafData.TextPointStyleId = new List <uint>(); leafData.TextPointText = new List <string>(); foreach (SceneObject sceneObject in data) { int geoId = -1; OsmSharp.UI.Renderer.Scene.Scene2D.ScenePoint point; OsmSharp.UI.Renderer.Scene.Scene2D.ScenePoints points; switch (sceneObject.Enum) { case SceneObjectType.IconObject: // get point. point = _scene.GetPoint(sceneObject.GeoId); // set point data and keep id. if (!addedPoint.TryGetValue(sceneObject.GeoId, out geoId)) { // the point was not added yet. geoId = leafData.PointsX.Count; //leafData.PointsIndexes.Add(geoId); leafData.PointsX.Add((long)(scaleFactor * point.X)); leafData.PointsY.Add((long)(scaleFactor * point.Y)); addedPoint.Add(sceneObject.GeoId, geoId); } leafData.IconPointId.Add(geoId); // add point. // add image id. leafData.IconImageId.Add(sceneObject.StyleId); break; case SceneObjectType.PointObject: // get point. point = _scene.GetPoint(sceneObject.GeoId); // set point data and keep id. if (!addedPoint.TryGetValue(sceneObject.GeoId, out geoId)) { // the point was not added yet. geoId = leafData.PointsX.Count; leafData.PointsX.Add((long)(scaleFactor * point.X)); leafData.PointsY.Add((long)(scaleFactor * point.Y)); //leafData.PointsIndexes.Add(leafData.PointsY.Count); addedPoint.Add(sceneObject.GeoId, geoId); } leafData.PointPointId.Add(geoId); // add point style. leafData.PointStyleId.Add(sceneObject.StyleId); break; case SceneObjectType.TextObject: // get point. point = _scene.GetPoint(sceneObject.GeoId); // set point data and keep id. if (!addedPoint.TryGetValue(sceneObject.GeoId, out geoId)) { // the point was not added yet. geoId = leafData.PointsX.Count; leafData.PointsX.Add((long)(scaleFactor * point.X)); leafData.PointsY.Add((long)(scaleFactor * point.Y)); //leafData.PointsIndexes.Add(leafData.PointsY.Count); addedPoint.Add(sceneObject.GeoId, geoId); } leafData.TextPointPointId.Add(geoId); // add point style. leafData.TextPointStyleId.Add(sceneObject.StyleId); // add text. leafData.TextPointText.Add( _scene.GetText((sceneObject as SceneTextObject).TextId)); break; case SceneObjectType.LineObject: // get points. points = _scene.GetPoints(sceneObject.GeoId); // set points data and keep id. if (!addedPoints.TryGetValue(sceneObject.GeoId, out geoId)) { // the point was not added yet. geoId = leafData.PointsX.Count; leafData.PointsX.AddRange(points.X.ConvertToLongArray(scaleFactor)); leafData.PointsY.AddRange(points.Y.ConvertToLongArray(scaleFactor)); //leafData.PointsIndexes.Add(leafData.PointsY.Count); addedPoints.Add(sceneObject.GeoId, geoId); } leafData.LinePointsId.Add(geoId); // add point style. leafData.LineStyleId.Add(sceneObject.StyleId); break; case SceneObjectType.LineTextObject: // get points. points = _scene.GetPoints(sceneObject.GeoId); // set points data and keep id. if (!addedPoints.TryGetValue(sceneObject.GeoId, out geoId)) { // the point was not added yet. geoId = leafData.PointsX.Count; leafData.PointsX.AddRange(points.X.ConvertToLongArray(scaleFactor)); leafData.PointsY.AddRange(points.Y.ConvertToLongArray(scaleFactor)); //leafData.PointsIndexes.Add(leafData.PointsY.Count); addedPoints.Add(sceneObject.GeoId, geoId); } leafData.LineTextPointsId.Add(geoId); // add point style. leafData.LineTextStyleId.Add(sceneObject.StyleId); // add text. leafData.LineTextText.Add( _scene.GetText((sceneObject as SceneLineTextObject).TextId)); break; case SceneObjectType.PolygonObject: // get points. points = _scene.GetPoints(sceneObject.GeoId); // set points data and keep id. if (!addedPoints.TryGetValue(sceneObject.GeoId, out geoId)) { // the point was not added yet. geoId = leafData.PointsX.Count; leafData.PointsX.AddRange(points.X.ConvertToLongArray(scaleFactor)); leafData.PointsY.AddRange(points.Y.ConvertToLongArray(scaleFactor)); //leafData.PointsIndexes.Add(leafData.PointsY.Count); addedPoints.Add(sceneObject.GeoId, geoId); } leafData.PolygonPointsId.Add(geoId); // add point style. leafData.PolygonStyleId.Add(sceneObject.StyleId); break; } } // get the mins. leafData.PointsXMin = leafData.PointsX.Min(); leafData.PointsYMin = leafData.PointsY.Min(); // encode. for (int idx = 0; idx < leafData.PointsX.Count; idx++) { leafData.PointsX[idx] = leafData.PointsX[idx] - leafData.PointsXMin; leafData.PointsY[idx] = leafData.PointsY[idx] - leafData.PointsYMin; } // serialize. MemoryStream stream = new MemoryStream(); typeModel.Serialize(stream, leafData); byte[] serializedData = stream.ToArray(); stream.Dispose(); if (_compressed) { // compress. serializedData = GZipStream.CompressBuffer(serializedData); } return(serializedData); }
/// <summary> /// Does the v2 serialization. /// </summary> /// <param name="stream"></param> /// <param name="graph"></param> /// <returns></returns> protected override void DoSerialize(LimitedStream stream, DynamicGraphRouterDataSource <CHEdgeData> graph) { // sort the graph. var sortedGraph = graph; // create the regions. var regions = new SortedDictionary <ulong, List <uint> >(); for (uint newVertexId = 1; newVertexId < sortedGraph.VertexCount + 1; newVertexId++) { // add to the CHRegions. float latitude, longitude; sortedGraph.GetVertex(newVertexId, out latitude, out longitude); var tile = Tile.CreateAroundLocation(new GeoCoordinate( latitude, longitude), _regionZoom); List <uint> regionVertices; if (!regions.TryGetValue(tile.Id, out regionVertices)) { regionVertices = new List <uint>(); regions.Add(tile.Id, regionVertices); } regionVertices.Add(newVertexId); } // serialize the sorted graph. // [START_OF_BLOCKS][START_OF_SHAPES][START_OF_REVERSE][START_OF_TAGS][[SIZE_OF_REGION_INDEX][REGION_INDEX][REGIONS]] // [[SIZE_OF_BLOCK_INDEX][BLOCK_INDEX][BLOCKS]] // [[SIZE_OF_SHAPE_INDEX][SHAPE_INDEX][SHAPES]] // [[SIZE_OF_REVERSE_INDEX][REVERSE_INDEX][REVERSE]] // [TAGS] // START_OF_BLOCKS: 4bytes // START_OF_SHAPES: 4bytes // START_OF_STAGS: 4bytes // START_OF_REVERSE 4bytes // SIZE_OF_REGION_INDEX: 4bytes // REGION_INDEX: see SIZE_OF_REGION_INDEX // REGIONS: see START_OF_BLOCKS - 4bytes // SIZE_OF_BLOCK_INDEX: 4bytes // BLOCK_INDEX: see SIZE_OF_BLOCK_INDEX. // BLOCKS: from START_OF_BLOCKS + 4bytes + SIZE_OF_BLOCKS_INDEX until END. // serialize regions and build their index. var chRegionIndex = new CHVertexRegionIndex(); chRegionIndex.LocationIndex = new int[regions.Count]; chRegionIndex.RegionIds = new ulong[regions.Count]; var memoryStream = new MemoryStream(); int regionIdx = 0; foreach (var region in regions) { // serialize. var chRegion = new CHVertexRegion(); chRegion.Vertices = region.Value.ToArray(); _runtimeTypeModel.Serialize(memoryStream, chRegion); // set index. chRegionIndex.LocationIndex[regionIdx] = (int)memoryStream.Position; chRegionIndex.RegionIds[regionIdx] = region.Key; regionIdx++; } stream.Seek(INDEX_SIZE, SeekOrigin.Begin); // move to beginning of [REGION_INDEX] _runtimeTypeModel.Serialize(stream, chRegionIndex); // write region index. var sizeRegionIndex = (int)(stream.Position - INDEX_SIZE); // now at beginning of [REGIONS] memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.WriteTo(stream); // write regions. memoryStream.Dispose(); var startOfBlocks = (int)stream.Position; // now at beginning of [SIZE_OF_BLOCK_INDEX] // serialize the blocks and build their index. memoryStream = new MemoryStream(); var blocks = new List <CHBlock>(); var blockLocations = new List <int>(); var blockShapeLocations = new List <int>(); var blockReverseLocations = new List <int>(); var blockShapes = new List <CHBlockCoordinates>(); var reverseNeighbours = new Dictionary <uint, List <uint> >(); uint vertexId = 1; while (vertexId < sortedGraph.VertexCount + 1) { uint blockId = vertexId; var blockArcs = new List <CHArc>(); var blockArcCoordinates = new List <CHArcCoordinates>(); var blockVertices = new List <CHVertex>(); while (vertexId < blockId + _blockVertexSize && vertexId < sortedGraph.VertexCount + 1) { // create this block. var chVertex = new CHVertex(); float latitude, longitude; sortedGraph.GetVertex(vertexId, out latitude, out longitude); chVertex.Latitude = latitude; chVertex.Longitude = longitude; chVertex.ArcIndex = (ushort)(blockArcs.Count); List <uint> reverse = null; foreach (var sortedArc in sortedGraph.GetEdges(vertexId)) { var chArc = new CHArc(); chArc.TargetId = sortedArc.Neighbour; chArc.Meta = sortedArc.EdgeData.Meta; chArc.Value = sortedArc.EdgeData.Value; chArc.Weight = sortedArc.EdgeData.Weight; blockArcs.Add(chArc); var chArcCoordinates = new CHArcCoordinates(); chArcCoordinates.Coordinates = sortedArc.Intermediates.ToSimpleArray(); blockArcCoordinates.Add(chArcCoordinates); if (sortedArc.EdgeData.RepresentsNeighbourRelations) { if (!reverseNeighbours.TryGetValue(sortedArc.Neighbour, out reverse)) { reverse = new List <uint>(); reverseNeighbours[sortedArc.Neighbour] = reverse; } reverse.Add(vertexId); } } chVertex.ArcCount = (ushort)(blockArcs.Count - chVertex.ArcIndex); blockVertices.Add(chVertex); vertexId++; // move to the next vertex. } // create block. var block = new CHBlock(); block.Arcs = blockArcs.ToArray(); block.Vertices = blockVertices.ToArray(); // TODO: get rid of the list and create an array to begin with. blocks.Add(block); var blockCoordinates = new CHBlockCoordinates(); blockCoordinates.Arcs = blockArcCoordinates.ToArray(); // write blocks. var blockStream = new MemoryStream(); _runtimeTypeModel.Serialize(blockStream, block); var compressed = GZipStream.CompressBuffer(blockStream.ToArray()); blockStream.Dispose(); memoryStream.Write(compressed, 0, compressed.Length); blockLocations.Add((int)memoryStream.Position); blockShapes.Add(blockCoordinates); } var blockIndex = new CHBlockIndex(); blockIndex.BlockLocationIndex = blockLocations.ToArray(); stream.Seek(startOfBlocks + 4, SeekOrigin.Begin); // move to beginning of [BLOCK_INDEX] _runtimeTypeModel.Serialize(stream, blockIndex); // write block index. int sizeBlockIndex = (int)(stream.Position - (startOfBlocks + 4)); // now at beginning of [BLOCKS] memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.WriteTo(stream); // write blocks. memoryStream.Dispose(); // write shapes and index. memoryStream = new MemoryStream(); var startOfShapes = stream.Position; foreach (var blockCoordinates in blockShapes) { // write shape blocks. var blockShapeStream = new MemoryStream(); _runtimeTypeModel.Serialize(blockShapeStream, blockCoordinates); var compressed = GZipStream.CompressBuffer(blockShapeStream.ToArray()); blockShapeStream.Dispose(); memoryStream.Write(compressed, 0, compressed.Length); blockShapeLocations.Add((int)memoryStream.Position); } blockIndex = new CHBlockIndex(); blockIndex.BlockLocationIndex = blockShapeLocations.ToArray(); stream.Seek(startOfShapes + 4, SeekOrigin.Begin); // move to beginning of [SHAPE_INDEX] _runtimeTypeModel.Serialize(stream, blockIndex); // write shape index. int sizeShapeIndex = (int)(stream.Position - (startOfShapes + 4)); // now at beginning of [SHAPE] memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.WriteTo(stream); // write shapes. memoryStream.Dispose(); // build reverse blocks. var blockReverses = new List <CHBlockReverse>(); List <uint> storedReverses = null; uint currentVertexId = 0; for (int i = 0; i < blocks.Count; i++) { var blockReverse = new CHBlockReverse(); blockReverse.Vertices = new CHArcReverse[blocks[i].Vertices.Length]; for (int j = 0; j < blocks[i].Vertices.Length; j++) { currentVertexId++; var blockReverseArc = new CHArcReverse(); blockReverseArc.Neighbours = new uint[0]; if (reverseNeighbours.TryGetValue(currentVertexId, out storedReverses)) { blockReverseArc.Neighbours = storedReverses.ToArray(); } blockReverse.Vertices[j] = blockReverseArc; } blockReverses.Add(blockReverse); } // write reverse and index. memoryStream = new MemoryStream(); var startOfReverses = stream.Position; foreach (var blockReverse in blockReverses) { // write reverse blocks. var blockReverseStream = new MemoryStream(); _runtimeTypeModel.Serialize(blockReverseStream, blockReverse); var compressed = GZipStream.CompressBuffer(blockReverseStream.ToArray()); blockReverseStream.Dispose(); memoryStream.Write(compressed, 0, compressed.Length); blockReverseLocations.Add((int)memoryStream.Position); } blockIndex = new CHBlockIndex(); blockIndex.BlockLocationIndex = blockReverseLocations.ToArray(); stream.Seek(startOfReverses + 4, SeekOrigin.Begin); // move to beginning of [REVERSE_INDEX] _runtimeTypeModel.Serialize(stream, blockIndex); // write reverse index. int sizeReverseIndex = (int)(stream.Position - (startOfReverses + 4)); // now at beginning of [REVERSE] memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.WriteTo(stream); // write reverses. memoryStream.Dispose(); // write tags after reverses. int tagsPosition = (int)stream.Position; TagIndexSerializer.SerializeBlocks(stream, graph.TagsIndex, 10); stream.Seek(startOfBlocks, SeekOrigin.Begin); // move to [SIZE_OF_BLOCK_INDEX] stream.Write(BitConverter.GetBytes(sizeBlockIndex), 0, 4); // write start position of blocks. stream.Seek(startOfShapes, SeekOrigin.Begin); // move to [SIZE_OF_BLOCK_INDEX] stream.Write(BitConverter.GetBytes(sizeShapeIndex), 0, 4); // write start position of blocks. stream.Seek(startOfReverses, SeekOrigin.Begin); // move to [SIZE_OF_REVERSE_INDEX] stream.Write(BitConverter.GetBytes(sizeReverseIndex), 0, 4); // write start position of blocks. // write index. stream.Seek(0, SeekOrigin.Begin); // move to beginning stream.Write(BitConverter.GetBytes(startOfBlocks), 0, 4); // write start position of blocks. stream.Write(BitConverter.GetBytes(startOfShapes), 0, 4); // write start position of shapes. stream.Write(BitConverter.GetBytes(startOfReverses), 0, 4); // write start position of shapes. stream.Write(BitConverter.GetBytes(tagsPosition), 0, 4); stream.Write(BitConverter.GetBytes(sizeRegionIndex), 0, 4); // write size of region index. stream.Flush(); }
// Overwrite To Data to save Data into ChannelBlob instead of File Blob // This needs to be done to avoid data duplication public Dictionary <int, byte[]> ToData() { Dictionary <int, byte[]> result = new Dictionary <int, byte[]>(); var timeSeries = m_dataSeries[0].DataPoints .Select(dataPoint => new { Time = dataPoint.Time.Ticks, Compressed = false }) .ToList(); for (int i = 1; i < timeSeries.Count; i++) { long previousTimestamp = m_dataSeries[0][i - 1].Time.Ticks; long timestamp = timeSeries[i].Time; long diff = timestamp - previousTimestamp; if (diff >= 0 && diff <= ushort.MaxValue) { timeSeries[i] = new { Time = diff, Compressed = true } } ; } int timeSeriesByteLength = timeSeries.Sum(obj => obj.Compressed ? sizeof(ushort) : sizeof(int) + sizeof(long)); int dataSeriesByteLength = sizeof(int) + (2 * sizeof(double)) + (m_samples * sizeof(ushort)); int totalByteLength = sizeof(int) + timeSeriesByteLength + dataSeriesByteLength; foreach (DataSeries dataSeries in m_dataSeries) { byte[] data = new byte[totalByteLength]; int offset = 0; offset += LittleEndian.CopyBytes(m_samples, data, offset); List <int> uncompressedIndexes = timeSeries .Select((obj, Index) => new { obj.Compressed, Index }) .Where(obj => !obj.Compressed) .Select(obj => obj.Index) .ToList(); for (int i = 0; i < uncompressedIndexes.Count; i++) { int index = uncompressedIndexes[i]; int nextIndex = (i + 1 < uncompressedIndexes.Count) ? uncompressedIndexes[i + 1] : timeSeries.Count; offset += LittleEndian.CopyBytes(nextIndex - index, data, offset); offset += LittleEndian.CopyBytes(timeSeries[index].Time, data, offset); for (int j = index + 1; j < nextIndex; j++) { offset += LittleEndian.CopyBytes((ushort)timeSeries[j].Time, data, offset); } } if (dataSeries.Calculated) { continue; } const ushort NaNValue = ushort.MaxValue; const ushort MaxCompressedValue = ushort.MaxValue - 1; int seriesID = dataSeries.SeriesInfo?.ID ?? 0; double range = dataSeries.Maximum - dataSeries.Minimum; double decompressionOffset = dataSeries.Minimum; double decompressionScale = range / MaxCompressedValue; double compressionScale = (decompressionScale != 0.0D) ? 1.0D / decompressionScale : 0.0D; offset += LittleEndian.CopyBytes(seriesID, data, offset); offset += LittleEndian.CopyBytes(decompressionOffset, data, offset); offset += LittleEndian.CopyBytes(decompressionScale, data, offset); foreach (DataPoint dataPoint in dataSeries.DataPoints) { ushort compressedValue = (ushort)Math.Round((dataPoint.Value - decompressionOffset) * compressionScale); if (compressedValue == NaNValue) { compressedValue--; } if (double.IsNaN(dataPoint.Value)) { compressedValue = NaNValue; } offset += LittleEndian.CopyBytes(compressedValue, data, offset); } byte[] returnArray = GZipStream.CompressBuffer(data); returnArray[0] = 0x44; returnArray[1] = 0x33; int dataSeriesID = dataSeries.SeriesInfo?.ID ?? 0; result.Add(dataSeriesID, returnArray); } return(result); }
public static void HostServer(ServerSettings settings, bool fromReplay, bool hadSimulation, bool asyncTime) { Log.Message($"Starting the server"); var session = new MultiplayerSession(); if (Multiplayer.session != null) // This is the case when hosting from a replay { session.dataSnapshot = Multiplayer.session.dataSnapshot; } Multiplayer.session = session; session.myFactionId = Faction.OfPlayer.loadID; session.localServerSettings = settings; session.gameName = settings.gameName; // Server already pre-inited in HostWindow var localServer = Multiplayer.LocalServer; MultiplayerServer.instance = Multiplayer.LocalServer; if (hadSimulation) { localServer.savedGame = GZipStream.CompressBuffer(session.dataSnapshot.gameData); localServer.semiPersistent = GZipStream.CompressBuffer(session.dataSnapshot.semiPersistentData); localServer.mapData = session.dataSnapshot.mapData.ToDictionary(kv => kv.Key, kv => GZipStream.CompressBuffer(kv.Value)); localServer.mapCmds = session.dataSnapshot.mapCmds.ToDictionary(kv => kv.Key, kv => kv.Value.Select(ScheduledCommand.Serialize).ToList()); } localServer.commands.debugOnlySyncCmds = Sync.handlers.Where(h => h.debugOnly).Select(h => h.syncId).ToHashSet(); localServer.commands.hostOnlySyncCmds = Sync.handlers.Where(h => h.hostOnly).Select(h => h.syncId).ToHashSet(); localServer.hostUsername = Multiplayer.username; localServer.coopFactionId = Faction.OfPlayer.loadID; localServer.rwVersion = VersionControl.CurrentVersionString; localServer.mpVersion = MpVersion.Version; localServer.defInfos = MultiplayerData.localDefInfos; localServer.serverData = JoinData.WriteServerData(settings.syncConfigs); if (settings.steam) { localServer.TickEvent += SteamIntegration.ServerSteamNetTick; } if (fromReplay) { localServer.gameTimer = TickPatch.Timer; } if (!fromReplay) { SetupGameFromSingleplayer(); } foreach (var tickable in TickPatch.AllTickables) { tickable.Cmds.Clear(); } Find.PlaySettings.usePlanetDayNightSystem = false; Multiplayer.RealPlayerFaction = Faction.OfPlayer; localServer.playerFactions[Multiplayer.username] = Faction.OfPlayer.loadID; SetupLocalClient(); Find.MainTabsRoot.EscapeCurrentTab(false); Multiplayer.session.AddMsg("If you are having any issues with the mod and would like some help resolving them, then please reach out to us on our Discord server:", false); Multiplayer.session.AddMsg(new ChatMsg_Url("https://discord.gg/S4bxXpv"), false); if (hadSimulation) { StartServerThread(); } else { Multiplayer.WorldComp.TimeSpeed = TimeSpeed.Paused; foreach (var map in Find.Maps) { map.AsyncTime().TimeSpeed = TimeSpeed.Paused; } Multiplayer.WorldComp.UpdateTimeSpeed(); Multiplayer.GameComp.asyncTime = asyncTime; Multiplayer.GameComp.debugMode = settings.debugMode; Multiplayer.GameComp.logDesyncTraces = settings.desyncTraces; LongEventHandler.QueueLongEvent(() => { Multiplayer.session.dataSnapshot = SaveLoad.CreateGameDataSnapshot(SaveLoad.SaveAndReload()); SaveLoad.SendGameData(Multiplayer.session.dataSnapshot, false); StartServerThread(); }, "MpSaving", false, null); } void StartServerThread() { localServer.running = true; Multiplayer.LocalServer.serverThread = new Thread(localServer.Run) { Name = "Local server thread" }; Multiplayer.LocalServer.serverThread.Start(); const string text = "Server started."; Messages.Message(text, MessageTypeDefOf.SilentInput, false); Log.Message(text); } }
public static void HostServer(ServerSettings settings, bool fromReplay, bool withSimulation = false, bool debugMode = false, bool logDesyncTraces = false) { Log.Message($"Starting the server"); var session = Multiplayer.session = new MultiplayerSession(); session.myFactionId = Faction.OfPlayer.loadID; session.localSettings = settings; session.gameName = settings.gameName; var localServer = new MultiplayerServer(settings); if (withSimulation) { localServer.savedGame = GZipStream.CompressBuffer(OnMainThread.cachedGameData); localServer.mapData = OnMainThread.cachedMapData.ToDictionary(kv => kv.Key, kv => GZipStream.CompressBuffer(kv.Value)); localServer.mapCmds = OnMainThread.cachedMapCmds.ToDictionary(kv => kv.Key, kv => kv.Value.Select(c => c.Serialize()).ToList()); } else { OnMainThread.ClearCaches(); } localServer.debugMode = debugMode; localServer.debugOnlySyncCmds = new HashSet <int>(Sync.handlers.Where(h => h.debugOnly).Select(h => h.syncId)); localServer.hostOnlySyncCmds = new HashSet <int>(Sync.handlers.Where(h => h.hostOnly).Select(h => h.syncId)); localServer.hostUsername = Multiplayer.username; localServer.coopFactionId = Faction.OfPlayer.loadID; localServer.rwVersion = session.mods.remoteRwVersion = VersionControl.CurrentVersionString; localServer.modNames = session.mods.remoteModNames = LoadedModManager.RunningModsListForReading.Select(m => m.Name).ToArray(); localServer.modIds = session.mods.remoteModIds = LoadedModManager.RunningModsListForReading.Select(m => m.PackageId).ToArray(); localServer.workshopModIds = session.mods.remoteWorkshopModIds = ModManagement.GetEnabledWorkshopMods().ToArray(); localServer.defInfos = session.mods.defInfo = Multiplayer.localDefInfos; Log.Message($"MP Host modIds: {string.Join(", ", localServer.modIds)}"); Log.Message($"MP Host workshopIds: {string.Join(", ", localServer.workshopModIds)}"); if (settings.steam) { localServer.NetTick += SteamIntegration.ServerSteamNetTick; } if (fromReplay) { localServer.gameTimer = TickPatch.Timer; } MultiplayerServer.instance = localServer; session.localServer = localServer; if (!fromReplay) { SetupGame(); } foreach (var tickable in TickPatch.AllTickables) { tickable.Cmds.Clear(); } Find.PlaySettings.usePlanetDayNightSystem = false; Multiplayer.RealPlayerFaction = Faction.OfPlayer; localServer.playerFactions[Multiplayer.username] = Faction.OfPlayer.loadID; SetupLocalClient(); Find.MainTabsRoot.EscapeCurrentTab(false); Multiplayer.session.AddMsg("If you are having a issue with the mod and would like some help resolving it, then please reach out to us on our discord server:", false); Multiplayer.session.AddMsg(new ChatMsg_Url("https://discord.gg/S4bxXpv"), false); if (withSimulation) { StartServerThread(); } else { var timeSpeed = TimeSpeed.Paused; Multiplayer.WorldComp.TimeSpeed = timeSpeed; foreach (var map in Find.Maps) { map.AsyncTime().TimeSpeed = timeSpeed; } Multiplayer.WorldComp.UpdateTimeSpeed(); Multiplayer.WorldComp.debugMode = debugMode; Multiplayer.WorldComp.logDesyncTraces = logDesyncTraces; LongEventHandler.QueueLongEvent(() => { SaveLoad.CacheGameData(SaveLoad.SaveAndReload()); SaveLoad.SendCurrentGameData(false); StartServerThread(); }, "MpSaving", false, null); } void StartServerThread() { var netStarted = localServer.StartListeningNet(); var lanStarted = localServer.StartListeningLan(); string text = "Server started."; if (netStarted != null) { text += (netStarted.Value ? $" Direct at {settings.bindAddress}:{localServer.NetPort}." : " Couldn't bind direct."); } if (lanStarted != null) { text += (lanStarted.Value ? $" LAN at {settings.lanAddress}:{localServer.LanPort}." : " Couldn't bind LAN."); } session.serverThread = new Thread(localServer.Run) { Name = "Local server thread" }; session.serverThread.Start(); Messages.Message(text, MessageTypeDefOf.SilentInput, false); Log.Message(text); } }
private async void MutilPack(string[] arr, int splitCount) { string fname; SaveFileDialog sd = new SaveFileDialog { Filter = "PackFile(*.dat)|*.dat", Title = "请输入保存文件名" }; if (sd.ShowDialog(this) == DialogResult.OK) { fname = sd.FileName; } else { return; } int size = arr.Length / splitCount; Task[] taskArr = new Task[splitCount]; pnMain.Enabled = false; for (int i = 0; i < splitCount; i++) { int index = i * size; if (i == splitCount - 1) { size = arr.Length; } string[] subarr = arr.Skip(index).Take(size).ToArray(); var t = Task.Run(() => { PackImage(subarr); }); taskArr[i] = t; } await Task.WhenAll(taskArr); pbar.Value = 99; Main.PackData pack = new Main.PackData(); List <MemoryStream> ls = new List <MemoryStream>(); ArrayList akeys = new ArrayList(hashtable.Keys); akeys.Sort(); foreach (string skey in akeys) { using (MemoryStream ms = new MemoryStream()) { var ba = (byte[])hashtable[skey]; ms.Write(ba, 0, ba.Length); ls.Add(ms); } } pack.img = ls; MemoryStream mm = new MemoryStream(); var formatter = new BinaryFormatter(); formatter.Serialize(mm, pack); var data = GZipStream.CompressBuffer(mm.ToArray()); if (File.Exists(fname)) { File.Delete(fname); } FileStream fs = new FileStream(fname, FileMode.Create); fs.Write(data, 0, data.Length); fs.Dispose(); pbar.Value = 100; if (Directory.GetFiles(Directory.GetCurrentDirectory() + "\\VideoTempOutput").Length >= 0) { foreach (var file in Directory.GetFiles(Directory.GetCurrentDirectory() + "\\VideoTempOutput")) { File.Delete(file); } Directory.Delete(Directory.GetCurrentDirectory() + "\\VideoTempOutput"); } Process.Start("Explorer.exe", "/select," + fname); pnMain.Enabled = true; mm.Dispose(); Close(); }