Skip to content

Commit

Permalink
adds rustg_dmi_icon_states (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
silicons authored Jul 8, 2023
1 parent e9f41f4 commit f00c980
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ rayon = { version = "1.7", optional = true }
dbpnoise = { version = "0.1.2", optional = true }
pathfinding = { version = "4.2.1", optional = true }
num = { version = "0.4.0", optional = true }
dmi = { version = "0.3.0", optional = true }

[features]
default = [
Expand All @@ -77,7 +78,7 @@ default = [
# default features
acreplace = ["aho-corasick"]
cellularnoise = ["rand", "rayon"]
dmi = ["png", "image"]
dmi = ["png", "image", "dep:dmi"]
file = []
git = ["git2", "chrono"]
http = ["reqwest", "serde", "serde_json", "once_cell", "jobs"]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ To get additional features, pass a list to `--features`, for example `--features
The default features are:
* acreplace: Aho-Corasick string matching and replacement.
* cellularnoise: Function to generate cellular automata-based noise.
* dmi: DMI manipulations which are impossible from within BYOND.
Used by the asset cache subsystem to improve load times.
* dmi: DMI manipulations which are impossible or degraded from within BYOND.
Mostly used by the asset cache subsystem to improve load times.
* file: Faster replacements for `file2text` and `text2file`, as well as reading or checking if files exist.
* git: Functions for robustly checking the current git revision.
* http: Asynchronous HTTP(s) client supporting most standard methods.
Expand Down
6 changes: 6 additions & 0 deletions dmsrc/dmi.dm
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#define rustg_dmi_strip_metadata(fname) RUSTG_CALL(RUST_G, "dmi_strip_metadata")(fname)
#define rustg_dmi_create_png(path, width, height, data) RUSTG_CALL(RUST_G, "dmi_create_png")(path, width, height, data)
#define rustg_dmi_resize_png(path, width, height, resizetype) RUSTG_CALL(RUST_G, "dmi_resize_png")(path, width, height, resizetype)
/**
* input: must be a path, not an /icon; you have to do your own handling if it is one, as icon objects can't be directly passed to rustg.
*
* output: json_encode'd list. json_decode to get a flat list with icon states in the order they're in inside the .dmi
*/
#define rustg_dmi_icon_states(fname) RUSTG_CALL(RUST_G, "dmi_icon_states")(fname)
24 changes: 24 additions & 0 deletions src/dmi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::error::{Error, Result};
use dmi::icon::Icon;
use png::{Decoder, Encoder, OutputInfo, Reader};
use std::{
fs::{create_dir_all, File},
io::BufReader,
path::Path,
};

Expand All @@ -25,6 +27,10 @@ byond_fn!(fn dmi_resize_png(path, width, height, resizetype) {
resize_png(path, width, height, resizetype).err()
});

byond_fn!(fn dmi_icon_states(path) {
read_states(path).ok()
});

fn strip_metadata(path: &str) -> Result<()> {
let (reader, frame_info, image) = read_png(path)?;
write_png(path, &reader, &frame_info, &image, true)
Expand Down Expand Up @@ -112,3 +118,21 @@ fn resize_png<P: AsRef<Path>>(

Ok(newimg.save_with_format(path.as_ref(), image::ImageFormat::Png)?)
}

/// Output is a JSON string for reading within BYOND
///
/// Erroring at any point will produce an empty string
fn read_states(path: &str) -> Result<String> {
let reader = BufReader::new(File::open(path)?);
let icon = Icon::load(reader).ok();
if icon.is_none() {
return Err(Error::InvalidPngData);
}
let states: Vec<_> = icon
.unwrap()
.states
.iter()
.map(|s| s.name.clone())
.collect();
Ok(serde_json::to_string(&states)?)
}

0 comments on commit f00c980

Please sign in to comment.