Example #1
0
        private static void Run(string inWz, string outPath, WZVariant wzVar, bool initialEnc) {
            Console.WriteLine("Input .wz: {0}{1}Output .nx: {2}", Path.GetFullPath(inWz),
                Environment.NewLine, Path.GetFullPath(outPath));

            Stopwatch swOperation = new Stopwatch();
            Stopwatch fullTimer = new Stopwatch();

            Action<string> reportDone = str => {
                Console.WriteLine("done. E{0} T{1}", swOperation.Elapsed,
                    fullTimer.Elapsed);
                swOperation.Restart();
                Console.Write(str);
            };

            fullTimer.Start();
            swOperation.Start();
            Console.Write("Parsing input WZ... ".PadRight(31));

            WZReadSelection rFlags = WZReadSelection.EagerParseImage |
                                     WZReadSelection.EagerParseStrings;
            if (!dumpImg)
                rFlags |= WZReadSelection.NeverParseCanvas;

            using (WZFile wzf = new WZFile(inWz, wzVar, initialEnc, rFlags))
            using (
                FileStream outFs = new FileStream(outPath, FileMode.Create, FileAccess.ReadWrite,
                    FileShare.None))
            using (BinaryWriter bw = new BinaryWriter(outFs)) {
                DumpState state = new DumpState();

                reportDone("Writing header... ".PadRight(31));
                bw.Write(PKG4);
                bw.Write(new byte[(4 + 8)*4]);

                reportDone("Writing nodes... ".PadRight(31));
                outFs.EnsureMultiple(4);
                ulong nodeOffset = (ulong) bw.BaseStream.Position;
                List<WZObject> nodeLevel = new List<WZObject> {wzf.MainDirectory};
                while (nodeLevel.Count > 0)
                    WriteNodeLevel(ref nodeLevel, state, bw);

                ulong stringOffset;
                uint stringCount = (uint) state.Strings.Count;
                {
                    reportDone("Writing string data...".PadRight(31));
                    Dictionary<uint, string> strings = state.Strings.ToDictionary(kvp => kvp.Value,
                        kvp => kvp.Key);
                    ulong[] offsets = new ulong[stringCount];
                    for (uint idx = 0; idx < stringCount; ++idx) {
                        outFs.EnsureMultiple(2);
                        offsets[idx] = (ulong) bw.BaseStream.Position;
                        WriteString(strings[idx], bw);
                    }

                    outFs.EnsureMultiple(8);
                    stringOffset = (ulong) bw.BaseStream.Position;
                    for (uint idx = 0; idx < stringCount; ++idx)
                        bw.Write(offsets[idx]);
                }

                ulong bitmapOffset = 0UL;
                uint bitmapCount = 0U;
                if (dumpImg) {
                    reportDone("Writing canvas data...".PadRight(31));
                    bitmapCount = (uint) state.Canvases.Count;
                    ulong[] offsets = new ulong[bitmapCount];
                    long cId = 0;
                    foreach (WZCanvasProperty cNode in state.Canvases) {
                        outFs.EnsureMultiple(8);
                        offsets[cId++] = (ulong) bw.BaseStream.Position;
                        WriteBitmap(cNode, bw);
                    }
                    outFs.EnsureMultiple(8);
                    bitmapOffset = (ulong) bw.BaseStream.Position;
                    for (uint idx = 0; idx < bitmapCount; ++idx)
                        bw.Write(offsets[idx]);
                }

                ulong soundOffset = 0UL;
                uint soundCount = 0U;
                if (dumpSnd) {
                    reportDone("Writing MP3 data... ".PadRight(31));
                    soundCount = (uint) state.MP3s.Count;
                    ulong[] offsets = new ulong[soundCount];
                    long cId = 0;
                    foreach (WZAudioProperty mNode in state.MP3s) {
                        outFs.EnsureMultiple(8);
                        offsets[cId++] = (ulong) bw.BaseStream.Position;
                        WriteMP3(mNode, bw);
                    }
                    outFs.EnsureMultiple(8);
                    soundOffset = (ulong) bw.BaseStream.Position;
                    for (uint idx = 0; idx < soundCount; ++idx)
                        bw.Write(offsets[idx]);
                }

                reportDone("Writing linked node data... ".PadRight(31));
                byte[] uolReplace = new byte[16];
                foreach (KeyValuePair<WZUOLProperty, Action<BinaryWriter, byte[]>> pair in state.UOLs) {
                    WZObject result = pair.Key.FinalTarget;
                    if (result == null)
                        continue;
                    bw.BaseStream.Position = (long) (nodeOffset + state.GetNodeID(result)*20 + 4);
                    bw.BaseStream.Read(uolReplace, 0, 16);
                    pair.Value(bw, uolReplace);
                }

                reportDone("Finalising... ".PadRight(31));

                bw.Seek(4, SeekOrigin.Begin);
                bw.Write((uint) state.Nodes.Count);
                bw.Write(nodeOffset);
                bw.Write(stringCount);
                bw.Write(stringOffset);
                bw.Write(bitmapCount);
                bw.Write(bitmapOffset);
                bw.Write(soundCount);
                bw.Write(soundOffset);

                reportDone("Completed!");
            }
        }
        public void SerializeFile(WzFile file, string path)
        {
            String filename = file.Name.Replace(".wz", ".nx");

            using (FileStream fs = new FileStream(Path.Combine(Path.GetDirectoryName(path), filename), FileMode.Create, FileAccess.ReadWrite,
                                                  FileShare.None))
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    DumpState state = new DumpState();
                    bw.Write(PKG4);
                    bw.Write(new byte[(4 + 8) * 4]);
                    fs.EnsureMultiple(4);
                    ulong           nodeOffset = (ulong)bw.BaseStream.Position;
                    List <WzObject> nodeLevel  = new List <WzObject> {
                        file.WzDirectory
                    };
                    while (nodeLevel.Count > 0)
                    {
                        WriteNodeLevel(ref nodeLevel, state, bw);
                    }

                    ulong stringOffset;
                    uint  stringCount = (uint)state.Strings.Count;
                    {
                        Dictionary <uint, string> strings = state.Strings.ToDictionary(kvp => kvp.Value,
                                                                                       kvp => kvp.Key);
                        ulong[] offsets = new ulong[stringCount];
                        for (uint idx = 0; idx < stringCount; ++idx)
                        {
                            fs.EnsureMultiple(2);
                            offsets[idx] = (ulong)bw.BaseStream.Position;
                            WriteString(strings[idx], bw);
                        }

                        fs.EnsureMultiple(8);
                        stringOffset = (ulong)bw.BaseStream.Position;
                        for (uint idx = 0; idx < stringCount; ++idx)
                        {
                            bw.Write(offsets[idx]);
                        }
                    }

                    ulong bitmapOffset = 0UL;
                    uint  bitmapCount  = 0U;
                    bool  flag         = true;
                    if (flag)
                    {
                        bitmapCount = (uint)state.Canvases.Count;
                        ulong[] offsets = new ulong[bitmapCount];
                        long    cId     = 0;
                        foreach (WzCanvasProperty cNode in state.Canvases)
                        {
                            fs.EnsureMultiple(8);
                            offsets[cId++] = (ulong)bw.BaseStream.Position;
                            WriteBitmap(cNode, bw);
                        }
                        fs.EnsureMultiple(8);
                        bitmapOffset = (ulong)bw.BaseStream.Position;
                        for (uint idx3 = 0U; idx3 < bitmapCount; idx3 += 1U)
                        {
                            bw.Write(offsets[(int)idx3]);
                        }
                    }
                    ulong soundOffset = 0UL;
                    uint  soundCount  = 0U;
                    bool  flag2       = true;
                    if (flag2)
                    {
                        soundCount = (uint)state.MP3s.Count;
                        ulong[] offsets = new ulong[soundCount];
                        long    cId     = 0L;
                        foreach (WzBinaryProperty mNode in state.MP3s)
                        {
                            fs.EnsureMultiple(8);
                            offsets[cId++] = (ulong)bw.BaseStream.Position;
                            WriteMP3(mNode, bw);
                        }
                        fs.EnsureMultiple(8);
                        soundOffset = (ulong)bw.BaseStream.Position;
                        for (uint idx4 = 0U; idx4 < soundCount; idx4 += 1U)
                        {
                            bw.Write(offsets[(int)idx4]);
                        }
                    }
                    byte[] uolReplace = new byte[16];
                    foreach (KeyValuePair <WzUOLProperty, Action <BinaryWriter, byte[]> > pair in state.UOLs)
                    {
                        WzObject result = pair.Key.LinkValue;
                        bool     flag3  = result == null;
                        if (!flag3)
                        {
                            bw.BaseStream.Position = (long)(nodeOffset + (ulong)(state.GetNodeID(result) * 20U) + 4UL);
                            bw.BaseStream.Read(uolReplace, 0, 16);
                            pair.Value(bw, uolReplace);
                        }
                    }
                    bw.Seek(4, SeekOrigin.Begin);
                    bw.Write((uint)state.Nodes.Count);
                    bw.Write(nodeOffset);
                    bw.Write(stringCount);
                    bw.Write(stringOffset);
                    bw.Write(bitmapCount);
                    bw.Write(bitmapOffset);
                    bw.Write(soundCount);
                    bw.Write(soundOffset);
                }
        }
Example #3
0
        private static void Run(string inWz, string outPath, WZVariant wzVar, bool initialEnc)
        {
            Console.WriteLine("Input .wz: {0}{1}Output .nx: {2}", Path.GetFullPath(inWz),
                              Environment.NewLine, Path.GetFullPath(outPath));

            Stopwatch swOperation = new Stopwatch();
            Stopwatch fullTimer   = new Stopwatch();

            Action <string> reportDone = str => {
                Console.WriteLine("done. E{0} T{1}", swOperation.Elapsed,
                                  fullTimer.Elapsed);
                swOperation.Restart();
                Console.Write(str);
            };

            fullTimer.Start();
            swOperation.Start();
            Console.Write("Parsing input WZ... ".PadRight(31));

            WZReadSelection rFlags = WZReadSelection.EagerParseImage |
                                     WZReadSelection.EagerParseStrings;

            if (!dumpImg)
            {
                rFlags |= WZReadSelection.NeverParseCanvas;
            }

            using (WZFile wzf = new WZFile(inWz, wzVar, initialEnc, rFlags))
                using (
                    FileStream outFs = new FileStream(outPath, FileMode.Create, FileAccess.ReadWrite,
                                                      FileShare.None))
                    using (BinaryWriter bw = new BinaryWriter(outFs)) {
                        DumpState state = new DumpState();

                        reportDone("Writing header... ".PadRight(31));
                        bw.Write(PKG4);
                        bw.Write(new byte[(4 + 8) * 4]);

                        reportDone("Writing nodes... ".PadRight(31));
                        outFs.EnsureMultiple(4);
                        ulong           nodeOffset = (ulong)bw.BaseStream.Position;
                        List <WZObject> nodeLevel  = new List <WZObject> {
                            wzf.MainDirectory
                        };
                        while (nodeLevel.Count > 0)
                        {
                            WriteNodeLevel(ref nodeLevel, state, bw);
                        }

                        ulong stringOffset;
                        uint  stringCount = (uint)state.Strings.Count;
                        {
                            reportDone("Writing string data...".PadRight(31));
                            Dictionary <uint, string> strings = state.Strings.ToDictionary(kvp => kvp.Value,
                                                                                           kvp => kvp.Key);
                            ulong[] offsets = new ulong[stringCount];
                            for (uint idx = 0; idx < stringCount; ++idx)
                            {
                                outFs.EnsureMultiple(2);
                                offsets[idx] = (ulong)bw.BaseStream.Position;
                                WriteString(strings[idx], bw);
                            }

                            outFs.EnsureMultiple(8);
                            stringOffset = (ulong)bw.BaseStream.Position;
                            for (uint idx = 0; idx < stringCount; ++idx)
                            {
                                bw.Write(offsets[idx]);
                            }
                        }

                        ulong bitmapOffset = 0UL;
                        uint  bitmapCount  = 0U;
                        if (dumpImg)
                        {
                            reportDone("Writing canvas data...".PadRight(31));
                            bitmapCount = (uint)state.Canvases.Count;
                            ulong[] offsets = new ulong[bitmapCount];
                            long    cId     = 0;
                            foreach (WZCanvasProperty cNode in state.Canvases)
                            {
                                outFs.EnsureMultiple(8);
                                offsets[cId++] = (ulong)bw.BaseStream.Position;
                                WriteBitmap(cNode, bw);
                            }
                            outFs.EnsureMultiple(8);
                            bitmapOffset = (ulong)bw.BaseStream.Position;
                            for (uint idx = 0; idx < bitmapCount; ++idx)
                            {
                                bw.Write(offsets[idx]);
                            }
                        }

                        ulong soundOffset = 0UL;
                        uint  soundCount  = 0U;
                        if (dumpSnd)
                        {
                            reportDone("Writing MP3 data... ".PadRight(31));
                            soundCount = (uint)state.MP3s.Count;
                            ulong[] offsets = new ulong[soundCount];
                            long    cId     = 0;
                            foreach (WZAudioProperty mNode in state.MP3s)
                            {
                                outFs.EnsureMultiple(8);
                                offsets[cId++] = (ulong)bw.BaseStream.Position;
                                WriteMP3(mNode, bw);
                            }
                            outFs.EnsureMultiple(8);
                            soundOffset = (ulong)bw.BaseStream.Position;
                            for (uint idx = 0; idx < soundCount; ++idx)
                            {
                                bw.Write(offsets[idx]);
                            }
                        }

                        reportDone("Writing linked node data... ".PadRight(31));
                        byte[] uolReplace = new byte[16];
                        foreach (KeyValuePair <WZUOLProperty, Action <BinaryWriter, byte[]> > pair in state.UOLs)
                        {
                            WZObject result = pair.Key.FinalTarget;
                            if (result == null)
                            {
                                continue;
                            }
                            bw.BaseStream.Position = (long)(nodeOffset + state.GetNodeID(result) * 20 + 4);
                            bw.BaseStream.Read(uolReplace, 0, 16);
                            pair.Value(bw, uolReplace);
                        }

                        reportDone("Finalising... ".PadRight(31));

                        bw.Seek(4, SeekOrigin.Begin);
                        bw.Write((uint)state.Nodes.Count);
                        bw.Write(nodeOffset);
                        bw.Write(stringCount);
                        bw.Write(stringOffset);
                        bw.Write(bitmapCount);
                        bw.Write(bitmapOffset);
                        bw.Write(soundCount);
                        bw.Write(soundOffset);

                        reportDone("Completed!");
                    }
        }