public static FormatCompress Get_Format(string input) { CompressionFormat fmt = null; foreach (FormatCompress f in Enum.GetValues(typeof(FormatCompress))) { switch (f) { //case FormatCompress.LZOVL: fmt = new LZOvl(); break; case FormatCompress.LZ10: fmt = new LZ10(); break; case FormatCompress.LZ11: fmt = new LZ11(); break; case FormatCompress.RLE: fmt = new RLE(); break; case FormatCompress.HUFF: fmt = new Huffman(); break; } if (fmt == null) { continue; } if (fmt.Supports(input)) { return(f); } } return(FormatCompress.Invalid); }
public void Write(Stream input, uint offset) { using (var bw = new BinaryWriterX(input, true)) { var startOffset = bw.BaseStream.Position; bw.BaseStream.Position = offset; if (State == ArchiveFileState.Archived) { entry.offset = offset; base.FileData.CopyTo(bw.BaseStream); } else { if ((entry.size & 0x80000000) == 0) { entry.offset = offset; base.FileData.CopyTo(bw.BaseStream); entry.size = (uint)base.FileData.Length; } else { entry.offset = offset; var comp = LZ11.Compress(base.FileData); bw.Write(comp); entry.size = (uint)(comp.Length | 0x80000000); } } bw.WriteAlignment(0x4); bw.BaseStream.Position = startOffset; } }
public void Save(Stream input) { ImageSettings modSettings = settings; modSettings.Width = Image.Width; modSettings.Height = Image.Height; byte[] data = Common.Save(Image, modSettings); using (BinaryWriterX br = new BinaryWriterX(new MemoryStream())) { header.width = (ushort)Image.Width; header.height = (ushort)Image.Height; br.WriteStruct <Header>(header); br.BaseStream.Position = header.dataStart; br.Write(data); br.BaseStream.Position = 0; if (lz11_compressed) { byte[] comp = LZ11.Compress(br.BaseStream); input.Write(comp, 0, comp.Length); } else { input.Write(new BinaryReaderX(br.BaseStream).ReadBytes((int)br.BaseStream.Length), 0, (int)br.BaseStream.Length); } input.Close(); } }
private static byte[] ComprimirComLZ11(LZ11 LZ11, byte[] arquivo) { MemoryStream output = new MemoryStream(); LZ11.Compress(new MemoryStream(arquivo), arquivo.Length, output); return(output.ToArray()); }
public RawJTEX(Stream input) { using (var br = new BinaryReaderX(input)) { Stream stream; if (br.ReadByte() == 0x11) { br.BaseStream.Position = 0; uint size = br.ReadUInt32() >> 8; br.BaseStream.Position = 0; lz11_compressed = true; byte[] decomp = LZ11.Decompress(br.BaseStream); stream = new MemoryStream(decomp); } else { br.BaseStream.Position = 0; stream = br.BaseStream; } //File.OpenWrite("test.decomp").Write(new BinaryReaderX(stream).ReadBytes((int)stream.Length), 0, (int)stream.Length); using (BinaryReaderX br2 = new BinaryReaderX(stream)) { JTEXRawHeader = br2.ReadStruct <RawHeader>(); br2.BaseStream.Position = JTEXRawHeader.dataStart; Settings = new ImageSettings { Width = JTEXRawHeader.width, Height = JTEXRawHeader.height, Format = JTEXRawHeader.format }; Image = Common.Load(br2.ReadBytes((int)(br2.BaseStream.Length - br2.BaseStream.Position)), Settings); } } }
private Task <SaveThemeResults> SaveTheme_Execute(string path, bool start) { var viewModel = ViewModel; var busyPickingFile = MainResources.Busy_PickingFile; var busySavingTheme = MainResources.Busy_SavingTheme; var task = new Task <SaveThemeResults>(() => { BusyText = busyPickingFile; var result = new SaveThemeResults { Saved = false, Path = path }; if (string.IsNullOrEmpty(result.Path)) { var svfl = new SaveFileDialog { Filter = "3DS Theme File|*.bin", FileName = "body_lz" }; var dlg = svfl.ShowDialog(); if (dlg.HasValue && !dlg.Value) { return(result); } result.Path = svfl.FileName; } BusyText = busySavingTheme; using (var decBuffer = new MemoryStream()) using (var comBuffer = new MemoryStream()) { try { viewModel.Save(decBuffer); decBuffer.Position = 0; LZ11.Compress(decBuffer, decBuffer.Length, comBuffer, true); comBuffer.Position = 0; using (var fs = File.Open(result.Path, FileMode.Create)) comBuffer.CopyTo(fs); result.Saved = true; } catch { // Ignore } } return(result); }, TaskCreationOptions.LongRunning); if (start) { task.Start(); } return(task); }
public bool Identify(string filename) { using (var br = new BinaryReaderX(File.OpenRead(filename))) { if (br.BaseStream.Length < 4) { return(false); } //check for compression if (br.ReadByte() == 0x11) { br.BaseStream.Position = 0; uint size = br.ReadUInt32() >> 8; br.BaseStream.Position = 0; byte[] decomp = LZ11.Decompress(br.BaseStream); if (decomp.Length == size) { if (new BinaryReaderX(new MemoryStream(decomp)).ReadString(4) == "F3XT") { return(true); } } } br.BaseStream.Position = 0; return(br.ReadString(4) == "F3XT"); } }
static void Test(byte[] bytes, Method method) { byte[] bytes2 = new byte[bytes.Length]; switch (method) { case Method.LZ10: bytes2 = LZ10.Decompress(new MemoryStream(LZ10.Compress(new MemoryStream(bytes))), bytes.Length); Assert.IsTrue(bytes.SequenceEqual(bytes2)); break; case Method.LZ11: bytes2 = LZ11.Decompress(new MemoryStream(LZ11.Compress(new MemoryStream(bytes))), bytes.Length); Assert.IsTrue(bytes.SequenceEqual(bytes2)); break; case Method.LZ40: bytes2 = LZ40.Decompress(new MemoryStream(LZ40.Compress(new MemoryStream(bytes))), bytes.Length); Assert.IsTrue(bytes.SequenceEqual(bytes2)); break; case Method.LZ77: bytes2 = LZ77.Decompress(new MemoryStream(LZ77.Compress(new MemoryStream(bytes)))); Assert.IsTrue(bytes.SequenceEqual(bytes2)); break; case Method.RevLZ77: bytes2 = RevLZ77.Decompress(new MemoryStream(RevLZ77.Compress(new MemoryStream(bytes)))); Assert.IsTrue(bytes.SequenceEqual(bytes2)); break; case Method.LZ4: bytes2 = LZ4.Decompress(new MemoryStream(LZ4.Compress(new MemoryStream(bytes)))); Assert.IsTrue(bytes.SequenceEqual(bytes2)); break; case Method.LZECD: bytes2 = LZECD.Decompress(new MemoryStream(LZECD.Compress(new MemoryStream(bytes)))); Assert.IsTrue(bytes.SequenceEqual(bytes2)); break; case Method.LZOvl: bytes2 = LZOvl.Decompress(new MemoryStream(LZOvl.Compress(new MemoryStream(bytes)))); Assert.IsTrue(bytes.SequenceEqual(bytes2)); break; case Method.MIO0: bytes2 = MIO0.Decompress(new MemoryStream(MIO0.Compress(new MemoryStream(bytes), ByteOrder.LittleEndian)), ByteOrder.LittleEndian); Assert.IsTrue(bytes.SequenceEqual(bytes2)); break; } }
public AAIBin(string dirBin) { _lz11 = new LZ11(); BinPath = dirBin; using (BinaryReader br = new BinaryReader(File.OpenRead(dirBin))) { Entries = ReadEntries(br); } if (IsExportNeeded(dirBin)) { ExportFiles(dirBin, ExportPath); } }
private static byte[] DecompressOrCompress(LZ11 LZ11, byte[] arquivo, bool compress) { MemoryStream output = new MemoryStream(); if (compress) { _ = LZ11.Compress(new MemoryStream(arquivo), arquivo.Length, output); } else { _ = LZ11.Decompress(new MemoryStream(arquivo), arquivo.Length, output); } return(output.ToArray()); }
public static void Exportar(string dirBin) { string subPasta = dirBin.Contains("jpn") ? "jpn" : "com"; string dirExt = $"__Binarios\\{subPasta}_{Path.GetFileName(dirBin).Replace(".bin", "")}"; var listaDeArquivos = new List <string>(); if (!Directory.Exists(dirExt)) { Directory.CreateDirectory(dirExt); } LZ11 lZ11 = new LZ11(); using (BinaryReader br = new BinaryReader(File.Open(dirBin, FileMode.Open))) { List <EntradaAAIBin> entradas = LerEntradas(br); int contador = 0; foreach (EntradaAAIBin entrada in entradas) { br.BaseStream.Position = entrada.Endereco; byte[] arquivo = br.ReadBytes((int)entrada.Tamanho); if (entrada.Comprimido) { arquivo = DescomprimirComLZ11(lZ11, arquivo); } int ext = 0; if (arquivo.Length >= 4) { ext = BitConverter.ToInt32(arquivo, 0); } string extensao = ext == 0 ? ".bin" : ObtenhaExtensao(ext); string prefixoCompressao = entrada.Comprimido ? "LZ11_" : ""; File.WriteAllBytes($"{dirExt}\\{contador.ToString("0000")}_{Path.GetFileName(dirBin).Replace(".bin", extensao)}", arquivo); listaDeArquivos.Add($"{dirExt}\\{contador.ToString("0000")}{prefixoCompressao}{Path.GetFileName(dirBin).Replace(".bin", extensao)}"); contador++; } File.WriteAllLines($"__Binarios\\_InfoBinarios\\{subPasta}_{Path.GetFileName(dirBin).Replace(".bin", ".txt")}", listaDeArquivos); } }
private Task <LoadThemeResults> LoadNullTheme_Execute(bool start) { var busyLoadingTheme = MainResources.Busy_LoadingTheme; var task = new Task <LoadThemeResults>(() => { var result = new LoadThemeResults { Loaded = false, Path = null, }; BusyText = busyLoadingTheme; Stream fs; if (!File.Exists("default_body_lz.bin")) { var res = Extensions.GetResources("body_lz\\.bin"); fs = (Stream)res.Values.First(); } else { fs = File.OpenRead("default_body_lz.bin"); } using (fs) using (var ms = new MemoryStream()) { try { LZ11.Decompress(fs, fs.Length, ms); ms.Position = 0; result.Theme = Theme.Read(ms); result.Loaded = true; } catch { return(result); } } return(result); }); if (start) { task.Start(); } return(task); }
public static int DoCompress(string infile, MemoryStream output, FormatCompress format, out FormatCompress actualFormat) { CompressionFormat fmt = null; switch (format) { case FormatCompress.LZ10: fmt = new LZ10(); break; case FormatCompress.LZ11: fmt = new LZ11(); break; case FormatCompress.LZOVL: fmt = new LZOvl(); break; case FormatCompress.RLE: fmt = new RLE(); break; case FormatCompress.HUFF4: Huffman.CompressBlockSize = Huffman.BlockSize.FOURBIT; fmt = new Huffman(); break; case FormatCompress.HUFF8: Huffman.CompressBlockSize = Huffman.BlockSize.EIGHTBIT; fmt = new Huffman(); break; case FormatCompress.HUFF: return(CompressHuff(infile, output, out actualFormat)); case FormatCompress.GBA: return(CompressGBA(infile, output, out actualFormat)); case FormatCompress.NDS: return(CompressNDS(infile, output, out actualFormat)); default: actualFormat = FormatCompress.Invalid; return(-1); } actualFormat = format; using (FileStream inStream = File.OpenRead(infile)) { try { return(fmt.Compress(inStream, inStream.Length, output)); } catch (Exception s) { // any exception generated by compression is a fatal exception Console.WriteLine(s.Message); return(-1); } } }
private static int DoCompress(string infile, MemoryStream output, Formats format, out Formats actualFormat) { CompressionFormat fmt = null; switch (format) { case Formats.LZ10: fmt = new LZ10(); break; case Formats.LZ11: fmt = new LZ11(); break; case Formats.LZOVL: fmt = new LZOvl(); break; case Formats.RLE: fmt = new RLE(); break; case Formats.HUFF4: fmt = new Huffman4(); break; case Formats.HUFF8: fmt = new Huffman8(); break; case Formats.HUFF: return(CompressHuff(infile, output, out actualFormat)); case Formats.GBA: return(CompressGBA(infile, output, out actualFormat)); case Formats.NDS: return(CompressNDS(infile, output, out actualFormat)); default: throw new Exception("Unhandled compression format " + format); } actualFormat = format; using (FileStream inStream = File.OpenRead(infile)) { try { return(fmt.Compress(inStream, inStream.Length, output)); } catch (Exception s) { // any exception generated by compression is a fatal exception Console.WriteLine(s.Message); return(-1); } } }
public void Write(Stream input) { using (var bw = new BinaryWriterX(input, true)) { if (State == ArchiveFileState.Archived || Entry.uncompSize == 0) { base.FileData.CopyTo(bw.BaseStream); } else { var compData = new MemoryStream(LZ11.Compress(base.FileData)); Entry.uncompSize = (uint)base.FileData.Length; Entry.compSize = (uint)compData.Length; compData.CopyTo(bw.BaseStream); } } }
public F3XT(Stream input) { using (BinaryReaderX br = new BinaryReaderX(input)) { BinaryReaderX stream; //check for lz11 compression if (br.ReadByte() == 0x11) { br.BaseStream.Position = 0; byte[] decomp = LZ11.Decompress(br.BaseStream); br.BaseStream.Position = 0; uint size = br.ReadUInt32() >> 8; if (decomp.Length == size) { lz11_compressed = true; } stream = new BinaryReaderX(new MemoryStream(decomp, 0, decomp.Length)); } else { br.BaseStream.Position = 0; stream = new BinaryReaderX(input); } //map the file header = stream.ReadStruct <Header>(); settings = new ImageSettings { Width = header.width, Height = header.height, Format = ImageSettings.ConvertFormat(header.format), PadToPowerOf2 = false }; stream.BaseStream.Position = header.dataStart; //throw new Exception((stream.BaseStream.Length - stream.BaseStream.Position).ToString()); Image = Common.Load(stream.ReadBytes((int)(stream.BaseStream.Length - stream.BaseStream.Position)), settings); } }
private static long Decompress(MemoryStream inputStream, MemoryStream output, FormatCompress format) { CompressionFormat realFormat = null; switch (format) { case FormatCompress.HUFF: realFormat = new Huffman(); break; case FormatCompress.LZ10: realFormat = new LZ10(); break; case FormatCompress.LZ11: realFormat = new LZ11(); break; case FormatCompress.LZOVL: realFormat = new LZOvl(); break; case FormatCompress.RLE: realFormat = new RLE(); break; default: return(-1); } if (!realFormat.Supports(inputStream, inputStream.Length)) { return(-1); } try { return(realFormat.Decompress(inputStream, inputStream.Length, output)); } catch (TooMuchInputException e) { Console.WriteLine(e.Message); return(output.Length); } catch (Exception e) { Console.WriteLine(String.Format(Main.Get_Traduction("S1D"), format.ToString(), e.Message)); return(-1); } }
private static string CriarNovoBinario(List <string> listaDeArquivos, string dirNovoArquivo) { var entradas = new List <EntradaAAIBin>(); LZ11 LZ11 = new LZ11(); uint tamanhoTabela = (uint)listaDeArquivos.Count * 8; uint endereco = tamanhoTabela; using (BinaryWriter bw = new BinaryWriter(File.Open("temp.bin", FileMode.Append))) { File.Create("temp.bin").Close(); foreach (var caminho in listaDeArquivos) { bool comprimido = caminho.Contains("LZ11_") ? true : false; FileInfo infoArquivo = new FileInfo(comprimido ? caminho.Replace("LZ11_", "") : caminho); if (infoArquivo == null) { return($"Arquivo não encontrado:\r\n {caminho}"); } byte[] arquivo = File.ReadAllBytes(infoArquivo.Directory.FullName); uint tamanhoArquivo = (uint)arquivo.Length; if (comprimido) { arquivo = ComprimirComLZ11(LZ11, arquivo); } bw.Write(arquivo); entradas.Add(new EntradaAAIBin(endereco, tamanhoArquivo, comprimido)); endereco += (uint)arquivo.Length; } } byte[] tabelaNova = new byte[tamanhoTabela]; tabelaNova = CrieUmaNovaTabela(tabelaNova, entradas); CrieNovoBinarioComTabela(dirNovoArquivo, tabelaNova); return(""); }
public static FormatCompress Get_Format(FileStream input, bool arm9) { CompressionFormat fmt = null; foreach (FormatCompress f in Enum.GetValues(typeof(FormatCompress))) { switch (f) { //case FormatCompress.LZOVL: fmt = new LZOvl(); break; case FormatCompress.LZ10: fmt = new LZ10(); break; case FormatCompress.LZ11: fmt = new LZ11(); break; case FormatCompress.RLE: fmt = new RLE(); break; case FormatCompress.HUFF: fmt = new Huffman(); break; } if (fmt == null) { continue; } long fLength = input.Length; if (arm9) { fLength -= 0xC; } if (fmt.Supports(input, fLength)) { return(f); } } return(FormatCompress.Invalid); }
private void saveAsButton_Click(object sender, EventArgs e) { SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.Filter = "Binary files (*.bin)|*.bin"; if (saveDialog.ShowDialog() != DialogResult.OK) { return; } // For testing purpose if (Header == null) { File.WriteAllBytes(saveDialog.FileName, SaveData); return; } LZ11 lz11 = new LZ11(); byte[] compressed; using (MemoryStream inStream = new MemoryStream(SaveData)) { using (MemoryStream outStream = new MemoryStream()) { lz11.Compress(inStream, SaveData.Length, outStream); compressed = outStream.ToArray(); } } using (FileStream fs = new FileStream(saveDialog.FileName, FileMode.Create, FileAccess.Write)) { using (BinaryWriter bw = new BinaryWriter(fs)) { bw.Write(Header); bw.Write(compressed); } } }
private void DownloadSharedFont() { string sharedFontDestination = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), nameof(Citra), "sysdata", "shared_font.bin"); if (System.IO.File.Exists(sharedFontDestination)) { return; } GClass80 gclass80 = new GClass80((WebProxy)null, true, true); FrmWait frmWait = new FrmWait("Downloading the Shared Font from NUS...", true); gclass80.Event_5 += (EventHandler <GClass81>)((_param1, _param2) => { Path.Combine(Path.GetTempPath(), "game.cia"); GClass28.gclass32_0.method_5(Path.GetTempPath(), "game"); string outputDir = Path.GetTempPath(); GClass27.smethod_11(outputDir); ProcessStartInfo processStartInfo1 = new ProcessStartInfo() { FileName = Path.Combine(outputDir, "PART1"), WorkingDirectory = outputDir }; Process process1 = new Process(); process1.StartInfo = processStartInfo1; process1.EnableRaisingEvents = true; process1.Exited += (EventHandler)((sender1, e1) => { try { ProcessStartInfo processStartInfo2 = new ProcessStartInfo() { FileName = Path.Combine(outputDir, "3dstool.exe"), WorkingDirectory = outputDir, Arguments = "-xvtf cfa contents.0000.00000000 --romfs DecryptedRomFS.bin --romfs-auto-key" }; Process process2 = new Process(); process2.StartInfo = processStartInfo2; process2.Exited += (EventHandler)((sender2, e2) => { ProcessStartInfo processStartInfo2 = new ProcessStartInfo() { FileName = "3dstool.exe", WorkingDirectory = outputDir, Arguments = "-xvtf romfs DecryptedRomFS.bin --romfs-dir ExtractedRomFS" }; Process process2 = new Process(); process2.StartInfo = processStartInfo2; process2.EnableRaisingEvents = true; process2.Exited += (EventHandler)((sender3, e3) => { byte[] buffer = new LZ11().Decompress(System.IO.File.ReadAllBytes(Path.Combine(outputDir, "ExtractedRomFS", "cbf_std.bcfnt.lz"))); Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), nameof(Citra), "sysdata")); using (FileStream fileStream = System.IO.File.Create(sharedFontDestination)) { using (MemoryStream memoryStream = new MemoryStream(buffer)) { memoryStream.Seek(928L, SeekOrigin.Begin); fileStream.Write(Class3.byte_1, 0, Class3.byte_1.Length); for (int index = 0; index < 3095744; ++index) { int num = memoryStream.ReadByte(); fileStream.WriteByte((byte)num); } fileStream.Write(Class3.byte_0, 0, Class3.byte_0.Length); } } frmWait.method_0(); }); process2.Start(); }); process2.EnableRaisingEvents = true; process2.Start(); } catch { } }); process1.Start(); }); gclass80.Event_6 += (EventHandler <GEventArgs0>)((sender, e) => frmWait.method_2(e.GameProgress)); gclass80.method_1(((IEnumerable <GClass30>) new GClass30[1] { (GClass30)GClass28.gclass32_0 }).ToList <GClass30>(), 100, 10000); int num1 = (int)frmWait.ShowDialog(); }
public static void Compress(object sender, EventArgs e) { var tsi = sender as ToolStripMenuItem; if (!PrepareFiles("Open a decompressed " + tsi.Tag.ToString() + "file...", "Save your compressed file...", ".comp", out FileStream openFile, out FileStream saveFile)) { return; } try { using (openFile) using (var outFs = new BinaryWriterX(saveFile)) switch (tsi.Tag) { /*case Compression.L5LZSS: * outFs.Write(Level5.Compress(openFile, 1)); * break; * case Compression.L5Huff4: * outFs.Write(Level5.Compress(openFile, 2)); * break; * case Compression.L5Huff8: * outFs.Write(Level5.Compress(openFile, 3)); * break; * case Compression.L5RLE: * outFs.Write(Level5.Compress(openFile, 4)); * break;*/ case Compression.GZip: outFs.Write(GZip.Compress(openFile)); break; case Compression.Huff4: outFs.Write(Huffman.Compress(openFile, 4)); break; case Compression.Huff8: outFs.Write(Huffman.Compress(openFile, 8)); break; case Compression.LZ10: outFs.Write(LZ10.Compress(openFile)); break; case Compression.LZ11: outFs.Write(LZ11.Compress(openFile)); break; /*case Compression.LZSS: * outFs.Write(LZSS.Compress(openFile)); * break;*/ case Compression.RevLZ77: outFs.Write(RevLZ77.Compress(openFile)); break; case Compression.RLE: outFs.Write(RLE.Compress(openFile)); break; case Compression.ZLib: outFs.Write(ZLib.Compress(openFile)); break; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } MessageBox.Show($"Successfully compressed {Path.GetFileName(openFile.Name)}.", tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); }
private Task <ZipThemeResults> ZipTheme_Execute(string bgmPath, bool start) { var viewModel = ViewModel; var busySavingTheme = MainResources.Busy_SavingTheme; var bmpPreview = RenderPreview(PreviewKind.Both); var task = new Task <ZipThemeResults>(() => { BusyText = busySavingTheme; var results = new ZipThemeResults { Saved = false, BGMPath = bgmPath, Data = null }; var themeName = viewModel.Info.ShortDescription; var rnd = new Random(); if (string.IsNullOrEmpty(themeName)) { var bytes = new byte[4]; rnd.NextBytes(bytes); themeName = "3DSThemeEditor-" + BitConverter.ToString(bytes); } themeName = Regex.Replace(themeName, "[^ -~]+", "_"); using (var ms = new MemoryStream()) { using (var arch = new ZipArchive(ms, ZipArchiveMode.Create)) { var bodyLz = arch.CreateEntry($"{themeName}/body_LZ.bin"); using (var tgt = bodyLz.Open()) using (var srcDec = new MemoryStream()) using (var srcCom = new MemoryStream()) { viewModel.Save(srcDec); srcDec.Position = 0; LZ11.Compress(srcDec, srcDec.Length, srcCom, true); srcCom.Position = 0; srcCom.CopyTo(tgt); } var info = arch.CreateEntry($"{themeName}/info.smdh"); using (var tgt = info.Open()) viewModel.Info.Save(tgt); var preview = arch.CreateEntry($"{themeName}/preview.png"); using (var tgt = preview.Open()) using (var src = new MemoryStream()) { var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmpPreview)); encoder.Save(src); src.Position = 0; src.CopyTo(tgt); } if (viewModel.Flags.BackgroundMusic) { var bgm = arch.CreateEntry($"{themeName}/BGM.BCSTM"); using (var src = File.OpenRead(results.BGMPath)) using (var tgt = bgm.Open()) src.CopyTo(tgt); } } results.Data = ms.ToArray(); results.Saved = true; #if DEBUG File.WriteAllBytes(@"C:\Temp\theme.zip", results.Data); #endif } return(results); }, TaskCreationOptions.LongRunning); if (start) { task.Start(); } return(task); }
private void openButton_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Binary files (*.bin)|*.bin|All files (*.*)|*.*"; if (openFileDialog.ShowDialog() != DialogResult.OK) { return; } long length = (new FileInfo(openFileDialog.FileName)).Length; if (length > saveLength) { MessageBox.Show("Invalid save file", "Error"); return; } // Open a decompressed save file, for testing purpose if (length == saveLength) { Header = null; SaveData = File.ReadAllBytes(openFileDialog.FileName); enableButtons(); return; } Header = new byte[headerLength]; byte[] compressed = new byte[length - headerLength]; using (FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)) { using (BinaryReader br = new BinaryReader(fs)) { br.BaseStream.Seek(headerLength, SeekOrigin.Begin); byte isLZ11 = br.ReadByte(); if (isLZ11 != 0x11) { MessageBox.Show("Invalid save file", "Error"); return; } br.BaseStream.Seek(0x00, SeekOrigin.Begin); br.Read(Header, 0x0, headerLength); br.Read(compressed, 0x0, (int)length - headerLength); } } // Decompress byte[] decompressed = null; LZ11 lz11 = new LZ11(); using (MemoryStream inStream = new MemoryStream(compressed)) { using (MemoryStream outStream = new MemoryStream()) { lz11.Decompress(inStream, compressed.Length, outStream); if (outStream != null) { decompressed = outStream.ToArray(); } else { MessageBox.Show("Invalid save file", "Error"); return; } } } // Check file validity and save loading if (decompressed.Length != saveLength) { MessageBox.Show("Invalid save file", "Error"); return; } SaveData = decompressed; enableButtons(); }
private Task <LoadThemeResults> LoadTheme_Execute(string path, bool start) { var busyPickingFile = MainResources.Busy_PickingFile; var busyLoadingTheme = MainResources.Busy_LoadingTheme; var task = new Task <LoadThemeResults>(() => { var result = new LoadThemeResults { Loaded = false, Path = path, }; if (string.IsNullOrEmpty(result.Path)) { BusyText = busyPickingFile; var opfl = new OpenFileDialog { Filter = "3DS Theme File|*.bin", Multiselect = false }; var dlg = opfl.ShowDialog(); if (dlg.HasValue && dlg.Value) { result.Path = opfl.FileName; } } if (string.IsNullOrEmpty(result.Path)) { return(result); } var themeDir = Path.GetDirectoryName(result.Path) ?? "."; BusyText = busyLoadingTheme; using (var fs = File.OpenRead(result.Path)) using (var ms = new MemoryStream()) { try { LZ11.Decompress(fs, fs.Length, ms); ms.Position = 0; result.Theme = Theme.Read(ms); #if DEBUG ms.Seek(0, SeekOrigin.Begin); File.WriteAllBytes("dump.bin", ms.ToArray()); #endif result.Loaded = true; } catch { // Ignore } } var smdhPath = Path.Combine(themeDir, "info.smdh"); if (result.Loaded && File.Exists(smdhPath)) { using (var fs = File.OpenRead(smdhPath)) { try { result.Info = SMDH.Read(fs); } catch { result.Info = null; } } } return(result); }); if (start) { task.Start(); } return(task); }
private static int CompressBest(string infile, MemoryStream output, out Formats actualFormat, params Formats[] formats) { // only read the input data once from the file. byte[] inputData; using (FileStream inStream = File.OpenRead(infile)) { inputData = new byte[inStream.Length]; inStream.Read(inputData, 0, inputData.Length); } MemoryStream bestOutput = null; int minCompSize = int.MaxValue; actualFormat = Formats.GBA; foreach (Formats format in formats) { #region compress the file in each format, and save the best one MemoryStream currentOutput = new MemoryStream(); CompressionFormat realFormat = null; switch (format) { case Formats.HUFF4: realFormat = new Huffman4(); break; case Formats.HUFF8: realFormat = new Huffman8(); break; case Formats.LZ10: realFormat = new LZ10(); break; case Formats.LZ11: realFormat = new LZ11(); break; case Formats.LZOVL: realFormat = new LZOvl(); break; case Formats.RLE: realFormat = new RLE(); break; default: Console.WriteLine("Unsupported single format: " + format); continue; } int currentOutSize; try { using (MemoryStream inStream = new MemoryStream(inputData)) { currentOutSize = realFormat.Compress(inStream, inStream.Length, currentOutput); } } catch (InputTooLargeException i) { Console.WriteLine(i.Message); actualFormat = format; return(-1); } catch (Exception) { continue; } if (currentOutSize < minCompSize) { bestOutput = currentOutput; minCompSize = currentOutSize; actualFormat = format; } #endregion } if (bestOutput == null) { Console.WriteLine("The file could not be compressed in any format."); return(-1); } bestOutput.WriteTo(output); return(minCompSize); }
// Token: 0x06000918 RID: 2328 RVA: 0x0005E348 File Offset: 0x0005C548 internal void c1ba471e7e0d48232a37646b1ed0039a7(object cd020a9448b1902e4410f2e9f4a256959, EventArgs c920e624ed3f68a7518020d25dc06be2d) { string path = Path.Combine(this.c54a7d6cb5c0c0420ac46560b5c93cf03, ceffa389ee86c0ee26f31dd3efacbdd38.c5dd75c94760bfb37727a44ddc794d278(98637), ceffa389ee86c0ee26f31dd3efacbdd38.c5dd75c94760bfb37727a44ddc794d278(98666)); byte[] buffer = new LZ11().Decompress(File.ReadAllBytes(path)); Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath((Environment.SpecialFolder)cebbdeae8341a51f374f3ef4c993a069a.c6bc1c09ec46e3a402ca1f5ca208c573e(30460)), ceffa389ee86c0ee26f31dd3efacbdd38.c5dd75c94760bfb37727a44ddc794d278(39426), ceffa389ee86c0ee26f31dd3efacbdd38.c5dd75c94760bfb37727a44ddc794d278(39638))); FileStream fileStream = File.Create(this.c8c38354cddaefb45b9cfcd9ed2838c37.c19b7656a3c5703a5e5e43cfd6140a025); try { MemoryStream memoryStream = new MemoryStream(buffer); try { memoryStream.Seek((long)cebbdeae8341a51f374f3ef4c993a069a.c6bc1c09ec46e3a402ca1f5ca208c573e(30464), (SeekOrigin)cebbdeae8341a51f374f3ef4c993a069a.c6bc1c09ec46e3a402ca1f5ca208c573e(30468)); fileStream.Write(c45ab3113ea09209e9f00b6f5f2beea47.c277aeed55c5fcb223240f078ce05c2e0, cebbdeae8341a51f374f3ef4c993a069a.c6bc1c09ec46e3a402ca1f5ca208c573e(30472), (int)c7e3f6af67bf34eefc1a4cca086e17069.cfeb04651f165d0166e7c10ab1acf8b28(c45ab3113ea09209e9f00b6f5f2beea47.c277aeed55c5fcb223240f078ce05c2e0)); for (int i = cebbdeae8341a51f374f3ef4c993a069a.c6bc1c09ec46e3a402ca1f5ca208c573e(30476); i < cebbdeae8341a51f374f3ef4c993a069a.c6bc1c09ec46e3a402ca1f5ca208c573e(30484); i += cebbdeae8341a51f374f3ef4c993a069a.c6bc1c09ec46e3a402ca1f5ca208c573e(30480)) { int num = memoryStream.ReadByte(); fileStream.WriteByte((byte)num); } for (;;) { switch (6) { case 0: continue; } break; } if (!true) { RuntimeMethodHandle runtimeMethodHandle = methodof(c8b671fda31f88dc7c7875175126002f3.cdfe092796793745a1ce1d64d233593bd.c1ba471e7e0d48232a37646b1ed0039a7(object, EventArgs)).MethodHandle; } fileStream.Write(c45ab3113ea09209e9f00b6f5f2beea47.c53d022dd05da782de06207ef49e1606b, cebbdeae8341a51f374f3ef4c993a069a.c6bc1c09ec46e3a402ca1f5ca208c573e(30488), (int)c7e3f6af67bf34eefc1a4cca086e17069.cfeb04651f165d0166e7c10ab1acf8b28(c45ab3113ea09209e9f00b6f5f2beea47.c53d022dd05da782de06207ef49e1606b)); } finally { if (memoryStream != null) { for (;;) { switch (1) { case 0: continue; } break; } ((IDisposable)memoryStream).Dispose(); } } } finally { if (fileStream != null) { for (;;) { switch (2) { case 0: continue; } break; } ((IDisposable)fileStream).Dispose(); } } this.c8c38354cddaefb45b9cfcd9ed2838c37.cfe60371517f0baf9152d2203b196e54f.cc5957c0b466d791dc387469fda39883c(); }
private static int CompressBest(string infile, MemoryStream output, out FormatCompress actualFormat, params FormatCompress[] formats) { // only read the input data once from the file. byte[] inputData; using (FileStream inStream = File.OpenRead(infile)) { inputData = new byte[inStream.Length]; inStream.Read(inputData, 0, inputData.Length); } MemoryStream bestOutput = null; int minCompSize = int.MaxValue; actualFormat = FormatCompress.GBA; foreach (FormatCompress format in formats) { #region compress the file in each format, and save the best one MemoryStream currentOutput = new MemoryStream(); CompressionFormat realFormat = null; switch (format) { case FormatCompress.HUFF4: Huffman.CompressBlockSize = Huffman.BlockSize.FOURBIT; realFormat = new Huffman(); break; case FormatCompress.HUFF8: Huffman.CompressBlockSize = Huffman.BlockSize.EIGHTBIT; realFormat = new Huffman(); break; case FormatCompress.LZ10: realFormat = new LZ10(); break; case FormatCompress.LZ11: realFormat = new LZ11(); break; case FormatCompress.LZOVL: realFormat = new LZOvl(); break; case FormatCompress.RLE: realFormat = new RLE(); break; } int currentOutSize; try { using (MemoryStream inStream = new MemoryStream(inputData)) { currentOutSize = realFormat.Compress(inStream, inStream.Length, currentOutput); } } catch (InputTooLargeException i) { Console.WriteLine(i.Message); actualFormat = format; return(-1); } catch (Exception) { continue; } if (currentOutSize < minCompSize) { bestOutput = currentOutput; minCompSize = currentOutSize; actualFormat = format; } #endregion } if (bestOutput == null) { Console.WriteLine(Main.Get_Traduction("S1A")); return(-1); } bestOutput.WriteTo(output); return(minCompSize); }