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
/*
 * Created on Fri Dec 25 2020
 *
 * Copyright (c) storycraft. Licensed under the MIT Licence.
 */

pub mod collection;
pub mod number;
pub mod reference;
pub mod binary_tree;
pub mod string;

use std::io::{Read, Seek, Write};

use collection::{PsbUintArray, PsbList, PsbObject};
use number::PsbNumber;
use reference::PsbExtraRef;

use crate::{PsbError, PsbErrorKind, PsbRefs};
use byteorder::{ReadBytesExt, WriteBytesExt};

use self::{reference::PsbResourceRef, string::PsbString};

#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};

pub const PSB_TYPE_NONE: u8 = 0x00;

pub const PSB_TYPE_NULL: u8 = 0x01;

pub const PSB_TYPE_FALSE: u8 = 0x02;
pub const PSB_TYPE_TRUE: u8 = 0x03;

/// 0 <= N <= 8
pub const PSB_TYPE_INTEGER_N: u8 = 0x04;
pub const PSB_TYPE_FLOAT0: u8 = 0x1d;
pub const PSB_TYPE_FLOAT: u8 = 0x1e;
pub const PSB_TYPE_DOUBLE: u8 = 0x1f;

/// 1 <= N <= 8
pub const PSB_TYPE_INTEGER_ARRAY_N: u8 = 0x0C;

/// 1 <= N <= 4
pub const PSB_TYPE_STRING_N: u8 = 0x14;

/// 1 <= N <= 4
pub const PSB_TYPE_RESOURCE_N: u8 = 0x18;

pub const PSB_TYPE_LIST: u8 = 0x20;
pub const PSB_TYPE_OBJECT: u8 = 0x21;

/// 1 <= N <= 8
pub const PSB_TYPE_EXTRA_N: u8 = 0x21;

pub const PSB_COMPILER_INTEGER: u8 = 0x80;
pub const PSB_COMPILER_STRING: u8 = 0x81;
pub const PSB_COMPILER_RESOURCE: u8 = 0x82;
pub const PSB_COMPILER_DECIMAL: u8 = 0x83;
pub const PSB_COMPILER_ARRAY: u8 = 0x84;
pub const PSB_COMPILER_BOOL: u8 = 0x85;
pub const PSB_COMPILER_BINARY_TREE: u8 = 0x86;

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
pub enum PsbValue {

    None, Null,
    Bool(bool),
    Number(PsbNumber),
    IntArray(PsbUintArray),

    String(PsbString),

    List(PsbList),
    Object(PsbObject),

    Resource(PsbResourceRef),
    ExtraResource(PsbExtraRef),

    CompilerNumber,
    CompilerString,
    CompilerResource,
    CompilerDecimal,
    CompilerArray,
    CompilerBool,
    CompilerBinaryTree

}

impl PsbValue {

    fn from_bytes_type<T: Read + Seek>(value_type: u8, stream: &mut T) -> Result<(u64, PsbValue), PsbError> {
        match value_type {
            PSB_TYPE_NONE => Ok((1, PsbValue::None)),
            PSB_TYPE_NULL => Ok((1, PsbValue::Null)),

            PSB_TYPE_FALSE => Ok((1, PsbValue::Bool(false))),
            PSB_TYPE_TRUE => Ok((1, PsbValue::Bool(true))),
            
            PSB_TYPE_DOUBLE => {
                let (read, val) = PsbNumber::from_bytes(value_type, stream)?;
                Ok((read + 1, PsbValue::Number(val)))
            },

            PSB_TYPE_FLOAT0 => {
                let (read, val) = PsbNumber::from_bytes(value_type, stream)?;
                Ok((read + 1, PsbValue::Number(val)))
            },

            PSB_TYPE_FLOAT => {
                let (read, val) = PsbNumber::from_bytes(value_type, stream)?;
                Ok((read + 1, PsbValue::Number(val)))
            },

            _ if value_type >= PSB_TYPE_INTEGER_N && value_type <= PSB_TYPE_INTEGER_N + 8 => {
                let (read, number) = PsbNumber::from_bytes(value_type, stream)?;
                Ok((read + 1, PsbValue::Number(number)))
            },

            _ if value_type > PSB_TYPE_INTEGER_ARRAY_N && value_type <= PSB_TYPE_INTEGER_ARRAY_N + 8 => {
                let (read, array) = PsbUintArray::from_bytes(value_type - PSB_TYPE_INTEGER_ARRAY_N, stream)?;
                Ok((read + 1, PsbValue::IntArray(array)))
            },

            _ if value_type > PSB_TYPE_RESOURCE_N && value_type <= PSB_TYPE_RESOURCE_N + 4 => {
                let (read, map) = PsbResourceRef::from_bytes(value_type - PSB_TYPE_RESOURCE_N, stream)?;

                Ok((read + 1, PsbValue::Resource(map)))
            },

            _ if value_type > PSB_TYPE_EXTRA_N && value_type <= PSB_TYPE_EXTRA_N + 4 => {
                let (read, map) = PsbExtraRef::from_bytes(value_type - PSB_TYPE_EXTRA_N, stream)?;

                Ok((read + 1, PsbValue::ExtraResource(map)))
            },

            PSB_COMPILER_INTEGER => Ok((1, PsbValue::CompilerNumber)),
            PSB_COMPILER_STRING => Ok((1, PsbValue::CompilerString)),
            PSB_COMPILER_RESOURCE => Ok((1, PsbValue::CompilerResource)),
            PSB_COMPILER_ARRAY => Ok((1, PsbValue::CompilerArray)),
            PSB_COMPILER_BOOL => Ok((1, PsbValue::CompilerBool)),
            PSB_COMPILER_BINARY_TREE => Ok((1, PsbValue::CompilerBinaryTree)),

            _ => {
                Err(PsbError::new(PsbErrorKind::InvalidPSBValue, None))
            }
        }
    }

    pub fn from_bytes<T: Read + Seek>(stream: &mut T) -> Result<(u64, PsbValue), PsbError> {
        Self::from_bytes_type(stream.read_u8()?, stream)
    }

    pub fn from_bytes_refs<T: Read + Seek>(stream: &mut T, table: &PsbRefs) -> Result<(u64, PsbValue), PsbError> {
        let value_type = stream.read_u8()?;

        match value_type {

            PSB_TYPE_LIST => {
                let (read, list) = PsbList::from_bytes(stream, table)?;

                Ok((read + 1, PsbValue::List(list)))
            },

            PSB_TYPE_OBJECT => {
                let (read, map) = PsbObject::from_bytes(stream, table)?;

                Ok((read + 1, PsbValue::Object(map)))
            },

            _ if value_type > PSB_TYPE_STRING_N && value_type <= PSB_TYPE_STRING_N + 4 => {
                let (read, string) = PsbString::from_bytes(value_type - PSB_TYPE_STRING_N, stream, table)?;

                Ok((read + 1, PsbValue::String(string)))
            },

            _ => {
                Self::from_bytes_type(value_type, stream)
            }

        }
    }

    pub fn write_bytes(&self, stream: &mut impl Write) -> Result<u64, PsbError> {
        match self {
            PsbValue::None => {
                stream.write_u8(PSB_TYPE_NONE)?;
                Ok(1)
            },
            PsbValue::Null => {
                stream.write_u8(PSB_TYPE_NULL)?;
                Ok(1)
            },
            PsbValue::Bool(value) => {
                if *value {
                    stream.write_u8(PSB_TYPE_TRUE)?;
                } else {
                    stream.write_u8(PSB_TYPE_FALSE)?;
                }

                Ok(1)
            },
            PsbValue::Number(number) => {
                match number {
                    PsbNumber::Integer(integer) => {
                        let n = if *integer == 0 {
                            0
                        } else {
                            PsbNumber::get_n(*integer)
                        };

                        stream.write_u8(PSB_TYPE_INTEGER_N + n)?;
                    },

                    PsbNumber::Double(_) => {
                        stream.write_u8(PSB_TYPE_DOUBLE)?;
                    },

                    PsbNumber::Float(float) => {
                        if *float == 0_f32 {
                            stream.write_u8(PSB_TYPE_FLOAT0)?;
                        } else {
                            stream.write_u8(PSB_TYPE_FLOAT)?;
                        }
                    }
                }

                Ok(1 + number.write_bytes(stream)?)
            },

            PsbValue::IntArray(array) => {
                stream.write_u8(PSB_TYPE_INTEGER_ARRAY_N + array.get_n())?;

                Ok(1 + array.write_bytes(stream)?)
            },

            PsbValue::Resource(res) => {
                stream.write_u8(PSB_TYPE_RESOURCE_N + res.get_n())?;

                Ok(1 + res.write_bytes(stream)?)
            },
            PsbValue::ExtraResource(res) => {
                stream.write_u8(PSB_TYPE_EXTRA_N + res.get_n())?;

                Ok(1 + res.write_bytes(stream)?)
            },

            PsbValue::CompilerNumber => {
                stream.write_u8(PSB_COMPILER_INTEGER)?;
                Ok(1)
            },
            PsbValue::CompilerString => {
                stream.write_u8(PSB_COMPILER_STRING)?;
                Ok(1)
            },
            PsbValue::CompilerResource => {
                stream.write_u8(PSB_COMPILER_RESOURCE)?;
                Ok(1)
            },
            PsbValue::CompilerDecimal => {
                stream.write_u8(PSB_COMPILER_DECIMAL)?;
                Ok(1)
            },
            PsbValue::CompilerArray => {
                stream.write_u8(PSB_COMPILER_ARRAY)?;
                Ok(1)
            },
            PsbValue::CompilerBool => {
                stream.write_u8(PSB_COMPILER_BOOL)?;
                Ok(1)
            },
            PsbValue::CompilerBinaryTree => {
                stream.write_u8(PSB_COMPILER_BINARY_TREE)?;
                Ok(1)
            },

            _ => {
                Err(PsbError::new(PsbErrorKind::InvalidPSBValue, None))
            }
        }
    }

    pub fn write_bytes_refs(&self, stream: &mut impl Write, table: &PsbRefs) -> Result<u64, PsbError> {
        match &self {

            PsbValue::String(string) => {
                let n = PsbNumber::get_uint_n(match table.find_string_index(string.string()) {

                    Some(ref_index) => {
                        Ok(ref_index)
                    },
        
                    None => Err(PsbError::new(PsbErrorKind::InvalidOffsetTable, None))
                }?);
                
                stream.write_u8(PSB_TYPE_STRING_N + n)?;

                Ok(1 + string.write_bytes(stream, table)?)
            },

            PsbValue::List(list) => {
                stream.write_u8(PSB_TYPE_LIST)?;

                Ok(1 + list.write_bytes(stream, table)?)
            },

            PsbValue::Object(object) => {
                stream.write_u8(PSB_TYPE_OBJECT)?;

                Ok(1 + object.write_bytes(stream, table)?)
            },

            _ => self.write_bytes(stream)

        }
    }

}