Esempio n. 1
0
        private void SaveARC(string path)
        {
            if (!Directory.Exists(path))
            {
                WinFormsUtil.Error("Input path is not a Folder", path); return;
            }
            string folderName = Path.GetFileName(path);
            string parentName = Directory.GetParent(path).FullName;
            int    type       = CB_Repack.SelectedIndex;

            switch (type)
            {
            case 0:     // AutoDetect
            {
                if (!folderName.Contains("_"))
                {
                    WinFormsUtil.Alert("Unable to autodetect pack type."); return;
                }

                if (folderName.Contains("_g"))
                {
                    goto case 1;
                }
                if (folderName.Contains("_d"))
                {
                    goto case 2;
                }
                // else
                goto case 3;
            }

            case 1:     // GARC Pack
            {
                if (threads > 0)
                {
                    WinFormsUtil.Alert("Please wait for all operations to finish first."); return;
                }
                DialogResult dr = WinFormsUtil.Prompt(MessageBoxButtons.YesNoCancel, "Format Selection:",
                                                      "Yes: Sun/Moon (Version 6)\nNo: XY/ORAS (Version 4)");
                if (dr == DialogResult.Cancel)
                {
                    return;
                }

                var version = dr == DialogResult.Yes ? GARC.VER_6 : GARC.VER_4;
                int padding = (int)NUD_Padding.Value;
                if (version == GARC.VER_4)
                {
                    padding = 4;
                }

                string outfolder = Directory.GetParent(path).FullName;
                new Thread(() =>
                    {
                        bool r = GarcUtil.PackGARC(path, Path.Combine(outfolder, folderName + ".garc"), version, padding, pBar1);
                        if (!r)
                        {
                            WinFormsUtil.Alert("Packing failed."); return;
                        }
                        // Delete path after repacking
                        if (CHK_Delete.Checked && Directory.Exists(path))
                        {
                            Directory.Delete(path, true);
                        }

                        System.Media.SystemSounds.Asterisk.Play();
                    }).Start();
                return;
            }

            case 2:     // DARC Pack (from existing if exists)
            {
                string oldFile = path.Replace("_d", "");
                if (File.Exists(Path.Combine(parentName, oldFile)))
                {
                    oldFile = Path.Combine(parentName, oldFile);
                }
                else if (File.Exists(Path.Combine(parentName, oldFile + ".bin")))
                {
                    oldFile = Path.Combine(parentName, oldFile + ".bin");
                }
                else if (File.Exists(Path.Combine(parentName, oldFile + ".darc")))
                {
                    oldFile = Path.Combine(parentName, oldFile + ".darc");
                }
                else
                {
                    oldFile = null;
                }

                bool r = Core.CTR.DARC.Files2darc(path, false, oldFile);
                if (!r)
                {
                    WinFormsUtil.Alert("Packing failed.");
                }
                break;
            }

            case 3:     // Mini Pack
            {
                // Get Folder Name
                string fileName = Path.GetFileName(path);
                if (fileName.Length < 3)
                {
                    WinFormsUtil.Error("Mini Folder name not valid:", path); return;
                }

                int    index   = fileName.LastIndexOf('_');
                string fileNum = fileName.Substring(0, index);
                string fileExt = fileName.Substring(index + 1);

                // Find old file for reference...
                string file;
                if (File.Exists(Path.Combine(parentName, fileNum + ".bin")))
                {
                    file = Path.Combine(parentName, fileNum + ".bin");
                }
                else if (File.Exists(Path.Combine(parentName, fileNum + "." + fileExt)))
                {
                    file = Path.Combine(parentName, fileNum + "." + fileExt);
                }
                else
                {
                    file = null;
                }

                byte[] oldData = file != null?File.ReadAllBytes(file) : null;

                bool r = Mini.PackMini2(path, fileExt, Path.Combine(parentName, fileNum + "." + fileExt));
                if (!r)
                {
                    WinFormsUtil.Alert("Packing failed.");
                    break;
                }

                // Check to see if the header size is different...
                if (oldData == null)     // No data to compare to.
                {
                    break;
                }

                byte[] newData = File.ReadAllBytes(Path.Combine(parentName, fileNum + "." + fileExt));
                if (newData[2] == oldData[2])
                {
                    int newPtr = BitConverter.ToInt32(newData, 4);
                    int oldPtr = BitConverter.ToInt32(oldData, 4);
                    if (newPtr != oldPtr)     // Header size is different. Prompt repointing.
                    {
                        if (DialogResult.Yes != WinFormsUtil.Prompt(MessageBoxButtons.YesNo, "Header size of existing file is nonstandard.", "Adjust newly packed file to have the same header size as old file? Data pointers will be updated accordingly."))
                        {
                            break;
                        }

                        // Fix pointers
                        byte[] update = Mini.AdjustMiniHeader(newData, oldPtr);
                        File.WriteAllBytes(Path.Combine(parentName, fileNum + "." + fileExt), update);
                    }
                }

                break;
            }

            default:
                WinFormsUtil.Alert("Repacking not implemented." + Environment.NewLine + path);
                return;
            }
            // Delete path after repacking
            if (CHK_Delete.Checked && Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }
            System.Media.SystemSounds.Asterisk.Play();
        }