Add text input support: safe uint16_t* to wstring conversion

Replace unsafe (wchar_t*)pchText casts with uint16_to_wstring() in all
keyboard callbacks. The direct cast is incorrect on platforms where
wchar_t is 4 bytes (Linux/macOS). New helpers in StringHelpers route
through u16string for proper UTF-16 to wchar_t conversion.

- Add uint16_len, uint16_to_u16string, uint16_to_wstring to StringHelpers
- Fix casts in AnvilMenu, CreateWorldMenu, DebugCreateSchematic,
  DebugSetCamera, LaunchMoreOptionsMenu, SignEntryMenu
- Truncate sign text to 15 chars in SignEntryMenu
- Move m_bIgnoreInput reset after if-block in LaunchMoreOptionsMenu
This commit is contained in:
MatthewBeshay 2026-03-30 12:46:50 +11:00
parent dcc937da5e
commit 1ead072c45
8 changed files with 39 additions and 17 deletions

View file

@ -4,6 +4,7 @@
#include "../../Minecraft.World/Headers/net.minecraft.world.level.tile.entity.h"
#include "../../Minecraft.Client/Player/MultiPlayerLocalPlayer.h"
#include "../../Minecraft.Client/Minecraft.h"
#include "../../Minecraft.World/Util/StringHelpers.h"
#include "UIScene_AnvilMenu.h"
UIScene_AnvilMenu::UIScene_AnvilMenu(int iPad, void* _initData,
@ -300,8 +301,8 @@ int UIScene_AnvilMenu::KeyboardCompleteCallback(void* lpParam, bool bRes) {
uint16_t pchText[128];
ZeroMemory(pchText, 128 * sizeof(uint16_t));
InputManager.GetText(pchText);
pClass->setEditNameValue((wchar_t*)pchText);
pClass->m_itemName = (wchar_t*)pchText;
pClass->setEditNameValue(uint16_to_wstring(pchText));
pClass->m_itemName = uint16_to_wstring(pchText);
pClass->updateItemName();
}
return 0;

View file

@ -723,8 +723,8 @@ int UIScene_CreateWorldMenu::KeyboardCompleteWorldNameCallback(void* lpParam,
InputManager.GetText(pchText);
if (pchText[0] != 0) {
pClass->m_editWorldName.setLabel((wchar_t*)pchText);
pClass->m_worldName = (wchar_t*)pchText;
pClass->m_editWorldName.setLabel(uint16_to_wstring(pchText));
pClass->m_worldName = uint16_to_wstring(pchText);
}
pClass->m_buttonCreateWorld.setEnable(!pClass->m_worldName.empty());
@ -901,9 +901,9 @@ IDS_PRO_NOTONLINE_TEXT, uiIDA, 1, ProfileManager.GetPrimaryPad()); return;
bool pccFriendsAllowed = true;
bool bContentRestricted = false;
ProfileManager.AllowedPlayerCreatedContent(
ProfileManager.GetPrimaryPad(), false, &pccAllowed,
&pccFriendsAllowed);
GetAllowedPlayerCreatedContentFlags(ProfileManager.GetPrimaryPad(),
false, &pccAllowed,
&pccFriendsAllowed);
#if defined(__PS3__) || defined(__PSVITA__)
if (isOnlineGame && isSignedInLive) {
ProfileManager.GetChatAndContentRestrictions(
@ -1346,9 +1346,9 @@ int UIScene_CreateWorldMenu::StartGame_SignInReturned(void* pParam,
bool pccAllowed = true;
bool pccFriendsAllowed = true;
ProfileManager.AllowedPlayerCreatedContent(
ProfileManager.GetPrimaryPad(), false, &pccAllowed,
&pccFriendsAllowed);
GetAllowedPlayerCreatedContentFlags(ProfileManager.GetPrimaryPad(),
false, &pccAllowed,
&pccFriendsAllowed);
if (!pccAllowed && !pccFriendsAllowed) noUGC = true;
if (isOnlineGame && (noPrivileges || noUGC)) {

View file

@ -146,7 +146,7 @@ int UIScene_DebugCreateSchematic::KeyboardCompleteCallback(void* lpParam,
InputManager.GetText(pchText);
if (pchText[0] != 0) {
std::wstring value = (wchar_t*)pchText;
std::wstring value = uint16_to_wstring(pchText);
int iVal = 0;
if (!value.empty()) iVal = _fromString<int>(value);
switch (pClass->m_keyboardCallbackControl) {

View file

@ -124,7 +124,7 @@ int UIScene_DebugSetCamera::KeyboardCompleteCallback(void* lpParam, bool bRes) {
InputManager.GetText(pchText);
if (pchText[0] != 0) {
std::wstring value = (wchar_t*)pchText;
std::wstring value = uint16_to_wstring(pchText);
double val = 0;
if (!value.empty()) val = _fromString<double>(value);
switch (pClass->m_keyboardCallbackControl) {

View file

@ -1,5 +1,6 @@
#include "../../Minecraft.World/Platform/stdafx.h"
#include "UI.h"
#include "../../Minecraft.World/Util/StringHelpers.h"
#include "UIScene_LaunchMoreOptionsMenu.h"
#define GAME_CREATE_ONLINE_TIMER_ID 0
@ -589,7 +590,6 @@ int UIScene_LaunchMoreOptionsMenu::KeyboardCompleteSeedCallback(void* lpParam,
bool bRes) {
UIScene_LaunchMoreOptionsMenu* pClass =
(UIScene_LaunchMoreOptionsMenu*)lpParam;
pClass->m_bIgnoreInput = false;
// 4J HEG - No reason to set value if keyboard was cancelled
if (bRes) {
#ifdef __PSVITA__
@ -601,9 +601,10 @@ int UIScene_LaunchMoreOptionsMenu::KeyboardCompleteSeedCallback(void* lpParam,
ZeroMemory(pchText, 128 * sizeof(uint16_t));
#endif
InputManager.GetText(pchText);
pClass->m_editSeed.setLabel((wchar_t*)pchText);
pClass->m_params->seed = (wchar_t*)pchText;
pClass->m_editSeed.setLabel(uint16_to_wstring(pchText));
pClass->m_params->seed = uint16_to_wstring(pchText);
}
pClass->m_bIgnoreInput = false;
return 0;
}
@ -643,6 +644,7 @@ void UIScene_LaunchMoreOptionsMenu::handlePress(F64 controlId, F64 childId) {
0, 60,
&UIScene_LaunchMoreOptionsMenu::KeyboardCompleteSeedCallback,
this, C_4JInput::EKeyboardMode_Default);
KeyboardCompleteSeedCallback(this, true);
#endif
} break;
}

View file

@ -1,5 +1,6 @@
#include "../../Minecraft.World/Platform/stdafx.h"
#include "UI.h"
#include "../../Minecraft.World/Util/StringHelpers.h"
#include "UIScene_SignEntryMenu.h"
#include "../../Minecraft.Client/Minecraft.h"
#include "../../Minecraft.Client/Player/MultiPlayerLocalPlayer.h"
@ -144,8 +145,9 @@ int UIScene_SignEntryMenu::KeyboardCompleteCallback(void* lpParam, bool bRes) {
uint16_t pchText[128];
ZeroMemory(pchText, 128 * sizeof(uint16_t));
InputManager.GetText(pchText);
pClass->m_textInputLines[pClass->m_iEditingLine].setLabel(
(wchar_t*)pchText);
std::wstring str = uint16_to_wstring(pchText);
str.resize(15);
pClass->m_textInputLines[pClass->m_iEditingLine].setLabel(str);
}
return 0;
}

View file

@ -36,6 +36,19 @@ bool equalsIgnoreCase(const std::wstring& a, const std::wstring& b) {
return out;
}
size_t uint16_len(const uint16_t* str) {
return std::char_traits<char16_t>::length(
reinterpret_cast<const char16_t*>(str));
}
std::u16string uint16_to_u16string(const uint16_t* str) {
return std::u16string(reinterpret_cast<const char16_t*>(str), uint16_len(str));
}
std::wstring uint16_to_wstring(const uint16_t* str) {
return u16string_to_wstring(uint16_to_u16string(str));
}
std::wstring convStringToWstring(const std::string& converting) {
std::wstring converted(converting.length(), L' ');
copy(converting.begin(), converting.end(), converted.begin());

View file

@ -29,6 +29,10 @@ T _fromHEXString(const std::wstring& s) {
return t;
}
size_t uint16_len(const uint16_t* str);
std::u16string uint16_to_u16string(const uint16_t* str);
std::wstring uint16_to_wstring(const uint16_t* str);
std::wstring convStringToWstring(const std::string& converting);
std::wstring u16string_to_wstring(const std::u16string& converting);
std::u16string wstring_to_u16string(const std::wstring& converting);