From 25dc97e3cc72a122e48c9634eec896044f315609 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 27 Jan 2026 23:48:46 -0600 Subject: [PATCH] Update some script stuff. --- assets/init.lua | 12 +- src/event/event.c | 2 +- src/script/CMakeLists.txt | 5 +- src/script/module/CMakeLists.txt | 11 ++ src/script/module/event/CMakeLists.txt | 10 ++ .../{moduleevent.h => event/moduleevent.c} | 4 +- src/script/module/event/moduleevent.h | 21 ++++ src/script/module/input/CMakeLists.txt | 10 ++ .../{moduleinput.h => input/moduleinput.c} | 105 +++++++++--------- src/script/module/input/moduleinput.h | 40 +++++++ src/script/module/item/CMakeLists.txt | 10 ++ .../{moduleitem.h => item/moduleitem.c} | 47 ++++---- src/script/module/item/moduleitem.h | 80 +++++++++++++ src/script/module/locale/CMakeLists.txt | 10 ++ .../{modulelocale.h => locale/modulelocale.c} | 26 ++--- src/script/module/locale/modulelocale.h | 40 +++++++ src/script/module/moduleplatform.h | 1 + src/script/module/moduletime.h | 33 ++++-- src/script/module/system/CMakeLists.txt | 10 ++ .../{modulesystem.h => system/modulesystem.c} | 19 ++-- src/script/module/system/modulesystem.h | 40 +++++++ src/script/scriptmodule.c | 11 +- src/script/scriptstruct.c | 17 ++- src/script/scriptstruct.h | 20 +++- 24 files changed, 451 insertions(+), 133 deletions(-) create mode 100644 src/script/module/CMakeLists.txt create mode 100644 src/script/module/event/CMakeLists.txt rename src/script/module/{moduleevent.h => event/moduleevent.c} (95%) create mode 100644 src/script/module/event/moduleevent.h create mode 100644 src/script/module/input/CMakeLists.txt rename src/script/module/{moduleinput.h => input/moduleinput.c} (89%) create mode 100644 src/script/module/input/moduleinput.h create mode 100644 src/script/module/item/CMakeLists.txt rename src/script/module/{moduleitem.h => item/moduleitem.c} (99%) create mode 100644 src/script/module/item/moduleitem.h create mode 100644 src/script/module/locale/CMakeLists.txt rename src/script/module/{modulelocale.h => locale/modulelocale.c} (97%) create mode 100644 src/script/module/locale/modulelocale.h create mode 100644 src/script/module/system/CMakeLists.txt rename src/script/module/{modulesystem.h => system/modulesystem.c} (98%) create mode 100644 src/script/module/system/modulesystem.h diff --git a/assets/init.lua b/assets/init.lua index 8f7de7f..2f0072c 100644 --- a/assets/init.lua +++ b/assets/init.lua @@ -2,7 +2,6 @@ module('platform') module('input') module('scene') module('locale') -module('event') -- Default Input bindings. if PLATFORM == "psp" then @@ -39,13 +38,6 @@ else end end --- sceneSet('map') --- mapLoad('map/testmap/testmap.dmf') localeSet(DUSK_LOCALE_EN_US) - -function eventTest(data) - print("Pressed") - data.action = 2 -end - -eventSubscribe(INPUT_EVENT_PRESSED, eventTest) \ No newline at end of file +-- sceneSet('map') +-- mapLoad('map/testmap/testmap.dmf') \ No newline at end of file diff --git a/src/event/event.c b/src/event/event.c index 0a89b8c..cc3fdf5 100644 --- a/src/event/event.c +++ b/src/event/event.c @@ -228,7 +228,7 @@ void eventInvoke( lua_rawgeti(L, LUA_REGISTRYINDEX, listener->user.script.luaFunctionRef); if(eventParams != NULL && metatableName != NULL) { - scriptStructPush( + scriptStructPushMetatable( listener->user.script.context, metatableName, (void *)eventParams diff --git a/src/script/CMakeLists.txt b/src/script/CMakeLists.txt index 259d4b5..da4ed3c 100644 --- a/src/script/CMakeLists.txt +++ b/src/script/CMakeLists.txt @@ -10,4 +10,7 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME} scriptcontext.c scriptmodule.c scriptstruct.c -) \ No newline at end of file +) + +# Subdirectories +add_subdirectory(module) \ No newline at end of file diff --git a/src/script/module/CMakeLists.txt b/src/script/module/CMakeLists.txt new file mode 100644 index 0000000..787abce --- /dev/null +++ b/src/script/module/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/src/script/module/event/CMakeLists.txt b/src/script/module/event/CMakeLists.txt new file mode 100644 index 0000000..ffc4e91 --- /dev/null +++ b/src/script/module/event/CMakeLists.txt @@ -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 +) \ No newline at end of file diff --git a/src/script/module/moduleevent.h b/src/script/module/event/moduleevent.c similarity index 95% rename from src/script/module/moduleevent.h rename to src/script/module/event/moduleevent.c index 9512c10..371a129 100644 --- a/src/script/module/moduleevent.h +++ b/src/script/module/event/moduleevent.c @@ -5,10 +5,10 @@ * https://opensource.org/licenses/MIT */ -#pragma once -#include "script/scriptcontext.h" +#include "moduleevent.h" #include "event/event.h" #include "engine/engine.h" +#include "assert/assert.h" int moduleEventSubscribe(lua_State *L) { assertNotNull(L, "Lua state cannot be NULL"); diff --git a/src/script/module/event/moduleevent.h b/src/script/module/event/moduleevent.h new file mode 100644 index 0000000..c7c34f0 --- /dev/null +++ b/src/script/module/event/moduleevent.h @@ -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); \ No newline at end of file diff --git a/src/script/module/input/CMakeLists.txt b/src/script/module/input/CMakeLists.txt new file mode 100644 index 0000000..928bd2d --- /dev/null +++ b/src/script/module/input/CMakeLists.txt @@ -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 +) \ No newline at end of file diff --git a/src/script/module/moduleinput.h b/src/script/module/input/moduleinput.c similarity index 89% rename from src/script/module/moduleinput.h rename to src/script/module/input/moduleinput.c index 4b27e53..e441d85 100644 --- a/src/script/module/moduleinput.h +++ b/src/script/module/input/moduleinput.c @@ -1,14 +1,63 @@ /** - * Copyright (c) 2025 Dominic Masters + * Copyright (c) 2026 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ -#pragma once -#include "script/scriptcontext.h" +#include "moduleinput.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) { assertNotNull(L, "Lua state cannot be NULL"); @@ -43,52 +92,4 @@ int moduleInputBind(lua_State *L) { inputBind(btn, action); 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); } \ No newline at end of file diff --git a/src/script/module/input/moduleinput.h b/src/script/module/input/moduleinput.h new file mode 100644 index 0000000..bacedb1 --- /dev/null +++ b/src/script/module/input/moduleinput.h @@ -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); \ No newline at end of file diff --git a/src/script/module/item/CMakeLists.txt b/src/script/module/item/CMakeLists.txt new file mode 100644 index 0000000..250fc61 --- /dev/null +++ b/src/script/module/item/CMakeLists.txt @@ -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 +) \ No newline at end of file diff --git a/src/script/module/moduleitem.h b/src/script/module/item/moduleitem.c similarity index 99% rename from src/script/module/moduleitem.h rename to src/script/module/item/moduleitem.c index 968b668..8d94441 100644 --- a/src/script/module/moduleitem.h +++ b/src/script/module/item/moduleitem.c @@ -5,10 +5,32 @@ * https://opensource.org/licenses/MIT */ -#pragma once -#include "script/scriptcontext.h" +#include "moduleitem.h" #include "item/inventory.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) { assertNotNull(L, "Lua state cannot be NULL"); @@ -237,24 +259,3 @@ int moduleInventorySort(lua_State *L) { 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); -} \ No newline at end of file diff --git a/src/script/module/item/moduleitem.h b/src/script/module/item/moduleitem.h new file mode 100644 index 0000000..91b0ac9 --- /dev/null +++ b/src/script/module/item/moduleitem.h @@ -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); diff --git a/src/script/module/locale/CMakeLists.txt b/src/script/module/locale/CMakeLists.txt new file mode 100644 index 0000000..fc5b99b --- /dev/null +++ b/src/script/module/locale/CMakeLists.txt @@ -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 +) \ No newline at end of file diff --git a/src/script/module/modulelocale.h b/src/script/module/locale/modulelocale.c similarity index 97% rename from src/script/module/modulelocale.h rename to src/script/module/locale/modulelocale.c index 9d22055..a53311c 100644 --- a/src/script/module/modulelocale.h +++ b/src/script/module/locale/modulelocale.c @@ -5,9 +5,20 @@ * https://opensource.org/licenses/MIT */ -#pragma once -#include "script/scriptcontext.h" +#include "modulelocale.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) { assertNotNull(L, "Lua state cannot be NULL"); @@ -63,15 +74,4 @@ int moduleLocaleGetName(lua_State *L) { const char_t *localeName = LOCALE_INFOS[locale].file; lua_pushstring(L, localeName); 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); } \ No newline at end of file diff --git a/src/script/module/locale/modulelocale.h b/src/script/module/locale/modulelocale.h new file mode 100644 index 0000000..e3abc1d --- /dev/null +++ b/src/script/module/locale/modulelocale.h @@ -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); diff --git a/src/script/module/moduleplatform.h b/src/script/module/moduleplatform.h index f50cf04..7ae4bf2 100644 --- a/src/script/module/moduleplatform.h +++ b/src/script/module/moduleplatform.h @@ -7,6 +7,7 @@ #pragma once #include "script/scriptcontext.h" +#include "assert/assert.h" #ifndef DUSK_TARGET_SYSTEM #error "DUSK_TARGET_SYSTEM must be defined" diff --git a/src/script/module/moduletime.h b/src/script/module/moduletime.h index c98bb85..f0d0251 100644 --- a/src/script/module/moduletime.h +++ b/src/script/module/moduletime.h @@ -8,22 +8,31 @@ #pragma once #include "script/scriptcontext.h" #include "time/time.h" +#include "util/string.h" -int moduleTimeGetDelta(lua_State *L) { - assertNotNull(L, "Lua state cannot be NULL"); - lua_pushnumber(L, TIME.delta); - return 1; -} - -int moduleTimeGetTime(lua_State *L) { - assertNotNull(L, "Lua state cannot be NULL"); - lua_pushnumber(L, TIME.time); - return 1; +void moduleTimeGetter( + const scriptcontext_t *context, + const char_t *key, + 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; + } } void moduleTime(scriptcontext_t *ctx) { assertNotNull(ctx, "Script context cannot be NULL"); - scriptContextRegFunc(ctx, "timeGetDelta", moduleTimeGetDelta); - scriptContextRegFunc(ctx, "timeGetTime", moduleTimeGetTime); + // Script structure + scriptStructRegister(ctx, "time_mt", moduleTimeGetter, NULL); + + // Register struct + scriptStructPush(ctx, "time_mt", "TIME", &TIME); } \ No newline at end of file diff --git a/src/script/module/system/CMakeLists.txt b/src/script/module/system/CMakeLists.txt new file mode 100644 index 0000000..efb9b2e --- /dev/null +++ b/src/script/module/system/CMakeLists.txt @@ -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 +) \ No newline at end of file diff --git a/src/script/module/modulesystem.h b/src/script/module/system/modulesystem.c similarity index 98% rename from src/script/module/modulesystem.h rename to src/script/module/system/modulesystem.c index fff2ffd..efe2a00 100644 --- a/src/script/module/modulesystem.h +++ b/src/script/module/system/modulesystem.c @@ -5,13 +5,20 @@ * https://opensource.org/licenses/MIT */ -#pragma once -#include "script/scriptcontext.h" +#include "modulesystem.h" #include "debug/debug.h" #include "assert/assert.h" #include "util/string.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) { assertNotNull(L, "Lua state cannot be NULL"); @@ -111,12 +118,4 @@ int moduleSysModule(lua_State *L) { module->callback(ctx); 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); } \ No newline at end of file diff --git a/src/script/module/system/modulesystem.h b/src/script/module/system/modulesystem.h new file mode 100644 index 0000000..d267462 --- /dev/null +++ b/src/script/module/system/modulesystem.h @@ -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); \ No newline at end of file diff --git a/src/script/scriptmodule.c b/src/script/scriptmodule.c index b42c914..d30a772 100644 --- a/src/script/scriptmodule.c +++ b/src/script/scriptmodule.c @@ -6,14 +6,15 @@ */ #include "scriptmodule.h" -#include "script/module/modulesystem.h" -#include "script/module/moduleinput.h" +#include "script/module/system/modulesystem.h" +#include "script/module/input/moduleinput.h" #include "script/module/moduleplatform.h" #include "script/module/modulescene.h" -#include "script/module/moduleitem.h" -#include "script/module/modulelocale.h" +#include "script/module/item/moduleitem.h" +#include "script/module/locale/modulelocale.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[] = { { .name = "system", .callback = moduleSystem }, diff --git a/src/script/scriptstruct.c b/src/script/scriptstruct.c index 39f10a2..401d8a7 100644 --- a/src/script/scriptstruct.c +++ b/src/script/scriptstruct.c @@ -146,7 +146,7 @@ structmetatablecontext_t * scriptStructGetMetatableContext(lua_State *L) { return metaContext; } -void scriptStructPush( +void scriptStructPushMetatable( scriptcontext_t *context, const char_t *metatableName, void *structPtr @@ -162,4 +162,19 @@ void scriptStructPush( // Set metatable luaL_getmetatable(context->luaState, metatableName); 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); } \ No newline at end of file diff --git a/src/script/scriptstruct.h b/src/script/scriptstruct.h index 4fde009..ea3adbf 100644 --- a/src/script/scriptstruct.h +++ b/src/script/scriptstruct.h @@ -71,15 +71,29 @@ int scriptStructNewIndex(lua_State *l); structmetatablecontext_t *scriptStructGetMetatableContext(lua_State *l); /** - * Pushes a structure onto the Lua stack, associating it with the given - * metatable. + * Pushes a structure metatable onto the Lua stack. * * @param context The script context. * @param metatableName The name of the metatable to associate with. * @param structPtr Pointer to the structure to push. */ -void scriptStructPush( +void scriptStructPushMetatable( scriptcontext_t *context, const char_t *metatableName, 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 ); \ No newline at end of file