Skip to content

Commit

Permalink
Reworked error checking
Browse files Browse the repository at this point in the history
Also fixed compile time error for tests
  • Loading branch information
TCA166 committed Apr 20, 2024
1 parent b58ab3d commit 18844f9
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 109 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"generator.h": "c",
"stdbool.h": "c",
"htable.h": "c",
"stdlib.h": "c"
"stdlib.h": "c",
"functional": "c"
},
"cSpell.words": [
"blockstates",
Expand Down
35 changes: 19 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ all: radiusGenerator modelGenerator chunkExtractor regionFileReader blockPrint
debug: CFLAGS := -Wall -Werror -Wpedantic -g
debug: radiusGenerator modelGenerator chunkExtractor regionFileReader blockPrint

mcErrno.o: src/lib/mcErrno.c
gcc src/lib/mcErrno.c -o mcErrno.o -c $(CFLAGS)

cNBT.o:
gcc cNBT/buffer.c -o cNBT/buffer.o -c $(CFLAGS)
gcc cNBT/nbt_parsing.c -o cNBT/nbt_parsing.o -c $(CFLAGS)
Expand All @@ -38,23 +41,23 @@ hTable.o: src/lib/hTable.c
model.o: src/lib/model.c
gcc src/lib/model.c -o model.o -c $(CFLAGS)

regionFileReader: regionParser.o src/regionFileReader.c
gcc src/regionFileReader.c regionParser.o -o regionFileReader $(ZLIB) -lm $(CFLAGS)
regionFileReader: regionParser.o mcErrno.o src/regionFileReader.c
gcc src/regionFileReader.c regionParser.o mcErrno.o -o regionFileReader $(ZLIB) -lm $(CFLAGS)

chunkExtractor: regionParser.o src/chunkExtractor.c
gcc src/chunkExtractor.c regionParser.o -o chunkExtractor $(ZLIB) -lm $(CFLAGS)
chunkExtractor: regionParser.o mcErrno.o src/chunkExtractor.c
gcc src/chunkExtractor.c regionParser.o mcErrno.o -o chunkExtractor $(ZLIB) -lm $(CFLAGS)

generator.o: src/lib/generator.c
gcc src/lib/generator.c -o generator.o -c $(CFLAGS)

modelGenerator: model.o generator.o hTable.o chunkParser.o cNBT.o src/modelGenerator.c
gcc src/modelGenerator.c generator.o model.o hTable.o chunkParser.o cNBT.o -o modelGenerator -lm $(CFLAGS)
modelGenerator: model.o generator.o hTable.o mcErrno.o chunkParser.o cNBT.o src/modelGenerator.c
gcc src/modelGenerator.c generator.o model.o mcErrno.o hTable.o chunkParser.o cNBT.o -o modelGenerator -lm $(CFLAGS)

radiusGenerator: model.o generator.o hTable.o chunkParser.o regionParser.o cNBT.o src/radiusGenerator.c
gcc src/radiusGenerator.c generator.o model.o regionParser.o hTable.o chunkParser.o cNBT.o $(ZLIB) -lm -o radiusGenerator $(CFLAGS)
radiusGenerator: model.o generator.o hTable.o mcErrno.o chunkParser.o regionParser.o cNBT.o src/radiusGenerator.c
gcc src/radiusGenerator.c generator.o model.o mcErrno.o regionParser.o hTable.o chunkParser.o cNBT.o $(ZLIB) -lm -o radiusGenerator $(CFLAGS)

blockPrint: src/blockPrint.c chunkParser.o cNBT.o hTable.o
gcc src/blockPrint.c chunkParser.o cNBT.o hTable.o -o blockPrint -lm $(CFLAGS)
blockPrint: src/blockPrint.c chunkParser.o mcErrno.o cNBT.o hTable.o
gcc src/blockPrint.c chunkParser.o cNBT.o mcErrno.o hTable.o -o blockPrint -lm $(CFLAGS)

cNBT.ow:
x86_64-w64-mingw32-gcc-win32 cNBT/buffer.c -o cNBT/buffer.ow -c $(CFLAGS)
Expand Down Expand Up @@ -109,26 +112,26 @@ clean:
rm -f blockPrint
rm -f blockPrint.exe

check: hTable.o regionParser.o chunkParser.o cNBT.o model.o
check: hTable.o regionParser.o chunkParser.o cNBT.o model.o mcErrno.o
#hTable tests
checkmk tests/hTable.check > tests/hTableCheck.c
gcc tests/hTableCheck.c hTable.o -lcheck -g -lm $(SUBUNIT) -Wall -o tests/hTableCheck
gcc tests/hTableCheck.c hTable.o mcErrno.o -lcheck -g -lm $(SUBUNIT) -Wall -o tests/hTableCheck
./tests/hTableCheck
#regionParser tests
checkmk tests/regionParser.check > tests/regionParserCheck.c
gcc tests/regionParserCheck.c regionParser.o -lcheck -g -lm $(ZLIB) $(SUBUNIT) -o tests/regionParserCheck
gcc tests/regionParserCheck.c regionParser.o mcErrno.o -lcheck -g -lm $(ZLIB) $(SUBUNIT) -o tests/regionParserCheck
./tests/regionParserCheck
#chunkParser tests
checkmk tests/chunkParser.check > tests/chunkParserCheck.c
gcc tests/chunkParserCheck.c chunkParser.o cNBT.o -lcheck -g -lm $(SUBUNIT) -o tests/chunkParserCheck
gcc tests/chunkParserCheck.c chunkParser.o cNBT.o mcErrno.o -lcheck -g -lm $(SUBUNIT) -o tests/chunkParserCheck
./tests/chunkParserCheck
#model tests
checkmk tests/model.check > tests/modelCheck.c
gcc tests/modelCheck.c model.o hTable.o -lcheck -g -lm $(SUBUNIT) -o tests/modelCheck
gcc tests/modelCheck.c model.o hTable.o mcErrno.o -lcheck -g -lm $(SUBUNIT) -o tests/modelCheck
./tests/modelCheck
#wavefront tests - doesn't work on windows because os bs
#checkmk tests/wavefront.check > tests/wavefrontCheck.c
#gcc tests/wavefrontCheck.c model.o hTable.o -lcheck -g -lm $(SUBUNIT) -o tests/wavefrontCheck
#gcc tests/wavefrontCheck.c model.o hTable.o mcErrno.o -lcheck -g -lm $(SUBUNIT) -o tests/wavefrontCheck
#./tests/wavefrontCheck

doc: doc/build/html
Expand Down
11 changes: 7 additions & 4 deletions src/blockPrint.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,37 @@
int main(int argc, char** argv){
if(argc < 2){
argCountError("blockPrint <path to nbt file>");
errorCheck("argCountError");
}
unsigned char* data;
long sz;
{
FILE* file = fopen(argv[1], "rb");
if(file == NULL){
fileError(argv[1], "opened");
errorCheck("fopen");
}
if(fseek(file, 0, SEEK_END) != 0){
fileError(argv[1], "seeked");
errorCheck("fseek");
}
sz = ftell(file);
if(fseek(file, 0, SEEK_SET) != 0){
fileError(argv[1], "seeked");
errorCheck("fseek");
}
data = malloc(sz);
if(fread(data, sz, 1, file) != 1){
fileError(argv[1], "read");
errorCheck("fread");
}
fclose(file);
}
section sections[maxSections];
int sectionN = getSections(data, sz, sections);
errorCheck("getSections");
free(data);
hashTable* blockCount = initHashTable(49152); //max blocks in chunk divided by 2
for(int i = 0; i < sectionN; i++){
int outLen;
unsigned int* blockStates = getBlockStates(sections[i], &outLen);
errorCheck("getBlockStates");
if(blockStates == NULL){
char* key = sections[i].blockPalette[0];
if(getVal(blockCount, key) == NULL){
Expand Down
6 changes: 4 additions & 2 deletions src/chunkExtractor.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@
int main(int argc, char** argv){
if(argc < 3){
argCountError("chunkExtractor <path to region directory> <x> <z>");
errorCheck("argCountError");
}
for(int i = 3; i < argc; i+=3){
int x = atoi(argv[i - 1]);
int z = atoi(argv[i]);
chunk ourChunk = extractChunk(argv[i - 2], x, z);
errorCheck("extractChunk");
char* filename = malloc(8 + 10 + 10);
sprintf(filename, "./%d.%d.nbt", x, z);
FILE* outFile = fopen(filename, "wb");
if(outFile == NULL){
fileError(filename, "opened");
errorCheck("fopen");
}
if(fwrite(ourChunk.data, ourChunk.byteLength, 1, outFile) != 1){
fileError(filename, "written to");
errorCheck("fwrite");
}
free(filename);
fclose(outFile);
Expand Down
10 changes: 9 additions & 1 deletion src/lib/chunkParser.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ unsigned int getSections(unsigned char* nbtFileData, long sz, section* sections)
nbt_node* node = nbt_parse(nbtFileData, sz);
if(errno != 0){
cNBTError("the nbt file")
return 0;
}
if(node->type != TAG_COMPOUND){
nbtTypeError(node->type, 10);
return 0;
}
//Debug message
//const struct list_head* head = &node->payload.tag_compound->entry;
Expand All @@ -79,6 +81,7 @@ unsigned int getSections(unsigned char* nbtFileData, long sz, section* sections)
nbt_node* sectionsNode = nbt_find_by_name(node, "sections");
if(sectionsNode == NULL){
nbtTagError("sections");
return 0;
}
struct nbt_list* sectionsList = sectionsNode->payload.tag_list;
struct list_head* pos;
Expand All @@ -93,12 +96,14 @@ unsigned int getSections(unsigned char* nbtFileData, long sz, section* sections)
nbt_node* yNode = nbt_find_by_name(compound, "Y");
if(yNode == NULL){
nbtTagError("Y");
return 0;
}
newSection.y = yNode->payload.tag_byte;
//get the block data
nbt_node* blockNode = nbt_find_by_name(compound, "block_states");
if(blockNode == NULL){
nbtTagError("block_states");
return 0;
}
//get the individual block data
nbt_node* blockData = nbt_find_by_name(blockNode, "data");
Expand All @@ -115,6 +120,7 @@ unsigned int getSections(unsigned char* nbtFileData, long sz, section* sections)
nbt_node* palette = nbt_find_by_name(blockNode, "palette");
if(palette == NULL){
nbtTagError("palette");
return 0;
}
//const struct list_head* paletteHead = &palette->payload.tag_list->entry;
char** blockPalette = malloc(sizeof(char*));
Expand All @@ -127,6 +133,7 @@ unsigned int getSections(unsigned char* nbtFileData, long sz, section* sections)
nbt_node* string = nbt_find_by_name(pal->data, "Name");
if(string == NULL){
nbtTagError("Name");
return 0;
}
//Instead of appending all properties in block using a for loop we extract a few specific ones
char* direction = NULL;
Expand Down Expand Up @@ -202,6 +209,7 @@ unsigned int getSections(unsigned char* nbtFileData, long sz, section* sections)
newSection.blockPalette = blockPalette;
if(i == UINT32_MAX){
overflowError("UINT32");
return 0;
}
newSection.paletteLen = i;
sections[n] = newSection;
Expand Down Expand Up @@ -260,7 +268,7 @@ block createBlock(int x, int y, int z, unsigned int* blockStates, section parent
int state = blockStates[blockPos];
//paletteLen and I are fine it must be something with the data extraction process
if(state >= parentSection.paletteLen){
statesError(state, parentSection.paletteLen, newBlock);
statesWarning(state, parentSection.paletteLen, newBlock);
newBlock.type = mcAir;
}
else{
Expand Down
Loading

0 comments on commit 18844f9

Please sign in to comment.