diff --git a/AddOns/!!!ClassicAPI/GlobalStrings.lua b/AddOns/!!!ClassicAPI/GlobalStrings.lua index 5be8ac40..9dac914a 100644 --- a/AddOns/!!!ClassicAPI/GlobalStrings.lua +++ b/AddOns/!!!ClassicAPI/GlobalStrings.lua @@ -1,6 +1,10 @@ +BINDING_NAME_FOCUSTARGET = "Focus Target"; +BINDING_NAME_TARGETFOCUS = "Target Focus"; +FOCUSTARGET = "Focus Target"; ITEMS_VARIABLE_QUANTITY = "%d |4Item:Items;"; ITEMS_EQUIPPED = "%d |4item:items; equipped"; ITEMS_IN_INVENTORY = "%d |4item:items; in inventory"; ITEM_SLOTS_IGNORED = "%d |4slot:slots; ignored"; ITEM_MISSING = "%s missing"; ITEMS_NOT_IN_INVENTORY = "%d |4item:items; missing"; +TARGETFOCUS = "Target Focus"; diff --git a/src/addons/Embedded.cpp b/src/addons/Embedded.cpp index d08a27b4..28076b8c 100644 --- a/src/addons/Embedded.cpp +++ b/src/addons/Embedded.cpp @@ -47,6 +47,7 @@ #include "Game.h" #include "Offsets.h" +#include "bindings/Inject.h" #include #include @@ -248,6 +249,14 @@ void DecideSource() { int __stdcall FileRead_h(int unused, const char *path, void **outBuf, size_t *outSize, size_t extraBytes, int flag1, int flag2) { + // FrameXML Bindings.xml gets an inline splice for our TARGETING + // additions — see `bindings/Inject.cpp`. Returns true with the + // patched buffer ready for the engine's binding parser to consume. + if (Bindings::Inject::TryHandle(unused, path, outBuf, outSize, + extraBytes, flag1, flag2, FileRead_o)) { + return 1; + } + const char *suffix = StripAddonPrefix(path); if (suffix == nullptr) { // Path isn't under `Interface\AddOns\!!!ClassicAPI\` — straight diff --git a/src/bindings/Inject.cpp b/src/bindings/Inject.cpp new file mode 100644 index 00000000..ad217452 --- /dev/null +++ b/src/bindings/Inject.cpp @@ -0,0 +1,159 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU Lesser 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License along with +// ClassicAPI. If not, see . + +// Inject ClassicAPI-owned bindings into the engine's TARGETING group. +// +// Why a file-read hook rather than an addon-side Bindings.xml: vanilla +// stores every binding in a single linearly-indexed table, and the +// keybind UI walks that table in index order with `HEADER_*` pseudo- +// entries splitting groups. Addon Bindings.xml files get parsed +// AFTER FrameXML, so even with `header="TARGETING"` an addon binding +// receives a display index higher than every engine binding — it +// renders orphaned at the very bottom of the keybind list rather than +// under TARGETING. +// +// To land inside TARGETING, the binding's display index has to fall +// between HEADER_TARGETING and HEADER_INTERFACE. The cleanest way to +// arrange that is to splice the bindings into the FrameXML file the +// engine itself parses, so they take their indices from the same +// running counter every native binding does. +// +// Splice site: just before `` +// elements assigning each the next display index from a running +// counter. By the time our injected bindings are processed, the +// engine has already emitted HEADER_TARGETING and a sequence of +// TARGETING entries; ours append to that sequence and HEADER_INTERFACE +// follows. + +#include "Inject.h" +#include "Offsets.h" + +#include + +namespace Bindings::Inject { + +namespace { + +constexpr const char *kFrameXMLBindingsPath = "Interface\\FrameXML\\Bindings.xml"; + +// First INTERFACE-section binding. Splicing immediately before this +// puts our entries at the tail of the TARGETING block. +constexpr const char *kSpliceMarker = "`). +constexpr const char *kInjectedXml = + "\n" + "\t\tFocusUnit(\"target\");\n" + "\t\n" + "\t\n" + "\t\tTargetUnit(\"focus\");\n" + "\t\n" + "\t"; + +char NormalizeChar(char c) { + if (c == '/') return '\\'; + if (c >= 'A' && c <= 'Z') return static_cast(c + 32); + return c; +} + +bool PathEqualsCI(const char *a, const char *b) { + while (*a != '\0' && *b != '\0') { + if (NormalizeChar(*a) != NormalizeChar(*b)) return false; + ++a; ++b; + } + return *a == *b; +} + +using SMemAlloc_t = void *(__stdcall *)(size_t size, const char *file, int line, int flags); +using SMemFree_t = void(__stdcall *)(void *buf, const char *file, int line, int flags); + +const char *FindMarker(const char *content, size_t size, const char *marker) { + const size_t markerLen = std::strlen(marker); + if (size < markerLen) return nullptr; + for (size_t i = 0; i + markerLen <= size; ++i) { + if (std::memcmp(content + i, marker, markerLen) == 0) + return content + i; + } + return nullptr; +} + +} // namespace + +bool TryHandle(int unused, const char *path, void **outBuf, size_t *outSize, + size_t extraBytes, int flag1, int flag2, FileReadFn original) { + if (path == nullptr || !PathEqualsCI(path, kFrameXMLBindingsPath)) + return false; + + // Pull the real file through the original reader with the caller's + // requested `extraBytes` honored — the original allocates a Storm + // buffer of `fileSize + extraBytes` and zero-fills the tail. + void *origBuf = nullptr; + size_t origSize = 0; + const int readOk = original(unused, path, &origBuf, &origSize, + extraBytes, flag1, flag2); + if (readOk == 0 || origBuf == nullptr) + return false; + + const char *content = static_cast(origBuf); + const char *splice = FindMarker(content, origSize, kSpliceMarker); + if (splice == nullptr) { + // Marker missing — likely a modified or non-vanilla FrameXML + // (Turtle patch level, custom build). Pass the original + // through unchanged rather than corrupting it. + if (outBuf != nullptr) *outBuf = origBuf; + if (outSize != nullptr) *outSize = origSize; + return true; + } + + const size_t injectLen = std::strlen(kInjectedXml); + const size_t splicePos = static_cast(splice - content); + const size_t newSize = origSize + injectLen; + + auto SMemAlloc = reinterpret_cast(Offsets::FUN_STORM_SMEM_ALLOC); + auto SMemFree = reinterpret_cast(Offsets::FUN_STORM_SMEM_FREE); + + void *newBuf = SMemAlloc(newSize + extraBytes, __FILE__, __LINE__, 0); + if (newBuf == nullptr) { + // Allocation failed — fall back to the original buffer so the + // engine still loads its own bindings. + if (outBuf != nullptr) *outBuf = origBuf; + if (outSize != nullptr) *outSize = origSize; + return true; + } + + auto *dest = static_cast(newBuf); + std::memcpy(dest, content, splicePos); + std::memcpy(dest + splicePos, kInjectedXml, injectLen); + std::memcpy(dest + splicePos + injectLen, + content + splicePos, origSize - splicePos); + if (extraBytes > 0) + std::memset(dest + newSize, 0, extraBytes); + + SMemFree(origBuf, __FILE__, __LINE__, 0); + + if (outBuf != nullptr) *outBuf = newBuf; + if (outSize != nullptr) *outSize = newSize; + return true; +} + +} // namespace Bindings::Inject diff --git a/src/bindings/Inject.h b/src/bindings/Inject.h new file mode 100644 index 00000000..0e7b5bf7 --- /dev/null +++ b/src/bindings/Inject.h @@ -0,0 +1,37 @@ +// This file is part of ClassicAPI. +// +// ClassicAPI is free software: you can redistribute it and/or modify it under the terms +// of the GNU Lesser 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License along with +// ClassicAPI. If not, see . + +#pragma once + +#include + +namespace Bindings::Inject { + +// `FUN_FILE_READ` shape — Storm-style: __stdcall, returns 1 on success +// with the buffer allocated via SMemAlloc into `*outBuf` (size in +// `*outSize`, plus `extraBytes` of trailing zero padding). +using FileReadFn = int(__stdcall *)(int unused, const char *path, + void **outBuf, size_t *outSize, + size_t extraBytes, + int flag1, int flag2); + +// Path-matches `Interface\FrameXML\Bindings.xml`. On match: fetches +// the real file via `original`, splices our ClassicAPI bindings into +// the TARGETING block, and returns `true` with `*outBuf` / `*outSize` +// set to a fresh Storm buffer the caller can SMemFree normally. +// Returns `false` for any other path — caller should fall through to +// its own logic. +bool TryHandle(int unused, const char *path, void **outBuf, size_t *outSize, + size_t extraBytes, int flag1, int flag2, FileReadFn original); + +} // namespace Bindings::Inject