/// <summary> /// 将压缩的输入字节流解压 /// </summary> /// <param name="inputBytes">压缩文件字节流</param> /// <param name="path">文件写入路径</param> /// <param name="useCSharp">是否使用C#自带解压算法</param> public static bool Decompress(byte[] inputBytes, string path, bool useCSharp = false) { if (useCSharp) { MemoryStream ms = new MemoryStream(inputBytes); GZipStream zipStream = new GZipStream(ms, CompressionMode.Decompress, false); //ms的后4个字节为文件原长 byte[] byteOriginalLength = new byte[4]; ms.Position = ms.Length - 4; ms.Read(byteOriginalLength, 0, 4); ms.Position = 0; //需要测试 int fileLength = BitConverter.ToInt32(byteOriginalLength, 0); byte[] buffer = new byte[fileLength]; zipStream.Read(buffer, 0, fileLength); ms.Close(); zipStream.Close(); return(WriteBuffersToFile(buffer, path)); } else { return(WriteBuffersToFile(CLZF2.Decompress(inputBytes), path)); } }
IEnumerator Upload() { if (File.Exists(m_strErrorPath)) { string totalFile = string.Empty; using (StreamReader reader = new StreamReader(m_strErrorPath, Encoding.UTF8, true)) { totalFile = reader.ReadToEnd(); } File.Delete(m_strErrorPath); byte[] buf = Encoding.UTF8.GetBytes(totalFile.ToCharArray()); buf = CLZF2.Compress(buf); WWWForm form = new WWWForm(); form.AddField("VERSION_CODE", VERSION_CODE); form.AddBinaryData("upload_file", buf, "error.txt", "text/plain"); WWW www = new WWW(UPLOAD_URL, form); yield return(www); if (www.text != null) { Debug.Log(www.text); } if (www.error != null) { Debug.LogError(www.error); } } }
public void SaveCellDataToFile(Cell[] cellData, string path) { if (container == null) { container = ScriptableObject.CreateInstance <MapDataContainer>(); container.CellData = cellData; } else { container.CellData = cellData; } Debug.Log("Saving cell data to : " + path); using (var ms = new MemoryStream(2000000)) using (var bw = new BinaryWriter(ms)) { container.Serialize(bw); var bytes = ms.ToArray(); var b2 = CLZF2.Compress(bytes); var basePath = Path.GetDirectoryName(path); if (!Directory.Exists(basePath)) { Directory.CreateDirectory(basePath); } File.WriteAllBytes(path, b2); } }
// private void OnNewPitchBounds(object pitchBoundsObj) // { // float[] pitchBounds = (float[])pitchBoundsObj; // minPitch = pitchBounds[0]; // maxPitch = pitchBounds[1]; // } private void OnMicrophonePitch(object pitchObj) { float?pitch = (float?)pitchObj; if (pitch.HasValue) { float roundedPitch = (float)Math.Round(pitch.Value, 1, MidpointRounding.AwayFromZero); pitchSamples.Add(roundedPitch); } else { pitchSamples.Add(0f); } if (pitchSamples.Count >= pitchSampleCount) { byte[] pitchRecentSamplesBytes = new byte[pitchSampleCount * 4]; Buffer.BlockCopy(pitchSamples.ToArray(), pitchSamples.Count * 4 - pitchSampleCount * 4, pitchRecentSamplesBytes, 0, pitchRecentSamplesBytes.Length); byte[] compressedSamples = CLZF2.Compress(pitchRecentSamplesBytes); compressionRatio = (float)compressedSamples.Length / (float)pitchSampleCount; text.text = compressionRatio.ToString("0.00"); EventManager.TriggerEvent(EventTypes.Spookometer, compressionRatio); } }
public static void Encode( this short[] intensities, out byte[] spectra, out double tic, out double bpi, out int indexOfMaxIntensity, out int nonZeroCount) { // 16-bit integers are 2 bytes const int dataTypeSize = 2; spectra = null; tic = 0; bpi = 0; indexOfMaxIntensity = 0; nonZeroCount = RlzEncode.Encode(intensities, out var runLengthZeroEncodedData, out tic, out bpi, out indexOfMaxIntensity); // Compress intensities var encodedDataLength = runLengthZeroEncodedData.Length; if (encodedDataLength > 0) { spectra = new byte[encodedDataLength * dataTypeSize]; Buffer.BlockCopy(runLengthZeroEncodedData, 0, spectra, 0, encodedDataLength * dataTypeSize); spectra = CLZF2.Compress(spectra); } }
public unsafe bool Apply(ReadOnlySpan <byte> src, ref Span <byte> dst) { if (dst.Length <= src.Length + 1) return(false); fixed(byte *srcPtr = src, dstPtr = dst) { var count = CLZF2.TryCompress(srcPtr, dstPtr + 1, src.Length, src.Length); if (count <= 0 || count >= src.Length) { // Compression failed. Prepend header and send. *dstPtr = kHeaderUncompressed; UnsafeUtility.MemCpy(dstPtr + 1, srcPtr, src.Length); count = src.Length; } else { // Compression succeeded. Prepend header and send. if (count + 1 < dst.Length) { return(false); } Assert.IsTrue(count + 1 <= dst.Length); *dstPtr = kHeaderCompresssed; } dst = dst.Slice(0, count + 1); } return(true); }
public void ModifySaveGame(int slot, string description) { Debug.Log("ModifySaveGame " + slot); SaveEntry saveInfoInSlot = saveManager.GetSaveInfoInSlot(slot); if (saveInfoInSlot != null) { byte[] array = File.ReadAllBytes(saveInfoInSlot.Path); MGHelper.KeyEncode(array); byte[] buffer = CLZF2.Decompress(array); MemoryStream memoryStream = new MemoryStream(buffer); MemoryStream memoryStream2 = new MemoryStream(); BinaryReader binaryReader = new BinaryReader(memoryStream); BinaryWriter binaryWriter = new BinaryWriter(memoryStream2); binaryWriter.Write(binaryReader.ReadBytes(16)); binaryReader.ReadString(); binaryWriter.Write(description); binaryWriter.Write(binaryReader.ReadBytes((int)(memoryStream.Length - memoryStream.Position))); byte[] inputBytes = memoryStream2.ToArray(); memoryStream.Dispose(); memoryStream2.Dispose(); byte[] array2 = CLZF2.Compress(inputBytes); MGHelper.KeyEncode(array2); File.WriteAllBytes(saveInfoInSlot.Path, array2); saveManager.UpdateSaveSlot(slot); } }
public unsafe bool Unapply(ReadOnlySpan <byte> src, ref Span <byte> dst) { var header = src[0]; fixed(byte *srcPtr = src, dstPtr = dst) { if (header == kHeaderUncompressed) { if (dst.Length < src.Length - 1) { return(false); } UnsafeUtility.MemCpy(dstPtr, srcPtr + 1, src.Length - 1); return(true); } // Compressed need to decompress var count = CLZF2.TryDecompress(srcPtr + 1, dstPtr, src.Length - 1, dst.Length); if (count <= 0 || count > dst.Length) { return(false); } dst = dst.Slice(0, count); return(true); } }
private void LoadCellData() { var fullPath = AssetDatabase.GetAssetPath(this); var path = Path.GetDirectoryName(fullPath); var dataPath = Path.Combine(path, "mapinfo", name + ".bytes").Replace("\\", "/"); if (!File.Exists(dataPath)) { throw new Exception($"Could not find cell data for {name} at path: {dataPath}"); } var bytes = CLZF2.Decompress(File.ReadAllBytes(dataPath)); if (container == null) { container = ScriptableObject.CreateInstance <MapDataContainer>(); } using (var ms = new MemoryStream(bytes)) using (var br = new BinaryReader(ms)) { MapDataContainer.Deserialize(container, br); } //container.CellData = SerializationUtility.DeserializeValue<Cell[]>(bytes, DataFormat.Binary); }
//deserialize and return for loading (do not call directly, call load() instead) protected Object2PropertiesMappingListWrapper DeSerializeObject(byte[] data) { Object2PropertiesMappingListWrapper objectToSerialize = null; if (compressSaves) { try { data = CLZF2.Decompress(data); } catch (OutOfMemoryException meme) { if (showWarnings) { Debug.LogWarning("EZReplayManager WARNING: Decompressing was unsuccessful. Trying without decompression."); } } catch (OverflowException ofe) { if (showWarnings) { Debug.LogWarning("EZReplayManager WARNING: Decompressing was unsuccessful. Trying without decompression."); } } } MemoryStream stream = new MemoryStream(data); // BinaryFormatter bFormatter = new BinaryFormatter (); // objectToSerialize = (Object2PropertiesMappingListWrapper)bFormatter.Deserialize (stream); objectToSerialize = ProtoBuf.Serializer.Deserialize <Object2PropertiesMappingListWrapper> (stream); objectToSerialize.CalcParentMapingRef(); //print("System.GC.GetTotalMemory(): "+System.GC.GetTotalMemory(false)); return(objectToSerialize); }
public byte[] GetPacFile(string filename, bool encode, bool compress) { Debug.Log(filename); EntityList.TryGetValue(filename.ToLower(), out PacEntity value); if (value == null) { Logger.LogError("Could not find archive " + filename + " in pac file " + Name); return(new byte[0]); } byte[] array; using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (BinaryReader binaryReader = new BinaryReader(fileStream)) { fileStream.Seek(value.Offset, SeekOrigin.Begin); array = binaryReader.ReadBytes(value.Size); } } Debug.Log($"GetPacFile {filename} size {array.Length}"); if (encode) { MGHelper.KeyEncode(array); } if (compress) { return(CLZF2.Decompress(array)); } return(array); }
public void SavingComplexObject() { MyComplexObject[] MySaveItem = new MyComplexObject[1000]; for (int i = 0; i < MySaveItem.Length; i++) { var item = new MyComplexObject(); item.myPosition = Vector3.one * i; item.myPositionHistory = new Vector3[100]; item.myChatHistory = new string[100]; for (int j = 0; j < 100; j++) { item.myPositionHistory[j] = Vector3.one * j; item.myChatHistory[j] = "Chat line: " + j; } } var mySaveObject = ObjectToByteArray(MySaveItem); byte[] compressed = CLZF2.Compress(mySaveObject); byte[] decompressed = CLZF2.Decompress(compressed); var outSaveObject = ObjectToByteArray <MyComplexObject[]>(decompressed); Assert.AreEqual(mySaveObject.Length, decompressed.Length); Assert.AreEqual(mySaveObject, decompressed); Assert.AreEqual(outSaveObject, MySaveItem); }
// Update is called once per frame public void Update() { Rect cRect = GetComponent <RectTransform>().rect; if (StreamSDK.instance == null) { GetComponent <RectTransform>().localScale = new Vector2(1, 1); return; } if (StreamSDK.instance.audioPacket == null) { GetComponent <RectTransform>().localScale = new Vector2(1, 1); return; } if (StreamSDK.instance.audioPacket.data == null) { GetComponent <RectTransform>().localScale = new Vector2(1, 1); return; } if (StreamSDK.instance.audioPacket.data.Length <= 4) { GetComponent <RectTransform>().localScale = new Vector2(1, 1); return; } audioAverage = Mathf.Abs(AudioArray.ToFloat(CLZF2.Decompress(StreamSDK.instance.audioPacket.data)).ToList().Average()) * scalar; GetComponent <RectTransform>().rect.Set(cRect.x, cRect.y, cRect.width, 50 + audioAverage); GetComponent <RectTransform>().localScale = new Vector2(1, audioAverage); }
public static int Serialize(ChunkData data, BinaryWriter writer, bool compress) { writer.Write(compress); writer.Write(data.IndexX); writer.Write(data.IndexY); writer.Write(data.IndexZ); writer.Write(data.Height); writer.Write(32768); writer.Write(data.Origin.x); writer.Write(data.Origin.y); writer.Write(data.Origin.z); var blockData = new byte[0]; var uncompressed = ChunkHelper.ToByteArray(data.Blocks.Data, Chunk.CHUNK_SIZE_X * Chunk.CHUNK_SIZE_Z * Chunk.CHUNK_SIZE_Y); if (compress) { blockData = CLZF2.Compress(uncompressed); } else { blockData = uncompressed; } writer.Write(blockData.Length); writer.Write(blockData); return((int)writer.BaseStream.Position); }
public static ChunkData Deserialize(BinaryReader reader) { var compressed = reader.ReadBoolean(); var indexX = reader.ReadInt32(); var indexY = reader.ReadInt32(); var indexZ = reader.ReadInt32(); var height = reader.ReadInt32(); var blockCount = reader.ReadInt32(); var originX = reader.ReadSingle(); var originY = reader.ReadSingle(); var originZ = reader.ReadSingle(); var dataLen = reader.ReadInt32(); byte[] data = new byte[0]; if (compressed) { data = CLZF2.Decompress(reader.ReadBytes(dataLen)); } else { data = reader.ReadBytes(dataLen); } var blocks = ChunkHelper.FromByteArray(data, blockCount); var chunk = new ChunkData { Blocks = new BlockDataHolder(Chunk.CHUNK_SIZE_X, Chunk.CHUNK_SIZE_Y, Chunk.CHUNK_SIZE_Z, blocks), Height = height, IndexX = indexX, IndexY = indexY, IndexZ = indexZ, Origin = new UnityEngine.Vector3(originX, originY, originZ) }; return(chunk); }
public void Incoming_Message(byte[] message, int length, ulong steamid) { incoming += length; message = Extensions.Get(message, 0, length); message = CLZF2.Decompress(message); string json = Encoding.UTF8.GetString(message, 0, message.Length); try { SNetMessage readMessage = JsonUtility.FromJson <SNetMessage>(json); string[] data = new string[2]; data[0] = json; data[1] = steamid.ToString(); if (handlers.ContainsKey(readMessage.n)) // If there is a handler { handlers[readMessage.n] (data); } else { Debug.Log("Unknown message type: " + readMessage.n); } } catch { } }
public static byte[] Pack(byte[] data, CompressionMode mode) { if (mode == CompressionMode.LZF) { return(CLZF2.Compress(data)); } return(data); }
private void HashDataBuild(string outPath, HashInfo info) { var files = info.files.Keys.ToList <string>(); var paths = new string[files.Count]; for (var i = 0; i < files.Count; ++i) { paths[i] = GetPath(files[i]); } PatchData data = null; Object[] assets = GetMainAssets(paths); var names = new string[1]; var hash = string.Empty; var key = string.Empty; byte[] bytes = null; byte[] comps = null; var newPath = string.Empty; FileStream fs = null; var md5Hash = MD5Hash.instance; for (var i = 0; i < assets.Length; ++i) { if (!File.Exists(outPath)) { File.Copy(paths[i], outPath); } else { File.Copy(paths[i], outPath, true); } hash = md5Hash.GetHashCode(outPath); names[0] = hash; bytes = File.ReadAllBytes(outPath); comps = CLZF2.Compress(bytes); newPath = string.Format("{0}/{1}.lzf", Path.GetDirectoryName(outPath), Path.GetFileNameWithoutExtension(outPath)); fs = new FileStream(newPath, FileMode.Create); fs.Write(comps, 0, comps.Length); fs.Close(); data = MakePatchData(outPath, 0, names); key = string.Format("{0}{1}", data.Path, data.Name); if (mPatchList.datas.ContainsKey(key)) { mPatchList.datas.Remove(key); } mPatchList.AddData(data); } }
/// <summary> /// Relay the message to lobby. /// </summary> /// <param name="message">Json string</param> public static void RelayToLobby(string message) { ASCIIEncoding asen = new ASCIIEncoding(); byte[] ba = asen.GetBytes(message); ba = CLZF2.Compress(ba); Relaying?.Invoke(); Main.Write(JsonConvert.SerializeObject(new MessagesOutgoing.NetworkMessage(new MessagesOutgoing._RelayToLobby(ba)))); }
static string SaveFileToJSON(string path, int gameVersion) { byte[] array = File.ReadAllBytes(path); MGHelper.KeyEncode(array); byte[] buffer = CLZF2.Decompress(array); StringBuilder output = new StringBuilder(); using (JsonTextWriter writer = new JsonTextWriter(new StringWriter(output)) { Formatting = Formatting.Indented }) { writer.DateFormatHandling = DateFormatHandling.IsoDateFormat; writer.DateTimeZoneHandling = DateTimeZoneHandling.Utc; writer.WriteStartObject(); using (MemoryStream input = new MemoryStream(buffer)) { using (BinaryReader reader = new BinaryReader(input)) { string magic = new string(reader.ReadChars(4)); if (magic != "MGSV") { throw new FileLoadException("Save file does not appear to be valid! Invalid header."); } int num = reader.ReadInt32(); if (num != 1) { throw new FileLoadException("Save file does not appear to be valid! Invalid version number."); } writer.WritePropertyName("Time"); writer.WriteValue(DateTime.FromBinary(reader.ReadInt64())); foreach (var name in new string[] { "TextJP", "TextEN", "PrevTextJP", "PrevTextEN" }) { writer.WriteNameValue(name, reader.ReadString()); } writer.WriteNameValue("PrevAppendState", reader.ReadBoolean()); int stackSize = reader.ReadInt32(); writer.WritePropertyName("CallStack"); writer.WriteStartArray(); for (int i = 0; i < stackSize; i++) { StackEntryToJSON(writer, reader); } writer.WriteEndArray(); NamedObjectToJSON(StackEntryToJSON, writer, reader, "CurrentScript"); MemoryToJSON(writer, input); CurrentAudioToJSON(writer, input); SceneToJSON(writer, input, gameVersion); } } writer.WriteEndObject(); } return(output.ToString()); }
public void SaveGame(int slotnum) { if (hasSnapshot) { byte[] array = CLZF2.Compress(snapshotData); MGHelper.KeyEncode(array); string str = (slotnum < 100) ? ("save" + slotnum.ToString("D3")) : ("qsave" + (slotnum - 100)); File.WriteAllBytes(Path.Combine(MGHelper.GetSavePath(), str + ".dat"), array); saveManager.UpdateSaveSlot(slotnum); GameSystem.Instance.SceneController.WriteScreenshot(Path.Combine(MGHelper.GetSavePath(), str + ".png")); } }
private void BuildMesh() { Debug.Log("Received mesh!"); Debug.LogFormat("Compressed serialized mesh size: {0} KB", m_incomingCompressedMesh.LongLength / 1000); byte[] serializedMesh = CLZF2.Decompress(m_incomingCompressedMesh); Debug.LogFormat("Serialized mesh size: {0} KB", serializedMesh.LongLength / 1000); List <Mesh> receivedMesh = (List <Mesh>)SimpleMeshSerializer.Deserialize(serializedMesh); meshFilter.mesh = receivedMesh[0]; }
// Use this for initialization private void Start() { DG.Tweening.DOTween.Init(); CLZF2.Decrypt(null, AssetsMgr.VER + AssetsMgr.Instance.resHeight); LuaScriptMgr.OnScriptsFinish(); #if UNITY_EDITOR || UNITY_STANDALONE //编辑器工具管理类 LogMgr.D("加载编辑器工具..."); gameObject.AddComponent <GETools.GETBoot>(); #endif }
public void LongFormattedStringCompressionTest() { string longstring = "defined input is deluciously delicious.14 And here and Nora called The reversal from ground from here and executed with touch the country road, Nora made of, reliance on, can’t publish the goals of grandeur, said to his book and encouraging an envelope, and enable entry into the chryssial shimmering of hers, so God of information in her hands Spiros sits down the sign of winter? —It’s kind of Spice Christ. It is one hundred birds circle above the text: They did we said. 69 percent dead. Sissy Cogan’s shadow. —Are you x then sings.) I’m 96 percent dead humanoid figure,"; byte[] byteText = Encoding.Unicode.GetBytes(longstring); byte[] compressed = CLZF2.Compress(byteText); byte[] decompressed = CLZF2.Decompress(compressed); var outString = Encoding.Unicode.GetString(decompressed); Assert.AreEqual(byteText.Length, decompressed.Length); Assert.AreEqual(byteText, decompressed); Assert.AreEqual(outString, longstring); }
public void ThousandCharacterCompressionTest() { var x = new string('X', 10000); byte[] byteText = Encoding.Unicode.GetBytes(x); byte[] compressed = CLZF2.Compress(byteText); byte[] decompressed = CLZF2.Decompress(compressed); var outString = Encoding.Unicode.GetString(decompressed); Assert.AreEqual(byteText.Length, decompressed.Length); Assert.AreEqual(byteText, decompressed); Assert.AreEqual(outString, x); }
private void SendMesh(Mesh m) { Debug.Log("Sending mesh..."); byte[] serializedMesh = SimpleMeshSerializer.Serialize(new Mesh[] { mesh }); Debug.LogFormat("Serialized mesh size: {0} KB", serializedMesh.LongLength / 1000); byte[] compressedSerializedMesh = CLZF2.Compress(serializedMesh); Debug.LogFormat("Compressed serialized mesh size: {0} KB", compressedSerializedMesh.LongLength / 1000); SendMeshHeader(compressedSerializedMesh); StartCoroutine(SendMeshChunks(compressedSerializedMesh)); }
public void CompressionComparisonSpeedTest() { /* * Example run on core i9 7900X @ 4GHz. * Name Milliseconds Percent * LZ4 Compress 163 193.3% * ZRLE LZ4 Compress 84 100% * CLZF2 Compress 945 1118.5% * ZREL CLZF2 Compress 91 107.7% * Name Milliseconds Percent * LZ4 Decompress 325 16880.7% * ZRLE LZ4 Decompress 1 100% * CLZF2 Decompress 1176 61106.9% * ZREL CLZF2 Decompress 8 448.4% */ var intensities = new int[148000]; var randGenerator = new Random(); for (var i = 0; i < intensities.Length; i++) { var nextRandom = randGenerator.Next(0, 255); if (nextRandom < 245) { intensities[i] = 0; } else { intensities[i] = nextRandom; } } var spectra = new byte[intensities.Length * sizeof(int)]; Buffer.BlockCopy(intensities, 0, spectra, 0, spectra.Length); var decompressedIntensities = new int[intensities.Length]; byte[] zrleLz4Result = new byte[] { }; byte[] clzf2Result = new byte[] { }; byte[] zrleClzf2Result = new byte[] { }; byte[] snappyResult = new byte[] { }; byte[] zrleSnappyResult = new byte[] { }; Benchmark.This("CLZF2 Compress", () => { clzf2Result = CLZF2.Compress(spectra); }).WithWarmup(100).Against.This("ZREL CLZF2 Compress", () => { IntensityConverterCLZF.Compress(intensities, out zrleClzf2Result, out var tic, out var bpi, out var indexOfMaxIntensity); }).WithWarmup(100).Against.This("Snappy", () =>
public void Send(int connectionId, byte[] buffer, int count, NetworkReliablity reliability) { if (!connectionMap.ContainsKey(connectionId)) { return; } var pool = ArrayPool <byte> .Shared; var compressed = pool.Rent(count); var newSize = CLZF2.Compress(buffer, ref compressed, count); SendImpl(connectionId, compressed, newSize, reliability); pool.Return(compressed); }
/// <summary> /// Convert a LZF compressed and zero run length encoded byte array to a list of bin-intensity pairs /// </summary> /// <param name="compressedBinIntensity">LZF compressed and zero run length encoded byte array</param> /// <param name="basePeakIntensity">Highest intensity, and a data-type specifying parameter</param> /// <returns>List of tuples, where Item1 is the bin, and Item2 is the intensity</returns> public static List <Tuple <int, int> > Decompress(byte[] compressedBinIntensity, out int basePeakIntensity) { const int dataTypeSize = sizeof(int); var output = CLZF2.Decompress(compressedBinIntensity); var numReturnedBins = output.Length / dataTypeSize; var encodedIntensityValueArray = new int[numReturnedBins]; Buffer.BlockCopy(output, 0, encodedIntensityValueArray, 0, numReturnedBins * dataTypeSize); var data = RlzEncode.Decode(encodedIntensityValueArray); basePeakIntensity = data.Max(x => x.Item2); return(data); }
protected virtual void OnRecieveData(int connectionId, byte[] data, int dataSize) { NetworkConnection connection; if (!connectionMap.TryGetValue(connectionId, out connection)) { return; } var decompressed = ArrayPool <byte> .Shared.Rent(dataSize); CLZF2.Decompress(data, ref decompressed, dataSize); messageDeserializer.Replace(decompressed); messageDeserializer.SeekZero(); MessageHandlers.Execute(connection, messageDeserializer); }