From 03804e0ff4cdf928e3134f0334d60303421f83e4 Mon Sep 17 00:00:00 2001 From: VDm Date: Mon, 9 Dec 2024 22:10:40 +0400 Subject: [PATCH 01/17] chore(ui): add CSimpleFrame_SetBackdropColor and CSimpleFrame_SetBackdropBorderColor --- src/net/connection/RealmConnection.cpp | 2 +- src/ui/CBackdropGenerator.cpp | 7 +++++ src/ui/CBackdropGenerator.hpp | 1 + src/ui/CSimpleFrame.cpp | 14 ++++++++- src/ui/CSimpleFrame.hpp | 1 + src/ui/CSimpleFrameScript.cpp | 43 ++++++++++++++++++++++++-- 6 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/net/connection/RealmConnection.cpp b/src/net/connection/RealmConnection.cpp index 64d5c48..345686d 100644 --- a/src/net/connection/RealmConnection.cpp +++ b/src/net/connection/RealmConnection.cpp @@ -110,7 +110,7 @@ int32_t RealmConnection::HandleAuthChallenge(AuthenticationChallenge* challenge) // TODO switch to WDataStore CDataStore msg; - uint32_t localChallenge; + uint32_t localChallenge = 0; msg.Put(static_cast(CMSG_AUTH_SESSION)); diff --git a/src/ui/CBackdropGenerator.cpp b/src/ui/CBackdropGenerator.cpp index 5be5ac0..467b472 100644 --- a/src/ui/CBackdropGenerator.cpp +++ b/src/ui/CBackdropGenerator.cpp @@ -152,6 +152,13 @@ void CBackdropGenerator::LoadXML(XMLNode* node, CStatus* status) { } } +void CBackdropGenerator::SetVertexColor(const CImVector& color) { + this->m_color = color; + if (this->m_backgroundTexture) { + this->m_backgroundTexture->SetVertexColor(color); + } +} + void CBackdropGenerator::SetBorderVertexColor(const CImVector& borderColor) { this->m_borderColor = borderColor; diff --git a/src/ui/CBackdropGenerator.hpp b/src/ui/CBackdropGenerator.hpp index 06f248e..ce9fca6 100644 --- a/src/ui/CBackdropGenerator.hpp +++ b/src/ui/CBackdropGenerator.hpp @@ -42,6 +42,7 @@ class CBackdropGenerator { CBackdropGenerator(); void Generate(const CRect* rect); void LoadXML(XMLNode* node, CStatus* status); + void SetVertexColor(const CImVector& color); void SetBorderVertexColor(const CImVector& borderColor); void SetOutput(CSimpleFrame* frame); }; diff --git a/src/ui/CSimpleFrame.cpp b/src/ui/CSimpleFrame.cpp index f0ef022..6aa167a 100644 --- a/src/ui/CSimpleFrame.cpp +++ b/src/ui/CSimpleFrame.cpp @@ -1327,7 +1327,19 @@ void CSimpleFrame::SetBeingScrolled(int32_t a2, int32_t a3) { } void CSimpleFrame::SetFrameAlpha(uint8_t alpha) { - // TODO + if (this->m_alpha == alpha) { + return; + } + + this->m_alpha = alpha; + + for (auto region = this->m_regions.Head(); region; region = this->m_regions.Link(region)->Next()) { + region->OnColorChanged(true); + } + + for (auto child = this->m_children.Head(); child; child = this->m_children.Link(child)->Next()) { + child->frame->SetFrameAlpha(alpha); + } } void CSimpleFrame::SetFrameFlag(int32_t flag, int32_t on) { diff --git a/src/ui/CSimpleFrame.hpp b/src/ui/CSimpleFrame.hpp index 02eb4ab..86fd306 100644 --- a/src/ui/CSimpleFrame.hpp +++ b/src/ui/CSimpleFrame.hpp @@ -37,6 +37,7 @@ class CSimpleFrame : public CScriptRegion { float m_depth = 0.0; FRAME_STRATA m_strata = FRAME_STRATA_MEDIUM; int32_t m_level = 0; + uint8_t m_alpha = 255; uint32_t m_eventmask = 0; int32_t m_shown = 0; int32_t m_visible = 0; diff --git a/src/ui/CSimpleFrameScript.cpp b/src/ui/CSimpleFrameScript.cpp index dd9e075..752578f 100644 --- a/src/ui/CSimpleFrameScript.cpp +++ b/src/ui/CSimpleFrameScript.cpp @@ -2,6 +2,7 @@ #include "gx/Coordinate.hpp" #include "ui/CSimpleFrame.hpp" #include "ui/FrameScript.hpp" +#include "ui/CBackdropGenerator.hpp" #include "util/Lua.hpp" #include "util/Unimplemented.hpp" #include @@ -460,7 +461,26 @@ int32_t CSimpleFrame_GetBackdropColor(lua_State* L) { } int32_t CSimpleFrame_SetBackdropColor(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + if (lua_type(L, 1) != LUA_TTABLE) { + luaL_error(L, "Attempt to find 'this' in non-table object (used '.' instead of ':' ?)"); + } + + lua_rawgeti(L, 1, 0); + auto object = reinterpret_cast(lua_touserdata(L, -1)); + lua_settop(L, -2); + + STORM_ASSERT(object); + + CImVector color; + auto red = lua_tonumber(L, 2); + auto green = lua_tonumber(L, 3); + auto blue = lua_tonumber(L, 4); + auto alpha = lua_isnumber(L, 5) ? lua_tonumber(L, 5) : 1.0; + color.Set(alpha, red, green, blue); + + if (object->m_backdrop) { + object->m_backdrop->SetVertexColor(color); + } } int32_t CSimpleFrame_GetBackdropBorderColor(lua_State* L) { @@ -468,7 +488,26 @@ int32_t CSimpleFrame_GetBackdropBorderColor(lua_State* L) { } int32_t CSimpleFrame_SetBackdropBorderColor(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + if (lua_type(L, 1) != LUA_TTABLE) { + luaL_error(L, "Attempt to find 'this' in non-table object (used '.' instead of ':' ?)"); + } + + lua_rawgeti(L, 1, 0); + auto object = reinterpret_cast(lua_touserdata(L, -1)); + lua_settop(L, -2); + + STORM_ASSERT(object); + + CImVector color; + auto red = lua_tonumber(L, 2); + auto green = lua_tonumber(L, 3); + auto blue = lua_tonumber(L, 4); + auto alpha = lua_isnumber(L, 5) ? lua_tonumber(L, 5) : 1.0; + color.Set(alpha, red, green, blue); + + if (object->m_backdrop) { + object->m_backdrop->SetBorderVertexColor(color); + } } int32_t CSimpleFrame_SetDepth(lua_State* L) { From 382d9629006344065ec634efcf7d095088926067 Mon Sep 17 00:00:00 2001 From: VDm Date: Tue, 10 Dec 2024 00:12:26 +0400 Subject: [PATCH 02/17] feat(ui): add temporary solution for CSimpleFontString_SetFormattedText --- src/ui/CSimpleEditBoxScript.cpp | 27 +++- src/ui/CSimpleFontStringScript.cpp | 175 ++++++++++++++++++++- src/ui/CSimpleFrameScript.cpp | 4 + src/ui/ScriptFunctionsGlueScriptEvents.cpp | 11 +- 4 files changed, 212 insertions(+), 5 deletions(-) diff --git a/src/ui/CSimpleEditBoxScript.cpp b/src/ui/CSimpleEditBoxScript.cpp index 7b98f94..d24ad96 100644 --- a/src/ui/CSimpleEditBoxScript.cpp +++ b/src/ui/CSimpleEditBoxScript.cpp @@ -176,11 +176,34 @@ int32_t CSimpleEditBox_GetTextInsets(lua_State* L) { } int32_t CSimpleEditBox_SetFocus(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + if (lua_type(L, 1) != LUA_TTABLE) { + luaL_error(L, "Attempt to find 'this' in non-table object (used '.' instead of ':' ?)"); + } + + lua_rawgeti(L, 1, 0); + auto object = reinterpret_cast(lua_touserdata(L, -1)); + lua_settop(L, -2); + + STORM_ASSERT(object); + + CSimpleEditBox::SetKeyboardFocus(object); + return 0; } int32_t CSimpleEditBox_ClearFocus(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + if (lua_type(L, 1) != LUA_TTABLE) { + luaL_error(L, "Attempt to find 'this' in non-table object (used '.' instead of ':' ?)"); + } + + lua_rawgeti(L, 1, 0); + auto object = reinterpret_cast(lua_touserdata(L, -1)); + lua_settop(L, -2); + + STORM_ASSERT(object); + + // TODO + // CSimpleEditBox::ClearKeyboardFocus(object); + return 0; } int32_t CSimpleEditBox_HasFocus(lua_State* L) { diff --git a/src/ui/CSimpleFontStringScript.cpp b/src/ui/CSimpleFontStringScript.cpp index f789928..489e9e5 100644 --- a/src/ui/CSimpleFontStringScript.cpp +++ b/src/ui/CSimpleFontStringScript.cpp @@ -4,6 +4,162 @@ #include "util/Lua.hpp" #include "util/Unimplemented.hpp" #include +#include + + +// TEMPORARY SOLUTION (based on lstrlib.c from LUA) +/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ +#define MAX_ITEM 512 +/* valid flags in a format specification */ +#define FLAGS "-+ #0" +/* +** maximum size of each format specification (such as '%-099.99d') +** (+10 accounts for %99.99x plus margin of error) +*/ +#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10) + +#define uchar(c) ((unsigned char)(c)) + + +static void addchar(char* b, char ch) { + auto len = strlen(b); + b[len++] = ch; + b[len] = '\0'; +} + +static void addquoted(lua_State* L, char* b, int arg) { + size_t l; + const char* s = luaL_checklstring(L, arg, &l); + addchar(b, '"'); + while (l--) { + switch (*s) { + case '"': + case '\\': + case '\n': { + addchar(b, '\\'); + addchar(b, *s); + break; + } + case '\r': { + strcat(b, "\\r"); + break; + } + case '\0': { + strcat(b, "\\000"); + break; + } + default: { + addchar(b, *s); + break; + } + } + s++; + } + addchar(b, '"'); +} + +static const char* scanformat(lua_State* L, const char* strfrmt, char* form) { + const char* p = strfrmt; + while (*p != '\0' && strchr(FLAGS, *p) != NULL) + p++; /* skip flags */ + if ((size_t)(p - strfrmt) >= sizeof(FLAGS)) + luaL_error(L, "invalid format (repeated flags)"); + if (isdigit(uchar(*p))) + p++; /* skip width */ + if (isdigit(uchar(*p))) + p++; /* (2 digits at most) */ + if (*p == '.') { + p++; + if (isdigit(uchar(*p))) + p++; /* skip precision */ + if (isdigit(uchar(*p))) + p++; /* (2 digits at most) */ + } + if (isdigit(uchar(*p))) + luaL_error(L, "invalid format (width or precision too long)"); + *(form++) = '%'; + strncpy(form, strfrmt, p - strfrmt + 1); + form += p - strfrmt + 1; + *form = '\0'; + return p; +} + +static void addintlen(char* form) { + size_t l = strlen(form); + char spec = form[l - 1]; + strcpy(form + l - 1, LUA_INTFRMLEN); + form[l + sizeof(LUA_INTFRMLEN) - 2] = spec; + form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0'; +} + +static int str_format(lua_State* L, char* b) { + int arg = 2; + size_t sfl; + const char* strfrmt = luaL_checklstring(L, arg, &sfl); + const char* strfrmt_end = strfrmt + sfl; + while (strfrmt < strfrmt_end) { + if (*strfrmt != '%') + addchar(b, *strfrmt++); + else if (*++strfrmt == '%') + addchar(b, *strfrmt++); /* %% */ + else { /* format item */ + char form[MAX_FORMAT]; /* to store the format (`%...') */ + char buff[MAX_ITEM]; /* to store the formatted item */ + arg++; + strfrmt = scanformat(L, strfrmt, form); + switch (*strfrmt++) { + case 'c': { + sprintf(buff, form, (int)luaL_checknumber(L, arg)); + break; + } + case 'd': + case 'i': { + addintlen(form); + sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); + break; + } + case 'o': + case 'u': + case 'x': + case 'X': { + addintlen(form); + sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg)); + break; + } + case 'e': + case 'E': + case 'f': + case 'g': + case 'G': { + sprintf(buff, form, (double)luaL_checknumber(L, arg)); + break; + } + case 'q': { + addquoted(L, b, arg); + continue; /* skip the 'addsize' at the end */ + } + case 's': { + size_t l; + const char* s = luaL_checklstring(L, arg, &l); + if (!strchr(form, '.') && l >= 100) { + /* no precision and string is too long to be formatted; + keep original string */ + continue; /* skip the `addsize' at the end */ + } else { + sprintf(buff, form, s); + break; + } + } + default: { /* also treat cases `pnLlh' */ + return luaL_error(L, "invalid option " LUA_QL("%%%c") " to " LUA_QL("format"), *(strfrmt - 1)); + } + } + strcat(b, buff); + } + } + return 1; +} +// END OF TEMPORARY SOLUTION int32_t CSimpleFontString_IsObjectType(lua_State* L) { WHOA_UNIMPLEMENTED(0); @@ -132,7 +288,24 @@ int32_t CSimpleFontString_SetText(lua_State* L) { } int32_t CSimpleFontString_SetFormattedText(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + if (lua_type(L, 1) != LUA_TTABLE) { + luaL_error(L, "Attempt to find 'this' in non-table object (used '.' instead of ':' ?)"); + return 0; + } + + auto type = CSimpleFontString::GetObjectType(); + auto string = static_cast(FrameScript_GetObjectThis(L, type)); + + if (!string->m_font) { + luaL_error(L, "%s:SetText(): Font not set", string->GetDisplayName()); + return 0; + } + + char b[2048] = {}; + str_format(L, b); + string->SetText(b, 0); + + return 0; } int32_t CSimpleFontString_GetTextColor(lua_State* L) { diff --git a/src/ui/CSimpleFrameScript.cpp b/src/ui/CSimpleFrameScript.cpp index 752578f..470035e 100644 --- a/src/ui/CSimpleFrameScript.cpp +++ b/src/ui/CSimpleFrameScript.cpp @@ -481,6 +481,8 @@ int32_t CSimpleFrame_SetBackdropColor(lua_State* L) { if (object->m_backdrop) { object->m_backdrop->SetVertexColor(color); } + + return 0; } int32_t CSimpleFrame_GetBackdropBorderColor(lua_State* L) { @@ -508,6 +510,8 @@ int32_t CSimpleFrame_SetBackdropBorderColor(lua_State* L) { if (object->m_backdrop) { object->m_backdrop->SetBorderVertexColor(color); } + + return 0; } int32_t CSimpleFrame_SetDepth(lua_State* L) { diff --git a/src/ui/ScriptFunctionsGlueScriptEvents.cpp b/src/ui/ScriptFunctionsGlueScriptEvents.cpp index 376524a..e97945e 100644 --- a/src/ui/ScriptFunctionsGlueScriptEvents.cpp +++ b/src/ui/ScriptFunctionsGlueScriptEvents.cpp @@ -19,7 +19,12 @@ int32_t Script_IsShiftKeyDown(lua_State* L) { } int32_t Script_GetBuildInfo(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + lua_pushstring(L, "WHOA"); + lua_pushstring(L, "Release"); + lua_pushstring(L, "3.3.5"); + lua_pushstring(L, "12340"); + lua_pushstring(L, "Jun 24 2010"); + return 5; } int32_t Script_GetLocale(lua_State* L) { @@ -27,7 +32,9 @@ int32_t Script_GetLocale(lua_State* L) { } int32_t Script_GetSavedAccountName(lua_State* L) { - WHOA_UNIMPLEMENTED(0); + // TODO + lua_pushstring(L, ""); + return 1; } int32_t Script_SetSavedAccountName(lua_State* L) { From 02e683d699fb3e2fa341880d0107ef045aaadb9e Mon Sep 17 00:00:00 2001 From: VDm Date: Tue, 10 Dec 2024 01:34:59 +0400 Subject: [PATCH 03/17] feat(script): implement FrameScript_Sprintf --- src/ui/CSimpleFontStringScript.cpp | 160 +--------------- src/ui/FrameScript.cpp | 211 +++++++++++++++++++++ src/ui/FrameScript.hpp | 2 + src/ui/ScriptFunctionsGlueScriptEvents.cpp | 7 +- 4 files changed, 221 insertions(+), 159 deletions(-) diff --git a/src/ui/CSimpleFontStringScript.cpp b/src/ui/CSimpleFontStringScript.cpp index 489e9e5..247918b 100644 --- a/src/ui/CSimpleFontStringScript.cpp +++ b/src/ui/CSimpleFontStringScript.cpp @@ -7,160 +7,6 @@ #include -// TEMPORARY SOLUTION (based on lstrlib.c from LUA) -/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ -#define MAX_ITEM 512 -/* valid flags in a format specification */ -#define FLAGS "-+ #0" -/* -** maximum size of each format specification (such as '%-099.99d') -** (+10 accounts for %99.99x plus margin of error) -*/ -#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10) - -#define uchar(c) ((unsigned char)(c)) - - -static void addchar(char* b, char ch) { - auto len = strlen(b); - b[len++] = ch; - b[len] = '\0'; -} - -static void addquoted(lua_State* L, char* b, int arg) { - size_t l; - const char* s = luaL_checklstring(L, arg, &l); - addchar(b, '"'); - while (l--) { - switch (*s) { - case '"': - case '\\': - case '\n': { - addchar(b, '\\'); - addchar(b, *s); - break; - } - case '\r': { - strcat(b, "\\r"); - break; - } - case '\0': { - strcat(b, "\\000"); - break; - } - default: { - addchar(b, *s); - break; - } - } - s++; - } - addchar(b, '"'); -} - -static const char* scanformat(lua_State* L, const char* strfrmt, char* form) { - const char* p = strfrmt; - while (*p != '\0' && strchr(FLAGS, *p) != NULL) - p++; /* skip flags */ - if ((size_t)(p - strfrmt) >= sizeof(FLAGS)) - luaL_error(L, "invalid format (repeated flags)"); - if (isdigit(uchar(*p))) - p++; /* skip width */ - if (isdigit(uchar(*p))) - p++; /* (2 digits at most) */ - if (*p == '.') { - p++; - if (isdigit(uchar(*p))) - p++; /* skip precision */ - if (isdigit(uchar(*p))) - p++; /* (2 digits at most) */ - } - if (isdigit(uchar(*p))) - luaL_error(L, "invalid format (width or precision too long)"); - *(form++) = '%'; - strncpy(form, strfrmt, p - strfrmt + 1); - form += p - strfrmt + 1; - *form = '\0'; - return p; -} - -static void addintlen(char* form) { - size_t l = strlen(form); - char spec = form[l - 1]; - strcpy(form + l - 1, LUA_INTFRMLEN); - form[l + sizeof(LUA_INTFRMLEN) - 2] = spec; - form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0'; -} - -static int str_format(lua_State* L, char* b) { - int arg = 2; - size_t sfl; - const char* strfrmt = luaL_checklstring(L, arg, &sfl); - const char* strfrmt_end = strfrmt + sfl; - while (strfrmt < strfrmt_end) { - if (*strfrmt != '%') - addchar(b, *strfrmt++); - else if (*++strfrmt == '%') - addchar(b, *strfrmt++); /* %% */ - else { /* format item */ - char form[MAX_FORMAT]; /* to store the format (`%...') */ - char buff[MAX_ITEM]; /* to store the formatted item */ - arg++; - strfrmt = scanformat(L, strfrmt, form); - switch (*strfrmt++) { - case 'c': { - sprintf(buff, form, (int)luaL_checknumber(L, arg)); - break; - } - case 'd': - case 'i': { - addintlen(form); - sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); - break; - } - case 'o': - case 'u': - case 'x': - case 'X': { - addintlen(form); - sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg)); - break; - } - case 'e': - case 'E': - case 'f': - case 'g': - case 'G': { - sprintf(buff, form, (double)luaL_checknumber(L, arg)); - break; - } - case 'q': { - addquoted(L, b, arg); - continue; /* skip the 'addsize' at the end */ - } - case 's': { - size_t l; - const char* s = luaL_checklstring(L, arg, &l); - if (!strchr(form, '.') && l >= 100) { - /* no precision and string is too long to be formatted; - keep original string */ - continue; /* skip the `addsize' at the end */ - } else { - sprintf(buff, form, s); - break; - } - } - default: { /* also treat cases `pnLlh' */ - return luaL_error(L, "invalid option " LUA_QL("%%%c") " to " LUA_QL("format"), *(strfrmt - 1)); - } - } - strcat(b, buff); - } - } - return 1; -} -// END OF TEMPORARY SOLUTION - int32_t CSimpleFontString_IsObjectType(lua_State* L) { WHOA_UNIMPLEMENTED(0); } @@ -301,9 +147,9 @@ int32_t CSimpleFontString_SetFormattedText(lua_State* L) { return 0; } - char b[2048] = {}; - str_format(L, b); - string->SetText(b, 0); + char text[2048] = {}; + FrameScript_Sprintf(L, 2, text, sizeof(text)); + string->SetText(text, 0); return 0; } diff --git a/src/ui/FrameScript.cpp b/src/ui/FrameScript.cpp index 558c517..fb51ca6 100644 --- a/src/ui/FrameScript.cpp +++ b/src/ui/FrameScript.cpp @@ -10,6 +10,7 @@ #include #include #include +#include const char* g_glueScriptEvents[41]; const char* g_scriptEvents[722]; @@ -867,6 +868,216 @@ void FrameScript_UnregisterScriptEvent(FrameScript_Object* object, FrameScript_E } } +static void addchar(char* buffer, size_t bufferSize, char ch) { + auto length = SStrLen(buffer); + if (length + 1 < bufferSize) + { + buffer[length++] = ch; + buffer[length] = '\0'; + } +} + +static void addstring(char* buffer, size_t bufferSize, const char* source) { + uint32_t dsize = 0; + uint32_t size = 0; + + dsize = SStrLen(buffer); + size = SStrLen(source); + + if (dsize + size >= bufferSize) { + size = bufferSize - dsize; + // Check for space for trailing zero + if (size < 2) { + size = 0; + } else { + size--; + } + } + + if (size > 0) + memmove(&buffer[dsize], source, size); + + buffer[dsize + size] = '\0'; +} + +static void addstring(char* buffer, size_t bufferSize, const char* source, size_t count) { + uint32_t dsize = 0; + uint32_t size = 0; + + dsize = SStrLen(buffer); + size = std::min(SStrLen(source), count); + + if (dsize + size >= bufferSize) { + size = bufferSize - dsize; + // Check for space for trailing zero + if (size < 2) { + size = 0; + } else { + size--; + } + } + + if (size > 0) + memmove(&buffer[dsize], source, size); + + buffer[dsize + size] = '\0'; +} + +static void addquoted(lua_State* L, char* buffer, size_t bufferSize, int arg) { + size_t l; + const char* s = luaL_checklstring(L, arg, &l); + addchar(buffer, bufferSize, '"'); + while (l--) { + switch (*s) { + case '"': + case '\\': + case '\n': { + addchar(buffer, bufferSize, '\\'); + addchar(buffer, bufferSize, *s); + break; + } + case '\r': { + addstring(buffer, bufferSize, "\\r"); + break; + } + case '\0': { + addstring(buffer, bufferSize, "\\000"); + break; + } + default: { + addchar(buffer, bufferSize, *s); + break; + } + } + s++; + } + addchar(buffer, bufferSize, '"'); +} + +#define FORMAT_FLAGS "-+ #0" + +static const char* scanformat(lua_State* L, const char* strfrmt, char* form) { + const char* flags = "-+ #0"; + const char* p = strfrmt; + + while (*p != '\0' && SStrChrR(FORMAT_FLAGS, *p) != NULL) { + p++; /* skip flags */ + } + + if ((size_t)(p - strfrmt) >= sizeof(FORMAT_FLAGS)) { + luaL_error(L, "invalid format (repeated flags)"); + } + + if (isdigit((unsigned char)(*p))) { + p++; /* skip width */ + } + + if (isdigit((unsigned char)(*p))) { + p++; /* (2 digits at most) */ + } + + if (*p == '.') { + p++; + if (isdigit((unsigned char)(*p))) { + p++; /* skip precision */ + } + if (isdigit((unsigned char)(*p))) { + p++; /* (2 digits at most) */ + } + } + if (isdigit((unsigned char)(*p))) { + luaL_error(L, "invalid format (width or precision too long)"); + } + + *(form++) = '%'; + strncpy(form, strfrmt, p - strfrmt + 1); + form += p - strfrmt + 1; + *form = '\0'; + return p; +} + +static void addintlen(char* form) { + size_t l = SStrLen(form); + char spec = form[l - 1]; + strcpy(form + l - 1, LUA_INTFRMLEN); + form[l + sizeof(LUA_INTFRMLEN) - 2] = spec; + form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0'; +} + +void FrameScript_Sprintf(lua_State* L, int startIndex, char* buffer, uint32_t bufferSize) { + // maximum size of each formatted item (> len(format('%99.99f', -1e308))) + const size_t MAX_ITEM = 512; + + // maximum size of each format specification (such as '%-099.99d') + // (+10 accounts for %99.99x plus margin of error) + const size_t MAX_FORMAT = sizeof(FORMAT_FLAGS) + sizeof(LUA_INTFRMLEN) + 10; + + int arg = startIndex; + size_t sfl; + const char* strfrmt = luaL_checklstring(L, arg, &sfl); + const char* strfrmt_end = strfrmt + sfl; + while (strfrmt < strfrmt_end) { + if (*strfrmt != '%') { + addchar(buffer, bufferSize, *strfrmt++); + } else if (*++strfrmt == '%') { + addchar(buffer, bufferSize, *strfrmt++); /* %% */ + } else { /* format item */ + char form[MAX_FORMAT]; /* to store the format (`%...') */ + char buff[MAX_ITEM]; /* to store the formatted item */ + arg++; + strfrmt = scanformat(L, strfrmt, form); + switch (*strfrmt++) { + case 'c': { + sprintf(buff, form, (int)luaL_checknumber(L, arg)); + break; + } + case 'd': + case 'i': { + addintlen(form); + sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); + break; + } + case 'o': + case 'u': + case 'x': + case 'X': { + addintlen(form); + sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg)); + break; + } + case 'e': + case 'E': + case 'f': + case 'g': + case 'G': { + sprintf(buff, form, (double)luaL_checknumber(L, arg)); + break; + } + case 'q': { + addquoted(L, buffer, bufferSize, arg); + continue; /* skip the 'addsize' at the end */ + } + case 's': { + size_t l; + const char* s = luaL_checklstring(L, arg, &l); + if (!strchr(form, '.') && l >= 100) { + /* no precision and string is too long to be formatted; + keep original string */ + continue; /* skip the `addsize' at the end */ + } else { + sprintf(buff, form, s); + break; + } + } + default: { /* also treat cases `pnLlh' */ + luaL_error(L, "invalid option " LUA_QL("%%%c") " to " LUA_QL("format"), *(strfrmt - 1)); + } + } + addstring(buffer, bufferSize, buff); + } + } +} + void GlueScriptEventsInitialize() { g_glueScriptEvents[0] = "SET_GLUE_SCREEN"; g_glueScriptEvents[1] = "START_GLUE_MUSIC"; diff --git a/src/ui/FrameScript.hpp b/src/ui/FrameScript.hpp index debf40f..6221927 100644 --- a/src/ui/FrameScript.hpp +++ b/src/ui/FrameScript.hpp @@ -100,6 +100,8 @@ void FrameScript_SignalEvent(uint32_t index, const char* format, ...); void FrameScript_UnregisterScriptEvent(FrameScript_Object* object, FrameScript_EventObject* event); +void FrameScript_Sprintf(lua_State* L, int startIndex, char* buffer, uint32_t bufferSize); + void GlueScriptEventsInitialize(); void ScriptEventsInitialize(); diff --git a/src/ui/ScriptFunctionsGlueScriptEvents.cpp b/src/ui/ScriptFunctionsGlueScriptEvents.cpp index e97945e..e34011a 100644 --- a/src/ui/ScriptFunctionsGlueScriptEvents.cpp +++ b/src/ui/ScriptFunctionsGlueScriptEvents.cpp @@ -19,8 +19,11 @@ int32_t Script_IsShiftKeyDown(lua_State* L) { } int32_t Script_GetBuildInfo(lua_State* L) { - lua_pushstring(L, "WHOA"); - lua_pushstring(L, "Release"); + auto szVersion = FrameScript_GetText("VERSION", -1, GENDER_NOT_APPLICABLE); + auto szVersionType = FrameScript_GetText("RELEASE_BUILD", -1, GENDER_NOT_APPLICABLE); + + lua_pushstring(L, szVersion); + lua_pushstring(L, szVersionType); lua_pushstring(L, "3.3.5"); lua_pushstring(L, "12340"); lua_pushstring(L, "Jun 24 2010"); From 6f108b306741208eb95d8a0584766257afc9f905 Mon Sep 17 00:00:00 2001 From: VDm Date: Tue, 10 Dec 2024 20:37:18 +0400 Subject: [PATCH 04/17] chore(ui): add TAB button handler for CSimpleEditBox --- src/ui/CSimpleEditBox.cpp | 11 +++++++++++ src/ui/CSimpleEditBox.hpp | 1 + 2 files changed, 12 insertions(+) diff --git a/src/ui/CSimpleEditBox.cpp b/src/ui/CSimpleEditBox.cpp index 6945ce9..5f0b9ae 100644 --- a/src/ui/CSimpleEditBox.cpp +++ b/src/ui/CSimpleEditBox.cpp @@ -871,6 +871,11 @@ int32_t CSimpleEditBox::OnLayerKeyDown(const CKeyEvent& evt) { return 1; } + case KEY_TAB: { + // TODO correct implementation + this->RunOnTabPressedScript(); + } + // TODO // - remaining keys @@ -994,6 +999,12 @@ void CSimpleEditBox::RunOnEnterPressedScript() { } } +void CSimpleEditBox::RunOnTabPressedScript() { + if (this->m_onTabPressed.luaRef) { + this->RunScript(this->m_onTabPressed, 0, 0); + } +} + void CSimpleEditBox::RunOnTextChangedScript(int32_t changed) { if (this->m_onTextChanged.luaRef) { auto L = FrameScript_GetContext(); diff --git a/src/ui/CSimpleEditBox.hpp b/src/ui/CSimpleEditBox.hpp index 5fb7e26..2c264d8 100644 --- a/src/ui/CSimpleEditBox.hpp +++ b/src/ui/CSimpleEditBox.hpp @@ -98,6 +98,7 @@ class CSimpleEditBox : public CSimpleFrame, CSimpleFontedFrame { void RunOnEditFocusGainedScript(); void RunOnEditFocusLostScript(); void RunOnEnterPressedScript(); + void RunOnTabPressedScript(); void RunOnTextChangedScript(int32_t changed); void SetCursorPosition(int32_t position); void SetHistoryLines(int32_t a2); From 5fb8536fced775515ac0387a6860088a11c2f011 Mon Sep 17 00:00:00 2001 From: VDm Date: Wed, 11 Dec 2024 00:20:01 +0400 Subject: [PATCH 05/17] fix(d3d): fix hardware cursor --- lib/squall | 2 +- src/console/Device.cpp | 1 + src/gx/d3d/CGxDeviceD3d.cpp | 16 +++++++++++++--- src/gx/d3d/CGxDeviceD3d.hpp | 1 + 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/squall b/lib/squall index 9722c1e..22e9686 160000 --- a/lib/squall +++ b/lib/squall @@ -1 +1 @@ -Subproject commit 9722c1e8c35e14e57f9a4f1e45806d8237df957b +Subproject commit 22e9686d64db308a9b480ecf6e1fba43c2563315 diff --git a/src/console/Device.cpp b/src/console/Device.cpp index 5a1df3b..2d87392 100644 --- a/src/console/Device.cpp +++ b/src/console/Device.cpp @@ -274,6 +274,7 @@ void ConsoleDeviceInitialize(const char* title) { s_requestedFormat.hwTnL = true; // TODO + s_requestedFormat.hwCursor = true; CGxFormat format; memcpy(&format, &s_requestedFormat, sizeof(s_requestedFormat)); diff --git a/src/gx/d3d/CGxDeviceD3d.cpp b/src/gx/d3d/CGxDeviceD3d.cpp index b60fc97..eeaa104 100644 --- a/src/gx/d3d/CGxDeviceD3d.cpp +++ b/src/gx/d3d/CGxDeviceD3d.cpp @@ -334,11 +334,17 @@ LRESULT CGxDeviceD3d::WindowProcD3d(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM case WM_SETCURSOR: { if (device) { - if (device->m_d3dDevice && lParam == 1) { + if (device->m_d3dDevice && LOWORD(lParam) == HTCLIENT) { SetCursor(nullptr); BOOL show = device->m_cursorVisible && device->m_hardwareCursor ? TRUE : FALSE; device->m_d3dDevice->ShowCursor(show); + } else { + // Uncomment when the "glove" cursor will be fixed + //break; } + } else { + // Uncomment when the "glove" cursor will be fixed + //break; } return 1; @@ -488,8 +494,7 @@ int32_t CGxDeviceD3d::DeviceSetFormat(const CGxFormat& format) { if (this->ICreateWindow(createFormat) && this->ICreateD3dDevice(createFormat) && this->CGxDevice::DeviceSetFormat(format)) { this->intF64 = 1; - - // TODO + this->m_hwCursorNeedsUpdate = 1; if (this->m_format.window == 0) { RECT windowRect; @@ -1249,6 +1254,11 @@ void CGxDeviceD3d::CursorSetVisible(int32_t visible) { } } +void CGxDeviceD3d::CursorUnlock(uint32_t x, uint32_t y) { + CGxDevice::CursorUnlock(x, y); + this->m_hwCursorNeedsUpdate = 1; +} + void CGxDeviceD3d::ICursorDraw() { if (!this->m_hardwareCursor) { this->ISceneBegin(); diff --git a/src/gx/d3d/CGxDeviceD3d.hpp b/src/gx/d3d/CGxDeviceD3d.hpp index 16c9f3e..ceeee09 100644 --- a/src/gx/d3d/CGxDeviceD3d.hpp +++ b/src/gx/d3d/CGxDeviceD3d.hpp @@ -252,6 +252,7 @@ class CGxDeviceD3d : public CGxDevice { virtual void ICursorDestroy(); virtual void ICursorDraw(); virtual void CursorSetVisible(int32_t visible); + virtual void CursorUnlock(uint32_t x, uint32_t y); virtual int32_t DeviceCreate(int32_t (*windowProc)(void* window, uint32_t message, uintptr_t wparam, intptr_t lparam), const CGxFormat& format); virtual int32_t DeviceSetFormat(const CGxFormat& format); virtual void* DeviceWindow(); From 6191acb291c14abd6eb0ca20fe698ebf61ec26ab Mon Sep 17 00:00:00 2001 From: VDm Date: Thu, 12 Dec 2024 01:53:04 +0400 Subject: [PATCH 06/17] feat(d3d): add GxRs_Lighting case for CGxDeviceD3d::IRsSendToHw --- src/gx/d3d/CGxDeviceD3d.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/gx/d3d/CGxDeviceD3d.cpp b/src/gx/d3d/CGxDeviceD3d.cpp index eeaa104..12c621c 100644 --- a/src/gx/d3d/CGxDeviceD3d.cpp +++ b/src/gx/d3d/CGxDeviceD3d.cpp @@ -1107,6 +1107,17 @@ void CGxDeviceD3d::IRsSendToHw(EGxRenderState which) { break; } + case GxRs_Lighting: { + int32_t lightingEnable = 0; + + if (this->MasterEnable(GxMasterEnable_Lighting)) { + lightingEnable = static_cast(state->m_value); + } + + this->m_d3dDevice->SetRenderState(D3DRS_LIGHTING, lightingEnable); + break; + } + case GxRs_DepthTest: case GxRs_DepthFunc: { auto depthTest = static_cast((&this->m_appRenderStates[GxRs_DepthTest])->m_value); From f22485952b742f185a057b17088b3df4a9aba443 Mon Sep 17 00:00:00 2001 From: VDm Date: Sat, 14 Dec 2024 18:17:55 +0400 Subject: [PATCH 07/17] chore(gx): store software cursor workaround --- src/gx/CGxDevice.cpp | 5 ++++- src/gx/glsdl/GLSDLDevice.cpp | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/gx/CGxDevice.cpp b/src/gx/CGxDevice.cpp index 8e07f12..32f888c 100644 --- a/src/gx/CGxDevice.cpp +++ b/src/gx/CGxDevice.cpp @@ -333,7 +333,7 @@ void CGxDevice::ICursorDraw() { // Turn off everything GxRsSet(GxRs_PolygonOffset, 0); GxRsSet(GxRs_NormalizeNormals, 0); - GxRsSet(GxRs_BlendingMode, 1); + GxRsSet(GxRs_BlendingMode, GxBlend_AlphaKey); GxRsSetAlphaRef(); GxRsSet(GxRs_Lighting, 0); GxRsSet(GxRs_Fog, 0); @@ -355,6 +355,9 @@ void CGxDevice::ICursorDraw() { float cursorDepth = 1.0f; C44Matrix projection; + // Workaround for software cursor + // C44Matrix projection(2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0.002, 0, -1, -1, 0, 1); + // this->XformSetProjection(projection); if (!this->StereoEnabled() || (CGxDevice::s_uiVertexShader == 0 || !s_uiVertexShader->Valid()) || diff --git a/src/gx/glsdl/GLSDLDevice.cpp b/src/gx/glsdl/GLSDLDevice.cpp index 81bfa15..6e67dac 100644 --- a/src/gx/glsdl/GLSDLDevice.cpp +++ b/src/gx/glsdl/GLSDLDevice.cpp @@ -1578,7 +1578,7 @@ GLTexture* GLSDLDevice::CreateTextureCubeMap(uint32_t size, uint32_t numMipMap, } void GLSDLDevice::Draw(GLEnum primitive, uint32_t a3, uint32_t a4) { - // TODO + this->GLSDLDraw(primitive, a3, a3 + a4, 0, 0, 0); } void GLSDLDevice::DrawIndexed(GLEnum primitive, uint32_t a3, uint32_t a4, uint32_t a5, uint32_t a6, uint32_t count) { From 9df49cd3f7da8f069151adb7db4927ea792c19a0 Mon Sep 17 00:00:00 2001 From: VDm Date: Sun, 15 Dec 2024 16:29:00 +0400 Subject: [PATCH 08/17] fix(input): convert local code-page characters to Unicode ones --- src/event/win/Input.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/event/win/Input.cpp b/src/event/win/Input.cpp index 687892b..a08b241 100644 --- a/src/event/win/Input.cpp +++ b/src/event/win/Input.cpp @@ -539,7 +539,10 @@ int32_t OsWindowProc(void* window, uint32_t message, uintptr_t wparam, intptr_t uint32_t character = wparam; if (wparam >= 128) { - // TODO + // Workaround + wchar_t u16_character = 0; + ::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, reinterpret_cast(&wparam), 1, &u16_character, 1); + character = u16_character; } OsQueuePut(OS_INPUT_CHAR, character, LOWORD(lparam), 0, 0); From eeab0356646a3d68086c6fcb4ce956f9f59d3df0 Mon Sep 17 00:00:00 2001 From: VDm Date: Sat, 1 Mar 2025 21:53:08 +0400 Subject: [PATCH 09/17] fix(console): use SUniSPutUTF8 in OnChar handler --- src/console/Handlers.cpp | 9 ++---- src/glue/CGlueMgr.cpp | 66 +++++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/console/Handlers.cpp b/src/console/Handlers.cpp index 6bf03d5..b5e30d9 100644 --- a/src/console/Handlers.cpp +++ b/src/console/Handlers.cpp @@ -4,6 +4,7 @@ #include "console/Command.hpp" #include "console/Screen.hpp" #include "event/Event.hpp" +#include "storm/Unicode.hpp" #include static int32_t s_historyIndex = 0; @@ -11,24 +12,20 @@ static int32_t s_historyIndex = 0; namespace { int32_t OnChar(const EVENT_DATA_CHAR* data, void* param) { - char character[2]; + char character[8] = {}; if (ConsoleAccessGetEnabled() && EventIsKeyDown(ConsoleGetHotKey())) { return 0; } if (ConsoleGetActive()) { - character[0] = char(data->ch); - character[1] = 0; + SUniSPutUTF8(data->ch, character); PasteInInputLine(character); ResetHighlight(); return 0; } - // SUniSPutUTF8(data->ch, character); - - return 1; } diff --git a/src/glue/CGlueMgr.cpp b/src/glue/CGlueMgr.cpp index 1061cde..93a6bd3 100644 --- a/src/glue/CGlueMgr.cpp +++ b/src/glue/CGlueMgr.cpp @@ -366,46 +366,48 @@ void CGlueMgr::PollAccountLogin(int32_t errorCode, const char* msg, int32_t comp FrameScript_SignalEvent(4, "%s", msg); } - if (complete) { - if (result == 0) { - if (errorCode != 2) { + if (!complete) { + return; + } + + if (result == 0) { + if (errorCode != 2) { + // TODO + } + + CGlueMgr::m_idleState = IDLE_NONE; + CGlueMgr::m_showedDisconnect = 0; + + if (errorCode == 2) { + // TODO CGlueMgr::m_disconnectPending = 1; + // TODO ClientServices::Connection()->Disconnect(); + } + + if (errorCode != 13) { + // TODO CCharacterSelection::ClearCharacterList(); + + if (ClientServices::GetInstance()->m_realmList.Count()) { + FrameScript_SignalEvent(5, nullptr); + CRealmList::UpdateList(); + } else { // TODO } - CGlueMgr::m_idleState = IDLE_NONE; - CGlueMgr::m_showedDisconnect = 0; - - if (errorCode == 2) { - // TODO CGlueMgr::m_disconnectPending = 1; - // TODO ClientServices::Connection()->Disconnect(); - } - - if (errorCode != 13) { - // TODO CCharacterSelection::ClearCharacterList(); - - if (ClientServices::GetInstance()->m_realmList.Count()) { - FrameScript_SignalEvent(5, nullptr); - CRealmList::UpdateList(); - } else { - // TODO - } - - return; - } - - if (!SStrCmpI(CGlueMgr::m_currentScreen, "charselect", STORM_MAX_STR)) { - CGlueMgr::SetScreen("login"); - return; - } - return; } - if (op == COP_CONNECT) { - // TODO - + if (!SStrCmpI(CGlueMgr::m_currentScreen, "charselect", STORM_MAX_STR)) { + CGlueMgr::SetScreen("login"); return; } + + return; + } + + if (op == COP_CONNECT) { + // TODO + + return; } } From 86153ccf53251a6391c7d10290ee750c264c34b6 Mon Sep 17 00:00:00 2001 From: VDm Date: Sat, 1 Mar 2025 23:41:31 +0400 Subject: [PATCH 10/17] feat(client): implement locale related calls --- src/client/Client.cpp | 111 +++++++++++++++++++++++++--------- src/client/Client.hpp | 3 +- src/client/ClientServices.cpp | 5 ++ src/client/ClientServices.hpp | 1 + src/console/CVar.hpp | 2 +- 5 files changed, 90 insertions(+), 32 deletions(-) diff --git a/src/client/Client.cpp b/src/client/Client.cpp index a8d90e8..f71aa63 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -24,6 +24,7 @@ CVar* Client::g_accountListVar; HEVENTCONTEXT Client::g_clientEventContext; +char Client::g_currentLocaleName[5] = {}; void AsyncFileInitialize() { // TODO @@ -158,12 +159,17 @@ void SetPaths() { OsSetCurrentDirectory(datadir); } +bool LocaleChangedCallback(CVar*, const char*, const char* value, void*) { + SStrCopy(Client::g_currentLocaleName, value, sizeof(Client::g_currentLocaleName)); + return true; +} + int32_t InitializeGlobal() { - // TODO - ProcessCommandLine(); + SetPaths(); - // sub_403600("WoW.mfil"); + // TODO: + // WowConfigureFileSystem::ReadBuildKeyFromFile("WoW.mfil"); // if (dword_B2FA10 != 2) { // sub_403560(); @@ -175,42 +181,70 @@ int32_t InitializeGlobal() { // LOBYTE(v24) = OsDirectoryExists((int)"WTF/Account") == 0; // } - // ClientServices::LoadCDKey(); - - SetPaths(); - - OpenArchives(); + ClientServices::LoadCDKey(); ConsoleInitializeClientCommand(); - ConsoleInitializeClientCVar("Config.wtf"); - - // TODO - // replace enUS with detected locale - ClientServices::InitLoginServerCVars(1, "enUS"); - - // sub_7663F0(); + // TODO: CVar::ArchiveCodeRegisteredOnly(); // v18 = 0; // v19 = 0; // ptr = 0; // v21 = 0; - // sub_406740(&v18, &CVar::Load); + // ::ForEveryRunOnceWTF::Execute(&v18, &CVar::Load); // if (ptr) { // SMemFree(ptr, a_pad, -2, 0); // } - // CVar::Register("dbCompress", "Database compression", 0, "-1", 0, 5, 0, 0, 0); + CVar::Register( + "dbCompress", + "Database compression", + 0, + "-1", + nullptr, + CATEGORY::DEFAULT, + false, + nullptr, + false + ); - // v2 = CVar::Register("locale", "Set the game locale", 0, "****", &LocaleChangedCallback, 5, 0, 0, 0); + CVar* locale = CVar::Register( + "locale", + "Set the game locale", + 0, + "****", + &LocaleChangedCallback, + CATEGORY::DEFAULT, + false, + nullptr, + false + ); - // if (!SStrCmp(v2->m_stringValue.m_str, "****", 0x7FFFFFFFu)) { - // CVar::Set(v2, "enUS", 1, 0, 0, 1); - // } + if (!SStrCmp(locale->GetString(), "****", STORM_MAX_STR)) { + locale->Set("enUS", true, false, false, true); + } + + CVar::Register( + "useEnglishAudio", + "override the locale and use English audio", + 0, + "0", + nullptr, + CATEGORY::DEFAULT, + false, + nullptr, + false + ); + + + OpenArchives(); + + // TODO + // replace enUS with detected locale + ClientServices::InitLoginServerCVars(1, locale->GetString()); - // CVar::Register("useEnglishAudio", "override the locale and use English audio", 0, "0", 0, 5, 0, 0, 0); // if (sub_422140()) { // sub_4036B0(v24, 0, a2, (int)v2, (char)v24); @@ -343,19 +377,36 @@ int32_t InitializeGlobal() { void CommonMain() { StormInitialize(); - // TODO - // - error log setup - // - misc other setup + // TODO: + // SErrCatchUnhandledExceptions(); + // OsSystemInitialize("Blizzard Entertainment World of Warcraft", 0); + // int option = 1; + // StormSetOption(10, &option, sizeof(option)); + // StormSetOption(11, &option, sizeof(option)); + // OsSystemEnableCpuLog(); + + // SetPaths() moved into InitializeGlobal() + + // int sendErrorLogs = 1; + // if (!SRegLoadValue("World of Warcraft\\Client", "SendErrorLogs", 0, &sendErrorLogs)) { + // sendErrorLogs = 1; + // SRegSaveValue("World of Warcraft\\Client", "SendErrorLogs", 0, sendErrorLogs); + // } + + // SErrSetLogTitleString("World of WarCraft (build 12340)"); + // SErrSetLogTitleCallback(WowLogHeader); + // if (sendErrorLogs) { + // SErrRegisterHandler(SendErrorLog); + // } if (InitializeGlobal()) { EventDoMessageLoop(); - - // TODO - // sub_406B70(); + // TODO: DestroyGlobal(); } - // TODO - // - misc cleanup + // TODO: + // StormDestroy(); + // Misc Cleanup } void BlizzardAssertCallback(const char* a1, const char* a2, const char* a3, uint32_t a4) { diff --git a/src/client/Client.hpp b/src/client/Client.hpp index 7d3f60b..4983fb0 100644 --- a/src/client/Client.hpp +++ b/src/client/Client.hpp @@ -9,7 +9,8 @@ class CVar; namespace Client { extern CVar* g_accountListVar; extern HEVENTCONTEXT g_clientEventContext; -} + extern char g_currentLocaleName[5]; + } void ClientPostClose(int32_t a1); diff --git a/src/client/ClientServices.cpp b/src/client/ClientServices.cpp index fc55a16..dead8bb 100644 --- a/src/client/ClientServices.cpp +++ b/src/client/ClientServices.cpp @@ -264,6 +264,11 @@ const char* ClientServices::GetDefaultPatchListString() { return "public-test.patch.battle.net:1119/patch"; } +bool ClientServices::LoadCDKey() { + // TODO + return true; +} + void ClientServices::InitLoginServerCVars(int32_t overwrite, const char* locale) { if ((ClientServices::s_realmListBNVar == nullptr || ClientServices::s_realmListVar == nullptr) || overwrite != 0 ) { ClientServices::s_decorateAccountName = CVar::Register( diff --git a/src/client/ClientServices.hpp b/src/client/ClientServices.hpp index e203cc7..763c563 100644 --- a/src/client/ClientServices.hpp +++ b/src/client/ClientServices.hpp @@ -45,6 +45,7 @@ class ClientServices : public LoginResponse { static void InitLoginServerCVars(int32_t overwrite, const char* locale); static const char* GetDefaultRealmlistString(); static const char* GetDefaultPatchListString(); + static bool LoadCDKey(); // Virtual member functions virtual int32_t GetLoginServerType(); diff --git a/src/console/CVar.hpp b/src/console/CVar.hpp index f1e5451..948a7d7 100644 --- a/src/console/CVar.hpp +++ b/src/console/CVar.hpp @@ -37,7 +37,7 @@ class CVar : public TSHashObject { int32_t GetInt(); const char* GetString(void); void InternalSet(const char*, bool, bool, bool, bool); - bool Set(const char*, bool, bool, bool, bool); + bool Set(const char* value, bool setValue, bool setReset, bool setDefault, bool a6); bool Reset(); bool Default(); int32_t Update(); From 745dfcc1297c2d853343b11e38d2c4c26f05c68b Mon Sep 17 00:00:00 2001 From: VDm Date: Sun, 2 Mar 2025 01:42:32 +0400 Subject: [PATCH 11/17] feat(client): add CheckAvailableLocales method --- src/client/Client.cpp | 148 ++++++++++++++++++++++++++++++++++++++---- src/client/Client.hpp | 2 +- 2 files changed, 138 insertions(+), 12 deletions(-) diff --git a/src/client/Client.cpp b/src/client/Client.cpp index f71aa63..4dcee3e 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -21,11 +21,21 @@ #include #include #include +#include CVar* Client::g_accountListVar; HEVENTCONTEXT Client::g_clientEventContext; char Client::g_currentLocaleName[5] = {}; + +static uint8_t s_expansionLevel = 0; +static bool g_hasIsoLocale[12] = {}; +static char* s_localeArray[12] = { + "deDE", "enGB", "enUS", "esES", "frFR", "koKR", + "zhCN", "zhTW", "enCN", "enTW", "esMX", "ruRU" +}; + + void AsyncFileInitialize() { // TODO AsyncFileReadInitialize(0, 100); @@ -140,6 +150,32 @@ int32_t InitializeEngineCallback(const void* a1, void* a2) { return 1; } +uint8_t GetExpansionLevel() { + return s_expansionLevel; +} + +const char* UpdateInstallLocation() { + // TODO + return nullptr; +} + +bool UpdateInstallLocationForName(int32_t a1, size_t size, const char* filename, char* buffer, const char* locale) { + if (a1 == 2) { + auto location = UpdateInstallLocation(); + if (!location) { + return false; + } + SStrPrintf(buffer, size, "%s%s%s", location, "Data\\", filename); + } else { + SStrPrintf(buffer, size, "%s%s", "Data\\", filename); + } + for (auto i = SStrStr(buffer, "****"); i; i = SStrStr(buffer, "****")) { + size_t offset = static_cast(i - buffer); + memcpy(&buffer[offset], locale, 4); + } + return true; +} + void SetPaths() { // SFile::DisableSFileCheckDisk(); // SFile::EnableDirectAccess(0); @@ -159,6 +195,98 @@ void SetPaths() { OsSetCurrentDirectory(datadir); } +bool IsCommonMpqExists() { + char path1[1024]; + SStrPrintf(path1, sizeof(path1), "%s%s", "Data\\", "common.MPQ"); + for (auto i = SStrStr(path1, "****"); i; i = SStrStr(path1, "****")) { + size_t offset = static_cast(i - path1); + memcpy(&path1[offset], "----", 4); + } + + char path2[1024]; + SStrPrintf(path2, sizeof(path2), "%s%s", "..\\Data\\", "common.MPQ"); + for (auto i = SStrStr(path2, "****"); i; i = SStrStr(path2, "****")) { + size_t offset = static_cast(i - path2); + memcpy(&path2[offset], "----", 4); + } + + auto location = UpdateInstallLocation(); + if (location) { + char path3[1024]; + SStrPrintf(path3, sizeof(path3), "%s%s%s", location, "Data\\", "common.MPQ"); + for (auto i = SStrStr(path3, "****"); i; i = SStrStr(path3, "****")) { + size_t offset = static_cast(i - path3); + memcpy(&path3[offset], "----", 4); + } + + if (!Blizzard::File::Exists(path1) && !Blizzard::File::Exists(path2)) { + return Blizzard::File::Exists(path3); + } + } else if (!Blizzard::File::Exists(path1)) { + return Blizzard::File::Exists(path2); + } + + return true; +} + +size_t GetLocaleIndex(const char* locale) { + for (size_t i = 0; i < 12; ++i) { + if (SStrCmpI(locale, s_localeArray[i], 4) == 0) { + return i; + } + } + return 2; // s_localeArray[2] == "enUS" +} + +void CheckAvailableLocales(char* locale) { + if (!IsCommonMpqExists()) { + return; + } + + for (size_t localeIndex = 0; localeIndex < 12; ++localeIndex) { + g_hasIsoLocale[localeIndex] = false; + + const char* filename = "****\\locale-****.MPQ"; + + char path[1024]; + SStrPrintf(path, sizeof(path), "%s%s", "Data\\", filename); + for (auto i = SStrStr(path, "****"); i; i = SStrStr(path, "****")) { + size_t offset = static_cast(i - path); + memcpy(&path[offset], s_localeArray[localeIndex], 4); + } + + if (Blizzard::File::Exists(path)) { + g_hasIsoLocale[localeIndex] = true; + continue; + } + + SStrPrintf(path, sizeof(path), "%s%s", "..\\Data\\", filename); + for (auto i = SStrStr(path, "****"); i; i = SStrStr(path, "****")) { + size_t offset = static_cast(i - path); + memcpy(&path[offset], s_localeArray[localeIndex], 4); + } + + if (Blizzard::File::Exists(path)) { + g_hasIsoLocale[localeIndex] = true; + continue; + } + + if (UpdateInstallLocationForName(2, sizeof(path), filename, path, s_localeArray[localeIndex]) && + Blizzard::File::Exists(path)) { + g_hasIsoLocale[localeIndex] = true; + } + } + + size_t localeIndex = GetLocaleIndex(locale); + for (size_t i = 0; i < 12; ++i) { + if (g_hasIsoLocale[localeIndex]) { + break; + } + localeIndex = (localeIndex + 1) % 12; + } + SStrCopy(locale, s_localeArray[localeIndex], STORM_MAX_STR); +} + bool LocaleChangedCallback(CVar*, const char*, const char* value, void*) { SStrCopy(Client::g_currentLocaleName, value, sizeof(Client::g_currentLocaleName)); return true; @@ -238,23 +366,21 @@ int32_t InitializeGlobal() { false ); - - OpenArchives(); - - // TODO - // replace enUS with detected locale - ClientServices::InitLoginServerCVars(1, locale->GetString()); - - + // TODO: SFile::IsTrial() check // if (sub_422140()) { // sub_4036B0(v24, 0, a2, (int)v2, (char)v24); // } - // SStrCopy(&a1a, v2->m_stringValue.m_str, 5); + char existingLocale[5] = {}; + SStrCopy(existingLocale, locale->GetString(), sizeof(existingLocale)); + CheckAvailableLocales(existingLocale); + locale->Set(existingLocale, true, false, false, true); - // sub_402D50(&a1a); - // CVar::Set(v2, &a1a, 1, 0, 0, 1); + OpenArchives(); + + // TODO: This method should be placed inside OpenArchives + ClientServices::InitLoginServerCVars(1, locale->GetString()); // SStrPrintf(dest, 260, "%s%s", *(_DWORD *)off_AB6158, v2->m_stringValue.m_str); diff --git a/src/client/Client.hpp b/src/client/Client.hpp index 4983fb0..ca6ea88 100644 --- a/src/client/Client.hpp +++ b/src/client/Client.hpp @@ -10,7 +10,7 @@ namespace Client { extern CVar* g_accountListVar; extern HEVENTCONTEXT g_clientEventContext; extern char g_currentLocaleName[5]; - } +} void ClientPostClose(int32_t a1); From 3993e9bde0598680c8405119e0d4dd9c00f25780 Mon Sep 17 00:00:00 2001 From: VDm Date: Sun, 9 Mar 2025 22:17:51 +0400 Subject: [PATCH 12/17] feat(client): add PatchFiles class --- src/client/Client.cpp | 11 ++++---- src/client/Client.hpp | 2 ++ src/client/Patch.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++ src/client/Patch.hpp | 33 ++++++++++++++++++++++ src/util/SFile.cpp | 19 +++++++++++-- src/util/SFile.hpp | 2 ++ 6 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 src/client/Patch.cpp create mode 100644 src/client/Patch.hpp diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 4dcee3e..aefcb3f 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -376,18 +376,17 @@ int32_t InitializeGlobal() { CheckAvailableLocales(existingLocale); locale->Set(existingLocale, true, false, false, true); + char path[STORM_MAX_PATH]; + SStrPrintf(path, sizeof(path), "%s%s", "Data\\", locale->GetString()); + SFile::SetDataPathAlternate(path); + SFile::RebuildHash(); + OpenArchives(); // TODO: This method should be placed inside OpenArchives ClientServices::InitLoginServerCVars(1, locale->GetString()); - // SStrPrintf(dest, 260, "%s%s", *(_DWORD *)off_AB6158, v2->m_stringValue.m_str); - - // sub_421B50(dest); - - // sub_423D70(); - // sub_405DD0(); // CVar* v3 = CVar::Register( diff --git a/src/client/Client.hpp b/src/client/Client.hpp index ca6ea88..3ad8056 100644 --- a/src/client/Client.hpp +++ b/src/client/Client.hpp @@ -14,6 +14,8 @@ namespace Client { void ClientPostClose(int32_t a1); +const char* UpdateInstallLocation(); + void CommonMain(); void StormInitialize(); diff --git a/src/client/Patch.cpp b/src/client/Patch.cpp new file mode 100644 index 0000000..0e63151 --- /dev/null +++ b/src/client/Patch.cpp @@ -0,0 +1,65 @@ +#include "client/Patch.hpp" + +#include + +#include "client/Client.hpp" + + +PatchFiles::PatchFiles() { + this->path = nullptr; +} + +void PatchFiles::SearchArchives(const char* locale, int32_t a3) { + char fullPath[256] = {}; + char pattern[256] = {}; + char path[256] = {}; + + const size_t listSize = 12; + + ListEntry list[listSize] = { + { 0, 0, false, "Data\\", "patch-?.MPQ" }, + { 0, 0, false, "Data\\%s\\", "patch-%s-?.MPQ" }, + { 1, 0, false, "Data\\", "patch.MPQ" }, + { 1, 0, false, "Data\\%s\\", "patch-%s.MPQ" }, + { 1, 1, false, "Data\\", "patch-4.MPQ" }, + { 1, 1, false, "Data\\%s\\", "patch-%s-4.MPQ" }, + { 1, 1, false, "Data\\", "patch-3.MPQ" }, + { 1, 1, false, "Data\\%s\\", "patch-%s-3.MPQ" }, + { 1, 1, false, "..\\Data\\", "patch-2.MPQ" }, + { 1, 1, false, "..\\Data\\%s\\", "patch-%s-2.MPQ" }, + { 1, 1, false, "..\\Data\\", "patch.MPQ" }, + { 1, 1, false, "..\\Data\\%s\\", "patch-%s.MPQ" }, + }; + + const char* installRoot = UpdateInstallLocation(); + + // Warning: assigning pointer to a temporary variable + // Need this for OsFileList's callback + this->path = path; + + for (size_t i = 0; i <= 2; ++i) { + for (size_t j = 0; j < listSize; ++j) { + auto entry = &list[j]; + if (entry->num1 != i || entry->num2 != (a3 != 0)) { + continue; + } + + SStrPrintf(path, sizeof(path), entry->dataDir, locale); + SStrPrintf(pattern, sizeof(pattern), entry->pattern, locale); + if (entry->absolute && installRoot && installRoot[0]) { + SStrPrintf(fullPath, sizeof(fullPath), "%s%s", installRoot, path); + SStrCopy(path, fullPath, sizeof(path)); + + } + // TODO: OsFileList(this->path, pattern, PatchFiles::EnumPatchArchives, this, 0); + } + + if (i == 0) { + std::qsort(this->files.Ptr(), this->files.Count(), sizeof(char*), &PatchFiles::qsortpatchfiles); + } + } +} + +int PatchFiles::qsortpatchfiles(const void* a1, const void* a2) { + return -SStrCmpI(*((const char**) a1), *((const char**) a2), STORM_MAX_STR); +} diff --git a/src/client/Patch.hpp b/src/client/Patch.hpp new file mode 100644 index 0000000..443c879 --- /dev/null +++ b/src/client/Patch.hpp @@ -0,0 +1,33 @@ +#ifndef CLIENT_PATCH_HPP +#define CLIENT_PATCH_HPP + + +#include + + +class PatchFiles { + public: + PatchFiles(); + + void SearchArchives(const char* locale, int32_t a3); + + public: + char* path; + TSGrowableArray files; + + private: + static int qsortpatchfiles(const void* a1, const void* a2); + + private: + struct ListEntry { + uint32_t num1; + uint32_t num2; + bool absolute; + const char* dataDir; + const char* pattern; + }; +}; + + + +#endif diff --git a/src/util/SFile.cpp b/src/util/SFile.cpp index 84a29f6..5cc0d76 100644 --- a/src/util/SFile.cpp +++ b/src/util/SFile.cpp @@ -8,8 +8,9 @@ #include #include "util/Filesystem.hpp" -static char s_basepath[STORM_MAX_PATH] = {0}; -static char s_datapath[STORM_MAX_PATH] = {0}; +static char s_basepath[STORM_MAX_PATH] = { 0 }; +static char s_datapath[STORM_MAX_PATH] = { 0 }; +static char s_datapath2[STORM_MAX_PATH] = { 0 }; // TODO Proper implementation int32_t SFile::Close(SFile* file) { @@ -279,3 +280,17 @@ int32_t SFile::GetDataPath(char* buffer, size_t bufferchars) { SStrCopy(buffer, s_datapath, bufferchars); return 1; } + +int32_t SFile::SetDataPathAlternate(const char* path) { + SStrCopy(s_datapath2, path, STORM_MAX_PATH); + size_t length = SStrLen(s_datapath2); + if (length && s_datapath2[length - 1] != '\\' && s_datapath2[length - 1] != '/') { + SStrPack(s_datapath2, "\\", STORM_MAX_PATH); + } + return 1; +} + +int32_t SFile::RebuildHash() { + // TODO + return 1; +} diff --git a/src/util/SFile.hpp b/src/util/SFile.hpp index 1e3db95..350859d 100644 --- a/src/util/SFile.hpp +++ b/src/util/SFile.hpp @@ -39,6 +39,8 @@ class SFile { static int32_t SetDataPath(const char* path); static int32_t GetBasePath(char* path, size_t capacity); static int32_t GetDataPath(char* path, size_t capacity); + static int32_t SetDataPathAlternate(const char* path); + static int32_t RebuildHash(); // Member variables SFILE_TYPE m_type; From 132770245174bf2a77a8d7783911a33ca216a200 Mon Sep 17 00:00:00 2001 From: VDm Date: Sun, 9 Mar 2025 22:42:42 +0400 Subject: [PATCH 13/17] fix(console): revert OnChar handler due to the fact that the original console does not support Unicode --- src/console/Handlers.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/console/Handlers.cpp b/src/console/Handlers.cpp index b5e30d9..80bdab9 100644 --- a/src/console/Handlers.cpp +++ b/src/console/Handlers.cpp @@ -12,14 +12,15 @@ static int32_t s_historyIndex = 0; namespace { int32_t OnChar(const EVENT_DATA_CHAR* data, void* param) { - char character[8] = {}; + char character[2] = {}; if (ConsoleAccessGetEnabled() && EventIsKeyDown(ConsoleGetHotKey())) { return 0; } if (ConsoleGetActive()) { - SUniSPutUTF8(data->ch, character); + character[0] = char(data->ch); + character[1] = 0; PasteInInputLine(character); ResetHighlight(); From ba97426d5b65f8bcaf514333d66a1fd240f5283b Mon Sep 17 00:00:00 2001 From: VDm Date: Mon, 10 Mar 2025 00:17:48 +0400 Subject: [PATCH 14/17] feat(event): proper code page conversion --- src/event/win/Input.cpp | 166 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 161 insertions(+), 5 deletions(-) diff --git a/src/event/win/Input.cpp b/src/event/win/Input.cpp index a08b241..619942f 100644 --- a/src/event/win/Input.cpp +++ b/src/event/win/Input.cpp @@ -8,6 +8,142 @@ #include "event/sdl/Input.hpp" #endif +static const uint32_t s_latin5lookup[256] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, + 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, + 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, + 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, + 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, + 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, + 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, + 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, + 0x7E, 0x7F, 0x20AC, 0x20, 0x201A, 0x192, 0x201E, 0x2026, 0x2020, + 0x2021, 0x2C6, 0x2030, 0x160, 0x2039, 0x152, 0x20, 0x20, 0x20, + 0x20, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x2DC, + 0x2122, 0x161, 0x203A, 0x153, 0x20, 0x20, 0x178, 0x0A0, 0x0A1, + 0x0A2, 0x0A3, 0x0A4, 0x0A5, 0x0A6, 0x0A7, 0x0A8, 0x0A9, 0x0AA, + 0x0AB, 0x0AC, 0x0AD, 0x0AE, 0x0AF, 0x0B0, 0x0B1, 0x0B2, 0x0B3, + 0x0B4, 0x0B5, 0x0B6, 0x0B7, 0x0B8, 0x0B9, 0x0BA, 0x0BB, 0x0BC, + 0x0BD, 0x0BE, 0x0BF, 0x0C0, 0x0C1, 0x0C2, 0x0C3, 0x0C4, 0x0C5, + 0x0C6, 0x0C7, 0x0C8, 0x0C9, 0x0CA, 0x0CB, 0x0CC, 0x0CD, 0x0CE, + 0x0CF, 0x11E, 0x0D1, 0x0D2, 0x0D3, 0x0D4, 0x0D5, 0x0D6, 0x0D7, + 0x0D8, 0x0D9, 0x0DA, 0x0DB, 0x0DC, 0x130, 0x15E, 0x0DF, 0x0E0, + 0x0E1, 0x0E2, 0x0E3, 0x0E4, 0x0E5, 0x0E6, 0x0E7, 0x0E8, 0x0E9, + 0x0EA, 0x0EB, 0x0EC, 0x0ED, 0x0EE, 0x0EF, 0x11F, 0x0F1, 0x0F2, + 0x0F3, 0x0F4, 0x0F5, 0x0F6, 0x0F7, 0x0F8, 0x0F9, 0x0FA, 0x0FB, + 0x0FC, 0x131, 0x15F, 0x0FF +}; + +static const uint32_t s_latin1lookup[32] = { + 0x0FFFE, 0x0FFFE, 0x201A, 0x192, 0x201E, 0x2026, 0x2020, 0x2021, + 0x2C6, 0x2030, 0x160, 0x2039, 0x152, 0x0FFFE, 0x0FFFE, 0x0FFFE, + 0x0FFFE, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + 0x2DC, 0x2122, 0x161, 0x203A, 0x153, 0x0FFFE, 0x0FFFE, 0x178 +}; + +static const uint32_t s_cyrilliclookup[256] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, + 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, + 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, + 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, + 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, + 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, + 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, + 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, + 0x7E, 0x7F, 0x402, 0x403, 0x201A, 0x453, 0x201E, 0x2026, 0x2020, + 0x2021, 0x20AC, 0x2030, 0x409, 0x2039, 0x40A, 0x40C, 0x40B, 0x40F, + 0x452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x20, + 0x2122, 0x459, 0x203A, 0x45A, 0x45C, 0x45B, 0x45F, 0x0A0, 0x40E, + 0x45E, 0x408, 0x0A4, 0x490, 0x0A6, 0x0A7, 0x401, 0x0A9, 0x404, + 0x0AB, 0x0AC, 0x0AD, 0x0AE, 0x407, 0x0B0, 0x0B1, 0x406, 0x456, + 0x491, 0x0B5, 0x0B6, 0x0B7, 0x451, 0x2116, 0x454, 0x0BB, 0x458, + 0x405, 0x455, 0x457, 0x410, 0x411, 0x412, 0x413, 0x414, 0x415, + 0x416, 0x417, 0x418, 0x419, 0x41A, 0x41B, 0x41C, 0x41D, 0x41E, + 0x41F, 0x420, 0x421, 0x422, 0x423, 0x424, 0x425, 0x426, 0x427, + 0x428, 0x429, 0x42A, 0x42B, 0x42C, 0x42D, 0x42E, 0x42F, 0x430, + 0x431, 0x432, 0x433, 0x434, 0x435, 0x436, 0x437, 0x438, 0x439, + 0x43A, 0x43B, 0x43C, 0x43D, 0x43E, 0x43F, 0x440, 0x441, 0x442, + 0x443, 0x444, 0x445, 0x446, 0x447, 0x448, 0x449, 0x44A, 0x44B, + 0x44C, 0x44D, 0x44E, 0x44F +}; + +static const uint32_t s_latin2lookup[256] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, + 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, + 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, + 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, + 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, + 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, + 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, + 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, + 0x7E, 0x7F, 0x20AC, 0x0FFFE, 0x201A, 0x0FFFE, 0x201E, 0x2026, 0x2020, + 0x2021, 0x0FFFE, 0x2030, 0x160, 0x2039, 0x15A, 0x164, 0x17D, 0x179, + 0x0FFFE, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x0FFFE, + 0x2122, 0x161, 0x203A, 0x15B, 0x165, 0x17E, 0x17A, 0x0A0, 0x2C7, + 0x2D8, 0x141, 0x0A4, 0x104, 0x0A6, 0x0A7, 0x0A8, 0x0A9, 0x15E, + 0x0AB, 0x0AC, 0x0AD, 0x0AE, 0x17B, 0x0B0, 0x0B1, 0x2DB, 0x142, + 0x0B4, 0x0B5, 0x0B6, 0x0B7, 0x0B8, 0x105, 0x15F, 0x0BB, 0x13D, + 0x2DD, 0x13E, 0x17C, 0x154, 0x0C1, 0x0C2, 0x102, 0x0C4, 0x139, + 0x106, 0x0C7, 0x10C, 0x0C9, 0x118, 0x0CB, 0x11A, 0x0CD, 0x0CE, + 0x10E, 0x110, 0x143, 0x147, 0x0D3, 0x0D4, 0x150, 0x0D6, 0x0D7, + 0x158, 0x16E, 0x0DA, 0x170, 0x0DC, 0x0DD, 0x162, 0x0DF, 0x155, + 0x0E1, 0x0E2, 0x103, 0x0E4, 0x13A, 0x107, 0x0E7, 0x10D, 0x0E9, + 0x119, 0x0EB, 0x11B, 0x0ED, 0x0EE, 0x10F, 0x111, 0x144, 0x148, + 0x0F3, 0x0F4, 0x151, 0x0F6, 0x0F7, 0x159, 0x16F, 0x0FA, 0x171, + 0x0FC, 0x0FD, 0x163, 0x2D9 +}; + +static const uint32_t s_thailookup[256] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, + 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, + 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, + 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, + 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, + 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, + 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, + 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, + 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, + 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, + 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, + 0x7E, 0x7F, 0x20AC, 0x20, 0x20, 0x20, 0x20, 0x2026, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, + 0x20, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x20, + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x0A0, 0x0E01, + 0x0E02, 0x0E03, 0x0E04, 0x0E05, 0x0E06, 0x0E07, 0x0E08, 0x0E09, 0x0E0A, + 0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F, 0x0E10, 0x0E11, 0x0E12, 0x0E13, + 0x0E14, 0x0E15, 0x0E16, 0x0E17, 0x0E18, 0x0E19, 0x0E1A, 0x0E1B, 0x0E1C, + 0x0E1D, 0x0E1E, 0x0E1F, 0x0E20, 0x0E21, 0x0E22, 0x0E23, 0x0E24, 0x0E25, + 0x0E26, 0x0E27, 0x0E28, 0x0E29, 0x0E2A, 0x0E2B, 0x0E2C, 0x0E2D, 0x0E2E, + 0x0E2F, 0x0E30, 0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37, + 0x0E38, 0x0E39, 0x0E3A, 0x20, 0x20, 0x20, 0x20, 0x0E3F, 0x0E40, + 0x0E41, 0x0E42, 0x0E43, 0x0E44, 0x0E45, 0x0E46, 0x0E47, 0x0E48, 0x0E49, + 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D, 0x0E4E, 0x0E4F, 0x0E50, 0x0E51, 0x0E52, + 0x0E53, 0x0E54, 0x0E55, 0x0E56, 0x0E57, 0x0E58, 0x0E59, 0x0E5A, 0x0E5B, + 0x20, 0x20, 0x20, 0x20 +}; + + static RECT s_defaultWindowRect; static int32_t s_savedResize; @@ -469,7 +605,13 @@ void OsInputGetMousePosition(int32_t* x, int32_t* y) { } } +uint32_t OsInputGetCodePage() { + return ::GetACP(); +} + int32_t OsWindowProc(void* window, uint32_t message, uintptr_t wparam, intptr_t lparam) { + static uint32_t s_codepage = 0; + auto hwnd = static_cast(window); // TODO @@ -539,14 +681,28 @@ int32_t OsWindowProc(void* window, uint32_t message, uintptr_t wparam, intptr_t uint32_t character = wparam; if (wparam >= 128) { - // Workaround - wchar_t u16_character = 0; - ::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, reinterpret_cast(&wparam), 1, &u16_character, 1); - character = u16_character; + if (s_codepage == 0) { + s_codepage = OsInputGetCodePage(); + } + if (s_codepage == 1254) { + // ANSI Turkish; Turkish (Windows) + character = s_latin5lookup[wparam % 256]; + } else if (s_codepage == 1252 && wparam < 0xA0) { + // ANSI Latin 1; Western European (Windows) + character = s_latin1lookup[wparam % 32]; + } else if (s_codepage == 1251) { + // ANSI Cyrillic; Cyrillic (Windows) + character = s_cyrilliclookup[wparam % 256]; + } else if (s_codepage == 1250) { + // ANSI Central European; Central European (Windows) + character = s_latin2lookup[wparam % 256]; + } else if (s_codepage == 874) { + // Thai (Windows) + character = s_thailookup[wparam % 256]; + } } OsQueuePut(OS_INPUT_CHAR, character, LOWORD(lparam), 0, 0); - return 0; } From 35a59942ff046b9e0a642b398b4600def3a50841 Mon Sep 17 00:00:00 2001 From: VDm Date: Tue, 11 Mar 2025 00:16:55 +0400 Subject: [PATCH 15/17] feat(console): implement RegisterGxCVars --- src/console/CVar.cpp | 8 +- src/console/CVar.hpp | 14 ++- src/console/Device.cpp | 212 ++++++++++++++++++++++++++--------------- src/gx/CGxFormat.cpp | 3 + src/gx/CGxFormat.hpp | 2 + 5 files changed, 157 insertions(+), 82 deletions(-) create mode 100644 src/gx/CGxFormat.cpp diff --git a/src/console/CVar.cpp b/src/console/CVar.cpp index c7349e8..5a3b05e 100644 --- a/src/console/CVar.cpp +++ b/src/console/CVar.cpp @@ -17,7 +17,7 @@ CVar* CVar::Lookup(const char* name) { : nullptr; } -CVar* CVar::Register(const char* name, const char* help, uint32_t flags, const char* value, bool (*fcn)(CVar*, const char*, const char*, void*), uint32_t category, bool a7, void* arg, bool a9) { +CVar* CVar::Register(const char* name, const char* help, uint32_t flags, const char* value, bool (*fcn)(CVar*, const char*, const char*, void*), uint32_t category, bool setCommand, void* arg, bool a9) { CVar* var = CVar::s_registeredCVars.Ptr(name); if (var) { @@ -36,7 +36,7 @@ CVar* CVar::Register(const char* name, const char* help, uint32_t flags, const c var->Set(value, setValue, setReset, setDefault, false); - if (!a7) { + if (!setCommand) { var->m_flags |= 0x80000000; } @@ -59,7 +59,7 @@ CVar* CVar::Register(const char* name, const char* help, uint32_t flags, const c var->m_arg = arg; var->m_help.Copy(help); - if (a7) { + if (setCommand) { var->Set(value, true, true, false, false); } else { var->Set(value, true, false, true, false); @@ -67,7 +67,7 @@ CVar* CVar::Register(const char* name, const char* help, uint32_t flags, const c var->m_flags = flags | 0x1; - if (!a7) { + if (!setCommand) { var->m_flags |= 0x8000000; } diff --git a/src/console/CVar.hpp b/src/console/CVar.hpp index 948a7d7..41df648 100644 --- a/src/console/CVar.hpp +++ b/src/console/CVar.hpp @@ -6,6 +6,8 @@ #include #include +#include "console/Types.hpp" + class CVar : public TSHashObject { public: // Static variables @@ -14,7 +16,17 @@ class CVar : public TSHashObject { // Static functions static CVar* Lookup(const char* name); - static CVar* Register(const char*, const char*, uint32_t, const char*, bool (*)(CVar*, const char*, const char*, void*), uint32_t, bool, void*, bool); + static CVar* Register( + const char* name, + const char* help, + uint32_t flags, + const char* value, + bool (*fcn)(CVar*, const char*, const char*, void*) = nullptr, + uint32_t category = CATEGORY::DEFAULT, + bool setCommand = false, + void* arg = nullptr, + bool a9 = false + ); static void Initialize(const char* filename); static int32_t Load(const char* filename); static int32_t Load(HOSFILE fileHandle); diff --git a/src/console/Device.cpp b/src/console/Device.cpp index 2d87392..567d9d6 100644 --- a/src/console/Device.cpp +++ b/src/console/Device.cpp @@ -8,6 +8,24 @@ #include #include + +CVar* s_cvGxFixedFunction; +CVar* s_cvGxWindowResizeLock; +CVar* s_cvGxVideoOptionsVersion; +CVar* s_cvGxMaxFPSBk; +CVar* s_cvGxMaxFPS; +CVar* s_cvGxOverride; +CVar* s_cvGxStereoEnabled; +CVar* s_cvGxFixLag; +CVar* s_cvGxMultisampleQuality; +CVar* s_cvGxMultisample; +CVar* s_cvGxCursor; +CVar* s_cvGxAspect; +CVar* s_cvGxVSync; +CVar* s_cvGxTripleBuffer; +CVar* s_cvGxRefresh; +CVar* s_cvGxDepthBits; +CVar* s_cvGxColorBits; CVar* s_cvGxMaximize; CVar* s_cvGxResolution; CVar* s_cvGxWidescreen; @@ -44,6 +62,86 @@ EGxApi g_gxApiSupported[] = { size_t g_numGxApiSupported = sizeof(g_gxApiSupported) / sizeof(EGxApi); + +bool CVGxWindowResizeLockCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool GxVideoOptionsVersionCallback(CVar*, const char*, const char*, void*) { + return true; +} + +bool CVGxMaxFPSBkCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool CVGxMaxFPSCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool CVGxOverrideCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool CVGxStereoEnabledCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool CVGxFixLagCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool CVGxMultisampleQualityCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool CVGxMultisampleCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool CVGxCursorCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool CVGxAspectCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool CVGxVSyncCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool CVGxTripleBufferCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool CVGxRefreshCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool CVGxDepthBitsCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + +bool CVGxColorBitsCallback(CVar*, const char*, const char*, void*) { + // TODO + return true; +} + bool CVGxMaximizeCallback(CVar*, const char*, const char*, void*) { // TODO return true; @@ -107,92 +205,52 @@ EGxApi GxApiDefault() { } void RegisterGxCVars() { - auto& format = s_defaults.format; + const auto& format = s_defaults.format; - // TODO CURRENT_LANGUAGE check? - auto v1 = true; + // TODO: bool isChinese = s_currentLocaleIndex == 4 (zhCN) + bool isChinese = false; - s_cvGxWidescreen = CVar::Register( - "widescreen", - "Allow widescreen support", - 0x0, - "1", - nullptr, - 1, - false, - nullptr, - false - ); + const uint32_t graphics = CATEGORY::GRAPHICS; - s_cvGxWindow = CVar::Register( - "gxWindow", - "toggle fullscreen/window", - 0x1 | 0x2, - v1 ? "1" : "0", - &CVGxWindowCallback, - 1, - 0, - 0, - 0 - ); + s_cvGxWidescreen = CVar::Register("widescreen", "Allow widescreen support", 0x0, "1", nullptr, graphics); + s_cvGxWindow = CVar::Register("gxWindow", "toggle fullscreen/window", 0x1 | 0x2, isChinese ? "1" : "0", &CVGxWindowCallback, graphics); + s_cvGxMaximize = CVar::Register("gxMaximize", "maximize game window", 0x1 | 0x2, isChinese ? "1" : "0", &CVGxMaximizeCallback, graphics); - s_cvGxMaximize = CVar::Register( - "gxMaximize", - "maximize game window", - 0x1 | 0x2, - v1 ? "1" : "0", - &CVGxMaximizeCallback, - 1, - 0, - 0, - 0 - ); + char value[260] = {}; + SStrPrintf(value, sizeof(value), "%s", CGxFormat::formatToColorBitsString[format.colorFormat]); + s_cvGxColorBits = CVar::Register("gxColorBits", "color bits", 0x1 | 0x2, value, &CVGxColorBitsCallback, graphics); - // TODO s_cvGxColorBits - // TODO s_cvGxDepthBits + SStrPrintf(value, sizeof(value), "%s", CGxFormat::formatToColorBitsString[format.depthFormat]); + s_cvGxDepthBits = CVar::Register("gxDepthBits", "depth bits", 0x1 | 0x2, value, &CVGxDepthBitsCallback, graphics); - char resolution[260]; - SStrPrintf(resolution, 260, "%dx%d", format.size.x, format.size.y); - s_cvGxResolution = CVar::Register( - "gxResolution", - "resolution", - 0x1 | 0x2, - resolution, - &CVGxResolutionCallback, - 1, - false, - nullptr, - false - ); + SStrPrintf(value, 260, "%dx%d", format.size.x, format.size.y); + s_cvGxResolution = CVar::Register("gxResolution", "resolution", 0x1 | 0x2, value, &CVGxResolutionCallback, graphics); - // TODO s_cvGxRefresh - // TODO s_cvGxTripleBuffer - // TODO s_cvGxApi + s_cvGxRefresh = CVar::Register("gxRefresh", "refresh rate", 0x1 | 0x2, "75", &CVGxRefreshCallback, graphics); + s_cvGxTripleBuffer = CVar::Register("gxTripleBuffer", "triple buffer", 0x1 | 0x2, "0", &CVGxTripleBufferCallback, graphics); + s_cvGxApi = CVar::Register("gxApi", "graphics api", 0x1 | 0x2, g_gxApiNames[GxApiDefault()], &CVGxApiCallback, graphics); - s_cvGxApi = CVar::Register( - "gxApi", - "graphics api", - 0x1 | 0x2, - g_gxApiNames[GxApiDefault()], - CVGxApiCallback, - 1, - false, - nullptr, - false - ); + s_cvGxVSync = CVar::Register("gxVSync", "vsync on or off", 0x1 | 0x2, "1", &CVGxVSyncCallback, graphics); + s_cvGxAspect = CVar::Register("gxAspect", "constrain window aspect", 0x1 | 0x2, "1", &CVGxAspectCallback, graphics); - // TODO s_cvGxVSync - // TODO s_cvGxAspect - // TODO s_cvGxCursor - // TODO s_cvGxMultisample - // TODO s_cvGxFixLag - // TODO s_cvGxStereoEnabled - // TODO s_cvGxOverride - // TODO s_cvGxAspect - // TODO s_cvGxMaxFPS - // TODO s_cvGxMaxFPSBk - // TODO s_cvWindowResizeLock - // TODO s_cvFixedFunction + s_cvGxCursor = CVar::Register("gxCursor", "toggle hardware cursor", 0x1 | 0x2, "1", &CVGxCursorCallback, graphics); + + // TODO: v10 = *(_DWORD*)(dword_CABB60 + 84); + int v10 = 0; + SStrPrintf(value, sizeof(value), "%d", v10); + s_cvGxMultisample = CVar::Register("gxMultisample", "multisample", 0x1 | 0x2, value, &CVGxMultisampleCallback, graphics); + s_cvGxMultisampleQuality = CVar::Register("gxMultisampleQuality", "multisample quality", 0x1 | 0x2, "0.0", &CVGxMultisampleQualityCallback, graphics); + + // TODO: v10 = *(_DWORD*)(dword_CABB60 + 80); + SStrPrintf(value, sizeof(value), "%d", v10); + s_cvGxFixLag = CVar::Register("gxFixLag", "prevent cursor lag", 0x1 | 0x2, value, &CVGxFixLagCallback, graphics); + s_cvGxStereoEnabled = CVar::Register("gxStereoEnabled", "Enable stereoscopic rendering", 0x1, "0", &CVGxStereoEnabledCallback, graphics); + s_cvGxOverride = CVar::Register("gxOverride", "gx overrides", 0x1, "", &CVGxOverrideCallback, graphics); + s_cvGxMaxFPS = CVar::Register("maxFPS", "Set FPS limit", 0x1, "200", &CVGxMaxFPSCallback, graphics); + s_cvGxMaxFPSBk = CVar::Register("maxFPSBk", "Set background FPS limit", 0x1, "30", &CVGxMaxFPSBkCallback, graphics); + s_cvGxVideoOptionsVersion = CVar::Register("videoOptionsVersion", "Video options version", 0x1 | 0x2, "0", &GxVideoOptionsVersionCallback, graphics); + s_cvGxWindowResizeLock = CVar::Register("windowResizeLock", "prevent resizing in windowed mode", 1, "0", &CVGxWindowResizeLockCallback, graphics); + s_cvGxFixedFunction = CVar::Register("fixedFunction", "Force fixed function rendering", 0x1 | 0x2, "0", 0, graphics); } void UpdateGxCVars() { diff --git a/src/gx/CGxFormat.cpp b/src/gx/CGxFormat.cpp new file mode 100644 index 0000000..f14838a --- /dev/null +++ b/src/gx/CGxFormat.cpp @@ -0,0 +1,3 @@ +#include "gx/CGxFormat.hpp" + +const char* CGxFormat::formatToColorBitsString[Formats_Last] = { "16", "24", "24", "30", "16", "24", "24", "32" }; diff --git a/src/gx/CGxFormat.hpp b/src/gx/CGxFormat.hpp index 8960d6b..0e33aa4 100644 --- a/src/gx/CGxFormat.hpp +++ b/src/gx/CGxFormat.hpp @@ -19,6 +19,8 @@ class CGxFormat { Formats_Last = 8 }; + static const char* formatToColorBitsString[Formats_Last]; + // Member variables bool hwTnL; bool hwCursor; From 1e366389643d86b916dad6d9377a34eefa9292cd Mon Sep 17 00:00:00 2001 From: VDm Date: Tue, 11 Mar 2025 01:22:05 +0400 Subject: [PATCH 16/17] feat(console): update SetGxCVars and UpdateGxCVars --- src/console/Device.cpp | 121 ++++++++++++++++++++++++++++++++--------- src/gx/CGxDevice.cpp | 8 +++ src/gx/CGxDevice.hpp | 2 + src/gx/Gx.cpp | 12 ++++ src/gx/Gx.hpp | 6 ++ 5 files changed, 123 insertions(+), 26 deletions(-) diff --git a/src/console/Device.cpp b/src/console/Device.cpp index 567d9d6..98042b3 100644 --- a/src/console/Device.cpp +++ b/src/console/Device.cpp @@ -3,12 +3,15 @@ #include "client/Gui.hpp" #include "console/Console.hpp" #include "console/CVar.hpp" +#include "console/Command.hpp" #include "event/Input.hpp" +#include "gx/Gx.hpp" #include "gx/Device.hpp" #include #include +CVar* s_cvHwDetect; CVar* s_cvGxFixedFunction; CVar* s_cvGxWindowResizeLock; CVar* s_cvGxVideoOptionsVersion; @@ -190,6 +193,11 @@ bool CVGxApiCallback(CVar* h, const char* oldValue, const char* newValue, void* return false; } +int32_t CCGxRestart(const char*, const char*) { + // TODO + return 1; +} + EGxApi GxApiDefault() { #if defined(WHOA_SYSTEM_WIN) return GxApi_D3d9; @@ -254,23 +262,27 @@ void RegisterGxCVars() { } void UpdateGxCVars() { - // TODO others - + s_cvGxColorBits->Update(); + s_cvGxDepthBits->Update(); s_cvGxWindow->Update(); s_cvGxResolution->Update(); - - // TODO others - + s_cvGxRefresh->Update(); + s_cvGxTripleBuffer->Update(); + s_cvGxApi->Update(); + s_cvGxVSync->Update(); + s_cvGxAspect->Update(); s_cvGxMaximize->Update(); - - // TODO others + s_cvGxCursor->Update(); + s_cvGxMultisample->Update(); + s_cvGxMultisampleQuality->Update(); + s_cvGxFixLag->Update(); } void SetGxCVars(const CGxFormat& format) { - char value[1024]; + char value[1024] = {}; - // TODO s_cvGxColorBits - // TODO s_cvGxDepthBits + s_cvGxColorBits->Set(CGxFormat::formatToColorBitsString[format.colorFormat], true, false, false, true); + s_cvGxDepthBits->Set(CGxFormat::formatToColorBitsString[format.depthFormat], true, false, false, true); SStrPrintf(value, sizeof(value), "%d", format.window); s_cvGxWindow->Set(value, true, false, false, true); @@ -278,43 +290,100 @@ void SetGxCVars(const CGxFormat& format) { SStrPrintf(value, sizeof(value), "%dx%d", format.size.x, format.size.y); s_cvGxResolution->Set(value, true, false, false, true); - // TODO s_cvGxRefresh - // TODO others + SStrPrintf(value, sizeof(value), "%d", format.refreshRate); + s_cvGxRefresh->Set(value, true, false, false, true); + + // TODO: (format + 28) > 1 + s_cvGxTripleBuffer->Set("0", true, false, false, true); + + SStrPrintf(value, sizeof(value), "%d", format.vsync); + s_cvGxVSync->Set(value, true, false, false, true); + + // TODO: format.aspectRatio + SStrPrintf(value, sizeof(value), "%d", 0); + s_cvGxAspect->Set(value, true, false, false, true); SStrPrintf(value, sizeof(value), "%d", format.maximize); s_cvGxMaximize->Set(value, true, false, false, true); - // TODO others + SStrPrintf(value, sizeof(value), "%d", format.hwCursor); + s_cvGxCursor->Set(value, true, false, false, true); + + SStrPrintf(value, sizeof(value), "%d", format.sampleCount); + s_cvGxMultisample->Set(value, true, false, false, true); + + // TODO: format.multisampleQuality + SStrPrintf(value, sizeof(value), "%f", 0.0f); + s_cvGxMultisampleQuality->Set(value, true, false, false, true); + + SStrPrintf(value, sizeof(value), "%d", format.fixLag); + s_cvGxFixLag->Set(value, true, false, false, true); UpdateGxCVars(); } void ConsoleDeviceInitialize(const char* title) { - // TODO + GxLogOpen(); + + s_cvHwDetect = CVar::Register("hwDetect", "do hardware detection", 0x1, "1", nullptr, CATEGORY::GRAPHICS); + + // TODO: sub_76BA30(&unk_CABB38, &byte_CABCBD); << ConsoleDetect + // TODO: byte_CABCBC = 1; + + if (CmdLineGetBool(WOWCMD_HW_DETECT) || s_cvHwDetect->GetInt() != 0) { + s_hwDetect = true; + s_cvHwDetect->Set("0", true, false, false, true); + } else { + s_hwDetect = false; + } - // TODO proper logic - s_hwDetect = true; ConsoleAccessSetEnabled(CmdLineGetBool(WOWCMD_CONSOLE)); - // TODO + // TODO: sub_76B520(&unk_CABAF0, &unk_CABB38); RegisterGxCVars(); + ConsoleCommandRegister("gxRestart", &CCGxRestart, CATEGORY::GRAPHICS, nullptr); - // TODO ConsoleCommandRegister("gxRestart", &CCGxRestart, 1, nullptr); + // TODO: GxAdapterMonitorModes((int)&unk_CABCC8); + // TODO: ValidateFormatMonitor(&unk_CABDA8); - // TODO + // TODO: if ( GxAdapterDesktopMode(&v28) ) + if (true) { + s_requestedFormat.size.x = 1024; + s_requestedFormat.size.y = 768; + s_requestedFormat.colorFormat = CGxFormat::Fmt_Argb8888; + s_requestedFormat.depthFormat = CGxFormat::Fmt_Ds248; + } - // TODO - // - source the size values correctly - s_requestedFormat.size.x = 1024; - s_requestedFormat.size.y = 768; - s_requestedFormat.colorFormat = CGxFormat::Fmt_Argb8888; - s_requestedFormat.depthFormat = CGxFormat::Fmt_Ds248; + GxLog("ConsoleDeviceInitialize(): hwDetect = %d, hwChanged = %d", s_hwDetect, s_hwChanged); + + if (CmdLineGetBool(WOWCMD_RES_800x600)) { + s_requestedFormat.size.x = 800; + s_requestedFormat.size.y = 600; + } else if (CmdLineGetBool(WOWCMD_RES_1024x768)) { + s_requestedFormat.size.x = 1024; + s_requestedFormat.size.y = 768; + } else if (CmdLineGetBool(WOWCMD_RES_1280x960)) { + s_requestedFormat.size.x = 1280; + s_requestedFormat.size.y = 960; + } else if (CmdLineGetBool(WOWCMD_RES_1280x1024)) { + s_requestedFormat.size.x = 1280; + s_requestedFormat.size.y = 1024; + } else if (CmdLineGetBool(WOWCMD_RES_1600x1200)) { + s_requestedFormat.size.x = 1600; + s_requestedFormat.size.y = 1200; + } + + if (s_cvGxFixedFunction->GetInt() != 0) { + // TODO: (dword_CABD20 = 0) s_requestedFormat.unknown_field = 0; + s_requestedFormat.pos.y = 0; // <--- CHECK THIS + s_requestedFormat.pos.x = 0; + } if (s_hwDetect || s_hwChanged) { // TODO Sub76B3F0(&UnkCABAF0, &UnkCABB38); - // TODO s_cvFixedFunction->Set("0", 1, 0, 0, 1); + s_cvGxFixedFunction->Set("0", true, false, false, true); // TODO memcpy(&s_requestedFormat, &s_defaults.format, sizeof(s_requestedFormat)); s_requestedFormat.window = s_cvGxWindow->GetInt() != 0; diff --git a/src/gx/CGxDevice.cpp b/src/gx/CGxDevice.cpp index 32f888c..4af728a 100644 --- a/src/gx/CGxDevice.cpp +++ b/src/gx/CGxDevice.cpp @@ -106,6 +106,14 @@ uint32_t CGxDevice::s_texFormatBytesPerBlock[] = { CGxShader* CGxDevice::s_uiVertexShader = nullptr; CGxShader* CGxDevice::s_uiPixelShader = nullptr; +void CGxDevice::LogOpen() { + // TODO +} + +void CGxDevice::LogClose() { + // TODO +} + void CGxDevice::Log(const char* format, ...) { // TODO } diff --git a/src/gx/CGxDevice.hpp b/src/gx/CGxDevice.hpp index 91eedf0..9cf9609 100644 --- a/src/gx/CGxDevice.hpp +++ b/src/gx/CGxDevice.hpp @@ -51,6 +51,8 @@ class CGxDevice { static CGxShader* s_uiPixelShader; // Static functions + static void LogOpen(); + static void LogClose(); static void Log(const char* format, ...); static void Log(const CGxFormat& format); #if defined(WHOA_SYSTEM_WIN) diff --git a/src/gx/Gx.cpp b/src/gx/Gx.cpp index 07797f3..72a4ea9 100644 --- a/src/gx/Gx.cpp +++ b/src/gx/Gx.cpp @@ -59,3 +59,15 @@ void GxFormatColor(CImVector& color) { color = formattedColor; } } + +void GxLogOpen() { + CGxDevice::LogOpen(); +} + +void GxLogClose() { + CGxDevice::LogClose(); +} + +void GxLog(const char* format, ...) { + // TODO +} diff --git a/src/gx/Gx.hpp b/src/gx/Gx.hpp index 337090d..bd01c83 100644 --- a/src/gx/Gx.hpp +++ b/src/gx/Gx.hpp @@ -18,4 +18,10 @@ void GxCapsWindowSize(CRect&); void GxFormatColor(CImVector&); +void GxLogOpen(); + +void GxLogClose(); + +void GxLog(const char* format, ...); + #endif From 236f0372e4533f2d74d6455ebf900d15dbf9f01e Mon Sep 17 00:00:00 2001 From: VDm Date: Tue, 11 Mar 2025 02:12:14 +0400 Subject: [PATCH 17/17] fix(gx): use CVar value for hardware cursor --- src/console/Device.cpp | 48 ++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/console/Device.cpp b/src/console/Device.cpp index 98042b3..c8b9fcb 100644 --- a/src/console/Device.cpp +++ b/src/console/Device.cpp @@ -110,8 +110,9 @@ bool CVGxMultisampleCallback(CVar*, const char*, const char*, void*) { return true; } -bool CVGxCursorCallback(CVar*, const char*, const char*, void*) { - // TODO +bool CVGxCursorCallback(CVar*, const char*, const char* value, void*) { + s_requestedFormat.hwCursor = SStrToInt(value) != 0; + ConsoleWrite("set pending gxRestart", DEFAULT_COLOR); return true; } @@ -342,6 +343,12 @@ void ConsoleDeviceInitialize(const char* title) { // TODO: sub_76B520(&unk_CABAF0, &unk_CABB38); + // CHANGE: Remove this when the rest will be ready + s_defaults.format.size.x = 1024; + s_defaults.format.size.y = 768; + s_defaults.format.colorFormat = CGxFormat::Fmt_Argb8888; + s_defaults.format.depthFormat = CGxFormat::Fmt_Ds248; + RegisterGxCVars(); ConsoleCommandRegister("gxRestart", &CCGxRestart, CATEGORY::GRAPHICS, nullptr); @@ -395,18 +402,6 @@ void ConsoleDeviceInitialize(const char* title) { SetGxCVars(s_requestedFormat); } - // TODO - - // TODO s_requestedFormat.hwTnL = !CmdLineGetBool(CMD_SW_TNL); - s_requestedFormat.hwTnL = true; - - // TODO - s_requestedFormat.hwCursor = true; - - CGxFormat format; - memcpy(&format, &s_requestedFormat, sizeof(s_requestedFormat)); - - // Select gxApi based on user CVars and command-line parameters EGxApi api = GxApiDefault(); auto gxApiName = s_cvGxApi->GetString(); @@ -414,6 +409,12 @@ void ConsoleDeviceInitialize(const char* title) { auto gxOverride = CmdLineGetString(WOWCMD_GX_OVERRIDE); if (*gxOverride != '\0') { gxApiName = gxOverride; + } else if (CmdLineGetBool(CMD_OPENGL)) { + gxApiName = g_gxApiNames[GxApi_OpenGl]; + } else if (CmdLineGetBool(CMD_D3D)) { + gxApiName = g_gxApiNames[GxApi_D3d9]; + } else if (CmdLineGetBool(CMD_D3D9EX)) { + gxApiName = g_gxApiNames[GxApi_D3d9Ex]; } // Sanitize chosen gxApi against list of supported gxApis @@ -425,13 +426,28 @@ void ConsoleDeviceInitialize(const char* title) { } } - // Log - printf("GxApi_%s selected\n", g_gxApiNames[api]); + s_requestedFormat.fixLag = s_cvGxFixLag->GetInt() != 0; + s_requestedFormat.hwTnL = !CmdLineGetBool(CMD_SW_TNL); + + bool windowed = s_cvGxWindow->GetInt() != 0; + if (CmdLineGetBool(CMD_FULL_SCREEN)) { + windowed = false; + } else if (CmdLineGetBool(WOWCMD_WINDOWED)) { + windowed = true; + } + + s_requestedFormat.window = windowed; + // TODO: byte_CABD47 = windowed; + + GxLog("GxApi_%s selected\n", g_gxApiNames[api]); // Set internally (CVar value reflects the current gxApi at launch), // this will not Set() as CVar gxApi is latched s_cvGxApi->InternalSet(g_gxApiNames[api], true, false, false, true); + CGxFormat format; + memcpy(&format, &s_requestedFormat, sizeof(s_requestedFormat)); + CGxDevice* device = GxDevCreate(api, OsWindowProc, format); // TODO