-
Notifications
You must be signed in to change notification settings - Fork 10
/
asset.rs
186 lines (156 loc) · 6.08 KB
/
asset.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! This module contains all [Asset]s definition.
use std::io::{Cursor, Error as IoError, ErrorKind, Read};
#[cfg(feature = "user_properties")]
use std::ops::Deref;
use std::path::Path;
use std::sync::Arc;
#[cfg(feature = "user_properties")]
use bevy::reflect::TypeRegistryArc;
#[cfg(feature = "user_properties")]
use crate::properties::load::DeserializedMapProperties;
use bevy::{
asset::{io::Reader, AssetLoader, AssetPath, AsyncReadExt, LoadContext},
prelude::*,
utils::HashMap,
};
use bevy_ecs_tilemap::prelude::*;
/// Tiled map `Asset`.
///
/// `Asset` holding Tiled map informations.
#[derive(TypePath, Asset)]
pub struct TiledMap {
pub map: tiled::Map,
pub tilemap_textures: HashMap<usize, TilemapTexture>,
#[cfg(feature = "user_properties")]
pub(crate) properties: DeserializedMapProperties,
// The offset into the tileset_images for each tile id within each tileset.
#[cfg(not(feature = "atlas"))]
pub tile_image_offsets: HashMap<(usize, tiled::TileId), u32>,
}
struct BytesResourceReader<'a, 'b> {
bytes: Arc<[u8]>,
context: &'a mut LoadContext<'b>,
}
impl<'a, 'b> BytesResourceReader<'a, 'b> {
fn new(bytes: &'a [u8], context: &'a mut LoadContext<'b>) -> Self {
Self {
bytes: Arc::from(bytes),
context,
}
}
}
impl<'a> tiled::ResourceReader for BytesResourceReader<'a, '_> {
type Resource = Box<dyn Read + 'a>;
type Error = IoError;
fn read_from(&mut self, path: &Path) -> std::result::Result<Self::Resource, Self::Error> {
if let Some(extension) = path.extension() {
if extension == "tsx" {
let future = self.context.read_asset_bytes(path.to_path_buf());
let data = futures_lite::future::block_on(future)
.map_err(|err| IoError::new(ErrorKind::NotFound, err))?;
return Ok(Box::new(Cursor::new(data)));
}
}
Ok(Box::new(Cursor::new(self.bytes.clone())))
}
}
pub(crate) struct TiledLoader {
#[cfg(feature = "user_properties")]
pub registry: TypeRegistryArc,
}
impl FromWorld for TiledLoader {
fn from_world(_world: &mut World) -> Self {
Self {
#[cfg(feature = "user_properties")]
registry: _world.resource::<AppTypeRegistry>().0.clone(),
}
}
}
/// [TiledMap] loading error.
#[derive(Debug, thiserror::Error)]
pub enum TiledAssetLoaderError {
/// An [IO](std::io) Error
#[error("Could not load Tiled file: {0}")]
Io(#[from] std::io::Error),
}
impl AssetLoader for TiledLoader {
type Asset = TiledMap;
type Settings = ();
type Error = TiledAssetLoaderError;
async fn load<'a>(
&'a self,
reader: &'a mut Reader<'_>,
_settings: &'a Self::Settings,
load_context: &'a mut LoadContext<'_>,
) -> Result<Self::Asset, Self::Error> {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
log::info!("Start loading map '{}'", load_context.path().display());
let map_path = load_context.path().to_path_buf();
let map = {
// Allow the loader to also load tileset images.
let mut loader = tiled::Loader::with_cache_and_reader(
tiled::DefaultResourceCache::new(),
BytesResourceReader::new(&bytes, load_context),
);
// Load the map and all tiles.
loader.load_tmx_map(&map_path).map_err(|e| {
std::io::Error::new(ErrorKind::Other, format!("Could not load TMX map: {e}"))
})?
};
let mut tilemap_textures = HashMap::default();
#[cfg(not(feature = "atlas"))]
let mut tile_image_offsets = HashMap::default();
for (tileset_index, tileset) in map.tilesets().iter().enumerate() {
let tilemap_texture = match &tileset.image {
None => {
#[cfg(feature = "atlas")]
{
log::info!("Skipping image collection tileset '{}' which is incompatible with atlas feature", tileset.name);
continue;
}
#[cfg(not(feature = "atlas"))]
{
let mut tile_images: Vec<Handle<Image>> = Vec::new();
for (tile_id, tile) in tileset.tiles() {
if let Some(img) = &tile.image {
let asset_path = AssetPath::from(img.source.clone());
log::debug!("Loading tile image from {asset_path:?} as image ({tileset_index}, {tile_id})");
let texture: Handle<Image> = load_context.load(asset_path.clone());
tile_image_offsets
.insert((tileset_index, tile_id), tile_images.len() as u32);
tile_images.push(texture.clone());
}
}
TilemapTexture::Vector(tile_images)
}
}
Some(img) => {
let asset_path = AssetPath::from(img.source.clone());
let texture: Handle<Image> = load_context.load(asset_path.clone());
TilemapTexture::Single(texture.clone())
}
};
tilemap_textures.insert(tileset_index, tilemap_texture);
}
#[cfg(feature = "user_properties")]
let properties =
DeserializedMapProperties::load(&map, self.registry.read().deref(), load_context);
#[cfg(feature = "user_properties")]
trace!(?properties, "user properties");
let asset_map = TiledMap {
map,
tilemap_textures,
#[cfg(feature = "user_properties")]
properties,
#[cfg(not(feature = "atlas"))]
tile_image_offsets,
};
log::info!("Loaded map '{}'", load_context.path().display());
Ok(asset_map)
}
fn extensions(&self) -> &[&str] {
static EXTENSIONS: &[&str] = &["tmx"];
EXTENSIONS
}
}