Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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;
            }
        }
Ejemplo n.º 4
0
        private static byte[] ComprimirComLZ11(LZ11 LZ11, byte[] arquivo)
        {
            MemoryStream output = new MemoryStream();

            LZ11.Compress(new MemoryStream(arquivo), arquivo.Length, output);
            return(output.ToArray());
        }
Ejemplo n.º 5
0
        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;
            }
        }
Ejemplo n.º 6
0
        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());
        }
Ejemplo n.º 7
0
 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);
         }
     }
 }
Ejemplo n.º 8
0
        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);
                }
            }
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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);
        }