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
pub mod address;
pub mod audio;
pub mod color;
pub mod function;
pub mod image;
pub mod key;
pub mod logger;
pub mod stream;
pub mod text;
pub mod trigger;
pub mod value;
pub use crate::resource::image::*;
pub use address::*;
pub use audio::*;
pub use color::*;
pub use function::*;
pub use key::*;
pub use logger::*;
pub use stream::*;
pub use text::*;
pub use trigger::Trigger;
pub use value::*;
use crate::assets::{IMAGE_FIXATION, IMAGE_RUSTACEAN};
use crate::server::{Config, Env};
use eframe::egui::mutex::RwLock;
use eframe::epaint;
use eyre::{eyre, Context, Result};
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone)]
pub struct ResourceManager(Arc<Mutex<HashMap<ResourceAddr, ResourceValue>>>);
#[derive(Clone)]
pub struct IoManager {
audio: Arc<AudioDevice>,
}
impl ResourceManager {
#[inline(always)]
pub fn new(_config: &Config) -> Result<Self> {
Ok(Self(Default::default()))
}
pub fn preload_block(
&mut self,
resources: Vec<ResourceAddr>,
tex_manager: Arc<RwLock<epaint::TextureManager>>,
#[allow(unused)] config: &Config,
env: &Env,
) -> Result<()> {
let mut map = self.0.lock().unwrap();
map.clear();
let src = ResourceAddr::Image("fixation.svg".into());
map.entry(src.clone()).or_insert({
let tex_manager = tex_manager.clone();
let (texture, size) = svg_from_bytes(tex_manager, IMAGE_FIXATION, src.path())?;
ResourceValue::Image(texture, size)
});
let mut default_fixation = true;
let src = ResourceAddr::Image("rustacean.svg".into());
map.entry(src.clone()).or_insert({
let tex_manager = tex_manager.clone();
let (texture, size) = svg_from_bytes(tex_manager, IMAGE_RUSTACEAN, src.path())?;
ResourceValue::Image(texture, size)
});
let mut default_rustacean = true;
for src in resources {
let mut is_new = !map.contains_key(&src);
match src.path().to_str().unwrap() {
"fixation.svg" => {
if default_fixation {
is_new = true;
default_fixation = false;
}
}
"rustacean.svg" => {
if default_rustacean {
is_new = true;
default_rustacean = false;
}
}
_ => {}
}
if is_new {
let data = match src.prefix(env.resource()) {
ResourceAddr::Ref(path) => ResourceValue::Ref(path),
ResourceAddr::Text(path) => {
let text = std::fs::read_to_string(&path)
.wrap_err_with(|| eyre!("Failed to load text resource ({path:?})"))?;
ResourceValue::Text(Arc::new(text))
}
ResourceAddr::Image(path) => {
let tex_manager = tex_manager.clone();
let (texture, size) = match src.extension().as_deref() {
Some("svg") => {
svg_from_file(tex_manager, &path).wrap_err_with(|| {
eyre!("Failed to load SVG resource ({path:?})")
})?
}
_ => image_from_file(tex_manager, &path).wrap_err_with(|| {
eyre!("Failed to load image resource ({path:?})")
})?,
};
ResourceValue::Image(texture, size)
}
ResourceAddr::Audio(path) => ResourceValue::Audio(
audio_from_file(&path, config)
.wrap_err_with(|| eyre!("Failed to load audio resource ({path:?})"))?,
),
ResourceAddr::Video(path) => {
let tex_manager = tex_manager.clone();
let (frames, framerate) = video_from_file(tex_manager, &path, config)
.wrap_err_with(|| eyre!("Failed to load video resource ({path:?})"))?;
ResourceValue::Video(frames, framerate)
}
ResourceAddr::Stream(path) => {
let tex_manager = tex_manager.clone();
ResourceValue::Stream(
stream_from_file(tex_manager, &path, config).wrap_err_with(|| {
eyre!("Failed to load stream resource ({path:?})")
})?,
)
}
};
println!("+ {src:?} : {data:?}");
map.insert(src, data);
}
}
Ok(())
}
pub fn fetch(&self, src: &ResourceAddr) -> Result<ResourceValue> {
if let Some(res) = self.0.lock().unwrap().get(src) {
Ok(res.clone())
} else {
Err(eyre!("Tried to fetch unexpected resource: {src:?}"))
}
}
}
impl Debug for IoManager {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "<IO>")
}
}
impl IoManager {
pub fn new(config: &Config) -> Result<Self> {
Ok(Self {
audio: Arc::new(AudioDevice::new(config)?),
})
}
pub fn audio(&self) -> Result<AudioSink> {
self.audio.sink()
}
}