Files
Navmesher_old/espReader/ESPReader.h
2022-09-05 13:51:05 +10:00

264 lines
6.9 KiB
C

#pragma once
/* For reading structured data out of Creation Engine esp/esm files.
* Based on information from:
* https://en.uesp.net/wiki/Skyrim_Mod:Mod_File_Format
*
* esp/esm files generally have a tree-like structure where:
* - top level is a a TES4 record concatenated with each of the top-level groups
* - groups contain a concatenation of 0 or more groups or records
* - records contain a concatenation of 0 or more fields
* - fields contain a structure of statically or dynamically sized data
* - groups, records, and fields all have headers
*
* === NOTE ===
* Assumptions:
* - Little endian system
* - Compiled with MSVC
*/
#include <stdint.h>
#include "msh.h"
// Guards for C++ usage
#ifdef __cplusplus
extern "C" {
#endif
#define RFS_INNER_SIZE 32
#define RT_SIZE 128
#define RT_HASH_SIZE 512
#define RT_HASH_BITS 9
#define RT_HASH_SEED 131261257
#define COMPRESSED_FLAG (((uint32_t)1) << 18)
#define GO_SIZE 118
#define GTS_SIZE 10
//
// === FORWARD DEFS ===
//
typedef union type4 Type4;
typedef struct timestamp Timestamp;
typedef const struct group Group;
typedef const struct record Record;
typedef const struct field Field;
//
// === SIMPLE TYPES ===
//
// Basic types
typedef uint32_t formid;
// char[4] with uint32_t access
union type4 {
char bytes[4];
uint32_t uint;
};
// indexed by flag bit
typedef const char *const rfs_inner[RFS_INNER_SIZE];
//
// === ENUMS ===
//
// Tag for generic node tagged union
enum node_type { // NT_ prefix
NT_GROUP,
NT_RECORD,
NT_FIELD
};
// Record type enum
enum record_type {
_NONE,
AACT, ACHR, ACTI, ADDN, ALCH, AMMO,
ANIO, APPA, ARMA, ARMO, ARTO, ASPC,
ASTP, AVIF, BOOK, BPTD, CAMS, CELL,
CLAS, CLDC, CLFM, CLMT, COBJ, COLL,
CONT, CPTH, CSTY, DEBR, DIAL, DLBR,
DLVW, DOBJ, DOOR, DUAL, ECZN, EFSH,
ENCH, EQUP, EXPL, EYES, FACT, FLOR,
FLST, FSTP, FSTS, FURN, GLOB, GMST,
GRAS, GRUP, HAIR, HAZD, HDPT, IDLE,
IDLM, IMAD, IMGS, INFO, INGR, IPCT,
IPDS, KEYM, KYWD, LAND, LCRT, LCTN,
LGTM, LIGH, LSCR, LTEX, LVLI, LVLN,
LVSP, MATO, MATT, MESG, MGEF, MISC,
MOVT, MSTT, MUSC, MUST, NAVI, NAVM,
NOTE, NPC_, OTFT, PACK, PERK, PGRE,
PHZD, PROJ, PWAT, QUST, RACE, REFR,
REGN, RELA, REVB, RFCT, RGDL, SCEN,
SCOL, SCPT, SCRL, SHOU, SLGM, SMBN,
SMEN, SMQN, SNCT, SNDR, SOPM, SOUN,
SPEL, SPGD, STAT, TACT, TES4, TREE,
TXST, VTYP, WATR, WEAP, WOOP, WRLD,
WTHR,
};
// Enums of perfect hash values for
enum record_type_hash {
RT_AACT = 496, RT_ACHR = 249, RT_ACTI = 293, RT_ADDN = 316,
RT_ALCH = 312, RT_AMMO = 157, RT_ANIO = 297, RT_APPA = 230,
RT_ARMA = 222, RT_ARMO = 218, RT_ARTO = 328, RT_ASPC = 252,
RT_ASTP = 384, RT_AVIF = 309, RT_BOOK = 318, RT_BPTD = 454,
RT_CAMS = 319, RT_CELL = 18, RT_CLAS = 16, RT_CLDC = 68,
RT_CLFM = 389, RT_CLMT = 497, RT_COBJ = 422, RT_COLL = 140,
RT_CONT = 169, RT_CPTH = 30, RT_CSTY = 193, RT_DEBR = 460,
RT_DIAL = 304, RT_DLBR = 340, RT_DLVW = 434, RT_DOBJ = 437,
RT_DOOR = 347, RT_DUAL = 246, RT_ECZN = 229, RT_EFSH = 509,
RT_ENCH = 194, RT_EQUP = 57, RT_EXPL = 153, RT_EYES = 181,
RT_FACT = 62, RT_FLOR = 137, RT_FLST = 199, RT_FSTP = 462,
RT_FSTS = 388, RT_FURN = 105, RT_GLOB = 376, RT_GMST = 125,
RT_GRAS = 49, RT_GRUP = 511, RT_HAIR = 481, RT_HAZD = 21,
RT_HDPT = 100, RT_IDLE = 204, RT_IDLM = 348, RT_IMAD = 390,
RT_IMGS = 187, RT_INFO = 82, RT_INGR = 463, RT_IPCT = 292,
RT_IPDS = 89, RT_KEYM = 54, RT_KYWD = 123, RT_LAND = 261,
RT_LCRT = 138, RT_LCTN = 172, RT_LGTM = 177, RT_LIGH = 13,
RT_LSCR = 288, RT_LTEX = 447, RT_LVLI = 235, RT_LVLN = 453,
RT_LVSP = 343, RT_MATO = 2, RT_MATT = 220, RT_MESG = 66,
RT_MGEF = 32, RT_MISC = 145, RT_MOVT = 378, RT_MSTT = 132,
RT_MUSC = 87, RT_MUST = 9, RT_NAVI = 50, RT_NAVM = 122,
RT_NOTE = 366, RT_NPC_ = 440, RT_OTFT = 365, RT_PACK = 441,
RT_PERK = 243, RT_PGRE = 210, RT_PHZD = 26, RT_PROJ = 120,
RT_PWAT = 397, RT_QUST = 71, RT_RACE = 108, RT_REFR = 449,
RT_REGN = 320, RT_RELA = 182, RT_REVB = 266, RT_RFCT = 311,
RT_RGDL = 19, RT_SCEN = 119, RT_SCOL = 276, RT_SCPT = 363,
RT_SCRL = 104, RT_SHOU = 115, RT_SLGM = 70, RT_SMBN = 413,
RT_SMEN = 240, RT_SMQN = 63, RT_SNCT = 117, RT_SNDR = 280,
RT_SOPM = 306, RT_SOUN = 165, RT_SPEL = 190, RT_SPGD = 443,
RT_STAT = 202, RT_TACT = 282, RT_TES4 = 474, RT_TREE = 27,
RT_TXST = 359, RT_VTYP = 335, RT_WATR = 84, RT_WEAP = 10,
RT_WOOP = 352, RT_WRLD = 38, RT_WTHR = 83,
};
// GRUP type values
enum group_type { // GT_ prefix
GT_TOP = 0,
GT_WORLD_CHILDREN = 1,
GT_INTERIOR_CELL_BLOCK = 2,
GT_INTERIOR_CELL_SUBBLOCK = 3,
GT_EXTERIOR_CELL_BLOCK = 4,
GT_EXTERIOR_CELL_SUBBLOCK = 5,
GT_CELL_CHILDREN = 6,
GT_TOPIC_CHILDREN = 7,
GT_CELL_PERSISTENT_CHILDREN = 8,
GT_CELL_TEMPORARY_CHILDREN = 9,
};
//
// === COMPOSITE TYPES ===
//
// Generic node
typedef const struct node Node;
struct node {
const union {
Group *const group;
Record *const record;
Field *const field;
};
const char *const data;
const enum node_type type;
const uint32_t _pad;
};
// calculated timestamp
struct timestamp {
uint16_t year;
uint8_t month;
uint8_t day;
};
//
// === BINARY DATA OVERLAYS ===
//
#pragma pack(push, 1)
// Group header overlay
struct group {
const Type4 grup; // always RT_GRUP
const uint32_t size; // uncludes the 24 byte group header
const union {
const Type4 type; // this may be mangled and should not be relied on
const formid formid;
const int32_t number;
const int16_t coord[2];
} label; // access determined by the `type` below
const int32_t type; // group_type enum
const uint16_t timestamp;
const uint16_t vcinfo;
const uint32_t unknown;
};
// Record header overlay
struct record {
const Type4 type;
const uint32_t size;
const uint32_t flags;
const uint32_t formid;
const uint16_t timestamp;
const uint16_t vcinfo;
const uint16_t version;
const uint16_t unknown;
};
// Field header overlay
struct field {
const Type4 type;
const uint16_t size;
};
#pragma pack(pop)
//
// === LUTs ===
//
// record type enum to fourcc value
extern const uint32_t rt[RT_SIZE];
// for converting between record_type and record_type_hash enums
extern const uint16_t rt2rth[RT_SIZE];
extern const uint8_t rth2rt[RT_HASH_SIZE];
// type -> flag mappings
// NULL table pointers indicate no flags
// NULL string pointers indicate invalid flag
extern rfs_inner *const rfs[RT_HASH_SIZE];
extern rfs_inner *const rfs_refr[RT_HASH_SIZE];
// Expected (probably) order of top level groups in an esp/esm
extern const enum record_type group_order[GO_SIZE];
// Printable strings for group types
extern const char *const group_type_strings[GTS_SIZE];
//
// === FUNCTIONS ===
//
// hashes type value into RT_ hash value
inline uint32_t rt_hash(uint32_t type) {
return uint32_t_msh(type, RT_HASH_BITS, RT_HASH_SEED);
}
// Walks the nodes of the esp/esm structured data at `data`
void walk(const char *data, size_t size);
// End C++ guard
#ifdef __cplusplus
}
#endif