feat(script): add lua_convertdecimal

This commit is contained in:
VDm 2025-03-29 16:30:15 +04:00
parent f039d4f88c
commit 6030a75530
5 changed files with 71 additions and 17 deletions

View File

@ -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;
}

View File

@ -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<int32_t>(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';

View File

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

View File

@ -5,6 +5,10 @@
#include "util/Unimplemented.hpp"
#include <cstdint>
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<int32_t>(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;
}
}

View File

@ -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