-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
651 lines (585 loc) · 19.1 KB
/
build.rs
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
use std::{
env,
fs::{self, File},
io::Write,
path::{Path, PathBuf},
};
use symphonia::core::formats::FormatOptions;
use symphonia::core::io::MediaSourceStream;
use symphonia::core::meta::MetadataOptions;
use symphonia::core::probe::Hint;
use symphonia::default::get_probe;
const ASSET_PATH_VAR: &str = "BEVY_ASSET_PATH";
const OUTPUT_FILE_NAME: &str = "audio_controller.rs";
fn main() {
cargo_emit::rerun_if_env_changed!(ASSET_PATH_VAR);
let out_dir = env::var_os("OUT_DIR").unwrap();
let mut marker_file = File::create(Path::new(&out_dir).join(OUTPUT_FILE_NAME)).unwrap();
let mut files = Vec::new();
// Check if env variable is set for the assets folder
if let Some(dir) = env::var(ASSET_PATH_VAR)
.ok()
.map(|v| Path::new(&v).to_path_buf())
.and_then(|path| {
if path.exists() {
Some(path)
} else {
cargo_emit::warning!(
"${} points to an unknown folder: {}",
ASSET_PATH_VAR,
path.to_string_lossy()
);
None
}
})
// Otherwise, search for the target folder and look for an assets folder next to it
.or_else(|| {
env::var("OUT_DIR")
.ok()
.map(|v| Path::new(&v).to_path_buf())
.and_then(|path| {
for ancestor in path.ancestors() {
if let Some(last) = ancestor.file_name() {
if last == "target" {
return ancestor.parent().map(|parent| {
let imported_dir = parent.join("imported_assets");
if imported_dir.exists() {
imported_dir.join("Default")
} else {
parent.join("assets")
}
});
}
}
}
None
})
.and_then(|path| {
if path.exists() {
Some(path)
} else {
cargo_emit::warning!(
"Could not find asset folder from Cargo build directory"
);
None
}
})
})
{
cargo_emit::rerun_if_changed!(dir.to_string_lossy());
// cargo_emit::warning!("Asset folder found: {}", dir.to_string_lossy());
let building_for_wasm = std::env::var("CARGO_CFG_TARGET_ARCH") == Ok("wasm32".to_string());
visit_dirs(&dir)
.iter()
.map(|path| (path, path.strip_prefix(&dir).unwrap()))
.for_each(|(full_path, path)| {
let mut path = path.to_string_lossy().to_string();
if building_for_wasm {
// building for wasm. replace paths with forward slash in case we're building from windows
path = path.replace(std::path::MAIN_SEPARATOR, "/");
}
cargo_emit::rerun_if_changed!(full_path.to_string_lossy());
if let Some(duration) = get_audio_duration(&full_path) {
files.push(AudioFile { path, duration });
}
});
} else if std::env::var("DOCS_RS").is_ok() {
// let out_dir = env::var_os("OUT_DIR").unwrap();
// let dest_path = Path::new(&out_dir).join("audio_lengths.rs");
// let mut file = File::create(dest_path).unwrap();
// file.write_all(
// "/// Generated function that will return the length of the input audio file. It does not panic if a file is missing but will log a warning.
// fn include_all_assets(registry: impl EmbeddedRegistry){}"
// .as_ref(),
// )
// .unwrap();
} else {
cargo_emit::warning!(
"Could not find asset folder, please specify its path with ${}",
ASSET_PATH_VAR
);
// panic!("No asset folder found");
}
// Write the markers
marker_file
.write_all(
format!(
r#"pub mod markers {{
#![allow(unused)]
use bevy::ecs::component::Component;
#[cfg(feature = "inspect")]
use bevy::{{ecs::reflect::ReflectComponent, reflect::Reflect}};
{}
}}
"#,
files
.iter()
.map(|f| f.get_marker_struct())
.collect::<Vec<_>>()
.join("\n")
)
.as_ref(),
)
.unwrap();
// Write the insert_audio_track trait for entity commands so we can do some jank component inserts
marker_file
.write_all(
format!(
r#"
mod ac_traits {{
#![allow(unused)]
use bevy::ecs::system::EntityCommands;
use super::{{audio_files::*, markers::*}};
pub(crate) trait CommandAudioTracks {{
fn insert_audio_track(&mut self, id: &AudioFiles) -> &mut Self;
fn remove_audio_track(&mut self, id: &AudioFiles) -> &mut Self;
}}
impl<'a> CommandAudioTracks for EntityCommands<'a> {{
fn insert_audio_track(&mut self, id: &AudioFiles) -> &mut EntityCommands<'a> {{
match id {{
{}
AudioFiles::Unknown => self,
}}
}}
fn remove_audio_track(&mut self, id: &AudioFiles) -> &mut EntityCommands<'a> {{
match id {{
{}
AudioFiles::Unknown => self,
}}
}}
}}
}}
"#,
files
.iter()
.map(|f| f.insert_audio_track_impl())
.collect::<Vec<_>>()
.join("\n "),
files
.iter()
.map(|f| f.remove_audio_track_impl())
.collect::<Vec<_>>()
.join("\n ")
)
.as_ref(),
)
.unwrap();
// Write the enum for the audio files
marker_file
.write_all(
format!(
r#"
pub mod audio_files {{
#![allow(unused)]
use std::path::Path;
use bevy::{{core::Name, log::warn}};
#[cfg(feature = "inspect")]
use bevy::{{ecs::reflect::ReflectComponent, reflect::Reflect}};
/// Contains the path and duration of the audio file
#[derive(Debug, Default)]
#[cfg_attr(feature = "inspect", derive(Reflect))]
pub struct AudioFile {{
pub path: &'static str,
pub duration: f32,
}}
/// This is your "library" of audio files. It is a convenient way to safely spawn audio files in your game without having to rely on "magic strings".
///
/// If you need to insert strings dynamically from a file or a network request, you can use the `From<&str>` implementation to convert it to an `AudioFiles` enum.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "inspect", derive(Reflect))]
pub enum AudioFiles {{
#[default]
Unknown,
{}
}}
pub(super) const ALL_FILES: [AudioFiles; {}] = [
{}
];
impl ToString for AudioFiles {{
fn to_string(&self) -> String {{
match self {{
{}
Self::Unknown => "Unknown",
}}
.to_string()
}}
}}
impl From<&str> for AudioFiles {{
fn from(file_name: &str) -> Self {{
match file_name.replace('\\', "/").as_str() {{
{}
unknown => {{
warn!("Unknown audio file '{{}}' requested", unknown);
AudioFiles::Unknown
}}
}}
}}
}}
impl From<String> for AudioFiles {{
fn from(file_name: String) -> Self {{
Self::from(&file_name)
}}
}}
impl From<&String> for AudioFiles {{
fn from(file_name: &String) -> Self {{
Self::from(file_name.as_str())
}}
}}
impl From<AudioFiles> for &str {{
fn from(file_name: AudioFiles) -> &'static str {{
match file_name {{
{}
unknown => {{
warn!("Unknown audio file '{{:?}}' requested", unknown);
"Unknown"
}}
}}
}}
}}
impl From<&Name> for AudioFiles {{
fn from(name: &Name) -> Self {{
Self::from(&name.to_string())
}}
}}
impl AudioFiles {{
{}
pub fn get(&self) -> AudioFile {{
match self {{
{}
Self::Unknown => {{
warn!("Unknown audio file requested");
AudioFile::default()
}}
}}
}}
pub fn duration(&self) -> f32 {{
match self {{
{}
Self::Unknown => {{
warn!("Unknown audio duration requested");
0.0
}}
}}
}}
pub fn path(&self) -> &'static str {{
match self {{
{}
Self::Unknown => {{
warn!("Unknown audio file name requested");
""
}}
}}
}}
}}
}}
"#,
files
.iter()
.map(|f| f.enum_creator())
.collect::<Vec<_>>()
.join("\n "),
files.len(),
files
.iter()
.map(|f| f.build_iterable())
.collect::<Vec<_>>()
.join("\n "),
files
.iter()
.map(|f| f.get_enum_file_match())
.collect::<Vec<_>>()
.join("\n "),
files
.iter()
.map(|f| f.get_enum_match())
.collect::<Vec<_>>()
.join("\n "),
files
.iter()
.map(|f| f.get_enum_file_match())
.collect::<Vec<_>>()
.join("\n "),
files
.iter()
.map(|f| f.audio_file_struct())
.collect::<Vec<_>>()
.join("\n "),
files
.iter()
.map(|f| f.get())
.collect::<Vec<_>>()
.join("\n "),
files
.iter()
.map(|f| f.get_duration())
.collect::<Vec<_>>()
.join("\n "),
files
.iter()
.map(|f| f.get_path())
.collect::<Vec<_>>()
.join("\n "),
)
.as_ref(),
)
.unwrap();
marker_file
.write_all(
format!(
r#"
mod ac_assets {{
#![allow(unused)]
use bevy::{{
asset::{{AssetServer, Handle}},
audio::AudioSource,
ecs::system::{{Res, ResMut, Resource}},
}};
#[cfg(feature = "inspect")]
use bevy::{{ecs::reflect::ReflectResource, reflect::Reflect}};
use super::audio_files::AudioFiles;
pub(super) fn load_assets(
asset_server: Res<AssetServer>,
mut internal_loader: ResMut<ACAssetLoader>,
) {{
{}
}}
#[derive(Default, Resource)]
#[cfg_attr(feature = "inspect", derive(Reflect))]
#[cfg_attr(feature = "inspect", reflect(Resource))]
pub(super) struct ACAssetLoader {{
{}
}}
impl ACAssetLoader {{
pub(super) fn get(&self, id: &AudioFiles) -> Option<Handle<AudioSource>> {{
match id {{
{}
AudioFiles::Unknown => None,
}}
}}
}}
}}
"#,
files
.iter()
.map(|f| f.asset_loader())
.collect::<Vec<_>>()
.join("\n"),
files
.iter()
.map(|f| f.asset_field())
.collect::<Vec<_>>()
.join("\n"),
files
.iter()
.map(|f| f.asset_getter())
.collect::<Vec<_>>()
.join("\n")
)
.as_ref(),
)
.unwrap();
}
struct AudioFile {
path: String,
duration: f32,
}
impl AudioFile {
fn build_iterable(&self) -> String {
format!("AudioFiles::{},", self.pascal_case())
}
fn get_enum_match(&self) -> String {
let struct_name = self.pascal_case();
format!(
"path if Path::new(path) == Path::new(Self::{}.path) => AudioFiles::{},",
self.snake_case().to_uppercase(),
struct_name
)
}
fn get_enum_file_match(&self) -> String {
format!(
"AudioFiles::{} => AudioFiles::{}.path,",
self.pascal_case(),
self.snake_case().to_uppercase(),
)
}
fn insert_audio_track_impl(&self) -> String {
let struct_name = self.pascal_case();
format!(
r#"AudioFiles::{} => self.insert({}::default()),"#,
struct_name, struct_name
)
}
fn remove_audio_track_impl(&self) -> String {
let struct_name = self.pascal_case();
format!(
r#"AudioFiles::{} => self.remove::<{}>(),"#,
struct_name, struct_name
)
}
fn enum_creator(&self) -> String {
format!("{},", self.pascal_case())
}
fn asset_field(&self) -> String {
let field_name = self.snake_case();
format!(r#" pub(super) {}: Handle<AudioSource>,"#, field_name)
}
fn asset_loader(&self) -> String {
format!(
r#" internal_loader.{} = asset_server.load(AudioFiles::{}.path());"#,
self.snake_case(),
self.pascal_case()
)
}
fn asset_getter(&self) -> String {
format!(
r#" AudioFiles::{} => Some(self.{}.clone()),"#,
self.pascal_case(),
self.snake_case()
)
}
fn get(&self) -> String {
format!(
" Self::{} => Self::{},",
self.pascal_case(),
self.snake_case().to_uppercase(),
)
}
fn get_duration(&self) -> String {
format!(
" Self::{} => Self::{}.duration,",
self.pascal_case(),
self.snake_case().to_uppercase(),
)
}
fn get_path(&self) -> String {
format!(
" Self::{} => Self::{}.path,",
self.pascal_case(),
self.snake_case().to_uppercase(),
)
}
fn audio_file_struct(&self) -> String {
format!(
"const {}: AudioFile = AudioFile {{
path: {:?},
duration: {},
}};",
self.snake_case().to_uppercase(),
self.path,
self.duration
)
}
fn get_marker_struct(&self) -> String {
let struct_name = self.pascal_case();
format!(
r#"
/// Marker for the audio file: {:?}
///
/// This is not meant to be inserted or spawned directly outside of the plugin internals
///
/// Only use it for querying
#[derive(Debug, Component, Default)]
#[cfg_attr(feature = "inspect", derive(Reflect))]
#[cfg_attr(feature = "inspect", reflect(Component))]
pub struct {};"#,
self.path, struct_name,
)
}
fn pascal_case(&self) -> String {
let mut parts: Vec<String> = self
.path
.split(|c: char| {
c.is_whitespace() || "-_.".contains(c) || c == std::path::MAIN_SEPARATOR
})
.filter(|s| !s.is_empty())
.map(|s| {
let mut chars = s.chars();
match chars.next() {
Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
None => String::new(),
}
})
.collect();
if let Some(extension) = parts.last_mut() {
*extension = extension.to_uppercase();
}
parts.concat()
}
fn snake_case(&self) -> String {
let mut snake_case = String::new();
let name = self.path.replace(&['/', '\\', '.', '-'][..], "_");
let mut prev_char = '\0';
for (i, ch) in name.chars().enumerate() {
if ch.is_uppercase() && i > 0 && prev_char != '_' {
snake_case.push('_');
}
snake_case.push(ch.to_ascii_lowercase());
prev_char = ch;
}
snake_case
}
}
fn visit_dirs(dir: &Path) -> Vec<PathBuf> {
let mut collected = vec![];
if dir.is_dir() {
for entry in fs::read_dir(dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_dir() {
collected.append(&mut visit_dirs(&path));
} else {
if is_supported_audio_file(&path) {
collected.push(path);
}
}
}
}
collected
}
fn is_supported_audio_file(path: &Path) -> bool {
#[allow(unused_mut)]
let mut formats = Vec::<&str>::new();
#[cfg(feature = "flac")]
formats.push("flac");
#[cfg(feature = "mp3")]
formats.push("mp3");
#[cfg(feature = "ogg")]
formats.push("ogg");
#[cfg(feature = "wav")]
formats.push("wav");
if let Some(ext) = path.extension().and_then(|ext| ext.to_str()) {
formats.contains(&ext)
} else {
return false;
}
}
fn get_audio_duration(path: &Path) -> Option<f32> {
let file = std::fs::File::open(path).ok()?;
let mss = MediaSourceStream::new(Box::new(file), Default::default());
let hint = Hint::new();
let probed = get_probe()
.format(
&hint,
mss,
&FormatOptions::default(),
&MetadataOptions::default(),
)
.ok()?;
let mut format = probed.format;
if let Some(track) = format.default_track() {
let codec_params = &track.codec_params;
let sample_rate = codec_params.sample_rate? as f32;
let mut decoder = symphonia::default::get_codecs()
.make(&codec_params, &Default::default())
.ok()?;
let mut total_frames = 0;
while let Ok(packet) = format.next_packet() {
if let Ok(decoded) = decoder.decode(&packet) {
total_frames += decoded.frames();
}
}
Some(total_frames as f32 / sample_rate)
} else {
None
}
}