Skip to content

Commit 9e71806

Browse files
committed
bdat: partial xc3 support
1 parent 7abe307 commit 9e71806

File tree

6 files changed

+485
-203
lines changed

6 files changed

+485
-203
lines changed

include/xenolib/bdat.hpp

Lines changed: 25 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@
2424
namespace BDAT {
2525
constexpr static uint32 ID = CompileFourCC("BDAT");
2626
template <class C> using Pointer = es::PointerX86<C>;
27-
template <class C> using Pointer16 = es::PointerX86<C, int16>;
28-
29-
enum class BaseType : uint8 {
30-
None,
31-
Default,
32-
Array,
33-
Flag,
34-
};
3527

3628
enum class DataType : uint8 {
3729
None,
@@ -43,35 +35,31 @@ enum class DataType : uint8 {
4335
i32,
4436
StringPtr,
4537
Float,
38+
KeyHash,
39+
Unk = 11, // Pointer??
40+
Unk1 = 13, // Child index??
4641
};
4742

48-
struct KeyDesc;
49-
50-
struct BaseTypeDesc {
51-
BaseType baseType;
52-
};
53-
54-
struct FlagTypeDesc : BaseTypeDesc {
55-
uint8 index;
56-
uint16 null;
57-
uint16 value;
58-
Pointer16<KeyDesc> belongsTo;
59-
};
60-
61-
struct TypeDesc : BaseTypeDesc {
62-
DataType type;
63-
uint16 offset;
64-
};
65-
66-
struct ArrayTypeDesc : TypeDesc {
67-
uint16 numItems;
68-
};
69-
70-
struct KeyDesc {
71-
Pointer16<BaseTypeDesc> typeDesc;
72-
Pointer16<KeyDesc> unk; // random refs
73-
Pointer16<char> name;
74-
};
43+
inline size_t TypeSize(DataType type) {
44+
switch (type) {
45+
case BDAT::DataType::i16:
46+
case BDAT::DataType::u16:
47+
case BDAT::DataType::Unk1:
48+
return 2;
49+
case BDAT::DataType::i32:
50+
case BDAT::DataType::u32:
51+
case BDAT::DataType::StringPtr:
52+
case BDAT::DataType::Float:
53+
case BDAT::DataType::KeyHash:
54+
case BDAT::DataType::Unk:
55+
return 4;
56+
case BDAT::DataType::i8:
57+
case BDAT::DataType::u8:
58+
return 1;
59+
default:
60+
throw std::runtime_error("Unhandled data type");
61+
}
62+
}
7563

7664
union Value {
7765
int8 asI8;
@@ -84,74 +72,6 @@ union Value {
8472
Pointer<char> asString;
8573
};
8674

87-
struct KVPair {
88-
const BaseTypeDesc *desc;
89-
union {
90-
const char *data;
91-
const Value *value;
92-
};
93-
94-
bool XN_EXTERN operator==(const Value &other) const;
95-
96-
bool operator==(es::string_view other) const {
97-
if (IsString()) {
98-
return other == value->asString.Get();
99-
}
100-
101-
throw std::runtime_error("Invalid call for string comparison");
102-
}
103-
104-
bool IsString() const {
105-
if (desc->baseType != BaseType::Default) {
106-
return false;
107-
}
108-
109-
auto &asDef = *static_cast<const TypeDesc *>(desc);
110-
111-
return asDef.type == DataType::StringPtr;
112-
}
113-
};
114-
115-
enum class Type : uint8 {
116-
EncryptFloat,
117-
PersistentFlag, // Is this whole encryption?
118-
};
119-
120-
using Flags = es::Flags<Type>;
121-
122-
struct Header {
123-
uint32 id;
124-
uint8 version;
125-
Flags flags;
126-
Pointer16<char> name;
127-
uint16 kvBlockStride;
128-
Pointer16<char> unk1Offset;
129-
uint16 unk1Size;
130-
Pointer16<char> keyValues;
131-
uint16 numKeyValues;
132-
uint16 unk3;
133-
uint16 numEncKeys;
134-
uint8 encKeys[2];
135-
Pointer<char> strings;
136-
uint32 stringsSize;
137-
Pointer16<KeyDesc> keyDescs;
138-
uint16 numKeyDescs;
139-
140-
const char XN_EXTERN *FindBlock(es::string_view keyName, Value value);
141-
const char XN_EXTERN *FindBlock(es::string_view keyName,
142-
es::string_view value);
143-
};
144-
145-
struct Collection {
146-
uint32 numDatas;
147-
uint32 fileSize;
148-
Pointer<Header> datas[1];
149-
150-
Pointer<Header> *begin() { return datas; }
151-
Pointer<Header> *end() { return datas + numDatas; }
152-
const Pointer<Header> *begin() const { return datas; }
153-
const Pointer<Header> *end() const { return datas + numDatas; }
154-
155-
const Header XN_EXTERN *FindData(es::string_view name) const;
156-
};
75+
#include "internal/bdat1.inl"
76+
#include "internal/bdat4.inl"
15777
} // namespace BDAT

include/xenolib/internal/bdat1.inl

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* Xenoblade Engine Format Library
2+
Copyright(C) 2022 Lukas Cone
3+
4+
This program is free software : you can redistribute it and / or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program.If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
#pragma once
19+
20+
namespace V1 {
21+
using namespace BDAT;
22+
template <class C> using Pointer16 = es::PointerX86<C, int16>;
23+
24+
enum class BaseType : uint8 {
25+
None,
26+
Default,
27+
Array,
28+
Flag,
29+
};
30+
31+
struct KeyDesc;
32+
33+
struct BaseTypeDesc {
34+
BaseType baseType;
35+
};
36+
37+
struct FlagTypeDesc : BaseTypeDesc {
38+
uint8 index;
39+
uint16 null;
40+
uint16 value;
41+
Pointer16<KeyDesc> belongsTo;
42+
};
43+
44+
struct TypeDesc : BaseTypeDesc {
45+
DataType type;
46+
uint16 offset;
47+
};
48+
49+
struct ArrayTypeDesc : TypeDesc {
50+
uint16 numItems;
51+
};
52+
53+
struct KeyDesc {
54+
Pointer16<BaseTypeDesc> typeDesc;
55+
Pointer16<KeyDesc> unk; // random refs
56+
Pointer16<char> name;
57+
};
58+
59+
struct KVPair {
60+
const BaseTypeDesc *desc;
61+
union {
62+
const char *data;
63+
const Value *value;
64+
};
65+
66+
bool XN_EXTERN operator==(const Value &other) const;
67+
68+
bool operator==(es::string_view other) const {
69+
if (IsString()) {
70+
return other == value->asString.Get();
71+
}
72+
73+
throw std::runtime_error("Invalid call for string comparison");
74+
}
75+
76+
bool IsString() const {
77+
if (desc->baseType != BaseType::Default) {
78+
return false;
79+
}
80+
81+
auto &asDef = *static_cast<const TypeDesc *>(desc);
82+
83+
return asDef.type == DataType::StringPtr;
84+
}
85+
};
86+
87+
enum class Type : uint8 {
88+
EncryptFloat,
89+
PersistentFlag, // Is this whole encryption?
90+
};
91+
92+
using Flags = es::Flags<Type>;
93+
94+
struct Header {
95+
uint32 id;
96+
uint8 version;
97+
Flags flags;
98+
Pointer16<char> name;
99+
uint16 kvBlockStride;
100+
Pointer16<char> unk1Offset;
101+
uint16 unk1Size;
102+
Pointer16<char> keyValues;
103+
uint16 numKeyValues;
104+
uint16 unk3;
105+
uint16 numEncKeys;
106+
uint8 encKeys[2];
107+
Pointer<char> strings;
108+
uint32 stringsSize;
109+
Pointer16<KeyDesc> keyDescs;
110+
uint16 numKeyDescs;
111+
112+
const char XN_EXTERN *FindBlock(es::string_view keyName, Value value);
113+
const char XN_EXTERN *FindBlock(es::string_view keyName,
114+
es::string_view value);
115+
};
116+
117+
struct Collection {
118+
uint32 numDatas;
119+
uint32 fileSize;
120+
Pointer<Header> datas[1];
121+
122+
Pointer<Header> *begin() { return datas; }
123+
Pointer<Header> *end() { return datas + numDatas; }
124+
const Pointer<Header> *begin() const { return datas; }
125+
const Pointer<Header> *end() const { return datas + numDatas; }
126+
127+
const Header XN_EXTERN *FindData(es::string_view name) const;
128+
};
129+
} // namespace V1

include/xenolib/internal/bdat4.inl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* Xenoblade Engine Format Library
2+
Copyright(C) 2022 Lukas Cone
3+
4+
This program is free software : you can redistribute it and / or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program.If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
#pragma once
19+
20+
namespace V4 {
21+
using namespace BDAT;
22+
23+
enum class Type : uint8 {
24+
Data,
25+
Collection,
26+
};
27+
28+
struct HeaderBase {
29+
uint32 id;
30+
uint8 version;
31+
uint8 headerSize;
32+
uint8 null;
33+
Type type;
34+
};
35+
36+
struct Descriptor {
37+
DataType type;
38+
uint8 nameOffset0;
39+
uint8 nameOffset1;
40+
uint16 NameOffset() const { return nameOffset0 + (nameOffset1 * 256); }
41+
};
42+
43+
struct Key {
44+
uint32 hash;
45+
uint32 index;
46+
};
47+
48+
struct Header : HeaderBase {
49+
uint32 numDescs;
50+
uint32 numKeys;
51+
uint32 unk3;
52+
uint32 selfHash; // could be used as encKey?
53+
Pointer<Descriptor> descriptors;
54+
Pointer<Key> keys;
55+
Pointer<char> values;
56+
uint32 kvBlockSize;
57+
Pointer<char> strings;
58+
uint32 stringsSize;
59+
};
60+
61+
struct Collection : HeaderBase {
62+
uint32 numDatas;
63+
uint32 fileSize;
64+
Pointer<Header> datas[1];
65+
66+
Pointer<Header> *begin() { return datas; }
67+
Pointer<Header> *end() { return datas + numDatas; }
68+
const Pointer<Header> *begin() const { return datas; }
69+
const Pointer<Header> *end() const { return datas + numDatas; }
70+
};
71+
72+
} // namespace V4

0 commit comments

Comments
 (0)