diff --git a/assets/init.lua b/assets/init.lua index d05291c..7d27e3a 100644 --- a/assets/init.lua +++ b/assets/init.lua @@ -1,6 +1,7 @@ module('platform') module('input') module('scene') +module('locale') -- Default Input bindings. if PLATFORM == "psp" then @@ -38,4 +39,5 @@ else end -- sceneSet('map') --- mapLoad('map/testmap/testmap.dmf') \ No newline at end of file +-- mapLoad('map/testmap/testmap.dmf') +localeSet(DUSK_LOCALE_EN_US) \ No newline at end of file diff --git a/src/locale/localemanager.c b/src/locale/localemanager.c index 295aba0..92b24b2 100644 --- a/src/locale/localemanager.c +++ b/src/locale/localemanager.c @@ -14,12 +14,14 @@ localemanager_t LOCALE; errorret_t localeManagerInit() { memoryZero(&LOCALE, sizeof(localemanager_t)); - errorChain(localeManagerSetLocale(DUSK_LOCALE_EN_US)); + // errorChain(localeManagerSetLocale(DUSK_LOCALE_EN_US)); errorOk(); } errorret_t localeManagerSetLocale(const dusklocale_t locale) { assertTrue(locale < DUSK_LOCALE_COUNT, "Invalid locale."); + assertTrue(locale != DUSK_LOCALE_NULL, "Cannot set locale to NULL."); + LOCALE.locale = locale; char_t languageFile[FILENAME_MAX]; snprintf( diff --git a/src/script/module/modulelocale.h b/src/script/module/modulelocale.h index e74d9fa..9d22055 100644 --- a/src/script/module/modulelocale.h +++ b/src/script/module/modulelocale.h @@ -9,6 +9,69 @@ #include "script/scriptcontext.h" #include "locale/localemanager.h" +int moduleLocaleGet(lua_State *L) { + assertNotNull(L, "Lua state cannot be NULL"); + + // No arguments expected + dusklocale_t locale = LOCALE.locale; + lua_pushinteger(L, (lua_Integer)locale); + return 1; +} + +int moduleLocaleSet(lua_State *L) { + assertNotNull(L, "Lua state cannot be NULL"); + + // Requires locale ID + if(!lua_isinteger(L, 1)) { + luaL_error(L, "localeSet: Expected locale ID as first argument"); + return 0; + } + + errorret_t err; + dusklocale_t locale = (dusklocale_t)lua_tointeger(L, 1); + if(locale >= DUSK_LOCALE_COUNT || locale == DUSK_LOCALE_NULL) { + luaL_error(L, "localeSet: Invalid locale ID"); + return 0; + } + + err = localeManagerSetLocale(locale); + if(err.code != ERROR_OK) { + luaL_error(L, "localeSet: Failed to set locale"); + return 0; + } + + return 0; +} + +int moduleLocaleGetName(lua_State *L) { + assertNotNull(L, "Lua state cannot be NULL"); + + // Optional ID, otherwise return current locale name + dusklocale_t locale = LOCALE.locale; + if(lua_gettop(L) >= 1) { + if(!lua_isinteger(L, 1)) { + luaL_error(L, "localeGetName: Expected locale ID as first argument"); + return 0; + } + locale = (dusklocale_t)lua_tointeger(L, 1); + if(locale >= DUSK_LOCALE_COUNT || locale == DUSK_LOCALE_NULL) { + luaL_error(L, "localeGetName: Invalid locale ID"); + return 0; + } + } + + 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/tools/asset/process/language.py b/tools/asset/process/language.py index 1e5cb37..63222fe 100644 --- a/tools/asset/process/language.py +++ b/tools/asset/process/language.py @@ -149,6 +149,7 @@ def processLanguageList(): f.write(headerKeys) # Generate language list. + langValues = {} headerLocale = "#pragma once\n#include \"locale/localeinfo.h\"\n\n" headerLocale += "typedef enum {\n" count = 0 @@ -156,6 +157,7 @@ def processLanguageList(): count += 1 for lang in LANGUAGE_DATA: langKey = lang.replace('-', '_').replace(' ', '_').upper() + langValues[lang] = count headerLocale += f" DUSK_LOCALE_{langKey} = {count},\n" count += 1 headerLocale += f" DUSK_LOCALE_COUNT = {count}\n" @@ -169,6 +171,13 @@ def processLanguageList(): headerLocale += f" }},\n" headerLocale += "};\n" + headerLocale += f"static const char_t *LOCALE_SCRIPT = \n" + for lang in LANGUAGE_DATA: + langKey = lang.replace('-', '_').replace(' ', '_').upper() + langValue = langValues[lang] + headerLocale += f" \"DUSK_LOCALE_{langKey} = {langValue}\\n\"\n" + headerLocale += ";\n" + # Write out the locale enum header file outputFile = os.path.join(args.headers_dir, "locale", "locale.h") os.makedirs(os.path.dirname(outputFile), exist_ok=True)