mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-18 20:35:45 +01:00
Compare commits
7 Commits
e9816febfa
...
78db656a92
| Author | SHA1 | Date | |
|---|---|---|---|
| 78db656a92 | |||
| 767f3a5c13 | |||
| 43e3e4fe85 | |||
| fad6f366b4 | |||
| b7859d56d9 | |||
| 7a45724de7 | |||
| 1c863099a9 |
@@ -19,7 +19,12 @@ I have picked C as the language for the core portions of the application, where
|
|||||||
This version is not intended to serve to entirely replace MCA selector, just to offer an alternative. Both tools will have their strengths and weaknesses.
|
This version is not intended to serve to entirely replace MCA selector, just to offer an alternative. Both tools will have their strengths and weaknesses.
|
||||||
|
|
||||||
# planned features
|
# planned features
|
||||||
- [ ] (very basic) world map viewing
|
- [ ] (very basic) world map viewing (Y level + surface)
|
||||||
- [ ] filtering chunks based on time spent in them
|
- [ ] filtering chunks based on time spent in them
|
||||||
- [ ] chunk-level removal of data.
|
- [ ] chunk-level removal of data.
|
||||||
- [ ] CLI-only version
|
- [ ] CLI-only version
|
||||||
|
- [ ] (idea) change chunk compression
|
||||||
|
- [ ] (idea) view chunk metadata
|
||||||
|
- [ ] (idea) change chunk sNBT (this'll be tricky to add)
|
||||||
|
*note that entries marked with `(idea)` aren't guaranteed to be implemented.
|
||||||
|
Due to their viability and usefulness is still to be measured.*
|
||||||
|
|||||||
@@ -3,10 +3,69 @@
|
|||||||
#include <endian.h>
|
#include <endian.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "../util/compat/endian.h"
|
#include "../util/compat/endian.h"
|
||||||
#include "../util/intdef.h"
|
#include "../util/intdef.h"
|
||||||
|
|
||||||
|
/* 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.
|
||||||
|
* Returns the bytes removed by this function. */
|
||||||
|
static size_t delchunk(u8 *restrict buf, size_t rmb, int sidx, int eidx) {
|
||||||
|
// load the table data, and clear it
|
||||||
|
u32 *table = (u32 *)buf;
|
||||||
|
size_t slen, bidx, blen;
|
||||||
|
slen = be32toh(table[sidx] & 0xFF); // acquire the sector length of the chunk
|
||||||
|
bidx = be32toh(table[sidx] >> 8) * 0x1000; // acquire and compute the byte offset the chunk starts at
|
||||||
|
blen = slen * 0x1000; // compute the byte length of the chunk
|
||||||
|
table[sidx] = 0;
|
||||||
|
table[sidx + 0x400] = time(NULL); // assign the current time to the timestamp, for correctness NOTE: might need to zero-out instead
|
||||||
|
|
||||||
|
u8 *dst = buf + bidx - rmb;
|
||||||
|
u8 *src = buf + bidx + blen;
|
||||||
|
rmb = blen;
|
||||||
|
blen = 0;
|
||||||
|
for (sidx++; sidx < eidx; sidx++) {
|
||||||
|
blen += be32toh(table[sidx] & 0xFF) * 0x1000;
|
||||||
|
table[sidx] -= htobe32(slen << 8);
|
||||||
|
}
|
||||||
|
memmove(dst, src, blen);
|
||||||
|
return rmb;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t mcx_delchunk(u8 *restrict buf, int chunk) {
|
||||||
|
return delchunk(buf, 0, chunk, 0x400);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
u16 y2 = *(u16 *)y;
|
||||||
|
return (x2 > y2) - (x2 < y2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mcx_delchunks(u8 *restrict buf, const u16 *restrict chunks, int chunkc) {
|
||||||
|
// ensure the chunks ids we're working on are sorted from least to greatest
|
||||||
|
u16 chunkids[chunkc + 1];
|
||||||
|
memcpy(chunkids, chunks, chunkc);
|
||||||
|
qsort(chunkids, chunkc, sizeof(int), cmp_chunkids);
|
||||||
|
chunkids[chunkc] = 0; // set the spare chunk to zero, to prevent out-of-bounds access
|
||||||
|
|
||||||
|
size_t rmb = 0;
|
||||||
|
for (int i = 0; i < chunkc; i++)
|
||||||
|
rmb += delchunk(buf, rmb, chunkids[i], chunkids[i + 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Sum together the 4th byte in each location integer to compute the sector size of all chunks.
|
||||||
|
* Multiplying by `0x1000`, and adding the size of the table itself. */
|
||||||
|
size_t mcx_calcsize(const u8 *restrict buf) {
|
||||||
|
size_t size = 0;
|
||||||
|
for (uint i = 0; i < 0x400; i++)
|
||||||
|
size += *(buf + (i * 4) + 3);
|
||||||
|
return (size * 0x1000) + 0x2000;
|
||||||
|
}
|
||||||
|
|
||||||
/* an `*.mcX` contains a `0x2000` byte long table, the first `0x1000` containing
|
/* an `*.mcX` contains a `0x2000` byte long table, the first `0x1000` containing
|
||||||
* `0x400` entries of chunk data.
|
* `0x400` entries of chunk data.
|
||||||
* This chunk data is big-endian, where bytes `0xFFFFFF00` represent the `0x1000` sector offset.
|
* This chunk data is big-endian, where bytes `0xFFFFFF00` represent the `0x1000` sector offset.
|
||||||
|
|||||||
@@ -14,6 +14,16 @@ struct mcx_chunk {
|
|||||||
u32 time; // modification time in epoch seconds
|
u32 time; // modification time in epoch seconds
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Deletes chunk `idx` from `buf`, moving all chunks downwards in the process. */
|
||||||
|
size_t mcx_delchunk(u8 *restrict buf, int idx);
|
||||||
|
|
||||||
|
/* Deletes `chunkc` chunks specified in `chunks` from the `*.mcX` file.
|
||||||
|
* This is done in a way to perform minimal memmove operations. */
|
||||||
|
void mcx_delchunks(u8 *restrict buf, const u16 *restrict chunks, int chunkc);
|
||||||
|
|
||||||
|
/* Computes the byte size of the `*.mcX` file in `buf` and returns it. */
|
||||||
|
size_t mcx_calcsize(const u8 *restrict buf) NONNULL((1)) PURE;
|
||||||
|
|
||||||
/* indexes the chunks in an `*.mcX` file, writing `0x400` of entries to `chunks` */
|
/* indexes the chunks in an `*.mcX` file, writing `0x400` of entries to `chunks` */
|
||||||
void mcx_index(const u8 *restrict buf, struct mcx_chunk *restrict chunks) NONNULL((1, 2));
|
void mcx_index(const u8 *restrict buf, struct mcx_chunk *restrict chunks) NONNULL((1, 2));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user