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
use std::{
    borrow::Cow,
    fmt::Write,
    sync::{Arc, LazyLock},
};

use scc::{
    hash_map::{Entry, VacantEntry},
    HashMap,
};

use super::*;

static NAMES: &str = include_str!("../../data/botw_hashed_names.txt");
static NUMBERED_NAMES: &str = include_str!("../../data/botw_numbered_names.txt");

type StringBuffer = crate::types::FixedSafeString<256>;

impl<const N: usize> Write for crate::types::FixedSafeString<N> {
    fn write_str(&mut self, s: &str) -> std::fmt::Result {
        match s
            .len()
            .min(N.saturating_sub(self.len).saturating_sub(s.len()))
        {
            0 => Ok(()),
            len => {
                self.data[self.len..self.len + len].copy_from_slice(s.as_bytes());
                self.len += len;
                Ok(())
            }
        }
    }
}

/// Since there are basically no good runtime string formatting options in Rust,
/// we'll just do this instead.
struct ChildFormatIterator<'a, 'b> {
    string: &'a str,
    pos: usize,
    index: usize,
    buf: &'b mut StringBuffer,
}

impl<'a, 'b> ChildFormatIterator<'a, 'b> {
    pub fn new(string: &'a str, pos: usize, buf: &'b mut StringBuffer) -> Self {
        ChildFormatIterator {
            string,
            pos,
            index: 0,
            buf,
        }
    }
}

impl<'a, 'b> Iterator for ChildFormatIterator<'a, 'b> {
    type Item = u32;

    fn next(&mut self) -> Option<Self::Item> {
        self.buf.clear(); // Clear the buffer for reuse

        use std::fmt::Write;
        let result = match self.index {
            0 => write!(self.buf, "{}{}", self.string, self.pos),
            1 => write!(self.buf, "{}{:02}", self.string, self.pos),
            2 => write!(self.buf, "{}{:03}", self.string, self.pos),
            3 => write!(self.buf, "{}_{}", self.string, self.pos),
            4 => write!(self.buf, "{}_{:02}", self.string, self.pos),
            5 => write!(self.buf, "{}_{:03}", self.string, self.pos),
            _ => return None,
        };

        self.index += 1;
        result.ok().map(|_| hash_name(self.buf.as_str()))
    }
}

impl ExactSizeIterator for ChildFormatIterator<'_, '_> {
    fn len(&self) -> usize {
        6
    }
}

fn format_numbered_name(name: &str, pos: usize, buf: &mut StringBuffer) {
    buf.clear();

    if name.contains("%d") {
        let mut split = name.split("%d");
        let prefix = unsafe { split.next().unwrap_unchecked() };
        buf.insert_str(0, prefix);
        write!(buf, "{}", pos).expect("Format failure");
        if let Some(suffix) = split.next() {
            buf.push_str(suffix);
        }
    } else if name.contains("%02d") {
        let mut split = name.split("%02d");
        let prefix = unsafe { split.next().unwrap_unchecked() };
        buf.insert_str(0, prefix);
        write!(buf, "{:02}", pos).expect("Format failure");
        if let Some(suffix) = split.next() {
            buf.push_str(suffix);
        }
    } else if name.contains("%03d") {
        let mut split = name.split("%03d");
        let prefix = unsafe { split.next().unwrap_unchecked() };
        buf.insert_str(0, prefix);
        write!(buf, "{:03}", pos).expect("Format failure");
        if let Some(suffix) = split.next() {
            buf.push_str(suffix);
        }
    } else if name.contains("%04d") {
        let mut split = name.split("%04d");
        let prefix = unsafe { split.next().unwrap_unchecked() };
        buf.insert_str(0, prefix);
        write!(buf, "{:04}", pos).expect("Format failure");
        if let Some(suffix) = split.next() {
            buf.push_str(suffix);
        }
    } else if name.contains("%u") {
        let mut split = name.split("%u");
        let prefix = unsafe { split.next().unwrap_unchecked() };
        buf.insert_str(0, prefix);
        write!(buf, "{}", pos).expect("Format failure");
        if let Some(suffix) = split.next() {
            buf.push_str(suffix);
        }
    } else if name.contains("%02u") {
        let mut split = name.split("%02u");
        let prefix = unsafe { split.next().unwrap_unchecked() };
        buf.insert_str(0, prefix);
        write!(buf, "{:02}", pos).expect("Format failure");
        if let Some(suffix) = split.next() {
            buf.push_str(suffix);
        }
    } else {
        unsafe { core::hint::unreachable_unchecked() }
    }
}

macro_rules! free_cow {
    ($cow:expr, $life:tt) => {{
        let cow = $cow as *const _;
        unsafe { &*(cow as *const Cow<$life, str>) }
    }};
}

/// A table of names that is used to recover original names in binary parameter
/// archives which store only name hashes.
///
/// Because binary parameter archives only store CRC32 hashes of structure
/// names, recovering the original names – which is useful for converting
/// archives to a human-readable format – requires the use of a name table.
///
/// When serializing to YAML, by default roead will use a table that contains
/// strings from Breath of the Wild’s executable.
#[derive(Default)]
pub struct NameTable<'a> {
    names: HashMap<u32, Cow<'a, str>, rustc_hash::FxBuildHasher>,
}

impl<'a> NameTable<'a> {
    /// Create a new name table, optionally including default BOTW strings.
    pub fn new(botw_strings: bool) -> NameTable<'a> {
        if botw_strings {
            Self {
                names: {
                    let map = HashMap::with_capacity_and_hasher(58469, rustc_hash::FxBuildHasher);
                    for line in NAMES.lines() {
                        let _ = map.insert(hash_name(line), line.into());
                    }
                    map
                },
            }
        } else {
            Default::default()
        }
    }

    /// Add a known string to the name table.
    pub fn add_name(&self, name: impl Into<Cow<'a, str>>) {
        let name = name.into();
        let hash = hash_name(&name);
        self.names.entry(hash).or_insert(name);
    }

    /// Add a known string to the name table if you already know the hash (to
    /// avoid computing it).
    pub fn add_name_with_hash(&self, name: impl Into<Cow<'a, str>>, hash: u32) {
        self.names.entry(hash).or_insert_with(|| name.into());
    }

    /// Add a known string to the name table.
    pub fn add_name_str<'s: 'a>(&'a self, name: &'s str) {
        let hash = hash_name(name);
        self.names.entry(hash).or_insert_with(|| name.into());
    }

    /// Tries to guess the name that is associated with the given hash and index
    /// (of the parameter / object / list in its parent).
    ///
    /// The table is automatically updated with any newly found names if an
    /// indice-based guess was necessary.
    pub fn get_name(&self, hash: u32, index: usize, parent_hash: u32) -> Option<&Cow<'_, str>> {
        fn test_names<'a: 'b, 'b, 'c>(
            entry: VacantEntry<'b, u32, Cow<'a, str>, rustc_hash::FxBuildHasher>,
            hash: u32,
            index: usize,
            prefix: &str,
            buf: &'c mut StringBuffer,
        ) -> std::result::Result<
            &'b Cow<'a, str>,
            VacantEntry<'b, u32, Cow<'a, str>, rustc_hash::FxBuildHasher>,
        > {
            for i in index..(index + 1) {
                for guess_hash in ChildFormatIterator::new(prefix, i, buf) {
                    if guess_hash == hash {
                        let name = entry.insert_entry(buf.to_string().into());
                        return Ok(free_cow!(name.get(), 'a));
                    }
                }
            }
            Err(entry)
        }

        let parent_name = self.names.get(&parent_hash).map(|c| free_cow!(c.get(), 'a));
        match self.names.entry(hash) {
            Entry::Occupied(entry) => Some(free_cow!(entry.get(), 'a)),
            Entry::Vacant(entry) => {
                let mut entry = entry;
                let mut guess_buffer = StringBuffer::default();
                if let Some(parent_name) = parent_name
                // Try to guess the name from the parent structure if possible.
                {
                    let guess = test_names(entry, hash, index, parent_name, &mut guess_buffer)
                        .or_else(|entry| {
                            test_names(entry, hash, index, "Children", &mut guess_buffer)
                        })
                        .or_else(|entry| test_names(entry, hash, index, "Child", &mut guess_buffer))
                        .or_else(|mut entry| {
                            // Sometimes the parent name is plural and the object names are
                            // singular.
                            for suffix in ["s", "es", "List"] {
                                if let Some(singular) = parent_name.strip_suffix(suffix) {
                                    let guess =
                                        test_names(entry, hash, index, singular, &mut guess_buffer);
                                    match guess {
                                        Ok(found) => return Ok(found),
                                        Err(ret_entry) => entry = ret_entry,
                                    }
                                }
                            }
                            Err(entry)
                        });
                    match guess {
                        Ok(found) => return Some(free_cow!(found, 'a)),
                        Err(ret_entry) => {
                            entry = ret_entry;
                        }
                    }
                }
                // Last resort: test all numbered names.
                for format in NUMBERED_NAMES.lines() {
                    for i in 0..(index + 2) {
                        format_numbered_name(format, i, &mut guess_buffer);
                        if hash_name(&guess_buffer) == hash {
                            let name =
                                entry.insert_entry(Cow::Owned(guess_buffer.as_str().to_owned()));
                            return Some(free_cow!(name.get(), 'a));
                        }
                    }
                }
                None
            }
        }
    }
}

static DEFAULT_NAME_TABLE: LazyLock<Arc<NameTable<'static>>> =
    LazyLock::new(|| Arc::new(NameTable::new(true)));

/// Returns the default instance of the name table, which is automatically
/// populated with Breath of the Wild strings. It is initialised on first use
/// and has interior mutability.
pub fn get_default_name_table() -> &'static LazyLock<Arc<NameTable<'static>>> {
    &DEFAULT_NAME_TABLE
}