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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//! A module for untyped resources. See [`UntypedResource`] docs for more info.

#![allow(missing_docs)]

use crate::{
    core::{
        parking_lot::Mutex, reflect::prelude::*, uuid::Uuid, visitor::prelude::*, TypeUuidProvider,
    },
    manager::ResourceManager,
    state::{LoadError, ResourceState},
    Resource, ResourceData, ResourceLoadError, TypedResourceData, CURVE_RESOURCE_UUID,
    MODEL_RESOURCE_UUID, SHADER_RESOURCE_UUID, SOUND_BUFFER_RESOURCE_UUID, TEXTURE_RESOURCE_UUID,
};
use fyrox_core::curve::Curve;
use fyrox_core::visitor::RegionGuard;
use std::ffi::OsStr;
use std::fmt::Display;
use std::path::Path;
use std::{
    fmt::{Debug, Formatter},
    future::Future,
    hash::{Hash, Hasher},
    marker::PhantomData,
    path::PathBuf,
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
};

// Heuristic function to guess resource uuid based on inner content of a resource.
fn guess_uuid(region: &mut RegionGuard) -> Uuid {
    assert!(region.is_reading());

    let mut region = region.enter_region("Details").unwrap();

    let mut mip_count = 0u32;
    if mip_count.visit("MipCount", &mut region).is_ok() {
        return TEXTURE_RESOURCE_UUID;
    }

    let mut curve = Curve::default();
    if curve.visit("Curve", &mut region).is_ok() {
        return CURVE_RESOURCE_UUID;
    }

    let mut id = 0u32;
    if id.visit("Id", &mut region).is_ok() {
        return SOUND_BUFFER_RESOURCE_UUID;
    }

    let mut path = PathBuf::new();
    if path.visit("Path", &mut region).is_ok() {
        let ext = path.extension().unwrap_or_default().to_ascii_lowercase();
        if ext == OsStr::new("rgs") || ext == OsStr::new("fbx") {
            return MODEL_RESOURCE_UUID;
        } else if ext == OsStr::new("shader")
            || path == OsStr::new("Standard")
            || path == OsStr::new("StandardTwoSides")
            || path == OsStr::new("StandardTerrain")
        {
            return SHADER_RESOURCE_UUID;
        }
    }

    Default::default()
}

#[derive(Default, Reflect, Debug, Visit, Clone, PartialEq, Hash)]
pub enum ResourceKind {
    #[default]
    Embedded,
    External(PathBuf),
}

impl From<Option<PathBuf>> for ResourceKind {
    fn from(value: Option<PathBuf>) -> Self {
        match value {
            None => Self::Embedded,
            Some(path) => Self::External(path),
        }
    }
}

impl From<PathBuf> for ResourceKind {
    fn from(value: PathBuf) -> Self {
        Self::External(value)
    }
}

impl<'a> From<&'a str> for ResourceKind {
    fn from(value: &'a str) -> Self {
        Self::External(value.into())
    }
}

impl ResourceKind {
    #[inline]
    pub fn make_external(&mut self, path: PathBuf) {
        *self = ResourceKind::External(path);
    }

    #[inline]
    pub fn make_embedded(&mut self) {
        *self = ResourceKind::Embedded;
    }

    #[inline]
    pub fn is_embedded(&self) -> bool {
        matches!(self, Self::Embedded)
    }

    #[inline]
    pub fn is_external(&self) -> bool {
        !self.is_embedded()
    }

    #[inline]
    pub fn path(&self) -> Option<&Path> {
        match self {
            ResourceKind::Embedded => None,
            ResourceKind::External(path) => Some(path),
        }
    }

    #[inline]
    pub fn path_owned(&self) -> Option<PathBuf> {
        self.path().map(|p| p.to_path_buf())
    }

    #[inline]
    pub fn into_path(self) -> Option<PathBuf> {
        match self {
            ResourceKind::Embedded => None,
            ResourceKind::External(path) => Some(path),
        }
    }
}

impl Display for ResourceKind {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ResourceKind::Embedded => {
                write!(f, "Embedded")
            }
            ResourceKind::External(path) => {
                write!(f, "External ({})", path.display())
            }
        }
    }
}

#[derive(Reflect, Debug)]
pub struct ResourceHeader {
    pub type_uuid: Uuid,
    pub kind: ResourceKind,
    pub state: ResourceState,
}

impl Visit for ResourceHeader {
    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
        let mut region = visitor.enter_region(name)?;

        if region.is_reading() {
            let mut id: u32 = 0;

            if id.visit("Id", &mut region).is_ok() {
                // Reading old version, convert it to the new.

                let mut type_uuid = Uuid::default();
                if type_uuid.visit("TypeUuid", &mut region).is_err() {
                    // We might be reading the old version, try to guess an actual type uuid by
                    // the inner content of the resource data.
                    type_uuid = guess_uuid(&mut region);
                };

                // We're interested only in embedded resources.
                if id == 2 {
                    let resource_manager = region.blackboard.get::<ResourceManager>().expect(
                        "Resource data constructor container must be \
                provided when serializing resources!",
                    );
                    let resource_manager_state = resource_manager.state();

                    if let Some(mut instance) = resource_manager_state
                        .constructors_container
                        .try_create(&type_uuid)
                    {
                        drop(resource_manager_state);

                        if let Ok(mut details_region) = region.enter_region("Details") {
                            if type_uuid == SOUND_BUFFER_RESOURCE_UUID {
                                let mut sound_region = details_region.enter_region("0")?;
                                let mut path = PathBuf::new();
                                path.visit("Path", &mut sound_region).unwrap();
                                self.kind.make_external(path);
                            } else {
                                let mut path = PathBuf::new();
                                path.visit("Path", &mut details_region).unwrap();
                                self.kind.make_external(path);
                            }
                        }

                        instance.visit("Details", &mut region)?;

                        self.state = ResourceState::Ok(instance);

                        return Ok(());
                    } else {
                        return Err(VisitError::User(format!(
                            "There's no constructor registered for type {type_uuid}!"
                        )));
                    }
                } else {
                    self.state = ResourceState::LoadError {
                        error: LoadError::new("Old resource"),
                    };
                }

                return Ok(());
            }
        }

        self.kind.visit("Kind", &mut region)?;
        self.type_uuid.visit("TypeUuid", &mut region)?;

        if self.kind == ResourceKind::Embedded {
            self.state.visit("State", &mut region)?;
        }

        Ok(())
    }
}

/// Untyped resource is a universal way of storing arbitrary resource types. Internally it wraps
/// [`ResourceState`] in a `Arc<Mutex<>` so the untyped resource becomes shareable. In most of the
/// cases you don't need to deal with untyped resources, use typed [`Resource`] wrapper instead.
/// Untyped resource could be useful in cases when you need to collect a set resources of different
/// types in a single collection and do something with them.
///
/// ## Default state
///
/// Default state of every untyped resource is [`ResourceState::LoadError`] with a warning message,
/// that the resource is in default state. This is a trade-off to prevent wrapping internals into
/// `Option`, that in some cases could lead to convoluted code with lots of `unwrap`s and state
/// assumptions.
#[derive(Clone, Reflect)]
pub struct UntypedResource(pub Arc<Mutex<ResourceHeader>>);

impl Visit for UntypedResource {
    fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
        self.0.visit(name, visitor)?;

        // Try to restore the shallow handle on deserialization for external resources.
        if visitor.is_reading() && !self.is_embedded() {
            let resource_manager = visitor
                .blackboard
                .get::<ResourceManager>()
                .expect("Resource manager must be available when deserializing resources!");

            let path = self.kind().path_owned().unwrap();
            self.0 = resource_manager.request_untyped(path).0;
        }

        Ok(())
    }
}

impl Default for UntypedResource {
    fn default() -> Self {
        Self(Arc::new(Mutex::new(ResourceHeader {
            kind: Default::default(),
            type_uuid: Default::default(),
            state: ResourceState::new_load_error(LoadError::new(
                "Default resource state of unknown type.",
            )),
        })))
    }
}

impl Debug for UntypedResource {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Resource")
    }
}

impl PartialEq for UntypedResource {
    fn eq(&self, other: &Self) -> bool {
        std::ptr::eq(&*self.0, &*other.0)
    }
}

impl Eq for UntypedResource {}

impl Hash for UntypedResource {
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write_u64(&*self.0 as *const _ as u64)
    }
}

impl UntypedResource {
    /// Creates new untyped resource in pending state using the given path and type uuid.
    pub fn new_pending(kind: ResourceKind, type_uuid: Uuid) -> Self {
        Self(Arc::new(Mutex::new(ResourceHeader {
            kind,
            type_uuid,
            state: ResourceState::new_pending(),
        })))
    }

    /// Creates new untyped resource in ok (fully loaded) state using the given data of any type, that
    /// implements [`ResourceData`] trait.
    pub fn new_ok<T>(kind: ResourceKind, data: T) -> Self
    where
        T: ResourceData,
    {
        Self(Arc::new(Mutex::new(ResourceHeader {
            kind,
            type_uuid: data.type_uuid(),
            state: ResourceState::new_ok(data),
        })))
    }

    /// Creates new untyped resource in error state.
    pub fn new_load_error(kind: ResourceKind, error: LoadError, type_uuid: Uuid) -> Self {
        Self(Arc::new(Mutex::new(ResourceHeader {
            kind,
            type_uuid,
            state: ResourceState::new_load_error(error),
        })))
    }

    /// Returns actual unique type id of underlying resource data.
    pub fn type_uuid(&self) -> Uuid {
        self.0.lock().type_uuid
    }

    /// Returns true if the resource is still loading.
    pub fn is_loading(&self) -> bool {
        matches!(self.0.lock().state, ResourceState::Pending { .. })
    }

    /// Returns true if the resource is procedural (its data is generated at runtime, not stored in an external
    /// file).
    pub fn is_embedded(&self) -> bool {
        self.0.lock().kind.is_embedded()
    }

    /// Returns exact amount of users of the resource.
    #[inline]
    pub fn use_count(&self) -> usize {
        Arc::strong_count(&self.0)
    }

    /// Returns a pointer as numeric value which can be used as a hash.
    #[inline]
    pub fn key(&self) -> usize {
        (&*self.0 as *const _) as usize
    }

    /// Returns path of the untyped resource.
    pub fn kind(&self) -> ResourceKind {
        self.0.lock().kind.clone()
    }

    /// Set a new path for the untyped resource.
    pub fn set_kind(&self, new_kind: ResourceKind) {
        self.0.lock().kind = new_kind;
    }

    /// Tries to cast untyped resource to a particular type.
    pub fn try_cast<T>(&self) -> Option<Resource<T>>
    where
        T: TypedResourceData,
    {
        if self.type_uuid() == <T as TypeUuidProvider>::type_uuid() {
            Some(Resource {
                untyped: self.clone(),
                phantom: PhantomData::<T>,
            })
        } else {
            None
        }
    }

    /// Changes ResourceState::Pending state to ResourceState::Ok(data) with given `data`.
    /// Additionally it wakes all futures.
    #[inline]
    pub fn commit(&self, state: ResourceState) {
        self.0.lock().state.commit(state);
    }

    /// Changes internal state to [`ResourceState::Ok`]
    pub fn commit_ok<T: ResourceData>(&self, data: T) {
        let mut guard = self.0.lock();
        guard.type_uuid = data.type_uuid();
        guard.state.commit_ok(data);
    }

    /// Changes internal state to [`ResourceState::LoadError`].
    pub fn commit_error<E: ResourceLoadError>(&self, error: E) {
        self.0.lock().state.commit_error(error);
    }
}

impl Future for UntypedResource {
    type Output = Result<Self, LoadError>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let state = self.0.clone();
        let mut guard = state.lock();
        match guard.state {
            ResourceState::Pending { ref mut wakers, .. } => {
                // Collect wakers, so we'll be able to wake task when worker thread finish loading.
                let cx_waker = cx.waker();
                if let Some(pos) = wakers.iter().position(|waker| waker.will_wake(cx_waker)) {
                    wakers[pos] = cx_waker.clone();
                } else {
                    wakers.push(cx_waker.clone())
                }

                Poll::Pending
            }
            ResourceState::LoadError { ref error, .. } => Poll::Ready(Err(error.clone())),
            ResourceState::Ok(_) => Poll::Ready(Ok(self.clone())),
        }
    }
}

#[cfg(test)]
mod test {
    use futures::task::noop_waker;
    use fyrox_core::futures;
    use std::error::Error;
    use std::task::{self};

    use super::*;

    #[derive(Debug, Default, Reflect, Visit, Clone, Copy)]
    struct Stub {}

    impl ResourceData for Stub {
        fn as_any(&self) -> &dyn std::any::Any {
            self
        }

        fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
            self
        }

        fn type_uuid(&self) -> Uuid {
            Uuid::default()
        }

        fn save(&mut self, _path: &Path) -> Result<(), Box<dyn Error>> {
            Err("Saving is not supported!".to_string().into())
        }

        fn can_be_saved(&self) -> bool {
            false
        }
    }

    impl TypeUuidProvider for Stub {
        fn type_uuid() -> Uuid {
            Uuid::default()
        }
    }

    impl ResourceLoadError for str {}

    #[test]
    fn visit_for_untyped_resource() {
        let mut r = UntypedResource::default();
        let mut visitor = Visitor::default();

        assert!(r.visit("name", &mut visitor).is_ok());
    }

    #[test]
    fn debug_for_untyped_resource() {
        let r = UntypedResource::default();

        assert_eq!(format!("{r:?}"), "Resource\n");
    }

    #[test]
    fn untyped_resource_new_pending() {
        let r = UntypedResource::new_pending(PathBuf::from("/foo").into(), Uuid::default());

        assert_eq!(r.0.lock().type_uuid, Uuid::default());
        assert_eq!(
            r.0.lock().kind,
            ResourceKind::External(PathBuf::from("/foo"))
        );
    }

    #[test]
    fn untyped_resource_new_load_error() {
        let r = UntypedResource::new_load_error(
            PathBuf::from("/foo").into(),
            Default::default(),
            Uuid::default(),
        );

        assert_eq!(r.0.lock().type_uuid, Uuid::default());
        assert_eq!(
            r.0.lock().kind,
            ResourceKind::External(PathBuf::from("/foo"))
        );
    }

    #[test]
    fn untyped_resource_use_count() {
        let r = UntypedResource::default();

        assert_eq!(r.use_count(), 1);
    }

    #[test]
    fn untyped_resource_try_cast() {
        let r = UntypedResource::default();
        let r2 = UntypedResource::new_pending(
            PathBuf::from("/foo").into(),
            Uuid::from_u128(0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8u128),
        );

        assert!(r.try_cast::<Stub>().is_some());
        assert!(r2.try_cast::<Stub>().is_none());
    }

    #[test]
    fn untyped_resource_commit() {
        let path = PathBuf::from("/foo");
        let stub = Stub {};

        let r = UntypedResource::new_pending(path.clone().into(), Default::default());
        assert_eq!(r.0.lock().kind, ResourceKind::External(path.clone()));

        r.commit(ResourceState::Ok(Box::new(stub)));
        assert_eq!(r.0.lock().kind, ResourceKind::External(path));
    }

    #[test]
    fn untyped_resource_commit_ok() {
        let path = PathBuf::from("/foo");
        let stub = Stub {};

        let r = UntypedResource::new_pending(path.clone().into(), Default::default());
        assert_eq!(r.0.lock().kind, ResourceKind::External(path.clone()));

        r.commit_ok(stub);
        assert_eq!(r.0.lock().kind, ResourceKind::External(path));
    }

    #[test]
    fn untyped_resource_commit_error() {
        let path = PathBuf::from("/foo");
        let path2 = PathBuf::from("/bar");

        let r = UntypedResource::new_pending(path.clone().into(), Default::default());
        assert_eq!(r.0.lock().kind, ResourceKind::External(path));
        assert_ne!(r.0.lock().kind, ResourceKind::External(path2));
    }

    #[test]
    fn untyped_resource_poll() {
        let path = PathBuf::from("/foo");
        let stub = Stub {};

        let waker = noop_waker();
        let mut cx = task::Context::from_waker(&waker);

        let mut r = UntypedResource(Arc::new(Mutex::new(ResourceHeader {
            kind: path.clone().into(),
            type_uuid: Uuid::default(),
            state: ResourceState::Ok(Box::new(stub)),
        })));
        assert!(Pin::new(&mut r).poll(&mut cx).is_ready());

        let mut r = UntypedResource(Arc::new(Mutex::new(ResourceHeader {
            kind: path.clone().into(),
            type_uuid: Uuid::default(),
            state: ResourceState::LoadError {
                error: Default::default(),
            },
        })));
        assert!(Pin::new(&mut r).poll(&mut cx).is_ready());
    }
}