예제 #1
0
파일: Form1.cs 프로젝트: VelouriasMoon/FEAT
        private void Open(string infile)
        {
            FileAttributes fileAttributes = File.GetAttributes(infile);

            if (fileAttributes.HasFlag(FileAttributes.Directory))
            {
                if (ModifierKeys == Keys.Control)
                {
                    AddLine(richTextBox1, $"Building Arc from {Path.GetFileName(infile)}...");
                    FEArc.PackArc(infile, Alignment, enablePaddingToolStripMenuItem.Checked);
                    AddLine(richTextBox1, "Done");
                }
                else if (ModifierKeys == Keys.Shift)
                {
                    AddLine(richTextBox1, $"Building BCH from {Path.GetFileName(infile)}...");

                    if (infile.EndsWith("_"))
                    {
                        infile = infile.Substring(0, infile.Length - "_".Length);
                    }

                    if (File.Exists(infile + ".bch"))
                    {
                        File.Delete(infile + ".bch");
                    }

                    List <string> files = Directory.GetFiles(infile, "*.*", SearchOption.AllDirectories).ToList();
                    H3D           Scene = new H3D();
                    Scene.ConverterVersion      = 44139;
                    Scene.BackwardCompatibility = 34;
                    Scene.ForwardCompatibility  = 35;
                    foreach (string file in files)
                    {
                        Bitmap texture;
                        try
                        {
                            texture = (Bitmap)Bitmap.FromFile(file);
                        }
                        catch (OutOfMemoryException)
                        {
                            Console.WriteLine("invalid image format, skipping");
                            continue;
                        }
                        Scene.Textures.Add(new H3DTexture(Path.GetFileNameWithoutExtension(file), texture, SPICA.PICA.Commands.PICATextureFormat.RGBA8));
                    }

                    if (Scene.Textures.Count <= 0)
                    {
                        AddLine(richTextBox1, "Error");
                        AddLine(richTextBox1, $"No images found in {Path.GetFileName(infile)}");
                    }
                    else
                    {
                        H3D.Save($"{infile}.bch", Scene);
                        AddLine(richTextBox1, "Done");
                    }
                    return;
                }
                else if (ModifierKeys == Keys.Alt)
                {
                    AddLine(richTextBox1, $"Building CTPK from {Path.GetFileName(infile)}...");
                    CTPK.MakeCTPK(infile);
                    AddLine(richTextBox1, "Done");
                }
                else
                {
                    foreach (string p in (new DirectoryInfo(infile)).GetFiles().Select(f => f.FullName))
                    {
                        Open(p);
                    }
                    foreach (string p in (new DirectoryInfo(infile)).GetDirectories().Select(f => f.FullName))
                    {
                        Open(p);
                    }
                }
            }
            else
            {
                byte[] data    = File.ReadAllBytes(infile);
                string magic   = FEIO.GetMagic(data);
                string ext     = Path.GetExtension(infile);
                string outpath = ext.Length == 0 ? infile : infile.Replace(ext, "");

                if (ModifierKeys == Keys.Control || batchCompressToolStripMenuItem.Checked) // Compression Method
                {
                    byte[] cmp;
                    if (lZ10CompressionToolStripMenuItem.Checked)
                    {
                        AddLine(richTextBox1, $"Compressing {Path.GetFileName(infile)} to {Path.GetFileName(infile)}.lz using lz10...");
                        try
                        {
                            cmp = FEIO.LZ10Compress(data);
                        }
                        catch (Exception ex)
                        {
                            AddLine(richTextBox1, $"\nUnable to automatically Compress {Path.GetFileName(infile)}");
                            return;
                        }
                        File.WriteAllBytes(infile + ".lz", cmp);
                        AddLine(richTextBox1, "Done");
                    }
                    else if (lZ11ToolStripMenuItem.Checked)
                    {
                        AddLine(richTextBox1, $"Compressing {Path.GetFileName(infile)} to {Path.GetFileName(infile)}.lz using lz11...");
                        try
                        {
                            cmp = FEIO.LZ11Compress(data);
                        }
                        catch (Exception ex)
                        {
                            AddLine(richTextBox1, $"\nUnable to automatically Compress {Path.GetFileName(infile)}");
                            return;
                        }
                        File.WriteAllBytes(infile + ".lz", cmp);
                        AddLine(richTextBox1, "Done");
                    }
                    else if (lZ13ToolStripMenuItem.Checked)
                    {
                        AddLine(richTextBox1, $"Compressing {Path.GetFileName(infile)} to {Path.GetFileName(infile)}.lz using lz13...");
                        try
                        {
                            cmp = FEIO.LZ13Compress(data);
                        }
                        catch (Exception ex)
                        {
                            AddLine(richTextBox1, $"\nUnable to automatically Compress {Path.GetFileName(infile)}");
                            return;
                        }
                        File.WriteAllBytes(infile + ".lz", cmp);
                        AddLine(richTextBox1, "Done");
                    }
                    else
                    {
                        AddLine(richTextBox1, "No Compression Method Selected, How did this even happen?");
                    }
                }
                else if (ext == ".lz" || magic == "Yaz0" || ext == ".cms" || ext == ".cmp") // Decompress Method
                {
                    byte[] dcmp;
                    if (data[0] == 0x10)
                    {
                        AddLine(richTextBox1, $"Decompressing {Path.GetFileName(infile)} using lz10...");
                        try
                        {
                            dcmp = FEIO.LZ10Decompress(data);
                        }
                        catch (Exception e)
                        {
                            AddLine(richTextBox1, $"\nUnable to automatically Decompress {Path.GetFileName(infile)}");
                            return;
                        }
                        File.WriteAllBytes(infile.Replace(".lz", ""), dcmp);
                        AddLine(richTextBox1, "Done");
                    } //LZ10
                    else if (data[0] == 0x11)
                    {
                        AddLine(richTextBox1, $"Decompressing {Path.GetFileName(infile)} using lz11...");
                        try
                        {
                            dcmp = FEIO.LZ11Decompress(data);
                        }
                        catch (Exception e)
                        {
                            AddLine(richTextBox1, $"\nUnable to automatically Decompress {Path.GetFileName(infile)}");
                            return;
                        }
                        File.WriteAllBytes(infile.Replace(".lz", ""), dcmp);
                        AddLine(richTextBox1, "Done");
                    } //LZ11
                    else if (data[0] == 0x13 && data[4] == 0x11)
                    {
                        AddLine(richTextBox1, $"Decompressing {Path.GetFileName(infile)} using lz13...");
                        try
                        {
                            dcmp = FEIO.LZ13Decompress(data);
                        }
                        catch (Exception e)
                        {
                            AddLine(richTextBox1, $"\nUnable to automatically Decompress {Path.GetFileName(infile)}");
                            return;
                        }
                        File.WriteAllBytes(infile.Replace(".lz", ""), dcmp);
                        AddLine(richTextBox1, "Done");
                    } //LZ13
                    else if (magic == "Yaz0")
                    {
                        AddLine(richTextBox1, "Yaz0 Method not implemented");
                        return;
                    } //Yaz0
                    if (File.Exists(infile.Replace(".lz", "")) && autoExtractToolStripMenuItem.Checked)
                    {
                        Open(infile.Replace(".lz", ""));
                    }
                }
                else if (ext == ".arc") //Archive file
                {
                    AddLine(richTextBox1, $"Extract Files from {Path.GetFileName(infile)}...");
                    FEArc.ExtractArc(outpath, data);
                    AddLine(richTextBox1, "Done");
                }
                else if (magic == "BCH" || magic == "CGFX") //BCH / Bcres file
                {
                    H3D Scene = new H3D();
                    if (magic == "CGFX")
                    {
                        Scene = Gfx.Open(infile).ToH3D();
                    }
                    else
                    {
                        Scene = H3D.Open(data);
                    }

                    if (Directory.Exists(outpath))
                    {
                        Directory.Delete(outpath, true);
                    }
                    if (File.Exists(outpath))
                    {
                        outpath = outpath + "_";
                    }

                    //Export method for textures, this is always enabled by default
                    if (Scene.Textures.Count > 0)
                    {
                        AddLine(richTextBox1, $"Extracting Textures from {Path.GetFileName(infile)}...");
                        if (!Directory.Exists(outpath))
                        {
                            Directory.CreateDirectory(outpath);
                        }

                        foreach (var texture in Scene.Textures)
                        {
                            Image img = texture.ToBitmap();
                            img.Save($"{outpath}\\{texture.Name}.png", System.Drawing.Imaging.ImageFormat.Png);
                        }
                        AddLine(richTextBox1, "Done");
                    }
                    if (exportDaeToolStripMenuItem.Checked || exportSMDToolStripMenuItem.Checked && Scene.Models.Count > 0)
                    {
                        AddLine(richTextBox1, $"Extracting Models from {Path.GetFileName(infile)}...");
                        if (!Directory.Exists(outpath))
                        {
                            Directory.CreateDirectory(outpath);
                        }

                        for (int i = 0; i < Scene.Models.Count; i++)
                        {
                            if (exportDaeToolStripMenuItem.Checked)
                            {
                                DAE dae = new DAE(Scene, i);
                                dae.Save($"{outpath}\\{Scene.Models[i].Name}.dae");
                            }
                            if (exportSMDToolStripMenuItem.Checked)
                            {
                                SMD smd = new SMD(Scene, i);
                                smd.Save($"{outpath}\\{Scene.Models[i].Name}.smd");
                            }
                        }
                        AddLine(richTextBox1, "Done");
                    }
                }
                else if (magic == "CTPK") //CTPK file
                {
                    AddLine(richTextBox1, $"Extract Textures from {Path.GetFileName(infile)}...");
                    CTPK.ExtractCTPK(infile);
                    AddLine(richTextBox1, "Done");
                }
                else if (ext == ".bin")
                {
                    if (FEIO.ReadStringFromArray(data, Encoding.UTF8, 0x20).Contains("MESS_ARCHIVE"))
                    {
                        AddLine(richTextBox1, $"Extracting Message Archive {Path.GetFileName(infile)}...");
                        FEMessage.ExtractMessage(infile.Replace(ext, ".txt"), data);
                        AddLine(richTextBox1, "Done");
                    }
                    else if (enableBinDecomplingToolStripMenuItem.Checked)
                    {
                        AddLine(richTextBox1, $"Decompiling {Path.GetFileName(infile)} to txt...");
                        FEBin.ExtractBin(infile);
                        AddLine(richTextBox1, "Done");
                    }
                }
                if (deleteAfterProcessingToolStripMenuItem.Checked)
                {
                    File.Delete(infile);
                }
            }
        }