static private void ConsoleDecode(string[] args) //Console Decode
 {
     if (args.Length == 2 && File.Exists(args[1]))
     {
         if (Yaz0.Decode(args[1], Yaz0.DecodeOutputFileRename(args[1])))
         {
             Console.WriteLine("Decode Successful");
         }
         else
         {
             Console.WriteLine("Decode error: " + Yaz0.lerror);
         }
     }
     else if (args.Length == 3 && File.Exists(args[1]))
     {
         if (Yaz0.Decode(args[1], args[2]))
         {
             Console.WriteLine("Decode Successful");
         }
         else
         {
             Console.WriteLine("Decode error: " + Yaz0.lerror);
         }
     }
     else
     {
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine("Error: Incorrect use of Decode command.");
         Console.WriteLine("/d <Input File> [Output File]");
         Console.ForegroundColor = ConsoleColor.White;
     }
 }
Exemple #2
0
        private void btnYaz0Decode_Click(object sender, EventArgs e)
        {
            OpenFileDialog oFile = new OpenFileDialog();

            if (tbxFolderRoot.Text != "")
            {
                oFile.InitialDirectory = tbxFolderRoot.Text;
            }
            if (oFile.ShowDialog() == DialogResult.Cancel)
            {
                goto toss;
            }
            string outFile = oFile.FileName;

            {
                loadingBar.Visible = true;
                if (!Yaz0.Decode(oFile.FileName, Yaz0.DecodeOutputFileRename(oFile.FileName)))
                {
                    MessageBox.Show("Decode error:" + "\n\n" + Yaz0.lerror);
                    goto toss;
                }
            }

            MessageBox.Show("Decode complete!" + "\n\n" + outFile);

toss:
            oFile.Dispose();
            GC.Collect();
            loadingBar.Visible = false;
        }
Exemple #3
0
        private void btnYaz0Decode_Click(object sender, EventArgs e)
        {
            loadingBar.Visible = true;
            OpenFileDialog oFile = new OpenFileDialog();

            if (tbxFolderRoot.Text != "")
            {
                oFile.InitialDirectory = tbxFolderRoot.Text;
            }
            if (oFile.ShowDialog() == DialogResult.Cancel)
            {
                goto toss;
            }
            string outFile = oFile.FileName;

            {
                if (!Yaz0.Decode(oFile.FileName, Yaz0.DecodeOutputFileRename(oFile.FileName)))
                {
                    MessageBox.Show("Decode error:" + "\n\n" + Yaz0.lerror);
                    goto toss;
                }
            }

            //XML
            if (cbxWriteYaz0Xml.Checked)
            {
                String oFolderName = Path.GetFileNameWithoutExtension(oFile.FileName);
                String oFolderPath = Path.GetDirectoryName(oFile.FileName) + "\\" + oFolderName;
                if (File.Exists(oFolderPath + "_Yaz0Debug.xml"))
                {
                    if (MessageBox.Show(oFolderName + ".xml already exists!" + "\n\n" + "Proceed anyway?", "Overwrite?", MessageBoxButtons.YesNo) == DialogResult.No)
                    {
                        goto toss;
                    }
                }
                if (!DebugWriter.WriteYaz0Xml(oFile.FileName, (oFolderPath + "_Yaz0Debug.xml")))
                {
                    MessageBox.Show("XML file failed to write by unknown reasons");
                }
            }

            MessageBox.Show("Decode complete!" + "\n\n" + outFile);

toss:
            oFile.Dispose();
            GC.Collect();
            loadingBar.Visible = false;
        }
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog inFile = new OpenFileDialog();

            inFile.Filter = "All Files|*.*";
            if (inFile.ShowDialog() == DialogResult.Cancel)
            {
                goto toss;
            }
            tbxFile.Text = inFile.FileName;

            btnSave.Enabled = true;
            dgvTable.Rows.Clear();
            dgvTable.Refresh();
verify:
            byte[] fileCheck = System.IO.File.ReadAllBytes(tbxFile.Text);
            if (fileCheck[0] == 'Y' && fileCheck[1] == 'a' && fileCheck[2] == 'z' && fileCheck[3] == '0') // if Yaz0 encoded, ask if they want to decode it
            {
                DialogResult diagResult = MessageBox.Show("This file is encoded!" + "\n\n" + "Do you want to decode?\nIt will create a seperate file automatically", "Yaz0 Encoded file...", MessageBoxButtons.YesNo);
                if (diagResult == DialogResult.No)
                {
                    tbxFile.Text    = "";
                    btnSave.Enabled = false;
                    goto toss;
                }
                string outFile = Yaz0.DecodeOutputFileRename(inFile.FileName);
                if (!Yaz0.Decode(inFile.FileName, outFile))
                {
                    MessageBox.Show("Decode error:" + "\n\n" + Yaz0.lerror);
                    tbxFile.Text    = "";
                    btnSave.Enabled = false;
                    goto toss;
                }
                tbxFile.Text = outFile;
                goto verify;
            }
            if (("" + ((char)fileCheck[0]) + ((char)fileCheck[1]) + ((char)fileCheck[2]) + ((char)fileCheck[3])) != "SARC")
            {
                MessageBox.Show("Not a SARC archive! Missing SARC header at 0x00" + "\n" + "( Your file header is: " + ((char)fileCheck[0]) + ((char)fileCheck[1]) + ((char)fileCheck[2]) + ((char)fileCheck[3]) + " )");
                tbxFile.Text    = "";
                btnSave.Enabled = false;
                goto toss;
            }

            int nodeCount = SARC.GetFileNodeCount(tbxFile.Text);

            string[] nodeTypes    = SARC.GetFileNodeType(tbxFile.Text);
            uint[]   nodeSizes    = SARC.GetFileNodeSizes(tbxFile.Text);
            string[] nodePaths    = SARC.GetFileNodePaths(tbxFile.Text);
            uint[]   nodePaddings = SARC.GetFileNodePaddings(tbxFile.Text);

            for (int i = 0; i < nodeCount; i++)
            {
                dgvTable.Rows.Add();
                dgvTable.Rows[i].Cells[0].Value = i + 1;
                dgvTable.Rows[i].Cells[1].Value = nodePaddings[i];
                dgvTable.Rows[i].Cells[2].Value = nodeTypes[i];
                dgvTable.Rows[i].Cells[3].Value = nodeSizes[i];
                dgvTable.Rows[i].Cells[4].Value = nodeSizes[i].ToString("X");
                dgvTable.Rows[i].Cells[5].Value = nodePaths[i];
            }

            fileSize           = fileCheck.Length;
            tbxFileSize.Text   = fileSize.ToString();
            dataOffset         = SARC.GetFileDataOffset(tbxFile.Text);
            tbxDataOffset.Text = "0x" + dataOffset.ToString("X");

toss:
            inFile.Dispose();
            GC.Collect();
        }
Exemple #5
0
        public static bool Extract(byte[] inFile, string outDir, bool autoDecode, bool nodeDecode, string inFileName)
        {
            //SARC header 0x00 - 0x13
            if (("" + ((char)inFile[0]) + ((char)inFile[1]) + ((char)inFile[2]) + ((char)inFile[3])) != "SARC")
            {
                if (inFile[0] == 'Y' && inFile[1] == 'a' && inFile[2] == 'z' && inFile[3] == '0')
                {
                    if (autoDecode)
                    {
                        string outFile = Yaz0.DecodeOutputFileRename(inFileName);
                        Yaz0.Decode(inFileName, outFile);
                        return(Extract(outFile, outDir, autoDecode)); //recursively run the code again
                    }
                    else
                    {
                        lerror = "Yaz0 file encoded, please decode it first";
                        return(false);
                    }
                }
                else
                {
                    lerror = "Not a SARC archive! Missing SARC header at 0x00" + "\n" + "( Your file header is: " + ((char)inFile[0]) + ((char)inFile[1]) + ((char)inFile[2]) + ((char)inFile[3]) + " )";
                    return(false);
                }
            }
            int    pos = 4;                                     //0x04
            ushort hdr = Makeu16(inFile[pos], inFile[pos + 1]); //SARC Header length

            pos += 2;                                           //0x06
            ushort bom = Makeu16(inFile[pos], inFile[pos + 1]); //Byte Order Mark

            if (bom != 65279)                                   //Check 0x06 for Byte Order Mark (if not 0xFEFF)
            {
                if (bom == 65518)
                {
                    lerror = "Unable to support Little Endian! (Byte Order Mark 0x06)";
                }
                else
                {
                    lerror = "Unknown SARC header (Byte Order Mark 0x06)";
                }
                return(false);
            }
            pos += 2; //0x08
            uint fileSize = Makeu32(inFile[pos], inFile[pos + 1], inFile[pos + 2], inFile[pos + 3]);

            pos += 4;                                                                                  //0x0C
            uint dataOffset = Makeu32(inFile[pos], inFile[pos + 1], inFile[pos + 2], inFile[pos + 3]); //Data offset start position

            pos += 4;                                                                                  //0x10
            uint unknown = Makeu32(inFile[pos], inFile[pos + 1], inFile[pos + 2], inFile[pos + 3]);    //unknown, always 0x01?

            pos += 4;                                                                                  //0x14

            //SFAT header 0x14 - 0x1F
            if (inFile[pos] != 'S' || inFile[pos + 1] != 'F' || inFile[pos + 2] != 'A' || inFile[pos + 3] != 'T')
            {
                lerror = "Unknown file table! (Missing SFAT header at 0x14)";
                return(false);
            }
            pos += 4;                                                                             //0x18
            ushort hdr2 = Makeu16(inFile[pos], inFile[pos + 1]);                                  //SFAT Header length

            pos += 2;                                                                             //0x1A
            ushort nodeCount = Makeu16(inFile[pos], inFile[pos + 1]);                             //Node Cluster count

            pos += 2;                                                                             //0x1C
            uint hashr = Makeu32(inFile[pos], inFile[pos + 1], inFile[pos + 2], inFile[pos + 3]); //Hash multiplier, always 0x65

            pos += 4;                                                                             //0x20

            SarcNode[] nodes   = new SarcNode[nodeCount];
            SarcNode   tmpnode = new SarcNode();

            for (int i = 0; i < nodeCount; i++) //Node cluster
            {
                tmpnode.hash    = Makeu32(inFile[pos], inFile[pos + 1], inFile[pos + 2], inFile[pos + 3]);
                pos            += 4;                                                                       //0x?4
                tmpnode.unknown = inFile[pos];                                                             //unknown, always 0x01? (not used in this case)
                pos            += 1;                                                                       //0x?5
                tmpnode.offset  = Makeu32(0, inFile[pos], inFile[pos + 1], inFile[pos + 2]);               //Node SFNT filename offset divided by 4 (not used)
                pos            += 3;                                                                       //0x?8
                tmpnode.start   = Makeu32(inFile[pos], inFile[pos + 1], inFile[pos + 2], inFile[pos + 3]); //Start Data offset position
                pos            += 4;                                                                       //0x?C
                tmpnode.end     = Makeu32(inFile[pos], inFile[pos + 1], inFile[pos + 2], inFile[pos + 3]); //End Data offset position
                pos            += 4;                                                                       //0x?0
                nodes[i]        = tmpnode;                                                                 //Store node data into array
            }

            if (inFile[pos] != 'S' || inFile[pos + 1] != 'F' || inFile[pos + 2] != 'N' || inFile[pos + 3] != 'T')
            {
                string posOffset = "0x" + pos.ToString("X");
                lerror = "Unknown file name table! (Missing SFNT header at " + posOffset + ")";
                return(false);
            }
            pos += 4;                                            //0x?4
            ushort hdr3 = Makeu16(inFile[pos], inFile[pos + 1]); //SFNT Header length, always 0x08

            pos += 2;                                            //0x?6
            ushort unk2 = Makeu16(inFile[pos], inFile[pos + 1]); //unknown, always 0x00?

            pos += 2;                                            //0x?8

            string[] fileNames = new string[nodeCount];
            string   tempName;

            for (int i = 0; i < nodeCount; i++) //Get file names for each node
            {
                tempName = "";                  //reset for each file
                while (inFile[pos] != 0)
                {
                    tempName = tempName + ((char)inFile[pos]).ToString(); //Build temp string for each letter
                    pos     += 1;
                }
                while (inFile[pos] == 0) //ignore every 0 byte, because why bother calculating the SFNT header offset anyway?
                {
                    pos += 1;
                }
                fileNames[i] = tempName; //Take built string and store it in the array
            }

            if (!System.IO.Directory.Exists(outDir))
            {
                System.IO.Directory.CreateDirectory(outDir);                                      //folder creation
            }
            System.IO.StreamWriter stream;

            for (int i = 0; i < nodeCount; i++) //Write files based from node information
            {
                MakeDirExist(System.IO.Path.GetDirectoryName(outDir + "/" + fileNames[i]));
                stream = new System.IO.StreamWriter(outDir + "/" + fileNames[i]);
                stream.BaseStream.Write(inFile, (int)(nodes[i].start + dataOffset), (int)(nodes[i].end - nodes[i].start)); //Write
                stream.Close();
                stream.Dispose();
                if (nodeDecode)
                {
                    Yaz0.Decode(outDir + "/" + fileNames[i], Yaz0.DecodeOutputFileRename(outDir + "/" + fileNames[i]));
                }
            }
            GC.Collect();
            return(true);
        } //--------------------------------------------------------------------------------------------------------------------------------------------
Exemple #6
0
        private void btnOriBrowse_Click(object sender, EventArgs e)
        {
            if (rbnOriFile.Checked)
            {
                OpenFileDialog oriFile = new OpenFileDialog();
                oriFile.Filter = "All Files|*.*";
                if (BotwUnpacker.Properties.Settings.Default.RootFolder != "")
                {
                    oriFile.InitialDirectory = BotwUnpacker.Properties.Settings.Default.RootFolder;
                }
                if (oriFile.ShowDialog() == DialogResult.Cancel)
                {
                    goto toss;
                }
                tbxOriFile.Text = oriFile.FileName;

                dgvOriTable.Rows.Clear();
                dgvOriTable.Refresh();
verify:
                byte[] fileCheck = System.IO.File.ReadAllBytes(tbxOriFile.Text);
                if (fileCheck[0] == 'Y' && fileCheck[1] == 'a' && fileCheck[2] == 'z' && fileCheck[3] == '0') // if Yaz0 encoded, ask if they want to decode it
                {
                    DialogResult diagResult = MessageBox.Show("This file is encoded!" + "\n\n" + "Do you want to decode?\nIt will create a seperate file automatically", "Yaz0 Encoded file...", MessageBoxButtons.YesNo);
                    if (diagResult == DialogResult.No)
                    {
                        tbxOriFile.Text = "";
                        goto toss;
                    }
                    string outFile = Yaz0.DecodeOutputFileRename(oriFile.FileName);
                    if (!Yaz0.Decode(oriFile.FileName, outFile))
                    {
                        MessageBox.Show("Decode error:" + "\n\n" + Yaz0.lerror);
                        tbxOriFile.Text = "";
                        goto toss;
                    }
                    tbxOriFile.Text = outFile;
                    goto verify;
                }
                if (("" + ((char)fileCheck[0]) + ((char)fileCheck[1]) + ((char)fileCheck[2]) + ((char)fileCheck[3])) != "SARC")
                {
                    MessageBox.Show("Not a SARC archive! Missing SARC header at 0x00" + "\n" + "( Your file header is: " + ((char)fileCheck[0]) + ((char)fileCheck[1]) + ((char)fileCheck[2]) + ((char)fileCheck[3]) + " )");
                    tbxOriFile.Text = "";
                    goto toss;
                }

                int      nodeCount    = SARC.GetFileNodeCount(tbxOriFile.Text);
                string[] nodeTypes    = SARC.GetFileNodeType(tbxOriFile.Text);
                uint[]   nodeSizes    = SARC.GetFileNodeSizes(tbxOriFile.Text);
                string[] nodePaths    = SARC.GetFileNodePaths(tbxOriFile.Text);
                uint[]   nodePaddings = SARC.GetFileNodePaddings(tbxOriFile.Text);

                for (int i = 0; i < nodeCount; i++)
                {
                    dgvOriTable.Rows.Add();
                    dgvOriTable.Rows[i].Cells[0].Value = i + 1;
                    dgvOriTable.Rows[i].Cells[1].Value = nodeTypes[i];
                    dgvOriTable.Rows[i].Cells[2].Value = nodeSizes[i];
                    dgvOriTable.Rows[i].Cells[3].Value = nodeSizes[i].ToString("X");
                    dgvOriTable.Rows[i].Cells[4].Value = nodePaths[i];
                    dgvOriTable.Rows[i].Cells[5].Value = nodePaddings[i];
                }

toss:
                oriFile.Dispose();
                GC.Collect();
            }
            else if (rbnOriFolder.Checked)
            {
                CommonOpenFileDialog oriFolder = new CommonOpenFileDialog();
                oriFolder.IsFolderPicker = true;
                if (BotwUnpacker.Properties.Settings.Default.RootFolder != "")
                {
                    oriFolder.InitialDirectory = BotwUnpacker.Properties.Settings.Default.RootFolder;
                }
                if (oriFolder.ShowDialog() == CommonFileDialogResult.Cancel)
                {
                    goto toss;
                }
                tbxOriFile.Text = oriFolder.FileName;

                dgvOriTable.Rows.Clear();
                dgvCusTable.Refresh();
                string[] oriFolderFiles = System.IO.Directory.GetFiles(oriFolder.FileName == "" ? System.Environment.CurrentDirectory : oriFolder.FileName, "*.*", System.IO.SearchOption.AllDirectories);
                int      nodeCount      = oriFolderFiles.Length;
                if (nodeCount > 1000)
                {
                    MessageBox.Show("Too many files (1000+)!\n\nI doubt you ment to select this folder...\n" + tbxOriFile.Text + "\n\nTry again");
                    tbxOriFile.Text = "";
                    goto toss;
                }
                uint[]   nodeSizes = SARC.GetFolderFileSizes(oriFolder.FileName);
                string[] nodeTypes = SARC.GetFolderFileTypes(oriFolder.FileName);
                string[] nodePaths = SARC.GetFolderFilePaths(oriFolder.FileName);

                for (int i = 0; i < nodeCount; i++)
                {
                    dgvOriTable.Rows.Add();
                    dgvOriTable.Rows[i].Cells[0].Value = i + 1;
                    dgvOriTable.Rows[i].Cells[1].Value = nodeTypes[i];
                    dgvOriTable.Rows[i].Cells[2].Value = nodeSizes[i];
                    dgvOriTable.Rows[i].Cells[3].Value = nodeSizes[i].ToString("X");
                    dgvOriTable.Rows[i].Cells[4].Value = nodePaths[i];
                }

toss:
                oriFolder.Dispose();
                GC.Collect();
            }
        }