Lua script something
This commit is contained in:
@@ -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
|
||||
}
|
||||
};
|
||||
@@ -11,4 +11,5 @@ target_sources(${DUSK_TARGET_NAME}
|
||||
assetlanguage.c
|
||||
assetmap.c
|
||||
assetchunk.c
|
||||
assetscript.c
|
||||
)
|
||||
58
src/asset/type/assetscript.c
Normal file
58
src/asset/type/assetscript.c
Normal 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();
|
||||
}
|
||||
57
src/asset/type/assetscript.h
Normal file
57
src/asset/type/assetscript.h
Normal 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);
|
||||
|
||||
Reference in New Issue
Block a user