diff --git a/src/ui/CSimpleFontStringScript.cpp b/src/ui/CSimpleFontStringScript.cpp index ee90d77..f7b5594 100644 --- a/src/ui/CSimpleFontStringScript.cpp +++ b/src/ui/CSimpleFontStringScript.cpp @@ -139,8 +139,8 @@ int32_t CSimpleFontString_SetFormattedText(lua_State* L) { luaL_error(L, "%s:SetFormattedText(): Font not set", string->GetDisplayName()); } - char buffer[4096] = {}; - char* text = FrameScript_Sprintf(L, 2, buffer, sizeof(buffer)); + char buffer[4096]; + auto text = FrameScript_Sprintf(L, 2, buffer, sizeof(buffer)); string->SetText(text, 1); return 0; } diff --git a/src/ui/FrameScript.cpp b/src/ui/FrameScript.cpp index 22386a4..f5b5c9f 100644 --- a/src/ui/FrameScript.cpp +++ b/src/ui/FrameScript.cpp @@ -2,6 +2,7 @@ #include "ui/FrameScriptInternal.hpp" #include "ui/FrameScript_Object.hpp" #include "ui/LuaMemory.hpp" +#include "ui/LuaExtraFuncs.hpp" #include "util/CStatus.hpp" #include "util/Lua.hpp" #include "util/SFile.hpp" @@ -451,26 +452,24 @@ void FrameScript_GetColor(lua_State* L, int32_t idx, CImVector& color) { color.Set(a, r, g, b); } +int32_t SetDecimalConversion(int32_t enabled) { + return lua_setdecimalconversion(enabled); +} + lua_State* FrameScript_GetContext(void) { return FrameScript::s_context; } -char* FrameScript_Sprintf(lua_State* L, int32_t idx, char* buffer, uint32_t size) { +const char* FrameScript_Sprintf(lua_State* L, int32_t idx, char* buffer, uint32_t size) { size_t formatLength = 0; const char* format = luaL_checklstring(L, idx, &formatLength); const char* formatEnd = format + formatLength; char* result = buffer; - if (format >= formatEnd) { - *buffer = '\0'; - return result; - } - - int32_t currentIndex = idx; - while (size > 1) { + while (format < formatEnd && size > 1) { char character = *format++; if (character == '%') { char argument = *format; @@ -552,7 +551,7 @@ char* FrameScript_Sprintf(lua_State* L, int32_t idx, char* buffer, uint32_t size double number = luaL_checknumber(L, currentIndex); size_t length = SStrPrintf(buffer, size, subformat, number); if (length > 0) { - // TODO: lua_convertdecimal(buffer) + lua_convertdecimal(buffer); buffer += length; size -= length; } @@ -584,7 +583,7 @@ char* FrameScript_Sprintf(lua_State* L, int32_t idx, char* buffer, uint32_t size auto number = static_cast(luaL_checknumber(L, currentIndex)); size_t length = SStrPrintf(buffer, size, subformat, number); if (length > 0) { - // TODO: lua_convertdecimal(buffer) + lua_convertdecimal(buffer); buffer += length; size -= length; } @@ -612,10 +611,6 @@ char* FrameScript_Sprintf(lua_State* L, int32_t idx, char* buffer, uint32_t size *buffer++ = character; --size; } - - if (format >= formatEnd) { - break; - } } *buffer = '\0'; diff --git a/src/ui/FrameScript.hpp b/src/ui/FrameScript.hpp index a9f12f0..11af907 100644 --- a/src/ui/FrameScript.hpp +++ b/src/ui/FrameScript.hpp @@ -78,9 +78,11 @@ void FrameScript_Flush(); void FrameScript_GetColor(lua_State* L, int32_t idx, CImVector& color); +int32_t SetDecimalConversion(int32_t enabled); + lua_State* FrameScript_GetContext(); -char* FrameScript_Sprintf(lua_State * L, int32_t idx, char* buffer, uint32_t size); +const char* FrameScript_Sprintf(lua_State * L, int32_t idx, char* buffer, uint32_t size); const char* FrameScript_GetCurrentObject(lua_State* L, int32_t a2); diff --git a/src/ui/LuaExtraFuncs.cpp b/src/ui/LuaExtraFuncs.cpp index 71664a3..f7362a3 100644 --- a/src/ui/LuaExtraFuncs.cpp +++ b/src/ui/LuaExtraFuncs.cpp @@ -5,6 +5,10 @@ #include "util/Unimplemented.hpp" #include + +static bool s_luaDecimalConversion = false; + + luaL_Reg FrameScriptInternal::extra_funcs[31] = { { "setglobal", &sub_8168D0 }, { "getglobal", &sub_816910 }, @@ -169,3 +173,53 @@ int32_t debuglocals(lua_State* L) { int32_t scrub(lua_State* L) { WHOA_UNIMPLEMENTED(0); } + +int32_t lua_setdecimalconversion(int32_t enabled) { + s_luaDecimalConversion = (enabled != 0); + return static_cast(s_luaDecimalConversion); +} + +void lua_convertdecimal(char* string) { + if (!s_luaDecimalConversion) { + return; + } + + size_t length = SStrLen(string); + if (!length) { + return; + } + + char* end = &string[length]; + while (string < end) { + if (*string < '0' || *string > '9') { + ++string; + continue; + } + + char ch; + while (true) { + ch = *string; + + if ((ch < '0' || ch > '9') && ch != '.') { + break; + } + + if (ch == ',') { +LABEL_13: + if (string[1] >= '0' && string[1] <= '9') { + *string = '.'; + } + } else { + if (ch == '.' && (string[1] < '0' || string[1] > '9')) { + *string = ','; + } + } + ++string; + } + if (ch == ',') { + goto LABEL_13; + } + + ++string; + } +} diff --git a/src/ui/LuaExtraFuncs.hpp b/src/ui/LuaExtraFuncs.hpp index c4f8966..0cf4542 100644 --- a/src/ui/LuaExtraFuncs.hpp +++ b/src/ui/LuaExtraFuncs.hpp @@ -36,4 +36,7 @@ int32_t debugstack(lua_State*); int32_t debuglocals(lua_State*); int32_t scrub(lua_State*); +int32_t lua_setdecimalconversion(int32_t enabled); +void lua_convertdecimal(char* string); + #endif