From d26f392a31666bc71ee865381b7d62d747831e71 Mon Sep 17 00:00:00 2001 From: Rex Magana Date: Sat, 4 May 2024 08:29:14 -0700 Subject: [PATCH 1/3] Make dialogue clone --- .../text_provider/shared_text_provider.rs | 4 ++++ .../text_provider/strings_file_text_provider.rs | 4 ++++ crates/runtime/src/dialogue.rs | 2 +- crates/runtime/src/text_provider.rs | 14 ++++++++++++++ crates/runtime/src/virtual_machine.rs | 2 +- .../yarnspinner/tests/test_base/text_provider.rs | 4 ++++ 6 files changed, 28 insertions(+), 2 deletions(-) diff --git a/crates/bevy_plugin/src/line_provider/text_provider/shared_text_provider.rs b/crates/bevy_plugin/src/line_provider/text_provider/shared_text_provider.rs index cdd92c23..2e169c56 100644 --- a/crates/bevy_plugin/src/line_provider/text_provider/shared_text_provider.rs +++ b/crates/bevy_plugin/src/line_provider/text_provider/shared_text_provider.rs @@ -46,6 +46,10 @@ impl TextProvider for SharedTextProvider { } impl UnderlyingTextProvider for SharedTextProvider { + fn clone_shallow(&self) -> Box { + Box::new(self.clone()) + } + fn accept_line_hints(&mut self, line_ids: &[LineId]) { self.0.write().unwrap().accept_line_hints(line_ids) } diff --git a/crates/bevy_plugin/src/line_provider/text_provider/strings_file_text_provider.rs b/crates/bevy_plugin/src/line_provider/text_provider/strings_file_text_provider.rs index f4c8783d..920c0ee0 100644 --- a/crates/bevy_plugin/src/line_provider/text_provider/strings_file_text_provider.rs +++ b/crates/bevy_plugin/src/line_provider/text_provider/strings_file_text_provider.rs @@ -41,6 +41,10 @@ impl Debug for StringsFileTextProvider { } impl UnderlyingTextProvider for StringsFileTextProvider { + fn clone_shallow(&self) -> Box { + Box::new(self.clone()) + } + fn accept_line_hints(&mut self, _line_ids: &[LineId]) { // no-op } diff --git a/crates/runtime/src/dialogue.rs b/crates/runtime/src/dialogue.rs index 4e9d94bd..7af72b3f 100644 --- a/crates/runtime/src/dialogue.rs +++ b/crates/runtime/src/dialogue.rs @@ -11,7 +11,7 @@ use yarnspinner_core::prelude::*; /// Co-ordinates the execution of Yarn programs. /// /// The main functions of interest are [`Dialogue::continue_`] and [`Dialogue::set_selected_option`]. -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Dialogue { vm: VirtualMachine, language_code: Option, diff --git a/crates/runtime/src/text_provider.rs b/crates/runtime/src/text_provider.rs index af64307f..f014d7f5 100644 --- a/crates/runtime/src/text_provider.rs +++ b/crates/runtime/src/text_provider.rs @@ -13,6 +13,10 @@ use yarnspinner_core::prelude::*; /// /// By injecting this, we don't need to expose `Dialogue.ExpandSubstitutions` and `Dialogue.ParseMarkup`, since we can apply them internally. pub trait TextProvider: Debug + Send + Sync { + /// Creates a shallow clone of this text provider, i.e. a clone that + /// shares the same underlying storage and will thus be perfectly in sync + /// with the original instance. + fn clone_shallow(&self) -> Box; /// Passes the [`LineId`]s that this [`TextProvider`] should soon provide text for. These are the [`LineId`]s that are contained in the current node and are not required to be actually reached. fn accept_line_hints(&mut self, line_ids: &[LineId]); /// Returns the text for the given [`LineId`]. Will only be called if [`TextProvider::are_lines_available`] returns `true`. @@ -31,6 +35,12 @@ pub trait TextProvider: Debug + Send + Sync { fn as_any_mut(&mut self) -> &mut dyn Any; } +impl Clone for Box { + fn clone(&self) -> Self { + self.clone_shallow() + } +} + #[allow(missing_docs)] pub type StringTable = HashMap; @@ -73,6 +83,10 @@ impl StringTableTextProvider { } impl TextProvider for StringTableTextProvider { + fn clone_shallow(&self) -> Box { + Box::new(self.clone()) + } + fn accept_line_hints(&mut self, _line_ids: &[LineId]) { // no-op } diff --git a/crates/runtime/src/virtual_machine.rs b/crates/runtime/src/virtual_machine.rs index 1d823c32..5a041871 100644 --- a/crates/runtime/src/virtual_machine.rs +++ b/crates/runtime/src/virtual_machine.rs @@ -15,7 +15,7 @@ use yarnspinner_core::prelude::*; mod execution_state; mod state; -#[derive(Debug)] +#[derive(Debug, Clone)] pub(crate) struct VirtualMachine { pub(crate) library: Library, pub(crate) program: Option, diff --git a/crates/yarnspinner/tests/test_base/text_provider.rs b/crates/yarnspinner/tests/test_base/text_provider.rs index dabc8b18..c2dcb3ff 100644 --- a/crates/yarnspinner/tests/test_base/text_provider.rs +++ b/crates/yarnspinner/tests/test_base/text_provider.rs @@ -22,6 +22,10 @@ impl SharedTextProvider { } impl TextProvider for SharedTextProvider { + fn clone_shallow(&self) -> Box { + Box::new(self.clone()) + } + fn accept_line_hints(&mut self, line_ids: &[LineId]) { self.0.write().unwrap().accept_line_hints(line_ids); } From eeec4830cabd1f671b63f5561753c10e84a8def2 Mon Sep 17 00:00:00 2001 From: Rex Magana Date: Sat, 4 May 2024 09:09:20 -0700 Subject: [PATCH 2/3] "storage" -> "provider" --- crates/runtime/src/text_provider.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/runtime/src/text_provider.rs b/crates/runtime/src/text_provider.rs index f014d7f5..78cc476a 100644 --- a/crates/runtime/src/text_provider.rs +++ b/crates/runtime/src/text_provider.rs @@ -14,7 +14,7 @@ use yarnspinner_core::prelude::*; /// By injecting this, we don't need to expose `Dialogue.ExpandSubstitutions` and `Dialogue.ParseMarkup`, since we can apply them internally. pub trait TextProvider: Debug + Send + Sync { /// Creates a shallow clone of this text provider, i.e. a clone that - /// shares the same underlying storage and will thus be perfectly in sync + /// shares the same underlying provider and will thus be perfectly in sync /// with the original instance. fn clone_shallow(&self) -> Box; /// Passes the [`LineId`]s that this [`TextProvider`] should soon provide text for. These are the [`LineId`]s that are contained in the current node and are not required to be actually reached. From c16e8ddef658169d8f6e0eb38b175bee3d63acd5 Mon Sep 17 00:00:00 2001 From: Rex Magana Date: Fri, 17 May 2024 07:34:27 -0700 Subject: [PATCH 3/3] Trigger CI