-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command for stripping a specific property from Instances to rbx-u…
…til (#426) When debugging an issue that cropped up on the Roblox devforums yesterday, I wanted an easy way to remove every instance of property from a file (specifically, `SurfaceAppearance.Color`). Given that there's no easy way to do this without converting a file to XML, removing the property, then converting it back, I think it'd be helpful to include in rbx-util in the future.
- Loading branch information
Showing
5 changed files
with
100 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
# Changelog | ||
|
||
## Version 0.2.1 | ||
|
||
- Added `remove-prop` command to strip a property from a file | ||
|
||
## Version 0.2.0 | ||
|
||
- Refactor commands into seperate files | ||
- Add `verbosity` and `color` global flags to control logging and terminal color | ||
|
||
# Version 0.1.0 | ||
## Version 0.1.0 | ||
|
||
- Initial implementation | ||
- Initial implementation |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use std::{ | ||
io::{BufReader, BufWriter}, | ||
path::PathBuf, | ||
}; | ||
|
||
use anyhow::Context as _; | ||
use clap::Parser; | ||
use fs_err::File; | ||
|
||
use crate::ModelKind; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct RemovePropCommand { | ||
/// The file to remove the property from. | ||
input: PathBuf, | ||
#[clap(long, short)] | ||
/// The place to write the stripped file to. | ||
output: PathBuf, | ||
/// The class name to remove the property from. | ||
class_name: String, | ||
/// The property to remove from the provided class. | ||
prop_name: String, | ||
} | ||
|
||
impl RemovePropCommand { | ||
pub fn run(&self) -> anyhow::Result<()> { | ||
let input_kind = ModelKind::from_path(&self.input)?; | ||
let output_kind = ModelKind::from_path(&self.output)?; | ||
|
||
let input_file = BufReader::new(File::open(&self.input)?); | ||
|
||
log::debug!("Reading from {input_kind:?} file {}", self.input.display()); | ||
let mut dom = match input_kind { | ||
ModelKind::Xml => { | ||
let options = rbx_xml::DecodeOptions::new() | ||
.property_behavior(rbx_xml::DecodePropertyBehavior::ReadUnknown); | ||
|
||
rbx_xml::from_reader(input_file, options) | ||
.with_context(|| format!("Failed to read {}", self.input.display()))? | ||
} | ||
|
||
ModelKind::Binary => rbx_binary::from_reader(input_file) | ||
.with_context(|| format!("Failed to read {}", self.input.display()))?, | ||
}; | ||
|
||
let mut queue = vec![dom.root_ref()]; | ||
while let Some(referent) = queue.pop() { | ||
let inst = dom.get_by_ref_mut(referent).unwrap(); | ||
if inst.class == self.class_name { | ||
log::trace!("Removed property {}.{}", inst.name, self.prop_name); | ||
inst.properties.remove(&self.prop_name); | ||
} | ||
queue.extend_from_slice(inst.children()); | ||
} | ||
|
||
let output_file = BufWriter::new(File::create(&self.output)?); | ||
|
||
let root_ids = dom.root().children(); | ||
match output_kind { | ||
ModelKind::Xml => { | ||
let options = rbx_xml::EncodeOptions::new() | ||
.property_behavior(rbx_xml::EncodePropertyBehavior::WriteUnknown); | ||
|
||
rbx_xml::to_writer(output_file, &dom, root_ids, options) | ||
.with_context(|| format!("Failed to write {}", self.output.display()))?; | ||
} | ||
|
||
ModelKind::Binary => { | ||
rbx_binary::to_writer(output_file, &dom, root_ids) | ||
.with_context(|| format!("Failed to write {}", self.output.display()))?; | ||
} | ||
} | ||
log::info!( | ||
"Wrote stripped {output_kind:?} file to {}", | ||
self.output.display() | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |