Skip to content
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

Datapack parsing #455

Merged
merged 8 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
.cargo

world/
feather/downloaded
feather/datapacks/minecraft/
/config.toml

# Python cache files (libcraft)
Expand Down
48 changes: 46 additions & 2 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions feather/blocks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ impl BlockId {
VANILLA_ID_TABLE[self.kind as u16 as usize][self.state as usize]
}

/*
/// Returns the vanilla fluid ID for this block in case it is a fluid.
/// The fluid ID is used in the Tags packet.
pub fn vanilla_fluid_id(self) -> Option<u16> {
Expand All @@ -112,7 +111,6 @@ impl BlockId {
None
}
}
*/

/// Returns the block corresponding to the given vanilla ID.
///
Expand Down
1 change: 1 addition & 0 deletions feather/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ uuid = { version = "0.8", features = [ "v4" ] }
libcraft-core = { path = "../../libcraft/core" }
rayon = "1.5"
worldgen = { path = "../worldgen", package = "feather-worldgen" }
datapacks = { path = "../datapacks", package = "feather-datapacks" }
rand = "0.8"
7 changes: 7 additions & 0 deletions feather/common/src/game.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{cell::RefCell, mem, rc::Rc, sync::Arc};

use base::{BlockId, BlockPosition, ChunkPosition, Position, Text, Title};
use datapacks::{RecipeRegistry, TagRegistry};
use ecs::{
Ecs, Entity, EntityBuilder, HasEcs, HasResources, NoSuchEntity, Resources, SysResult,
SystemExecutor,
Expand Down Expand Up @@ -54,6 +55,10 @@ pub struct Game {
entity_spawn_callbacks: Vec<EntitySpawnCallback>,

entity_builder: EntityBuilder,

pub tag_registry: TagRegistry,

pub recipe_registry: RecipeRegistry,
}

impl Default for Game {
Expand All @@ -74,6 +79,8 @@ impl Game {
tick_count: 0,
entity_spawn_callbacks: Vec::new(),
entity_builder: EntityBuilder::new(),
tag_registry: TagRegistry::new(),
recipe_registry: RecipeRegistry::new(),
}
}

Expand Down
13 changes: 9 additions & 4 deletions feather/datapacks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
[package]
name = "feather-datapacks"
version = "0.1.0"
authors = [ "caelunshun <[email protected]>" ]
authors = [ "koskja <[email protected]>", "caelunshun <[email protected]>" ]
edition = "2018"

[dependencies]
ahash = "0.4"
anyhow = "1"
log = "0.4"
serde = { version = "1", features = [ "derive" ] }
serde_json = "1"
smartstring = { version = "0.2", features = [ "serde" ] }
thiserror = "1"
ureq = { version = "2", default-features = false, features = [ "tls" ] }
zip = "0.5"
generated = { path = "../generated", package = "feather-generated" }
blocks = { path = "../blocks", package = "feather-blocks"}
protocol = { path = "../protocol", package = "feather-protocol" }
walkdir = "2.3.2"

[build-dependencies]
vanilla-assets = { path = "./vanilla_assets", package = "feather-vanilla-assets" }
anyhow = "1"
8 changes: 8 additions & 0 deletions feather/datapacks/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::path::PathBuf;

fn main() -> anyhow::Result<()> {
if !PathBuf::from("./minecraft").exists() {
vanilla_assets::download_vanilla_assets(&PathBuf::from("../"))?;
}
Ok(())
}
14 changes: 14 additions & 0 deletions feather/datapacks/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ impl NamespacedId {
pub fn name(&self) -> &str {
&self.name
}

pub fn from_parts(namespace: &str, name: &str) -> Result<Self, ParseError> {
let namespace = if namespace.is_empty() {
DEFAULT_NAMESPACE
} else {
namespace
};
validate_namespace(namespace)?;
validate_name(name)?;
Ok(Self {
namespace: SmartString::from(namespace),
name: SmartString::from(name),
})
}
}

/// Error returned when a namespaced ID was formatted incorrectly.
Expand Down
62 changes: 59 additions & 3 deletions feather/datapacks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,55 @@
//! This crate also downloads vanilla JARs and assets
//! at startup; see `download_vanilla_assets`.

use std::path::Path;

use ahash::AHashMap;
use id::ParseError;
use serde::Deserialize;
use smartstring::{LazyCompact, SmartString};

mod vanilla;
pub use vanilla::download_vanilla_assets;

mod id;
pub use id::NamespacedId;

mod serde_helpers;
pub(crate) use serde_helpers::*;

pub mod tag;
use tag::LoopError;
pub use tag::{TagRegistry, TagRegistryBuilder};

pub mod recipe;
pub use recipe::RecipeRegistry;

/// The default namespace for resource locations (NamespacedIds).
pub const DEFAULT_NAMESPACE: &str = "minecraft";

use thiserror::Error;
#[derive(Error, Debug)]
pub enum TagLoadError {
#[error("invalid namespaced id: {0}")]
Parse(#[from] ParseError),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("io error: {0}")]
WalkDir(#[from] walkdir::Error),
#[error("loop detected when parsing tags: {0}")]
FoundLoop(#[from] LoopError),
#[error("invalid tag link: {0} references {1}")]
InvalidLink(NamespacedId, NamespacedId),
#[error("json parsing error: {0}")]
Json(#[from] serde_json::Error),
}
#[derive(Error, Debug)]
pub enum RecipeLoadError {
#[error("invalid namespaced id: {0}")]
Parse(#[from] ParseError),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("json parsing error: {0}")]
Json(#[from] serde_json::Error),
}

/// The pack.mcmeta file at the root of a datapack.
///
/// Formatted with JSON.
Expand All @@ -33,3 +69,23 @@ pub struct Datapacks {
/// The metadata of loaded packs. Keyed by the datapack name.
_meta: AHashMap<SmartString<LazyCompact>, PackMeta>,
}
#[derive(Default)]
pub struct Datapack {
pub advancements: (),
pub loot_tables: (),
pub recipes: (),
pub structures: (),
pub tags: TagRegistry,
}

impl Datapack {
pub fn new() -> Self {
Self {
..Default::default()
}
}
pub fn from_folder(dir: &Path) -> Self {
assert!(dir.is_dir(), "not a directory");
todo!()
}
}
Loading