mirror of
https://github.com/thepigeongenerator/mcaselector-lite.git
synced 2025-12-18 22:05:44 +01:00
Compare commits
1 Commits
78db656a92
...
e9816febfa
| Author | SHA1 | Date | |
|---|---|---|---|
| e9816febfa |
@@ -19,12 +19,7 @@ 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 (Y level + surface)
|
- [ ] (very basic) world map viewing
|
||||||
- [ ] 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.*
|
|
||||||
|
|||||||
@@ -9,61 +9,23 @@
|
|||||||
#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`.
|
void mcx_delchunk(u8 *restrict buf, int idx) {
|
||||||
* `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
|
// load the table data, and clear it
|
||||||
u32 *table = (u32 *)buf;
|
u32 *table = (u32 *)buf;
|
||||||
size_t slen, bidx, blen;
|
size_t chunk = (table[idx] >> 8) * 0x1000; // compute the byte offset the chunk starts in
|
||||||
slen = be32toh(table[sidx] & 0xFF); // acquire the sector length of the chunk
|
size_t blen = (table[idx] & 0xFF) * 0x1000; // compute the byte length of the chunk
|
||||||
bidx = be32toh(table[sidx] >> 8) * 0x1000; // acquire and compute the byte offset the chunk starts at
|
table[idx] = 0;
|
||||||
blen = slen * 0x1000; // compute the byte length of the chunk
|
table[idx + 0x400] = time(NULL); // assign the current time to the timestamp, for correctness NOTE: might need to zero-out instead
|
||||||
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;
|
// store the head and tail end of the current chunk
|
||||||
u8 *src = buf + bidx + blen;
|
u8 *head = buf + chunk;
|
||||||
rmb = blen;
|
u8 *tail = buf + chunk + blen;
|
||||||
|
|
||||||
|
// count the amount of bytes that we must move
|
||||||
blen = 0;
|
blen = 0;
|
||||||
for (sidx++; sidx < eidx; sidx++) {
|
for (idx++; idx < 0x400; idx++)
|
||||||
blen += be32toh(table[sidx] & 0xFF) * 0x1000;
|
blen += table[idx] & 0xFF * 0x1000;
|
||||||
table[sidx] -= htobe32(slen << 8);
|
memmove(head, tail, blen);
|
||||||
}
|
|
||||||
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
|
||||||
|
|||||||
@@ -14,15 +14,9 @@ struct mcx_chunk {
|
|||||||
u32 time; // modification time in epoch seconds
|
u32 time; // modification time in epoch seconds
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO: should return some form of feedback about its success
|
||||||
/* Deletes chunk `idx` from `buf`, moving all chunks downwards in the process. */
|
/* Deletes chunk `idx` from `buf`, moving all chunks downwards in the process. */
|
||||||
size_t mcx_delchunk(u8 *restrict buf, int idx);
|
void 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