forked from WerWolv/ImHex-Patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbx.hexpat
193 lines (165 loc) · 5.96 KB
/
fbx.hexpat
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
#pragma description Kaydara FBX Binary
#pragma magic [4B 61 79 64 61 72 61 20 46 42 58 20 42 69 6E 61 72 79 20 20 00 1A 00] @ 0x00
/*
* Based on Blenders implementation of FBX import/export, see:
* (incomplete) https://code.blender.org/2013/08/fbx-binary-file-format-specification/
* https://projects.blender.org/blender/blender/src/branch/main/scripts/addons_core/io_scene_fbx/parse_fbx.py
* https://projects.blender.org/blender/blender/src/branch/main/scripts/addons_core/io_scene_fbx/encode_bin.py
*/
#pragma endian little
#ifdef __IMHEX__
import hex.dec;
#endif
import std.mem;
import std.string;
import std.sys;
import type.magic;
struct Array<E> {
u32 arrayLength;
u32 encoding;
u32 compressedLength;
std::assert(encoding < 2, "Invalid array encoding!");
if (encoding == 0) {
// Uncompressed
E contents[arrayLength];
} else {
// Compressed (zlib)
u128 pos = $;
u8 compressedContents[compressedLength];
#ifdef __IMHEX__
std::mem::Section contentsSection = std::mem::create_section(std::format("contentsSection @ {:#x}", pos));
hex::dec::zlib_decompress(compressedContents, contentsSection, 15);
auto contentsSectionSize = std::mem::get_section_size(contentsSection);
auto contentsElementSize = sizeof(E);
std::assert_warn((contentsSectionSize % contentsElementSize) == 0,
"The size of the contentsSection must be an integer multiple of sizeof(E) !");
E contents[contentsSectionSize / contentsElementSize] @ 0x00 in contentsSection;
#endif
}
};
enum PropertyTypeCode : char {
BYTE = 'Z',
SHORT = 'Y',
BOOL = 'B',
CHAR = 'C',
INT = 'I',
FLOAT = 'F',
DOUBLE = 'D',
LONG = 'L',
BINARY = 'R',
STRING = 'S',
ARRAY_BOOL = 'b',
ARRAY_UBYTE = 'c',
ARRAY_INT = 'i',
ARRAY_LONG = 'l',
ARRAY_FLOAT = 'f',
ARRAY_DOUBLE = 'd'
};
struct PropertyRecord {
PropertyTypeCode typeCode;
match (typeCode) {
(PropertyTypeCode::BYTE): s8 data;
(PropertyTypeCode::SHORT): s16 data;
(PropertyTypeCode::BOOL): bool data;
(PropertyTypeCode::CHAR): char data;
(PropertyTypeCode::INT): s32 data;
(PropertyTypeCode::FLOAT): float data;
(PropertyTypeCode::DOUBLE): double data;
(PropertyTypeCode::LONG): s64 data;
(PropertyTypeCode::BINARY): {
u32 dataLength;
u8 data[dataLength];
}
(PropertyTypeCode::STRING): {
u32 stringLength;
char string[stringLength];
}
(PropertyTypeCode::ARRAY_BOOL): Array<bool> data;
(PropertyTypeCode::ARRAY_UBYTE): Array<u8> data;
(PropertyTypeCode::ARRAY_INT): Array<s32> data;
(PropertyTypeCode::ARRAY_LONG): Array<s64> data;
(PropertyTypeCode::ARRAY_FLOAT): Array<float> data;
(PropertyTypeCode::ARRAY_DOUBLE): Array<double> data;
(_): std::error("Invalid property type code!");
}
};
struct NodeRecord32 {
u32 endOffset;
u32 numProperties;
u32 propertyListLen;
u8 nameLen;
// Detect sentinel record which marks the end of a list of node records
if (endOffset == 0
&& numProperties == 0
&& propertyListLen == 0
&& nameLen == 0) {
break;
}
char name[nameLen];
auto posBeforePropertyRecords = $;
auto posAfterPropertyRecords = posBeforePropertyRecords + propertyListLen;
PropertyRecord propertyRecords[numProperties];
std::assert($ == posAfterPropertyRecords, std::format("Invalid size of propertyRecords @ {:#x} !", posBeforePropertyRecords));
NodeRecord32 nestedList[while($ < endOffset)];
std::assert($ == endOffset, std::format("Invalid size of nestedList @ {:#x} !", posAfterPropertyRecords));
};
struct NodeRecord64 {
u64 endOffset;
u64 numProperties;
u64 propertyListLen;
u8 nameLen;
// Detect sentinel record which marks the end of a list of node records
if (endOffset == 0
&& numProperties == 0
&& propertyListLen == 0
&& nameLen == 0) {
break;
}
char name[nameLen];
auto posBeforePropertyRecords = $;
auto posAfterPropertyRecords = posBeforePropertyRecords + propertyListLen;
PropertyRecord propertyRecords[numProperties];
std::assert($ == posAfterPropertyRecords, std::format("Invalid size of propertyRecords @ {:#x} !", posBeforePropertyRecords));
NodeRecord64 nestedList[while($ < endOffset)];
std::assert($ == endOffset, std::format("Invalid size of nestedList @ {:#x} !", posAfterPropertyRecords));
};
fn assertZero (auto array, u128 size, auto message) {
bool nonzeroPadding = false;
for (u8 i = 0, i < size, i = i + 1) {
if (array[i] != 0) {
nonzeroPadding = true;
}
}
std::assert_warn(!nonzeroPadding, message);
};
struct Footer {
type::Magic<"\xFA\xBC\xAB\x09\xD0\xC8\xD4\x66\xB1\x76\xFB\x83\x1C\xF7\x26\x7E"> footerId;
char zeroes[4];
assertZero(zeroes, 4, "Found non-zero values in footer after footerId!");
u128 ofs = $;
u8 alignmentPaddingSize = ((ofs + 15) & ~15) - ofs;
if (alignmentPaddingSize == 0) {
alignmentPaddingSize = 16;
}
char alignmentPadding[alignmentPaddingSize];
assertZero(alignmentPadding, alignmentPaddingSize, "Found non-zero bytes in alignmentPadding!");
u32 version;
char staticPadding[120];
assertZero(staticPadding, 120, "Found non-zero bytes in staticPadding!");
type::Magic<"\xF8\x5A\x8C\x6A\xDE\xF5\xD9\x7E\xEC\xE9\x0C\xE3\x75\x8F\x29\x0B"> footerMagic;
};
struct Header {
type::Magic<"Kaydara FBX Binary \x00\x1A\x00"> magic;
u32 version;
};
struct FBX {
Header header;
if (header.version < 7500) {
NodeRecord32 rootRecords[while(true)];
} else {
NodeRecord64 rootRecords[while(true)];
}
Footer footer;
std::assert_warn(header.version == footer.version, "Version numbers in header and footer do not match!");
};
FBX fbx @ 0x00;