-
Notifications
You must be signed in to change notification settings - Fork 8
/
msg.rs
205 lines (181 loc) · 6.4 KB
/
msg.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
use crate::file_ext::*;
use crate::hash::hash_as_utf16;
use crate::rsz::Guid;
use anyhow::{bail, Result};
use serde::*;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::io::{Read, Seek};
#[derive(Debug, Serialize)]
pub struct MsgAttributeHeader {
pub ty: i32,
pub name: String,
}
#[derive(Debug, Serialize, Clone)]
pub enum MsgAttribute {
Int(i64),
Float(f64),
String(String),
Unknown(u64),
}
#[derive(Debug, Serialize, Clone)]
pub struct MsgEntry {
pub name: String,
pub guid: Guid,
pub hash: u32,
pub attributes: Vec<MsgAttribute>,
pub content: Vec<String>,
}
#[derive(Debug, Serialize)]
pub struct Msg {
pub attribute_headers: Vec<MsgAttributeHeader>,
pub entries: Vec<MsgEntry>,
}
impl Msg {
pub fn new<F: Read + Seek>(mut file: F) -> Result<Msg> {
let version = file.read_u32()?;
if version != 17 && version != 539100710 {
bail!("Wrong version {version} for MSG")
}
if &file.read_magic()? != b"GMSG" {
bail!("Wrong magic for MSG")
}
if file.read_u64()? != 0x10 {
bail!("Expected 0x10")
}
let entry_count = file.read_u32()?;
let attribute_count = file.read_u32()?;
let language_count = file.read_u32()?;
file.seek_align_up(8)?;
let data_offset = file.read_u64()?;
let p_offset = file.read_u64()?;
let languages_offset = file.read_u64()?;
let attribute_types_offset = file.read_u64()?;
let attribute_names_offset = file.read_u64()?;
let entries = (0..entry_count)
.map(|_| file.read_u64())
.collect::<Result<Vec<_>>>()?;
file.seek_noop(p_offset)?;
let p = file.read_u64()?;
if p != 0 {
bail!("Expected 0")
}
file.seek_noop(languages_offset)?;
let languages = (0..language_count)
.map(|_| file.read_u32())
.collect::<Result<Vec<_>>>()?;
for (i, language) in languages.into_iter().enumerate() {
if i != usize::try_from(language)? {
bail!("Unexpected language index")
}
}
file.seek_assert_align_up(attribute_types_offset, 8)?;
let attribute_types = (0..attribute_count)
.map(|_| file.read_i32())
.collect::<Result<Vec<_>>>()?;
file.seek_assert_align_up(attribute_names_offset, 8)?;
let attribute_names = (0..attribute_count)
.map(|_| file.read_u64())
.collect::<Result<Vec<_>>>()?;
let entries = entries
.into_iter()
.map(|entry| {
file.seek_noop(entry)?;
let mut guid = [0; 16];
file.read_exact(&mut guid)?;
let _ = file.read_u32()?; //???
let hash = file.read_u32()?;
let name = file.read_u64()?;
let attributes = file.read_u64()?;
let content = (0..language_count)
.map(|_| file.read_u64())
.collect::<Result<Vec<_>>>()?;
Ok((name, Guid { bytes: guid }, hash, attributes, content))
})
.collect::<Result<Vec<_>>>()?
.into_iter()
.map(|(name, guid, hash, attributes, content)| {
file.seek_noop(attributes)?;
let attributes = (0..attribute_count)
.map(|_| file.read_u64())
.collect::<Result<Vec<_>>>()?;
Ok((name, guid, hash, attributes, content))
})
.collect::<Result<Vec<_>>>()?;
file.seek_noop(data_offset)?;
let mut data = vec![];
file.read_to_end(&mut data)?;
let key = [
0xCF, 0xCE, 0xFB, 0xF8, 0xEC, 0x0A, 0x33, 0x66, 0x93, 0xA9, 0x1D, 0x93, 0x50, 0x39,
0x5F, 0x09,
];
let mut prev = 0;
for (i, byte) in data.iter_mut().enumerate() {
let cur = *byte;
*byte ^= prev ^ key[i & 0xF];
prev = cur;
}
let entries = entries
.into_iter()
.map(|(name, guid, hash, attributes, content)| {
let name = (&data[usize::try_from(name - data_offset)?..]).read_u16str()?;
if hash_as_utf16(&name) != hash {
bail!("Wrong hash")
}
let attributes = attributes
.into_iter()
.zip(&attribute_types)
.map(|(attr, &ty)| {
Ok(match ty {
0 => MsgAttribute::Int(attr as i64),
1 => MsgAttribute::Float(f64::from_bits(attr)),
2 => MsgAttribute::String(
(&data[usize::try_from(attr - data_offset)?..]).read_u16str()?,
),
-1 => MsgAttribute::Unknown(attr),
_ => bail!("Unknown attribute {ty}"),
})
})
.collect::<Result<Vec<_>>>()?;
let content = content
.into_iter()
.map(|o| (&data[usize::try_from(o - data_offset)?..]).read_u16str())
.collect::<Result<Vec<_>>>()?;
Ok(MsgEntry {
name,
guid,
hash,
attributes,
content,
})
})
.collect::<Result<Vec<_>>>()?;
let attribute_headers = attribute_types
.into_iter()
.zip(attribute_names)
.map(|(ty, name)| {
let name = (&data[usize::try_from(name - data_offset)?..]).read_u16str()?;
Ok(MsgAttributeHeader { ty, name })
})
.collect::<Result<Vec<_>>>()?;
Ok(Msg {
attribute_headers,
entries,
})
}
pub fn get_entry(&self, name: &str) -> Option<&MsgEntry> {
self.entries.iter().find(|entry| entry.name == name)
}
pub fn get_name_map(&self) -> HashMap<&String, &MsgEntry> {
self.entries
.iter()
.map(|entry| (&entry.name, entry))
.collect()
}
pub fn get_guid_map(&self) -> HashMap<Guid, &MsgEntry> {
self.entries
.iter()
.map(|entry| (entry.guid, entry))
.collect()
}
}