Lua script something
Some checks failed
Build Dusk / build-linux (push) Failing after 2m56s
Build Dusk / build-psp (push) Failing after 1m49s

This commit is contained in:
2025-12-04 00:02:26 -06:00
parent 9aaf271996
commit 9f507be7bc
16 changed files with 369 additions and 1 deletions

View File

@@ -5,6 +5,7 @@
find_package(cglm REQUIRED)
find_package(libzip REQUIRED)
find_package(lua REQUIRED)
# Libs
target_link_libraries(${DUSK_TARGET_NAME}
@@ -12,6 +13,7 @@ target_link_libraries(${DUSK_TARGET_NAME}
m
cglm
zip
lua
pthread
)
@@ -41,6 +43,7 @@ add_subdirectory(input)
add_subdirectory(locale)
add_subdirectory(rpg)
add_subdirectory(scene)
add_subdirectory(script)
add_subdirectory(thread)
add_subdirectory(time)
add_subdirectory(ui)

View File

@@ -11,6 +11,7 @@
#include "type/assetlanguage.h"
#include "type/assetmap.h"
#include "type/assetchunk.h"
#include "type/assetscript.h"
#include <zip.h>
typedef enum {
@@ -21,6 +22,7 @@ typedef enum {
ASSET_TYPE_LANGUAGE,
ASSET_TYPE_MAP,
ASSET_TYPE_CHUNK,
ASSET_TYPE_SCRIPT,
ASSET_TYPE_COUNT,
} assettype_t;
@@ -81,5 +83,11 @@ static const assettypedef_t ASSET_TYPE_DEFINITIONS[ASSET_TYPE_COUNT] = {
.header = "DCF",
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
.custom = assetChunkLoad
}
},
[ASSET_TYPE_SCRIPT] = {
.header = "DSF",
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
.custom = assetScriptHandler
}
};

View File

@@ -11,4 +11,5 @@ target_sources(${DUSK_TARGET_NAME}
assetlanguage.c
assetmap.c
assetchunk.c
assetscript.c
)

View File

@@ -0,0 +1,58 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "asset/asset.h"
#include "assert/assert.h"
errorret_t assetScriptHandler(assetcustom_t custom) {
assertNotNull(custom.zipFile, "Custom asset zip file cannot be NULL");
assertNotNull(custom.output, "Custom asset output cannot be NULL");
assetscript_t *script = (assetscript_t *)custom.output;
errorChain(assetScriptInit(script, custom.zipFile));
errorOk();
}
errorret_t assetScriptInit(
assetscript_t *script,
zip_file_t *zipFile
) {
assertNotNull(script, "Script asset cannot be NULL");
assertNotNull(zipFile, "Zip file cannot be NULL");
// We now own the zip file handle.
script->zip = zipFile;
errorOk();
}
const char_t * assetScriptReader(lua_State* lState, void* data, size_t* size) {
assetscript_t *script = (assetscript_t *)data;
zip_int64_t bytesRead = zip_fread(
script->zip, script->buffer, sizeof(script->buffer)
);
if(bytesRead < 0) {
*size = 0;
return NULL;
}
*size = (size_t)bytesRead;
return script->buffer;
}
errorret_t assetScriptDispose(assetscript_t *script) {
assertNotNull(script, "Script asset cannot be NULL");
if(script->zip != NULL) {
zip_fclose(script->zip);
script->zip = NULL;
}
errorOk();
}

View File

@@ -0,0 +1,57 @@
/**
* 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 "duskdefs.h"
#include <zip.h>
#include <lua.h>
#define ASSET_SCRIPT_BUFFER_SIZE 1024
typedef struct assetscript_s {
zip_file_t *zip;
char_t buffer[ASSET_SCRIPT_BUFFER_SIZE];
} assetscript_t;
typedef struct assetcustom_s assetcustom_t;
/**
* Receiving function from the asset manager to handle script assets.
*
* @param custom Custom asset loading data.
* @return Error code.
*/
errorret_t assetScriptHandler(assetcustom_t custom);
/**
* Initializes a script asset.
*
* @param script Script asset to initialize.
* @param zipFile Zip file handle for the script asset.
* @return Error code.
*/
errorret_t assetScriptInit(assetscript_t *script, zip_file_t *zipFile);
/**
* Reader function for Lua to read script data from the asset.
*
* @param L Lua state.
* @param data Pointer to the assetscript_t structure.
* @param size Pointer to store the size of the read data.
* @return Pointer to the read data buffer.
*/
const char_t * assetScriptReader(lua_State* L, void* data, size_t* size);
/**
* Disposes of a script asset, freeing any allocated resources.
*
* @param script Script asset to dispose of.
* @return Error code.
*/
errorret_t assetScriptDispose(assetscript_t *script);

View File

@@ -15,6 +15,7 @@
#include "asset/asset.h"
#include "ui/ui.h"
#include "rpg/rpg.h"
#include "script/scriptmanager.h"
#include "debug/debug.h"
engine_t ENGINE;
@@ -35,6 +36,12 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
errorChain(uiInit());
errorChain(rpgInit());
errorChain(sceneManagerInit());
errorChain(scriptManagerInit());
// scriptManagerExec(
// "print('Hello from Lua!')\n"
// "luaCallable()\n"
// );
errorOk();
}

View File

@@ -11,6 +11,7 @@
#include "input/input.h"
int main(int argc, char **argv) {
// Main applet
errorret_t ret;
// Init engine

10
src/script/CMakeLists.txt Normal file
View 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
)

View 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();
}

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 "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();