1use core::pin::Pin;
2use futures_io::AsyncBufRead;
3use std::{
4 hash::{Hash, Hasher},
5 io::{BufRead, IoSlice, Result as IOResult, Write},
6};
7
8use peridot_serialization_utils::VariableULong;
9
10pub fn asset_name_hash(name: &str, ext: &str) -> u64 {
11 let mut hasher = xxhash_rust::xxh3::Xxh3::new();
12 name.hash(&mut hasher);
13 ext.hash(&mut hasher);
14
15 hasher.finish()
16}
17
18#[derive(Debug, Clone, PartialEq, Eq, Hash)]
19pub struct AssetName {
20 pub name: String,
21 pub ext: String,
22}
23impl AssetName {
24 #[inline(always)]
25 pub fn hash(&self) -> u64 {
26 asset_name_hash(&self.name, &self.ext)
27 }
28}
29
30#[derive(Debug, Clone, PartialEq, Eq, Hash)]
31pub struct AssetNameRef<'s> {
32 pub name: &'s str,
33 pub ext: &'s str,
34}
35impl AssetNameRef<'_> {
36 #[inline(always)]
37 pub fn hash(&self) -> u64 {
38 asset_name_hash(self.name, self.ext)
39 }
40}
41
42#[derive(Debug)]
43pub struct AssetEntryHeadingPair {
44 pub byte_length: u64,
45 pub relative_offset: u64,
46}
47impl AssetEntryHeadingPair {
48 pub fn write(&self, writer: &mut (impl Write + ?Sized)) -> IOResult<usize> {
49 let b1 = VariableULong(self.byte_length).to_bytes();
50 let b2 = VariableULong(self.relative_offset).to_bytes();
51
52 crate::utils::write_all_vectored(writer, &mut [IoSlice::new(&b1), IoSlice::new(&b2)])?;
53
54 Ok(b1.len() + b2.len())
55 }
56
57 #[cfg(feature = "async-rt-async-std")]
58 pub async fn write_async(
59 &self,
60 writer: &mut (impl async_std::io::Write + Unpin + ?Sized),
61 ) -> IOResult<usize> {
62 let b1 = VariableULong(self.byte_length).to_bytes();
63 let b2 = VariableULong(self.relative_offset).to_bytes();
64
65 crate::utils::write_all_vectored_async(writer, &mut [IoSlice::new(&b1), IoSlice::new(&b2)])
66 .await?;
67
68 Ok(b1.len() + b2.len())
69 }
70
71 pub fn read(reader: &mut (impl BufRead + ?Sized)) -> IOResult<Self> {
72 let VariableULong(byte_length) = VariableULong::read(reader)?;
73 let VariableULong(relative_offset) = VariableULong::read(reader)?;
74
75 Ok(Self {
76 byte_length,
77 relative_offset,
78 })
79 }
80
81 pub async fn read_async(mut reader: Pin<&mut (impl AsyncBufRead + ?Sized)>) -> IOResult<Self> {
82 let VariableULong(byte_length) = VariableULong::read_async(reader.as_mut()).await?;
83 let VariableULong(relative_offset) = VariableULong::read_async(reader).await?;
84
85 Ok(Self {
86 byte_length,
87 relative_offset,
88 })
89 }
90
91 pub fn from_bytes_head(bytes: &[u8]) -> (Self, usize) {
92 let (VariableULong(byte_length), bl_bytes) = VariableULong::from_bytes_head(bytes);
93 let (VariableULong(relative_offset), ro_bytes) =
94 VariableULong::from_bytes_head(&bytes[bl_bytes..]);
95
96 (
97 Self {
98 byte_length,
99 relative_offset,
100 },
101 bl_bytes + ro_bytes,
102 )
103 }
104}