Convert Music to ogg/vorbis and load in build.rs (#14)
Some checks failed
Rust / rustfmt (push) Successful in 20s
Rust / clippy (push) Failing after 1m31s
Rust / build (push) Has been cancelled

Reviewed-on: #14
Co-authored-by: Dominic <git@msrd0.de>
Co-committed-by: Dominic <git@msrd0.de>
This commit is contained in:
Dominic 2024-07-07 13:29:41 +00:00 committed by msrd0.dev - Forgejo
parent d3a54938af
commit 03e12d6feb
Signed by: msrd0.dev - Forgejo
GPG key ID: E2F16281CAA26E5F
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.
assets: BTreeMap<String, String>,
/// Sound assets.
sound_assets: BTreeMap<String, String>,
/// Asset groups contained within this asset group, mapping their name to the assets
/// that group contains.
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,
canonical_path: P,
png: tiny_skia::Pixmap
) -> String {
let mut hasher = DefaultHasher::new();
canonical_path.as_ref().hash(&mut hasher);
let hash = hasher.finish();
let const_name = format!("ASSET_{hash:X}");
let const_name = Self::asset_name(canonical_path);
let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir: PathBuf = out_dir.into();
@ -62,6 +69,18 @@ impl AssetsWriter {
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 write_assets_struct(
file: &mut File,
@ -85,6 +104,13 @@ impl AssetsWriter {
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() {
writeln!(
file,
@ -101,6 +127,9 @@ impl AssetsWriter {
for asset_const_name in root.assets.values() {
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() {
writeln!(file, "{indent}\t\t{group_name}::Assets::load(c);")?;
}
@ -118,6 +147,13 @@ impl AssetsWriter {
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() {
writeln!(
file,
@ -170,6 +206,8 @@ fn process_dir<P: AsRef<Path> + Copy>(
groups.pop();
} else if path.extension().map(|ext| ext == "svg").unwrap_or(false) {
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());
let const_name = writer.add_png(
file.as_ref()
&file
.as_ref()
.canonicalize()
.expect("Failed to canonicalize"),
pixmap
@ -218,3 +257,33 @@ fn process_svg<P: AsRef<Path> + Copy, Q: AsRef<Path>>(
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
);
}