Update some script stuff.
Some checks failed
Build Dusk / run-tests (push) Failing after 2m12s
Build Dusk / build-linux (push) Successful in 2m3s
Build Dusk / build-psp (push) Failing after 1m28s

This commit is contained in:
2026-01-27 23:48:46 -06:00
parent cc85983736
commit 25dc97e3cc
24 changed files with 451 additions and 133 deletions

View File

@@ -0,0 +1,10 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
modulesystem.c
)

View File

@@ -0,0 +1,121 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "modulesystem.h"
#include "debug/debug.h"
#include "assert/assert.h"
#include "util/string.h"
#include "script/scriptmodule.h"
void moduleSystem(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
scriptContextRegFunc(context, "print", moduleSysPrint);
scriptContextRegFunc(context, "include", moduleSysInclude);
scriptContextRegFunc(context, "module", moduleSysModule);
}
int moduleSysPrint(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
int n = lua_gettop(L);
luaL_Buffer b;
luaL_buffinit(L, &b);
for (int i = 1; i <= n; ++i) {
size_t len;
const char *s = luaL_tolstring(L, i, &len); // converts any value to string
luaL_addlstring(&b, s, len);
lua_pop(L, 1); // pop result of luaL_tolstring
if (i < n) luaL_addlstring(&b, "\t", 1);
}
luaL_pushresult(&b);
const char *msg = lua_tostring(L, -1);
debugPrint("%s\n", msg);
return 0; // no values returned to Lua
}
int moduleSysInclude(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
if(!lua_isstring(L, 1)) {
luaL_error(L, "Expected string filename");
return 0;
}
scriptcontext_t* ctx = *(scriptcontext_t**)lua_getextraspace(L);
if(ctx == NULL) {
luaL_error(L, "Script context is NULL");
return 0;
}
const char_t *filename = luaL_checkstring(L, 1);
if(filename == NULL || filename[0] == '\0') {
luaL_error(L, "Filename cannot be NULL");
return 0;
}
// Copy out filename to mutable buffer
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);
}
// Execute the script file
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;
}
int moduleSysModule(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
if(!lua_isstring(L, 1)) {
luaL_error(L, "Expected string module name");
return 0;
}
const char_t *moduleName = luaL_checkstring(L, 1);
if(moduleName == NULL) {
luaL_error(L, "Module name cannot be NULL");
return 0;
}
const scriptmodule_t *module = scriptModuleGetByName(moduleName);
if(module == NULL) {
luaL_error(L, "Module '%s' not found", moduleName);
return 0;
}
scriptcontext_t* ctx = *(scriptcontext_t**)lua_getextraspace(L);
if(ctx == NULL) {
luaL_error(L, "Script context is NULL");
return 0;
}
module->callback(ctx);
return 0;
}

View File

@@ -0,0 +1,40 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/scriptcontext.h"
/**
* Register system module functions to the given script context.
*
* @param context The script context to register system module functions to.
*/
void moduleSystem(scriptcontext_t *context);
/**
* Script binding for printing messages to the debug console.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleSysPrint(lua_State *L);
/**
* Script binding for including and executing another script file.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleSysInclude(lua_State *L);
/**
* Script binding for loading a script module by name.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleSysModule(lua_State *L);