From e88c6ead3b57032a0f1abf187c04781e4b3aeca3 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Fri, 9 Oct 2026 03:23:03 +0000 Subject: [PATCH] decomp(sdmemory): drawTransferTitles via MSL wcsstr overload and TextBox::SetTextColor (363->50) (worker 9b634499) --- config/43U/symbols.txt | 4 +- include/system/iplMessageManager.h | 2 +- include/system/iplSystem.h | 2 +- libs/MSL/include/wchar.h | 10 +++ libs/NW4R/include/nw4r/lyt/textBox.h | 10 ++- src/scene/letterWriter/iplLetterWriter.cpp | 3 +- src/scene/sdChannelMemory/iplSDMemory.cpp | 74 +++++----------------- tools/decomp-assist/opus-sdmem.attempts.md | 53 ++++++++++++++++ 8 files changed, 90 insertions(+), 68 deletions(-) create mode 100644 tools/decomp-assist/opus-sdmem.attempts.md diff --git a/config/43U/symbols.txt b/config/43U/symbols.txt index 30e3d5bf..becb009a 100644 --- a/config/43U/symbols.txt +++ b/config/43U/symbols.txt @@ -4900,8 +4900,8 @@ onDialogState23__Q33ipl5scene8SDMemoryFv = .text:0x813EEDF8; // type:function si onDialogState24__Q33ipl5scene8SDMemoryFv = .text:0x813EEE70; // type:function size:0xC0 draw__Q33ipl5scene8SDMemoryFv = .text:0x813EEF30; // type:function size:0x88 drawTransferTitles__Q33ipl5scene8SDMemoryFv = .text:0x813EEFB8; // type:function size:0x70C -writeFourFlagBytes__Q23ipl5sceneFPUcUcUcUcUc = .text:0x813EF6C4; // type:function size:0x14 -setTitleRowColors__Q23ipl5sceneFPQ34nw4r3lyt7TextBoxRCQ34nw4r2ut5ColorRCQ34nw4r2ut5Color = .text:0x813EF6D8; // type:function size:0x48 +__ct__Q34nw4r2ut5ColorFiiii = .text:0x813EF6C4; // type:function size:0x14 +SetTextColor__Q34nw4r3lyt7TextBoxFQ34nw4r2ut5ColorQ34nw4r2ut5Color = .text:0x813EF6D8; // type:function size:0x48 GetBeginIter__Q34nw4r2ut28LinkListFv = .text:0x813EF720; // type:function size:0x8 GetEndIter__Q34nw4r2ut28LinkListFv = .text:0x813EF728; // type:function size:0x8 __ne__Q24nw4r2utFQ44nw4r2ut28LinkList8IteratorQ44nw4r2ut28LinkList8Iterator = .text:0x813EF730; // type:function size:0x18 diff --git a/include/system/iplMessageManager.h b/include/system/iplMessageManager.h index de7b7ff2..20d119f2 100644 --- a/include/system/iplMessageManager.h +++ b/include/system/iplMessageManager.h @@ -15,7 +15,7 @@ namespace ipl { Manager(EGG::Heap* heap); /** @return The message data in use. */ - const wchar_t* getMessage(u32 id) const { return mpMessage->getMessage(id); } + wchar_t* getMessage(u32 id) const { return mpMessage->getMessage(id); } void setResource(u8* msgData) const { mpMessage->setResource(msgData); } private: diff --git a/include/system/iplSystem.h b/include/system/iplSystem.h index 21f90d48..fd41f2c8 100644 --- a/include/system/iplSystem.h +++ b/include/system/iplSystem.h @@ -302,7 +302,7 @@ namespace ipl { } /** @return The Message ID as a Wide-string. */ - static const wchar_t* getMessage(u32 id) { return smArg.mpMessageMgr->getMessage(id); } + static wchar_t* getMessage(u32 id) { return smArg.mpMessageMgr->getMessage(id); } static message::Manager* getMessageManager() { return smArg.mpMessageMgr; } static void* getFont(); diff --git a/libs/MSL/include/wchar.h b/libs/MSL/include/wchar.h index 698ef700..2d2dc775 100644 --- a/libs/MSL/include/wchar.h +++ b/libs/MSL/include/wchar.h @@ -27,7 +27,11 @@ wchar_t* wcscat(wchar_t* dest, const wchar_t* src); wchar_t* wcsncat(wchar_t* dest, const wchar_t* src, size_t num); int wcsncmp(const wchar_t* s1, const wchar_t* s2, size_t num); wchar_t* wcschr(wchar_t* ws, wchar_t wc); +#ifndef __cplusplus wchar_t* wcsstr(const wchar_t* s1, const wchar_t* s2); +#else +const wchar_t* wcsstr(const wchar_t* s1, const wchar_t* s2); +#endif int swprintf(wchar_t* ws, size_t len, const wchar_t* format, ...); int vswprintf(wchar_t* ws, size_t len, const wchar_t* format, va_list arg); @@ -36,6 +40,12 @@ int wcsnicmp(const char* dst, const char* src, size_t num); #ifdef __cplusplus } + +extern "C++" { +inline wchar_t* wcsstr(wchar_t* s1, const wchar_t* s2) { + return const_cast(wcsstr(static_cast(s1), s2)); +} +} #endif #endif // MSL_WCHAR_H diff --git a/libs/NW4R/include/nw4r/lyt/textBox.h b/libs/NW4R/include/nw4r/lyt/textBox.h index 61be7d8e..84a9f67d 100644 --- a/libs/NW4R/include/nw4r/lyt/textBox.h +++ b/libs/NW4R/include/nw4r/lyt/textBox.h @@ -55,16 +55,14 @@ namespace nw4r { NW4R_ASSERT(type < TEXTCOLOR_MAX); return mTextColors[type]; } -#ifdef IPL_SDMEMORY_SET_TEXT_COLORS - void SetTextColors(const ut::Color& first, const ut::Color& second) { - mTextColors[0] = first; - mTextColors[1] = second; - } -#endif void SetTextColor(u32 type, ut::Color value) { NW4R_ASSERT(type < TEXTCOLOR_MAX); mTextColors[type] = value; } + void SetTextColor(ut::Color top, ut::Color bottom) { + mTextColors[0] = top; + mTextColors[1] = bottom; + } void SetTextPositionH(u8 pos) { detail::SetHorizontalPosition(&mTextPosition, pos); } void SetTextPositionV(u8 pos) { detail::SetVerticalPosition(&mTextPosition, pos); } diff --git a/src/scene/letterWriter/iplLetterWriter.cpp b/src/scene/letterWriter/iplLetterWriter.cpp index bcaa14c4..7826f130 100644 --- a/src/scene/letterWriter/iplLetterWriter.cpp +++ b/src/scene/letterWriter/iplLetterWriter.cpp @@ -194,7 +194,8 @@ namespace ipl { mpCaptionString->clear(); mpCaptionString->set(System::getMessage(MESG_LETTERWRITER_LETTER_HEADER)); - wchar_t* namePlaceholder = wcsstr(mpCaptionString->getWCString(), L"xxxxxxxxxx"); + wchar_t* caption = mpCaptionString->getWCString(); + wchar_t* namePlaceholder = wcsstr(caption, L"xxxxxxxxxx"); mpCaptionString->replace((namePlaceholder - mpCaptionString->getWCString()), RFL_NAME_LENGTH, headerCaption); return mpCaptionString->getWCString(); diff --git a/src/scene/sdChannelMemory/iplSDMemory.cpp b/src/scene/sdChannelMemory/iplSDMemory.cpp index 06c812ee..b24d3b74 100644 --- a/src/scene/sdChannelMemory/iplSDMemory.cpp +++ b/src/scene/sdChannelMemory/iplSDMemory.cpp @@ -7,13 +7,11 @@ #define IPL_SDMEMORY_SD_CHANNEL_SELECT_LAYOUT #define IPL_SDMEMORY_COMPLETION_PCT_ACCESSOR #define IPL_SDMEMORY_DIALOG_PROGRESS_FRAME_ACCESSOR -#define IPL_SDMEMORY_SET_TEXT_COLORS #define IPL_SDMEMORY_SCROLLER_STATE_ACCESSOR #include "system/iplDialogWindow.h" #undef IPL_SDMEMORY_DIALOG_PROGRESS_FRAME_ACCESSOR #include "scene/sdChannelMemory/iplSDMemory.h" #include "scene/sdChannelSelect/iplSDChannelSelect.h" -#undef IPL_SDMEMORY_SET_TEXT_COLORS #undef IPL_SDMEMORY_SCROLLER_STATE_ACCESSOR #undef IPL_SDMEMORY_DIALOG_STATE_ACCESSOR #undef IPL_SDMEMORY_SET_TRANSLATE_OUT_OF_LINE @@ -51,11 +49,6 @@ namespace ipl { typedef ::gui::PaneComponent GuiPaneComponent; typedef controller::Interface ControllerInterface; - void writeFourFlagBytes(u8* flags, u8 first, u8 second, u8 third, u8 fourth); - - void setTitleRowColors(nw4r::lyt::TextBox* textBox, const nw4r::ut::Color& first, - const nw4r::ut::Color& second) NO_INLINE; - static const char* sControlPaneNames[] = { "A", "B", "B_BtnA", }; @@ -1269,10 +1262,6 @@ namespace ipl { } } - static inline void setChildrenAlpha(nw4r::lyt::Pane& pane, const u8 alpha); - -#pragma push -#pragma ppc_iro_level 1 void SDMemory::drawTransferTitles() { f32 rowY; f32 rowTop; @@ -1291,12 +1280,12 @@ namespace ipl { u32 titleIndex; s32 visibleRows; s32 row; - const wchar_t* messageForCount; + wchar_t* messageForCount; s32 lineCount; - const wchar_t* messageLine; + wchar_t* messageLine; s32 totalLines; s32 lineIndex; - const wchar_t* lineEnd; + wchar_t* lineEnd; u32 lineLength; nw4r::lyt::TextBox* messageText; const nw4r::math::VEC3& translation = mpDialogLayout->FindPaneByName("N_Memo")->GetTranslate(); @@ -1324,12 +1313,10 @@ namespace ipl { if (mNandTitleCount != 0) { messageForCount = System::getMessage(0xCB); lineCount = 0; - const wchar_t* newline = wcsstr(messageForCount, L"\n"); - if (newline != NULL) { - do { - ++lineCount; - newline = wcsstr(newline + 1, L"\n"); - } while (newline != NULL); + wchar_t* newline = wcsstr(messageForCount, L"\n"); + while (newline != NULL) { + ++lineCount; + newline = wcsstr(newline + 1, L"\n"); } messageLine = System::getMessage(0xCB); @@ -1366,7 +1353,11 @@ namespace ipl { titleSizePane = mpDialogLayout->FindPaneByName("N_Body"); rowHeight = titleSizePane->GetSize().height; - setChildrenAlpha(*titleSizePane, alpha); + nw4r::lyt::PaneList::Iterator bodyChild = titleSizePane->GetChildList().GetBeginIter(); + while (bodyChild != titleSizePane->GetChildList().GetEndIter()) { + bodyChild->SetAlpha(alpha); + ++bodyChild; + } titleText = static_cast( mpDialogLayout->FindPaneByName("T_Letter")); @@ -1376,22 +1367,12 @@ namespace ipl { for (titleIndex = 0; titleIndex < mTitleCount; ++titleIndex) { utility::layout::set_string(titleText, mTitleNames[titleIndex]); if (nandTitleIndex < mNandTitleCount && mTitleIds[titleIndex] == mNandTitleIds[nandTitleIndex]) { - GXColor gxActive; ++nandTitleIndex; - writeFourFlagBytes(&gxActive.r, 0x34, 0xBE, 0xED, 0xFF); - GXColor activeColor = gxActive; - GXColor activeTop = activeColor; - GXColor activeBottom = activeColor; - setTitleRowColors(titleText, *reinterpret_cast(&activeTop), - *reinterpret_cast(&activeBottom)); + nw4r::ut::Color color(0x34, 0xBE, 0xED, 0xFF); + titleText->SetTextColor(color, color); } else { - GXColor gxInactive; - writeFourFlagBytes(&gxInactive.r, 0x64, 0x64, 0x64, 0xFF); - GXColor inactiveColor = gxInactive; - GXColor inactiveTop = inactiveColor; - GXColor inactiveBottom = inactiveColor; - setTitleRowColors(titleText, *reinterpret_cast(&inactiveTop), - *reinterpret_cast(&inactiveBottom)); + nw4r::ut::Color color(0x64, 0x64, 0x64, 0xFF); + titleText->SetTextColor(color, color); } nw4r::ut::Rect textRect = mpDialogLayout->getTextDrawRect("T_Letter"); @@ -1413,7 +1394,7 @@ namespace ipl { titleText->CalculateMtx(*mpDialogLayout->getDrawInfo()); mpDialogLayout->draw(titleText); } - titleOffset -= rowHeight * static_cast(visibleRows); + titleOffset -= visibleRows * rowHeight; } backgroundOffset += rowHeight; @@ -1435,27 +1416,6 @@ namespace ipl { mpDialogLayout->draw("N_Back"); mpDialogLayout->draw("N_Move"); } -#pragma pop - - static inline void setChildrenAlpha(nw4r::lyt::Pane& pane, const u8 alpha) { - nw4r::lyt::PaneList::Iterator child = pane.GetChildList().GetBeginIter(); - while (child != pane.GetChildList().GetEndIter()) { - child->SetAlpha(alpha); - ++child; - } - } - - void writeFourFlagBytes(u8* flags, u8 first, u8 second, u8 third, u8 fourth) { - flags[0] = first; - flags[1] = second; - flags[2] = third; - flags[3] = fourth; - } - - void setTitleRowColors(nw4r::lyt::TextBox* textBox, const nw4r::ut::Color& first, - const nw4r::ut::Color& second) NO_INLINE { - textBox->SetTextColors(first, second); - } s32 SDMemory::getControlPaneIndex(const char* paneName) { s32 paneIndex = -1; diff --git a/tools/decomp-assist/opus-sdmem.attempts.md b/tools/decomp-assist/opus-sdmem.attempts.md new file mode 100644 index 00000000..9d33065a --- /dev/null +++ b/tools/decomp-assist/opus-sdmem.attempts.md @@ -0,0 +1,53 @@ +# opus-sdmem attempts: SDMemory::drawTransferTitles + +Unit `src/scene/sdChannelMemory/iplSDMemory`, branch `agent/w1009/o-sdmem` from `72e9c8d6`. +Start: 363/451 differing, 447/451 instructions, 97.4102%. combosweep: no setting beats 363. + +## Header loops: MSL's C++ `wcsstr` overload + +The target's two guarded loops (`cmpwi; beq` / `addic.; ble`, then a preheader with +`li rX, "\n"@sda21`, then `b test`) come from MWCC's `IRO_PullOutInvariantAssignments` +(string `IRO_CopyLoopTestToLoopPreheaderAndBranchToSuccNode` in mwcceppc.exe). The pass +needs a loop-invariant *assignment* inside the loop. A plain `wcsstr(p, L"\n")` has none, +so no loop form, pragma, IRO level or `-O4,p` reproduces it in isolated tests. + +Real MSL (`MSL_C/wstring.h`, see RushRE/SonicRushAdventure-Decomp tools/cw/include) declares, +for non-embedded C++, `const wchar_t* wcsstr(const wchar_t*, const wchar_t*)` plus +`inline wchar_t* wcsstr(wchar_t* s1, const wchar_t* s2)` calling the const one. Calling it +with a `wchar_t*` inlines the wrapper; its parameter assignment `s2 = L"\n"` is the +invariant assignment the IRO pass pulls into a guarded preheader. Isolated test reproduces +the target sequence instruction for instruction. + +`message::Message::getMessage` already returns `wchar_t*`; the `Manager`/`System` wrappers +now return `wchar_t*` too, so message pointers in this function are non-const and use the +overload. The scoped `ppc_iro_level 1` is removed (it suppresses the pass) and the newline +counter is a plain `while` loop. + +Result: 88/451 differing, 451/451 instructions, 99.28603%. + +Side effect: `LetterWriter::makeHeaderCaption` passed a call result straight to `wcsstr`; +with the overload the literal is evaluated before the virtual call (100 -> 94%). A named +`caption` local restores the target order (three spellings tested, all exact). Full report +otherwise unchanged vs `72e9c8d6`, DOL SHA1 unchanged. + +## Title colors: real NW4R `TextBox::SetTextColor(ut::Color top, ut::Color bottom)` + +The two helpers after drawTransferTitles were invented names (`iplSDMemory_813EF6C4/6D8` +before #254). Their bodies and placement fit weak header inlines that this large function +did not inline: `nw4r::ut::Color::Color(int, int, int, int)` (four `stb`) and NW4R's +`TextBox::SetTextColor(ut::Color top, ut::Color bottom)` (by-value colors, two calls to the +weak `Color::operator=`; same inline in hotlandsoftware/wii-news-channel, declared in Petari). +By-value arguments explain the retail stack: the named color sits with the other named +locals, the two argument copies are temporaries in source order, the snapshot comes last. +`textBox.h` now has that overload instead of the `IPL_SDMEMORY_SET_TEXT_COLORS` hack, the +source is `nw4r::ut::Color color(...); titleText->SetTextColor(color, color);`, and +symbols.txt names the two functions `__ct__Q34nw4r2ut5ColorFiiii` and +`SetTextColor__Q34nw4r3lyt7TextBoxFQ34nw4r2ut5ColorQ34nw4r2ut5Color` (both 100%). + +The N_Body alpha loop is written in place like the header/footer loops: through the +`setChildrenAlpha` inline its iterator temporaries were allocated after the footer's +(inline expansion runs after parsing), retail has them in source order. +`titleOffset -= visibleRows * rowHeight` fixes the `fmuls` operand order. + +Result: 50/451 differing, register-blind identical apart from the renamed call targets, +99.39024%. Pool and .data/.sdata/.sdata2 identical; full report unchanged otherwise; DOL OK. -- 2.49.1