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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//! Contains the code for the loading of all assets and [ResourceId]
//!
//! In order for bottomless-pit to run on both desktop and web enviorments file reading
//! has to be done asynchronously. When you call [Texture::new](crate::texture::Texture::new),
//! [Shader::new](crate::shader::Shader::new), [Font::new](crate::text::Font::new),
//! or [Engine::create_resource](crate::engine_handle::Engine::create_resource) you are given a
//! handle to the resource and not the resoruce directly. Since resources are loaded asynchronously it will not
//! be imeaditly availble. In order to have the resource be available to use as soon as possible the engine halts
//! while waiting for the resource. [Game::update](crate::Game::update) will not be called and
//! [Game::render](crate::Game::render) will not be called either.
//! ```rust
//! fn main() {
//!     let engine = mut EngineBuilder::new().build();
//!     
//!     let texture: ResourceId<Texture> = Texture::new(&mut engine, "path.png");
//!     // anything loaded in main will be ready on the first frame of the game
//!     let material = MaterialBuilder::new().add_texture(texture).build();
//!
//!     let game = YourGame {
//!         textured_material: material,
//!     }
//! }
//!
//! struct YourGame {
//!     textured_material: Material,
//! }
//!
//! impl Game for YourGame {
//!     fn update(&mut self, engine: &mut Engine) {
//!         if engine.is_key_pressed(Key::A) {
//!             let texutre = Texture::new(engine, "path2.png");
//!             // render and update wont be called until after the texture finishes loading
//!         }
//!     }
//!
//!     fn render<'pass, 'others>(&'others mut self, renderer: RenderInformation<'pass, 'others>) where 'others: 'pass {
//!         // do stuff
//!     }
//! }
//! ```
//! Because of this stalling behavior it is recomended you do all your loading of assests in as large of chunks as possible.
use std::collections::HashMap;
use std::marker::PhantomData;
use std::num::NonZeroU64;
use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicU64;

use crate::engine_handle::{BpEvent, Engine};
use crate::io::{self, ReadError};
use crate::shader::Shader;
use crate::text::Font;
use crate::texture::Texture;

#[derive(Debug)]
pub(crate) struct Resource {
    path: PathBuf,
    pub(crate) data: Vec<u8>,
    pub(crate) id: NonZeroU64,
    pub(crate) resource_type: ResourceType,
}

#[derive(Debug)]
pub(crate) struct ResourceError {
    pub(crate) error: ReadError,
    path: PathBuf,
    pub(crate) id: NonZeroU64,
    pub(crate) resource_type: ResourceType,
}

impl Resource {
    pub fn from_result(
        result: Result<Vec<u8>, ReadError>,
        path: PathBuf,
        id: NonZeroU64,
        resource_type: ResourceType,
    ) -> Result<Self, ResourceError> {
        match result {
            Ok(data) => Ok(Self {
                path,
                data,
                id,
                resource_type,
            }),
            Err(e) => Err(ResourceError {
                error: e,
                path,
                id,
                resource_type,
            }),
        }
    }
}

pub(crate) fn compare_resources(
    left: &InProgressResource,
    right: &Result<Resource, ResourceError>,
) -> bool {
    match right {
        Ok(right) => {
            left.id == right.id
                && left.resource_type == right.resource_type
                && left.path == right.path
        }
        Err(right) => {
            left.id == right.id
                && left.resource_type == right.resource_type
                && left.path == right.path
        }
    }
}

#[derive(Debug)]
pub(crate) struct InProgressResource {
    path: PathBuf,
    id: NonZeroU64,
    resource_type: ResourceType,
}

impl InProgressResource {
    pub fn new(path: &Path, id: NonZeroU64, resource_type: ResourceType) -> Self {
        Self {
            path: path.to_owned(),
            id,
            resource_type,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ResourceType {
    Image,
    Shader(bool),
    Bytes,
    Font,
}

pub(crate) fn generate_id<T>() -> ResourceId<T> {
    static NEXT_ID: AtomicU64 = AtomicU64::new(1);
    let id = NEXT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    ResourceId(NonZeroU64::new(id).unwrap(), PhantomData::<T>)
}

pub(crate) fn start_load<P: AsRef<Path>>(
    engine: &Engine,
    path: P,
    ip_resource: &InProgressResource,
) {
    let path = path.as_ref().to_owned();
    let id = ip_resource.id;
    let resource_type = ip_resource.resource_type;
    let event_loop_proxy = engine.get_proxy();
    #[cfg(target_arch = "wasm32")]
    {
        use wasm_bindgen_futures::spawn_local;
        spawn_local(async move {
            let result = io::read(&path).await;
            let resource = Resource::from_result(result, path, id, resource_type);
            event_loop_proxy
                .send_event(BpEvent::ResourceLoaded(resource))
                .unwrap();
        });
    }

    #[cfg(not(target_arch = "wasm32"))]
    {
        let pool = engine.thread_pool();
        pool.spawn_ok(async move {
            let result = io::read(&path).await;
            let resource = Resource::from_result(result, path, id, resource_type);
            event_loop_proxy
                .send_event(BpEvent::ResourceLoaded(resource))
                .unwrap();
        });
    }
}

/// An Id for a specific type of resource used interally in the engine
#[derive(PartialOrd, Ord)]
pub struct ResourceId<T>(NonZeroU64, std::marker::PhantomData<T>);

impl<T> ResourceId<T> {
    pub(crate) fn from_number(number: NonZeroU64) -> Self {
        Self(number, PhantomData)
    }

    pub(crate) fn get_id(&self) -> NonZeroU64 {
        self.0
    }
}

impl<T> Clone for ResourceId<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for ResourceId<T> {}

impl<T> std::fmt::Debug for ResourceId<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("ID").field(&self.0).finish()
    }
}

impl<T> PartialEq for ResourceId<T> {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl<T> Eq for ResourceId<T> {}

impl<T> std::hash::Hash for ResourceId<T> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.0.hash(state)
    }
}

type ResourceMap<T> = HashMap<ResourceId<T>, T>;

pub(crate) struct ResourceManager {
    btye_resources: ResourceMap<Vec<u8>>,
    bindgroup_resources: ResourceMap<Texture>,
    pipeline_resource: ResourceMap<Shader>,
    fonts: ResourceMap<Font>,
}

impl ResourceManager {
    pub fn new() -> Self {
        Self {
            btye_resources: HashMap::new(),
            bindgroup_resources: HashMap::new(),
            pipeline_resource: HashMap::new(),
            fonts: HashMap::new(),
        }
    }

    pub fn insert_bytes(&mut self, key: ResourceId<Vec<u8>>, data: Vec<u8>) {
        self.btye_resources.insert(key, data);
    }

    pub fn insert_texture(&mut self, key: ResourceId<Texture>, data: Texture) {
        self.bindgroup_resources.insert(key, data);
    }

    pub fn insert_pipeline(&mut self, key: ResourceId<Shader>, data: Shader) {
        self.pipeline_resource.insert(key, data);
    }

    pub fn insert_font(&mut self, key: ResourceId<Font>, data: Font) {
        self.fonts.insert(key, data);
    }

    pub fn get_byte_resource(&self, key: &ResourceId<Vec<u8>>) -> Option<&Vec<u8>> {
        self.btye_resources.get(key)
    }

    pub fn get_texture(&self, key: &ResourceId<Texture>) -> Option<&Texture> {
        self.bindgroup_resources.get(key)
    }

    pub fn get_pipeline(&self, key: &ResourceId<Shader>) -> Option<&Shader> {
        self.pipeline_resource.get(key)
    }

    pub fn get_font(&self, key: &ResourceId<Font>) -> Option<&Font> {
        self.fonts.get(key)
    }
}