Created
May 17, 2025 20:40
-
-
Save lordofpipes/b8ec598fe08a3e86bba1c1cb78d7e89b to your computer and use it in GitHub Desktop.
Remove extraneous CustomData
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [package] | |
| name = "removecustomname" | |
| version = "0.1.0" | |
| edition = "2024" | |
| [dependencies] | |
| fastnbt = "2.5.0" | |
| flate2 = "1.1.1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use fastnbt::Value; | |
| use fastnbt::{from_reader, to_writer}; | |
| use flate2::{Compression, read::GzDecoder, write::GzEncoder}; | |
| use std::error::Error; | |
| use std::fs::{self, File}; | |
| use std::io::{self}; | |
| use std::path::Path; | |
| fn process_file(path: &Path) -> Result<(), Box<dyn Error>> { | |
| let metadata = fs::metadata(path)?; | |
| let original_mtime = metadata.modified()?; | |
| let file = File::open(path)?; | |
| let mut decoder = GzDecoder::new(file); | |
| let mut root = from_reader(&mut decoder)?; | |
| if let Value::Compound(compound) = &mut root { | |
| if compound.remove_entry("CustomName").is_some() { | |
| compound.remove_entry("CustomNameVisible"); | |
| println!("{}: removed CustomName", path.display()); | |
| let file = File::create(path)?; | |
| let mut encoder = GzEncoder::new(&file, Compression::default()); | |
| to_writer(&mut encoder, &root)?; | |
| encoder.finish()?; | |
| file.set_modified(original_mtime)?; | |
| } | |
| } | |
| Ok(()) | |
| } | |
| fn main() -> io::Result<()> { | |
| let args = std::env::args().collect::<Vec<_>>(); | |
| let dir_path = Path::new(&args[1]); | |
| for entry in fs::read_dir(dir_path)? { | |
| let entry = entry?; | |
| let path = entry.path(); | |
| if path.is_file() && path.extension().map(|ext| ext == "dat").unwrap_or(false) { | |
| if let Err(e) = process_file(&path) { | |
| eprintln!("{}: error processing file: {}", path.display(), e); | |
| } | |
| } | |
| } | |
| Ok(()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment