Files
ClassicAPI/src/frame/DontSavePosition.cpp
T
Brues b49c107778 refactor(lua): take frame-method tables as arrays, not pointer and count
Every RegisterFrameMethods call passed its method table and doc table
with a count beside each: sizeof(t) / sizeof(t[0]), std::size, or a
literal. The literals had to be kept in step with the tables by hand.

Game::ArrayRef<T> is a view over a file-scope array that takes the
count from the array's type, the way Doc::FieldList already did for a
function's arguments and returns. FieldList is now ArrayRef<Field>,
Doc::MethodList is ArrayRef<Method>, and RegisterFrameMethods takes
(context, methods, docs). The table and count still go to the engine's
frame-method iterator (FUN_REGISTER_FRAME_METHODS) unchanged, and to
the documentation recorder.

All 84 call sites pass only the arrays. The 16 literal counts were
checked against their tables first: each was the full length, so every
registry gets the same methods as before.
2026-11-06 11:27:22 -06:00

124 lines
4.7 KiB
C++

// This file is part of ClassicAPI.
//
// ClassicAPI is free software: you can redistribute it and/or modify it under the terms
// of the GNU General Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// ClassicAPI. If not, see <https://www.gnu.org/licenses/>.
// `frame:SetDontSavePosition([dontSave])` and `frame:GetDontSavePosition()`.
//
// The client saves the position and size of every named, user-placed,
// movable or resizable frame to layout-cache.txt, and puts the frame back
// there at the next login. An addon that saves its own frame's position has
// to fight that copy. 4.3.4 lets it opt out: SetDontSavePosition sets frame
// flag 0x80000, and the layout-cache writer (PlacedFrameCallback) skips a
// frame that has it. Nothing else reads the flag; the reader still applies
// a record already in the file.
//
// 1.12's writer calls FUN_LAYOUT_CACHE_SAVE_FRAME on each frame, and that
// function makes the same name / user-placed / movable test. The hook adds
// 4.3.4's flag test in front of it. XML sets the flag with the frame attribute
// `dontSavePosition`.
#include "Game.h"
#include "Offsets.h"
#include "xml/FrameLoad.h"
#include <cstdint>
namespace Frame::DontSavePosition {
namespace {
using SaveFrame_t = void(__fastcall *)(void *frame, void *file);
using SetFrameFlag_t = void(__fastcall *)(void *frame, void *edx, uint32_t mask, int on);
SaveFrame_t g_origSaveFrame = nullptr;
bool DontSave(void *frame) {
return (Game::Read<uint32_t>(frame, Offsets::OFF_FRAME_FLAGS) & Offsets::FRAME_FLAG_DONT_SAVE_POSITION) != 0;
}
void __fastcall SaveFrame_h(void *frame, void *file) {
if (DontSave(frame))
return;
g_origSaveFrame(frame, file);
}
const Game::HookAutoRegister _hook{Offsets::FUN_LAYOUT_CACHE_SAVE_FRAME, reinterpret_cast<void *>(&SaveFrame_h),
reinterpret_cast<void **>(&g_origSaveFrame)};
// 4.3.4 reads the flag with StringToBOOL(L, 2, 1): no argument sets it.
int __fastcall Script_SetDontSavePosition(void *L) {
void *frame = Game::Lua::ResolveFrame(L);
if (frame == nullptr)
return 0;
const bool on = Game::Lua::ToFlag(L, 2, true);
reinterpret_cast<SetFrameFlag_t>(Offsets::FUN_FRAME_SET_FRAME_FLAG)(
frame, nullptr, Offsets::FRAME_FLAG_DONT_SAVE_POSITION, on);
return 0;
}
// 4.3.4's frame loader sets the flag only when the node has the attribute, so
// a template's value stays unless the element names its own.
void OnFrameNode(void *object, const void *node, void * /*status*/) {
const char *value = Xml::Attr(node, "dontSavePosition");
if (value == nullptr || *value == '\0')
return;
reinterpret_cast<SetFrameFlag_t>(Offsets::FUN_FRAME_SET_FRAME_FLAG)(
object, nullptr, Offsets::FRAME_FLAG_DONT_SAVE_POSITION, Game::StringToFlag(value));
}
const Xml::FrameLoad::AutoSubscribe _xmlSub{&OnFrameNode};
// 4.3.4 pushes 1 or nil.
int __fastcall Script_GetDontSavePosition(void *L) {
void *frame = Game::Lua::ResolveFrame(L);
if (frame == nullptr)
return 0;
if (DontSave(frame))
Game::Lua::PushNumber(L, 1.0);
else
Game::Lua::PushNil(L);
return 1;
}
const Game::Lua::FrameMethodEntry g_methods[] = {
{"SetDontSavePosition", &Script_SetDontSavePosition},
{"GetDontSavePosition", &Script_GetDontSavePosition},
};
const Game::Doc::Field kSetArgs[] = {
Game::Doc::Opt("dontSave", "boolean", "true",
"When true, the client does not save the frame's position and size to layout-cache.txt."),
};
const Game::Doc::Field kGetRets[] = {
Game::Doc::Opt("dontSave", "number", nullptr, "1 when the frame's position is not saved, else nil."),
};
const Game::Doc::Function kSetDontSavePosition{
"Sets whether the client leaves the frame out of the saved frame layout.", kSetArgs, {}};
const Game::Doc::Function kGetDontSavePosition{
"Returns whether the client leaves the frame out of the saved frame layout.", {}, kGetRets};
const Game::Doc::Method g_methodDocs[] = {
{"SetDontSavePosition", &kSetDontSavePosition},
{"GetDontSavePosition", &kGetDontSavePosition},
};
void RegisterLuaFunctions() {
Game::Lua::RegisterFrameMethods(
reinterpret_cast<void *>(Offsets::VAR_FRAME_METHOD_REGISTRY), g_methods, g_methodDocs);
}
const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace
} // namespace Frame::DontSavePosition