Load duskscript files through the asset system, not the real filesystem
scriptLoadHandler used fopen/fread directly, but every .ds a script include()s is packed into dusk.dsk (compiled to .dsb) like every other asset now, not a loose file next to the binary - raw filesystem reads never found them outside a dev checkout. It now goes through assetFileInit/Open/Read/Close like the rest of the engine's asset loaders, failing the load the same way (returning SCRIPT_LOAD_FAILED) for a missing entry or one bigger than capacity. test_duskscript's fixtures moved to their own assets/ subfolder and pack into a small test.dsk with the same tools.asset.pack used for the real dusk.dsk, and the test opens that archive straight into ASSET.zip (skipping the full assetInit()/assetInitPlatform(), which expects a launched engine) so the tests exercise the real compile -> pack -> asset -> runtime pipeline instead of a filesystem shortcut around it. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
1 parent
5cf0678429
commit
a0294aae98
5 files changed
+70
-28
No files matched your search
@@ -9,8 +9,8 @@
|
|||||||
#include "native/scriptnativeconsole.h"
|
#include "native/scriptnativeconsole.h"
|
||||||
#include "builtin/scriptbuiltinregister.h"
|
#include "builtin/scriptbuiltinregister.h"
|
||||||
#include "console/console.h"
|
#include "console/console.h"
|
||||||
|
#include "asset/asset.h"
|
||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
script_t SCRIPT;
|
script_t SCRIPT;
|
||||||
|
|
||||||
@@ -29,14 +29,33 @@ void scriptPrintHandler(const char *text, const size_t length) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t scriptLoadHandler(const char *path, char *buffer, const size_t capacity) {
|
size_t scriptLoadHandler(const char *path, char *buffer, const size_t capacity) {
|
||||||
FILE *file;
|
assetfile_t file;
|
||||||
size_t length;
|
size_t size;
|
||||||
|
|
||||||
file = fopen(path, "rb");
|
// Scripts are packed into dusk.dsk like every other asset, so this reads
|
||||||
if(!file) return (size_t)-1;
|
// through the asset system rather than the real filesystem - a missing
|
||||||
length = fread(buffer, 1, capacity, file);
|
// entry or one too big for capacity both just fail the include/load.
|
||||||
fclose(file);
|
if(errorIsNotOk(assetFileInit(&file, path, NULL, NULL))) return SCRIPT_LOAD_FAILED;
|
||||||
return length;
|
|
||||||
|
size = (size_t)file.size;
|
||||||
|
if(size > capacity) {
|
||||||
|
assetFileDispose(&file);
|
||||||
|
return SCRIPT_LOAD_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(errorIsNotOk(assetFileOpen(&file))) {
|
||||||
|
assetFileDispose(&file);
|
||||||
|
return SCRIPT_LOAD_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(errorIsNotOk(assetFileRead(&file, buffer, size))) {
|
||||||
|
assetFileDispose(&file);
|
||||||
|
return SCRIPT_LOAD_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
|
assetFileClose(&file);
|
||||||
|
assetFileDispose(&file);
|
||||||
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t scriptInit(void) {
|
errorret_t scriptInit(void) {
|
||||||
|
|||||||
@@ -7,29 +7,32 @@ include(dusktest)
|
|||||||
|
|
||||||
dusktest(test_duskscript.c)
|
dusktest(test_duskscript.c)
|
||||||
|
|
||||||
# Compile the fixture scripts to bytecode with duskscriptcc at build time, so
|
# Scripts are loaded through the asset system (see scriptLoadHandler in
|
||||||
# the test exercises the real compiler -> runtime -> consolePrint pipeline.
|
# src/dusk/script/script.c), not the real filesystem, so the fixtures are
|
||||||
set(DUSKSCRIPT_TEST_FIXTURE_DS ${CMAKE_CURRENT_LIST_DIR}/fixture.ds)
|
# packed into a small test.dsk with the same tools.asset.pack used for the
|
||||||
set(DUSKSCRIPT_TEST_FIXTURE_DSB ${CMAKE_CURRENT_BINARY_DIR}/fixture.dsb)
|
# game's own dusk.dsk - this exercises the real compiler -> pack -> asset ->
|
||||||
set(DUSKSCRIPT_TEST_NUMBERS_DS ${CMAKE_CURRENT_LIST_DIR}/fixture_numbers.ds)
|
# runtime -> consolePrint pipeline rather than a shortcut around it.
|
||||||
set(DUSKSCRIPT_TEST_NUMBERS_DSB ${CMAKE_CURRENT_BINARY_DIR}/fixture_numbers.dsb)
|
set(DUSKSCRIPT_TEST_ASSETS_DIR ${CMAKE_CURRENT_LIST_DIR}/assets)
|
||||||
|
set(DUSKSCRIPT_TEST_DSK ${CMAKE_CURRENT_BINARY_DIR}/test.dsk)
|
||||||
|
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
OUTPUT ${DUSKSCRIPT_TEST_FIXTURE_DSB}
|
OUTPUT ${DUSKSCRIPT_TEST_DSK}
|
||||||
COMMAND duskscriptcc ${DUSKSCRIPT_TEST_FIXTURE_DS} -o ${DUSKSCRIPT_TEST_FIXTURE_DSB}
|
COMMAND ${Python3_EXECUTABLE} -m tools.asset.pack
|
||||||
DEPENDS duskscriptcc ${DUSKSCRIPT_TEST_FIXTURE_DS}
|
--input ${DUSKSCRIPT_TEST_ASSETS_DIR}
|
||||||
COMMENT "Compiling duskscript test fixture"
|
--output ${DUSKSCRIPT_TEST_DSK}
|
||||||
|
--duskscriptcc "$<TARGET_FILE:duskscriptcc>"
|
||||||
|
WORKING_DIRECTORY ${DUSK_ROOT_DIR}
|
||||||
|
DEPENDS
|
||||||
|
duskscriptcc
|
||||||
|
${DUSKSCRIPT_TEST_ASSETS_DIR}/fixture.ds
|
||||||
|
${DUSKSCRIPT_TEST_ASSETS_DIR}/fixture_numbers.ds
|
||||||
|
COMMENT "Packing duskscript test fixtures"
|
||||||
)
|
)
|
||||||
add_custom_command(
|
add_custom_target(duskscript_test_fixture DEPENDS ${DUSKSCRIPT_TEST_DSK})
|
||||||
OUTPUT ${DUSKSCRIPT_TEST_NUMBERS_DSB}
|
|
||||||
COMMAND duskscriptcc ${DUSKSCRIPT_TEST_NUMBERS_DS} -o ${DUSKSCRIPT_TEST_NUMBERS_DSB}
|
|
||||||
DEPENDS duskscriptcc ${DUSKSCRIPT_TEST_NUMBERS_DS}
|
|
||||||
COMMENT "Compiling duskscript numbers test fixture"
|
|
||||||
)
|
|
||||||
add_custom_target(duskscript_test_fixture DEPENDS ${DUSKSCRIPT_TEST_FIXTURE_DSB} ${DUSKSCRIPT_TEST_NUMBERS_DSB})
|
|
||||||
add_dependencies(test_duskscript duskscript_test_fixture)
|
add_dependencies(test_duskscript duskscript_test_fixture)
|
||||||
|
|
||||||
target_compile_definitions(test_duskscript PRIVATE
|
target_compile_definitions(test_duskscript PRIVATE
|
||||||
DUSKSCRIPT_TEST_FIXTURE="${DUSKSCRIPT_TEST_FIXTURE_DSB}"
|
DUSKSCRIPT_TEST_DSK="${DUSKSCRIPT_TEST_DSK}"
|
||||||
DUSKSCRIPT_TEST_NUMBERS="${DUSKSCRIPT_TEST_NUMBERS_DSB}"
|
DUSKSCRIPT_TEST_FIXTURE="fixture.dsb"
|
||||||
|
DUSKSCRIPT_TEST_NUMBERS="fixture_numbers.dsb"
|
||||||
)
|
)
|
||||||
File renamed without changes.
File renamed without changes.
@@ -8,6 +8,9 @@
|
|||||||
#include "dusktest.h"
|
#include "dusktest.h"
|
||||||
#include "script/script.h"
|
#include "script/script.h"
|
||||||
#include "console/console.h"
|
#include "console/console.h"
|
||||||
|
#include "asset/asset.h"
|
||||||
|
#include "asset/assetdsk.h"
|
||||||
|
#include "util/memory.h"
|
||||||
|
|
||||||
static void test_scriptRunsAndPrintsToConsole(void **state) {
|
static void test_scriptRunsAndPrintsToConsole(void **state) {
|
||||||
scripterror_t error;
|
scripterror_t error;
|
||||||
@@ -52,10 +55,27 @@ static void test_scriptNumbersAreAllFloats(void **state) {
|
|||||||
consoleDispose();
|
consoleDispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fixtures load through the asset system now, so ASSET needs a real zip
|
||||||
|
// behind it - open the packed test.dsk directly rather than the full
|
||||||
|
// assetInit()/assetInitPlatform(), which expects a real launched engine.
|
||||||
|
static int duskscript_test_setup(void **state) {
|
||||||
|
memoryZero(&ASSET, sizeof(ASSET));
|
||||||
|
threadMutexInit(&ASSET.zipLock);
|
||||||
|
assert_int_equal(errorIsOk(assetDskOpenFromPath(DUSKSCRIPT_TEST_DSK, &ASSET.zip)), true);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int duskscript_test_teardown(void **state) {
|
||||||
|
zip_close(ASSET.zip);
|
||||||
|
threadMutexDispose(&ASSET.zipLock);
|
||||||
|
memoryZero(&ASSET, sizeof(ASSET));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
const struct CMUnitTest tests[] = {
|
const struct CMUnitTest tests[] = {
|
||||||
cmocka_unit_test(test_scriptRunsAndPrintsToConsole),
|
cmocka_unit_test(test_scriptRunsAndPrintsToConsole),
|
||||||
cmocka_unit_test(test_scriptNumbersAreAllFloats),
|
cmocka_unit_test(test_scriptNumbersAreAllFloats),
|
||||||
};
|
};
|
||||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
return cmocka_run_group_tests(tests, duskscript_test_setup, duskscript_test_teardown);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user