public void EditorTest() { //Arrange var gameObject = new GameObject(); short rating = 1234; string ratingString = rating.ToString(); byte[] stringBytes = Encoding.UTF8.GetBytes(ratingString); byte[] shortBytes = BitConverter.GetBytes(rating); byte[] compressedShort = SevenZipHelper.Compress(shortBytes); byte[] compressedString = SevenZipHelper.Compress(stringBytes); Debug.Log(stringBytes.Length); Debug.Log(shortBytes.Length); Debug.Log("compressed short " + compressedShort.Length); Debug.Log("compressed string " + compressedString.Length); Debug.Log(ratingString); //Act //Try to rename the GameObject var newGameObjectName = "My game object"; gameObject.name = newGameObjectName; //Assert //The object has a new name Assert.AreEqual(newGameObjectName, gameObject.name); }
private static void WriteToFileQueue(object source, ElapsedEventArgs e, bool shuttingDown = false) { if (!shuttingDown) { ChangeFileName(); } ChangeFileNameWaitHandle.WaitOne(); // wait when the file name is being changed var bufferedData = new RoomInfoWrapper[JsonDataQueue.Count]; lock (JsonDataQueue) { JsonDataQueue.CopyTo(bufferedData, 0); JsonDataQueue.Clear(); } var utf8Text = JsonConvert.SerializeObject(bufferedData); var byteText = Encoding.ASCII.GetBytes(utf8Text); var b = SevenZipHelper.Compress(byteText); _sw.Write(Encoding.UTF8.GetString(b)); /*Console.WriteLine( * Encoding.ASCII.GetString( * SevenZipHelper.Decompress( * b)).Substring(0,400));*/ }
public static void ExportReplay(Score score) { if (!Directory.Exists("Replays")) { Directory.CreateDirectory("Replays"); } if (score.rawReplayCompressed == null) { score.rawReplayCompressed = SevenZipHelper.Compress(new ASCIIEncoding().GetBytes(score.replayFormatted)); } string filename = string.Format("{0} - {1} ({2:yyyyMMddhhmmss}).osr", score.playerName, BeatmapManager.Current.DisplayTitle, score.date.ToUniversalTime()); filename = "Replays\\" + GeneralHelper.WindowsFilenameStrip(filename); Stream stream = File.Open(filename, FileMode.Create); BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Serialize(stream, score); stream.Close(); GameBase.ShowMessage("Saved replay to " + filename, Color.BlueViolet, 6000); }
private void CompressDoUpdateHelper(byte[] content, string name, TypeName typeName, bool compress) { if (compress) { content = SevenZipHelper.Compress(content); } DoUpdateResource(typeName.ToString() + (compress ? "_LZMA" : ""), name, content); }
public void BytesCompressionTest() { byte[] messageBytes = message.ToBytes(); Debug.Log("uncompressed length: " + messageBytes.Length); byte[] compressedMessageBytes = SevenZipHelper.Compress(messageBytes); Debug.Log("compressed length: " + compressedMessageBytes.Length); Expect(compressedMessageBytes.Length, LessThan(messageBytes.Length)); }
public void JsonCompressionTest() { string jsonString = message.ToJsonString(); byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonString); Debug.Log(jsonString); Debug.Log("uncompressed length: " + jsonBytes.Length); byte[] jsonBytesCompressed = SevenZipHelper.Compress(jsonBytes); Debug.Log("compressed length: " + jsonBytesCompressed.Length); Expect(jsonBytesCompressed.Length, LessThan(jsonBytes.Length)); }
public static byte[] Compress(byte[] bytesToCompress, CompressionType compressionProvider) { if (compressionProvider == CompressionType.SevenZip) { return(SevenZipHelper.Compress(bytesToCompress)); } var ms = new MemoryStream(); Stream s = OutputStream(ms, compressionProvider); s.Write(bytesToCompress, 0, bytesToCompress.Length); s.Close(); return(ms.ToArray()); }
public string AddAssembly(string path) { try { byte[] assBytes = File.ReadAllBytes(path); // Not just into the Reflection context, because this Load is used to get the name and also to // check that the assembly can Load from bytes (mixed assemblies can't). Assembly ass = Assembly.Load(assBytes); string name = ass.GetName().Name.ToUpperInvariant(); // .ToUpperInvariant().Replace(".", "_"); byte[] data = SevenZipHelper.Compress(assBytes); DoUpdateResource("ASSEMBLY_LZMA", name, data); return(name); } catch (Exception e) { Console.WriteLine("Assembly at " + path + " could not be packed. Possibly a mixed assembly? (These are not supported yet.)\r\nException: " + e); return(null); } }
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 void Do() { try { if (_compressOrDecompress) { _output = SevenZipHelper.Compress(_input); } else { _output = SevenZipHelper.Decompress(_input); } } catch (Exception e) { _exceptionMessage = e.Message; } _over = true; }
private static byte[] internalCompress(byte[] data, InfoByte.CompressionType algo = InfoByte.CompressionType.Arithmetic) { byte[] dataCompr = new byte[0]; switch (algo) { case InfoByte.CompressionType.LZMA: default: dataCompr = SevenZipHelper.Compress(data); break; case InfoByte.CompressionType.BZIP2: using (MemoryStream msOut = new MemoryStream()) { msOut.Write(BitConverter.GetBytes(data.Length), 0, 4); using (ParallelBZip2OutputStream c = new ParallelBZip2OutputStream(msOut, 9, true)) { c.MaxWorkers = 4; c.Write(data, 0, data.Length); c.Flush(); c.Close(); msOut.Flush(); dataCompr = msOut.ToArray(); } } break; case InfoByte.CompressionType.Arithmetic: AbstractModel ac = new ModelOrder0(); //data = threeRLEencode(data); using (MemoryStream mcCompr = new MemoryStream()) { ac.Process(new MemoryStream(data), mcCompr, ModeE.MODE_ENCODE); mcCompr.Flush(); dataCompr = mcCompr.ToArray(); } //dataCompr = threeRLEencode(dataCompr); break; } return(dataCompr); }
static ReplayFile flipNotes(ReplayFile replay) { Console.Clear(); Console.WriteLine("Flipping notes (HR was toggled)..."); string[] list = Encoding.ASCII.GetString(SevenZipHelper.Decompress(replay.Replay)).Split(','); for (int index = 0; index < list.Length; index++) { int percentage = (int)((double)index / list.Length * 100); Console.SetCursorPosition(0, 1); Console.WriteLine(percentage.ToString().PadLeft(3) + "% done."); string[] strArray = list[index].Split('|'); if (strArray.Length == 4) { double num = 384.0 - double.Parse(strArray[2], new CultureInfo("en-US")); strArray[2] = num.ToString(new CultureInfo("en-US")); list[index] = string.Join("|", strArray); } } Console.WriteLine("Compressing replay data..."); replay.Replay = SevenZipHelper.Compress(Encoding.ASCII.GetBytes(string.Join(",", list))); return(replay); }
public void AddImage(byte[] imageBytes, string name) { Debug.Assert(name == name.ToUpperInvariant()); byte[] data = SevenZipHelper.Compress(imageBytes); DoUpdateResource("IMAGE_LZMA", name, data); }
public void AddDnaFile(byte[] dnaContent, string name) { Debug.Assert(name == name.ToUpperInvariant()); byte[] data = SevenZipHelper.Compress(dnaContent); DoUpdateResource("DNA_LZMA", name, data); }
public static bool ExportReplay(Score score, bool userTriggered = true) { string filename; if (userTriggered) { string replaysFolder = Path.Combine(OsuMain.UserPath, @"Replays"); Directory.CreateDirectory(replaysFolder); filename = string.Format(@"{0} - {1} ({2:yyyy-MM-dd}) {3}.osr", score.PlayerName, BeatmapManager.Current.DisplayTitle, score.Date.ToUniversalTime(), score.PlayMode); filename = Path.Combine(replaysFolder, GeneralHelper.WindowsFilenameStrip(filename)); } else { filename = score.ReplayFilename; } bool success = true; if (score.ReplayCompressed == null) { if (score.Replay == null || score.Replay.Count == 0) { success = false; } else { score.ReplayCompressed = SevenZipHelper.Compress(new ASCIIEncoding().GetBytes(score.ReplayString)); } } if (success) { try { using (Stream stream = File.Open(filename, FileMode.Create)) score.WriteToStream(new SerializationWriter(stream)); if (!userTriggered && score.Frames.Count > 0) { using (Stream stream = File.Open(score.GhostFilename, FileMode.Create)) using (SerializationWriter sw = new SerializationWriter(stream)) { sw.Write(General.VERSION); sw.Write(score.Frames); } } } catch (IOException) { success = false; } } if (userTriggered) { if (success) { NotificationManager.ShowMessage(string.Format(LocalisationManager.GetString(OsuString.ScoreManager_SavedReplayToFile), filename), Color.BlueViolet, 6000, delegate { GameBase.OpenFolderToFile(filename); }); } else { NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.ScoreManager_ReplayFailed)); } } return(success); }
private void submit(object sender, DoWorkEventArgs e) { #if ARCADE //Don't submit scores yet, no matter what. return; #endif #if NO_SCORE_SUBMIT NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.Score_SubmissionDisabled)); return; #endif GameBase.User.spriteInfo.Text = LocalisationManager.GetString(OsuString.Score_SubmittingScore); try { byte[] zipped = new byte[0]; if (Pass) { if (BeatmapManager.Current.onlinePersonalScore != null && TotalScore > BeatmapManager.Current.onlinePersonalScore.TotalScore) { BeatmapManager.Current.Scores.Clear(); } //We should re-retrieve online scores no matter what, otherwise when clicking 'retry' the personal score won't be updated. ReplayCompressed = SevenZipHelper.Compress(new ASCIIEncoding().GetBytes(ReplayString)); #if DEBUG if (ReplayCompressed.Length < 100) { LoadLocalData(); } #endif zipped = ReplayCompressed; } pWebRequest req = new pWebRequest(General.WEB_ROOT + @"/web/osu-submit-modular.php"); req.AddFile(@"score", zipped); string iv = null; #if SUBMISSION_DEBUG File.AppendAllText(@"DEBUG.txt", @"Debug at " + DateTime.Now + @"\n"); #endif if (Pass) { Process[] procs = GameBase.Processes; GameBase.Processes = null; if (procs == null || procs.Length == 0) { procs = Process.GetProcesses(); } StringBuilder b = new StringBuilder(); foreach (Process p in procs) { string filename = string.Empty; try { filename = p.MainModule.FileName; FileInfo fi = new FileInfo(filename); if (fi != null) { filename = CryptoHelper.GetMd5String(fi.Length.ToString()) + @" " + filename; } } catch { } b.AppendLine(filename + @" | " + p.ProcessName + @" (" + p.MainWindowTitle + @")"); } #if SUBMISSION_DEBUG File.AppendAllText(@"DEBUG.txt", @"Running Processes:\n" + b + @"\n\n"); #endif req.AddParameter(@"pl", CryptoHelper.EncryptString(b.ToString(), Secrets.GetScoreSubmissionKey(), ref iv)); } else { req.AddParameter(@"x", Exit ? @"1" : @"0"); req.AddParameter(@"ft", FailTime.ToString()); } #if SUBMISSION_DEBUG File.AppendAllText(@"DEBUG.txt", @"\n1:" + onlineFormatted + @"\n"); File.AppendAllText(@"DEBUG.txt", @"\n2:" + GameBase.clientHash + @"\n"); File.AppendAllText(@"DEBUG.txt", @"\n3:" + iv + @"\n"); #endif req.AddParameter(@"score", CryptoHelper.EncryptString(onlineFormatted, Secrets.GetScoreSubmissionKey(), ref iv)); req.AddParameter(@"fs", CryptoHelper.EncryptString(visualSettingsString, Secrets.GetScoreSubmissionKey(), ref iv)); req.AddParameter(@"c1", GameBase.CreateUniqueId()); req.AddParameter(@"pass", ConfigManager.sPassword); req.AddParameter(@"osuver", General.VERSION.ToString()); req.AddParameter(@"s", CryptoHelper.EncryptString(GameBase.ClientHash, Secrets.GetScoreSubmissionKey(), ref iv)); try { if (Pass && ExtraData != null) { req.AddFile(@"i", ExtraData.ToArray()); } else { req.AddParameter(@"i", string.Empty); } } catch { } GameBase.ChangeAllowance++; ExtraData = null; req.AddParameter(@"iv", iv); int retryCount = Pass ? 10 : 2; int retryDelay = 7500; bool didError = false; while (retryCount-- > 0) { try { req.BlockingPerform(); SubmissionResponseString = req.ResponseString; #if SUBMISSION_DEBUG Debug.Print(SubmissionResponseString); File.AppendAllText(@"DEBUG.txt", @"\nres:" + SubmissionResponseString + @"\n\n\n\n\n-------------------\n\n\n\n"); #endif if (SubmissionResponseString.Contains(@"error:")) { switch (SubmissionResponseString.Replace(@"error: ", string.Empty)) { case @"reset": BanchoClient.HandlePasswordReset(); break; case @"verify": BanchoClient.RequireVerification(); break; case @"nouser": NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.Score_ErrorNoUser)); break; case @"pass": NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.Score_ErrorPassword)); break; case @"inactive": case @"ban": NotificationManager.ShowMessage("ERROR: Your account is no longer active. Please send an email to [email protected] if you think this is a mistake."); break; case @"beatmap": if (Beatmap != null && Beatmap.SubmissionStatus > osu_common.SubmissionStatus.Pending) { NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.Score_ErrorBeatmap)); Beatmap.SubmissionStatus = osu_common.SubmissionStatus.Unknown; } break; case @"disabled": NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.Score_ErrorDisabled)); break; case @"oldver": NotificationManager.ShowMessage(LocalisationManager.GetString(OsuString.Score_ErrorVersion)); GameBase.CheckForUpdates(true); break; case @"no": break; } didError = true; } break; } catch { } if (retryDelay >= 60000) { NotificationManager.ShowMessage(string.Format(LocalisationManager.GetString(OsuString.Score_SubmissionFailed), (retryDelay / 60000))); } Thread.Sleep(retryDelay); retryDelay *= 2; } if (didError) { SubmissionStatus = ScoreSubmissionStatus.Complete; return; } } catch (Exception ex) { } if (SubmissionComplete != null) { SubmissionComplete(this); } SubmissionStatus = ScoreSubmissionStatus.Complete; if (!Pass) { GameBase.User.Refresh(); } }
internal static int InsertScore(Score score, bool checkOnly, bool saveReplay) { if (score == null) { return(-1); } int higherCount = 0; int count = 0; Score lowestHigherScore = null; Score lowestLowerScore = null; List <Score> songList = scoreList.FindAll(s => s.fileChecksum.Equals(score.fileChecksum)); foreach (Score s in songList) { count++; if (s.totalScore >= score.totalScore) { /*if (higherCount > 0) * s.replay = null;*/ higherCount++; if (lowestHigherScore == null || s.totalScore < lowestHigherScore.totalScore) { lowestHigherScore = s; } if (higherCount >= ScoresPerSong) { return(-1); } } else if (lowestLowerScore == null || s.totalScore < lowestLowerScore.totalScore) { lowestLowerScore = s; } //Found duplicate (even though the if (s.offlineScoreChecksum == score.offlineScoreChecksum) { return(count); } } if (!checkOnly) { if (lowestLowerScore != null && count >= ScoresPerSong) { scoreList.Remove(lowestLowerScore); } if (saveReplay) { try { if (score.rawReplayCompressed == null) { score.rawReplayCompressed = SevenZipHelper.Compress(new ASCIIEncoding().GetBytes(score.replayFormatted)); } } catch { } } else { score.rawReplayCompressed = null; } scoreList.Add(score); scoreList.Sort((s2, s1) => s1.totalScore.CompareTo(s2.totalScore)); } return(higherCount + 1); }
public bool Pack(AssetsFileReader reader, AssetsFileWriter writer, AssetBundleCompressionType compType, bool blockDirAtEnd = true) { reader.Position = 0; writer.Position = 0; if (Read(reader, false)) { AssetBundleHeader06 newHeader = new AssetBundleHeader06() { signature = bundleHeader6.signature, fileVersion = bundleHeader6.fileVersion, minPlayerVersion = bundleHeader6.minPlayerVersion, fileEngineVersion = bundleHeader6.fileEngineVersion, totalFileSize = 0, compressedSize = 0, decompressedSize = 0, flags = (uint)(0x43 | (blockDirAtEnd ? 0x80 : 0x00)) }; AssetBundleBlockAndDirectoryList06 newBlockAndDirList = new AssetBundleBlockAndDirectoryList06() { checksumLow = 0, checksumHigh = 0, blockCount = 0, blockInf = null, directoryCount = bundleInf6.directoryCount, dirInf = bundleInf6.dirInf }; //write header now and overwrite it later long startPos = writer.Position; newHeader.Write(writer); if (newHeader.fileVersion >= 7) { writer.Align16(); } int headerSize = (int)(writer.Position - startPos); long totalCompressedSize = 0; List <AssetBundleBlockInfo06> newBlocks = new List <AssetBundleBlockInfo06>(); List <Stream> newStreams = new List <Stream>(); //used if blockDirAtEnd == false long fileDataOffset = bundleHeader6.GetFileDataOffset(); int fileDataLength = (int)(bundleHeader6.totalFileSize - fileDataOffset); SegmentStream bundleDataStream = new SegmentStream(reader.BaseStream, fileDataOffset, fileDataLength); switch (compType) { case AssetBundleCompressionType.LZMA: { Stream writeStream; if (blockDirAtEnd) { writeStream = writer.BaseStream; } else { writeStream = GetTempFileStream(); } long writeStreamStart = writeStream.Position; SevenZipHelper.Compress(bundleDataStream, writeStream); uint writeStreamLength = (uint)(writeStream.Position - writeStreamStart); AssetBundleBlockInfo06 blockInfo = new AssetBundleBlockInfo06() { compressedSize = writeStreamLength, decompressedSize = (uint)fileDataLength, flags = 0x41 }; totalCompressedSize += blockInfo.compressedSize; newBlocks.Add(blockInfo); if (!blockDirAtEnd) { newStreams.Add(writeStream); } break; } case AssetBundleCompressionType.LZ4: { //compress into 0x20000 blocks BinaryReader bundleDataReader = new BinaryReader(bundleDataStream); byte[] uncompressedBlock = bundleDataReader.ReadBytes(0x20000); while (uncompressedBlock.Length != 0) { Stream writeStream; if (blockDirAtEnd) { writeStream = writer.BaseStream; } else { writeStream = GetTempFileStream(); } byte[] compressedBlock = LZ4Codec.Encode32HC(uncompressedBlock, 0, uncompressedBlock.Length); if (compressedBlock.Length > uncompressedBlock.Length) { writeStream.Write(uncompressedBlock, 0, uncompressedBlock.Length); AssetBundleBlockInfo06 blockInfo = new AssetBundleBlockInfo06() { compressedSize = (uint)uncompressedBlock.Length, decompressedSize = (uint)uncompressedBlock.Length, flags = 0x0 }; totalCompressedSize += blockInfo.compressedSize; newBlocks.Add(blockInfo); } else { writeStream.Write(compressedBlock, 0, compressedBlock.Length); AssetBundleBlockInfo06 blockInfo = new AssetBundleBlockInfo06() { compressedSize = (uint)compressedBlock.Length, decompressedSize = (uint)uncompressedBlock.Length, flags = 0x3 }; totalCompressedSize += blockInfo.compressedSize; newBlocks.Add(blockInfo); } if (!blockDirAtEnd) { newStreams.Add(writeStream); } uncompressedBlock = bundleDataReader.ReadBytes(0x20000); } break; } case AssetBundleCompressionType.NONE: { AssetBundleBlockInfo06 blockInfo = new AssetBundleBlockInfo06() { compressedSize = (uint)fileDataLength, decompressedSize = (uint)fileDataLength, flags = 0x00 }; totalCompressedSize += blockInfo.compressedSize; newBlocks.Add(blockInfo); if (blockDirAtEnd) { bundleDataStream.CopyToCompat(writer.BaseStream); } else { newStreams.Add(bundleDataStream); } break; } } newBlockAndDirList.blockInf = newBlocks.ToArray(); byte[] bundleInfoBytes; using (MemoryStream memStream = new MemoryStream()) { AssetsFileWriter infoWriter = new AssetsFileWriter(memStream); newBlockAndDirList.Write(infoWriter); bundleInfoBytes = memStream.ToArray(); } //listing is usually lz4 even if the data blocks are lzma byte[] bundleInfoBytesCom = LZ4Codec.Encode32HC(bundleInfoBytes, 0, bundleInfoBytes.Length); long totalFileSize = headerSize + bundleInfoBytesCom.Length + totalCompressedSize; newHeader.totalFileSize = totalFileSize; newHeader.decompressedSize = (uint)bundleInfoBytes.Length; newHeader.compressedSize = (uint)bundleInfoBytesCom.Length; if (!blockDirAtEnd) { writer.Write(bundleInfoBytesCom); foreach (Stream newStream in newStreams) { newStream.Position = 0; newStream.CopyToCompat(writer.BaseStream); newStream.Close(); } } else { writer.Write(bundleInfoBytesCom); } writer.Position = 0; newHeader.Write(writer); if (newHeader.fileVersion >= 7) { writer.Align16(); } return(true); } return(false); }
private void ThreadCompression() { byte[] inputBlock, outputBlock; uint length; uint actualUncompressedBlockSize; uint actualCompressedBlockSize; int localblockcount; while (true) { lock (Locker) { if (tempLength <= 0) { break; } length = tempLength; if (tempLength > sfarMaxBlockSize) { actualCompressedBlockSize = sfarMaxBlockSize; actualUncompressedBlockSize = sfarMaxBlockSize; tempLength -= sfarMaxBlockSize; } else { actualCompressedBlockSize = length; actualUncompressedBlockSize = length; tempLength = 0; } inputBlock = new byte[actualUncompressedBlockSize]; inStream.Read(inputBlock, 0, (int)actualUncompressedBlockSize); localblockcount = blockCounter++; } outputBlock = SevenZipHelper.Compress(inputBlock); actualCompressedBlockSize = (uint)outputBlock.Length; uint maxCompressedBlockSize = actualCompressedBlockSize; if (maxCompressedBlockSize >= actualUncompressedBlockSize || maxCompressedBlockSize >= sfarMaxBlockSize) { byte[] buff = new byte[actualUncompressedBlockSize]; Buffer.BlockCopy(outputBlock, 0, buff, 0, (int)actualUncompressedBlockSize); outputBlock = buff; if (length < sfarMaxBlockSize) { maxCompressedBlockSize = (ushort)actualUncompressedBlockSize; } else { maxCompressedBlockSize = 0; } } if (outputBlock.Length == 0) { throw new Exception(); } //if (localblockcount == 243) // throw new Exception(); compressedArray[localblockcount] = outputBlock; blockSizes[localblockcount] = (ushort)maxCompressedBlockSize; //lock (BlockLocker) //{ // compressedArray[blockCounter] = outputBlock; // blockSizes[blockCounter++] = (ushort)maxCompressedBlockSize; //} } }
public void Write(AssetsFileWriter writer, int optimizeStringTable = 1, int compress = 1) { long filePos = writer.BaseStream.Position; //lol don't do this if compress is 0 if ((compress & 0x80) == 0) { throw new NotImplementedException("Compression flag 0x80 must be used"); } //compress 1 for lz4 and 2 for lzma //this is backwards from assets files //build string table StringBuilder strTableBuilder = new StringBuilder(); Dictionary <string, uint> strTableMap; if (optimizeStringTable != 0) { strTableMap = new Dictionary <string, uint>(); } else { strTableMap = null; } foreach (ClassDatabaseFile cldb in files) { for (int i = 0; i < cldb.classes.Count; i++) { ClassDatabaseType type = cldb.classes[i]; AddStringTableEntry(cldb, strTableBuilder, strTableMap, ref type.name); if (header.fileVersion == 4 && (cldb.header.flags & 1) != 0) { AddStringTableEntry(cldb, strTableBuilder, strTableMap, ref type.assemblyFileName); } List <ClassDatabaseTypeField> fields = type.fields; for (int j = 0; j < fields.Count; j++) { ClassDatabaseTypeField field = fields[j]; AddStringTableEntry(cldb, strTableBuilder, strTableMap, ref field.fieldName); AddStringTableEntry(cldb, strTableBuilder, strTableMap, ref field.typeName); fields[j] = field; } } } header.fileCount = (uint)files.Count; header.Write(writer); using (MemoryStream cldbMs = new MemoryStream()) using (AssetsFileWriter cldbWriter = new AssetsFileWriter(cldbMs)) { //annoyingly, files and header.files are two different lists... for (int i = 0; i < files.Count; i++) { ClassDatabaseFile cldb = files[i]; long cldbStartFilePos = cldbWriter.BaseStream.Position; //does not support 0x80 self compression rn cldb.Write(cldbWriter, 0, 0, false); long cldbEndFilePos = cldbWriter.BaseStream.Position; string cldbName = header.files[i].name; header.files[i] = new ClassDatabaseFileRef() { offset = (uint)cldbStartFilePos, length = (uint)(cldbEndFilePos - cldbStartFilePos), name = cldbName }; } header.fileBlockSize = (uint)cldbMs.Length; cldbMs.Position = 0; if ((compress & 0x20) == 0) //compressed { if ((compress & 0x1f) == 1) //lz4 { byte[] compressedBlock = LZ4Codec.Encode32HC(cldbMs.ToArray(), 0, (int)cldbMs.Length); writer.Write(compressedBlock); } else if ((compress & 0x1f) == 2) //lzma { byte[] compressedBlock = SevenZipHelper.Compress(cldbMs.ToArray()); writer.Write(compressedBlock); } else { throw new ArgumentException("File marked as compressed but no valid compression option set!"); } } else //write normally { cldbMs.CopyToCompat(writer.BaseStream); } } header.stringTableOffset = (uint)writer.Position; byte[] stringTableBytes = Encoding.ASCII.GetBytes(strTableBuilder.ToString()); header.stringTableLenUncompressed = (uint)stringTableBytes.Length; if ((compress & 0x40) == 0) //string table is compressed { if ((compress & 0x1f) == 1) //lz4 { stringTableBytes = LZ4Codec.Encode32HC(stringTableBytes, 0, stringTableBytes.Length); } else if ((compress & 0x1f) == 2) //lzma { stringTableBytes = SevenZipHelper.Compress(stringTableBytes); } else { throw new ArgumentException("File marked as compressed but no valid compression option set!"); } } header.stringTableLenCompressed = (uint)stringTableBytes.Length; writer.Write(stringTableBytes); writer.Position = filePos; header.compressionType = (byte)compress; header.Write(writer); }
private void DoPack() { var tmpFile = Path.GetTempFileName(); using (var stream = File.OpenWrite(tmpFile)) using (var writer = new BinaryWriter(stream)) { writer.Write(AppPack.Version); writer.Write(AppPack.Identifier); writer.Write((ushort)mPack.TargetPlatform); writer.Write((uint)mPack.ApplicationId); writer.Write((uint)mPack.ApplicationVersion); var assemblies = (mPack.Assemblies != null) ? mPack.Assemblies.Where(x => x != null) : null; if (assemblies == null || !assemblies.Any()) { // No assemblies writer.Write((byte)0); } else { writer.Write((byte)assemblies.Count()); foreach (var asm in assemblies) { writer.Write(asm.Length); writer.Write(asm); } } writer.Write(mPack.SceneBundle.Length); writer.Write(mPack.SceneBundle); } // Compress using (var inStream = File.OpenRead(tmpFile)) using (var outStream = File.Create(mOutputPath)) { switch (mPack.CompressionType) { case ECompressionType.LMZA: SevenZipHelper.Compress(inStream, outStream); break; case ECompressionType.Uncompressed: inStream.CopyTo(outStream); break; default: SevenZipHelper.Compress(inStream, outStream); break; } } // Delete the temoprary file if it's still there... if (File.Exists(tmpFile)) { File.Delete(tmpFile); } }
private void UdpSenderWorkMethod(object obj) { if ((obj is UdpSenderWorkMethodParams) == false) { return; } UdpSenderWorkMethodParams udpSenderWorkMethodParams = (UdpSenderWorkMethodParams)obj; if (udpSenderWorkMethodParams.Socket == null || udpSenderWorkMethodParams.RemoteEP == null || udpSenderWorkMethodParams.Message == null) { return; } try { byte[] data = ConvertMessageToBytes(udpSenderWorkMethodParams.Message); data = SevenZipHelper.Compress(data); if (udpSenderWorkMethodParams.Message.HeaderData.MsgDataType != EMsgDataType.ReceiveConfirmation) { int refNo = udpSenderWorkMethodParams.Message.HeaderData.RefNo; bool receiveConfirmationReceived = false; //Send message max three times. Stop if confirmation is received. for (int i = 0; i < 3; i++) { udpSenderWorkMethodParams.Socket.SendTo(data, udpSenderWorkMethodParams.RemoteEP); //Check for confirmation for 5 sec int j = 0; while (true) { if (ReceivedConfirmationList != null && ReceivedConfirmationList.Contains(refNo)) { receiveConfirmationReceived = true; break; } //Wait for 10 micro sec Thread.Sleep(1000); j++; if (j == 5000) { receiveConfirmationReceived = false; break; } } if (receiveConfirmationReceived) { break; } } if (receiveConfirmationReceived == false) { NotifyFailedTransmission(udpSenderWorkMethodParams); } else { lock (ReceivedConfirmationList) { ReceivedConfirmationList.Remove(refNo); } } } else { udpSenderWorkMethodParams.Socket.SendTo(data, udpSenderWorkMethodParams.RemoteEP); } } catch (Exception ex) { Console.WriteLine(ex.Message); } udpSenderWorkMethodParams.Socket.Close(); }
override public byte[] Compress(byte[] inputbuffer) { return(SevenZipHelper.Compress(inputbuffer)); }
//when dealing with byte streams (what we call Raw in the GUI) it's always //good to have a byte buffer around. we'll use it when copying the data. //readonly byte[] FBuffer = new byte[1024]; private void CompressAsync(object io) { CompressorIO IO = (CompressorIO)io; IO.OutBytes = SevenZipHelper.Compress(IO.InBytes); }
private void doSendScore(object sender, DoWorkEventArgs e) { GameBase.User.spriteInfo.Text = "Submitting score..."; try { byte[] zipped = new byte[0]; if (pass) { rawReplayCompressed = SevenZipHelper.Compress(new ASCIIEncoding().GetBytes(replayFormatted)); zipped = rawReplayCompressed; /*MemoryStream s = new MemoryStream(); * DeflaterOutputStream ds = new DeflaterOutputStream(s, new Deflater(9)); * * ds.Write(bytes, 0, bytes.Length); * ds.Finish(); * ds.Flush(); * zipped = s.ToArray();*/ } FileUploadNetRequest fileUpload = new FileUploadNetRequest( string.Format("http://osu.ppy.sh/web/osu-submit.php?score={0}&pass={1}", onlineFormatted, ConfigManager.sPassword), zipped, "replay", "score"); int retryCount = 1; while (retryCount > 0) { try { string[] response = fileUpload.BlockingPerform().Split('\n'); if (response.Length > 0) { Int32.TryParse(response[0], out onlineRank); Int32.TryParse(response[1], out GameBase.User.NextRank); } if (response.Length > 2) { GameBase.User.AchievementImages = response[2].Split(' '); } else { GameBase.User.AchievementImages = null; } break; } catch { } retryCount--; } } catch (Exception ex) { Console.WriteLine(ex); } if (SubmissionComplete != null) { SubmissionComplete(this); } if (!pass) { GameBase.User.Refresh(); } else { BeatmapManager.GetOnlineBeatmapInfo(BeatmapManager.Current); } }
private void ProcessAssembly(ModuleDefMD module) { ReferenceCopyLocalPaths .Select(x => x.ItemSpec) .Where(referenceCopyLocalFile => !MergeConfig.ExcludeReferences.Select(v => v.ToLower()).Any(excludedRef => referenceCopyLocalFile.ToLower().EndsWith(excludedRef))).ToList() .ForEach(referenceCopyLocalFile => { if (referenceCopyLocalFile.ToLower().EndsWith(".dll")) { try { var referenceAssemblyData = File.ReadAllBytes(referenceCopyLocalFile); var refModule = ModuleDefMD.Load(referenceAssemblyData); module.Resources.Add(new EmbeddedResource(refModule.Assembly.Name.ToLower(), SevenZipHelper.Compress(referenceAssemblyData))); Logger.LogMessage($"Merged assembly {referenceCopyLocalFile}"); } catch (Exception e) { Log.LogMessage(MessageImportance.High, $"Failed to merge assembly {referenceCopyLocalFile} with error {e.Message}"); } } }); }
/// <summary> /// Encodes the raw data /// </summary> /// <param name="inputStream">The raw data stream</param> /// <param name="outputStream">The output stream</param> /// <returns>The encrypted data</returns> public void Encode(Stream inputStream, Stream outputStream) { SevenZipHelper.Compress(inputStream, outputStream); }
public bool Pack(AssetsFileReader reader, AssetsFileWriter writer, AssetBundleCompressionType compType) { reader.Position = 0; writer.Position = 0; if (Read(reader, false)) { AssetBundleHeader06 newHeader = new AssetBundleHeader06() { signature = bundleHeader6.signature, fileVersion = bundleHeader6.fileVersion, minPlayerVersion = bundleHeader6.minPlayerVersion, fileEngineVersion = bundleHeader6.fileEngineVersion, totalFileSize = 0, compressedSize = 0, decompressedSize = 0, flags = 0x43 }; AssetBundleBlockAndDirectoryList06 newBlockAndDirList = new AssetBundleBlockAndDirectoryList06() { checksumLow = 0, checksumHigh = 0, blockCount = 0, blockInf = null, directoryCount = bundleInf6.directoryCount, dirInf = bundleInf6.dirInf }; List <AssetBundleBlockInfo06> newBlocks = new List <AssetBundleBlockInfo06>(); reader.Position = bundleHeader6.GetFileDataOffset(); int fileDataLength = (int)(bundleHeader6.totalFileSize - reader.Position); byte[] fileData = reader.ReadBytes(fileDataLength); //todo, we just write everything to memory and then write to file //we could calculate the blocks we need ahead of time and correctly //size the block listing before this so we can write directly to file byte[] compressedFileData; switch (compType) { case AssetBundleCompressionType.LZMA: { compressedFileData = SevenZipHelper.Compress(fileData); newBlocks.Add(new AssetBundleBlockInfo06() { compressedSize = (uint)compressedFileData.Length, decompressedSize = (uint)fileData.Length, flags = 0x41 }); break; } case AssetBundleCompressionType.LZ4: { using (var memStreamCom = new MemoryStream()) using (var binaryWriter = new BinaryWriter(memStreamCom)) { using (var memStreamUnc = new MemoryStream(fileData)) using (var binaryReader = new BinaryReader(memStreamUnc)) { //compress into 0x20000 blocks byte[] uncompressedBlock = binaryReader.ReadBytes(131072); while (uncompressedBlock.Length != 0) { byte[] compressedBlock = LZ4Codec.Encode32HC(uncompressedBlock, 0, uncompressedBlock.Length); if (compressedBlock.Length > uncompressedBlock.Length) { newBlocks.Add(new AssetBundleBlockInfo06() { compressedSize = (uint)uncompressedBlock.Length, decompressedSize = (uint)uncompressedBlock.Length, flags = 0x0 }); binaryWriter.Write(uncompressedBlock); } else { newBlocks.Add(new AssetBundleBlockInfo06() { compressedSize = (uint)compressedBlock.Length, decompressedSize = (uint)uncompressedBlock.Length, flags = 0x3 }); binaryWriter.Write(compressedBlock); } uncompressedBlock = binaryReader.ReadBytes(131072); } } compressedFileData = memStreamCom.ToArray(); } break; } case AssetBundleCompressionType.NONE: { compressedFileData = fileData; newBlocks.Add(new AssetBundleBlockInfo06() { compressedSize = (uint)fileData.Length, decompressedSize = (uint)fileData.Length, flags = 0x00 }); break; } default: { return(false); } } newBlockAndDirList.blockInf = newBlocks.ToArray(); byte[] bundleInfoBytes; using (var memStream = new MemoryStream()) { var afw = new AssetsFileWriter(memStream); newBlockAndDirList.Write(afw); bundleInfoBytes = memStream.ToArray(); } if (bundleInfoBytes == null || bundleInfoBytes.Length == 0) { return(false); } //listing is usually lz4 even if the data blocks are lzma byte[] bundleInfoBytesCom = LZ4Codec.Encode32HC(bundleInfoBytes, 0, bundleInfoBytes.Length); byte[] bundleHeaderBytes = null; using (var memStream = new MemoryStream()) { var afw = new AssetsFileWriter(memStream); newHeader.Write(afw); bundleHeaderBytes = memStream.ToArray(); } if (bundleHeaderBytes == null || bundleHeaderBytes.Length == 0) { return(false); } uint totalFileSize = (uint)(bundleHeaderBytes.Length + bundleInfoBytesCom.Length + compressedFileData.Length); newHeader.totalFileSize = totalFileSize; newHeader.decompressedSize = (uint)bundleInfoBytes.Length; newHeader.compressedSize = (uint)bundleInfoBytesCom.Length; newHeader.Write(writer); if (newHeader.fileVersion >= 7) { writer.Align16(); } writer.Write(bundleInfoBytesCom); writer.Write(compressedFileData); return(true); } return(false); }
public static void CompressFile(Stream inStream, out ushort[] blockSizes, MemoryStream outStream) { #endif if (inStream == null) { throw new NullReferenceException("Error: null input stream"); } if (!inStream.CanRead) { throw new IOException("Error: not an input readable stream"); } if (outStream == null) { throw new NullReferenceException("Error: null output stream"); } if (!outStream.CanWrite) { throw new IOException("Error: not an output writable stream"); } int fileLength = (int)inStream.Length; blockSizes = new ushort[(int)Math.Ceiling((double)fileLength / (double)sfarMaxBlockSize)]; byte[] inputBlock; byte[] outputBlock = new byte[sfarMaxBlockSize + 100]; uint templength = (uint)fileLength; int blockCounter = 0; uint actualUncompressedBlockSize; uint actualCompressedBlockSize; while (templength > 0) { if (templength > sfarMaxBlockSize) { actualUncompressedBlockSize = sfarMaxBlockSize; actualCompressedBlockSize = sfarMaxBlockSize; } else { actualUncompressedBlockSize = templength; actualCompressedBlockSize = templength; } inputBlock = new byte[actualUncompressedBlockSize]; inStream.Read(inputBlock, 0, (int)actualUncompressedBlockSize); outputBlock = SevenZipHelper.Compress(inputBlock); actualCompressedBlockSize = (uint)outputBlock.Length; #if (WITH_GUI) if (worker != null) { int perc = (int)Math.Ceiling((float)count++ / (float)blockSizes.Length * 100); if (perc > highPerc) { highPerc = perc; if (perc > 100) { perc = 100; } worker.ReportProgress(perc); } } #endif /*var error = LZMA.Compress( * inputBlock, * ref actualCompressedBlockSize, * outputBlock, * actualUncompressedBlockSize, * properties, * ref prLength); * * //Console.WriteLine(" properties: {0}",String.Concat(Array.ConvertAll(properties, x => x.ToString("X2"))) ); * if (error != LZMA.ErrorCode.Ok) * { * Console.WriteLine("error: {0}",error); * throw new InvalidOperationException(); * }*/ uint maxCompressedBlockSize = (uint)outputBlock.Length; //maxCompressedBlockSize += prLength; if (maxCompressedBlockSize >= actualUncompressedBlockSize || maxCompressedBlockSize >= sfarMaxBlockSize) { outStream.Write(inputBlock, 0, (int)actualUncompressedBlockSize); if (templength < sfarMaxBlockSize) { ushort mini = (ushort)actualUncompressedBlockSize; maxCompressedBlockSize = mini; } else { maxCompressedBlockSize = 0; } } else { //outStream.Write(properties, 0, properties.Length); // Temporary hack to get around outofmemoryexception - we'll see how it goes while (true) { try { outStream.Write(outputBlock, 0, (int)actualCompressedBlockSize); break; } catch (OutOfMemoryException) { MessageBox.Show("Memory overuse detected!\nTotal in use: " + GC.GetTotalMemory(false)); GC.Collect(); MessageBox.Show("New memory use: " + GC.GetTotalMemory(true)); } } } ushort finalCompressedBlockSize = (ushort)maxCompressedBlockSize; blockSizes[blockCounter++] = finalCompressedBlockSize; //updating the pointer to the last position of Block Size if (templength > sfarMaxBlockSize) { templength -= sfarMaxBlockSize; } else { templength = 0; } } // end of while } //end of function