i honestly dont remember what changes this is, sorry to anyone reading this. i am committing currently so i can work on this at a different place.

This commit is contained in:
hayden 2019-08-21 14:40:10 -05:00
parent 09412cbd97
commit 720e3762c4
5 changed files with 124 additions and 12 deletions

6
README
View File

@ -23,11 +23,10 @@ features
planned
= = = = = = = = = = = = = = = = = = = = = = = = =
- Makefile based builds (no more cmake!)
- offline and online login
- multiple users
- online login
- async world handling
- chat
- multiple users
- commands
- plugins (in c of course)
- protocol compression
@ -35,6 +34,7 @@ planned
libraries
= = = = = = = = = = = = = = = = = = = = = = = = =
- dyad (https://github.com/rxi/dyad)
- vec (https://github.com/rxi/vec)
- log.c (https://github.com/rxi/log.c.git)
dependencies

View File

@ -13,4 +13,7 @@ void* scalloc(size_t size, unsigned long line);
/* same as above but with realloc */
void srealloc(void *ptr, size_t size, unsigned long line);
/* swap endian of a number */
void swap_endian(void*, size_t size);
#endif

View File

@ -38,12 +38,6 @@ main(int argc, char** argv)
// create and setup the server struct
server = SAFE_MALLOC(sizeof(struct server));
log_info("Loading world");
FILE* f = fopen("level.dat", "rb");
struct nbt_node *node;
nbt_parse_file(node, f);
fclose(f);
log_info("Starting network thread");
dyad_init();
server->dyad_stream = dyad_newStream();

106
src/nbt.c
View File

@ -71,7 +71,6 @@ nbt_parse_node(struct nbt_node *node, uint8_t *data, size_t data_len)
size_t size;
size = 0;
node = SAFE_MALLOC(sizeof(struct nbt_node));
node->name_len = 0;
node->name = NULL;
@ -84,6 +83,7 @@ nbt_parse_node(struct nbt_node *node, uint8_t *data, size_t data_len)
}
node->name_len = ((int16_t*)(data + size))[0];
swap_endian(&node->name_len, 2);
size += 2;
if (node->name_len == 0) {
@ -96,9 +96,71 @@ nbt_parse_node(struct nbt_node *node, uint8_t *data, size_t data_len)
}
switch (node->type) {
case TAG_COMPOUND:
case TAG_BYTE:
node->payload.tag_byte = ((int8_t*)(data + size))[0];
size += 1;
break;
case TAG_SHORT:
node->payload.tag_short = ((int16_t*)(data + size))[0];
swap_endian(&node->payload.tag_short, 2);
size += 2;
break;
case TAG_INT:
node->payload.tag_int = ((int32_t*)(data + size))[0];
swap_endian(&node->payload.tag_int, 4);
size += 4;
break;
case TAG_LONG:
node->payload.tag_long = ((int64_t*)(data + size))[0];
swap_endian(&node->payload.tag_long, 8);
size += 8;
break;
case TAG_FLOAT:
node->payload.tag_float = ((float*)(data + size))[0];
swap_endian(&node->payload.tag_float, 4);
size += 4;
break;
case TAG_DOUBLE:
node->payload.tag_long = ((double*)(data + size))[0];
swap_endian(&node->payload.tag_double, 8);
size += 8;
break;
case TAG_STRING:
{
size_t len;
len = ((int16_t*)(data + size))[0];
swap_endian(&len, 2);
size += 2;
if (len == 0){
node->payload.tag_string = NULL;
} else {
node->payload.tag_string = SAFE_MALLOC(len + 1);
memcpy(node->payload.tag_string, data + size, len);
node->payload.tag_string[len] = '\0';
size += len;
}
}
break;
case TAG_COMPOUND:
vec_init(&node->payload.tag_compound);
while (1) {
struct nbt_node *child_node;
child_node = SAFE_MALLOC(sizeof(struct nbt_node));
size += nbt_parse_node(child_node, data + size, data_len - size);
vec_push(&node->payload.tag_compound, child_node);
if (child_node->type == TAG_END){
break;
}
}
break;
default:
/* TODO remove this */
log_error("This NBT type has not been implimented");
}
return size;
@ -136,3 +198,41 @@ nbt_parse_bytes(struct nbt_node *node, uint8_t *data, size_t data_len)
free(idata);
return true;
}
struct nbt_node*
nbt_node_get(struct nbt_node node, char* name)
{
struct nbt_node *val;
int i;
if (node.type != TAG_COMPOUND) return NULL;
vec_foreach(&node.payload.tag_compound, val, i){
if (strcmp(node.name, val->name) == 0) {
return val;
}
}
return NULL;
}
void
nbt_node_destroy(struct nbt_node* node)
{
free(node->name);
switch (node->type) {
case TAG_STRING:
free(node->payload.tag_string);
break;
case TAG_COMPOUND:
{
int i;
struct nbt_node *val;
vec_foreach(&node->payload.tag_compound, val, i) {
nbt_node_destroy(val);
}
}
break;
}
free(node);
}

View File

@ -1,4 +1,5 @@
/* see LICENSE file for copyright and license details. */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -40,3 +41,17 @@ srealloc(void *ptr, size_t size, unsigned long line)
exit(EXIT_FAILURE);
}
}
void
swap_endian(void *data, size_t size)
{
uint8_t *d, tmp;
int i;
d = (uint8_t*)data;
for (i = 0;i < size / 2; i++) {
tmp = d[i];
d[i] = d[size - 1 - i];
d[size - 1 - i] = tmp;
}
}