示例#1
0
        public string GetSectionText(byte[] input)
        {
            string result = "";

            using (var br = new BinaryReaderY(new MemoryStream(input)))
            {
                var count = br.ReadInt32();
                var vals  = br.ReadMultiple <int>(count);

                int _lastValid   = 0;
                var sections     = new List <List <(string, int, bool)> >();
                int pointerCount = 0;
                int condCount    = 0;
                foreach (var v in vals)
                {
                    if ((v >= br.BaseStream.Length || v < _lastValid) && (v & 0xFFFF) > 0)
                    {
                        //Pointer
                        InjectPointerLabel(sections[(v >> 16) & 0xFFFF], v & 0xFFFF, pointerCount);
                        sections.Add(new List <(string, int, bool)> {
                            ("<Pointer: @pointer" + pointerCount++ + ">", 0, false)
                        });
示例#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());
            }
        }