Lua script something
This commit is contained in:
10
src/script/CMakeLists.txt
Normal file
10
src/script/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
scriptmanager.c
|
||||
)
|
||||
74
src/script/scriptmanager.c
Normal file
74
src/script/scriptmanager.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "scriptmanager.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
#include "asset/asset.h"
|
||||
|
||||
int luaCallable(lua_State *L) {
|
||||
printf("This function was called from Lua!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
scriptmanager_t SCRIPT_MANAGER;
|
||||
|
||||
errorret_t scriptManagerInit() {
|
||||
memoryZero(&SCRIPT_MANAGER, sizeof(scriptmanager_t));
|
||||
|
||||
SCRIPT_MANAGER.luaState = luaL_newstate();
|
||||
if(SCRIPT_MANAGER.luaState == NULL) {
|
||||
errorThrow("Failed to init Lua state");
|
||||
}
|
||||
|
||||
luaL_openlibs(SCRIPT_MANAGER.luaState);
|
||||
lua_register(SCRIPT_MANAGER.luaState, "luaCallable", luaCallable);
|
||||
|
||||
// SCript test
|
||||
assetscript_t script;
|
||||
assetLoad("script/test.dsf", &script);
|
||||
|
||||
if(lua_load(SCRIPT_MANAGER.luaState, assetScriptReader, &script, "script/test.dsf", NULL) != LUA_OK) {
|
||||
const char_t *strErr = lua_tostring(SCRIPT_MANAGER.luaState, -1);
|
||||
lua_pop(SCRIPT_MANAGER.luaState, 1);
|
||||
errorThrow("Failed to load Lua script: %s", strErr);
|
||||
}
|
||||
|
||||
if(lua_pcall(SCRIPT_MANAGER.luaState, 0, LUA_MULTRET, 0) != LUA_OK) {
|
||||
const char_t *strErr = lua_tostring(SCRIPT_MANAGER.luaState, -1);
|
||||
lua_pop(SCRIPT_MANAGER.luaState, 1);
|
||||
errorThrow("Failed to execute Lua script: %s", strErr);
|
||||
}
|
||||
|
||||
assetScriptDispose(&script);
|
||||
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t scriptManagerExec(const char_t *script) {
|
||||
if(SCRIPT_MANAGER.luaState == NULL) {
|
||||
errorThrow("Lua state is not initialized");
|
||||
}
|
||||
|
||||
if(luaL_dostring(SCRIPT_MANAGER.luaState, script) != LUA_OK) {
|
||||
const char_t *strErr = lua_tostring(SCRIPT_MANAGER.luaState, -1);
|
||||
lua_pop(SCRIPT_MANAGER.luaState, 1);
|
||||
errorThrow("Failed to execute Lua: ", strErr);
|
||||
}
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t scriptManagerDispose() {
|
||||
if(SCRIPT_MANAGER.luaState != NULL) {
|
||||
lua_close(SCRIPT_MANAGER.luaState);
|
||||
SCRIPT_MANAGER.luaState = NULL;
|
||||
}
|
||||
|
||||
errorOk();
|
||||
}
|
||||
40
src/script/scriptmanager.h
Normal file
40
src/script/scriptmanager.h
Normal 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 "error/error.h"
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
typedef struct scriptmanager_s {
|
||||
lua_State *luaState;
|
||||
} scriptmanager_t;
|
||||
|
||||
extern scriptmanager_t SCRIPT_MANAGER;
|
||||
|
||||
/**
|
||||
* Initialize the script manager.
|
||||
*
|
||||
* @return The error return value.
|
||||
*/
|
||||
errorret_t scriptManagerInit();
|
||||
|
||||
/**
|
||||
* Execute a Lua script.
|
||||
*
|
||||
* @param script The script to execute.
|
||||
* @return The error return value.
|
||||
*/
|
||||
errorret_t scriptManagerExec(const char_t *script);
|
||||
|
||||
/**
|
||||
* Dispose of the script manager.
|
||||
*
|
||||
* @return The error return value.
|
||||
*/
|
||||
errorret_t scriptManagerDispose();
|
||||
Reference in New Issue
Block a user