Modules
This commit is contained in:
@@ -8,4 +8,5 @@ target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
scriptmanager.c
|
||||
scriptcontext.c
|
||||
scriptmodule.c
|
||||
)
|
||||
@@ -1,14 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/scriptcontext.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
void scriptFuncCamera(scriptcontext_t *context) {
|
||||
assertNotNull(context, "Script context cannot be NULL");
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/scriptcontext.h"
|
||||
#include "scene/scenemanager.h"
|
||||
#include "debug/debug.h"
|
||||
#include "assert/assert.h"
|
||||
#include "error/error.h"
|
||||
|
||||
int32_t scriptFuncSetScene(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
assertTrue(lua_isstring(L, 1), "First argument must be a string");
|
||||
|
||||
const char_t *sceneName = lua_tostring(L, 1);
|
||||
scene_t *scene = sceneManagerGetSceneByName(sceneName);
|
||||
assertNotNull(scene, "Scene with given name does not exist");
|
||||
|
||||
sceneManagerSetScene(scene);
|
||||
|
||||
if(scene->init) {
|
||||
errorret_t err = scene->init(&SCENE_MANAGER.sceneData);
|
||||
assertTrue(err.code == ERROR_OK, "Scene initialization failed");
|
||||
}
|
||||
scene->flags |= SCENE_FLAG_INITIALIZED;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void scriptFuncScene(scriptcontext_t *context) {
|
||||
assertNotNull(context, "Script context cannot be NULL");
|
||||
|
||||
scriptContextRegFunc(context, "setScene", scriptFuncSetScene);
|
||||
}
|
||||
65
src/script/module/moduleinput.h
Normal file
65
src/script/module/moduleinput.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/scriptcontext.h"
|
||||
#include "input/input.h"
|
||||
|
||||
int32_t moduleInputBind(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
// Requires action and button.
|
||||
if(!lua_isstring(L, 1)) {
|
||||
luaL_error(L, "inputBind: Expected button name as first argument");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(!lua_isstring(L, 2)) {
|
||||
luaL_error(L, "inputBind: Expected action name as second argument");
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char_t *strBtn = lua_tostring(L, 1);
|
||||
const char_t *strAct = lua_tostring(L, 2);
|
||||
|
||||
// Get button by name
|
||||
inputbutton_t btn = inputButtonGetByName(strBtn);
|
||||
if(btn.type == INPUT_BUTTON_TYPE_NONE) {
|
||||
printf("inputBind: Unknown button name '%s'\n", strBtn);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get action by name
|
||||
inputaction_t act = inputActionGetByName(strAct);
|
||||
if(act == INPUT_ACTION_COUNT) {
|
||||
printf("inputBind: Unknown action name '%s'\n", strAct);
|
||||
return 0;
|
||||
}
|
||||
|
||||
inputBind(btn, act);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void moduleInput(scriptcontext_t *context) {
|
||||
assertNotNull(context, "Script context cannot be NULL");
|
||||
|
||||
// Input values.
|
||||
scriptContextExec(context,
|
||||
#if INPUT_KEYBOARD == 1
|
||||
"INPUT_KEYBOARD = true\n"
|
||||
#endif
|
||||
#if INPUT_GAMEPAD == 1
|
||||
"INPUT_GAMEPAD = true\n"
|
||||
#endif
|
||||
#if INPUT_SDL2 == 1
|
||||
"INPUT_SDL2 = true\n"
|
||||
#endif
|
||||
);
|
||||
|
||||
// Bind methods
|
||||
scriptContextRegFunc(context, "inputBind", moduleInputBind);
|
||||
}
|
||||
21
src/script/module/moduleplatform.h
Normal file
21
src/script/module/moduleplatform.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/scriptcontext.h"
|
||||
|
||||
#ifndef DUSK_TARGET_SYSTEM
|
||||
#error "DUSK_TARGET_SYSTEM must be defined"
|
||||
#endif
|
||||
|
||||
#define PLATFORM_VALUE "PLATFORM = '" DUSK_TARGET_SYSTEM "'"
|
||||
|
||||
void modulePlatform(scriptcontext_t *ctx) {
|
||||
assertNotNull(ctx, "Script context cannot be NULL");
|
||||
|
||||
scriptContextExec(ctx, PLATFORM_VALUE);
|
||||
}
|
||||
42
src/script/module/modulescene.h
Normal file
42
src/script/module/modulescene.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/scriptcontext.h"
|
||||
#include "scene/scenemanager.h"
|
||||
|
||||
int32_t moduleSceneSetScene(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
assertTrue(lua_isstring(L, 1), "Expected string scene name");
|
||||
|
||||
const char *sceneName = luaL_checkstring(L, 1);
|
||||
if(sceneName == NULL || sceneName[0] == '\0') {
|
||||
luaL_error(L, "Scene name cannot be NULL");
|
||||
return 0;
|
||||
}
|
||||
|
||||
scene_t *scene = sceneManagerGetSceneByName(sceneName);
|
||||
if(scene == NULL) {
|
||||
luaL_error(L, "Scene '%s' not found", sceneName);
|
||||
return 0;
|
||||
}
|
||||
|
||||
errorret_t err = sceneManagerSetScene(scene);
|
||||
if(err.code != ERROR_OK) {
|
||||
|
||||
luaL_error(L, "Failed to set scene '%s'", sceneName);
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void moduleScene(scriptcontext_t *ctx) {
|
||||
assertNotNull(ctx, "Script context cannot be NULL");
|
||||
|
||||
scriptContextRegFunc(ctx, "sceneSet", moduleSceneSetScene);
|
||||
}
|
||||
@@ -10,8 +10,9 @@
|
||||
#include "debug/debug.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/string.h"
|
||||
#include "script/scriptmodule.h"
|
||||
|
||||
int32_t scriptFuncPrint(lua_State *L) {
|
||||
int32_t moduleSysPrint(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
|
||||
int n = lua_gettop(L);
|
||||
@@ -32,16 +33,25 @@ int32_t scriptFuncPrint(lua_State *L) {
|
||||
return 0; // no values returned to Lua
|
||||
}
|
||||
|
||||
int32_t scriptFuncInclude(lua_State *L) {
|
||||
int32_t moduleSysInclude(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL");
|
||||
assertTrue(lua_isstring(L, 1), "Expected string filename");
|
||||
|
||||
if(!lua_isstring(L, 1)) {
|
||||
luaL_error(L, "Expected string filename");
|
||||
return 0;
|
||||
}
|
||||
|
||||
scriptcontext_t* ctx = *(scriptcontext_t**)lua_getextraspace(L);
|
||||
assertNotNull(ctx, "Script context cannot be NULL");
|
||||
if(ctx == NULL) {
|
||||
luaL_error(L, "Script context is NULL");
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char_t *filename = luaL_checkstring(L, 1);
|
||||
assertNotNull(filename, "Filename cannot be NULL");
|
||||
assertStrLenMin(filename, 1, "Filename cannot be empty");
|
||||
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];
|
||||
@@ -72,9 +82,41 @@ int32_t scriptFuncInclude(lua_State *L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void scriptFuncSystem(scriptcontext_t *context) {
|
||||
int32_t 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;
|
||||
}
|
||||
|
||||
void moduleSystem(scriptcontext_t *context) {
|
||||
assertNotNull(context, "Script context cannot be NULL");
|
||||
|
||||
scriptContextRegFunc(context, "print", scriptFuncPrint);
|
||||
scriptContextRegFunc(context, "include", scriptFuncInclude);
|
||||
scriptContextRegFunc(context, "print", moduleSysPrint);
|
||||
scriptContextRegFunc(context, "include", moduleSysInclude);
|
||||
scriptContextRegFunc(context, "module", moduleSysModule);
|
||||
}
|
||||
@@ -10,11 +10,7 @@
|
||||
#include "asset/asset.h"
|
||||
#include "util/memory.h"
|
||||
#include "debug/debug.h"
|
||||
|
||||
#include "script/func/scriptfunccamera.h"
|
||||
#include "script/func/scriptfuncentity.h"
|
||||
#include "script/func/scriptfuncsystem.h"
|
||||
#include "script/func/scriptfuncscene.h"
|
||||
#include "script/scriptmodule.h"
|
||||
|
||||
errorret_t scriptContextInit(scriptcontext_t *context) {
|
||||
assertNotNull(context, "Script context cannot be NULL");
|
||||
@@ -30,15 +26,13 @@ errorret_t scriptContextInit(scriptcontext_t *context) {
|
||||
|
||||
// Store context in Lua extraspace
|
||||
*(scriptcontext_t**)lua_getextraspace(context->luaState) = context;
|
||||
|
||||
// Register variables
|
||||
// scriptContextExec(context, "PLATFORM = 'DESKTOP'");
|
||||
|
||||
// Register functions
|
||||
scriptFuncSystem(context);
|
||||
scriptFuncEntity(context);
|
||||
scriptFuncCamera(context);
|
||||
scriptFuncScene(context);
|
||||
|
||||
// All scripts get the system module
|
||||
const scriptmodule_t *sysModule = scriptModuleGetByName("system");
|
||||
if(sysModule == NULL) {
|
||||
errorThrow("Failed to find system script module");
|
||||
}
|
||||
sysModule->callback(context);
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
32
src/script/scriptmodule.c
Normal file
32
src/script/scriptmodule.c
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "scriptmodule.h"
|
||||
#include "script/module/modulesystem.h"
|
||||
#include "script/module/moduleinput.h"
|
||||
#include "script/module/moduleplatform.h"
|
||||
#include "script/module/modulescene.h"
|
||||
|
||||
const scriptmodule_t SCRIPT_MODULE_LIST[] = {
|
||||
{ .name = "system", .callback = moduleSystem },
|
||||
{ .name = "input", .callback = moduleInput },
|
||||
{ .name = "platform", .callback = modulePlatform },
|
||||
{ .name = "scene", .callback = moduleScene },
|
||||
};
|
||||
|
||||
#define SCRIPT_MODULE_COUNT ( \
|
||||
sizeof(SCRIPT_MODULE_LIST) / sizeof(scriptmodule_t) \
|
||||
)
|
||||
|
||||
const scriptmodule_t * scriptModuleGetByName(const char_t *name) {
|
||||
for(uint_fast8_t i = 0; i < SCRIPT_MODULE_COUNT; i++) {
|
||||
if(stringCompare(SCRIPT_MODULE_LIST[i].name, name) != 0) continue;
|
||||
return &SCRIPT_MODULE_LIST[i];
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
24
src/script/scriptmodule.h
Normal file
24
src/script/scriptmodule.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "scriptcontext.h"
|
||||
|
||||
typedef struct scriptmodule_s {
|
||||
const char_t *name;
|
||||
void (*callback)(scriptcontext_t *ctx);
|
||||
} scriptmodule_t;
|
||||
|
||||
extern const scriptmodule_t SCRIPT_MODULE_LIST[];
|
||||
|
||||
/**
|
||||
* Retrieves a script module by its name.
|
||||
*
|
||||
* @param name The name of the script module.
|
||||
* @return Pointer to the script module, or NULL if not found.
|
||||
*/
|
||||
const scriptmodule_t * scriptModuleGetByName(const char_t *name);
|
||||
Reference in New Issue
Block a user