Beispiel #1
0
        public static byte[] Decompress(Stream input)
        {
            using (var br = new BinaryReaderY(input, true))
            {
                var methodSize = br.ReadUInt32();
                var method     = (Method)(methodSize & 0xff);
                int size       = (int)((methodSize & 0xffffff00) >> 8);

                using (var brB = new BinaryReaderY(new MemoryStream(br.ReadBytes((int)input.Length - 4))))
                    switch (method)
                    {
                    case Method.LZ10:
                        return(LZ10.Decompress(brB.BaseStream, size));

                    /*case Method.LZ11:
                     *  return LZ11.Decompress(brB.BaseStream, size);
                     * case Method.Huff4:
                     *  return Huffman.Decompress(brB.BaseStream, 4, size, ByteOrder.BigEndian);
                     * case Method.Huff8:
                     *  return Huffman.Decompress(brB.BaseStream, 8, size);
                     * case Method.RLE:
                     *  return RLE.Decompress(brB.BaseStream, size);
                     * case Method.LZ60:
                     *  throw new Exception("LZ60 isn't implemented yet");*/
                    //return LZ60.Decompress(brB.BaseStream);
                    default:
                        br.BaseStream.Position -= 4;
                        return(br.ReadAllBytes());   // BaseStream.StructToBytes();
                    }
            }
        }
Beispiel #2
0
        static void Main(string[] args2)
        {
            var options = ParseArgs(args2);

            if (options.mode == "extract")
            {
                var sections = new List <byte[]>();
                using (var br = new BinaryReaderY(File.OpenRead(options.path)))
                {
                    try
                    {
                        var entryCount = br.ReadInt32();
                        var entryList  = br.ReadMultiple <DlgEntry>(entryCount);
                        foreach (var e in entryList)
                        {
                            br.BaseStream.Position = e.offset;
                            sections.Add(Nintendo.Decompress(new MemoryStream(br.ReadBytes(e.size))));
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("This file seems a bit corrupted or isn't a dlg. Either way it won't work.");
                        Console.WriteLine(ex.Message + " - " + ex.InnerException);
                        return;
                    }
                }
                var writeDir = Path.Combine(Path.GetDirectoryName(options.path), Path.GetFileNameWithoutExtension(options.path));
                if (!Directory.Exists(writeDir))
                {
                    Directory.CreateDirectory(writeDir);
                }

                var txtCount = 0;
                foreach (var s in sections)
                {
                    using (var br = new BinaryReaderY(new MemoryStream(s)))
                    {
                        if (options.isBinary)
                        {
                            File.WriteAllBytes(Path.Combine(writeDir, $"{txtCount++:000}.bin"), s);
                        }
                        else
                        {
                            var enc = new AAEncoding(options.lang, options.game);
                            File.WriteAllText(Path.Combine(writeDir, $"{txtCount++:000}.txt"), enc.GetSectionText(s));
                        }
                    }
                }
            }
            else if (options.mode == "create")
            {
                var enc     = new AAEncoding(options.lang, options.game);
                var files   = Directory.GetFiles(options.path).Where(f => Path.GetExtension(f) == ((options.isBinary) ? ".bin" : ".txt")).OrderBy(name => name).ToList();
                var entries = new List <DlgEntry>();

                var ms = new MemoryStream();
                using (var bw = new BinaryWriterY(ms, true))
                {
                    bw.BaseStream.Position = files.Count * 8 + 4;

                    foreach (var file in files)
                    {
                        byte[] compBytes = null;
                        if (options.isBinary)
                        {
                            compBytes = Nintendo.Compress(new MemoryStream(File.ReadAllBytes(file)), Nintendo.Method.LZ10);
                        }
                        else
                        {
                            var sectionText = File.ReadAllText(file).Replace("\r\n", "");
                            var bytes       = enc.GetBytes(sectionText);
                            compBytes = Nintendo.Compress(new MemoryStream(bytes), Nintendo.Method.LZ10);
                        }

                        entries.Add(new DlgEntry {
                            offset = (int)bw.BaseStream.Position, size = compBytes.Length
                        });

                        bw.Write(compBytes);
                        bw.BaseStream.Position = (bw.BaseStream.Position + 3) & ~3;
                    }

                    bw.BaseStream.Position = 0;
                    bw.Write(entries.Count);
                    foreach (var entry in entries)
                    {
                        bw.WriteStruct(entry);
                    }
                }

                File.WriteAllBytes("new.dlg", ms.ToArray());
            }
        }