Add include()
This commit is contained in:
@@ -3,4 +3,5 @@
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
add_asset(SCRIPT test.lua)
|
||||
add_asset(SCRIPT test.lua)
|
||||
add_asset(SCRIPT test2.lua)
|
||||
@@ -1,3 +1,5 @@
|
||||
include('script/test2')
|
||||
|
||||
player = entityAdd(ENTITY_TYPE_PLAYER, 3, 6, 1)
|
||||
|
||||
print("Player entity ID: " .. player)
|
||||
1
assets/script/test2.lua
Normal file
1
assets/script/test2.lua
Normal file
@@ -0,0 +1 @@
|
||||
print("This is test2.lua")
|
||||
@@ -41,8 +41,6 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
||||
errorChain(sceneManagerInit());
|
||||
|
||||
// Run the initial script.
|
||||
|
||||
|
||||
scriptcontext_t testCtx;
|
||||
errorChain(scriptContextInit(&testCtx));
|
||||
errorChain(scriptContextExecFile(&testCtx, "script/test.dsf"));
|
||||
|
||||
@@ -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;
|
||||
@@ -9,7 +9,8 @@
|
||||
#include "cutscenewait.h"
|
||||
#include "cutscenecallback.h"
|
||||
#include "cutscenetext.h"
|
||||
#include "cutscenecutscene.h"
|
||||
|
||||
typedef struct cutscene_s cutscene_t;
|
||||
|
||||
typedef enum {
|
||||
CUTSCENE_ITEM_TYPE_NULL,
|
||||
@@ -27,7 +28,7 @@ typedef struct cutsceneitem_s {
|
||||
cutscenetext_t text;
|
||||
cutscenecallback_t callback;
|
||||
cutscenewait_t wait;
|
||||
const cutscenecutscene_t cutscene;
|
||||
const cutscene_t *cutscene;
|
||||
};
|
||||
} cutsceneitem_t;
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ typedef struct map_s map_t;
|
||||
typedef struct entity_s {
|
||||
uint8_t id;
|
||||
entitytype_t type;
|
||||
entitytypedata_t;
|
||||
entitytypedata_t data;
|
||||
|
||||
// Movement
|
||||
entitydir_t direction;
|
||||
|
||||
@@ -9,8 +9,9 @@
|
||||
#include "script/scriptcontext.h"
|
||||
#include "debug/debug.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");
|
||||
|
||||
int n = lua_gettop(L);
|
||||
@@ -31,8 +32,48 @@ int32_t scriptContextPrint(lua_State *L) {
|
||||
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) {
|
||||
assertNotNull(context, "Script context cannot be NULL");
|
||||
|
||||
scriptContextRegFunc(context, "print", scriptContextPrint);
|
||||
scriptContextRegFunc(context, "print", scriptFuncPrint);
|
||||
scriptContextRegFunc(context, "include", scriptFuncInclude);
|
||||
}
|
||||
@@ -27,6 +27,9 @@ errorret_t scriptContextInit(scriptcontext_t *context) {
|
||||
}
|
||||
luaL_openlibs(context->luaState);
|
||||
|
||||
// Store context in Lua extraspace
|
||||
*(scriptcontext_t**)lua_getextraspace(context->luaState) = context;
|
||||
|
||||
// Register functions
|
||||
scriptFuncSystem(context);
|
||||
scriptFuncEntity(context);
|
||||
|
||||
Reference in New Issue
Block a user