Still haven't properly tested MetaTree. But have all of the current features working together.

This commit is contained in:
2022-09-08 19:35:16 +10:00
parent abc9cf6a61
commit 392e226013
5 changed files with 195 additions and 94 deletions

View File

@@ -72,6 +72,7 @@
<ProjectGuid>{4ce7e7fd-7fff-4c42-ab16-c9c2b01e5d56}</ProjectGuid> <ProjectGuid>{4ce7e7fd-7fff-4c42-ab16-c9c2b01e5d56}</ProjectGuid>
<RootNamespace>NavmeshList</RootNamespace> <RootNamespace>NavmeshList</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>NavmeshList</ProjectName>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">

View File

@@ -11,50 +11,103 @@
#include <assert.h> #include <assert.h>
int main(void) { int main(void) {
errno_t ret = 0;
// Read in the esp file
struct sized_buf esp_buf = { 0 };
{
FILE *fp; FILE *fp;
errno_t ret = fopen_s(&fp, "Skyrim.esm", "rb"); ret = fopen_s(&fp, "Skyrim.esm", "rb");
if (ret || !fp) if (ret || !fp)
return ret; goto exit1;
fseek(fp, 0L, SEEK_END); fseek(fp, 0L, SEEK_END);
size_t size = ftell(fp); esp_buf.size = ftell(fp);
rewind(fp); rewind(fp);
char *buffer = malloc(size); esp_buf.data = malloc(esp_buf.size);
if (!buffer) if (!esp_buf.data) {
return errno; ret = errno;
goto exit1;
}
size_t read = fread(buffer, sizeof(char), size, fp); size_t read = fread(esp_buf.data, sizeof(char), esp_buf.size, fp);
assert(read == size); fclose(fp);
assert(read == esp_buf.size);
}
struct sized_buf esp = { .data = buffer, .size = size }; // Calculate esp stats
struct esp_stats stats = espr_stats(esp_buf);
struct esp_stats stats = espr_stats(buffer, size); // Decompress the esp file
struct sized_buf decom_buf = { 0 };
{
decom_buf.size = stats.decompressed_size;
decom_buf.data = malloc(decom_buf.size);
if (!decom_buf.data) {
ret = errno;
goto exit2;
}
/* espr_decompress(esp_buf, decom_buf);
char *decompressed = malloc(stats.decompressed_size); }
if (!decompressed)
return errno;
// espr_print(buffer, size); // Construct the meta tree
struct sized_buf tree_buf = { 0 };
MetaTree tree = { 0 };
{
tree_buf.size = espr_tree_size(stats);
tree_buf.data = malloc(tree_buf.size);
if (!tree_buf.data) {
ret = errno;
goto exit3;
}
tree = espr_create_tree(decom_buf, tree_buf);
}
espr_decompress(buffer, size, decompressed, stats.decompressed_size); // Serialize and write out the uncompressed esp
*/ struct sized_buf serialized_buf = { 0 };
{
serialized_buf.size = tree.size;
serialized_buf.data = malloc(serialized_buf.size);
if (!serialized_buf.data) {
ret = errno;
goto exit4;
}
size_t tree_size = espr_tree_size(stats); espr_serialize(tree, serialized_buf);
char *tree_data = malloc(tree_size);
struct sized_buf tree = { .data = tree_data, .size = tree_size };
MetaNode *root = espr_create_tree(esp, tree); printf("Serialized.\n");
free(buffer); FILE *fp;
ret = fopen_s(&fp, "Test.esm", "wb");
/* if (ret || !fp)
espr_print(decompressed, stats.decompressed_size); goto exit5;
free(decompressed); size_t written = fwrite(serialized_buf.data, sizeof(char),
*/ serialized_buf.size, fp);
fclose(fp);
assert(written == serialized_buf.size);
printf("Written.\n");
}
exit5:
free(serialized_buf.data);
exit4:
free(tree_buf.data);
exit3:
free(decom_buf.data);
exit2:
free(esp_buf.data);
exit1:
// lazy file pointer cleanup
_fcloseall();
printf("Finished.\n");
(void)getc(stdin);
return 0; return 0;
} }

View File

@@ -75,6 +75,7 @@ extern "C" {
typedef struct record Record; typedef struct record Record;
typedef struct field Field; typedef struct field Field;
typedef struct meta_node MetaNode; typedef struct meta_node MetaNode;
typedef struct meta_tree MetaTree;
// //
@@ -215,6 +216,11 @@ extern "C" {
void *data; void *data;
}; };
struct meta_callbacks {
void (*pre)(MetaNode *m, void *data);
void *data;
};
/* Meta Nodes are used for constructing a more flexible tree structure /* Meta Nodes are used for constructing a more flexible tree structure
* on top of the natural structure of ESP/ESM files. * on top of the natural structure of ESP/ESM files.
* *
@@ -249,6 +255,11 @@ extern "C" {
MetaNode *next; MetaNode *next;
}; };
struct meta_tree {
MetaNode *root;
size_t size;
};
// //
// === BINARY DATA OVERLAYS === // === BINARY DATA OVERLAYS ===
// //
@@ -340,8 +351,7 @@ extern "C" {
* increasing in terms of memory location within the buffer. * increasing in terms of memory location within the buffer.
*/ */
void espr_walk( void espr_walk(
char *data, struct sized_buf esp,
size_t size,
struct walker_callbacks cb, struct walker_callbacks cb,
void *from_parent void *from_parent
); );
@@ -349,13 +359,13 @@ extern "C" {
/* `espr_print` prints the header of every group and record in the given /* `espr_print` prints the header of every group and record in the given
* esp/esm binary data. * esp/esm binary data.
*/ */
void espr_print(char *data, size_t size); void espr_print(struct sized_buf esp);
/* Calculates the number of groups and records in the esp/esm file and /* Calculates the number of groups and records in the esp/esm file and
* the size of the esp/esm if all of the compressed records were * the size of the esp/esm if all of the compressed records were
* decompressed. * decompressed.
*/ */
struct esp_stats espr_stats(char *data, size_t size); struct esp_stats espr_stats(struct sized_buf esp);
// Calculates the number of formid's in an esm/esp from the stats // Calculates the number of formid's in an esm/esp from the stats
inline uint32_t espr_formid_count(struct esp_stats stats) { inline uint32_t espr_formid_count(struct esp_stats stats) {
@@ -378,10 +388,8 @@ extern "C" {
* `espr_decompressed_size`, and `buf` should be at least of that size. * `espr_decompressed_size`, and `buf` should be at least of that size.
*/ */
void espr_decompress( void espr_decompress(
char *data, struct sized_buf esp,
size_t size, struct sized_buf decom
char *buf,
size_t buf_size
); );
/* Constructs a MetaNode tree in `tree` over the esp/esm data in `in`. /* Constructs a MetaNode tree in `tree` over the esp/esm data in `in`.
@@ -390,7 +398,13 @@ extern "C" {
* data, and also allow for modifications that add, remove, or change * data, and also allow for modifications that add, remove, or change
* the size of groups/records/fields. * the size of groups/records/fields.
*/ */
MetaNode *espr_create_tree(struct sized_buf in, struct sized_buf tree); MetaTree espr_create_tree(struct sized_buf in, struct sized_buf tree);
void espr_meta_walk(MetaTree tree, struct meta_callbacks cb);
void espr_meta_node_walk(MetaNode *m, struct meta_callbacks cb);
void espr_serialize(MetaTree tree, struct sized_buf out);
// End C++ guard // End C++ guard
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -39,8 +39,7 @@ void asserts(void);
// Tree walkers // Tree walkers
char *walk_concat( char *walk_concat(
char *data, struct sized_buf tree,
size_t size,
struct walker_callbacks cb, struct walker_callbacks cb,
void *from_parent void *from_parent
); );
@@ -88,7 +87,7 @@ void decompress_pre(
void decompress_post( void decompress_post(
Node n, Node n,
void *data, void *data,
void **carry_in void *carry_in
); );
void create_tree_cb( void create_tree_cb(
Node n, Node n,
@@ -97,6 +96,7 @@ void create_tree_cb(
void *from_parent, void *from_parent,
void **to_children void **to_children
); );
void serialize_cb(MetaNode *m, void *data);
// //
// === FUNCTIONS === // === FUNCTIONS ===
@@ -118,22 +118,21 @@ void asserts(void)
} }
void espr_walk( void espr_walk(
char *data, struct sized_buf esp,
size_t size,
struct walker_callbacks cb, struct walker_callbacks cb,
void *from_parent void *from_parent
) { ) {
// check assertions that cannot be checked at compile time // check assertions that cannot be checked at compile time
asserts(); asserts();
char *data_start = data; char *data_start = esp.data;
// check that we are at the start of the file // check that we are at the start of the file
const Type4 type = *(const Type4 *)data; const Type4 type = *(const Type4 *)esp.data;
assert(type.uint == rt[TES4]); assert(type.uint == rt[TES4]);
data = walk_concat(data, size, cb, from_parent); esp.data = walk_concat(esp, cb, from_parent);
assert(data == data_start + size); assert(esp.data == data_start + esp.size);
} }
/* Unknown data will be some concatenation of groups and records. /* Unknown data will be some concatenation of groups and records.
@@ -142,27 +141,26 @@ void espr_walk(
* for each segment of unknown data in this concatenation. * for each segment of unknown data in this concatenation.
*/ */
char *walk_concat( char *walk_concat(
char *data, struct sized_buf tree,
size_t size,
struct walker_callbacks cb, struct walker_callbacks cb,
void *from_parent void *from_parent
) { ) {
const char *end = data + size; const char *end = tree.data + tree.size;
while (data != end) { while (tree.data != end) {
assert(data < end); assert(tree.data < end);
const Type4 *type = (Type4 *)data; const Type4 *type = (Type4 *)tree.data;
// check valid type // check valid type
assert(rt[rt_hash(type->uint)] == type->uint); assert(rt[rt_hash(type->uint)] == type->uint);
// only need to distinguish between groups and records // only need to distinguish between groups and records
if (type->uint == rt[GRUP]) if (type->uint == rt[GRUP])
data = walk_group(data, cb, from_parent); tree.data = walk_group(tree.data, cb, from_parent);
else else
data = walk_record(data, cb, from_parent); tree.data = walk_record(tree.data, cb, from_parent);
} }
return data; return tree.data;
} }
/* Walk a group record. Group records are containers for any other type of /* Walk a group record. Group records are containers for any other type of
@@ -193,7 +191,8 @@ char *walk_group(char *data, struct walker_callbacks cb, void *from_parent)
cb.pre(n, cb.data, &carry, from_parent, &to_children); cb.pre(n, cb.data, &carry, from_parent, &to_children);
// Walk through the concatenation of data inside the group. // Walk through the concatenation of data inside the group.
data = walk_concat(data_start, data_size, cb, to_children); struct sized_buf tree = { data_start, data_size };
data = walk_concat(tree, cb, to_children);
assert(data == data_end); assert(data == data_end);
// Post-walk callback // Post-walk callback
@@ -237,10 +236,10 @@ char *walk_record(char *data, struct walker_callbacks cb, void *from_parent)
return data; return data;
} }
void espr_print(char *data, size_t size) void espr_print(struct sized_buf esp)
{ {
struct walker_callbacks cb = { .pre = print_cb }; struct walker_callbacks cb = { .pre = print_cb };
espr_walk(data, size, cb, NULL); espr_walk(esp, cb, NULL);
} }
void print_cb( void print_cb(
@@ -266,11 +265,11 @@ void print_cb(
} }
} }
struct esp_stats espr_stats(char *data, size_t size) struct esp_stats espr_stats(struct sized_buf esp)
{ {
struct esp_stats stats = { 0 }; struct esp_stats stats = { 0 };
struct walker_callbacks cb = { .pre = stats_cb, .data = &stats }; struct walker_callbacks cb = { .pre = stats_cb, .data = &stats };
espr_walk(data, size, cb, NULL); espr_walk(esp, cb, NULL);
return stats; return stats;
} }
@@ -311,20 +310,14 @@ void stats_cb(
} }
} }
struct decom { void espr_decompress(struct sized_buf esp, struct sized_buf decom)
char *buf;
size_t remaining;
};
void espr_decompress(char *data, size_t size, char *buf, size_t buf_size)
{ {
struct decom s = { .buf = buf, .remaining = buf_size };
struct walker_callbacks cb = { struct walker_callbacks cb = {
.pre = decompress_pre, .pre = decompress_pre,
.post = decompress_post, .post = decompress_post,
.data = &s .data = &decom
}; };
espr_walk(data, size, cb, NULL); espr_walk(esp, cb, NULL);
} }
/* Handles the copying of groups and records, and the decompression of /* Handles the copying of groups and records, and the decompression of
@@ -349,21 +342,20 @@ void decompress_pre(
(void)from_parent; (void)from_parent;
(void)to_children; (void)to_children;
struct decom *d = decom_ptr; struct sized_buf *d = decom_ptr;
switch (n.type) { switch (n.type) {
case NT_RECORD: case NT_RECORD:
// compressed record // compressed record
if (n.header.record->flags & COMPRESSED_FLAG) { if (n.header.record->flags & COMPRESSED_FLAG) {
// copy header // copy header
memcpy(d->buf, n.header.record, sizeof(Record)); memcpy(d->data, n.header.record, sizeof(Record));
// copied header reference // copied header reference
Record *header = (Record *)d->buf; Record *header = (Record *)d->data;
// update decom struct // update decom struct
d->remaining -= sizeof(Record); sized_buf_update(d, sizeof(Record));
d->buf += sizeof(Record);
// decompress directly into buffer // decompress directly into buffer
// first 4 bytes are the decompressed size // first 4 bytes are the decompressed size
@@ -373,7 +365,7 @@ void decompress_pre(
n.header.record->size - sizeof(uint32_t); n.header.record->size - sizeof(uint32_t);
char *data_start = n.data + sizeof(uint32_t); char *data_start = n.data + sizeof(uint32_t);
int ret = uncompress( int ret = uncompress(
(Bytef *)d->buf, (Bytef *)d->data,
(uLongf *)&to_copy, (uLongf *)&to_copy,
(Bytef *)data_start, (Bytef *)data_start,
(uLong)cur_size (uLong)cur_size
@@ -382,8 +374,7 @@ void decompress_pre(
assert(to_copy == dc_size); assert(to_copy == dc_size);
// update decom struct // update decom struct
d->remaining -= dc_size; sized_buf_update(d, dc_size);
d->buf += dc_size;
// update header data size // update header data size
header->size = dc_size; header->size = dc_size;
@@ -393,23 +384,21 @@ void decompress_pre(
} else { } else {
// copy record // copy record
size_t record_size = sizeof(Record) + n.header.record->size; size_t record_size = sizeof(Record) + n.header.record->size;
memcpy(d->buf, n.header.record, record_size); memcpy(d->data, n.header.record, record_size);
// update decom // update decom
d->remaining -= record_size; sized_buf_update(d, record_size);
d->buf += record_size;
} }
break; break;
case NT_GROUP: case NT_GROUP:
// copy header, contents will be copied while walking // copy header, contents will be copied while walking
memcpy(d->buf, n.header.group, sizeof(Group)); memcpy(d->data, n.header.group, sizeof(Group));
// save copied header location for post-walk group size recalc // save copied header location for post-walk group size recalc
*carry_out = (void *)d->buf; *carry_out = (void *)d->data;
// update decom // update decom
d->buf += sizeof(Group); sized_buf_update(d, sizeof(Group));
d->remaining -= sizeof(Group);
break; break;
default: default:
@@ -423,14 +412,14 @@ void decompress_pre(
* based on the difference between the current destination pointer and the * based on the difference between the current destination pointer and the
* group header pointer. * group header pointer.
*/ */
void decompress_post(Node n, void *decom_ptr, void **carry_in) void decompress_post(Node n, void *decom_ptr, void *carry_in)
{ {
struct decom *d = decom_ptr; struct sized_buf *d = decom_ptr;
// only need to handle group resize // only need to handle group resize
if (n.type == NT_GROUP) { if (n.type == NT_GROUP) {
Group *g = (Group *)(*carry_in); Group *g = (Group *)carry_in;
uint32_t new_size = (uint32_t)((char *)d->buf - (char *)g); uint32_t new_size = (uint32_t)((char *)d->data - (char *)g);
g->size = new_size; g->size = new_size;
} }
} }
@@ -641,7 +630,7 @@ Timestamp convert_ts(uint16_t ts)
return (Timestamp) { year, month, day }; return (Timestamp) { year, month, day };
} }
MetaNode *espr_create_tree(struct sized_buf in, struct sized_buf tree) MetaTree espr_create_tree(struct sized_buf in, struct sized_buf tree)
{ {
// create root node // create root node
MetaNode *root = (MetaNode *)tree.data; MetaNode *root = (MetaNode *)tree.data;
@@ -652,9 +641,9 @@ MetaNode *espr_create_tree(struct sized_buf in, struct sized_buf tree)
// walk // walk
struct walker_callbacks cb = { .pre = create_tree_cb, .data = &tree }; struct walker_callbacks cb = { .pre = create_tree_cb, .data = &tree };
espr_walk(in.data, in.size, cb, root); espr_walk(in, cb, root);
return root; return (MetaTree) { .root = root, .size = in.size };
} }
void create_tree_cb( void create_tree_cb(
@@ -690,3 +679,47 @@ void create_tree_cb(
*to_children = m; *to_children = m;
} }
void espr_meta_walk(MetaTree tree, struct meta_callbacks cb) {
espr_meta_node_walk(tree.root, cb);
}
void espr_meta_node_walk(MetaNode *m, struct meta_callbacks cb) {
cb.pre(m, cb.data);
MetaNode *child = m->first_child;
while (child) {
espr_meta_node_walk(child, cb);
child = child->next;
}
}
void espr_serialize(MetaTree tree, struct sized_buf out) {
struct meta_callbacks cb = { .pre = serialize_cb, .data = &out };
espr_meta_walk(tree, cb);
}
void serialize_cb(MetaNode *m, void *data) {
struct sized_buf *out = data;
// exit on empty node
if (!m->n.data)
return;
switch (m->n.type) {
case NT_GROUP:
// only serialize the header of groups
assert(out->size >= sizeof(Group));
memcpy(out->data, m->n.header.group, sizeof(Group));
sized_buf_update(out, sizeof(Group));
break;
case NT_RECORD:
size_t data_size = m->n.header.record->size;
assert(out->size >= data_size + sizeof(Record));
// serialize header and data separately as they may be
// discontiguous
memcpy(out->data, m->n.header.record, sizeof(Record));
sized_buf_update(out, sizeof(Record));
memcpy(out->data, m->n.data, data_size);
sized_buf_update(out, data_size);
}
}

View File

@@ -111,7 +111,7 @@
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard> <LanguageStandard>stdcpp17</LanguageStandard>
<LanguageStandard_C>stdc11</LanguageStandard_C> <LanguageStandard_C>stdc11</LanguageStandard_C>
<AdditionalIncludeDirectories>..\zlib</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>..\zlib-win-build</AdditionalIncludeDirectories>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
</ClCompile> </ClCompile>
<Link> <Link>