mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-16 08:00:42 +00:00
FUN_TOKEN_TO_GUID (0x00515970) is a pure parser: it returns 0 for any input it doesn't recognize and never raises -- verified from the disassembly (plain stack frame, no assert/SEH prologue; every unknown-input path is xor eax,eax; ret). Both call sites had mirrored the engine's token grammar by hand to pre-screen input, on the mistaken belief this function throws (it was conflated with its sibling FUN_RESOLVE_UNIT_TOKEN 0x00515940, which does throw). - item/Use.cpp UseItemByName: delete LooksLikeToken / MatchBase / SkipTargetSuffixes / HasPrefix; just call FUN_TOKEN_TO_GUID and let it return 0 for non-tokens. - unit/Flags.cpp UnitIsInMyGuild: delete LooksLikeKnownToken / HasPrefix / AllDigits. Resolve the input to a GUID via FUN_TOKEN_TO_GUID, then resolve GUID -> object via the non-throwing FUN_OBJECT_RESOLVE_BY_GUID (unit|player mask) for the fast guild-key compare, and GUID -> name via the NameCache for the roster fallback. This avoids the throwing resolver entirely, and -- because the token->GUID step goes through our TokenExtensions hook -- now supports focus / nameplateN, which the old grammar mirror silently treated as literal names. Matches what 3.3.5's Script_UnitIsInMyGuild does (resolve to a GUID first). No grammar to keep in sync with the engine anymore. Behavior otherwise unchanged (self, guildmate/other-guild target, NPC, OOR raid member, literal name, empty slot all verified case-by-case).
96 lines
3.7 KiB
C++
96 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/>.
|
|
|
|
// `C_Item.UseItemByName(itemInfo [, unit])` — finds the first item in
|
|
// the player's bags matching `itemInfo` (itemID, link, or name) and
|
|
// uses it via the engine's `CGItem::UseItem` primitive. Optional
|
|
// `unit` is a unit token (`"player"`, `"target"`, `"focus"`, …)
|
|
// passed to spell-cast items (scrolls, traps, on-use targeted
|
|
// effects) as the cast target.
|
|
//
|
|
// Same structure 3.3.5's `Script_UseItemByName` uses: locate the item
|
|
// directly, then hand it to the engine's by-item-pointer use call.
|
|
// We skip `Script_UseContainerItem` — that function is a state-machine
|
|
// dispatcher for cursor modes (repair vendor, spell-cast targeting,
|
|
// drop-on-bag, ...) that don't apply to an addon-issued call from a
|
|
// clean cursor state. The default-path primitive `FUN_ITEM_USE`
|
|
// (`0x005D8D00`) handles every item-type-specific branch internally:
|
|
// food / potion / hearthstone / scroll / quiver all route through
|
|
// the same entry.
|
|
//
|
|
// Target arg: `FUN_ITEM_USE`'s `param_1` is read as a `uint64_t *`
|
|
// pointing at the target's GUID. The engine writes it through to the
|
|
// "use item" event (`DAT_00c4c1d0`/`DAT_00c4c1d4` + event 0x122) for
|
|
// spell-cast items, and into the cast-targeting helper at
|
|
// `FUN_006e5a90`. For self-use items (hearthstone, food, potion) the
|
|
// engine overwrites with the item's own GUID before dispatch, so a
|
|
// stray target is harmless. We default to a zero GUID when the caller
|
|
// omits `unit` — matches existing call sites.
|
|
//
|
|
// Returns nothing. Silently no-ops on bad input, item not found in
|
|
// bags, item locked, on cooldown, etc. An unrecognized `unit` string
|
|
// is treated as "no target" rather than raising, matching arg-1's
|
|
// "silently no-ops on bad input" contract.
|
|
|
|
#include "Game.h"
|
|
#include "Offsets.h"
|
|
#include "item/Arg.h"
|
|
#include "item/Location.h"
|
|
|
|
#include <cstdint>
|
|
|
|
namespace Item::Use {
|
|
|
|
namespace {
|
|
|
|
using UseItem_t = unsigned(__thiscall *)(const void *item, const uint64_t *targetGuid,
|
|
int flag);
|
|
|
|
using TokenToGUID_t = uint64_t(__fastcall *)(const char *token);
|
|
|
|
uint64_t ResolveUnitGuid(void *L, int idx) {
|
|
if (!Game::Lua::IsString(L, idx))
|
|
return 0;
|
|
const char *token = Game::Lua::ToString(L, idx);
|
|
auto fn = reinterpret_cast<TokenToGUID_t>(Offsets::FUN_TOKEN_TO_GUID);
|
|
return fn(token);
|
|
}
|
|
|
|
int __fastcall Script_C_Item_UseItemByName(void *L) {
|
|
const auto arg = Item::Arg::Resolve(L, 1);
|
|
if (arg.itemID <= 0 && arg.name == nullptr) {
|
|
return 0;
|
|
}
|
|
|
|
const uint64_t targetGuid = ResolveUnitGuid(L, 2);
|
|
|
|
Item::Location::ByGUIDResult found;
|
|
if (!Item::Location::FindByArgInBags(L, arg, &found)) {
|
|
return 0;
|
|
}
|
|
|
|
auto useItem = reinterpret_cast<UseItem_t>(Offsets::FUN_ITEM_USE);
|
|
useItem(found.item, &targetGuid, 0);
|
|
return 0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
static void RegisterLuaFunctions() {
|
|
Game::Lua::RegisterTableFunction("C_Item", "UseItemByName", &Script_C_Item_UseItemByName);
|
|
}
|
|
|
|
static const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions};
|
|
|
|
} // namespace Item::Use
|