// loads graffitis of specific size and displays the images with Graffiti_PicBox in flowLayoutPanel1 private void list_graffitis(int size) { flowLayoutPanel1.Controls.Clear(); tex_dir = txtb_jsrf_mod_dir.Text; if (tex_dir == "") { MessageBox.Show("Error: \"JSRF TEX folder in Settings is not defined, please select the folder."); tabControl1.SelectedIndex = 1; return; } if (!Directory.Exists(tex_dir)) { MessageBox.Show("Media\\Mark\\TEX\\ Directory not found.\nPlease make sure you have set the proper JSRF path\nand that JSRF files are in place."); return; } // search .grf grafitti files of a certain size string[] files = Directory.GetFiles(txtb_jsrf_mod_dir.Text.TrimEnd(Path.DirectorySeparatorChar), "grf_" + size.ToString() + "_*.grf"); if (files.Length == 0) { MessageBox.Show("No graffiti files found."); return; } foreach (var file in files) { Graffiti_PicBox picbox = load_image_to_graffitiPicBox(file); if (picbox != null) { // add graffiti picturebox to flowlayout panel flowLayoutPanel1.Controls.Add(picbox); } } }
// loads image as extended picture box private Graffiti_PicBox load_image_to_graffitiPicBox(string filepath) { Graffiti_PicBox PicBox = new Graffiti_PicBox(); string filename = Path.GetFileNameWithoutExtension(filepath); // make sure its a graffiti file if (!filename.Contains("grf_")) { return(null); } int size = Convert.ToInt16(filename.Split('_')[1]); // filepath without extension filepath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(filepath), System.IO.Path.GetFileNameWithoutExtension(filepath)); // make a copy of the .grf file as .tga if it doesn't already exist if (!File.Exists(filepath + ".tga")) { File.Copy(filepath + ".grf", filepath + ".tga"); } // if image hasn't been converted to bmp yet if (!File.Exists(filepath + ".png")) { // convert to bmp so we can load it in the picturebox img_convert(filename, tex_dir, "tga", "png", ""); } PicBox.filepath = filepath + ".tga"; Bitmap bitmap = LoadBitmap(filepath + ".png"); Bitmap bitmap_thumb = bitmap; // scale picturebox depending on the graffiti size switch (size) { case 0: case 1: PicBox.Size = new Size(125, 125); bitmap_thumb = new Bitmap(bitmap); break; case 2: PicBox.Size = new Size(150, 100); bitmap_thumb = new Bitmap(bitmap); break; case 3: PicBox.Size = new Size(175, 87); // resize image to save memory space bitmap_thumb = new Bitmap(bitmap, new Size(bitmap.Width / 3, bitmap.Height / 3)); break; case 4: PicBox.Size = new Size(256, 64); // resize image to save memory space bitmap_thumb = new Bitmap(bitmap, new Size(bitmap.Width / 4, bitmap.Height / 4)); break; } PicBox.set_image(bitmap_thumb); bitmap.Dispose(); return(PicBox); }
// revert graffiti image to original private void btn_restore_Click(object sender, EventArgs e) { if (txtb_jsrf_original_dir.Text == "") { MessageBox.Show("JSRF TEX folder (original files) in Settings is not defined, please select the folder."); tabControl1.SelectedIndex = 1; return; } if (!Directory.Exists(txtb_jsrf_original_dir.Text)) { MessageBox.Show("Error: \"JSRF folder: original files\" in Settings does not exist."); return; } string subolfder = ""; int split_dir_at = 0; string filepath = get_selected_graffiti_filepath(); if (filepath == "") { return; } string[] folders = filepath.Split(Path.DirectorySeparatorChar); // find the "Media" folder in path and substract the parent folders from it for (int i = 0; i < folders.Length; i++) { if (folders[i] == "TEX") { split_dir_at = i; } } for (int i = split_dir_at + 1; i < folders.Length; i++) { subolfder = subolfder + "\\" + folders[i]; } string ori_filepath = txtb_jsrf_original_dir.Text.TrimEnd(Path.DirectorySeparatorChar) + subolfder; string ori_filepath_noExt = ori_filepath.Replace(".tga", ""); if (!File.Exists(ori_filepath_noExt + ".grf")) { MessageBox.Show("Error: original file (" + ori_filepath_noExt + ".grf" + ") does not exist."); return; } if (!File.Exists(ori_filepath_noExt + ".prs")) { MessageBox.Show("Error: original file (" + ori_filepath_noExt + ".prs" + ") does not exist."); return; } // replace the modded file by a copy of the original File.Copy(ori_filepath_noExt + ".grf", filepath, true); File.Copy(ori_filepath_noExt + ".grf", filepath.Replace(".tga", ".grf"), true); File.Copy(ori_filepath_noExt + ".prs", filepath.Replace(".tga", ".prs"), true); int size = Convert.ToInt16(Path.GetFileNameWithoutExtension(filepath).Split('_')[1]); // re-import graffiti picturebox item for (int i = 0; i < flowLayoutPanel1.Controls.Count; i++) { Graffiti_PicBox item = (Graffiti_PicBox)flowLayoutPanel1.Controls[i]; if (item.filepath == filepath) { // convert to bmp so we can load it in the picturebox img_convert(Path.GetFileNameWithoutExtension(filepath), tex_dir, "tga", "png", ""); // Graffiti_PicBox import = load_image_to_graffitiPicBox(filepath); #region generate bitmap thumbnail Bitmap bitmap = LoadBitmap(filepath.Replace(".tga", ".png")); Bitmap bitmap_thumb = bitmap; // scale picturebox depending on the graffiti size switch (size) { case 0: case 1: bitmap_thumb = new Bitmap(bitmap); break; case 2: bitmap_thumb = new Bitmap(bitmap); break; case 3: // resize image to save memory space bitmap_thumb = new Bitmap(bitmap, new Size(bitmap.Width / 3, bitmap.Height / 3)); break; case 4: // resize image to save memory space bitmap_thumb = new Bitmap(bitmap, new Size(bitmap.Width / 4, bitmap.Height / 4)); break; } bitmap.Dispose(); #endregion // set image item.set_image(bitmap_thumb); item.Update(); break; } } System.Media.SystemSounds.Beep.Play(); }
// imports .tga image as .bmp and update to corresponding graffiti picturebox (determined by file name) // and convert .tga to .grf private void btn_import_Click(object sender, EventArgs e) { string filepath = get_selected_graffiti_filepath(); if (filepath == "") { return; } if (!File.Exists(filepath)) { MessageBox.Show("Could not find file: \n" + filepath); return; } int size = Convert.ToInt16(Path.GetFileNameWithoutExtension(filepath).Split('_')[1]); #region check tga dimensions are correct //read TGA file to get X Y dimensions string tga_size; using (BinaryReader b = new BinaryReader(File.Open(filepath, FileMode.Open))) { // jump headers and stuff directly to what we want b.BaseStream.Position = 12; tga_size = b.ReadInt16().ToString() + " " + b.ReadInt16().ToString(); b.Close(); b.Dispose(); } // if texture size from .tga doesn't match the selected graffiti size warn user and return if (tga_size != texture_sizes[size]) { MessageBox.Show("Texture resolution for this graffiti size should be " + texture_sizes[size].Replace(" ", " by ") + " pixels.", "Error: invalid texture resolution."); return; } #endregion // re-import graffiti picturebox item for (int i = 0; i < flowLayoutPanel1.Controls.Count; i++) { Graffiti_PicBox item = (Graffiti_PicBox)flowLayoutPanel1.Controls[i]; if (item.filepath == filepath) { // convert to bmp so we can load it in the picturebox img_convert(Path.GetFileNameWithoutExtension(filepath), tex_dir, "tga", "png", ""); // Graffiti_PicBox import = load_image_to_graffitiPicBox(filepath); #region generate bitmap thumbnail Bitmap bitmap = LoadBitmap(filepath.Replace(".tga", ".png")); Bitmap bitmap_thumb = bitmap; // scale picturebox depending on the graffiti size switch (size) { case 0: case 1: bitmap_thumb = new Bitmap(bitmap); break; case 2: bitmap_thumb = new Bitmap(bitmap); break; case 3: // resize image to save memory space bitmap_thumb = new Bitmap(bitmap, new Size(bitmap.Width / 3, bitmap.Height / 3)); break; case 4: // resize image to save memory space bitmap_thumb = new Bitmap(bitmap, new Size(bitmap.Width / 4, bitmap.Height / 4)); break; } bitmap.Dispose(); #endregion // set image item.set_image(bitmap_thumb); item.Update(); break; } } string filepath_noExt = filepath.Replace(".tga", ""); IO.delete_safe(filepath_noExt + ".dds"); // convert to DDS DXT3 (no mipmaps) img_convert(Path.GetFileNameWithoutExtension(filepath), tex_dir, "tga", "dds", "-format=DXT3"); if (!File.Exists(filepath_noExt + ".dds")) { MessageBox.Show("Could not convert .tga to .dds", "Error"); return; } IO.delete_safe(filepath_noExt + ".prs"); // rename dds to prs File.Move(filepath_noExt + ".dds", filepath_noExt + ".prs"); // save .tga as .grf IO.delete_safe(filepath_noExt + ".grf"); // rename dds to prs File.Copy(filepath_noExt + ".tga", filepath_noExt + ".grf"); // remove DDS header (128 bytes) try { byte[] bytes = System.IO.File.ReadAllBytes(filepath_noExt + ".prs"); byte[] headless = new byte[bytes.Length - 128]; Array.Copy(bytes, 128, headless, 0, bytes.Length - 128); System.IO.File.WriteAllBytes(filepath_noExt + ".prs", headless); } catch (System.Exception excep) { MessageBox.Show("Failed converting dds to prs: " + excep.Message); return; } System.Media.SystemSounds.Beep.Play(); }