From df3dfe92500ef1fde9332991872e7ec8882568de Mon Sep 17 00:00:00 2001
From: Louis Capitanchik <contact@louiscap.co>
Date: Sun, 27 Aug 2023 20:48:59 +0100
Subject: [PATCH] Support external level files

---
 Cargo.lock      |  2 +-
 Cargo.toml      |  2 +-
 README.md       |  4 ++--
 src/ldtk/mod.rs | 26 ++++++++++++++++++++------
 src/pregen.rs   | 18 ++++++++++++++++++
 5 files changed, 42 insertions(+), 10 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 8c4ca69..aa21e00 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1567,7 +1567,7 @@ dependencies = [
 
 [[package]]
 name = "micro_ldtk"
-version = "0.6.1"
+version = "0.7.0"
 dependencies = [
  "anyhow",
  "bevy",
diff --git a/Cargo.toml b/Cargo.toml
index 246a66f..c46f91f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "micro_ldtk"
-version = "0.6.1"
+version = "0.7.0"
 edition = "2021"
 
 authors = [
diff --git a/README.md b/README.md
index f4c3a8a..872653a 100644
--- a/README.md
+++ b/README.md
@@ -17,7 +17,7 @@ select the schema version you need:
 
 ```toml
 [dependencies]
-micro_ldtk = { version = "0.4.1", default-features = false, features = ["ldtk_1_3_0", "autotile"] }
+micro_ldtk = { version = "0.7.0", default-features = false, features = ["ldtk_1_3_0", "autotile"] }
 ```
 
 ### Features
@@ -38,7 +38,7 @@ corresponding version of LDTK and save it again.
 
 | Feature Flag               | Uses Schema Version                                                           |
 |----------------------------|-------------------------------------------------------------------------------|
-| `ldtk_1_3_0`               | [v1.2.5](https://github.com/deepnight/ldtk/blob/v1.3.0/docs/JSON_SCHEMA.json) |
+| `ldtk_1_3_0`               | [v1.3.0](https://github.com/deepnight/ldtk/blob/v1.3.0/docs/JSON_SCHEMA.json) |
 | `ldtk_1_2_5`               | [v1.2.5](https://github.com/deepnight/ldtk/blob/v1.2.5/docs/JSON_SCHEMA.json) |
 | `ldtk_1_2_4`               | [v1.2.4](https://github.com/deepnight/ldtk/blob/v1.2.4/docs/JSON_SCHEMA.json) |
 | `ldtk_1_2_3`, `ldtk_1_2_2` | [v1.2.2](https://github.com/deepnight/ldtk/blob/v1.2.2/docs/JSON_SCHEMA.json) |
diff --git a/src/ldtk/mod.rs b/src/ldtk/mod.rs
index ffa60bb..d7e5559 100644
--- a/src/ldtk/mod.rs
+++ b/src/ldtk/mod.rs
@@ -17,6 +17,7 @@ mod data_1_3_0;
 
 use bevy::asset::{AssetLoader, AssetPath, BoxedFuture, LoadContext, LoadedAsset};
 use bevy::reflect::{TypePath, TypeUuid, Uuid};
+
 #[cfg(feature = "ldtk_1_0_0")]
 pub use data_1_0_0::*;
 #[cfg(any(feature = "ldtk_1_1_1", feature = "ldtk_1_1_0"))]
@@ -155,12 +156,23 @@ impl AssetLoader for LdtkLoader {
 		load_context: &'a mut LoadContext,
 	) -> BoxedFuture<'a, anyhow::Result<(), anyhow::Error>> {
 		Box::pin(async move {
+			log::debug!(
+				"Loading ldtk project file {}",
+				load_context.path().display()
+			);
+
 			let project = Project::from_bytes(bytes)?;
 
 			let sub_levels = project
 				.levels
 				.iter()
 				.flat_map(|level| {
+					log::debug!(
+						"Checking if level is external: {} [{}]",
+						level.identifier,
+						level.external_rel_path.is_some()
+					);
+
 					level
 						.external_rel_path
 						.as_ref()
@@ -172,12 +184,13 @@ impl AssetLoader for LdtkLoader {
 				sub_levels
 					.into_iter()
 					.flat_map(|(id, path)| {
-						load_context
-							.path()
-							.join(path)
-							.canonicalize()
-							.map(|path| AssetPath::new(path, Some(id)))
-							.ok()
+						log::debug!(
+							"Checking for file {}",
+							load_context.path().parent()?.join(&path).display()
+						);
+
+						let path = load_context.path().parent()?.join(path);
+						Some(AssetPath::new(path, Some(id)))
 					})
 					.collect(),
 			);
@@ -200,6 +213,7 @@ impl AssetLoader for LdtkLevelLoader {
 		load_context: &'a mut LoadContext,
 	) -> BoxedFuture<'a, anyhow::Result<(), anyhow::Error>> {
 		Box::pin(async move {
+			log::debug!("Loading ldtkl level file {}", load_context.path().display());
 			load_context.set_default_asset(LoadedAsset::new(Level::from_bytes(bytes)?));
 			Ok(())
 		})
diff --git a/src/pregen.rs b/src/pregen.rs
index eeae7d9..119662b 100644
--- a/src/pregen.rs
+++ b/src/pregen.rs
@@ -95,6 +95,24 @@ impl Rasterise for LdtkLevel {
 		}
 	}
 }
+
+impl LdtkLevel {
+	pub fn write_filtered_to_texture(
+		&self,
+		buffer: &mut [u8],
+		format: &TextureFormat,
+		images: &impl SuppliesImage,
+		atlas: &impl SuppliesTextureAtlas,
+		predicate: impl Fn(&LdtkLayer) -> bool,
+	) {
+		for layer in self.layers() {
+			if predicate(layer) {
+				layer.write_to_texture(buffer, format, images, atlas);
+			}
+		}
+	}
+}
+
 impl Rasterise for LdtkLayer {
 	fn write_to_texture(
 		&self,
-- 
GitLab