-
-
Notifications
You must be signed in to change notification settings - Fork 163
Support for schematics file format #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
bc6d058
dbe79ab
8ee6c3f
b7d671a
86f7b9c
2a96178
4ae80ba
314eb0c
4c37f60
ada8da4
a2e32be
8f474ee
a076977
8c0054c
953a851
58001c9
ae387ff
9bf5af0
13e0594
1c5c8fa
d4eb42d
32f20ca
e4c20b9
3c7a2b2
e646c63
b24fb5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,9 @@ | |
| use std::fmt; | ||
| use std::fmt::Display; | ||
| use std::iter::FusedIterator; | ||
| use std::str::FromStr; | ||
|
|
||
| use thiserror::Error; | ||
| use valence_ident::{ident, Ident}; | ||
|
|
||
| use crate::item::ItemKind; | ||
|
|
@@ -48,6 +50,53 @@ fn fmt_block_state(bs: BlockState, f: &mut fmt::Formatter) -> fmt::Result { | |
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Error, PartialEq, Eq)] | ||
| pub enum ParseBlockStateError { | ||
| #[error("unknown block kind '{0}'")] | ||
| UnknownBlockKind(String), | ||
| #[error("invalid prop string '{0}'")] | ||
| InvalidPropString(String), | ||
| #[error("unknown prop name '{0}'")] | ||
| UnknownPropName(String), | ||
| #[error("unknown prop value '{0}'")] | ||
| UnknownPropValue(String), | ||
| } | ||
|
|
||
| impl FromStr for BlockState { | ||
| type Err = ParseBlockStateError; | ||
|
|
||
| fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add some tests for this?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should make this tolerant of the A bit out of scope, but we should really replace
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem that it's not necessarily
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
as for this: I believe that would be a better fit for the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The whole thing isn't an ident, but presumably the string before the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
ahh right I misread part of your first message 😄 I see know |
||
| let state = match s.split_once('[') { | ||
| Some((kind, props)) => { | ||
| let Some(kind) = BlockKind::from_str(kind) else { | ||
| return Err(ParseBlockStateError::UnknownBlockKind(kind.to_string())); | ||
| }; | ||
| props[..props.len() - 1] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will panic if |
||
| .split(',') | ||
| .map(|prop| prop.trim()) | ||
| .try_fold(kind.to_state(), |state, prop| { | ||
| let Some((name, val)) = prop.split_once('=') else { | ||
| return Err(ParseBlockStateError::InvalidPropString(prop.to_string())); | ||
| }; | ||
| let Some(name) = PropName::from_str(name) else { | ||
| return Err(ParseBlockStateError::UnknownPropName(name.to_string())); | ||
| }; | ||
| let Some(val) = PropValue::from_str(val) else { | ||
| return Err(ParseBlockStateError::UnknownPropValue(val.to_string())); | ||
| }; | ||
| Ok(state.set(name, val)) | ||
| })? | ||
| } | ||
| None => match BlockKind::from_str(s) { | ||
| Some(kind) => kind.to_state(), | ||
| None => return Err(ParseBlockStateError::UnknownBlockKind(s.to_string())), | ||
| }, | ||
| }; | ||
|
|
||
| Ok(state) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [package] | ||
| name = "valence_schem" | ||
| description = "A library for the Sponge Schematic Format." | ||
| documentation.workspace = true | ||
| repository = "https://github.com/valence-rs/valence/tree/main/crates/valence_schem" | ||
| readme = "README.md" | ||
| license.workspace = true | ||
| keywords = ["schematics", "minecraft", "deserialization"] | ||
| version.workspace = true | ||
| edition.workspace = true | ||
|
|
||
| [dependencies] | ||
| flate2.workspace = true | ||
| glam.workspace = true | ||
| thiserror.workspace = true | ||
| valence_server.workspace = true | ||
| valence_nbt = {workspace = true, features = ["valence_ident"]} | ||
|
Comment on lines
+14
to
+17
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. glam yes, but with from |
||
|
|
||
| [dev-dependencies] | ||
| valence.workspace = true | ||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,43 @@ | ||||||||||||
| # valence_schem | ||||||||||||
|
|
||||||||||||
| Support for the [Sponge schematic file format](https://github.com/SpongePowered/Schematic-Specification). | ||||||||||||
|
|
||||||||||||
| This crate implements [Sponge schematics] | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is redundant. |
||||||||||||
|
|
||||||||||||
| Loading schematics (version 1 through 3) from [`Compounds`](Compound) is | ||||||||||||
| supported. Saving schematics to [`Compounds`](Compound) (version 3 only) is | ||||||||||||
| supported. | ||||||||||||
|
|
||||||||||||
| # Examples | ||||||||||||
|
|
||||||||||||
| An example that shows how to load and save [schematics] from and to the | ||||||||||||
| filesystem | ||||||||||||
|
|
||||||||||||
| ```rust | ||||||||||||
| # use valence_schem::Schematic; | ||||||||||||
| use flate2::Compression; | ||||||||||||
|
Comment on lines
+17
to
+18
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| fn schem_from_file(path: &str) -> Schematic { | ||||||||||||
| Schematic::load(path).unwrap() | ||||||||||||
| } | ||||||||||||
| fn schem_to_file(schematic: &Schematic, path: &str) { | ||||||||||||
| schematic.save(path); | ||||||||||||
| } | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| There are also methods to serialize and deserialize [schematics] from and to | ||||||||||||
| [`Compounds`](Compound): | ||||||||||||
| ```rust | ||||||||||||
| # use valence_schem::Schematic; | ||||||||||||
| use valence_nbt::Compound; | ||||||||||||
|
Comment on lines
+30
to
+31
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| fn schem_from_compound(compound: &Compound) { | ||||||||||||
| let schematic = Schematic::deserialize(compound).unwrap(); | ||||||||||||
| let comp = schematic.serialize(); | ||||||||||||
| } | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ### See also | ||||||||||||
|
|
||||||||||||
| Examples in the `examples/` directory | ||||||||||||
|
|
||||||||||||
| [Sponge schematics]: <https://github.com/SpongePowered/Schematic-Specification> | ||||||||||||
| [schematics]: Schematic | ||||||||||||
Uh oh!
There was an error while loading. Please reload this page.