Added memory checks
Some checks failed
Build Dusk / run-tests (push) Successful in 2m6s
Build Dusk / build-linux (push) Successful in 2m6s
Build Dusk / build-psp (push) Failing after 1m47s

This commit is contained in:
2026-01-06 11:02:26 -06:00
parent af5bf987c8
commit 0df7845f2c
19 changed files with 640 additions and 13 deletions

View File

@@ -7,6 +7,7 @@
#include "inventory.h"
#include "util/memory.h"
#include "util/sort.h"
#include "assert/assert.h"
void inventoryInit(
@@ -113,7 +114,11 @@ void inventoryRemove(inventory_t *inventory, const itemid_t item) {
if(stack->item != item) continue;
// Match found, shift everything else down
memoryMove(stack, stack + 1, end - (stack + 1));
memoryMove(
stack,
stack + 1,
(end - (stack + 1)) * sizeof(inventorystack_t)
);
// Clear last stack.
inventorystack_t *last = end - 1;
@@ -162,4 +167,31 @@ bool_t inventoryIsFull(const inventory_t *inventory) {
bool_t inventoryItemFull(const inventory_t *inventory, const itemid_t item) {
return inventoryGetCount(inventory, item) == ITEM_STACK_QUANTITY_MAX;
}
void inventorySortById(inventory_t *inventory) {
assertNotNull(inventory, "Inventory pointer is NULL.");
assertNotNull(inventory->storage, "Storage pointer is NULL.");
assertTrue(inventory->storageSize > 0, "Storage too small.");
// Get count of used stacks
size_t count = 0;
inventorystack_t *stack = inventory->storage;
inventorystack_t *end = stack + inventory->storageSize;
do {
if(stack->item == ITEM_ID_NULL) break;
count++;
} while(++stack < end);
if(count == 0) return; // Nothing to sort
int_t comparator(const void *a, const void *b) {
const inventorystack_t *stackA = (const inventorystack_t*)a;
const inventorystack_t *stackB = (const inventorystack_t*)b;
if(stackA->item < stackB->item) return -1;
if(stackA->item > stackB->item) return 1;
return 0;
}
sort((void*)inventory->storage, count, sizeof(inventorystack_t), comparator);
}