Skip to content
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
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ jobs:
LD_LIBRARY_PATH="$(rustc --print target-libdir)" \
cargo test --workspace --features=bevy/dynamic_linking \
--lib --bins --tests --examples \
--exclude yarnspinner_without_bevy_examples
--exclude yarnspinner_without_bevy_examples \
--no-fail-fast

test-bevy-docs:
name: Test Docs with Bevy
Expand Down Expand Up @@ -151,7 +152,7 @@ jobs:
- name: Install alsa and udev
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev libxkbcommon-dev
- name: Run cargo test for non-bevy
run: cargo test --no-default-features -p yarnspinner -p yarnspinner_without_bevy_examples
run: cargo test --no-default-features --features=serde -p yarnspinner -p yarnspinner_without_bevy_examples --no-fail-fast
- name: Run doc tests for non-bevy
run: LD_LIBRARY_PATH="$(rustc --print target-libdir)" cargo test --doc --no-default-features -p yarnspinner -p yarnspinner_without_bevy_examples

Expand Down
32 changes: 30 additions & 2 deletions Cargo.lock

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

25 changes: 10 additions & 15 deletions crates/bevy_plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,31 @@ version = "0.7.0"
edition = "2024"
repository = "https://github.com/YarnSpinnerTool/YarnSpinner-Rust"
homepage = "https://docs.yarnspinner.dev/"
keywords = ["gamedev", "dialog", "yarn", "bevy"]
categories = ["game-development", "compilers"]
authors = ["Jan Hohenheim <jan@hohenheim.ch>"]
keywords = [ "gamedev", "dialog", "yarn", "bevy" ]
categories = [ "game-development", "compilers" ]
authors = [ "Jan Hohenheim <jan@hohenheim.ch>" ]
license = "MIT OR Apache-2.0"
exclude = ["assets", "wasm"]
exclude = [ "assets", "wasm" ]
description = "Bevy plugin for Yarn Spinner for Rust, friendly tool for writing game dialogue"
readme = "../../readme.md"

[features]
default = []
audio_assets = ["bevy/bevy_audio", "bevy/vorbis"]
default = [ ]
audio_assets = [ "bevy/bevy_audio", "bevy/vorbis" ]

[dependencies]
anyhow = "1"
csv = "1"
serde = { version = "1", features = ["derive"] }
serde = { version = "1", features = [ "derive" ] }
yarnspinner_internal_shared = { path = "../internal_shared", version = "0.1.0" }
yarnspinner = { path = "../yarnspinner", features = [
"bevy",
"serde",
], version = "0.7.0" }
yarnspinner = { path = "../yarnspinner", features = [ "bevy", "serde" ], version = "0.7.0" }
sha2 = "0.10"
rand = { version = "0.9", features = ["small_rng"] }
variadics_please = "1"


[dependencies.bevy]
version = "0.18"
default-features = false
features = ["bevy_asset", "multi_threaded", "bevy_log"]
features = [ "bevy_asset", "multi_threaded", "bevy_log" ]

[dev-dependencies]
tempfile = "3"
Expand All @@ -42,7 +37,7 @@ static_assertions = "1.1.0"
[dev-dependencies.bevy]
version = "0.18"
default-features = false
features = ["bevy_core_pipeline", "bevy_audio"]
features = [ "bevy_core_pipeline", "bevy_audio" ]

[target.'cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))'.dependencies]
glob = "0.3.1"
83 changes: 1 addition & 82 deletions crates/bevy_plugin/src/dialogue_runner/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::line_provider::SharedTextProvider;
use crate::prelude::*;
use bevy::platform::collections::HashMap;
use bevy::prelude::*;
use rand::{Rng, SeedableRng, rngs::SmallRng};
use std::any::{Any, TypeId};
use std::fmt::Debug;

Expand Down Expand Up @@ -32,7 +31,7 @@ impl DialogueRunnerBuilder {
yarn_project,
)),
asset_providers: HashMap::default(),
library: create_extended_standard_library(),
library: YarnLibrary::standard_library(),
commands: YarnCommands::builtin_commands(commands),
compilation: yarn_project.compilation().clone(),
localizations: yarn_project.localizations().cloned(),
Expand Down Expand Up @@ -119,83 +118,3 @@ impl DialogueRunnerBuilder {
Ok(dialogue_runner)
}
}

fn create_extended_standard_library() -> YarnLibrary {
let mut library = YarnLibrary::standard_library();
library
.add_function("random", || SmallRng::from_os_rng().random_range(0.0..1.0))
.add_function("random_range", |min: f32, max: f32| {
if let Some(min) = min.as_int()
&& let Some(max_inclusive) = max.as_int()
{
return SmallRng::from_os_rng().random_range(min..=max_inclusive) as f32;
}
SmallRng::from_os_rng().random_range(min..max)
})
.add_function("dice", |sides: u32| {
if sides == 0 {
return 1;
}
SmallRng::from_os_rng().random_range(1..=sides)
})
.add_function("round", |num: f32| num.round() as i32)
.add_function("round_places", |num: f32, places: u32| {
num.round_places(places)
})
.add_function("floor", |num: f32| num.floor() as i32)
.add_function("ceil", |num: f32| num.ceil() as i32)
.add_function("inc", |num: f32| {
if let Some(num) = num.as_int() {
num + 1
} else {
num.ceil() as i32
}
})
.add_function("dec", |num: f32| {
if let Some(num) = num.as_int() {
num - 1
} else {
num.floor() as i32
}
})
.add_function("decimal", |num: f32| num.fract())
.add_function("int", |num: f32| num.trunc() as i32);
library
}

trait FloatExt: Copy {
fn as_int(self) -> Option<i32>;
fn round_places(self, places: u32) -> Self;
}

impl FloatExt for f32 {
fn as_int(self) -> Option<i32> {
(self.fract() <= f32::EPSILON).then_some(self as i32)
}

fn round_places(self, places: u32) -> Self {
let factor = 10_u32.pow(places) as f32;
(self * factor).round() / factor
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn rounds_places() {
for (num, places, expected) in [
(1.0, 0, 1.0),
(1.2, 1, 1.2),
(0.4, 0, 0.0),
(43.132, 0, 43.0),
(1.1, 2, 1.1),
(123.123, 3, 123.123),
(-10.3, 1, -10.3),
(-11.99, 1, -12.0),
] {
assert_eq!(expected, num.round_places(places));
}
}
}
6 changes: 3 additions & 3 deletions crates/bevy_plugin/src/localization/line_id_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn handle_yarn_file_events(
yarn_file.string_table.clone(),
));

let Some(source_with_added_ids) = add_tags_to_lines(yarn_file)? else {
let Some((source_with_added_ids, _)) = add_tags_to_lines(yarn_file)? else {
if matches!(event, AssetEvent::LoadedWithDependencies { .. }) {
continue;
}
Expand Down Expand Up @@ -155,12 +155,12 @@ fn handle_yarn_file_events(
}

/// Adapted from <https://github.com/YarnSpinnerTool/YarnSpinner-Console/blob/main/src/YarnSpinner.Console/Commands/TagCommand.cs#L11>
fn add_tags_to_lines(yarn_file: &YarnFile) -> YarnCompilerResult<Option<String>> {
fn add_tags_to_lines(yarn_file: &YarnFile) -> YarnCompilerResult<Option<(String, Vec<LineId>)>> {
let existing_tags = yarn_file
.string_table
.iter()
.filter(|(_, string_info)| !string_info.is_implicit_tag)
.map(|(key, _)| key.clone())
.collect();
YarnCompiler::add_tags_to_lines(yarn_file.file.source.clone(), existing_tags)
YarnCompiler::tag_lines(yarn_file.file.source.clone(), existing_tags)
}
18 changes: 11 additions & 7 deletions crates/compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ version = "0.7.0"
edition = "2024"
repository = "https://github.com/YarnSpinnerTool/YarnSpinner-Rust"
homepage = "https://docs.yarnspinner.dev/"
categories = ["game-development", "compilers"]
authors = ["Jan Hohenheim <jan@hohenheim.ch>"]
categories = [ "game-development", "compilers" ]
authors = [ "Jan Hohenheim <jan@hohenheim.ch>" ]
license = "MIT OR Apache-2.0"
description = "Compiler for Yarn Spinner for Rust, the friendly tool for writing game dialogue"

[features]
default = []
serde = ["dep:serde", "bevy?/serialize", "yarnspinner_core/serde"]
bevy = ["dep:bevy", "yarnspinner_core/bevy"]
default = [ ]
serde = [ "dep:serde", "dep:serde_json", "bevy?/serialize", "yarnspinner_core/serde" ]
bevy = [ "dep:bevy", "yarnspinner_core/bevy" ]

[dependencies]
antlr-rust = "=0.3.0-beta"
Expand All @@ -21,9 +21,13 @@ regex = "1"
yarnspinner_internal_shared = { path = "../internal_shared", version = "0.1.0" }
yarnspinner_core = { path = "../core", version = "0.7.0" }
annotate-snippets = "0.10"
serde = { version = "1", features = ["derive"], optional = true }
serde = { version = "1", features = [ "derive" ], optional = true }
serde_json = { version = "1", optional = true }
bevy = { version = "0.18", default-features = false, optional = true }
rand = { version = "0.9", features = ["small_rng"] }
rand = { version = "0.9", features = [ "small_rng" ] }
crc32fast = "1.5"
globset = "0.4"
walkdir = "2.5"

[target.'cfg(target_arch = "wasm32")'.dependencies]
instant = { version = "0.1.12", features = [
Expand Down
7 changes: 4 additions & 3 deletions crates/compiler/src/compilation_steps.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod add_error_for_empty_nodes;
mod add_initial_value_registrations;
mod add_tracking_declarations;
mod check_types;
Expand All @@ -14,9 +15,9 @@ mod resolve_deferred_type_diagnostic;
mod validate_unique_node_names;

pub(crate) use self::{
add_initial_value_registrations::*, add_tracking_declarations::*, check_types::*,
clean_up_diagnostics::*, create_declarations_for_tracking_nodes::*, early_breaks::*,
find_tracking_nodes::*, generate_code::*, get_declarations::*, parse_files::*,
add_error_for_empty_nodes::*, add_initial_value_registrations::*, add_tracking_declarations::*,
check_types::*, clean_up_diagnostics::*, create_declarations_for_tracking_nodes::*,
early_breaks::*, find_tracking_nodes::*, generate_code::*, get_declarations::*, parse_files::*,
register_initial_variables::*, register_strings::*, resolve_deferred_type_diagnostic::*,
validate_unique_node_names::*,
};
Loading