Files
mtgodot-poc/extension/tests/proto_item_layout_test.cpp

80 lines
2.8 KiB
C++

// Synthetic item_proto record test. It locks the offsets used by the
// ClientVS22 TItemTable layout without requiring the 2.2G asset tree.
#include "../src/proto/proto.h"
#include <array>
#include <cstdint>
#include <cstdio>
#include <cstring>
static int g_fail = 0;
#define CHECK(c, msg) \
do { \
if (!(c)) { \
std::fprintf(stderr, "FAIL: %s\n", msg); \
++g_fail; \
} \
} while (0)
static void put_u32(std::array<uint8_t, 236> &record, size_t offset, uint32_t value) {
record[offset + 0] = static_cast<uint8_t>(value);
record[offset + 1] = static_cast<uint8_t>(value >> 8);
record[offset + 2] = static_cast<uint8_t>(value >> 16);
record[offset + 3] = static_cast<uint8_t>(value >> 24);
}
int main() {
std::array<uint8_t, 236> record{};
put_u32(record, 0, 12345);
put_u32(record, 4, 7);
std::memcpy(record.data() + 8, "TestItem", 9);
std::memcpy(record.data() + 73, "LocaleItem", 11);
record[138] = 3;
record[139] = 4;
record[140] = 2;
record[141] = 1;
put_u32(record, 142, 1u << 8);
put_u32(record, 146, 1u << 3);
put_u32(record, 150, 0x12345678);
put_u32(record, 158, 9000);
put_u32(record, 162, 6000);
record[166] = 1;
put_u32(record, 167, 42);
record[176] = 2;
put_u32(record, 177, -7);
put_u32(record, 191 + 3 * 4, 3);
put_u32(record, 215, 1001);
put_u32(record, 219, -1);
put_u32(record, 227, 12346);
record[231] = 0x34;
record[232] = 0x12;
record[233] = 11;
record[234] = 75;
record[235] = 88;
const mtproto::ItemRecord item = mtproto::parse_item(record.data(), record.size());
CHECK(item.vnum == 12345, "vnum offset");
CHECK(item.vnum_range == 7, "vnum range offset");
CHECK(item.name == "TestItem", "name offset");
CHECK(item.locale_name == "LocaleItem", "locale name offset");
CHECK(item.type == 3 && item.sub_type == 4, "type offsets");
CHECK(item.anti_flags == (1u << 8), "anti_flags offset");
CHECK(item.flags == (1u << 3), "flags offset");
CHECK(item.wear_flags == 0x12345678, "wear_flags offset");
CHECK(item.buy_price == 9000 && item.sell_price == 6000, "price offsets");
CHECK(item.limits[0].type == 1 && item.limits[0].value == 42, "limit offsets");
CHECK(item.applies[0].type == 2 && item.applies[0].value == -7, "apply offsets");
CHECK(item.values[3] == 3, "values offset");
CHECK(item.sockets[0] == 1001 && item.sockets[1] == -1, "socket offsets");
CHECK(item.refined_vnum == 12346 && item.refine_set == 0x1234, "refine offsets");
CHECK(item.alter_to_magic_pct == 11 && item.specular == 75 && item.gain_socket_pct == 88,
"tail offsets");
if (g_fail) {
std::fprintf(stderr, "%d check(s) failed\n", g_fail);
return 1;
}
std::printf("all item layout checks passed\n");
return 0;
}