Add include()
Some checks failed
Build Dusk / build-linux (push) Successful in 1m16s
Build Dusk / build-psp (push) Failing after 1m14s

This commit is contained in:
2025-12-24 09:41:05 +10:00
parent a495179e5f
commit aed202ebf9
9 changed files with 55 additions and 21 deletions

View File

@@ -3,4 +3,5 @@
# This software is released under the MIT License. # This software is released under the MIT License.
# https://opensource.org/licenses/MIT # https://opensource.org/licenses/MIT
add_asset(SCRIPT test.lua) add_asset(SCRIPT test.lua)
add_asset(SCRIPT test2.lua)

View File

@@ -1,3 +1,5 @@
include('script/test2')
player = entityAdd(ENTITY_TYPE_PLAYER, 3, 6, 1) player = entityAdd(ENTITY_TYPE_PLAYER, 3, 6, 1)
print("Player entity ID: " .. player) print("Player entity ID: " .. player)

1
assets/script/test2.lua Normal file
View File

@@ -0,0 +1 @@
print("This is test2.lua")

View File

@@ -41,8 +41,6 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
errorChain(sceneManagerInit()); errorChain(sceneManagerInit());
// Run the initial script. // Run the initial script.
scriptcontext_t testCtx; scriptcontext_t testCtx;
errorChain(scriptContextInit(&testCtx)); errorChain(scriptContextInit(&testCtx));
errorChain(scriptContextExecFile(&testCtx, "script/test.dsf")); errorChain(scriptContextExecFile(&testCtx, "script/test.dsf"));

View File

@@ -1,13 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "dusk.h"
typedef struct cutscene_s cutscene_t;
typedef cutscene_t* cutscenecutscene_t;

View File

@@ -9,7 +9,8 @@
#include "cutscenewait.h" #include "cutscenewait.h"
#include "cutscenecallback.h" #include "cutscenecallback.h"
#include "cutscenetext.h" #include "cutscenetext.h"
#include "cutscenecutscene.h"
typedef struct cutscene_s cutscene_t;
typedef enum { typedef enum {
CUTSCENE_ITEM_TYPE_NULL, CUTSCENE_ITEM_TYPE_NULL,
@@ -27,7 +28,7 @@ typedef struct cutsceneitem_s {
cutscenetext_t text; cutscenetext_t text;
cutscenecallback_t callback; cutscenecallback_t callback;
cutscenewait_t wait; cutscenewait_t wait;
const cutscenecutscene_t cutscene; const cutscene_t *cutscene;
}; };
} cutsceneitem_t; } cutsceneitem_t;

View File

@@ -16,7 +16,7 @@ typedef struct map_s map_t;
typedef struct entity_s { typedef struct entity_s {
uint8_t id; uint8_t id;
entitytype_t type; entitytype_t type;
entitytypedata_t; entitytypedata_t data;
// Movement // Movement
entitydir_t direction; entitydir_t direction;

View File

@@ -9,8 +9,9 @@
#include "script/scriptcontext.h" #include "script/scriptcontext.h"
#include "debug/debug.h" #include "debug/debug.h"
#include "assert/assert.h" #include "assert/assert.h"
#include "util/string.h"
int32_t scriptContextPrint(lua_State *L) { int32_t scriptFuncPrint(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL"); assertNotNull(L, "Lua state cannot be NULL");
int n = lua_gettop(L); int n = lua_gettop(L);
@@ -31,8 +32,48 @@ int32_t scriptContextPrint(lua_State *L) {
return 0; // no values returned to Lua return 0; // no values returned to Lua
} }
int32_t scriptFuncInclude(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
scriptcontext_t* ctx = *(scriptcontext_t**)lua_getextraspace(L);
if (!ctx) return luaL_error(L, "Lua extraspace ctx not set");
const char_t *filename = luaL_checkstring(L, 1);
if(filename == NULL || filename[0] == '\0') {
luaL_error(L, "Filename cannot be NULL or empty");
return 0;
}
//
char_t buffer[1024];
stringCopy(buffer, filename, 1024);
// Ensure it has .dsf extension
size_t len = strlen(buffer);
if(len < 4 || strcmp(&buffer[len - 4], ".dsf") != 0) {
// Append .dsf
if(len + 4 >= 1024) {
luaL_error(L, "Filename too long to append .dsf");
return 0;
}
stringCopy(&buffer[len], ".dsf", 5);
}
errorret_t err = scriptContextExecFile(
ctx,
buffer
);
if(err.code != ERROR_OK) {
luaL_error(L, "Failed to include script file");
errorCatch(errorPrint(err));
return 0;
}
return 0;
}
void scriptFuncSystem(scriptcontext_t *context) { void scriptFuncSystem(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL"); assertNotNull(context, "Script context cannot be NULL");
scriptContextRegFunc(context, "print", scriptContextPrint); scriptContextRegFunc(context, "print", scriptFuncPrint);
scriptContextRegFunc(context, "include", scriptFuncInclude);
} }

View File

@@ -27,6 +27,9 @@ errorret_t scriptContextInit(scriptcontext_t *context) {
} }
luaL_openlibs(context->luaState); luaL_openlibs(context->luaState);
// Store context in Lua extraspace
*(scriptcontext_t**)lua_getextraspace(context->luaState) = context;
// Register functions // Register functions
scriptFuncSystem(context); scriptFuncSystem(context);
scriptFuncEntity(context); scriptFuncEntity(context);