Files
Navmesher_old/NavmeshList/main.c
2022-09-09 19:40:22 +10:00

114 lines
3.0 KiB
C

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0.If a copy of the MPL was not distributed with this
* file, You can obtain one at http ://mozilla.org/MPL/2.0/.
*/
#include "ESPReader.h"
#include <stdio.h>
#include <stdlib.h>
#undef NDEBUG
#include <assert.h>
int main(void) {
errno_t ret = 0;
// Read in the esp file
SizedBuf esp_buf = { 0 };
{
FILE *fp;
ret = fopen_s(&fp, "Skyrim.esm", "rb");
if (ret || !fp)
goto exit1;
fseek(fp, 0L, SEEK_END);
esp_buf.size = ftell(fp);
rewind(fp);
esp_buf.data = malloc(esp_buf.size);
if (!esp_buf.data) {
ret = errno;
goto exit1;
}
size_t read = fread(esp_buf.data, sizeof(char), esp_buf.size, fp);
fclose(fp);
assert(read == esp_buf.size);
}
// Calculate esp stats
ESPStats stats = espr_stats(esp_buf);
// Decompress the esp file
SizedBuf 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);
}
// Construct the meta tree
SizedBuf 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);
}
// Serialize and write out the uncompressed esp
SizedBuf serialized_buf = { 0 };
{
serialized_buf.size = tree.size;
serialized_buf.data = malloc(serialized_buf.size);
if (!serialized_buf.data) {
ret = errno;
goto exit4;
}
espr_serialize(tree, serialized_buf);
printf("Serialized.\n");
FILE *fp;
ret = fopen_s(&fp, "Test.esm", "wb");
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;
}