Commit cf8d93723c3794db71c89936c69334b4ce61d349
1 parent
4f07b876
Use new version of libnbt
Showing
1 changed file
with
46 additions
and
67 deletions
mcplayerstat.c
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | -#include <unistd.h> | ||
3 | -#include <sys/mman.h> | ||
4 | -#include <sys/stat.h> | ||
5 | -#include <fcntl.h> | ||
6 | -#include <stdint.h> | ||
7 | -#include <byteswap.h> | ||
8 | 2 | ||
9 | #include "nbt.h" | 3 | #include "nbt.h" |
10 | 4 | ||
11 | -double convertDouble(uint64_t data) { | ||
12 | - double d = 0; | ||
13 | - uint64_t swapped = __bswap_64(data); | ||
14 | - memcpy(&d,&swapped,sizeof(uint64_t)); | ||
15 | - return d; | ||
16 | -} | ||
17 | - | ||
18 | void printInventory(TagList *inv) { | 5 | void printInventory(TagList *inv) { |
19 | for(int j = 0; j < inv->size; ++j) { | 6 | for(int j = 0; j < inv->size; ++j) { |
20 | TagCompound item = *(TagCompound*)inv->list[j].payload; | 7 | TagCompound item = *(TagCompound*)inv->list[j].payload; |
@@ -36,63 +23,60 @@ void printInventory(TagList *inv) { | @@ -36,63 +23,60 @@ void printInventory(TagList *inv) { | ||
36 | } | 23 | } |
37 | 24 | ||
38 | int parseMCPlayerFile(const char* filename) { | 25 | int parseMCPlayerFile(const char* filename) { |
39 | - struct stat sb; | ||
40 | unsigned int pos = 0; | 26 | unsigned int pos = 0; |
27 | + void* data; | ||
41 | 28 | ||
42 | - int fd = open(filename,O_RDONLY); | ||
43 | - if(fd == -1) { | ||
44 | - perror("Can't open file"); | ||
45 | - return 3; | ||
46 | - } | ||
47 | - fstat(fd, &sb); | ||
48 | - | ||
49 | - void* mappedFile = mmap(NULL,sb.st_size,PROT_READ,MAP_PRIVATE,fd,0); | 29 | + ssize_t dblen = loadDB(filename, &data); |
30 | + if(dblen > 0) { | ||
31 | + // int playerGameType | ||
32 | + // int XpLevel | ||
33 | + // list(compound) Inventory | ||
34 | + // list(compound) EnderItems | ||
35 | + // list(float) Pos | ||
50 | 36 | ||
51 | - // int playerGameType | ||
52 | - // int XpLevel | ||
53 | - // list(compound) Inventory | ||
54 | - // list(compound) EnderItems | ||
55 | - // list(float) Pos | ||
56 | - | ||
57 | - Tag t; | ||
58 | - pos = parseTag((mappedFile + pos),&t); | ||
59 | - if(pos != sb.st_size) { | ||
60 | - fprintf(stderr,"Didn't reach end of NBT file\n"); | ||
61 | - return 4; | ||
62 | - } | 37 | + Tag t; |
38 | + pos = parseTag((data + pos),&t); | ||
39 | + if(pos != dblen) { | ||
40 | + fprintf(stderr,"Didn't reach end of NBT file\n"); | ||
41 | + return 4; | ||
42 | + } | ||
63 | 43 | ||
64 | - TagCompound root = *(TagCompound*) t.payload; | ||
65 | - for(int i = 0; i < root.numTags; ++i) { | ||
66 | - Tag node = root.list[i]; | ||
67 | - if(node.type == TAG_INT) { | ||
68 | - if(!strncmp(node.name,"playerGameType",node.nameLength)) { | ||
69 | - int playerGameType = __bswap_32(*((uint32_t*)node.payload)); | ||
70 | - printf("playerGameType: %s\n",(playerGameType == 0 ? "Survival" : (playerGameType == 1 ? "Creative" : (playerGameType == 2 ? "Adventure" : (playerGameType == 3 ? "Spectator" : "UNKNOWN"))))); | ||
71 | - } else if (!strncmp(node.name,"XpLevel",node.nameLength)) { | ||
72 | - int xpLevel = __bswap_32(*((uint32_t*)node.payload)); | ||
73 | - printf("XpLevel: %u\n",xpLevel); | ||
74 | - } | ||
75 | - } else if(node.type == TAG_LIST) { | ||
76 | - TagList* l = (TagList*)node.payload; | ||
77 | - if(!strncmp(node.name,"Inventory",node.nameLength)) { | ||
78 | - printf("Inventory:\n"); | ||
79 | - printInventory(l); | ||
80 | - } else if (!strncmp(node.name,"EnderItems",node.nameLength)) { | ||
81 | - printf("Ender Inventory:\n"); | ||
82 | - printInventory(l); | ||
83 | - } else if (!strncmp(node.name,"Pos",node.nameLength)) { | ||
84 | - double x = convertDouble((*(uint64_t*)l->list[0].payload)); | ||
85 | - double y = convertDouble((*(uint64_t*)l->list[1].payload)); | ||
86 | - double z = convertDouble((*(uint64_t*)l->list[2].payload)); | ||
87 | - printf("Pos: (x: %f,y: %f,z: %f)\n",x,y,z); | 44 | + TagCompound root = *(TagCompound*) t.payload; |
45 | + for(int i = 0; i < root.numTags; ++i) { | ||
46 | + Tag node = root.list[i]; | ||
47 | + if(node.type == TAG_INT) { | ||
48 | + if(!strncmp(node.name,"playerGameType",node.nameLength)) { | ||
49 | + int playerGameType = *((uint32_t*)node.payload); | ||
50 | + printf("playerGameType: %s\n",(playerGameType == 0 ? "Survival" : (playerGameType == 1 ? "Creative" : (playerGameType == 2 ? "Adventure" : (playerGameType == 3 ? "Spectator" : "UNKNOWN"))))); | ||
51 | + } else if (!strncmp(node.name,"XpLevel",node.nameLength)) { | ||
52 | + int xpLevel = *((uint32_t*)node.payload); | ||
53 | + printf("XpLevel: %u\n",xpLevel); | ||
54 | + } | ||
55 | + } else if(node.type == TAG_LIST) { | ||
56 | + TagList* l = (TagList*)node.payload; | ||
57 | + if(!strncmp(node.name,"Inventory",node.nameLength)) { | ||
58 | + printf("Inventory:\n"); | ||
59 | + printInventory(l); | ||
60 | + } else if (!strncmp(node.name,"EnderItems",node.nameLength)) { | ||
61 | + printf("Ender Inventory:\n"); | ||
62 | + printInventory(l); | ||
63 | + } else if (!strncmp(node.name,"Pos",node.nameLength)) { | ||
64 | + double x = (*(double*)l->list[0].payload); | ||
65 | + double y = (*(double*)l->list[1].payload); | ||
66 | + double z = (*(double*)l->list[2].payload); | ||
67 | + printf("Pos: (x: %f,y: %f,z: %f)\n",x,y,z); | ||
68 | + } | ||
88 | } | 69 | } |
89 | } | 70 | } |
71 | + | ||
72 | + destroyTag(&t); | ||
73 | + free(data); | ||
74 | + return 0; | ||
90 | } | 75 | } |
91 | 76 | ||
92 | - destroyTag(&t); | ||
93 | - munmap(mappedFile,sb.st_size); | ||
94 | - close(fd); | ||
95 | - return 0; | 77 | + |
78 | + | ||
79 | + | ||
96 | } | 80 | } |
97 | 81 | ||
98 | int main(int argc, char** argv) { | 82 | int main(int argc, char** argv) { |
@@ -101,10 +85,5 @@ int main(int argc, char** argv) { | @@ -101,10 +85,5 @@ int main(int argc, char** argv) { | ||
101 | return 1; | 85 | return 1; |
102 | } | 86 | } |
103 | 87 | ||
104 | - if(access(argv[1],R_OK) == -1) { | ||
105 | - perror("Can't access file"); | ||
106 | - return 2; | ||
107 | - } | ||
108 | - | ||
109 | return parseMCPlayerFile(argv[1]); | 88 | return parseMCPlayerFile(argv[1]); |
110 | } | 89 | } |
111 | \ No newline at end of file | 90 | \ No newline at end of file |