Add include()
This commit is contained in:
@@ -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