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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * Created on Fri Dec 25 2020
 *
 * Copyright (c) storycraft. Licensed under the MIT Licence.
 */

pub mod types;

pub mod header;
pub mod offsets;

pub mod reader;
pub mod writer;

mod internal;

pub use reader::PsbReader;
pub use writer::PsbWriter;

use header::PsbHeader;
use io::Seek;
use offsets::{PsbOffsets, PsbResourcesOffset};
use types::{PsbValue, collection::PsbObject};

use std::{error::Error, io::{self, Read, SeekFrom}};

/// psb file signature
pub const PSB_SIGNATURE: u32 = 0x425350;

/// compressed psb file signature
pub const PSB_MDF_SIGNATURE: u32 = 0x66646D;

#[derive(Debug)]
pub struct PsbError {

    kind: PsbErrorKind,
    err: Option<Box<dyn Error>>

}

impl PsbError {

    pub fn new(kind: PsbErrorKind, err: Option<Box<dyn Error>>) -> Self {
        Self { kind, err }
    }

    pub fn kind(&self) -> &PsbErrorKind {
        &self.kind
    }

    pub fn source(&self) -> &Option<Box<dyn Error>> {
        &self.err
    }

}

#[derive(Debug)]
pub enum PsbErrorKind {

    Io(io::Error),
    InvalidFile,
    InvalidHeader,
    UnknownHeaderVersion,
    InvalidIndex,
    InvalidPSBValue,
    InvalidPSBRoot,
    InvalidOffsetTable,
    Custom

}

impl From<io::Error> for PsbError {

    fn from(err: io::Error) -> Self {
        PsbError::new(PsbErrorKind::Io(err), None)
    }

}

#[derive(Debug, Clone)]
pub struct PsbRefs {

    names: Vec<String>,

    strings: Vec<String>,

}

impl PsbRefs {

    pub fn new(names: Vec<String>, strings: Vec<String>) -> Self {
        Self {
            names, strings
        }
    }

    pub fn names(&self) -> &Vec<String> {
        &self.names
    }

    pub fn names_mut(&mut self) -> &mut Vec<String> {
        &mut self.names
    }

    pub fn names_len(&self) -> usize {
        self.names.len()
    }

    pub fn get_name(&self, index: usize) -> Option<&String> {
        self.names.get(index)
    }

    pub fn get_name_mut(&mut self, index: usize) -> Option<&mut String> {
        self.names.get_mut(index)
    }

    pub fn find_name_index(&self, name: &String) -> Option<u64> {
        for (i, nm) in self.names.iter().enumerate() {
            if nm == name {
                return Some(i as u64)
            }
        }

        None
    }

    pub fn strings(&self) -> &Vec<String> {
        &self.strings
    }

    pub fn strings_mut(&mut self) -> &mut Vec<String> {
        &mut self.strings
    }

    pub fn strings_len(&self) -> usize {
        self.strings.len()
    }

    pub fn get_string(&self, index: usize) -> Option<&String> {
        self.strings.get(index)
    }

    pub fn get_string_mut(&mut self, index: usize) -> Option<&mut String> {
        self.strings.get_mut(index)
    }

    pub fn find_string_index(&self, string: &String) -> Option<u64> {
        for (i, st) in self.strings.iter().enumerate() {
            if st == string {
                return Some(i as u64)
            }
        }

        None
    }
}

#[derive(Debug)]
pub struct VirtualPsb {

    header: PsbHeader,

    resources: Vec<Vec<u8>>,
    extra: Vec<Vec<u8>>,

    root: PsbObject

}

impl VirtualPsb {

    pub fn new(
        header: PsbHeader,
        resources: Vec<Vec<u8>>,
        extra: Vec<Vec<u8>>,
        root: PsbObject
    ) -> Self {
        Self {
            header,
            resources,
            extra,
            root
        }
    }

    pub fn header(&self) -> PsbHeader {
        self.header
    }

    pub fn resources(&self) -> &Vec<Vec<u8>> {
        &self.resources
    }

    pub fn resources_mut(&mut self) -> &mut Vec<Vec<u8>> {
        &mut self.resources
    }

    pub fn extra(&self) -> &Vec<Vec<u8>> {
        &self.extra
    }

    pub fn extra_mut(&mut self) -> &mut Vec<Vec<u8>> {
        &mut self.extra
    }

    pub fn root(&self) -> &PsbObject {
        &self.root
    }

    pub fn root_mut(&mut self) -> &mut PsbObject {
        &mut self.root
    }

    pub fn set_root(&mut self, root: PsbObject) {
        self.root = root;
    }

    pub fn unwrap(self) -> (PsbHeader, Vec<Vec<u8>>, Vec<Vec<u8>>, PsbObject) {
        (
            self.header,
            self.resources,
            self.extra,
            self.root
        )
    }

}

#[derive(Debug)]
pub struct PsbFile<T: Read + Seek> {

    header: PsbHeader,
    
    refs: PsbRefs,
    offsets: PsbOffsets,

    stream: T

}

impl<T: Read + Seek> PsbFile<T> {

    pub fn new(header: PsbHeader, refs: PsbRefs, offsets: PsbOffsets, stream: T) -> Self {
        Self {
            header,
            refs,
            offsets,
            stream
        }
    }

    pub fn header(&self) -> PsbHeader {
        self.header
    }

    pub fn refs(&self) -> &PsbRefs {
        &self.refs
    }

    pub fn offsets(&self) -> PsbOffsets {
        self.offsets
    }

    pub fn entry_point(&self) -> u32 {
        self.offsets.entry_point as u32
    }

    pub fn load_root(&mut self) -> Result<PsbObject, PsbError> {
        self.stream.seek(SeekFrom::Start(self.entry_point() as u64))?;
        let (_, root) = PsbValue::from_bytes_refs(&mut self.stream, &self.refs)?;

        if let PsbValue::Object(root_obj) = root {
            Ok(root_obj)
        } else {
            Err(PsbError::new(PsbErrorKind::InvalidPSBRoot, None))
        }
    }

    pub fn load_resources(&mut self) -> Result<Vec<Vec<u8>>, PsbError> {
        Self::load_from_table(&mut self.stream, self.offsets.resources)
    }

    pub fn load_extra(&mut self) -> Result<Vec<Vec<u8>>, PsbError> {
        if self.offsets.extra.is_none() {
            Ok(Vec::new())
        } else {
            Self::load_from_table(&mut self.stream, self.offsets.extra.unwrap())
        }
    }

    fn load_from_table<R: Read + Seek>(stream: &mut R, table: PsbResourcesOffset) -> Result<Vec<Vec<u8>>, PsbError> {
        stream.seek(SeekFrom::Start(table.offset_pos as u64))?;
        let (_, resource_offsets) = match PsbValue::from_bytes(stream)? {
    
            (read, PsbValue::IntArray(array)) => Ok((read, array)),

            _ => Err(PsbError::new(PsbErrorKind::InvalidOffsetTable, None))

        }?;
        
        stream.seek(SeekFrom::Start(table.lengths_pos as u64))?;
        let (_, resource_lengths) = match PsbValue::from_bytes(stream)? {

            (read, PsbValue::IntArray(array)) => Ok((read, array)),

            _ => Err(PsbError::new(PsbErrorKind::InvalidOffsetTable, None))

        }?;

        if resource_offsets.len() < resource_lengths.len() {
            return Err(PsbError::new(PsbErrorKind::InvalidOffsetTable, None));
        }

        let mut resources = Vec::new();

        let resource_offsets = resource_offsets.unwrap();
        let resource_lengths = resource_lengths.unwrap();

        for i in 0..resource_offsets.len() {
            let mut buffer = Vec::new();

            stream.seek(SeekFrom::Start(table.data_pos as u64 + resource_offsets[i] as u64))?;
            stream.take(resource_lengths[i] as u64).read_to_end(&mut buffer)?;

            resources.push(buffer);
        }

        Ok(resources)
    }

    /// Load Psb file to memory.
    /// Returns VirtualPsb.
    pub fn load(&mut self) -> Result<VirtualPsb, PsbError> {
        let root = self.load_root()?;
        let res = self.load_resources()?;
        let extra = self.load_extra()?;

        Ok(VirtualPsb::new(self.header, res, extra, root))
    }

    /// Unwrap stream
    pub fn unwrap(self) -> T {
        self.stream
    }

}