Just trying to fix things now
This commit is contained in:
+30
-34
@@ -11,38 +11,6 @@
|
||||
#include "util/memory.h"
|
||||
#include <unistd.h>
|
||||
|
||||
/* ---- Per-entry event trampolines ----------------------------------------- */
|
||||
|
||||
static void assetBatchEntryOnLoadedCb(void *params, void *user) {
|
||||
assetentry_t *entry = (assetentry_t *)params;
|
||||
assetbatch_t *batch = (assetbatch_t *)user;
|
||||
|
||||
batch->loadedCount++;
|
||||
eventInvoke(&batch->onEntryLoaded, entry);
|
||||
|
||||
if((uint16_t)(batch->loadedCount + batch->errorCount) >= batch->count) {
|
||||
if(batch->errorCount == 0) {
|
||||
eventInvoke(&batch->onLoaded, batch);
|
||||
} else {
|
||||
eventInvoke(&batch->onError, batch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void assetBatchEntryOnErrorCb(void *params, void *user) {
|
||||
assetentry_t *entry = (assetentry_t *)params;
|
||||
assetbatch_t *batch = (assetbatch_t *)user;
|
||||
|
||||
batch->errorCount++;
|
||||
eventInvoke(&batch->onEntryError, entry);
|
||||
|
||||
if((uint16_t)(batch->loadedCount + batch->errorCount) >= batch->count) {
|
||||
eventInvoke(&batch->onError, batch);
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- Public API ---------------------------------------------------------- */
|
||||
|
||||
void assetBatchInit(
|
||||
assetbatch_t *batch,
|
||||
const uint16_t count,
|
||||
@@ -86,7 +54,7 @@ void assetBatchInit(
|
||||
);
|
||||
|
||||
if(batch->entries[i]->state == ASSET_ENTRY_STATE_LOADED) {
|
||||
/* Already loaded (cached) - count it now, no subscription needed. */
|
||||
// Already loaded (cached) - count it now, no subscription needed.
|
||||
batch->loadedCount++;
|
||||
} else if(batch->entries[i]->state == ASSET_ENTRY_STATE_ERROR) {
|
||||
batch->errorCount++;
|
||||
@@ -161,9 +129,37 @@ void assetBatchDispose(assetbatch_t *batch) {
|
||||
if(batch->entries[i]) {
|
||||
// Unsubscribe while we still hold a lock so the entry is live.
|
||||
eventUnsubscribe(&batch->entries[i]->onLoaded, assetBatchEntryOnLoadedCb);
|
||||
eventUnsubscribe(&batch->entries[i]->onError, assetBatchEntryOnErrorCb);
|
||||
eventUnsubscribe(&batch->entries[i]->onError, assetBatchEntryOnErrorCb);
|
||||
assetUnlockEntry(batch->entries[i]);
|
||||
}
|
||||
}
|
||||
memoryZero(batch, sizeof(assetbatch_t));
|
||||
}
|
||||
|
||||
void assetBatchEntryOnLoadedCb(void *params, void *user) {
|
||||
assetentry_t *entry = (assetentry_t *)params;
|
||||
assetbatch_t *batch = (assetbatch_t *)user;
|
||||
|
||||
batch->loadedCount++;
|
||||
eventInvoke(&batch->onEntryLoaded, entry);
|
||||
|
||||
if((uint16_t)(batch->loadedCount + batch->errorCount) >= batch->count) {
|
||||
if(batch->errorCount == 0) {
|
||||
eventInvoke(&batch->onLoaded, batch);
|
||||
} else {
|
||||
eventInvoke(&batch->onError, batch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void assetBatchEntryOnErrorCb(void *params, void *user) {
|
||||
assetentry_t *entry = (assetentry_t *)params;
|
||||
assetbatch_t *batch = (assetbatch_t *)user;
|
||||
|
||||
batch->errorCount++;
|
||||
eventInvoke(&batch->onEntryError, entry);
|
||||
|
||||
if((uint16_t)(batch->loadedCount + batch->errorCount) >= batch->count) {
|
||||
eventInvoke(&batch->onError, batch);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,3 +104,21 @@ errorret_t assetBatchRequireLoaded(assetbatch_t *batch);
|
||||
* @param batch Batch to dispose.
|
||||
*/
|
||||
void assetBatchDispose(assetbatch_t *batch);
|
||||
|
||||
/**
|
||||
* Event trampoline invoked when a batch entry finishes loading.
|
||||
* Increments the loaded counter and fires batch-level events.
|
||||
*
|
||||
* @param params The loaded assetentry_t pointer.
|
||||
* @param user The owning assetbatch_t pointer.
|
||||
*/
|
||||
void assetBatchEntryOnLoadedCb(void *params, void *user);
|
||||
|
||||
/**
|
||||
* Event trampoline invoked when a batch entry fails to load.
|
||||
* Increments the error counter and fires batch-level events.
|
||||
*
|
||||
* @param params The errored assetentry_t pointer.
|
||||
* @param user The owning assetbatch_t pointer.
|
||||
*/
|
||||
void assetBatchEntryOnErrorCb(void *params, void *user);
|
||||
|
||||
@@ -7,9 +7,13 @@ var Scene = {
|
||||
};
|
||||
|
||||
Scene.update = () => {
|
||||
if(!Scene.current || !Scene.current.update) return;
|
||||
Scene.current.update();
|
||||
}
|
||||
|
||||
Scene.dynamicUpdate = () => {
|
||||
if(!Scene.current || !Scene.current.dynamicUpdate) return;
|
||||
Scene.current.dynamicUpdate();
|
||||
}
|
||||
|
||||
Scene.set = (newScene) => {
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
*/
|
||||
|
||||
#include "moduleassetbatch.h"
|
||||
#include "util/string.h"
|
||||
|
||||
#define ASSET_BATCH_LOADED_MAX 8
|
||||
|
||||
@@ -273,6 +274,30 @@ moduleBaseFunction(moduleAssetBatchEntry) {
|
||||
return scriptProtoCreateValue(&MODULE_ASSET_ENTRY_PROTO, &e);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleAssetBatchGetAssetByPath) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jsassetbatch_t *b = moduleAssetBatchSelf(callInfo);
|
||||
if(!b || !b->batch) return jerry_undefined();
|
||||
if(!jerry_value_is_string(args[0])) return jerry_undefined();
|
||||
|
||||
char_t path[ASSET_FILE_NAME_MAX];
|
||||
jerry_size_t pathLen = jerry_string_to_buffer(
|
||||
args[0], JERRY_ENCODING_UTF8,
|
||||
(jerry_char_t *)path, ASSET_FILE_NAME_MAX - 1
|
||||
);
|
||||
path[pathLen] = '\0';
|
||||
|
||||
for(uint16_t i = 0; i < b->batch->count; i++) {
|
||||
assetentry_t *entry = b->batch->entries[i];
|
||||
if(!entry) continue;
|
||||
if(!stringEquals(entry->name, path)) continue;
|
||||
assetEntryLock(entry);
|
||||
jsassetentry_t e = { .entry = entry };
|
||||
return scriptProtoCreateValue(&MODULE_ASSET_ENTRY_PROTO, &e);
|
||||
}
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleAssetBatchGetOnLoaded) {
|
||||
jsassetbatch_t *b = moduleAssetBatchSelf(callInfo);
|
||||
if(!b || !b->batch) return jerry_undefined();
|
||||
@@ -349,6 +374,10 @@ void moduleAssetBatchInit(void) {
|
||||
scriptProtoDefineFunc(
|
||||
&MODULE_ASSET_BATCH_PROTO, "entry", moduleAssetBatchEntry
|
||||
);
|
||||
scriptProtoDefineFunc(
|
||||
&MODULE_ASSET_BATCH_PROTO, "getAssetByPath",
|
||||
moduleAssetBatchGetAssetByPath
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_ASSET_BATCH_PROTO, "onLoaded",
|
||||
moduleAssetBatchGetOnLoaded, NULL
|
||||
|
||||
@@ -89,6 +89,15 @@ moduleBaseFunction(moduleAssetBatchUnlock);
|
||||
*/
|
||||
moduleBaseFunction(moduleAssetBatchEntry);
|
||||
|
||||
/**
|
||||
* getAssetByPath(path) - finds the first entry whose name matches path
|
||||
* and returns it as a locked AssetEntry, or undefined if not found.
|
||||
* The returned entry must be unlocked separately when no longer needed.
|
||||
* @param args[0] Path string to match against entry names.
|
||||
* @return AssetEntry, or undefined if no entry matches.
|
||||
*/
|
||||
moduleBaseFunction(moduleAssetBatchGetAssetByPath);
|
||||
|
||||
/** @return The onLoaded Event (fires once when all entries load). */
|
||||
moduleBaseFunction(moduleAssetBatchGetOnLoaded);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user