Esempio n. 1
0
        public static void DumpDatabase(string folder)
        {
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            foreach (TEXN tex in m_textures)
            {
                string textureName = "tex_" + tex.TextureID.Data.ToString("x16") + ".png";

                PNG png = new PNG(tex.Texture);
                png.Write(folder + textureName);
            }
        }
Esempio n. 2
0
        private void button_Unpack2_Click(object sender, EventArgs e)
        {
            InitializeJSON();

            // create json
            string sdtextureoverridePath = textBox_Folder2.Text + SM_TEXTURES + "\\" + SM_TEXOVER;

            if (!Directory.Exists(Path.GetDirectoryName(sdtextureoverridePath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(sdtextureoverridePath));
            }
            using (FileStream stream = File.Open(sdtextureoverridePath, FileMode.Create))
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    writer.Write(Resources.SDTextureOverride_SM2_Clean);
                }
            }

            // unpack mapped data from disk tac
            string tadFilepath = textBox_Folder2.Text + SM_ARCHIVE_DATA + SM2_DISK + ".tad";
            string tacFilepath = textBox_Folder2.Text + SM_ARCHIVE_DATA + SM2_DISK + ".tac";

            TAD tad = new TAD(tadFilepath);
            TAC tac = new TAC(tad);

            textures.Clear();
            foreach (var entry in tad.Entries)
            {
                if (entry.FileName == "/misc/cold.bin")
                {
                    continue;                                     // not supported
                }
                byte[] buffer = tac.GetFileBuffer(entry);
                ReadFileRecursive(buffer);
            }

            bool   first       = true;
            string prevDdsPath = "";
            string prevPngPath = "";

            foreach (var tex in textures)
            {
                first = true;
                List <HashEntry> matches = new List <HashEntry>();
                foreach (var t in SM2_Hashes)
                {
                    if (tex.texID == t.texID)
                    {
                        if (first) // if first match, write DDS or PNG file
                        {
                            if (radioButton_DDS1.Checked)
                            {
                                DDS    dds      = new DDS(tex.image);
                                string filepath = textBox_Folder2.Text + SM_TEXTURES + t.Filepath.Replace("/", "\\");
                                dds.MipHandling   = DDSGeneral.MipHandling.KeepTopOnly;
                                dds.AlphaSettings = DDSGeneral.AlphaSettings.KeepAlpha;
                                dds.FormatDetails = new DDSFormats.DDSFormatDetails(DDSFormat.DDS_DXT3);
                                dds.Write(filepath);
                                prevDdsPath = filepath;
                            }

                            if (radioButton_PNG1.Checked)
                            {
                                PNG    png         = new PNG(tex.image);
                                string filepath    = textBox_Folder2.Text + SM_TEXTURES + t.Filepath.Replace("/", "\\");
                                string filepathPng = Path.ChangeExtension(filepath, ".png");
                                png.Write(filepathPng);
                                prevPngPath = filepathPng;
                            }

                            first = false;
                        }
                        else // if duplicate, copy/paste the texture file (DDS and PNG writing costs alot of resources)
                        {
                            string filepath    = textBox_Folder2.Text + SM_TEXTURES + t.Filepath.Replace("/", "\\");
                            string filepathPng = Path.ChangeExtension(filepath, ".png");
                            string dir         = Path.GetDirectoryName(filepath);
                            if (!Directory.Exists(dir))
                            {
                                Directory.CreateDirectory(dir);
                            }
                            if (radioButton_DDS1.Checked)
                            {
                                File.Copy(prevDdsPath, filepath, true);
                            }
                            if (radioButton_PNG1.Checked)
                            {
                                File.Copy(prevPngPath, filepathPng, true);
                            }
                        }
                        matches.Add(t);
                    }
                }

                // Remove matches to increase iteration speed, will result in the first found texture being used
                foreach (var match in matches)
                {
                    SM2_Hashes.Remove(match);
                }
            }
            textures.Clear();
            foreach (var hash in SM2_Hashes)
            {
                Console.WriteLine("Texture not found: {0} = {1}", hash.texID.HexStr, hash.Filepath);
            }
        }
        private void button_Convert_Click(object sender, EventArgs e)
        {
            string outputFolder = textBox_OutputFolder.Text;

            if (String.IsNullOrEmpty(outputFolder))
            {
                return;
            }
            if (!Directory.Exists(outputFolder))
            {
                Directory.CreateDirectory(outputFolder);
            }

            List <BaseImage> images = new List <BaseImage>();

            if (checkBox_ConvertSelected.Checked)
            {
                foreach (BaseImage img in listBox_Images.SelectedItems)
                {
                    images.Add(img);
                }
            }
            else
            {
                foreach (BaseImage img in listBox_Images.Items)
                {
                    images.Add(img);
                }
            }

            if ((Type)comboBox_ImageFormat.SelectedItem == typeof(PVRT))
            {
                PVRTSettings settings = pvrtControl.Settings;
                foreach (BaseImage image in images)
                {
                    PVRT pvrt = new PVRT(image);
                    pvrt.PixelFormat = settings.PixelFormat;
                    pvrt.DataFormat  = settings.DataFormat;

                    if (settings.CreateTEXN)
                    {
                        string   srcFilename = Path.GetFileName(image.FilePath);
                        string[] splitted    = srcFilename.Split('.');
                        string   texIDString = splitted[0];
                        UInt64   texID       = Convert.ToUInt64(texIDString, 16);

                        TEXN texn = new TEXN();
                        texn.TextureID.Data = texID;

                        string filename = String.Format("{0}.{1}.TEXN", Helper.ByteArrayToString(BitConverter.GetBytes(texn.TextureID.Data)), texn.TextureID.Name.Replace("\0", "_"));
                        string filepath = outputFolder + "\\" + filename;

                        using (FileStream fileStream = new FileStream(filepath, FileMode.Create))
                        {
                            using (BinaryWriter writer = new BinaryWriter(fileStream))
                            {
                                long offset = fileStream.Position;
                                texn.WriteHeader(writer);
                                if (settings.InsertDDS && typeof(DDS).IsAssignableFrom(image.GetType()))
                                {
                                    pvrt.WriteDDSRaw(writer, (DDS)image);
                                }
                                else
                                {
                                    pvrt.Write(writer);
                                }
                                texn.EntrySize = (uint)(fileStream.Position - offset);
                                fileStream.Seek(offset, SeekOrigin.Begin);
                                texn.WriteHeader(writer);
                            }
                        }
                    }
                    else
                    {
                        string filepath = outputFolder + "\\" + Path.ChangeExtension(Path.GetFileName(image.FilePath), ".PVR");

                        using (FileStream fileStream = new FileStream(filepath, FileMode.Create))
                        {
                            using (BinaryWriter writer = new BinaryWriter(fileStream))
                            {
                                if (settings.InsertDDS && typeof(DDS).IsAssignableFrom(image.GetType()))
                                {
                                    pvrt.WriteDDSRaw(writer, (DDS)image);
                                }
                                else
                                {
                                    pvrt.Write(writer);
                                }
                            }
                        }
                    }
                }
            }
            else if ((Type)comboBox_ImageFormat.SelectedItem == typeof(DDS))
            {
                DDSSettings settings = ddsControl.Settings;
                foreach (BaseImage image in images)
                {
                    string filepath = outputFolder + "\\" + Path.ChangeExtension(Path.GetFileName(image.FilePath), ".dds");
                    DDS    dds      = new DDS(image);
                    dds.AlphaSettings = settings.AlphaSettings;
                    dds.MipHandling   = settings.MipHandling;
                    dds.FormatDetails = new DDSFormats.DDSFormatDetails(settings.DDSFormat, settings.DXGIFormat);
                    dds.Write(filepath);
                }
            }
            else if ((Type)comboBox_ImageFormat.SelectedItem == typeof(PNG))
            {
                foreach (BaseImage image in images)
                {
                    string filepath = outputFolder + "\\" + Path.ChangeExtension(Path.GetFileName(image.FilePath), ".png");
                    PNG    png      = new PNG(image);
                    png.Write(filepath);
                }
            }
            else if ((Type)comboBox_ImageFormat.SelectedItem == typeof(BMP))
            {
                foreach (BaseImage image in images)
                {
                    string filepath = outputFolder + "\\" + Path.ChangeExtension(Path.GetFileName(image.FilePath), ".bmp");
                    BMP    bmp      = new BMP(image);
                    bmp.Write(filepath);
                }
            }
            else if ((Type)comboBox_ImageFormat.SelectedItem == typeof(JPEG))
            {
                foreach (BaseImage image in images)
                {
                    string filepath = outputFolder + "\\" + Path.ChangeExtension(Path.GetFileName(image.FilePath), ".jpg");
                    JPEG   jpg      = new JPEG(image);
                    jpg.Write(filepath);
                }
            }
        }