Files
ClassicAPI/src/editbox/Ime.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

97 lines
3.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/>.
// Input-method composition: `editBox:IsInIMECompositionMode()` and the
// `OnCharComposition(self, text)` script.
//
// 1.12's edit box already handles input-method events the way 4.3.4's
// OnLayerIme does, and keeps the same composition flag
// (OFF_EDITBOX_IME_COMPOSITION), so IsInIMECompositionMode reads it as
// 4.3.4's does: 1 or nil.
//
// When the composition string changes, both clients insert it into the box;
// 4.3.4 then runs OnCharComposition with that string. 1.12 has no such
// script. It is an EditBox Frame::ExtraScripts script, and the input-method
// slot's one insert call is redirected to a function that makes the same
// insert and then runs the script.
#include "Common.h"
#include "Game.h"
#include "Offsets.h"
#include "frame/ExtraScripts.h"
#include "frame/ScriptArgs.h"
#include <cstdint>
namespace EditBox::Ime {
namespace {
const Frame::ExtraScripts::Script g_onCharComposition{"oncharcomposition", Offsets::VAR_EDITBOX_LUA_TYPE_ID};
using Insert_t = void(__fastcall *)(void *editBox, void *edx, const char *text, const char *context, int runScripts,
int highlight);
void __fastcall CompositionInsert(void *editBox, void *edx, const char *text, const char *context, int runScripts,
int highlight) {
reinterpret_cast<Insert_t>(Offsets::FUN_EDITBOX_INSERT)(editBox, edx, text, context, runScripts, highlight);
const uint32_t *slot = g_onCharComposition.Slot(editBox);
if (slot == nullptr || slot[0] == 0)
return;
void *L = Game::Lua::State();
if (L == nullptr)
return;
const int top = Game::Lua::GetTop(L);
Game::Lua::PushString(L, text);
Frame::ScriptArgs::RunSlotWithValues(L, slot, editBox, top + 1, 1);
Game::Lua::SetTop(L, top);
}
int __fastcall Script_IsInIMECompositionMode(void *L) {
void *editBox = Game::Lua::ResolveEditBox(L);
if (editBox == nullptr)
return 0;
if (Game::Read<int>(editBox, Offsets::OFF_EDITBOX_IME_COMPOSITION) != 0)
Game::Lua::PushNumber(L, 1.0);
else
Game::Lua::PushNil(L);
return 1;
}
const Game::Lua::FrameMethodEntry g_methods[] = {
{"IsInIMECompositionMode", &Script_IsInIMECompositionMode},
};
const Game::Doc::Field kRets[] = {
Game::Doc::Opt("composing", "number", nullptr, "1 while an input method composes text in the box, else nil."),
};
const Game::Doc::Function kIsInIMECompositionMode{
"Returns whether an input method is composing text in the edit box.", {}, kRets};
const Game::Doc::Method g_methodDocs[] = {
{"IsInIMECompositionMode", &kIsInIMECompositionMode},
};
void RegisterLuaFunctions() {
Common::RedirectCall(Offsets::CALL_EDITBOX_IME_COMPOSITION_INSERT, Offsets::FUN_EDITBOX_INSERT,
reinterpret_cast<const void *>(&CompositionInsert));
Game::Lua::RegisterFrameMethods(
reinterpret_cast<void *>(Offsets::VAR_EDITBOX_METHOD_REGISTRY), g_methods, g_methodDocs);
}
const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
} // namespace
} // namespace EditBox::Ime