msg_tool 0.4.0-alpha.3

A command-line tool for exporting, importing, packing, and unpacking script files.
Documentation
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
//!Extensions for emote_psb crate.
use crate::ext::io::*;
use adler::Adler32;
use anyhow::Result;
use emote_psb::PsbError;
use emote_psb::PsbFile;
use emote_psb::PsbReader;
use emote_psb::PsbRefs;
use emote_psb::PsbWriter;
use emote_psb::VirtualPsb;
use emote_psb::header::PsbHeader;
use emote_psb::offsets::{PsbOffsets, PsbResourcesOffset, PsbStringOffset};
use emote_psb::reader::MdfReader;
use emote_psb::types::collection::*;
use emote_psb::types::number::*;
use emote_psb::types::reference::*;
use emote_psb::types::string::*;
use emote_psb::types::*;
#[cfg(feature = "json")]
use json::JsonValue;
#[cfg(feature = "json")]
use json::number::Number;
use serde::ser::SerializeStruct;
use serde::{Deserialize, Serialize};
use std::cmp::PartialEq;
use std::collections::{BTreeMap, HashMap};
use std::io::{Read, Seek, SeekFrom, Write};
use std::ops::{Index, IndexMut};

#[cfg(feature = "json")]
fn f32_to_number(x: f32) -> Number {
    if !x.is_finite() {
        return Number::from_parts(true, 0, 0);
    }

    let s = format!("{}", x);

    if s.contains('e') || s.contains('E') {
        let value = x as f64;
        return if value >= 0.0 {
            Number::from(value)
        } else {
            Number::from(value)
        };
    }

    let positive = !s.starts_with('-');
    let s = s.trim_start_matches('-');

    let parts: Vec<&str> = s.split('.').collect();

    if parts.len() == 1 {
        let mantissa: u64 = parts[0].parse().unwrap_or(0);
        Number::from_parts(positive, mantissa, 0)
    } else {
        let int_part = parts[0];
        let frac_part = parts[1].trim_end_matches('0'); // 去除尾部的0

        if frac_part.is_empty() {
            // 没有实际小数部分
            let mantissa: u64 = int_part.parse().unwrap_or(0);
            Number::from_parts(positive, mantissa, 0)
        } else {
            let combined = format!("{}{}", int_part, frac_part);
            let mantissa: u64 = combined.parse().unwrap_or(0);
            let exponent: i16 = -(frac_part.len() as i16);

            Number::from_parts(positive, mantissa, exponent)
        }
    }
}

const NONE: PsbValueFixed = PsbValueFixed::None;

#[derive(Debug, Serialize, Deserialize)]
/// Represents of a PSB value.
pub enum PsbValueFixed {
    /// No value.
    None,
    /// Represents a null value.
    Null,
    /// Represents a boolean value.
    Bool(bool),
    /// Represents a number value.
    Number(PsbNumber),
    /// Represents an array of integers.
    IntArray(PsbUintArray),
    /// Represents a string value.
    String(PsbString),
    /// Represents a list of PSB values.
    List(PsbListFixed),
    /// Represents an object with key-value pairs.
    Object(PsbObjectFixed),
    /// Represents a resource reference.
    Resource(PsbResourceRef),
    /// Represents an extra resource reference.
    ExtraResource(PsbExtraRef),
    /// Represents a compiler number.
    CompilerNumber,
    /// Represents a compiler string.
    CompilerString,
    /// Represents a compiler resource.
    CompilerResource,
    /// Represents a compiler decimal.
    CompilerDecimal,
    /// Represents a compiler array.
    CompilerArray,
    /// Represents a compiler boolean.
    CompilerBool,
    /// Represents a compiler binary tree.
    CompilerBinaryTree,
}

impl From<String> for PsbValueFixed {
    fn from(value: String) -> Self {
        PsbValueFixed::String(PsbString::from(value))
    }
}

impl From<bool> for PsbValueFixed {
    fn from(value: bool) -> Self {
        PsbValueFixed::Bool(value)
    }
}

impl From<i64> for PsbValueFixed {
    fn from(value: i64) -> Self {
        PsbValueFixed::Number(PsbNumber::Integer(value))
    }
}

impl From<f64> for PsbValueFixed {
    fn from(value: f64) -> Self {
        PsbValueFixed::Number(PsbNumber::Double(value))
    }
}

impl From<f32> for PsbValueFixed {
    fn from(value: f32) -> Self {
        PsbValueFixed::Number(PsbNumber::Float(value))
    }
}

impl From<PsbObjectFixed> for PsbValueFixed {
    fn from(value: PsbObjectFixed) -> Self {
        PsbValueFixed::Object(value)
    }
}

impl From<PsbListFixed> for PsbValueFixed {
    fn from(value: PsbListFixed) -> Self {
        PsbValueFixed::List(value)
    }
}

impl From<&[PsbValueFixed]> for PsbValueFixed {
    fn from(value: &[PsbValueFixed]) -> Self {
        PsbValueFixed::List(PsbListFixed {
            values: value.to_vec(),
        })
    }
}

impl PsbValueFixed {
    /// Converts this value to original PSB value type.
    pub fn to_psb(self, warn_on_none: bool) -> PsbValue {
        match self {
            PsbValueFixed::None => {
                if warn_on_none {
                    eprintln!("Warning: PSB value is None, output script may broken.");
                    crate::COUNTER.inc_warning();
                }
                PsbValue::None
            }
            PsbValueFixed::Null => PsbValue::Null,
            PsbValueFixed::Bool(b) => PsbValue::Bool(b),
            PsbValueFixed::Number(n) => PsbValue::Number(n),
            PsbValueFixed::IntArray(arr) => PsbValue::IntArray(arr),
            PsbValueFixed::String(s) => PsbValue::String(s),
            PsbValueFixed::List(l) => PsbValue::List(l.to_psb(warn_on_none)),
            PsbValueFixed::Object(o) => PsbValue::Object(o.to_psb(warn_on_none)),
            PsbValueFixed::Resource(r) => PsbValue::Resource(r),
            PsbValueFixed::ExtraResource(er) => PsbValue::ExtraResource(er),
            PsbValueFixed::CompilerNumber => PsbValue::CompilerNumber,
            PsbValueFixed::CompilerString => PsbValue::CompilerString,
            PsbValueFixed::CompilerResource => PsbValue::CompilerResource,
            PsbValueFixed::CompilerDecimal => PsbValue::CompilerDecimal,
            PsbValueFixed::CompilerArray => PsbValue::CompilerArray,
            PsbValueFixed::CompilerBool => PsbValue::CompilerBool,
            PsbValueFixed::CompilerBinaryTree => PsbValue::CompilerBinaryTree,
        }
    }

    /// Returns true if this value is a list.
    pub fn is_list(&self) -> bool {
        matches!(self, PsbValueFixed::List(_))
    }

    /// Returns true if this value is an object.
    pub fn is_object(&self) -> bool {
        matches!(self, PsbValueFixed::Object(_))
    }

    /// Returns true if this value is a string or null.
    pub fn is_string_or_null(&self) -> bool {
        self.is_string() || self.is_null()
    }

    /// Returns true if this value is a string.
    pub fn is_string(&self) -> bool {
        matches!(self, PsbValueFixed::String(_))
    }

    /// Returns true if this value is none.
    pub fn is_none(&self) -> bool {
        matches!(self, PsbValueFixed::None)
    }

    /// Returns true if this value is null.
    pub fn is_null(&self) -> bool {
        matches!(self, PsbValueFixed::Null)
    }

    /// Find the resource's key in object
    pub fn find_resource_key<'a>(
        &'a self,
        resource_id: u64,
        now: Vec<&'a str>,
    ) -> Option<Vec<&'a str>> {
        match self {
            PsbValueFixed::List(l) => l.find_resource_key(resource_id, now),
            PsbValueFixed::Object(o) => o.find_resource_key(resource_id, now),
            _ => None,
        }
    }

    /// Find the extra resource's key in object
    pub fn find_extra_resource_key<'a>(
        &'a self,
        extra_resource_id: u64,
        now: Vec<&'a str>,
    ) -> Option<Vec<&'a str>> {
        match self {
            PsbValueFixed::List(l) => l.find_extra_resource_key(extra_resource_id, now),
            PsbValueFixed::Object(o) => o.find_extra_resource_key(extra_resource_id, now),
            _ => None,
        }
    }

    /// Sets the value of this PSB value to a new integer.
    pub fn set_i64(&mut self, value: i64) {
        *self = PsbValueFixed::Number(PsbNumber::Integer(value));
    }

    /// Sets the value of this PSB value to a new object
    pub fn set_obj(&mut self, value: PsbObjectFixed) {
        *self = PsbValueFixed::Object(value);
    }

    /// Sets the value of this PSB value to a new string.
    pub fn set_str(&mut self, value: &str) {
        match self {
            PsbValueFixed::String(s) => {
                let s = s.string_mut();
                s.clear();
                s.push_str(value);
            }
            _ => {
                *self = PsbValueFixed::String(PsbString::from(value.to_owned()));
            }
        }
    }

    /// Sets the value of this PSB value to a new string.
    pub fn set_string(&mut self, value: String) {
        self.set_str(&value);
    }

    /// Returns the value as a boolean, if it is a boolean.
    pub fn as_u8(&self) -> Option<u8> {
        self.as_i64().map(|n| n.try_into().ok()).flatten()
    }

    /// Returns the value as a [u32], if it is a number.
    pub fn as_u32(&self) -> Option<u32> {
        self.as_i64().map(|n| n as u32)
    }

    /// Returns the value as a [i64], if it is a number.
    pub fn as_i64(&self) -> Option<i64> {
        match self {
            PsbValueFixed::Number(n) => match n {
                PsbNumber::Integer(n) => Some(*n),
                PsbNumber::Double(n) if n.fract() == 0.0 => Some(*n as i64),
                PsbNumber::Float(n) if n.fract() == 0.0 => Some(*n as i64),
                _ => None,
            },
            _ => None,
        }
    }

    /// Returns the value as a string, if it is a string.
    pub fn as_str(&self) -> Option<&str> {
        match self {
            PsbValueFixed::String(s) => Some(s.string()),
            _ => None,
        }
    }

    /// Returns the lengtho of a list or object.
    pub fn len(&self) -> usize {
        match self {
            PsbValueFixed::List(l) => l.len(),
            PsbValueFixed::Object(o) => o.values.len(),
            _ => 0,
        }
    }

    /// Returns a iterator over the entries of an object.
    pub fn entries(&self) -> ObjectIter<'_> {
        match self {
            PsbValueFixed::Object(o) => o.iter(),
            _ => ObjectIter::empty(),
        }
    }

    /// Returns a mutable iterator over the entries of an object.
    pub fn entries_mut(&mut self) -> ObjectIterMut<'_> {
        match self {
            PsbValueFixed::Object(o) => o.iter_mut(),
            _ => ObjectIterMut::empty(),
        }
    }

    /// Returns a iterator over the members of a list.
    pub fn members(&self) -> ListIter<'_> {
        match self {
            PsbValueFixed::List(l) => l.iter(),
            _ => ListIter::empty(),
        }
    }

    /// Returns a mutable iterator over the members of a list.
    pub fn members_mut(&mut self) -> ListIterMut<'_> {
        match self {
            PsbValueFixed::List(l) => l.iter_mut(),
            _ => ListIterMut::empty(),
        }
    }

    /// Pushes a new member to a list. If this value is not a list, it will be converted to a list.
    pub fn push_member<T: Into<PsbValueFixed>>(&mut self, value: T) {
        match self {
            PsbValueFixed::List(l) => {
                l.values.push(value.into());
            }
            _ => {
                *self = PsbValueFixed::List(PsbListFixed {
                    values: vec![value.into()],
                });
            }
        }
    }

    /// Clears all members in a list. If this value is not a list, it will be converted to an empty list.
    pub fn clear_members(&mut self) {
        match self {
            PsbValueFixed::List(l) => {
                l.clear();
            }
            _ => {
                *self = PsbValueFixed::List(PsbListFixed { values: vec![] });
            }
        }
    }

    /// Inserts a new member at the specified index in a list. If this value is not a list, it will be converted to a list.
    /// If the index is out of bounds, the value will be appended to the end of the list.
    pub fn insert_member<T: Into<PsbValueFixed>>(&mut self, index: usize, value: T) {
        match self {
            PsbValueFixed::List(l) => {
                l.insert(index, value);
            }
            _ => {
                *self = PsbValueFixed::List(PsbListFixed {
                    values: vec![value.into()],
                });
            }
        }
    }

    /// Returns the resource ID if this value is a resource reference.
    pub fn resource_id(&self) -> Option<u64> {
        match self {
            PsbValueFixed::Resource(r) => Some(r.resource_ref),
            _ => None,
        }
    }

    /// Returns the extra resource ID if this value is an extra resource reference.
    pub fn extra_resource_id(&self) -> Option<u64> {
        match self {
            PsbValueFixed::ExtraResource(er) => Some(er.extra_resource_ref),
            _ => None,
        }
    }

    /// Converts this value to a JSON value, if possible.
    #[cfg(feature = "json")]
    pub fn to_json(&self) -> Option<JsonValue> {
        match self {
            PsbValueFixed::Null => Some(JsonValue::Null),
            PsbValueFixed::Bool(b) => Some(JsonValue::Boolean(*b)),
            PsbValueFixed::Number(n) => match n {
                PsbNumber::Integer(i) => Some(JsonValue::Number((*i).into())),
                PsbNumber::Float(f) => Some(JsonValue::Number(f32_to_number(*f))),
                PsbNumber::Double(d) => Some(JsonValue::Number((*d).into())),
            },
            PsbValueFixed::String(s) => Some(JsonValue::String(s.string().to_owned())),
            PsbValueFixed::Resource(s) => {
                Some(JsonValue::String(format!("#resource#{}", s.resource_ref)))
            }
            PsbValueFixed::ExtraResource(s) => Some(JsonValue::String(format!(
                "#resource@{}",
                s.extra_resource_ref
            ))),
            PsbValueFixed::IntArray(arr) => Some(JsonValue::Array(
                arr.iter().map(|n| JsonValue::Number((*n).into())).collect(),
            )),
            PsbValueFixed::List(l) => Some(l.to_json()),
            PsbValueFixed::Object(o) => Some(o.to_json()),
            _ => None,
        }
    }

    /// Converts a JSON value to a PSB value.
    #[cfg(feature = "json")]
    pub fn from_json(obj: &JsonValue) -> Self {
        match obj {
            JsonValue::Null => PsbValueFixed::Null,
            JsonValue::Boolean(b) => PsbValueFixed::Bool(*b),
            JsonValue::Number(n) => {
                let data: f64 = (*n).into();
                if data.fract() == 0.0 {
                    PsbValueFixed::Number(PsbNumber::Integer(data as i64))
                } else {
                    PsbValueFixed::Number(PsbNumber::Float(data as f32))
                }
            }
            JsonValue::String(s) => {
                if s.starts_with("#resource#") {
                    if let Ok(id) = s[10..].parse::<u64>() {
                        return PsbValueFixed::Resource(PsbResourceRef { resource_ref: id });
                    }
                } else if s.starts_with("#resource@") {
                    if let Ok(id) = s[10..].parse::<u64>() {
                        return PsbValueFixed::ExtraResource(PsbExtraRef {
                            extra_resource_ref: id,
                        });
                    }
                }
                PsbValueFixed::String(PsbString::from(s.clone()))
            }
            JsonValue::Array(arr) => {
                let values: Vec<PsbValueFixed> = arr.iter().map(PsbValueFixed::from_json).collect();
                PsbValueFixed::List(PsbListFixed { values })
            }
            JsonValue::Object(obj) => {
                let mut values = BTreeMap::new();
                for (key, value) in obj.iter() {
                    values.insert(key.to_owned(), PsbValueFixed::from_json(value));
                }
                PsbValueFixed::Object(PsbObjectFixed { values })
            }
            JsonValue::Short(n) => {
                let s = n.as_str();
                if s.starts_with("#resource#") {
                    if let Ok(id) = s[10..].parse::<u64>() {
                        return PsbValueFixed::Resource(PsbResourceRef { resource_ref: id });
                    }
                } else if s.starts_with("#resource@") {
                    if let Ok(id) = s[10..].parse::<u64>() {
                        return PsbValueFixed::ExtraResource(PsbExtraRef {
                            extra_resource_ref: id,
                        });
                    }
                }
                PsbValueFixed::String(PsbString::from(s.to_owned()))
            }
        }
    }
}

impl Index<usize> for PsbValueFixed {
    type Output = PsbValueFixed;

    fn index(&self, index: usize) -> &Self::Output {
        match self {
            PsbValueFixed::List(l) => &l[index],
            _ => &NONE,
        }
    }
}

impl IndexMut<usize> for PsbValueFixed {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        match self {
            PsbValueFixed::List(l) => {
                if index < l.values.len() {
                    &mut l.values[index]
                } else {
                    l.values.push(NONE);
                    l.values.last_mut().unwrap()
                }
            }
            _ => {
                *self = PsbValueFixed::List(PsbListFixed { values: vec![NONE] });
                self.index_mut(0)
            }
        }
    }
}

impl<'a> Index<&'a str> for PsbValueFixed {
    type Output = PsbValueFixed;

    fn index(&self, index: &'a str) -> &Self::Output {
        match self {
            PsbValueFixed::Object(o) => &o[index],
            _ => &NONE,
        }
    }
}

impl<'a> Index<&'a String> for PsbValueFixed {
    type Output = PsbValueFixed;

    fn index(&self, index: &'a String) -> &Self::Output {
        self.index(index.as_str())
    }
}

impl Index<String> for PsbValueFixed {
    type Output = PsbValueFixed;

    fn index(&self, index: String) -> &Self::Output {
        self.index(index.as_str())
    }
}

impl IndexMut<&str> for PsbValueFixed {
    fn index_mut(&mut self, index: &str) -> &mut Self::Output {
        match self {
            PsbValueFixed::Object(o) => o.index_mut(index),
            _ => {
                *self = PsbValueFixed::Object(PsbObjectFixed {
                    values: BTreeMap::new(),
                });
                self.index_mut(index)
            }
        }
    }
}

impl IndexMut<&String> for PsbValueFixed {
    fn index_mut(&mut self, index: &String) -> &mut Self::Output {
        self.index_mut(index.as_str())
    }
}

impl IndexMut<String> for PsbValueFixed {
    fn index_mut(&mut self, index: String) -> &mut Self::Output {
        self.index_mut(index.as_str())
    }
}

impl Clone for PsbValueFixed {
    fn clone(&self) -> Self {
        match self {
            PsbValueFixed::None => PsbValueFixed::None,
            PsbValueFixed::Null => PsbValueFixed::Null,
            PsbValueFixed::Bool(b) => PsbValueFixed::Bool(*b),
            PsbValueFixed::Number(n) => PsbValueFixed::Number(n.clone()),
            PsbValueFixed::IntArray(arr) => PsbValueFixed::IntArray(arr.clone()),
            PsbValueFixed::String(s) => PsbValueFixed::String(PsbString::from(s.string().clone())),
            PsbValueFixed::List(l) => PsbValueFixed::List(l.clone()),
            PsbValueFixed::Object(o) => PsbValueFixed::Object(o.clone()),
            PsbValueFixed::Resource(r) => PsbValueFixed::Resource(r.clone()),
            PsbValueFixed::ExtraResource(er) => PsbValueFixed::ExtraResource(er.clone()),
            PsbValueFixed::CompilerNumber => PsbValueFixed::CompilerNumber,
            PsbValueFixed::CompilerString => PsbValueFixed::CompilerString,
            PsbValueFixed::CompilerResource => PsbValueFixed::CompilerResource,
            PsbValueFixed::CompilerDecimal => PsbValueFixed::CompilerDecimal,
            PsbValueFixed::CompilerArray => PsbValueFixed::CompilerArray,
            PsbValueFixed::CompilerBool => PsbValueFixed::CompilerBool,
            PsbValueFixed::CompilerBinaryTree => PsbValueFixed::CompilerBinaryTree,
        }
    }
}

impl PartialEq<String> for PsbValueFixed {
    fn eq(&self, other: &String) -> bool {
        self == other.as_str()
    }
}

impl PartialEq<str> for PsbValueFixed {
    fn eq(&self, other: &str) -> bool {
        match self {
            PsbValueFixed::String(s) => s.string() == other,
            _ => false,
        }
    }
}

impl<'a> PartialEq<&'a str> for PsbValueFixed {
    fn eq(&self, other: &&'a str) -> bool {
        self == *other
    }
}

/// Trait to convert a PSB value to a fixed PSB value.
pub trait PsbValueExt {
    /// Converts this PSB value to a fixed PSB value.
    fn to_psb_fixed(self) -> PsbValueFixed;
}

impl PsbValueExt for PsbValue {
    fn to_psb_fixed(self) -> PsbValueFixed {
        match self {
            PsbValue::None => PsbValueFixed::None,
            PsbValue::Null => PsbValueFixed::Null,
            PsbValue::Bool(b) => PsbValueFixed::Bool(b),
            PsbValue::Number(n) => PsbValueFixed::Number(n),
            PsbValue::IntArray(arr) => PsbValueFixed::IntArray(arr),
            PsbValue::String(s) => PsbValueFixed::String(s),
            PsbValue::List(l) => PsbValueFixed::List(PsbList::to_psb_fixed(l)),
            PsbValue::Object(o) => PsbValueFixed::Object(PsbObject::to_psb_fixed(o)),
            PsbValue::Resource(r) => PsbValueFixed::Resource(r),
            PsbValue::ExtraResource(er) => PsbValueFixed::ExtraResource(er),
            PsbValue::CompilerNumber => PsbValueFixed::CompilerNumber,
            PsbValue::CompilerString => PsbValueFixed::CompilerString,
            PsbValue::CompilerResource => PsbValueFixed::CompilerResource,
            PsbValue::CompilerDecimal => PsbValueFixed::CompilerDecimal,
            PsbValue::CompilerArray => PsbValueFixed::CompilerArray,
            PsbValue::CompilerBool => PsbValueFixed::CompilerBool,
            PsbValue::CompilerBinaryTree => PsbValueFixed::CompilerBinaryTree,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(transparent)]
/// Represents a PSB list of PSB values.
pub struct PsbListFixed {
    /// The values in the list.
    pub values: Vec<PsbValueFixed>,
}

impl PsbListFixed {
    pub fn new() -> Self {
        PsbListFixed { values: vec![] }
    }

    /// Converts this PSB list to a original PSB list.
    pub fn to_psb(self, warn_on_none: bool) -> PsbList {
        let v: Vec<_> = self
            .values
            .into_iter()
            .map(|v| v.to_psb(warn_on_none))
            .collect();
        PsbList::from(v)
    }

    /// Find the resource's key in object
    pub fn find_resource_key<'a>(
        &'a self,
        resource_id: u64,
        now: Vec<&'a str>,
    ) -> Option<Vec<&'a str>> {
        for value in &self.values {
            if let Some(key) = value.find_resource_key(resource_id, now.clone()) {
                return Some(key);
            }
        }
        None
    }

    /// Find the extra resource's key in object
    pub fn find_extra_resource_key<'a>(
        &'a self,
        extra_resource_id: u64,
        now: Vec<&'a str>,
    ) -> Option<Vec<&'a str>> {
        for value in &self.values {
            if let Some(key) = value.find_extra_resource_key(extra_resource_id, now.clone()) {
                return Some(key);
            }
        }
        None
    }

    /// Returns a iterator over the values in the list.
    pub fn iter(&self) -> ListIter<'_> {
        ListIter {
            inner: self.values.iter(),
        }
    }

    /// Returns a mutable iterator over the values in the list.
    pub fn iter_mut(&mut self) -> ListIterMut<'_> {
        ListIterMut {
            inner: self.values.iter_mut(),
        }
    }

    /// Returns a reference to the values in the list.
    pub fn values(&self) -> &Vec<PsbValueFixed> {
        &self.values
    }

    /// Returns the length of the list.
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// Clears all values in the list.
    pub fn clear(&mut self) {
        self.values.clear();
    }

    /// Inserts a new value at the specified index in the list.
    /// If the index is out of bounds, the value will be appended to the end of the list.
    pub fn insert<V: Into<PsbValueFixed>>(&mut self, index: usize, value: V) {
        if index <= self.values.len() {
            self.values.insert(index, value.into());
        } else {
            self.values.push(value.into());
        }
    }

    /// Converts this PSB list to a JSON value.
    #[cfg(feature = "json")]
    pub fn to_json(&self) -> JsonValue {
        let data: Vec<_> = self.values.iter().filter_map(|v| v.to_json()).collect();
        JsonValue::Array(data)
    }
}

impl Index<usize> for PsbListFixed {
    type Output = PsbValueFixed;

    fn index(&self, index: usize) -> &Self::Output {
        self.values.get(index).unwrap_or(&NONE)
    }
}

impl IndexMut<usize> for PsbListFixed {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        if index < self.values.len() {
            &mut self.values[index]
        } else {
            self.values.push(NONE);
            self.values.last_mut().unwrap()
        }
    }
}

/// Iterator for a slice of PSB values in a list.
pub struct ListIter<'a> {
    inner: std::slice::Iter<'a, PsbValueFixed>,
}

impl<'a> ListIter<'a> {
    /// Creates an empty iterator.
    pub fn empty() -> Self {
        ListIter {
            inner: Default::default(),
        }
    }
}

impl<'a> Iterator for ListIter<'a> {
    type Item = &'a PsbValueFixed;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}

impl<'a> ExactSizeIterator for ListIter<'a> {
    fn len(&self) -> usize {
        self.inner.len()
    }
}

impl<'a> DoubleEndedIterator for ListIter<'a> {
    #[inline(always)]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.inner.next_back()
    }
}

/// Mutable iterator for a slice of PSB values in a list.
pub struct ListIterMut<'a> {
    inner: std::slice::IterMut<'a, PsbValueFixed>,
}

impl<'a> ListIterMut<'a> {
    /// Creates an empty mutable iterator.
    pub fn empty() -> Self {
        ListIterMut {
            inner: Default::default(),
        }
    }
}

impl<'a> Iterator for ListIterMut<'a> {
    type Item = &'a mut PsbValueFixed;

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}

impl<'a> ExactSizeIterator for ListIterMut<'a> {
    fn len(&self) -> usize {
        self.inner.len()
    }
}

impl<'a> DoubleEndedIterator for ListIterMut<'a> {
    #[inline(always)]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.inner.next_back()
    }
}

/// Trait to convert a PSB list to a fixed PSB list.
pub trait PsbListExt {
    /// Converts this PSB list to a fixed PSB list.
    fn to_psb_fixed(self) -> PsbListFixed;
}

impl PsbListExt for PsbList {
    fn to_psb_fixed(self) -> PsbListFixed {
        let values: Vec<_> = self
            .unwrap()
            .into_iter()
            .map(PsbValue::to_psb_fixed)
            .collect();
        PsbListFixed { values }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(transparent)]
/// Represents a PSB object with key-value pairs.
pub struct PsbObjectFixed {
    /// The key-value pairs in the object.
    pub values: BTreeMap<String, PsbValueFixed>,
}

impl PsbObjectFixed {
    pub fn new() -> Self {
        Self {
            values: BTreeMap::new(),
        }
    }

    /// Creates a new empty PSB object.
    pub fn to_psb(self, warn_on_none: bool) -> PsbObject {
        let mut hash_map = HashMap::new();
        for (key, value) in self.values {
            hash_map.insert(key, value.to_psb(warn_on_none));
        }
        PsbObject::from(hash_map)
    }

    /// Gets a reference of value in the object by key.
    pub fn get_value(&self, key: &str) -> Option<&PsbValueFixed> {
        self.values.get(key)
    }

    /// Find the resource's key in object
    pub fn find_resource_key<'a>(
        &'a self,
        resource_id: u64,
        now: Vec<&'a str>,
    ) -> Option<Vec<&'a str>> {
        for (key, value) in &self.values {
            let mut now = now.clone();
            now.push(key);
            if let Some(id) = value.resource_id() {
                if id == resource_id {
                    return Some(now);
                }
            }
            if let Some(key) = value.find_resource_key(resource_id, now) {
                return Some(key);
            }
        }
        None
    }

    /// Find the extra resource's key in object
    pub fn find_extra_resource_key<'a>(
        &'a self,
        extra_resource_id: u64,
        now: Vec<&'a str>,
    ) -> Option<Vec<&'a str>> {
        for (key, value) in &self.values {
            let mut now = now.clone();
            now.push(key);
            if let Some(id) = value.extra_resource_id() {
                if id == extra_resource_id {
                    return Some(now);
                }
            }
            if let Some(key) = value.find_extra_resource_key(extra_resource_id, now) {
                return Some(key);
            }
        }
        None
    }

    /// Returns a iterator over the entries of the object.
    pub fn iter(&self) -> ObjectIter<'_> {
        ObjectIter {
            inner: self.values.iter(),
        }
    }

    /// Returns a mutable iterator over the entries of the object.
    pub fn iter_mut(&mut self) -> ObjectIterMut<'_> {
        ObjectIterMut {
            inner: self.values.iter_mut(),
        }
    }

    /// Converts this PSB object to a JSON value.
    #[cfg(feature = "json")]
    pub fn to_json(&self) -> JsonValue {
        let mut obj = json::object::Object::new();
        for (key, value) in &self.values {
            if let Some(json_value) = value.to_json() {
                obj.insert(key, json_value);
            }
        }
        JsonValue::Object(obj)
    }

    /// Converts a JSON object to a PSB object.
    #[cfg(feature = "json")]
    pub fn from_json(obj: &JsonValue) -> Self {
        let mut values = BTreeMap::new();
        for (key, value) in obj.entries() {
            values.insert(key.to_owned(), PsbValueFixed::from_json(value));
        }
        PsbObjectFixed { values }
    }
}

impl<'a> Index<&'a str> for PsbObjectFixed {
    type Output = PsbValueFixed;

    fn index(&self, index: &'a str) -> &Self::Output {
        self.values.get(index).unwrap_or(&NONE)
    }
}

impl<'a> Index<&'a String> for PsbObjectFixed {
    type Output = PsbValueFixed;

    fn index(&self, index: &'a String) -> &Self::Output {
        self.index(index.as_str())
    }
}

impl Index<String> for PsbObjectFixed {
    type Output = PsbValueFixed;

    fn index(&self, index: String) -> &Self::Output {
        self.index(index.as_str())
    }
}

impl<'a> IndexMut<&'a str> for PsbObjectFixed {
    fn index_mut(&mut self, index: &'a str) -> &mut Self::Output {
        self.values.entry(index.to_string()).or_insert(NONE)
    }
}

impl<'a> IndexMut<&'a String> for PsbObjectFixed {
    fn index_mut(&mut self, index: &'a String) -> &mut Self::Output {
        self.index_mut(index.as_str())
    }
}

impl IndexMut<String> for PsbObjectFixed {
    fn index_mut(&mut self, index: String) -> &mut Self::Output {
        self.values.entry(index).or_insert(NONE)
    }
}

/// Trait to convert a PSB object to a fixed PSB object.
pub trait PsbObjectExt {
    /// Converts this PSB object to a fixed PSB object.
    fn to_psb_fixed(self) -> PsbObjectFixed;
}

impl PsbObjectExt for PsbObject {
    fn to_psb_fixed(self) -> PsbObjectFixed {
        let mut hash_map = BTreeMap::new();
        for (key, value) in self.unwrap() {
            hash_map.insert(key, PsbValue::to_psb_fixed(value));
        }
        PsbObjectFixed { values: hash_map }
    }
}

/// Iterator for a slice of PSB values in an object.
pub struct ObjectIter<'a> {
    inner: std::collections::btree_map::Iter<'a, String, PsbValueFixed>,
}

impl<'a> ObjectIter<'a> {
    /// Creates an empty iterator.
    pub fn empty() -> Self {
        ObjectIter {
            inner: Default::default(),
        }
    }
}

impl<'a> Iterator for ObjectIter<'a> {
    type Item = (&'a String, &'a PsbValueFixed);

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}

impl<'a> ExactSizeIterator for ObjectIter<'a> {
    fn len(&self) -> usize {
        self.inner.len()
    }
}

impl<'a> DoubleEndedIterator for ObjectIter<'a> {
    #[inline(always)]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.inner.next_back()
    }
}

/// Mutable iterator for a slice of PSB values in an object.
pub struct ObjectIterMut<'a> {
    inner: std::collections::btree_map::IterMut<'a, String, PsbValueFixed>,
}

impl<'a> ObjectIterMut<'a> {
    /// Creates an empty mutable iterator.
    pub fn empty() -> Self {
        ObjectIterMut {
            inner: Default::default(),
        }
    }
}

impl<'a> Iterator for ObjectIterMut<'a> {
    type Item = (&'a String, &'a mut PsbValueFixed);

    #[inline(always)]
    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }
}

impl<'a> ExactSizeIterator for ObjectIterMut<'a> {
    fn len(&self) -> usize {
        self.inner.len()
    }
}

impl<'a> DoubleEndedIterator for ObjectIterMut<'a> {
    #[inline(always)]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.inner.next_back()
    }
}

/// Represents a fixed version of a virtual PSB.
#[derive(Clone, Debug)]
pub struct VirtualPsbFixed {
    header: PsbHeader,
    resources: Vec<Vec<u8>>,
    extra: Vec<Vec<u8>>,
    root: PsbObjectFixed,
}

impl Serialize for VirtualPsbFixed {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut state = serializer.serialize_struct("VirtualPsbFixed", 3)?;
        state.serialize_field("version", &self.header.version)?;
        state.serialize_field("encryption", &self.header.encryption)?;
        state.serialize_field("data", &self.root)?;
        state.end()
    }
}

#[derive(Deserialize)]
pub struct VirtualPsbFixedData {
    version: u16,
    encryption: u16,
    data: PsbObjectFixed,
}

impl VirtualPsbFixed {
    /// Creates a new fixed virtual PSB.
    pub fn new(
        header: PsbHeader,
        resources: Vec<Vec<u8>>,
        extra: Vec<Vec<u8>>,
        root: PsbObjectFixed,
    ) -> Self {
        Self {
            header,
            resources,
            extra,
            root,
        }
    }

    /// Returns the header of the PSB.
    pub fn header(&self) -> PsbHeader {
        self.header
    }

    /// Return a mutable reference to the header of the PSB.
    pub fn header_mut(&mut self) -> &mut PsbHeader {
        &mut self.header
    }

    /// Returns a reference to the resources of the PSB.
    pub fn resources(&self) -> &Vec<Vec<u8>> {
        &self.resources
    }

    /// Returns a mutable reference to the resources of the PSB.
    pub fn resources_mut(&mut self) -> &mut Vec<Vec<u8>> {
        &mut self.resources
    }

    /// Returns a reference to the extra resources of the PSB.
    pub fn extra(&self) -> &Vec<Vec<u8>> {
        &self.extra
    }

    /// Returns a mutable reference to the extra resources of the PSB.
    pub fn extra_mut(&mut self) -> &mut Vec<Vec<u8>> {
        &mut self.extra
    }

    /// Returns a reference to the root object of the PSB.
    pub fn root(&self) -> &PsbObjectFixed {
        &self.root
    }

    /// Returns a mutable reference to the root object of the PSB.
    pub fn root_mut(&mut self) -> &mut PsbObjectFixed {
        &mut self.root
    }

    /// Sets the root of the PSB.
    pub fn set_root(&mut self, root: PsbObjectFixed) {
        self.root = root;
    }

    /// Unwraps the PSB into its components.
    pub fn unwrap(self) -> (PsbHeader, Vec<Vec<u8>>, Vec<Vec<u8>>, PsbObjectFixed) {
        (self.header, self.resources, self.extra, self.root)
    }

    /// Converts this fixed PSB to a virtual PSB.
    pub fn to_psb(self, warn_on_none: bool) -> VirtualPsb {
        let (header, resources, extra, root) = self.unwrap();
        VirtualPsb::new(header, resources, extra, root.to_psb(warn_on_none))
    }

    /// Converts json object to a fixed PSB.
    #[cfg(feature = "json")]
    pub fn from_json(&mut self, obj: &JsonValue) -> Result<(), anyhow::Error> {
        let version = obj["version"]
            .as_u16()
            .ok_or_else(|| anyhow::anyhow!("Invalid PSB version"))?;
        let encryption = obj["encryption"]
            .as_u16()
            .ok_or_else(|| anyhow::anyhow!("Invalid PSB encryption"))?;
        self.header.version = version;
        self.header.encryption = encryption;
        self.root = PsbObjectFixed::from_json(&obj["data"]);
        Ok(())
    }

    #[cfg(feature = "json")]
    /// Creates a fixed PSB from a JSON object.
    pub fn with_json(obj: &JsonValue) -> Result<Self, anyhow::Error> {
        let version = obj["version"]
            .as_u16()
            .ok_or_else(|| anyhow::anyhow!("Invalid PSB version"))?;
        let encryption = obj["encryption"]
            .as_u16()
            .ok_or_else(|| anyhow::anyhow!("Invalid PSB encryption"))?;
        let root = PsbObjectFixed::from_json(&obj["data"]);
        Ok(Self {
            header: PsbHeader {
                version,
                encryption,
            },
            resources: Vec::new(),
            extra: Vec::new(),
            root,
        })
    }

    pub fn set_data(&mut self, data: VirtualPsbFixedData) {
        self.header.version = data.version;
        self.header.encryption = data.encryption;
        self.root = data.data;
    }

    /// Converts this fixed PSB to a JSON object.
    #[cfg(feature = "json")]
    pub fn to_json(&self) -> JsonValue {
        json::object! {
            "version": self.header.version,
            "encryption": self.header.encryption,
            "data": self.root.to_json(),
        }
    }
}

/// Trait to convert a virtual PSB to a fixed PSB.
pub trait VirtualPsbExt {
    /// Converts this virtual PSB to a fixed PSB.
    fn to_psb_fixed(self) -> VirtualPsbFixed;
}

impl VirtualPsbExt for VirtualPsb {
    fn to_psb_fixed(self) -> VirtualPsbFixed {
        let (header, resources, extra, root) = self.unwrap();
        VirtualPsbFixed::new(header, resources, extra, root.to_psb_fixed())
    }
}

/// Trait to extend PSB writer behavior.
pub trait PsbWriterExt {
    /// Writes PSB with a v4-compatible resource layout.
    fn finish_v4<T: Write + Seek>(self, stream: T) -> std::result::Result<u64, PsbError>;
}

impl PsbWriterExt for VirtualPsb {
    fn finish_v4<T: Write + Seek>(self, mut stream: T) -> std::result::Result<u64, PsbError> {
        let file_start = stream.seek(SeekFrom::Current(0))?;

        let (header, resources, extra, root) = self.unwrap();

        stream.write_u32(PSB_SIGNATURE)?;
        header.write_bytes(&mut stream)?;

        let offsets_end_pos = stream.seek(SeekFrom::Current(0))? - file_start;
        stream.write_u32(0)?;

        let offset_start_pos = stream.seek(SeekFrom::Current(0))? - file_start;
        let mut offsets = PsbOffsets::default();

        offsets.write_bytes(header.version, &mut stream)?;
        let offsets_end = stream.seek(SeekFrom::Current(0))? - file_start;

        let refs = {
            let mut names = Vec::new();
            let mut strings = Vec::new();

            root.collect_names(&mut names);
            root.collect_strings(&mut strings);

            names.sort();
            strings.sort();

            PsbRefs::new(names, strings)
        };

        offsets.name_offset = (stream.seek(SeekFrom::Current(0))? - file_start) as u32;
        PsbWriter::write_names(refs.names(), &mut stream)?;

        offsets.entry_point = (stream.seek(SeekFrom::Current(0))? - file_start) as u32;
        PsbValue::Object(root).write_bytes_refs(&mut stream, &refs)?;

        let (_, string_offsets) = PsbWriter::write_strings(refs.strings(), &mut stream)?;
        offsets.strings = string_offsets;

        // For v4, write extra resources before normal resources to keep compatibility
        // with tools expecting the FreeMote layout.
        if header.version > 3 {
            let (_, extra_offsets) = PsbWriter::write_resources(&extra, &mut stream)?;
            offsets.extra = Some(extra_offsets);
        }

        let (_, res_offsets) = PsbWriter::write_resources(&resources, &mut stream)?;
        offsets.resources = res_offsets;

        let file_end = stream.seek(SeekFrom::Current(0))?;

        stream.seek(SeekFrom::Start(offsets_end_pos))?;
        stream.write_u32(offsets_end as u32)?;

        if header.version > 2 {
            let mut adler = Adler32::new();

            adler.write_slice(&(offset_start_pos as u32).to_le_bytes());
            adler.write_slice(&offsets.name_offset.to_le_bytes());
            adler.write_slice(&offsets.strings.offset_pos.to_le_bytes());
            adler.write_slice(&offsets.strings.data_pos.to_le_bytes());
            adler.write_slice(&offsets.resources.offset_pos.to_le_bytes());
            adler.write_slice(&offsets.resources.lengths_pos.to_le_bytes());
            adler.write_slice(&offsets.resources.data_pos.to_le_bytes());
            adler.write_slice(&offsets.entry_point.to_le_bytes());

            offsets.checksum = Some(adler.checksum());
        }

        stream.seek(SeekFrom::Start(offset_start_pos))?;
        offsets.write_bytes(header.version, &mut stream)?;
        stream.seek(SeekFrom::Start(file_end))?;

        Ok(file_end - file_start)
    }
}

/// Trait to extend PSB reader functionality.
pub trait PsbReaderExt {
    /// Opens a PSB v2 file from a stream, handling other formats like LZ4 compression.
    fn open_psb_v2<T: Read + Seek>(stream: T) -> Result<VirtualPsb>;
}

const LZ4_SIGNATURE: u32 = 0x184D2204;
const PSB_SIGNATURE: u32 = emote_psb::PSB_SIGNATURE;
const MDF_SIGNATURE: u32 = emote_psb::PSB_MDF_SIGNATURE;
const PSB_TYPE_INTEGER_ARRAY_N: u8 = 0x0C;
const PSB_TYPE_STRING_N: u8 = 0x14;
const PSB_TYPE_RESOURCE_N: u8 = 0x18;
const PSB_TYPE_LIST: u8 = 0x20;
const PSB_TYPE_OBJECT: u8 = 0x21;
const PSB_TYPE_EXTRA_N: u8 = 0x21;

fn is_psb_array_type(value: u8) -> bool {
    (PSB_TYPE_INTEGER_ARRAY_N + 1..=PSB_TYPE_INTEGER_ARRAY_N + 8).contains(&value)
}

fn read_int_array<R: Read + Seek>(stream: &mut R) -> Result<Vec<u64>> {
    let (_, value) = PsbValue::from_bytes(stream)
        .map_err(|e| anyhow::anyhow!("Failed to read PSB array: {:?}", e))?;
    match value {
        PsbValue::IntArray(arr) => Ok(arr.unwrap()),
        _ => Err(anyhow::anyhow!("Expected PSB int array")),
    }
}

fn skip_psb_value<R: Read + Seek>(stream: &mut R) -> Result<u64> {
    let start = stream.seek(SeekFrom::Current(0))?;
    let value_type = stream.read_u8()?;
    match value_type {
        PSB_TYPE_LIST => {
            let ref_offsets = read_int_array(stream)?;
            if ref_offsets.is_empty() {
                return Ok(stream.seek(SeekFrom::Current(0))? - start);
            }
            let mut max_end = 0_u64;
            let data_start = stream.seek(SeekFrom::Current(0))?;
            for offset in ref_offsets {
                stream.seek(SeekFrom::Start(data_start + offset))?;
                let read = skip_psb_value(stream)?;
                max_end = max_end.max(offset + read);
            }
            stream.seek(SeekFrom::Start(data_start + max_end))?;
            Ok(stream.seek(SeekFrom::Current(0))? - start)
        }
        PSB_TYPE_OBJECT => {
            let _ = read_int_array(stream)?;
            let ref_offsets = read_int_array(stream)?;
            if ref_offsets.is_empty() {
                return Ok(stream.seek(SeekFrom::Current(0))? - start);
            }
            let mut max_end = 0_u64;
            let data_start = stream.seek(SeekFrom::Current(0))?;
            for offset in ref_offsets {
                stream.seek(SeekFrom::Start(data_start + offset))?;
                let read = skip_psb_value(stream)?;
                max_end = max_end.max(offset + read);
            }
            stream.seek(SeekFrom::Start(data_start + max_end))?;
            Ok(stream.seek(SeekFrom::Current(0))? - start)
        }
        // String/resource/extra references are encoded as fixed-width indexes.
        _ if (PSB_TYPE_STRING_N + 1..=PSB_TYPE_STRING_N + 4).contains(&value_type)
            || (PSB_TYPE_RESOURCE_N + 1..=PSB_TYPE_RESOURCE_N + 4).contains(&value_type)
            || (PSB_TYPE_EXTRA_N + 1..=PSB_TYPE_EXTRA_N + 4).contains(&value_type) =>
        {
            let n = if value_type <= PSB_TYPE_STRING_N + 4 {
                value_type - PSB_TYPE_STRING_N
            } else if value_type <= PSB_TYPE_RESOURCE_N + 4 {
                value_type - PSB_TYPE_RESOURCE_N
            } else {
                value_type - PSB_TYPE_EXTRA_N
            };
            stream.seek(SeekFrom::Current(n as i64))?;
            Ok(stream.seek(SeekFrom::Current(0))? - start)
        }
        _ => {
            stream.seek(SeekFrom::Start(start))?;
            let (read, _) = PsbValue::from_bytes(stream)
                .map_err(|e| anyhow::anyhow!("Failed to skip PSB value: {:?}", e))?;
            Ok(read)
        }
    }
}

fn detect_name_pos(data: &[u8]) -> Option<usize> {
    if data.len() < 8 {
        return None;
    }
    let mut name_pos = None;
    for i in 0..(data.len() - 7) {
        if data[i] == 0x0D || data[i] == 0x0E {
            let offset1 = (data[i] - 0x0B) as usize;
            if i + offset1 < data.len() {
                if data[i + offset1] == 0x0E
                    && i + 7 < data.len()
                    && data[i + 4..i + 8] == [1, 0, 0, 0]
                {
                    name_pos = Some(i);
                    break;
                }
                if data[i + offset1] == 0x0D
                    && i + offset1 + 2 < data.len()
                    && data[i + offset1 + 1..i + offset1 + 3] == [1, 0]
                {
                    name_pos = Some(i);
                    break;
                }
            }
        }
    }
    name_pos
}

fn load_psb_dullahan<R: Read + Seek>(mut stream: R) -> Result<VirtualPsb> {
    let mut probe = vec![0_u8; 1024];
    stream.seek(SeekFrom::Start(0))?;
    let probe_len = stream.read(&mut probe)?;
    probe.truncate(probe_len);

    let name_pos = detect_name_pos(&probe)
        .ok_or_else(|| anyhow::anyhow!("Dullahan fallback: cannot find names segment"))?
        as u64;

    let version = stream.peek_u16_at(4)?;
    let encryption = stream.peek_u16_at(6)?;
    let mut header = PsbHeader {
        version,
        encryption,
    };

    stream.seek(SeekFrom::Start(name_pos))?;
    let (_, names) = PsbReader::read_names(&mut stream)
        .map_err(|e| anyhow::anyhow!("Dullahan fallback: failed to read names: {:?}", e))?;

    let file_len = stream.stream_length()?;
    let mut entry_point = None;
    while stream.seek(SeekFrom::Current(0))? < file_len {
        let b = stream.read_u8()?;
        if b == PSB_TYPE_LIST || b == PSB_TYPE_OBJECT {
            entry_point = Some(stream.seek(SeekFrom::Current(-1))? as u32);
            break;
        }
    }
    let entry_point = entry_point
        .ok_or_else(|| anyhow::anyhow!("Dullahan fallback: cannot find root object/list"))?;

    stream.seek(SeekFrom::Start(entry_point as u64))?;
    let _ = skip_psb_value(&mut stream)?;

    let mut strings_offset_pos = None;
    while stream.seek(SeekFrom::Current(0))? < file_len {
        let b = stream.read_u8()?;
        if is_psb_array_type(b) {
            strings_offset_pos = Some(stream.seek(SeekFrom::Current(-1))? as u32);
            break;
        }
    }
    let strings_offset_pos = strings_offset_pos
        .ok_or_else(|| anyhow::anyhow!("Dullahan fallback: cannot find strings offset table"))?;

    stream.seek(SeekFrom::Start(strings_offset_pos as u64))?;
    let string_offsets = read_int_array(&mut stream)?;
    let strings_data_pos = stream.seek(SeekFrom::Current(0))? as u32;

    stream.seek(SeekFrom::Start(strings_offset_pos as u64))?;
    let (_, strings) = PsbReader::read_strings(strings_data_pos, &mut stream)
        .map_err(|e| anyhow::anyhow!("Dullahan fallback: failed to read strings: {:?}", e))?;

    let mut strings_data_end = strings_data_pos as u64;
    for (idx, offset) in string_offsets.iter().enumerate() {
        if let Some(s) = strings.get(idx) {
            strings_data_end = strings_data_end
                .max(strings_data_pos as u64 + *offset + s.as_bytes().len() as u64 + 1);
        }
    }

    stream.seek(SeekFrom::Start(strings_data_end.min(file_len)))?;
    let mut resource_array_start = None;
    while stream.seek(SeekFrom::Current(0))? < file_len {
        let b = stream.read_u8()?;
        if is_psb_array_type(b) {
            resource_array_start = Some(stream.seek(SeekFrom::Current(-1))? as u32);
            break;
        }
    }

    let mut resources = PsbResourcesOffset::default();
    let mut extra: Option<PsbResourcesOffset> = None;

    if let Some(pos1) = resource_array_start {
        stream.seek(SeekFrom::Start(pos1 as u64))?;
        let array1 = read_int_array(&mut stream)?;
        let pos2 = stream.seek(SeekFrom::Current(0))? as u32;
        let array2 = read_int_array(&mut stream)?;

        let after_array2 = stream.seek(SeekFrom::Current(0))?;
        let mut probe_next = 0_u8;
        let mut has_next = false;
        if after_array2 < file_len {
            probe_next = stream.read_u8()?;
            stream.seek(SeekFrom::Current(-1))?;
            has_next = true;
        }

        if has_next && (version >= 4 || is_psb_array_type(probe_next)) {
            header.version = 4;
            let mut extra_table = PsbResourcesOffset {
                offset_pos: pos1,
                lengths_pos: pos2,
                data_pos: after_array2 as u32,
            };

            if !array1.is_empty() && !array2.is_empty() {
                let max_idx = array1
                    .iter()
                    .enumerate()
                    .max_by_key(|(_, offset)| *offset)
                    .map(|(idx, _)| idx)
                    .unwrap_or(0);
                let should_be_len = array1[max_idx] + array2[max_idx];
                let detect_start = after_array2.saturating_add(should_be_len);
                let detect_end = (detect_start + 1024).min(file_len);

                let mut found = false;
                let mut cursor = detect_start;
                while cursor < detect_end {
                    stream.seek(SeekFrom::Start(cursor))?;
                    let b = stream.read_u8()?;
                    if !is_psb_array_type(b) {
                        cursor += 1;
                        continue;
                    }

                    stream.seek(SeekFrom::Start(cursor))?;
                    if read_int_array(&mut stream).is_ok() {
                        let off_pos = cursor as u32;
                        let len_pos = stream.seek(SeekFrom::Current(0))? as u32;
                        if read_int_array(&mut stream).is_ok() {
                            extra_table.data_pos = off_pos.saturating_sub(should_be_len as u32);
                            resources.offset_pos = off_pos;
                            resources.lengths_pos = len_pos;
                            resources.data_pos = stream.seek(SeekFrom::Current(0))? as u32;
                            found = true;
                            break;
                        }
                    }

                    cursor += 1;
                }

                if !found {
                    return Err(anyhow::anyhow!(
                        "Dullahan fallback: cannot find resource offset/length tables"
                    ));
                }
            } else {
                stream.seek(SeekFrom::Start(after_array2))?;
                while stream.seek(SeekFrom::Current(0))? < file_len {
                    let b = stream.read_u8()?;
                    if is_psb_array_type(b) {
                        stream.seek(SeekFrom::Current(-1))?;
                        resources.offset_pos = stream.seek(SeekFrom::Current(0))? as u32;
                        let _ = read_int_array(&mut stream)?;
                        resources.lengths_pos = stream.seek(SeekFrom::Current(0))? as u32;
                        let _ = read_int_array(&mut stream)?;
                        resources.data_pos = stream.seek(SeekFrom::Current(0))? as u32;
                        break;
                    }
                }
            }

            extra = Some(extra_table);
        } else {
            resources.offset_pos = pos1;
            resources.lengths_pos = pos2;
            resources.data_pos = after_array2 as u32;
        }

        stream.seek(SeekFrom::Start(resources.offset_pos as u64))?;
        let chunk_offsets = read_int_array(&mut stream).unwrap_or_default();
        stream.seek(SeekFrom::Start(resources.lengths_pos as u64))?;
        let chunk_lengths = read_int_array(&mut stream).unwrap_or_default();
        if !chunk_offsets.is_empty() && !chunk_lengths.is_empty() {
            let current_pos = resources.data_pos as u64;
            if current_pos <= file_len {
                let remain_length = file_len - current_pos;
                let max_idx = chunk_offsets
                    .iter()
                    .enumerate()
                    .max_by_key(|(_, offset)| *offset)
                    .map(|(idx, _)| idx)
                    .unwrap_or(0);
                let should_be_len = chunk_offsets[max_idx] + chunk_lengths[max_idx];
                let padding = remain_length.saturating_sub(should_be_len);
                resources.data_pos = current_pos.saturating_add(padding) as u32;
            }
        }
    }

    let refs = PsbRefs::new(names, strings);
    let offsets = PsbOffsets {
        name_offset: name_pos as u32,
        strings: PsbStringOffset {
            offset_pos: strings_offset_pos,
            data_pos: strings_data_pos,
        },
        resources,
        entry_point,
        checksum: Some(0),
        extra,
    };

    stream.seek(SeekFrom::Start(0))?;
    let mut file = PsbFile::new(header, refs, offsets, stream);
    file.load()
        .map_err(|e| anyhow::anyhow!("Dullahan fallback: failed to load PSB: {:?}", e))
}

impl PsbReaderExt for PsbReader {
    fn open_psb_v2<T: Read + Seek>(mut stream: T) -> Result<VirtualPsb> {
        let signature = stream.peek_u32_at(0)?;
        if signature == LZ4_SIGNATURE {
            let mut decoder = lz4::Decoder::new(stream)?;
            let mut mem_stream = MemWriter::new();
            std::io::copy(&mut decoder, &mut mem_stream)?;
            return Self::open_psb_v2(MemReader::new(mem_stream.into_inner()));
        }
        if signature == MDF_SIGNATURE {
            let mut file = MdfReader::open_mdf(stream)
                .map_err(|e| anyhow::anyhow!("Failed to open MDF/PSB: {:?}", e))?;
            return file
                .load()
                .map_err(|e| anyhow::anyhow!("Failed to load MDF/PSB: {:?}", e));
        }
        if signature != PSB_SIGNATURE {
            return Err(anyhow::anyhow!("Failed to open PSB: invalid signature"));
        }
        let normal_file = PsbReader::open_psb(&mut stream);
        match normal_file {
            Ok(mut file) => file
                .load()
                .map_err(|e| anyhow::anyhow!("Failed to load PSB: {:?}", e)),
            Err(err) => {
                stream.seek(SeekFrom::Start(0))?;
                let encryption = stream.peek_u16_at(6).unwrap_or(0);
                if encryption != 0 {
                    load_psb_dullahan(&mut stream).map_err(|fallback_err| {
                        anyhow::anyhow!(
                            "Failed to open PSB: {:?}; fallback failed: {}",
                            err,
                            fallback_err
                        )
                    })
                } else {
                    Err(anyhow::anyhow!("Failed to open PSB: {:?}", err))
                }
            }
        }
    }
}

#[cfg(feature = "json")]
#[test]
fn test_f32_to_json() {
    let num = PsbValueFixed::Number(PsbNumber::Float(3.03));
    let json_value = num.to_json().unwrap();
    assert_eq!(json_value.to_string(), "3.03");
    let num = PsbValueFixed::Number(PsbNumber::Double(3.03));
    let json_value = num.to_json().unwrap();
    assert_eq!(json_value.to_string(), "3.03");
}