diff --git a/src/dat/mcx.c b/src/dat/mcx.c index 2e82743..9da057b 100644 --- a/src/dat/mcx.c +++ b/src/dat/mcx.c @@ -54,6 +54,25 @@ size_t mcx_delchunk(u8 *restrict buf, int chunk) { return delchunk(buf, 0, chunk, 0x400); } +size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) { + assert(start < end && end < 0x400); + u32 *table = (u32 *)buf; + u8 *dst = buf + (be32toh(table[start]) >> 8) * 0x1000; + u8 *src = buf + (be32toh(table[end]) >> 8) * 0x1000; + src += (be32toh(table[end]) & 0xFF) * 0x1000; + + // zeroes-out the chunk data within this range. (and set the timestamp) + u32 ts = htobe32(time(NULL)); + for (int i = start; i <= end; i++) { + table[i] = 0; + table[i + 0x400] = ts; + } + + // move the remaining chunks down + mvchunks(buf, src, dst, start, end); + return src - dst; +} + /* comparer function for to be inputted into `qsort` to compare two */ static int cmp_chunkids(const void *restrict x, const void *restrict y) { u16 x2 = *(u16 *)x; diff --git a/src/dat/mcx.h b/src/dat/mcx.h index 164d19a..ff3c2b4 100644 --- a/src/dat/mcx.h +++ b/src/dat/mcx.h @@ -17,6 +17,9 @@ struct mcx_chunk { /* Deletes chunk `idx` from `buf`, moving all chunks downwards in the process. */ size_t mcx_delchunk(u8 *restrict buf, int idx) NONNULL((1)); +/* Deletes a range of chunks from index `start` to `end`. */ +size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) NONNULL((1)); + /* Deletes `chunkc` chunks specified in `chunks` from the `*.mcX` file. * This is done in a way to perform minimal memmove operations. */ size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) NONNULL((1, 2));