Convert Music to ogg/vorbis and load in build.rs #14

Merged
msrd0 merged 4 commits from music into main 2024-07-07 13:29:43 +00:00
13 changed files with 75 additions and 6 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
assets/sounds/bzz.ogg Normal file

Binary file not shown.

View file

@ -17,6 +17,9 @@ struct Assets {
/// of the constant storing their png. /// of the constant storing their png.
assets: BTreeMap<String, String>, assets: BTreeMap<String, String>,
/// Sound assets.
sound_assets: BTreeMap<String, String>,
/// Asset groups contained within this asset group, mapping their name to the assets /// Asset groups contained within this asset group, mapping their name to the assets
/// that group contains. /// that group contains.
groups: BTreeMap<String, Box<Assets>> groups: BTreeMap<String, Box<Assets>>
@ -37,15 +40,19 @@ impl AssetsWriter {
} }
} }
fn add_png<P: AsRef<Path>>( fn asset_name<P: AsRef<Path>>(canonical_path: P) -> String {
let mut hasher = DefaultHasher::new();
canonical_path.as_ref().hash(&mut hasher);
let hash = hasher.finish();
format!("ASSET_{hash:X}")
}
fn add_png<P: AsRef<Path> + Copy>(
&mut self, &mut self,
canonical_path: P, canonical_path: P,
png: tiny_skia::Pixmap png: tiny_skia::Pixmap
) -> String { ) -> String {
let mut hasher = DefaultHasher::new(); let const_name = Self::asset_name(canonical_path);
canonical_path.as_ref().hash(&mut hasher);
let hash = hasher.finish();
let const_name = format!("ASSET_{hash:X}");
let out_dir = env::var_os("OUT_DIR").unwrap(); let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir: PathBuf = out_dir.into(); let out_dir: PathBuf = out_dir.into();
@ -62,6 +69,18 @@ impl AssetsWriter {
const_name const_name
} }
fn add_ogg<P: AsRef<Path> + Copy>(&mut self, canonical_path: P) -> String {
let const_name = Self::asset_name(canonical_path);
writeln!(self.file, "// {}", canonical_path.as_ref().display()).unwrap();
writeln!(
self.file,
"const {const_name}: &[u8] = include_bytes!(\"{}\");",
canonical_path.as_ref().display()
)
.unwrap();
const_name
}
fn finish(mut self) { fn finish(mut self) {
fn write_assets_struct( fn write_assets_struct(
file: &mut File, file: &mut File,
@ -85,6 +104,13 @@ impl AssetsWriter {
asset_name.to_snake_case() asset_name.to_snake_case()
)?; )?;
} }
for asset_name in root.sound_assets.keys() {
writeln!(
file,
"{indent}\tpub {}: comfy::Sound,",
asset_name.to_snake_case()
)?;
}
for group_name in root.groups.keys() { for group_name in root.groups.keys() {
writeln!( writeln!(
file, file,
@ -101,6 +127,9 @@ impl AssetsWriter {
for asset_const_name in root.assets.values() { for asset_const_name in root.assets.values() {
writeln!(file, "{indent}\t\tc.load_texture_from_bytes({asset_const_name:?}, {asset_const_name});")?; writeln!(file, "{indent}\t\tc.load_texture_from_bytes({asset_const_name:?}, {asset_const_name});")?;
} }
for asset_const_name in root.sound_assets.values() {
writeln!(file, "{indent}\t\tcomfy::load_sound_from_bytes({asset_const_name:?}, {asset_const_name}, Default::default());")?;
}
for group_name in root.groups.keys() { for group_name in root.groups.keys() {
writeln!(file, "{indent}\t\t{group_name}::Assets::load(c);")?; writeln!(file, "{indent}\t\t{group_name}::Assets::load(c);")?;
} }
@ -118,6 +147,13 @@ impl AssetsWriter {
asset_name.to_snake_case() asset_name.to_snake_case()
)?; )?;
} }
for (asset_name, asset_const_name) in &root.sound_assets {
writeln!(
file,
"{indent}\t{}: comfy::sound_id({asset_const_name:?}),",
asset_name.to_snake_case()
)?;
}
for group_name in root.groups.keys() { for group_name in root.groups.keys() {
writeln!( writeln!(
file, file,
@ -170,6 +206,8 @@ fn process_dir<P: AsRef<Path> + Copy>(
groups.pop(); groups.pop();
} else if path.extension().map(|ext| ext == "svg").unwrap_or(false) { } else if path.extension().map(|ext| ext == "svg").unwrap_or(false) {
process_svg(&path, dir, writer, groups); process_svg(&path, dir, writer, groups);
} else if path.extension().map(|ext| ext == "ogg").unwrap_or(false) {
process_ogg(&path, dir, writer, groups);
} }
} }
} }
@ -196,7 +234,8 @@ fn process_svg<P: AsRef<Path> + Copy, Q: AsRef<Path>>(
resvg::render(&tree, transform, &mut pixmap.as_mut()); resvg::render(&tree, transform, &mut pixmap.as_mut());
let const_name = writer.add_png( let const_name = writer.add_png(
file.as_ref() &file
.as_ref()
.canonicalize() .canonicalize()
.expect("Failed to canonicalize"), .expect("Failed to canonicalize"),
pixmap pixmap
@ -218,3 +257,33 @@ fn process_svg<P: AsRef<Path> + Copy, Q: AsRef<Path>>(
const_name const_name
); );
} }
fn process_ogg<P: AsRef<Path> + Copy, Q: AsRef<Path>>(
file: P,
dir: Q,
writer: &mut AssetsWriter,
groups: &[String]
) {
let const_name = writer.add_ogg(
&file
.as_ref()
.canonicalize()
.expect("Failed to canonicalize")
);
let mut group = &mut writer.root;
for group_name in groups {
if !group.groups.contains_key(group_name) {
group.groups.insert(group_name.to_owned(), Box::default());
}
group = group.groups.get_mut(group_name).unwrap();
}
group.sound_assets.insert(
file.as_ref()
.file_stem()
.expect("File doesn't have a stem")
.to_str()
.expect("Non-UTF8 file names aren't allowed")
.into(),
const_name
);
}