mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-18 11:35:46 +01:00
Compare commits
10 Commits
main
...
7bdee8215f
| Author | SHA1 | Date | |
|---|---|---|---|
| 7bdee8215f | |||
| fb52517f06 | |||
| 63df7430b7 | |||
| 527b1e2b16 | |||
| 08ab721622 | |||
| 0baa1ac9e8 | |||
| 9cb0631df8 | |||
| 008604ff5e | |||
| 56427673cb | |||
| 93db7681eb |
@@ -1,9 +1,45 @@
|
|||||||
/* Copyright (c) 2025 Quinn
|
/* Copyright (c) 2025 Quinn
|
||||||
* Licensed under the MIT Licence. See LICENSE for details */
|
* Licensed under the MIT Licence. See LICENSE for details */
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#if __has_include(<endian.h>)
|
#ifndef PORTABLE_ENDIAN_H
|
||||||
#include <endian.h>
|
#define PORTABLE_ENDIAN_H 1
|
||||||
|
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
|
||||||
|
/* test for the byteswap header */
|
||||||
|
#if __has_include(<byteswap.h>)
|
||||||
|
#include <byteswap.h>
|
||||||
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||||
|
#define le16toh(x) (x)
|
||||||
|
#define le32toh(x) (x)
|
||||||
|
#define le64toh(x) (x)
|
||||||
|
#define htole16(x) (x)
|
||||||
|
#define htole32(x) (x)
|
||||||
|
#define htole64(x) (x)
|
||||||
|
#define be16toh(x) __bswap16(x)
|
||||||
|
#define be32toh(x) __bswap32(x)
|
||||||
|
#define be64toh(x) __bswap64(x)
|
||||||
|
#define htobe16(x) __bswap16(x)
|
||||||
|
#define htobe32(x) __bswap32(x)
|
||||||
|
#define htobe64(x) __bswap64(x)
|
||||||
|
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||||
|
#define le16toh(x) __bswap16(x)
|
||||||
|
#define le32toh(x) __bswap32(x)
|
||||||
|
#define le64toh(x) __bswap64(x)
|
||||||
|
#define htole16(x) __bswap16(x)
|
||||||
|
#define htole32(x) __bswap32(x)
|
||||||
|
#define htole64(x) __bswap64(x)
|
||||||
|
#define be16toh(x) (x)
|
||||||
|
#define be32toh(x) (x)
|
||||||
|
#define be64toh(x) (x)
|
||||||
|
#define htobe16(x) (x)
|
||||||
|
#define htobe32(x) (x)
|
||||||
|
#define htobe64(x) (x)
|
||||||
|
#else
|
||||||
|
#error machine architecture unsupported! Expected either big-endian or little-endian, make sure to use a compiler which defines __BYTE_ORDER__ (like clang or gcc)
|
||||||
|
#endif /* byte order */
|
||||||
|
|
||||||
|
/* otherwise, utilise the compiler built-ins */
|
||||||
#else
|
#else
|
||||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||||
#define le16toh(x) __uint16_identity(x)
|
#define le16toh(x) __uint16_identity(x)
|
||||||
@@ -33,5 +69,12 @@
|
|||||||
#define htobe64(x) __uint64_identity(x)
|
#define htobe64(x) __uint64_identity(x)
|
||||||
#else
|
#else
|
||||||
#error machine architecture unsupported! Expected either big-endian or little-endian, make sure to use a compiler which defines __BYTE_ORDER__ (like clang or gcc)
|
#error machine architecture unsupported! Expected either big-endian or little-endian, make sure to use a compiler which defines __BYTE_ORDER__ (like clang or gcc)
|
||||||
#endif
|
#endif /* byte order */
|
||||||
#endif
|
|
||||||
|
#endif /* has byteswap.h */
|
||||||
|
|
||||||
|
#else
|
||||||
|
#error GNU C is unavailable
|
||||||
|
#endif /* __GNUC__ */
|
||||||
|
|
||||||
|
#endif /* PORTABLE_ENDIAN_H */
|
||||||
10
makefile
10
makefile
@@ -38,20 +38,20 @@ endif
|
|||||||
ifeq ($(DEBUG),1)
|
ifeq ($(DEBUG),1)
|
||||||
PROF = dbg
|
PROF = dbg
|
||||||
CFLAGS += -UNDEBUG -Og -g -Wextra -Wpedantic
|
CFLAGS += -UNDEBUG -Og -g -Wextra -Wpedantic
|
||||||
CFLAGS += $(if $(filter 1,$(ISWIN)),,-fsanitize=address,undefined)
|
CFLAGS += $(if $(filter 1,$(ISWIN)),,-fsanitize=address,undefined) -ftrapv
|
||||||
LDFLAGS += $(if $(filter 1,$(ISWIN)),,-fsanitize=address,undefined)
|
LDFLAGS += $(if $(filter 1,$(ISWIN)),,-fsanitize=address,undefined) -ftrapv
|
||||||
# |--profile: testing
|
# |--profile: testing
|
||||||
else ifeq ($(DEBUG),test)
|
else ifeq ($(DEBUG),test)
|
||||||
PROF = test
|
PROF = test
|
||||||
CFLAGS += -UNDEBUG -O2 -g
|
CFLAGS += -UNDEBUG -O2 -g
|
||||||
CFLAGS += $(if $(filter 1,$(ISWIN)),,-fsanitize=address)
|
CFLAGS += $(if $(filter 1,$(ISWIN)),,-fsanitize=address) -ftrapv
|
||||||
LDFLAGS += $(if $(filter 1,$(ISWIN)),,-fsanitize=address)
|
LDFLAGS += $(if $(filter 1,$(ISWIN)),,-fsanitize=address) -ftrapv
|
||||||
else
|
else
|
||||||
PROF = rel
|
PROF = rel
|
||||||
CFLAGS += -DNDEBUG -O2
|
CFLAGS += -DNDEBUG -O2
|
||||||
endif
|
endif
|
||||||
|
|
||||||
CFLAGS += $(shell pkg-config --cflags glfw3 libarchive) -Ilib/glad/include
|
CFLAGS += $(shell pkg-config --cflags glfw3 libarchive) -Ilib/include -Ilib/glad/include
|
||||||
LDFLAGS += $(shell pkg-config --libs glfw3 libarchive) -lm
|
LDFLAGS += $(shell pkg-config --libs glfw3 libarchive) -lm
|
||||||
|
|
||||||
# get source files
|
# get source files
|
||||||
|
|||||||
@@ -2,23 +2,57 @@
|
|||||||
* Licensed under the MIT Licence. See LICENSE for details */
|
* Licensed under the MIT Licence. See LICENSE for details */
|
||||||
#include "mcx.h"
|
#include "mcx.h"
|
||||||
|
|
||||||
|
#include <archive.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <endian.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "../util/compat/endian.h"
|
#include "../error.h"
|
||||||
#include "../util/intdef.h"
|
#include "../util/intdef.h"
|
||||||
|
|
||||||
#define TABLE 0x2000 // table byte size
|
|
||||||
#define SECTOR 0x1000 // sector size
|
#define SECTOR 0x1000 // sector size
|
||||||
|
#define TABLE 0x800 // table (total) element count
|
||||||
#define CHUNKS 0x400 // amount of chunks in a file
|
#define CHUNKS 0x400 // amount of chunks in a file
|
||||||
|
|
||||||
|
enum mcx_compression {
|
||||||
|
MCX_COMPRESSION_GZIP = 0x01,
|
||||||
|
MCX_COMPRESSION_ZLIB = 0x02,
|
||||||
|
MCX_COMPRESSION_NONE = 0x03,
|
||||||
|
MCX_COMPRESSION_LZ4 = 0x04,
|
||||||
|
MCX_COMPRESSION_CUSTOM = 0x7F,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* first 4 bytes is an i32 indicating remaining bytes, the following byte defines the compression scheme */
|
||||||
|
static void mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int idx) {
|
||||||
|
const u8 *chunk = buf + (be32toh(table[idx]) >> 8) * SECTOR;
|
||||||
|
|
||||||
|
i32 len;
|
||||||
|
memcpy(&len, chunk, 4);
|
||||||
|
len = be32toh(len);
|
||||||
|
chunk += 4;
|
||||||
|
|
||||||
|
if (*chunk != MCX_COMPRESSION_CUSTOM) {
|
||||||
|
struct archive *archive = archive_read_new();
|
||||||
|
archive_read_support_format_raw(archive);
|
||||||
|
switch (*chunk) {
|
||||||
|
case MCX_COMPRESSION_GZIP: archive_read_support_filter_gzip(archive); break;
|
||||||
|
case MCX_COMPRESSION_ZLIB: archive_read_support_filter_zlib(archive); break; // BUG: this does not exist, but will have to do for now
|
||||||
|
case MCX_COMPRESSION_LZ4: archive_read_support_filter_lz4(archive); break;
|
||||||
|
case MCX_COMPRESSION_CUSTOM: // TODO: implement using filtering any format
|
||||||
|
default: fatal("compression type of '%ihh' is unsupported!", *chunk);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: implement decompression, somehow.
|
||||||
|
// https://github.com/libarchive/libarchive/wiki/Examples#user-content-A_Universal_Decompressor
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Moves chunks `src_s` to `src_e` (inclusive) from `src`, back onto `dst`. */
|
/* Moves chunks `src_s` to `src_e` (inclusive) from `src`, back onto `dst`. */
|
||||||
static void mvchunks(u8 *restrict buf, u8 *src, u8 *dst, int src_s, int src_e) {
|
static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e) {
|
||||||
assert(src > dst);
|
assert(src > dst);
|
||||||
u32 *table = (u32 *)buf;
|
|
||||||
size_t len = src - dst; // acquire the amount of bytes that we shall move
|
size_t len = src - dst; // acquire the amount of bytes that we shall move
|
||||||
assert(!(len % SECTOR));
|
assert(!(len % SECTOR));
|
||||||
|
|
||||||
@@ -34,9 +68,8 @@ static void mvchunks(u8 *restrict buf, u8 *src, u8 *dst, int src_s, int src_e) {
|
|||||||
/* Deletes chunk `sidx` by moving chunks up to `eidx` back over `sidx` in `buf`.
|
/* Deletes chunk `sidx` by moving chunks up to `eidx` back over `sidx` in `buf`.
|
||||||
* `rmb` is an optional additional offset that can be applied, and signifies bytes already removed.
|
* `rmb` is an optional additional offset that can be applied, and signifies bytes already removed.
|
||||||
* Returns the bytes removed by this function. */
|
* Returns the bytes removed by this function. */
|
||||||
static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) {
|
static size_t delchunk(u8 *restrict buf, u32 *restrict table, size_t rmb, int sidx, int eidx) {
|
||||||
// load the table data
|
// load the table data
|
||||||
u32 *table = (u32 *)buf;
|
|
||||||
size_t slen, bidx, blen;
|
size_t slen, bidx, blen;
|
||||||
slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk
|
slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk
|
||||||
bidx = (be32toh(table[sidx]) >> 8) * SECTOR; // acquire and compute the byte offset the chunk starts at
|
bidx = (be32toh(table[sidx]) >> 8) * SECTOR; // acquire and compute the byte offset the chunk starts at
|
||||||
@@ -49,20 +82,25 @@ static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) {
|
|||||||
// move the succeeding chunks over the deleted chunk
|
// move the succeeding chunks over the deleted chunk
|
||||||
u8 *dst = buf + bidx - rmb;
|
u8 *dst = buf + bidx - rmb;
|
||||||
u8 *src = buf + bidx + blen;
|
u8 *src = buf + bidx + blen;
|
||||||
mvchunks(buf, src, dst, sidx, eidx - 1);
|
mvchunks(dst, src, table, sidx, eidx - 1);
|
||||||
return blen;
|
return blen;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Just call `delchunk` with the parameters and some defaults.
|
/* Call `delchunk` with the parameters and some defaults. Ensuring the table is copied correctly as well.
|
||||||
* This is done instead of `delchunk` being globally linked, because
|
* This is done instead of `delchunk` being globally linked, because
|
||||||
* `delchunk` requests more specific parameters, which is confusing outside this module. */
|
* `delchunk` requests more specific parameters, which is confusing outside this module. */
|
||||||
size_t mcx_delchunk(u8 *restrict buf, int chunk) {
|
size_t mcx_delchunk(u8 *restrict buf, int chunk) {
|
||||||
return delchunk(buf, 0, chunk, CHUNKS);
|
u32 table[TABLE];
|
||||||
|
memcpy(table, buf, sizeof(table));
|
||||||
|
size_t res = delchunk(buf, table, 0, chunk, CHUNKS);
|
||||||
|
memcpy(buf, table, sizeof(table));
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) {
|
size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) {
|
||||||
assert(start < end && end < CHUNKS);
|
assert(start < end && end < CHUNKS);
|
||||||
u32 *table = (u32 *)buf;
|
u32 table[TABLE];
|
||||||
|
memcpy(table, buf, sizeof(table));
|
||||||
u8 *dst = buf + (be32toh(table[start]) >> 8) * SECTOR;
|
u8 *dst = buf + (be32toh(table[start]) >> 8) * SECTOR;
|
||||||
u8 *src = buf + (be32toh(table[end]) >> 8) * SECTOR;
|
u8 *src = buf + (be32toh(table[end]) >> 8) * SECTOR;
|
||||||
src += (be32toh(table[end]) & 0xFF) * SECTOR;
|
src += (be32toh(table[end]) & 0xFF) * SECTOR;
|
||||||
@@ -76,7 +114,8 @@ size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) {
|
|||||||
|
|
||||||
// move the remaining chunks down
|
// move the remaining chunks down
|
||||||
if (end < (CHUNKS - 1))
|
if (end < (CHUNKS - 1))
|
||||||
mvchunks(buf, src, dst, end, (CHUNKS - 1));
|
mvchunks(dst, src, table, end, (CHUNKS - 1));
|
||||||
|
memcpy(buf, table, sizeof(table));
|
||||||
return src - dst;
|
return src - dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,9 +135,14 @@ size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunk
|
|||||||
qsort(chunkids, chunkc, sizeof(int), cmp_chunkids);
|
qsort(chunkids, chunkc, sizeof(int), cmp_chunkids);
|
||||||
chunkids[chunkc] = CHUNKS; // set the spare chunk to the max chunks, so the rest of the chunks are moved
|
chunkids[chunkc] = CHUNKS; // set the spare chunk to the max chunks, so the rest of the chunks are moved
|
||||||
|
|
||||||
|
u32 table[TABLE];
|
||||||
|
memcpy(table, buf, sizeof(table));
|
||||||
|
|
||||||
size_t rmb = 0;
|
size_t rmb = 0;
|
||||||
for (int i = 0; i < chunkc; i++)
|
for (int i = 0; i < chunkc; i++)
|
||||||
rmb += delchunk(buf, rmb, chunkids[i], chunkids[i + 1]);
|
rmb += delchunk(buf, table, rmb, chunkids[i], chunkids[i + 1]);
|
||||||
|
|
||||||
|
memcpy(buf, table, sizeof(table));
|
||||||
return rmb;
|
return rmb;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,5 +152,5 @@ size_t mcx_calcsize(const u8 *restrict buf) {
|
|||||||
size_t size = 0;
|
size_t size = 0;
|
||||||
for (uint i = 0; i < CHUNKS; i++)
|
for (uint i = 0; i < CHUNKS; i++)
|
||||||
size += *(buf + (i * 4) + 3);
|
size += *(buf + (i * 4) + 3);
|
||||||
return (size * CHUNKS) + TABLE;
|
return (size * SECTOR) + (TABLE * 4);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,37 +3,61 @@
|
|||||||
#include "nbt.h"
|
#include "nbt.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <endian.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "../util/compat/endian.h"
|
|
||||||
#include "../util/intdef.h"
|
#include "../util/intdef.h"
|
||||||
|
|
||||||
#define MAX_DEPTH 512
|
#define MAX_DEPTH 512
|
||||||
|
|
||||||
|
/* Extracts a big endian 16 bit integer from address `buf`, converts it to host byte size if needed and returns. */
|
||||||
|
static inline u16 buftoh16(const void *restrict buf) {
|
||||||
|
u16 i;
|
||||||
|
memcpy(&i, buf, sizeof(i));
|
||||||
|
return be16toh(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Extracts a big endian 32 bit integer from address `buf`, converts it to host byte size if needed and returns. */
|
||||||
|
static inline u32 buftoh32(const void *restrict buf) {
|
||||||
|
u32 i;
|
||||||
|
memcpy(&i, buf, sizeof(i));
|
||||||
|
return be32toh(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Extracts a big endian 64 bit integer from address `buf`, converts it to host byte size if needed and returns. */
|
||||||
|
static inline u64 buftoh64(const void *restrict buf) {
|
||||||
|
u64 i;
|
||||||
|
memcpy(&i, buf, sizeof(i));
|
||||||
|
return be64toh(i);
|
||||||
|
}
|
||||||
|
|
||||||
/* Processes the incoming array data in `buf`. Which contains `nmem` items of `size`.
|
/* Processes the incoming array data in `buf`. Which contains `nmem` items of `size`.
|
||||||
* The data shall be converted to little-endian on little-endian systems
|
* The data shall be converted to little-endian on little-endian systems
|
||||||
* Outputs the allocated data to `out`, returns where the next pointer would be. */
|
* Outputs the allocated data to `out`, returns where the next pointer would be. */
|
||||||
static const u8 *procarr(const u8 *restrict buf, i32 nmem, uint size, struct nbt_array *restrict *restrict out) {
|
static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nbt_array *restrict out) {
|
||||||
size_t len = nmem * size;
|
size_t len = nmemb * size;
|
||||||
*out = malloc(sizeof(struct nbt_array) + len);
|
*out = (struct nbt_array){
|
||||||
if (!*out) return buf + len;
|
out->nmemb = nmemb,
|
||||||
|
out->dat = malloc(len),
|
||||||
|
};
|
||||||
|
if (!out->dat)
|
||||||
|
return buf + len;
|
||||||
|
|
||||||
memcpy((*out)->dat, buf, len);
|
memcpy(out->dat, buf, len);
|
||||||
(*out)->len = nmem;
|
|
||||||
buf += len;
|
buf += len;
|
||||||
|
|
||||||
/* Only include this code for little-endian systems. Since only they require this logic.
|
/* Only include this code for little-endian systems. Since only they require this logic.
|
||||||
* Producing optimised code for other platforms. */
|
* Producing optimised code for other platforms. */
|
||||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||||
if (size == 1) return buf;
|
if (size == 1) return buf;
|
||||||
size_t i = 0;
|
i32 i = 0;
|
||||||
while (i < len) {
|
while (i < nmemb) {
|
||||||
switch (size) {
|
switch (size) {
|
||||||
case 2: *(u16 *)((*out)->dat + i) = be16toh(*(u16 *)((*out)->dat + i)); break;
|
case 2: ((u16 *)out->dat)[i] = be16toh(((u16 *)out->dat)[i]); break;
|
||||||
case 4: *(u32 *)((*out)->dat + i) = be32toh(*(u32 *)((*out)->dat + i)); break;
|
case 4: ((u32 *)out->dat)[i] = be16toh(((u32 *)out->dat)[i]); break;
|
||||||
case 8: *(u64 *)((*out)->dat + i) = be64toh(*(u64 *)((*out)->dat + i)); break;
|
case 8: ((u64 *)out->dat)[i] = be16toh(((u64 *)out->dat)[i]); break;
|
||||||
default: __builtin_unreachable(); // this should be impossible
|
default: __builtin_unreachable(); // this should be impossible
|
||||||
}
|
}
|
||||||
i += size;
|
i += size;
|
||||||
@@ -43,12 +67,10 @@ static const u8 *procarr(const u8 *restrict buf, i32 nmem, uint size, struct nbt
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* calls `procarr` for the simple types available. */
|
/* calls `procarr` for the simple types available. */
|
||||||
static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict *restrict out) {
|
static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict out) {
|
||||||
uint size;
|
uint size;
|
||||||
|
|
||||||
*out = NULL;
|
switch (*(u8 *)buf) {
|
||||||
|
|
||||||
switch (*buf) {
|
|
||||||
case NBT_I8: size = 1; break;
|
case NBT_I8: size = 1; break;
|
||||||
case NBT_I16: size = 2; break;
|
case NBT_I16: size = 2; break;
|
||||||
case NBT_I32: // fall through
|
case NBT_I32: // fall through
|
||||||
@@ -59,7 +81,9 @@ static const u8 *proclist(const u8 *restrict buf, struct nbt_array *restrict *re
|
|||||||
}
|
}
|
||||||
|
|
||||||
buf++;
|
buf++;
|
||||||
i32 len = (i32)be32toh(*(u32 *)buf);
|
i32 len;
|
||||||
|
memcpy(&len, buf, 4);
|
||||||
|
len = be32toh(len);
|
||||||
buf += 4;
|
buf += 4;
|
||||||
return procarr(buf, len, size, out);
|
return procarr(buf, len, size, out);
|
||||||
}
|
}
|
||||||
@@ -73,25 +97,25 @@ const u8 *nbt_proctag(const u8 *restrict buf, u16 slen, void *restrict out) {
|
|||||||
|
|
||||||
switch (*buf) {
|
switch (*buf) {
|
||||||
case NBT_I8: *(u8 *)out = *ptr; return ptr + 1;
|
case NBT_I8: *(u8 *)out = *ptr; return ptr + 1;
|
||||||
case NBT_I16: *(u16 *)out = be16toh(*(u16 *)ptr); return ptr + 2;
|
case NBT_I16: *(u16 *)out = buftoh16(ptr); return ptr + 2;
|
||||||
case NBT_I32: // fall through
|
case NBT_I32: // fall through
|
||||||
case NBT_F32: *(u32 *)out = be16toh(*(u32 *)ptr); return ptr + 4;
|
case NBT_F32: *(u32 *)out = buftoh32(ptr); return ptr + 4;
|
||||||
case NBT_I64: // fall through
|
case NBT_I64: // fall through
|
||||||
case NBT_F64: *(u64 *)out = be16toh(*(u64 *)ptr); return ptr + 8;
|
case NBT_F64: *(u64 *)out = buftoh64(ptr); return ptr + 8;
|
||||||
|
|
||||||
case NBT_STR: nmem = be16toh(*(u16 *)ptr), size = 1, ptr += 2; break;
|
case NBT_STR: nmem = buftoh16(ptr), size = 1, ptr += 2; break;
|
||||||
case NBT_ARR_I8: nmem = be32toh(*(u32 *)ptr), size = 1, ptr += 4; break;
|
case NBT_ARR_I8: nmem = buftoh32(ptr), size = 1, ptr += 4; break;
|
||||||
case NBT_ARR_I32: nmem = be32toh(*(u32 *)ptr), size = 4, ptr += 4; break;
|
case NBT_ARR_I32: nmem = buftoh32(ptr), size = 4, ptr += 4; break;
|
||||||
case NBT_ARR_I64: nmem = be32toh(*(u32 *)ptr), size = 8, ptr += 4; break;
|
case NBT_ARR_I64: nmem = buftoh32(ptr), size = 8, ptr += 4; break;
|
||||||
|
|
||||||
case NBT_LIST:
|
case NBT_LIST:
|
||||||
return proclist(ptr, (struct nbt_array **)out);
|
return proclist(ptr, (struct nbt_array *)out);
|
||||||
return tmp;
|
return tmp;
|
||||||
|
|
||||||
default: return NULL;
|
default: return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return procarr(ptr, nmem, size, (struct nbt_array **)out);
|
return procarr(ptr, nmem, size, (struct nbt_array *)out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -104,17 +128,17 @@ static const u8 *nexttag_list(const u8 *restrict ptr, uint *restrict const dpt,
|
|||||||
ptr++;
|
ptr++;
|
||||||
switch (*tag) {
|
switch (*tag) {
|
||||||
case NBT_END: break;
|
case NBT_END: break;
|
||||||
case NBT_I8: ptr += (i32)be32toh(*(u32 *)ptr) * 1; break;
|
case NBT_I8: ptr += (i32)buftoh32(ptr) * 1; break;
|
||||||
case NBT_I16: ptr += (i32)be32toh(*(u32 *)ptr) * 2; break;
|
case NBT_I16: ptr += (i32)buftoh32(ptr) * 2; break;
|
||||||
case NBT_I32: // fall through
|
case NBT_I32: // fall through
|
||||||
case NBT_F32: ptr += (i32)be32toh(*(u32 *)ptr) * 4; break;
|
case NBT_F32: ptr += (i32)buftoh32(ptr) * 4; break;
|
||||||
case NBT_I64: // fall through
|
case NBT_I64: // fall through
|
||||||
case NBT_F64: ptr += (i32)be32toh(*(u32 *)ptr) * 8; break;
|
case NBT_F64: ptr += (i32)buftoh32(ptr) * 8; break;
|
||||||
default:
|
default:
|
||||||
// TODO: handle out of bounds... Might not be required if we use flexible array member
|
// TODO: handle out of bounds... Might not be required if we use flexible array member
|
||||||
(*dpt)++;
|
(*dpt)++;
|
||||||
tags[*dpt] = *tag;
|
tags[*dpt] = *tag;
|
||||||
lens[*dpt] = (i32)be32toh(*(u32 *)ptr);
|
lens[*dpt] = (i32)buftoh32(ptr);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
ptr += 4;
|
ptr += 4;
|
||||||
@@ -136,7 +160,7 @@ static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *
|
|||||||
*dpt -= !lens[*dpt];
|
*dpt -= !lens[*dpt];
|
||||||
} else {
|
} else {
|
||||||
type = *tag;
|
type = *tag;
|
||||||
ptr += be16toh(*(u16 *)(tag + 1)) + 3;
|
ptr += buftoh16(tag + 1) + 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@@ -147,10 +171,10 @@ static const u8 *nexttag(const u8 *restrict tag, uint *restrict const dpt, i32 *
|
|||||||
case NBT_I64: // fall through
|
case NBT_I64: // fall through
|
||||||
case NBT_F64: ptr += 8; break;
|
case NBT_F64: ptr += 8; break;
|
||||||
|
|
||||||
case NBT_ARR_I8: ptr += 4 + (i32)be32toh(*(u32 *)ptr) * 1; break;
|
case NBT_ARR_I8: ptr += 4 + (i32)buftoh32(ptr) * 1; break;
|
||||||
case NBT_ARR_I32: ptr += 4 + (i32)be32toh(*(u32 *)ptr) * 4; break;
|
case NBT_ARR_I32: ptr += 4 + (i32)buftoh32(ptr) * 4; break;
|
||||||
case NBT_ARR_I64: ptr += 4 + (i32)be32toh(*(u32 *)ptr) * 8; break;
|
case NBT_ARR_I64: ptr += 4 + (i32)buftoh32(ptr) * 8; break;
|
||||||
case NBT_STR: ptr += 2 + (u16)be16toh(*(u16 *)ptr) * 1; break;
|
case NBT_STR: ptr += 2 + (u16)buftoh16(ptr) * 1; break;
|
||||||
|
|
||||||
case NBT_END: (*dpt)--; break;
|
case NBT_END: (*dpt)--; break;
|
||||||
case NBT_COMPOUND: (*dpt)++; break;
|
case NBT_COMPOUND: (*dpt)++; break;
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <endian.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "../util/atrb.h"
|
#include "../util/atrb.h"
|
||||||
#include "../util/compat/endian.h"
|
|
||||||
#include "../util/intdef.h"
|
#include "../util/intdef.h"
|
||||||
|
|
||||||
/* NBT (named binary tag) is a tree data structure. Tags have a numeric type ID, name and a payload.
|
/* NBT (named binary tag) is a tree data structure. Tags have a numeric type ID, name and a payload.
|
||||||
@@ -37,8 +37,8 @@ enum nbt_tagid {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct nbt_array {
|
struct nbt_array {
|
||||||
i32 len;
|
i32 nmemb;
|
||||||
u8 dat[];
|
void *dat;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
51
src/error.c
Normal file
51
src/error.c
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#include "error.h"
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "util/intdef.h"
|
||||||
|
|
||||||
|
static void error_log(FILE *restrict stream, const char *restrict pfx, uint ln, const char *restrict file, const char *restrict fmt, va_list ap) {
|
||||||
|
fprintf(stream, "(%s:%iu) [%s] '", file, ln, pfx);
|
||||||
|
|
||||||
|
vfprintf(stream, fmt, ap);
|
||||||
|
|
||||||
|
fprintf(stream, "'\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void error_dbg(uint ln, const char *restrict file, const char *restrict fmt, ...) {
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
error_log(stdout, "\033[95mDBG\033[0m", ln, file, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void error_inf(uint ln, const char *restrict file, const char *restrict fmt, ...) {
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
error_log(stdout, "\033[93mINF\033[0m", ln, file, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void error_war(uint ln, const char *restrict file, const char *restrict fmt, ...) {
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
error_log(stdout, "\033[91mWAR\033[0m", ln, file, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void error_err(uint ln, const char *restrict file, const char *restrict fmt, ...) {
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
error_log(stdout, "\033[mFAT\033[0m", ln, file, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void error_fat(uint ln, const char *restrict file, const char *restrict fmt, ...) {
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
error_log(stdout, "\033[mFAT\033[0m", ln, file, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
23
src/error.h
23
src/error.h
@@ -2,20 +2,21 @@
|
|||||||
* Licensed under the MIT Licence. See LICENSE for details */
|
* Licensed under the MIT Licence. See LICENSE for details */
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#if __INCLUDE_LEVEL__ > 0
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "util/atrb.h"
|
||||||
|
#include "util/intdef.h"
|
||||||
#include "util/macro.h"
|
#include "util/macro.h"
|
||||||
#endif
|
|
||||||
|
|
||||||
#define debug(s, ...) printf("\033[95m" __FILE__ ":" MACRO_STR2(__LINE__) ": [DBG]: " s "\033[0m\n", ##__VA_ARGS__)
|
void error_dbg(uint ln, const char *restrict file, const char *restrict fmt, ...);
|
||||||
#define info(s, ...) printf(__FILE__ ":" MACRO_STR2(__LINE__) ": [INF]: " s "\n", ##__VA_ARGS__)
|
void error_inf(uint ln, const char *restrict file, const char *restrict fmt, ...);
|
||||||
#define warn(s, ...) fprintf(stderr, "\033[93m" __FILE__ ":" MACRO_STR2(__LINE__) ": [WAR]: " s "\033[0m\n", ##__VA_ARGS__)
|
void error_war(uint ln, const char *restrict file, const char *restrict fmt, ...);
|
||||||
#define error(s, ...) fprintf(stderr, "\033[91m" __FILE__ ":" MACRO_STR2(__LINE__) ": [ERR]: " s "\033[0m\n", ##__VA_ARGS__)
|
void error_err(uint ln, const char *restrict file, const char *restrict fmt, ...);
|
||||||
|
void error_fat(uint ln, const char *restrict file, const char *restrict fmt, ...) NORET;
|
||||||
|
|
||||||
#define fatal(s, ...) \
|
#define debug(s, ...) error_dbg(__LINE__, __FILE__, s, ##__VA_ARGS__)
|
||||||
do { \
|
#define info(s, ...) error_inf(__LINE__, __FILE__, s, ##__VA_ARGS__)
|
||||||
fprintf(stderr, "\033[101m" __FILE__ ":" MACRO_STR2(__LINE__) ": [FAT]: " s "\033[0m\n", ##__VA_ARGS__); \
|
#define warn(s, ...) error_war(__LINE__, __FILE__, s, ##__VA_ARGS__)
|
||||||
exit(EXIT_FAILURE); \
|
#define error(s, ...) error_err(__LINE__, __FILE__, s, ##__VA_ARGS__)
|
||||||
} while (0)
|
#define fatal(s, ...) error_fat(__LINE__, __FILE__, s, ##__VA_ARGS__)
|
||||||
|
|||||||
@@ -31,13 +31,15 @@ static inline int init(void) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void quit(void) {
|
static void quit(void) {
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
(void)argc, (void)argv;
|
(void)argc, (void)argv;
|
||||||
printf("debug: [DBG], info: [INF], warning: [WAR], error: [ERR], fatal: [FAT]\n");
|
printf("debug: [DBG], info: [INF], warning: [WAR], error: [ERR], fatal: [FAT]\n");
|
||||||
|
atexit(quit);
|
||||||
if (init()) fatal("failed to initialize!");
|
if (init()) fatal("failed to initialize!");
|
||||||
|
|
||||||
window_loop();
|
window_loop();
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
/* Copyright (c) 2025 Quinn
|
|
||||||
* Licensed under the MIT Licence. See LICENSE for details */
|
|
||||||
#pragma once
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
#include "vec.h"
|
|
||||||
|
|
||||||
#define COLOUR32_BLACK ((u8vec4){0x00, 0x00, 0x00, 0xFF})
|
|
||||||
#define COLOUR32_RED ((u8vec4){0xFF, 0x00, 0x00, 0xFF})
|
|
||||||
#define COLOUR32_YELLOW ((u8vec4){0xFF, 0xFF, 0x00, 0xFF})
|
|
||||||
#define COLOUR32_ORANGE ((u8vec4){0xFF, 0x6D, 0x00, 0xFF})
|
|
||||||
#define COLOUR32_GREEN ((u8vec4){0x00, 0xFF, 0x00, 0xFF})
|
|
||||||
#define COLOUR32_CYAN ((u8vec4){0x00, 0xFF, 0xFF, 0xFF})
|
|
||||||
#define COLOUR32_BLUE ((u8vec4){0x00, 0x00, 0xFF, 0xFF})
|
|
||||||
#define COLOUR32_MAGENTA ((u8vec4){0xFF, 0x00, 0xFF, 0xFF})
|
|
||||||
#define COLOUR32_WHITE ((u8vec4){0xFF, 0xFF, 0xFF, 0xFF})
|
|
||||||
|
|
||||||
// american macros:
|
|
||||||
#define COLOR32_BLACK COLOUR32_BLACK
|
|
||||||
#define COLOR32_RED COLOUR32_RED
|
|
||||||
#define COLOR32_YELLOW COLOUR32_YELLOW
|
|
||||||
#define COLOR32_ORANGE COLOUR32_ORANGE
|
|
||||||
#define COLOR32_GREEN COLOUR32_GREEN
|
|
||||||
#define COLOR32_CYAN COLOUR32_CYAN
|
|
||||||
#define COLOR32_BLUE COLOUR32_BLUE
|
|
||||||
#define COLOR32_MAGENTA COLOUR32_MAGENTA
|
|
||||||
#define COLOR32_WHITE COLOUR32_WHITE
|
|
||||||
Reference in New Issue
Block a user