Exemple #1
0
        /// <summary>
        /// Saves a wz file to the disk, AKA repacking.
        /// </summary>
        /// <param name="path">Path to the output wz file</param>
        public void SaveToDisk(string path, WzMapleVersion savingToPreferredWzVer = WzMapleVersion.UNKNOWN)
        {
            if (savingToPreferredWzVer == WzMapleVersion.UNKNOWN)
            {
                WzIv = WzTool.GetIvByMapleVersion(maplepLocalVersion); // get from local WzFile
            }
            else
            {
                WzIv = WzTool.GetIvByMapleVersion(savingToPreferredWzVer); // custom selected
            }
            bool bIsWzIvSimilar = true;                                    // check if its saving to the same IV.

            for (int i = 0; i < WzIv.Length; i++)
            {
                if (WzIv[i] != wzDir.WzIv[i])
                {
                    bIsWzIvSimilar = false;
                }
            }
            wzDir.WzIv = WzIv;

            CreateVersionHash();
            wzDir.SetVersionHash(versionHash);

            string tempFile = Path.GetFileNameWithoutExtension(path) + ".TEMP";

            File.Create(tempFile).Close();
            using (FileStream fs = new FileStream(tempFile, FileMode.Append, FileAccess.Write))
            {
                wzDir.GenerateDataFile(bIsWzIvSimilar ? null : WzIv, fs);
            }

            WzTool.StringCache.Clear();
            uint totalLen = wzDir.GetImgOffsets(wzDir.GetOffsets(Header.FStart + 2));

            using (WzBinaryWriter wzWriter = new WzBinaryWriter(File.Create(path), WzIv))
            {
                wzWriter.Hash = versionHash;
                Header.FSize  = totalLen - Header.FStart;
                for (int i = 0; i < 4; i++)
                {
                    wzWriter.Write((byte)Header.Ident[i]);
                }
                wzWriter.Write((long)Header.FSize);
                wzWriter.Write(Header.FStart);
                wzWriter.WriteNullTerminatedString(Header.Copyright);

                long extraHeaderLength = Header.FStart - wzWriter.BaseStream.Position;
                if (extraHeaderLength > 0)
                {
                    wzWriter.Write(new byte[(int)extraHeaderLength]);
                }
                wzWriter.Write(version);
                wzWriter.Header = Header;
                wzDir.SaveDirectory(wzWriter);
                wzWriter.StringCache.Clear();

                using (FileStream fs = File.OpenRead(tempFile))
                {
                    wzDir.SaveImages(wzWriter, fs);
                }
                File.Delete(tempFile);

                wzWriter.StringCache.Clear();
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        /// <summary>
        /// Saves a wz file to the disk, AKA repacking.
        /// </summary>
        /// <param name="path">Path to the output wz file</param>
        /// <param name="override_saveAs64BitWZ"></param>
        /// <param name="savingToPreferredWzVer"></param>
        public void SaveToDisk(string path, bool?override_saveAs64BitWZ, WzMapleVersion savingToPreferredWzVer = WzMapleVersion.UNKNOWN)
        {
            // WZ IV
            if (savingToPreferredWzVer == WzMapleVersion.UNKNOWN)
            {
                WzIv = WzTool.GetIvByMapleVersion(maplepLocalVersion); // get from local WzFile
            }
            else
            {
                WzIv = WzTool.GetIvByMapleVersion(savingToPreferredWzVer); // custom selected
            }
            bool bIsWzIvSimilar = WzIv.SequenceEqual(wzDir.WzIv);          // check if its saving to the same IV.

            wzDir.WzIv = WzIv;

            // MapleStory UserKey
            bool bIsWzUserKeyDefault = MapleCryptoConstants.IsDefaultMapleStoryUserKey(); // check if its saving to the same UserKey.
            // Save WZ as 64-bit wz format
            bool bSaveAs64BitWZ = override_saveAs64BitWZ != null ? (bool)override_saveAs64BitWZ : b64BitClient;

            CreateWZVersionHash();
            wzDir.SetVersionHash(versionHash);

            string tempFile = Path.GetFileNameWithoutExtension(path) + ".TEMP";

            File.Create(tempFile).Close();
            using (FileStream fs = new FileStream(tempFile, FileMode.Append, FileAccess.Write))
            {
                wzDir.GenerateDataFile(bIsWzIvSimilar ? null : WzIv, bIsWzUserKeyDefault, fs);
            }

            WzTool.StringCache.Clear();

            using (WzBinaryWriter wzWriter = new WzBinaryWriter(File.Create(path), WzIv))
            {
                wzWriter.Hash = versionHash;

                uint totalLen = wzDir.GetImgOffsets(wzDir.GetOffsets(Header.FStart + (bSaveAs64BitWZ && !this.b64BitClient_withVerHeader ? 0 : 2u)));
                Header.FSize = totalLen - Header.FStart;
                for (int i = 0; i < 4; i++)
                {
                    wzWriter.Write((byte)Header.Ident[i]);
                }
                wzWriter.Write((long)Header.FSize);
                wzWriter.Write(Header.FStart);
                wzWriter.WriteNullTerminatedString(Header.Copyright);

                long extraHeaderLength = Header.FStart - wzWriter.BaseStream.Position;
                if (extraHeaderLength > 0)
                {
                    wzWriter.Write(new byte[(int)extraHeaderLength]);
                }
                if (!bSaveAs64BitWZ || this.b64BitClient_withVerHeader)
                {
                    wzWriter.Write(wzVersionHeader);
                }

                wzWriter.Header = Header;
                wzDir.SaveDirectory(wzWriter);
                wzWriter.StringCache.Clear();

                using (FileStream fs = File.OpenRead(tempFile))
                {
                    wzDir.SaveImages(wzWriter, fs);
                }
                File.Delete(tempFile);

                wzWriter.StringCache.Clear();
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }