Skip to content

Commit

Permalink
Added blockPrint
Browse files Browse the repository at this point in the history
  • Loading branch information
TCA166 committed Apr 20, 2024
1 parent b7982d4 commit b58ab3d
Show file tree
Hide file tree
Showing 22 changed files with 174 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ mtlGen
*Check*
tests/benchmark
mtlFiles
doc/build/*
doc/build/*
blockPrint
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ ifneq (,$(findstring MSYS,$(UNAME)))
endif

all: CFLAGS := -O3
all: radiusGenerator modelGenerator chunkExtractor regionFileReader
all: radiusGenerator modelGenerator chunkExtractor regionFileReader blockPrint

debug: CFLAGS := -Wall -Werror -Wpedantic -g
debug: radiusGenerator modelGenerator chunkExtractor regionFileReader
debug: radiusGenerator modelGenerator chunkExtractor regionFileReader blockPrint

cNBT.o:
gcc cNBT/buffer.c -o cNBT/buffer.o -c $(CFLAGS)
Expand Down Expand Up @@ -53,6 +53,9 @@ modelGenerator: model.o generator.o hTable.o chunkParser.o cNBT.o src/modelGener
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)

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

cNBT.ow:
x86_64-w64-mingw32-gcc-win32 cNBT/buffer.c -o cNBT/buffer.ow -c $(CFLAGS)
x86_64-w64-mingw32-gcc-win32 cNBT/nbt_parsing.c -o cNBT/nbt_parsing.ow -c $(CFLAGS)
Expand Down Expand Up @@ -103,6 +106,8 @@ clean:
rm -f regionFileReader
rm -f regionFileReader.exe
rm -rf doc/build
rm -f blockPrint
rm -f blockPrint.exe

check: hTable.o regionParser.o chunkParser.o cNBT.o model.o
#hTable tests
Expand All @@ -128,5 +133,5 @@ check: hTable.o regionParser.o chunkParser.o cNBT.o model.o

doc: doc/build/html

doc/build/html: src/lib/*.h src/*.c src/lib/*.c doc/Doxyfile.conf src/dir.dox doc/src/*
doc/build/html: src/lib/*.h src/*.c src/lib/*.c doc/Doxyfile.conf src/dir.dox doc/pages/*
doxygen ./doc/Doxyfile.conf
2 changes: 1 addition & 1 deletion doc/Doxyfile.conf
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ WARN_LOGFILE =
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
# Note: If this tag is empty the current directory is searched.

INPUT = ./src ./doc/index.md ./doc/src
INPUT = ./src ./doc/index.md ./doc/pages

# This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
Expand Down
File renamed without changes.
File renamed without changes.
82 changes: 82 additions & 0 deletions src/blockPrint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*!
@file blockPrint.c
@brief Prints the number of each block in a chunk
@details This program prints the number of each block in a chunk
*/

#include <stdlib.h>
#include <stdio.h>
#include "./lib/errorDefs.h"
#include "./lib/chunkParser.h"
#include "./lib/hTable.h"

/*!
@brief Loads an nbt file and prints the number of each block in a chunk
@details the association of block type to block count is stored in a hash table
@see chunkParser.h
@see hTable.h
*/
int main(int argc, char** argv){
if(argc < 2){
argCountError("blockPrint <path to nbt file>");
}
unsigned char* data;
long sz;
{
FILE* file = fopen(argv[1], "rb");
if(file == NULL){
fileError(argv[1], "opened");
}
if(fseek(file, 0, SEEK_END) != 0){
fileError(argv[1], "seeked");
}
sz = ftell(file);
if(fseek(file, 0, SEEK_SET) != 0){
fileError(argv[1], "seeked");
}
data = malloc(sz);
if(fread(data, sz, 1, file) != 1){
fileError(argv[1], "read");
}
fclose(file);
}
section sections[maxSections];
int sectionN = getSections(data, sz, sections);
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);
if(blockStates == NULL){
char* key = sections[i].blockPalette[0];
if(getVal(blockCount, key) == NULL){
int* val = malloc(sizeof(int));
insertHashItem(blockCount, key, val);
}
else{
int* val = getVal(blockCount, key);
*val += 1;
}
}
else{
for(int j = 0; j < outLen; j++){
char* key = sections[i].blockPalette[blockStates[j]];
if(getVal(blockCount, key) == NULL){
int* val = malloc(sizeof(int));
insertHashItem(blockCount, key, val);
}
else{
int* val = getVal(blockCount, key);
*val += 1;
}
}
}
free(blockStates);
}
freeSections(sections, sectionN);
forHashTableItem(blockCount){
printf("%s: %d\n", item->key, *(int*)item->value);
}
freeHashTable(blockCount, free);
return EXIT_SUCCESS;
}
3 changes: 1 addition & 2 deletions src/chunkExtractor.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
@details Loads a chunk from a region file using the regionParser library, then writes it to a file using extractChunk.
@see regionParser.h
@see @ref extractChunk
@return The chunk object
*/
int main(int argc, char** argv){
if(argc < 3){
argCountError();
argCountError("chunkExtractor <path to region directory> <x> <z>");
}
for(int i = 3; i < argc; i+=3){
int x = atoi(argv[i - 1]);
Expand Down
5 changes: 0 additions & 5 deletions src/dir.dox
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,3 @@
@brief Directory for library sourcecode
@details contains all of the C libraries
*/

/*!
@dir /src/scripts
@brief Directory for Python utility scripts
*/
1 change: 1 addition & 0 deletions src/lib/chunkParser.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,5 +314,6 @@ void freeSections(section* sections, int sectionLen){
free(sections[i].blockPalette[n]);
}
free(sections[i].blockPalette);
free(sections[i].blockData);
}
}
9 changes: 7 additions & 2 deletions src/lib/chunkParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
@details This file contains functions for parsing chunk data
*/

#ifndef CHUNKPARSER_H
#define CHUNKPARSER_H

/*!
@defgroup chunkParser Chunk Parser
@brief Chunk data parsing functions
Expand Down Expand Up @@ -120,7 +123,7 @@ unsigned int getSections(unsigned char* nbtFileData, long sz, section* sections)
@details Returns NULL if it cannot be created. Be sure to free the result once you are done with it.
@param s The section to create the block states from
@param outLen The length of the block states array
@return The block states array
@return The block states array, or NULL if it cannot be created
@ingroup chunkParser
*/
unsigned int* getBlockStates(section s, int* outLen);
Expand Down Expand Up @@ -155,4 +158,6 @@ char** createGlobalPalette(section* sections, int len, int* outLen, bool freeSec
@param sectionLen The length of the sections array
@ingroup chunkParser
*/
void freeSections(section* sections, int sectionLen);
void freeSections(section* sections, int sectionLen);

#endif
9 changes: 7 additions & 2 deletions src/lib/errorDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

//Since the ERRNO macros are very unspecific and as far as I can see usually associated with errors a kernel may encounter, I have only loosely associated them with my own error convention

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

/*!
@defgroup errorDefs Error Definitions
@brief Contains error definitions for the project
Expand Down Expand Up @@ -106,10 +110,11 @@
/*!
* @def argCountError
* @brief Reports an error when an invalid amount of arguments is provided
* @param usage The usage of the program
* @ingroup errorDefs
*/
#define argCountError() \
fprintf(stderr, "Invalid number of arguments was provided\n."); \
#define argCountError(usage) \
fprintf(stderr, "Invalid number of arguments was provided\n" usage "\n"); \
errno = EINVAL; \
perror("Arg count error."); \
exit(EXIT_FAILURE);
Expand Down
5 changes: 2 additions & 3 deletions src/lib/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ model generateFromNbt(unsigned char* data, int dataSize, hashTable* materials, h
freeHashTable(materials);
}
if(!f){
long count = cullFaces(&cubeModel, !b, objects);
printf("%ld model faces culled\n", count);
cullFaces(&cubeModel, !b, objects);
//printf("%ld model faces culled\n", count);
}
model newModel = cubeModelToModel(&cubeModel, objects);
newModel.materialArr = materialsArr;
Expand All @@ -59,7 +59,6 @@ cubeModel createCubeModel(section* sections, int sectionLen, hashTable* material
for(int i = 0; i < sectionLen; i++){
//create the block state array
unsigned int* states = getBlockStates(sections[i], NULL);
free(sections[i].blockData);
//if we want to do face culling we first need to actually have all the blocks in one place
for(int x = 0; x < 16; x++){
for(int y = 0; y < 16; y++){
Expand Down
7 changes: 6 additions & 1 deletion src/lib/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
@details This file contains functions for generating 3d models from nbt data
*/

#ifndef GENERATOR_H
#define GENERATOR_H

#include <stdbool.h>
#include "model.h"
#include "chunkParser.h"
Expand Down Expand Up @@ -69,4 +72,6 @@ model generateFromNbt(unsigned char* data, int dataSize, hashTable* materials, h
@param objects The hashTable to free
@ingroup generator
*/
void freeObjectsHashTable(hashTable* objects);
void freeObjectsHashTable(hashTable* objects);

#endif
18 changes: 11 additions & 7 deletions src/lib/hTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,24 @@ hashTable* initHashTable(size_t size){
/*!
@brief Frees a given hash item
@param item the item to free
@param freeValue the function to free the value of the item
*/
static void freeHashItem(struct hTableItem* item){
static void freeHashItem(struct hTableItem* item, freeFunction freeValue){
if(item->next != NULL){
freeHashItem(item->next);
freeHashItem(item->next, freeValue);
}
if(freeValue != NULL){
freeValue(item->value);
}
free(item->key);
free(item);
}

void freeHashTable(hashTable* table){
void freeHashTable(hashTable* table, freeFunction freeValue){
for(int i = 0; i < table->size; i++){
struct hTableItem* item = table->items[i];
if(item != NULL){
freeHashItem(item);
freeHashItem(item, freeValue);
table->items[i] = NULL;
}
}
Expand All @@ -83,14 +87,14 @@ int insertHashItem(hashTable* table, const char* key, const void* value){
}
else if(strcmp(key, currentItem->key) == 0){ //if it isn't we try updating it
currentItem->value = (void*)value;
freeHashItem(newItem);
freeHashItem(newItem, NULL);
}
else{ //else we navigate the linked list
struct hTableItem* nextItem = currentItem->next;
while(nextItem != NULL){ //as long as the next item isn't NULL
if(strcmp(key, currentItem->key) == 0){ //we try updating
currentItem->value = (void*)value;
freeHashItem(newItem);
freeHashItem(newItem, NULL);
return table->count;
}
else{ //or move forward
Expand All @@ -100,7 +104,7 @@ int insertHashItem(hashTable* table, const char* key, const void* value){
}
if(strcmp(key, currentItem->key) == 0){ //one last try at updating
currentItem->value = (void*)value;
freeHashItem(newItem);
freeHashItem(newItem, NULL);
return table->count;
}
currentItem->next = newItem;
Expand Down
19 changes: 17 additions & 2 deletions src/lib/hTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
@brief A hash table implementation
*/

#ifndef HTABLE_H
#define HTABLE_H

/*!
@defgroup hTable Hash Table
@brief A hash table implementation
Expand Down Expand Up @@ -56,10 +59,19 @@ typedef struct{
int count;
} hashTable;

/*!
@typedef freeFunction
@brief A function pointer type for freeing memory
@param ptr The pointer to free
@ingroup hTable
*/
typedef void (*freeFunction)(void* ptr);

/*!
@def forHashTableItem(key, table)
@brief Macro wrapper for two for loops that provides a struct hTableItem* item variable for your use
@param table The hash table to get the index for
@details Provides a struct hTableItem* item variable for your use
@ingroup hTable
*/
#define forHashTableItem(table) \
Expand All @@ -77,9 +89,10 @@ hashTable* initHashTable(size_t size);
/*!
@brief Frees all the allocated memory in the hash table including the items, but not the values of the items.
@param table The hash table to free
@param freeValue The function to free the values of the items, can be NULL
@ingroup hTable
*/
void freeHashTable(hashTable* table);
void freeHashTable(hashTable* table, freeFunction freeValue);

/*!
@brief Inserts the given void* as the value of the given key in the given hashTable.
Expand Down Expand Up @@ -107,4 +120,6 @@ void* getVal(hashTable* table, const char* key);
@return The array of value pointers
@ingroup hTable
*/
void** hashTableToArray(hashTable* table);
void** hashTableToArray(hashTable* table);

#endif
5 changes: 5 additions & 0 deletions src/lib/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,11 @@ hashTable* getMaterials(char* filename){
return result;
}

void freeMaterial(material* m){
free(m->name);
free(m);
}

hashTable* readWavefront(char* filename, hashTable* materials, unsigned int side){
FILE* fp = fopen(filename, "r");
if(fp == NULL){
Expand Down
11 changes: 11 additions & 0 deletions src/lib/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
@brief Definitions for 3D model structs and functions
*/

#ifndef MODEL_H
#define MODEL_H

/*!
@defgroup model 3D Model
@brief Definitions for 3D model structs and functions
Expand Down Expand Up @@ -510,3 +513,11 @@ cube createGenericCube(unsigned int side);
@ingroup model
*/
object modelToObject(const model* m, const char* type);

/*!
@brief Frees a material struct
@param m The material to free
*/
void freeMaterial(material* m);

#endif
Loading

0 comments on commit b58ab3d

Please sign in to comment.