diff --git a/src/Offsets.h b/src/Offsets.h index 4079064d..7108d688 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -1063,6 +1063,7 @@ enum Offsets { OFF_SMF_LINE_ARRAY = 0x3A4, // base of the stride-0x10 line-entry array SMF_LINE_STRIDE = 0x10, // per-line-entry stride OFF_SMF_LINE_FONTSTRING = 0x8, // entry+8 = the line's CSimpleFontString + OFF_SMF_LINE_RECORD = 0xC, // entry+0xC = the ring record it shows (ShowLine 0x00788AF0) // THE pen↔anchor unit bridge. RebuildString (0x7724A0) multiplies every // text-unit quantity by region+0x7C when crossing into node creation: // nodePos = inset×s + rect corner, nodeFontH = fontPx×s, spacing = +0xF4×s, @@ -13324,6 +13325,17 @@ enum Offsets { // The record the lines are laid out from (4.3.4's m_currentScroll); // FUN_SMF_DISPLAY_REFRESH stores its argument here. OFF_SCROLLINGMESSAGEFRAME_SCROLL_RECORD = 0x348, + // The lines the last layout of a shown frame laid out (4.3.4's + // m_numDisplayed): FUN_SMF_DISPLAY_REFRESH zeroes it and counts each line + // it places, and the fader (0x00788460), RearmLines (0x00788B80) and the + // link-button rebuild (0x00788C00) walk display nodes 0..this-1, each + // through its record pointer (OFF_SMF_LINE_RECORD). + OFF_SCROLLINGMESSAGEFRAME_LINES_SHOWN = 0x34C, + // The top flag (4.3.4's m_atTop): the layout sets it when the lines it + // placed reach the oldest message (0x00788A37). Clear (0x007882B0) zeroes + // it, and a layout of an empty frame returns before it, so it is only + // meaningful while the frame has messages. + OFF_SCROLLINGMESSAGEFRAME_AT_TOP = 0x350, // A ring that is not full holds records 0..count-1: the writer fills // them in order from an empty ring (Clear: position -1), and only a full // ring wraps. ScrollOffset and ScrollToOffset (below) wrap by the count, diff --git a/src/scrollingmessageframe/MessageInfo.cpp b/src/scrollingmessageframe/MessageInfo.cpp index ee0add76..694679f9 100644 --- a/src/scrollingmessageframe/MessageInfo.cpp +++ b/src/scrollingmessageframe/MessageInfo.cpp @@ -57,8 +57,10 @@ #include "Offsets.h" #include "scrollingmessageframe/DisplayRefreshed.h" +#include #include #include +#include #include namespace ScrollingMessageFrame::MessageInfo { @@ -131,16 +133,22 @@ int ExtraCount(void *L, int extras) { // too (Clear and the writer leave a free slot's old text for the writer to // free when it reuses the slot). The lines still show the records they were // laid out from; the caller lays them out again. Self at stack slot 1. +// +// Both move in place. The records rotate as whole 0x18-byte values, and the +// extra-argument tables follow each cycle of the rotation, every one moved +// once, so a call makes no garbage table. void RotateRecords(void *L, void *frame, int span, int by) { using namespace Game::Lua; - by %= span; - if (span <= 1 || by == 0) + struct Record { + uint8_t bytes[Offsets::SCROLLINGMESSAGEFRAME_MESSAGE_STRIDE]; + }; + if (span <= 1) return; - constexpr size_t kStride = Offsets::SCROLLINGMESSAGEFRAME_MESSAGE_STRIDE; - uint8_t *records = Game::Read(frame, Offsets::OFF_SCROLLINGMESSAGEFRAME_MESSAGES); - const std::vector was(records, records + span * kStride); - for (int i = 0; i < span; ++i) - std::memcpy(records + i * kStride, was.data() + ((i + by) % span) * kStride, kStride); + by %= span; + if (by == 0) + return; + auto *records = Game::Read(frame, Offsets::OFF_SCROLLINGMESSAGEFRAME_MESSAGES); + std::rotate(records, records + by, records + span); int &scroll = Game::Ref(frame, Offsets::OFF_SCROLLINGMESSAGEFRAME_SCROLL_RECORD); if (scroll >= 0 && scroll < span) scroll = (scroll - by + span) % span; @@ -148,17 +156,21 @@ void RotateRecords(void *L, void *frame, int span, int by) { PushRecords(L, false); const int table = GetTop(L); if (Type(L, table) == TYPE_TABLE) { - NewTable(L); - const int moved = GetTop(L); - for (int i = 0; i < span; ++i) { + const int cycles = std::gcd(span, by); + for (int start = 0; start < cycles; ++start) { + RawGetI(L, table, start); // the cycle's first table, until its slot comes round + const int held = GetTop(L); + int i = start; + for (int next = (i + by) % span; next != start; next = (i + by) % span) { + PushNumber(L, i); + RawGetI(L, table, next); + RawSet(L, table); // records[i] = records[next] + i = next; + } PushNumber(L, i); - RawGetI(L, table, (i + by) % span); - RawSet(L, moved); - } - for (int i = 0; i < span; ++i) { - PushNumber(L, i); - RawGetI(L, moved, i); + PushValue(L, held); RawSet(L, table); + SetTop(L, held - 1); } } SetTop(L, table - 1); @@ -241,6 +253,16 @@ uint32_t MessageColor(void *L, void *frame) { // as the writer does, which shows the new line when there is room above the // others; scrolled up from the scroll record, which moved with its message, // so the view stays. +// +// A shown frame whose lines do not reach the oldest message (its top flag +// clear) cannot show the new one, so that layout would place the same lines +// again: the same texts, the same scroll record, no scroll script. Retail +// only marks its display dirty. The layout is skipped there, and the one +// thing it would change, each laid-out node's record pointer (the fader, +// RearmLines and the link-button rebuild read the record through it), moves +// one record up with the message it shows. The top flag is only kept while +// the frame has messages and is shown, so an empty or hidden frame is laid +// out as before (a hidden frame's layout places nothing). int __fastcall Script_BackFillMessage(void *L) { using namespace Game::Lua; using StormAlloc_t = void *(__stdcall *)(size_t size, const char *file, int line, int flags); @@ -267,6 +289,8 @@ int __fastcall Script_BackFillMessage(void *L) { return 0; std::memcpy(copy, text, bytes); + const bool offScreen = count > 0 && Game::Read(frame, Offsets::OFF_FRAME_VISIBLE) != 0 && + Game::Read(frame, Offsets::OFF_SCROLLINGMESSAGEFRAME_AT_TOP) == 0; RotateRecords(L, frame, count + 1, count); ++count; int &pos = Game::Ref(frame, Offsets::OFF_SCROLLINGMESSAGEFRAME_MESSAGE_POS); @@ -285,6 +309,15 @@ int __fastcall Script_BackFillMessage(void *L) { *reinterpret_cast(record + Offsets::OFF_SCROLLINGMESSAGEFRAME_MESSAGE_ID) = 0; StoreExtras(L, 0, top); + if (offScreen) { + const int shown = Game::Read(frame, Offsets::OFF_SCROLLINGMESSAGEFRAME_LINES_SHOWN); + auto *lines = Game::Read(frame, Offsets::OFF_SMF_LINE_ARRAY); + for (int i = 0; i < shown; ++i) + Game::Ref(lines + i * Offsets::SMF_LINE_STRIDE, Offsets::OFF_SMF_LINE_RECORD) += + Offsets::SCROLLINGMESSAGEFRAME_MESSAGE_STRIDE; + DisplayRefreshed::MarkDirty(frame); + return 0; + } reinterpret_cast(Offsets::FUN_SMF_DISPLAY_REFRESH)( frame, nullptr, Game::Read(frame, Offsets::OFF_SCROLLINGMESSAGEFRAME_AT_BOTTOM) != 0