Files
ClassicAPI/src/unit/FactionGroup.cpp
T
SkeewirtandBrues 27d8f90f03 fix(unit): UnitFactionGroup("player") answers while addons load (#79)
* fix(unit): UnitFactionGroup("player") answers while addons load

Script_UnitFactionGroup resolves "player" through the in-world unit
descriptor, so it returns nil at file scope, ADDON_LOADED and
VARIABLES_LOADED and only answers from PLAYER_LOGIN. UnitName, UnitRace
and UnitClass already answer then, and later clients answer
UnitFactionGroup too, so ported addons read it at file scope. AceDB-3.0
does exactly that to build its "<faction> - <realm>" profile key, and
fails to load on nil ("attempt to concatenate local 'factionKey'").

Co-hook the engine function: when it answers nothing for the "player"
token, fall back to the login-time race byte (VAR_PLAYER_RACE_BYTE) and
the same race -> faction-group chain its player branch uses. That chain
moves out of C_CreatureInfo.GetFactionInfo into a shared
Creature::Info::FactionGroupForRace; GetFactionInfo's behavior is
unchanged. Once the player object exists the engine answers by itself.

Adds FUN_SCRIPT_UNIT_FACTION_GROUP (slot 13 of table 0x00850438).

Measured in-game before the fix (Octo 1.18.1 client, Human):
UnitFactionGroup("player") = nil at file load / ADDON_LOADED /
VARIABLES_LOADED, "Alliance" from PLAYER_LOGIN; UnitName/UnitRace
answer at every stage.

Verified in-game after the fix (same client and character):
UnitFactionGroup("player") is "Alliance" at file load, ADDON_LOADED and
VARIABLES_LOADED, and AceDB-3.0 sets up Zygor Guides Viewer 2.0's saved
settings.

* fix(unit): keep UnitFactionGroup's in-world nil; describe both engine branches

Script_UnitFactionGroup reads the live faction template when the unit's
object is in the world, and only falls back to the name cache's race when
it isn't. The race fallback now runs only under that same condition (no
player object), so an in-world nil (e.g. charmed by a monster) stays nil
instead of turning into the race's faction. Comments and API.md now say
which engine branch the fallback mirrors.

---------

Co-authored-by: Brues <5278969+brues-code@users.noreply.github.com>
2026-10-17 02:14:04 -05:00

85 lines
3.5 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/>.
// `UnitFactionGroup("player")` while addons load.
//
// The engine's `Script_UnitFactionGroup` (0x00516630) has two branches. With
// the unit's object in the world it reads the live faction template
// (descriptor +0x74). Without it, it falls back to the name cache's race for
// that GUID -> ChrRaces -> faction template. During addon loading — file
// scope, ADDON_LOADED, VARIABLES_LOADED — the player object doesn't exist yet
// and the name-cache lookup misses, so it returns nil, and it only starts
// answering at PLAYER_LOGIN. UnitName, UnitRace and UnitClass already answer at that point
// (UnitRace/UnitClass special-case "player" to the login-time race/class byte
// globals, see VAR_PLAYER_RACE_BYTE), and later clients answer
// UnitFactionGroup there too, so ported addons read it at file scope:
// AceDB-3.0 builds its "<faction> - <realm>" profile key from it and fails to
// load on nil.
//
// Co-hook: run the engine function; when it answers nothing for the literal
// "player" token AND the player object doesn't exist — the engine's own
// name-cache branch condition — run that branch ourselves, fed from the
// login-time race byte instead of the cache
// (Creature::Info::FactionGroupForRace). Once the player object exists the
// engine's answer stands, nil included: a player whose live faction template
// has no named group (e.g. while charmed by a monster) stays nil, as stock.
#include "Game.h"
#include "Offsets.h"
#include "creature/Info.h"
#include "unit/Identity.h"
#include <cstdint>
namespace Unit::FactionGroup {
namespace {
using ScriptFn_t = int(__fastcall *)(void *L);
ScriptFn_t g_origUnitFactionGroup = nullptr;
int __fastcall UnitFactionGroup_h(void *L) {
const int base = Game::Lua::GetTop(L);
const int n = g_origUnitFactionGroup(L);
if (n > 0 && Game::Lua::Type(L, -n) != Game::Lua::TYPE_NIL)
return n; // the engine answered
if (!Game::Lua::IsString(L, 1))
return n;
const char *token = Game::Lua::ToString(L, 1);
if (token == nullptr || !Unit::Identity::IsPlayerToken(token))
return n;
if (Unit::Identity::PlayerObject() != nullptr)
return n; // in world: the engine read the live faction template
const uint8_t race = Game::Read<uint8_t>(Offsets::VAR_PLAYER_RACE_BYTE);
const char *english = nullptr;
const char *localized = nullptr;
if (race == 0 ||
!Creature::Info::FactionGroupForRace(race, &english, &localized))
return n;
Game::Lua::SetTop(L, base);
Game::Lua::PushString(L, english);
Game::Lua::PushString(L, localized);
return 2;
}
const Game::HookAutoRegister _unitFactionGroupHook{
Offsets::FUN_SCRIPT_UNIT_FACTION_GROUP,
reinterpret_cast<void *>(&UnitFactionGroup_h),
reinterpret_cast<void **>(&g_origUnitFactionGroup)};
} // namespace
} // namespace Unit::FactionGroup