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
#[cfg(feature = "generator")]
pub mod generator;
pub mod loaders;

use bevy_app::{App, Plugins};
use bevy_ecs::bundle::Bundle;
use bevy_ecs::component::Component;
use bevy_ecs::system::{BoxedSystem, EntityCommands, IntoSystem, Resource};
use bevy_ecs::world::EntityWorldMut;
use leptos::expect_context;
use std::any::type_name;
use std::path::Path;
use std::sync::{Arc, Mutex};

pub struct DataLayer {
    app: App,
}

impl DataLayer {
    pub fn new() -> Self {
        DataLayer { app: App::new() }
    }

    pub fn insert_resource<R: Resource>(&mut self, value: R) -> &mut Self {
        self.app.insert_resource(value);
        self
    }

    pub fn get_resource<R: Resource + Clone>(&self) -> Option<R> {
        self.app.world.get_resource::<R>().cloned()
    }

    pub fn spawn<B: Bundle>(&mut self, bundle: B) -> EntityWorldMut {
        self.app.world.spawn(bundle)
    }

    pub fn run_boxed<R: 'static, I: 'static>(
        &mut self,
        system: &mut BoxedSystem<I, R>,
        input: I,
    ) -> R {
        system.initialize(&mut self.app.world);
        let to_return = system.run(input, &mut self.app.world);
        system.apply_deferred(&mut self.app.world);

        to_return
    }

    pub fn add_plugins<M>(&mut self, plugins: impl Plugins<M>) -> &mut Self {
        self.app.add_plugins(plugins);
        self
    }

    pub fn run<S, I, R, T>(&mut self, system: S, input: I) -> R
    where
        S: IntoSystem<I, R, T>,
        R: 'static,
        I: 'static,
    {
        let mut boxed_system: BoxedSystem<I, R> = Box::new(IntoSystem::into_system(system));
        self.run_boxed(&mut boxed_system, input)
    }
}

impl Default for DataLayer {
    fn default() -> Self {
        Self::new()
    }
}

pub fn expect_resource<R: Resource + Clone>() -> R {
    use_resource::<R>().unwrap_or_else(|| {
        panic!(
            "Expected resource {}, but it didn't exist",
            type_name::<R>()
        )
    })
}

pub fn run_system<S, R, T>(system: S) -> R
where
    S: IntoSystem<(), R, T>,
    R: 'static,
{
    run_system_with_input(system, ())
}

pub fn run_system_with_input<S, I, R, T>(system: S, input: I) -> R
where
    S: IntoSystem<I, R, T>,
    R: 'static,
    I: 'static,
{
    let cinnog = expect_context::<Arc<Mutex<DataLayer>>>();
    let mut lock = cinnog.lock().unwrap();

    lock.run(system, input)
}

pub fn use_resource<R: Resource + Clone>() -> Option<R> {
    let cinnog = expect_context::<Arc<Mutex<DataLayer>>>();
    let lock = cinnog.lock().unwrap();
    lock.get_resource()
}

#[derive(Component, Clone)]
pub struct FileName(pub String);

#[derive(Component, Clone)]
pub struct FilePath(pub String);

pub trait Ingest {
    fn ingest(self, commands: &mut EntityCommands)
    where
        Self: Sized;

    fn ingest_path(&self, commands: &mut EntityCommands, path: &Path) {
        commands.insert(default_bundle_from_path(path));
    }
}

pub fn default_bundle_from_path(path: &Path) -> impl Bundle {
    let path_string = path.to_string_lossy().into_owned();
    let file_ending = path.extension();
    let file_name = if let Some(ending) = file_ending {
        path.file_name().map(|name| {
            name.to_string_lossy()
                .trim_end_matches(&format!(".{}", ending.to_string_lossy().as_ref()))
                .to_owned()
        })
    } else {
        path.file_name()
            .map(|name| name.to_string_lossy().into_owned())
    };

    (
        FileName(file_name.expect("No file name in path")),
        FilePath(path_string),
    )
}