Example #1
0
        public StarterEditor6()
        {
            specieslist[0] = "---";
            Array.Resize(ref specieslist, Main.Config.MaxSpeciesID);

            if (!File.Exists(CROPath))
            {
                Util.Error("CRO does not exist! Closing.", CROPath);
                Close();
            }
            if (!File.Exists(FieldPath))
            {
                Util.Error("CRO does not exist! Closing.", FieldPath);
                Close();
            }
            InitializeComponent();

            // 2 sets of Starters for X/Y
            // 4 sets of Starters for OR/AS
            Choices = new[]
            {
                new[] { CB_G1_0, CB_G1_1, CB_G1_2 },
                new[] { CB_G2_0, CB_G2_1, CB_G2_2 },
                new[] { CB_G3_0, CB_G3_1, CB_G3_2 },
                new[] { CB_G4_0, CB_G4_1, CB_G4_2 },
            };
            Previews = new[]
            {
                new[] { PB_G1_0, PB_G1_1, PB_G1_2 },
                new[] { PB_G2_0, PB_G2_1, PB_G2_2 },
                new[] { PB_G3_0, PB_G3_1, PB_G3_2 },
                new[] { PB_G4_0, PB_G4_1, PB_G4_2 },
            };
            Labels = new[] { L_Set1, L_Set2, L_Set3, L_Set4 };

            Width = Main.Config.ORAS ? Width : Width / 2 + 2;
            loadData();
        }
Example #2
0
        internal static void openARC(string path, ProgressBar pBar1, bool recursing = false)
        {
            string newFolder = "";

            try
            {
                // Pre-check file length to see if it is at least valid.
                FileInfo fi = new FileInfo(path);
                if (fi.Length > 1.6 * (1 << 30))
                {
                    Util.Error("File is too big!"); return;
                }                                                                          // 1.6 GB
                string folderPath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));

                byte[] first4 = new byte[4];
                try
                {
                    using (BinaryReader bw = new BinaryReader(new FileStream(path, FileMode.Open)))
                        first4 = bw.ReadBytes(4);
                }
                catch (Exception e)
                {
                    Util.Error("Cannot open file!", e.ToString());
                }

                // Determine if it is a DARC or a Mini
                // Check if Mini first
                string fx = fi.Length > 10 * (1 << 20) ? null : CTR.mini.getIsMini(path); // no mini is above 10MB
                if (fx != null)                                                           // Is Mini Packed File
                {
                    newFolder = folderPath + "_" + fx;
                    // Fetch Mini File Contents
                    CTR.mini.unpackMini(path, fx, newFolder, false);
                    // Recurse throught the extracted contents if they extract successfully
                    if (Directory.Exists(newFolder))
                    {
                        foreach (string file in Directory.GetFiles(newFolder))
                        {
                            openARC(file, pBar1, true);
                        }
                        batchRenameExtension(newFolder);
                    }
                }
                else if (first4.SequenceEqual(BitConverter.GetBytes(0x47415243))) // GARC
                {
                    if (threads > 0)
                    {
                        Util.Alert("Please wait for all operations to finish first."); return;
                    }
                    bool SkipDecompression = ModifierKeys == Keys.Control;
                    new Thread(() =>
                    {
                        threads++;
                        bool r = CTR.GARC.garcUnpack(path, folderPath + "_g", SkipDecompression, pBar1);
                        threads--;
                        if (r)
                        {
                            batchRenameExtension(newFolder);
                        }
                        else
                        {
                            Util.Alert("Unpacking failed."); return;
                        }
                        System.Media.SystemSounds.Asterisk.Play();
                    }).Start();
                    return;
                }
                else if (ARC.analyze(path).valid) // DARC
                {
                    var data = File.ReadAllBytes(path);
                    int pos  = 0;
                    while (BitConverter.ToUInt32(data, pos) != 0x63726164)
                    {
                        pos += 4;
                        if (pos >= data.Length)
                        {
                            return;
                        }
                    }
                    var darcData = data.Skip(pos).ToArray();
                    newFolder = folderPath + "_d";
                    bool r = CTR.DARC.darc2files(darcData, newFolder);
                    if (!r)
                    {
                        Util.Alert("Unpacking failed."); return;
                    }
                }
                else if (!recursing)
                {
                    Util.Alert("File is not a darc or a mini packed file:" + Environment.NewLine + path); return;
                }
            }
            catch (Exception e)
            {
                if (!recursing)
                {
                    Util.Error("File error:" + Environment.NewLine + path, e.ToString());
                }
                threads = 0;
            }
            System.Media.SystemSounds.Asterisk.Play();
        }
Example #3
0
        private void saveARC(string path)
        {
            if (!Directory.Exists(path))
            {
                Util.Error("Input path is not a Folder", path); return;
            }
            string folderName = Path.GetFileName(path);

            if (folderName == null)
            {
                return;
            }
            string parentName = Directory.GetParent(path).FullName;
            int    type       = CB_Repack.SelectedIndex;

            switch (type)
            {
            case 0:     // AutoDetect
            {
                if (!folderName.Contains("_"))
                {
                    Util.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)
                {
                    Util.Alert("Please wait for all operations to finish first."); return;
                }
                DialogResult dr = Util.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 ? CTR.GARC.VER_6 : CTR.GARC.VER_4;
                new Thread(() =>
                    {
                        string outfolder = Directory.GetParent(path).FullName;
                        bool r           = CTR.GARC.garcPackMS(path, Path.Combine(outfolder, folderName + ".garc"), version, pBar1);
                        if (!r)
                        {
                            Util.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 = CTR.DARC.files2darc(path, false, oldFile);
                if (!r)
                {
                    Util.Alert("Packing failed.");
                }
                break;
            }

            case 3:     // Mini Pack
            {
                // Get Folder Name
                string fileName = Path.GetFileName(path);
                if (fileName.Length < 3)
                {
                    Util.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 = CTR.mini.packMini2(path, fileExt, Path.Combine(parentName, fileNum + "." + fileExt));
                if (!r)
                {
                    Util.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 !=
                            Util.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 = CTR.mini.adjustMiniHeader(newData, oldPtr);
                        File.WriteAllBytes(Path.Combine(parentName, fileNum + "." + fileExt), update);
                    }
                }

                break;
            }

            default: Util.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();
        }
Example #4
0
        internal static byte[] getBytesForFile(string[] lines)
        {
            using (MemoryStream textFile = new MemoryStream())
                using (BinaryWriter bw = new BinaryWriter(textFile))
                    using (MemoryStream data = new MemoryStream())
                        using (BinaryWriter bz = new BinaryWriter(data))
                        {
                            ushort baseKey = 0x7C89;

                            // Write up header template
                            bw.Write((ushort)1);            // Always 1 ?
                            bw.Write((ushort)lines.Length); // Line Count
                            bw.Write((uint)0);              // (Temporary) Data Length - fixed at the end.
                            bw.Write((uint)0);              // Key, constant 0.
                            bw.Write((uint)0x10);           // Pointer to line data.

                            // Begin data
                            bw.Write((uint)0);  // (Temporary) Data Length - fixed at the end.

                            for (int i = 0; i < lines.Length; i++)
                            {
                                try
                                {
                                    ushort key = baseKey;
                                    uint   pos = (uint)data.Position;
                                    if (lines[i] == null)
                                    {
                                        lines[i] = $"[~ {i}]";
                                    }
                                    // Get crypted line data.
                                    {
                                        {
                                            // Write each character to the data stream, with handling for special characters.
                                            for (int j = 0; j < lines[i].Length; j++)
                                            {
                                                ushort val = lines[i][j];

                                                // Handle special text characters
                                                // Private Use Area characters
                                                if (val == 0x202F)
                                                {
                                                    val = 0xE07F;           // nbsp
                                                }
                                                else if (val == 0x2026)
                                                {
                                                    val = 0xE08D;           // …
                                                }
                                                else if (val == 0x2642)
                                                {
                                                    val = 0xE08E;           // ♂
                                                }
                                                else if (val == 0x2640)
                                                {
                                                    val = 0xE08F;           // ♀
                                                }
                                                // Variables
                                                if (val == '[' || val == '\\') // Variable
                                                {
                                                    encryptVar(bz, lines[i], ref j, ref key);
                                                }

                                                // Text
                                                else
                                                {
                                                    bz.Write(encryptU16(val, ref key));
                                                }
                                            }
                                            bz.Write(encryptU16(0, ref key)); // Add the null terminator, after encrypting it.
                                        }

                                        // Write the lineOffset and charCount to the header.
                                        bw.Write((uint)(pos + 0x4 + lines.Length * 8));
                                        bw.Write((uint)(data.Position - pos) / 2);
                                        if (data.Position % 4 > 0)
                                        {
                                            bz.Write((ushort)0);
                                        }

                                        // Increment the line initial key for the next line.
                                        baseKey += 0x2983;
                                    }
                                }
                                catch (Exception e) { Util.Error(e.ToString()); return(null); }
                            }

                            // Copy the data stream to the textFile stream.
                            data.Position = 0; data.CopyTo(textFile);

                            // Fix the temp values
                            textFile.Position = 0x4; bw.Write((uint)textFile.Length - 0x10);
                            textFile.Position = 0x10; bw.Write((uint)textFile.Length - 0x10);

                            return(textFile.ToArray());
                        }
        }