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

@@ -2,7 +2,6 @@ module('platform')
module('input') module('input')
module('scene') module('scene')
module('locale') module('locale')
module('event')
-- Default Input bindings. -- Default Input bindings.
if PLATFORM == "psp" then if PLATFORM == "psp" then
@@ -39,13 +38,6 @@ else
end end
end end
localeSet(DUSK_LOCALE_EN_US)
-- sceneSet('map') -- sceneSet('map')
-- mapLoad('map/testmap/testmap.dmf') -- mapLoad('map/testmap/testmap.dmf')
localeSet(DUSK_LOCALE_EN_US)
function eventTest(data)
print("Pressed")
data.action = 2
end
eventSubscribe(INPUT_EVENT_PRESSED, eventTest)

View File

@@ -228,7 +228,7 @@ void eventInvoke(
lua_rawgeti(L, LUA_REGISTRYINDEX, listener->user.script.luaFunctionRef); lua_rawgeti(L, LUA_REGISTRYINDEX, listener->user.script.luaFunctionRef);
if(eventParams != NULL && metatableName != NULL) { if(eventParams != NULL && metatableName != NULL) {
scriptStructPush( scriptStructPushMetatable(
listener->user.script.context, listener->user.script.context,
metatableName, metatableName,
(void *)eventParams (void *)eventParams

View File

@@ -11,3 +11,6 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
scriptmodule.c scriptmodule.c
scriptstruct.c scriptstruct.c
) )
# Subdirectories
add_subdirectory(module)

View File

@@ -0,0 +1,11 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Subdirectories
add_subdirectory(event)
add_subdirectory(input)
add_subdirectory(item)
add_subdirectory(locale)
add_subdirectory(system)

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_LIBRARY_TARGET_NAME}
PUBLIC
moduleevent.c
)

View File

@@ -5,10 +5,10 @@
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
#pragma once #include "moduleevent.h"
#include "script/scriptcontext.h"
#include "event/event.h" #include "event/event.h"
#include "engine/engine.h" #include "engine/engine.h"
#include "assert/assert.h"
int moduleEventSubscribe(lua_State *L) { int moduleEventSubscribe(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL"); assertNotNull(L, "Lua state cannot be NULL");

View File

@@ -0,0 +1,21 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/scriptcontext.h"
/**
* Register event functions to the given script context.
*
* @param context The script context to register event functions to.
*/
void moduleEvent(scriptcontext_t *context);
/**
* Script binding for subscribing to an event.
*/
int moduleEventSubscribe(lua_State *L);

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_LIBRARY_TARGET_NAME}
PUBLIC
moduleinput.c
)

View File

@@ -1,14 +1,63 @@
/** /**
* Copyright (c) 2025 Dominic Masters * Copyright (c) 2026 Dominic Masters
* *
* This software is released under the MIT License. * This software is released under the MIT License.
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
#pragma once #include "moduleinput.h"
#include "script/scriptcontext.h"
#include "input/input.h" #include "input/input.h"
#include "script/scriptstruct.h" #include "assert/assert.h"
#include "util/string.h"
void moduleInput(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
// Setup enums.
scriptContextExec(context, INPUT_ACTION_SCRIPT);
// Input values.
scriptContextExec(context,
#if INPUT_KEYBOARD == 1
"INPUT_KEYBOARD = true\n"
#else
""
#endif
#if INPUT_GAMEPAD == 1
"INPUT_GAMEPAD = true\n"
#else
""
#endif
#if INPUT_SDL2 == 1
"INPUT_SDL2 = true\n"
#else
""
#endif
);
// Script structure
scriptStructRegister(context, "input_mt", &moduleInputEventGetter, NULL);
// Events
scriptContextRegPointer(context,"INPUT_EVENT_PRESSED",&INPUT.eventPressed);
scriptContextRegPointer(context,"INPUT_EVENT_RELEASED",&INPUT.eventReleased);
// Bind methods
scriptContextRegFunc(context, "inputBind", moduleInputBind);
}
void moduleInputEventGetter(
const scriptcontext_t *context,
const char_t *key,
const void *structPtr,
scriptvalue_t *outValue
) {
if(stringCompare(key, "action") == 0) {
outValue->type = SCRIPT_VALUE_TYPE_INT;
outValue->value.intValue = ((const inputevent_t*)structPtr)->action;
return;
}
}
int moduleInputBind(lua_State *L) { int moduleInputBind(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL"); assertNotNull(L, "Lua state cannot be NULL");
@@ -44,51 +93,3 @@ int moduleInputBind(lua_State *L) {
inputBind(btn, action); inputBind(btn, action);
return 0; return 0;
} }
void moduleInputEventGetter(
const scriptcontext_t *context,
const char_t *key,
const void *structPtr,
scriptvalue_t *outValue
) {
if(stringCompare(key, "action") == 0) {
outValue->type = SCRIPT_VALUE_TYPE_INT;
outValue->value.intValue = ((const inputevent_t*)structPtr)->action;
return;
}
}
void moduleInput(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
// Setup enums.
scriptContextExec(context, INPUT_ACTION_SCRIPT);
// 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
);
// Script structure
scriptStructRegister(
context,
"input_mt",
&moduleInputEventGetter,
NULL
);
// Events
scriptContextRegPointer(context,"INPUT_EVENT_PRESSED",&INPUT.eventPressed);
scriptContextRegPointer(context,"INPUT_EVENT_RELEASED",&INPUT.eventReleased);
// Bind methods
scriptContextRegFunc(context, "inputBind", moduleInputBind);
}

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"
#include "script/scriptstruct.h"
/**
* Register input functions to the given script context.
*
* @param context The script context to register input functions to.
*/
void moduleInput(scriptcontext_t *context);
/**
* Script event getter for input events.
*
* @param context The script context.
* @param key The key to get.
* @param structPtr Pointer to the input event struct.
* @param outValue Output script value.
*/
void moduleInputEventGetter(
const scriptcontext_t *context,
const char_t *key,
const void *structPtr,
scriptvalue_t *outValue
);
/**
* Script binding for binding an input button to an action.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInputBind(lua_State *L);

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
moduleitem.c
)

View File

@@ -5,10 +5,32 @@
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
#pragma once #include "moduleitem.h"
#include "script/scriptcontext.h"
#include "item/inventory.h" #include "item/inventory.h"
#include "item/backpack.h" #include "item/backpack.h"
#include "assert/assert.h"
void moduleItem(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
// Set item information
scriptContextExec(context, ITEM_SCRIPT);
// Bind BACKPACK const pointer
scriptContextRegPointer(context, "BACKPACK", (void *)&BACKPACK);
// Bind Methods
scriptContextRegFunc(
context, "inventoryItemExists", moduleInventoryItemExists
);
scriptContextRegFunc(context, "inventoryAdd", moduleInventoryAdd);
scriptContextRegFunc(context, "inventorySet", moduleInventorySet);
scriptContextRegFunc(context, "inventoryRemove", moduleInventoryRemove);
scriptContextRegFunc(context, "inventoryGetCount", moduleInventoryGetCount);
scriptContextRegFunc(context, "inventoryIsFull", moduleInventoryIsFull);
scriptContextRegFunc(context, "inventoryItemFull", moduleInventoryItemFull);
scriptContextRegFunc(context, "inventorySort", moduleInventorySort);
}
int moduleInventoryItemExists(lua_State *L) { int moduleInventoryItemExists(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL"); assertNotNull(L, "Lua state cannot be NULL");
@@ -237,24 +259,3 @@ int moduleInventorySort(lua_State *L) {
return 0; return 0;
} }
void moduleItem(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
// Set item information
scriptContextExec(context, ITEM_SCRIPT);
// Bind BACKPACK const pointer
scriptContextRegPointer(context, "BACKPACK", (void *)&BACKPACK);
// Bind Methods
scriptContextRegFunc(
context, "inventoryItemExists", moduleInventoryItemExists
);
scriptContextRegFunc(context, "inventoryAdd", moduleInventoryAdd);
scriptContextRegFunc(context, "inventorySet", moduleInventorySet);
scriptContextRegFunc(context, "inventoryRemove", moduleInventoryRemove);
scriptContextRegFunc(context, "inventoryGetCount", moduleInventoryGetCount);
scriptContextRegFunc(context, "inventoryIsFull", moduleInventoryIsFull);
scriptContextRegFunc(context, "inventoryItemFull", moduleInventoryItemFull);
scriptContextRegFunc(context, "inventorySort", moduleInventorySort);
}

View File

@@ -0,0 +1,80 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/scriptcontext.h"
/**
* Register item functions to the given script context.
*
* @param context The script context to register item functions to.
*/
void moduleItem(scriptcontext_t *context);
/**
* Script binding for checking if an item exists in an inventory.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventoryItemExists(lua_State *L);
/**
* Script binding for adding an item to an inventory.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventorySet(lua_State *L);
/**
* Script binding for setting the quantity of an item in an inventory.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventoryAdd(lua_State *L);
/**
* Script binding for removing an item from an inventory.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventoryRemove(lua_State *L);
/**
* Script binding for getting the count of an item in an inventory.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventoryGetCount(lua_State *L);
/**
* Script binding for checking if an inventory is full.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventoryIsFull(lua_State *L);
/**
* Script binding for checking if an item stack in an inventory is full.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventoryItemFull(lua_State *L);
/**
* Script binding for sorting an inventory.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleInventorySort(lua_State *L);

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
modulelocale.c
)

View File

@@ -5,9 +5,20 @@
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
#pragma once #include "modulelocale.h"
#include "script/scriptcontext.h"
#include "locale/localemanager.h" #include "locale/localemanager.h"
#include "assert/assert.h"
void moduleLocale(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
// Execute the locale script definitions.
scriptContextExec(context, LOCALE_SCRIPT);
scriptContextRegFunc(context, "localeGet", moduleLocaleGet);
scriptContextRegFunc(context, "localeSet", moduleLocaleSet);
scriptContextRegFunc(context, "localeGetName", moduleLocaleGetName);
}
int moduleLocaleGet(lua_State *L) { int moduleLocaleGet(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL"); assertNotNull(L, "Lua state cannot be NULL");
@@ -64,14 +75,3 @@ int moduleLocaleGetName(lua_State *L) {
lua_pushstring(L, localeName); lua_pushstring(L, localeName);
return 1; return 1;
} }
void moduleLocale(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
// Execute the locale script definitions.
scriptContextExec(context, LOCALE_SCRIPT);
scriptContextRegFunc(context, "localeGet", moduleLocaleGet);
scriptContextRegFunc(context, "localeSet", moduleLocaleSet);
scriptContextRegFunc(context, "localeGetName", moduleLocaleGetName);
}

View File

@@ -0,0 +1,40 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "script/scriptcontext.h"
/**
* Register locale functions to the given script context.
*
* @param context The script context to register locale functions to.
*/
void moduleLocale(scriptcontext_t *context);
/**
* Script binding for getting the current locale ID.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleLocaleGet(lua_State *L);
/**
* Script binding for setting the current locale ID.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleLocaleSet(lua_State *L);
/**
* Script binding for getting the name of a locale by its ID.
*
* @param L The Lua state.
* @return Number of return values on the Lua stack.
*/
int moduleLocaleGetName(lua_State *L);

View File

@@ -7,6 +7,7 @@
#pragma once #pragma once
#include "script/scriptcontext.h" #include "script/scriptcontext.h"
#include "assert/assert.h"
#ifndef DUSK_TARGET_SYSTEM #ifndef DUSK_TARGET_SYSTEM
#error "DUSK_TARGET_SYSTEM must be defined" #error "DUSK_TARGET_SYSTEM must be defined"

View File

@@ -8,22 +8,31 @@
#pragma once #pragma once
#include "script/scriptcontext.h" #include "script/scriptcontext.h"
#include "time/time.h" #include "time/time.h"
#include "util/string.h"
int moduleTimeGetDelta(lua_State *L) { void moduleTimeGetter(
assertNotNull(L, "Lua state cannot be NULL"); const scriptcontext_t *context,
lua_pushnumber(L, TIME.delta); const char_t *key,
return 1; const void *structPtr,
scriptvalue_t *outValue
) {
if(stringCompare(key, "delta") == 0) {
outValue->type = SCRIPT_VALUE_TYPE_FLOAT;
outValue->value.floatValue = TIME.delta;
return;
} else if(stringCompare(key, "time") == 0) {
outValue->type = SCRIPT_VALUE_TYPE_FLOAT;
outValue->value.floatValue = TIME.time;
return;
} }
int moduleTimeGetTime(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL");
lua_pushnumber(L, TIME.time);
return 1;
} }
void moduleTime(scriptcontext_t *ctx) { void moduleTime(scriptcontext_t *ctx) {
assertNotNull(ctx, "Script context cannot be NULL"); assertNotNull(ctx, "Script context cannot be NULL");
scriptContextRegFunc(ctx, "timeGetDelta", moduleTimeGetDelta); // Script structure
scriptContextRegFunc(ctx, "timeGetTime", moduleTimeGetTime); scriptStructRegister(ctx, "time_mt", moduleTimeGetter, NULL);
// Register struct
scriptStructPush(ctx, "time_mt", "TIME", &TIME);
} }

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

@@ -5,13 +5,20 @@
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
#pragma once #include "modulesystem.h"
#include "script/scriptcontext.h"
#include "debug/debug.h" #include "debug/debug.h"
#include "assert/assert.h" #include "assert/assert.h"
#include "util/string.h" #include "util/string.h"
#include "script/scriptmodule.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) { int moduleSysPrint(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL"); assertNotNull(L, "Lua state cannot be NULL");
@@ -112,11 +119,3 @@ int moduleSysModule(lua_State *L) {
return 0; return 0;
} }
void moduleSystem(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be NULL");
scriptContextRegFunc(context, "print", moduleSysPrint);
scriptContextRegFunc(context, "include", moduleSysInclude);
scriptContextRegFunc(context, "module", moduleSysModule);
}

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

View File

@@ -6,14 +6,15 @@
*/ */
#include "scriptmodule.h" #include "scriptmodule.h"
#include "script/module/modulesystem.h" #include "script/module/system/modulesystem.h"
#include "script/module/moduleinput.h" #include "script/module/input/moduleinput.h"
#include "script/module/moduleplatform.h" #include "script/module/moduleplatform.h"
#include "script/module/modulescene.h" #include "script/module/modulescene.h"
#include "script/module/moduleitem.h" #include "script/module/item/moduleitem.h"
#include "script/module/modulelocale.h" #include "script/module/locale/modulelocale.h"
#include "script/module/moduletime.h" #include "script/module/moduletime.h"
#include "script/module/moduleevent.h" #include "script/module/event/moduleevent.h"
#include "util/string.h"
const scriptmodule_t SCRIPT_MODULE_LIST[] = { const scriptmodule_t SCRIPT_MODULE_LIST[] = {
{ .name = "system", .callback = moduleSystem }, { .name = "system", .callback = moduleSystem },

View File

@@ -146,7 +146,7 @@ structmetatablecontext_t * scriptStructGetMetatableContext(lua_State *L) {
return metaContext; return metaContext;
} }
void scriptStructPush( void scriptStructPushMetatable(
scriptcontext_t *context, scriptcontext_t *context,
const char_t *metatableName, const char_t *metatableName,
void *structPtr void *structPtr
@@ -163,3 +163,18 @@ void scriptStructPush(
luaL_getmetatable(context->luaState, metatableName); luaL_getmetatable(context->luaState, metatableName);
lua_setmetatable(context->luaState, -2); lua_setmetatable(context->luaState, -2);
} }
void scriptStructPush(
scriptcontext_t *context,
const char_t *metatableName,
const char_t *variableName,
void *structPtr
) {
assertNotNull(context, "Script context cannot be NULL");
assertNotNull(metatableName, "Metatable name cannot be NULL");
assertNotNull(variableName, "Variable name cannot be NULL");
assertNotNull(structPtr, "Structure pointer cannot be NULL");
scriptStructPushMetatable(context, metatableName, structPtr);
lua_setglobal(context->luaState, variableName);
}

View File

@@ -71,15 +71,29 @@ int scriptStructNewIndex(lua_State *l);
structmetatablecontext_t *scriptStructGetMetatableContext(lua_State *l); structmetatablecontext_t *scriptStructGetMetatableContext(lua_State *l);
/** /**
* Pushes a structure onto the Lua stack, associating it with the given * Pushes a structure metatable onto the Lua stack.
* metatable.
* *
* @param context The script context. * @param context The script context.
* @param metatableName The name of the metatable to associate with. * @param metatableName The name of the metatable to associate with.
* @param structPtr Pointer to the structure to push. * @param structPtr Pointer to the structure to push.
*/ */
void scriptStructPush( void scriptStructPushMetatable(
scriptcontext_t *context, scriptcontext_t *context,
const char_t *metatableName, const char_t *metatableName,
void *structPtr void *structPtr
); );
/**
* Pushes a structure onto the Lua stack and assigns it to a global variable.
*
* @param context The script context.
* @param metatableName The name of the metatable to associate with.
* @param variableName The name of the global variable to assign to.
* @param structPtr Pointer to the structure to push.
*/
void scriptStructPush(
scriptcontext_t *context,
const char_t *metatableName,
const char_t *variableName,
void *structPtr
);