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>
<RootNamespace>NavmeshList</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>NavmeshList</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">

View File

@@ -11,50 +11,103 @@
#include <assert.h>
int main(void) {
FILE *fp;
errno_t ret = fopen_s(&fp, "Skyrim.esm", "rb");
errno_t ret = 0;
if (ret || !fp)
return ret;
// Read in the esp file
struct sized_buf esp_buf = { 0 };
{
FILE *fp;
ret = fopen_s(&fp, "Skyrim.esm", "rb");
fseek(fp, 0L, SEEK_END);
size_t size = ftell(fp);
rewind(fp);
if (ret || !fp)
goto exit1;
char *buffer = malloc(size);
if (!buffer)
return errno;
fseek(fp, 0L, SEEK_END);
esp_buf.size = ftell(fp);
rewind(fp);
size_t read = fread(buffer, sizeof(char), size, fp);
assert(read == size);
esp_buf.data = malloc(esp_buf.size);
if (!esp_buf.data) {
ret = errno;
goto exit1;
}
struct sized_buf esp = { .data = buffer, .size = size };
size_t read = fread(esp_buf.data, sizeof(char), esp_buf.size, fp);
fclose(fp);
assert(read == esp_buf.size);
}
struct esp_stats stats = espr_stats(buffer, size);
// Calculate esp stats
struct esp_stats stats = espr_stats(esp_buf);
/*
char *decompressed = malloc(stats.decompressed_size);
if (!decompressed)
return errno;
// 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_print(buffer, size);
espr_decompress(esp_buf, decom_buf);
}
espr_decompress(buffer, size, decompressed, stats.decompressed_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);
}
size_t tree_size = espr_tree_size(stats);
char *tree_data = malloc(tree_size);
struct sized_buf tree = { .data = tree_data, .size = tree_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;
}
MetaNode *root = espr_create_tree(esp, tree);
espr_serialize(tree, serialized_buf);
free(buffer);
printf("Serialized.\n");
/*
espr_print(decompressed, stats.decompressed_size);
FILE *fp;
ret = fopen_s(&fp, "Test.esm", "wb");
free(decompressed);
*/
if (ret || !fp)
goto exit5;
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;
}