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
//! Defines the `Reload` trait.

use std::{sync::Arc, time::Instant};

use amethyst_core::{
    ecs::prelude::{DispatcherBuilder, Read, Resources, System, Write},
    SystemBundle, Time,
};
use amethyst_error::Error;

#[cfg(feature = "profiler")]
use thread_profiler::profile_scope;

use crate::{Format, FormatValue, Loader, Source};

/// This bundle activates hot reload for the `Loader`,
/// adds a `HotReloadStrategy` and the `HotReloadSystem`.
#[derive(Default)]
pub struct HotReloadBundle {
    strategy: HotReloadStrategy,
}

impl HotReloadBundle {
    /// Creates a new bundle.
    pub fn new(strategy: HotReloadStrategy) -> Self {
        HotReloadBundle { strategy }
    }
}

impl<'a, 'b> SystemBundle<'a, 'b> for HotReloadBundle {
    fn build(self, dispatcher: &mut DispatcherBuilder<'a, 'b>) -> Result<(), Error> {
        dispatcher.add(HotReloadSystem::new(self.strategy), "hot_reload", &[]);
        Ok(())
    }
}

/// An ECS resource which allows to configure hot reloading.
///
/// ## Examples
///
/// ```
/// # use amethyst_assets::HotReloadStrategy;
/// # use amethyst_core::ecs::prelude::World;
/// #
/// # fn main() {
/// let mut world = World::new();
/// // Assets will be reloaded every two seconds (in case they changed)
/// world.add_resource(HotReloadStrategy::every(2));
/// # }
/// ```
#[derive(Clone)]
pub struct HotReloadStrategy {
    inner: HotReloadStrategyInner,
}

impl HotReloadStrategy {
    /// Causes hot reloads every `n` seconds.
    pub fn every(n: u8) -> Self {
        use std::u64::MAX;

        HotReloadStrategy {
            inner: HotReloadStrategyInner::Every {
                interval: n,
                last: Instant::now(),
                frame_number: MAX,
            },
        }
    }

    /// This allows to use `trigger` for hot reloading.
    pub fn when_triggered() -> Self {
        use std::u64::MAX;

        HotReloadStrategy {
            inner: HotReloadStrategyInner::Trigger {
                triggered: false,
                frame_number: MAX,
            },
        }
    }

    /// Never do any hot-reloading.
    pub fn never() -> Self {
        HotReloadStrategy {
            inner: HotReloadStrategyInner::Never,
        }
    }

    /// The frame after calling this, all changed assets will be reloaded.
    /// Doesn't do anything if the strategy wasn't created with `when_triggered`.
    pub fn trigger(&mut self) {
        if let HotReloadStrategyInner::Trigger {
            ref mut triggered, ..
        } = self.inner
        {
            *triggered = true;
        }
    }

    /// Crate-internal method to check if reload is necessary.
    /// `reload_counter` is a per-storage value which is only used
    /// for and by this method.
    pub(crate) fn needs_reload(&self, current_frame: u64) -> bool {
        match self.inner {
            HotReloadStrategyInner::Every { frame_number, .. } => frame_number == current_frame,
            HotReloadStrategyInner::Trigger { frame_number, .. } => frame_number == current_frame,
            HotReloadStrategyInner::Never => false,
        }
    }
}

impl Default for HotReloadStrategy {
    fn default() -> Self {
        HotReloadStrategy::every(1)
    }
}

#[derive(Clone)]
enum HotReloadStrategyInner {
    Every {
        interval: u8,
        last: Instant,
        frame_number: u64,
    },
    Trigger {
        triggered: bool,
        frame_number: u64,
    },
    Never,
}

/// System for updating `HotReloadStrategy`.
pub struct HotReloadSystem {
    initial_strategy: HotReloadStrategy,
}

impl HotReloadSystem {
    /// Create a new reload system
    pub fn new(strategy: HotReloadStrategy) -> Self {
        HotReloadSystem {
            initial_strategy: strategy,
        }
    }
}

impl<'a> System<'a> for HotReloadSystem {
    type SystemData = (Read<'a, Time>, Write<'a, HotReloadStrategy>);

    fn run(&mut self, (time, mut strategy): Self::SystemData) {
        #[cfg(feature = "profiler")]
        profile_scope!("hot_reload_system");

        match strategy.inner {
            HotReloadStrategyInner::Trigger {
                ref mut triggered,
                ref mut frame_number,
            } => {
                if *triggered {
                    *frame_number = time.frame_number() + 1;
                }
                *triggered = false;
            }
            HotReloadStrategyInner::Every {
                interval,
                ref mut last,
                ref mut frame_number,
            } => {
                if last.elapsed().as_secs() > u64::from(interval) {
                    *frame_number = time.frame_number() + 1;
                    *last = Instant::now();
                }
            }
            HotReloadStrategyInner::Never => {}
        }
    }

    fn setup(&mut self, res: &mut Resources) {
        use amethyst_core::ecs::prelude::SystemData;
        Self::SystemData::setup(res);
        res.insert(self.initial_strategy.clone());
        res.fetch_mut::<Loader>().set_hot_reload(true);
    }
}

/// The `Reload` trait provides a method which checks if an asset needs to be reloaded.
pub trait Reload<D>: ReloadClone<D> + Send + Sync + 'static {
    /// Checks if a reload is necessary.
    fn needs_reload(&self) -> bool;
    /// Returns the asset name.
    fn name(&self) -> String;
    /// Returns the format name.
    fn format(&self) -> &'static str;
    /// Reloads the asset.
    fn reload(self: Box<Self>) -> Result<FormatValue<D>, Error>;
}

pub trait ReloadClone<D> {
    fn cloned(&self) -> Box<dyn Reload<D>>;
}

impl<D: 'static, T> ReloadClone<D> for T
where
    T: Clone + Reload<D>,
{
    fn cloned(&self) -> Box<dyn Reload<D>> {
        Box::new(self.clone())
    }
}

impl<D: 'static> Clone for Box<dyn Reload<D>> {
    fn clone(&self) -> Self {
        self.cloned()
    }
}

/// An implementation of `Reload` which just stores the modification time
/// and the path of the file.
pub struct SingleFile<D> {
    format: Box<dyn Format<D>>,
    modified: u64,
    path: String,
    source: Arc<dyn Source>,
}

impl<D: 'static> SingleFile<D> {
    /// Creates a new `SingleFile` reload object.
    pub fn new(
        format: Box<dyn Format<D>>,
        modified: u64,
        path: String,
        source: Arc<dyn Source>,
    ) -> Self {
        SingleFile {
            format,
            modified,
            path,
            source,
        }
    }
}

impl<D: 'static> Clone for SingleFile<D> {
    fn clone(&self) -> Self {
        SingleFile {
            format: self.format.clone(),
            modified: self.modified,
            path: self.path.clone(),
            source: self.source.clone(),
        }
    }
}

impl<D: 'static> Reload<D> for SingleFile<D> {
    fn needs_reload(&self) -> bool {
        self.modified != 0 && (self.source.modified(&self.path).unwrap_or(0) > self.modified)
    }

    fn name(&self) -> String {
        self.path.clone()
    }

    fn format(&self) -> &'static str {
        self.format.name()
    }

    fn reload(self: Box<Self>) -> Result<FormatValue<D>, Error> {
        #[cfg(feature = "profiler")]
        profile_scope!("reload_single_file");

        let this: SingleFile<D> = *self;
        let SingleFile {
            format,
            path,
            source,
            ..
        } = this;

        format.import(path, source, Some(objekt::clone(&format)))
    }
}