Update some script stuff.
This commit is contained in:
@@ -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
|
||||||
|
|
||||||
-- sceneSet('map')
|
|
||||||
-- mapLoad('map/testmap/testmap.dmf')
|
|
||||||
localeSet(DUSK_LOCALE_EN_US)
|
localeSet(DUSK_LOCALE_EN_US)
|
||||||
|
-- sceneSet('map')
|
||||||
function eventTest(data)
|
-- mapLoad('map/testmap/testmap.dmf')
|
||||||
print("Pressed")
|
|
||||||
data.action = 2
|
|
||||||
end
|
|
||||||
|
|
||||||
eventSubscribe(INPUT_EVENT_PRESSED, eventTest)
|
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -10,4 +10,7 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
|||||||
scriptcontext.c
|
scriptcontext.c
|
||||||
scriptmodule.c
|
scriptmodule.c
|
||||||
scriptstruct.c
|
scriptstruct.c
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Subdirectories
|
||||||
|
add_subdirectory(module)
|
||||||
11
src/script/module/CMakeLists.txt
Normal file
11
src/script/module/CMakeLists.txt
Normal 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)
|
||||||
10
src/script/module/event/CMakeLists.txt
Normal file
10
src/script/module/event/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_LIBRARY_TARGET_NAME}
|
||||||
|
PUBLIC
|
||||||
|
moduleevent.c
|
||||||
|
)
|
||||||
@@ -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");
|
||||||
21
src/script/module/event/moduleevent.h
Normal file
21
src/script/module/event/moduleevent.h
Normal 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);
|
||||||
10
src/script/module/input/CMakeLists.txt
Normal file
10
src/script/module/input/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_LIBRARY_TARGET_NAME}
|
||||||
|
PUBLIC
|
||||||
|
moduleinput.c
|
||||||
|
)
|
||||||
@@ -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");
|
||||||
@@ -43,52 +92,4 @@ 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);
|
|
||||||
}
|
}
|
||||||
40
src/script/module/input/moduleinput.h
Normal file
40
src/script/module/input/moduleinput.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 "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);
|
||||||
10
src/script/module/item/CMakeLists.txt
Normal file
10
src/script/module/item/CMakeLists.txt
Normal 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
|
||||||
|
)
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
80
src/script/module/item/moduleitem.h
Normal file
80
src/script/module/item/moduleitem.h
Normal 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);
|
||||||
10
src/script/module/locale/CMakeLists.txt
Normal file
10
src/script/module/locale/CMakeLists.txt
Normal 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
|
||||||
|
)
|
||||||
@@ -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");
|
||||||
@@ -63,15 +74,4 @@ int moduleLocaleGetName(lua_State *L) {
|
|||||||
const char_t *localeName = LOCALE_INFOS[locale].file;
|
const char_t *localeName = LOCALE_INFOS[locale].file;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
40
src/script/module/locale/modulelocale.h
Normal file
40
src/script/module/locale/modulelocale.h
Normal 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);
|
||||||
@@ -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"
|
||||||
|
|||||||
@@ -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
|
||||||
|
) {
|
||||||
int moduleTimeGetTime(lua_State *L) {
|
if(stringCompare(key, "delta") == 0) {
|
||||||
assertNotNull(L, "Lua state cannot be NULL");
|
outValue->type = SCRIPT_VALUE_TYPE_FLOAT;
|
||||||
lua_pushnumber(L, TIME.time);
|
outValue->value.floatValue = TIME.delta;
|
||||||
return 1;
|
return;
|
||||||
|
} else if(stringCompare(key, "time") == 0) {
|
||||||
|
outValue->type = SCRIPT_VALUE_TYPE_FLOAT;
|
||||||
|
outValue->value.floatValue = TIME.time;
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
10
src/script/module/system/CMakeLists.txt
Normal file
10
src/script/module/system/CMakeLists.txt
Normal 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
|
||||||
|
)
|
||||||
@@ -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");
|
||||||
|
|
||||||
@@ -111,12 +118,4 @@ int moduleSysModule(lua_State *L) {
|
|||||||
module->callback(ctx);
|
module->callback(ctx);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
40
src/script/module/system/modulesystem.h
Normal file
40
src/script/module/system/modulesystem.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 "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);
|
||||||
@@ -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 },
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -162,4 +162,19 @@ void scriptStructPush(
|
|||||||
// Set metatable
|
// Set metatable
|
||||||
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);
|
||||||
}
|
}
|
||||||
@@ -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
|
||||||
);
|
);
|
||||||
Reference in New Issue
Block a user