diff --git a/src/Offsets.h b/src/Offsets.h index 4ce78146..561053d1 100644 --- a/src/Offsets.h +++ b/src/Offsets.h @@ -4388,6 +4388,17 @@ enum Offsets { // ItemDisplayInfo row of the hand's item (CGUnit_C 0x00605EE0, CGPlayer_C // 0x005EC350); AttachHandItem's record source. VTBL_UNIT_GET_HELD_ITEM_DISPLAY = 0x9C, + // Unit vtable slot 38 — `const uint8_t *__thiscall(unit, hand 0/1, 1)`, + // ret 8: the 8-byte held-item lane of the hand's item (item class, + // subclass, inventory type at byte 3, sheathe type at byte 4), or null. + // DressUpModel SetModelInstance copies hands 0 and 1 into its lanes + // (`push 1; push 0; call [eax+0x98]`, 0x005043D9). + VTBL_UNIT_GET_HELD_ITEM_LANE = 0x98, + // CloseHand — `__fastcall(model, hand)`: HandsClosed (15) on the finger + // key bones, 8..12 for hand 0, 13..17 for hand 1. AttachUnitModel closes + // hand 1 when attachment 2 holds a model and hand 0 for attachment 1 + // (0x00505A34..0x00505A5E). + FUN_CLOSE_HAND = 0x00479660, // TryOnItem — `__thiscall(pane, itemID)`: the worker behind TryOn. FUN_DRESSUPMODEL_TRY_ON_ITEM = 0x00504540, // The worker's item-cache callback (0x00504BA0) re-runs it once the @@ -4497,11 +4508,21 @@ enum Offsets { // equipment piece (validated against ItemDisplayInfo.dbc, no-op for 0 / // out-of-range). Units pass CreatureDisplayInfoExtra +0x20..+0x44. FUN_CHARCOMP_SET_ITEM = 0x00478AA0, - // __thiscall(builder, int *unused /*engine passes 0*/) -> bool done. + // UpdateCharacterComponent — `bool __thiscall(builder, int force)`. // The per-frame pump: composites pending texture regions; the baked-NPC // path short-circuits to the geoset apply (FUN_00477520) and returns 1 on - // the first call. Keep calling until it returns 1. + // the first call. Keep calling until it returns 1. `force` loads every + // section blocking and ignores the load budget (benilla charactermodel/ + // component#update-driver): the unit model update passes 0 (0x00607E7C), + // the DressUpModel paint (0x00504470) and AttachUnitModel's pump of the + // unit's own component before the copy (0x005059BC) pass 1. FUN_CHARCOMP_PUMP = 0x00477860, + // CloneCharacterComponent — `bool __thiscall(builder, model, source)`, + // ret 8: copies the source component's appearance block, section cells + // and item records onto `builder`, for `model`, whose reference it takes + // over (0x00476CDA; DressUpModel SetModelInstance AddRefs the model first; + // it is the only caller, 0x005043D5). + FUN_CHARCOMP_CLONE = 0x00476CB0, // Model-instance API used around the compositor: FUN_MODEL_INSTANCE_ADDREF = 0x00710390, // (model /*ecx*/) // __thiscall(model, tryLoad, recurseChildren) -> bool loaded. The engine's @@ -5289,6 +5310,10 @@ enum Offsets { FUN_CGOBJECT_SET_LIGHT = 0x00614550, FUN_CGOBJECT_CLEAR_LIGHT = 0x006144F0, OFF_CGOBJECT_MODEL = 0xD8, + // The unit's character component, null for a unit with none (a creature + // without CreatureDisplayInfoExtra): AttachUnitModel pumps it at + // 0x005059B2 and DressUpModel SetModelInstance clones it. + OFF_CGUNIT_CHARACTER_COMPONENT = 0xD30, OFF_CGOBJECT_OVERRIDE_MODEL = 0xDC, OFF_CGOBJECT_VTBL_SELECTION_COLOR = 0x2C, OFF_CGUNIT_SELECTION_RADIUS = 0xCF0, diff --git a/src/model/DisplayInfo.cpp b/src/model/DisplayInfo.cpp index 6090e2b2..5df87c8d 100644 --- a/src/model/DisplayInfo.cpp +++ b/src/model/DisplayInfo.cpp @@ -97,7 +97,7 @@ using CharCompCreate_t = void *(__cdecl *)(); using CharCompDestroy_t = void(__fastcall *)(void *builder); using CharCompSetInfo_t = char(__thiscall *)(void *builder, const CharCompInfo *info); using CharCompSetItem_t = void(__thiscall *)(void *builder, int slot, int itemDisplayID); -using CharCompPump_t = char(__thiscall *)(void *builder, int *unused); +using CharCompPump_t = char(__thiscall *)(void *builder, int force); // A pending / finished dress for one Model frame. The model loads async, and // the engine only dresses a LOADED model (its poll gate FUN_00607da0 checks @@ -378,8 +378,8 @@ void LightChildren(void *frame, void *instance) { M2::SetRenderCallback(child, lightFogCb, frame); } -bool PumpDresser(void *dresser) { - return reinterpret_cast(Offsets::FUN_CHARCOMP_PUMP)(dresser, nullptr) != 0; +bool PumpDresser(void *dresser, bool force) { + return reinterpret_cast(Offsets::FUN_CHARCOMP_PUMP)(dresser, force ? 1 : 0) != 0; } void DestroyDresser(void *dresser) { diff --git a/src/model/DisplayInfo.h b/src/model/DisplayInfo.h index ff017efe..dfeb275a 100644 --- a/src/model/DisplayInfo.h +++ b/src/model/DisplayInfo.h @@ -30,9 +30,10 @@ void *CreateDisplayModel(void *scene, uint32_t displayID, const char **path = nu // `instance` from `extraRec` (a DressRecord) and fed its equipment, or null // when the engine pool is empty or rejects the info. It holds a reference on // the model. Pump it with PumpDresser until that returns true; DestroyDresser -// releases it (null is a no-op). +// releases it (null is a no-op). `force` loads every texture at once, +// ignoring the engine's per-frame load budget. void *CreateDresser(void *instance, const uint8_t *extraRec); -bool PumpDresser(void *dresser); +bool PumpDresser(void *dresser, bool force = false); void DestroyDresser(void *dresser); // Gives every model attached to `instance` the light and fog callback that diff --git a/src/model/DressUp.cpp b/src/model/DressUp.cpp index 21f5d4a1..b2932564 100644 --- a/src/model/DressUp.cpp +++ b/src/model/DressUp.cpp @@ -19,6 +19,8 @@ // 0x00504490), which 4.3.4 split per slot: a component slot is cleared with // UnequipCharacterSlot, a held lane is detached with DetachHeldItemSlot and // zeroed. The slot map is 4.3.4's, from inventory slot to component slot. +// That step, the lanes and the weapon hanging below are Model::Outfit's, +// which the ModelScene actor shares. // // TryOn keeps vanilla's worker (TryOnItem, FUN_DRESSUPMODEL_TRY_ON_ITEM) and // takes 4.3.4's argument forms in front of it: an item ID, an item link or @@ -75,7 +77,7 @@ #include "item/Record.h" #include "model/ItemDisplay.h" #include "model/M2.h" -#include "object/Resolve.h" +#include "model/Outfit.h" #include #include @@ -86,7 +88,6 @@ namespace Model::DressUp { namespace { -using UnequipSlot_t = void(__thiscall *)(void *component, uint32_t slot); using DetachHeldItem_t = void(__fastcall *)(void *model, uint32_t bodySlot, uint32_t sheatheType, bool shield); using AttachItemModel_t = int(__fastcall *)(void *model, const void *record, uint32_t bodySlot, @@ -95,15 +96,10 @@ using AttachItemModel_t = int(__fastcall *)(void *model, const void *record, uin using TryOnItem_t = void(__thiscall *)(void *pane, uint32_t itemID); using SetModelInstance_t = void(__thiscall *)(void *pane, void *model); using ClearModel_t = void(__thiscall *)(void *pane); -using GetHeldItemDisplay_t = const void *(__thiscall *)(void *unit, uint32_t hand); -using FindChild_t = void *(__thiscall *)(void *model, int attachId); -using GetVisibleItem_t = const void *(__thiscall *)(void *player, uint32_t slot); -using GetDescVisual_t = int(__thiscall *)(const void *visibleItem); using DisplayError_t = void(__cdecl *)(uint32_t id, ...); -// Body slots of the two held lanes. -constexpr uint32_t kBodySlotMainHand = 0xF; -constexpr uint32_t kBodySlotOffHand = 0x10; +using Outfit::kBodySlotMainHand; +using Outfit::kBodySlotOffHand; // Retail's Enum.ItemTryOnReason. constexpr int kTryOnSuccess = 0; @@ -111,32 +107,16 @@ constexpr int kTryOnWrongRace = 1; constexpr int kTryOnNotEquippable = 2; constexpr int kTryOnDataPending = 3; -// A held lane (OFF_DRESSUPMODEL_*_HAND_LANE). -struct HeldLane { - uint8_t itemClass; // 0 = empty - uint8_t unknown1[2]; - uint8_t inventoryType; - uint8_t sheatheType; - uint8_t unknown5[3]; -}; -static_assert(sizeof(HeldLane) == 8, "ClearModel zeroes 8 bytes per lane"); - -struct Lane { - uintptr_t offset; - uint32_t bodySlot; -}; -constexpr Lane kLanes[2] = { - {Offsets::OFF_DRESSUPMODEL_MAIN_HAND_LANE, kBodySlotMainHand}, - {Offsets::OFF_DRESSUPMODEL_OFF_HAND_LANE, kBodySlotOffHand}, -}; +static_assert(Offsets::OFF_DRESSUPMODEL_OFF_HAND_LANE == + Offsets::OFF_DRESSUPMODEL_MAIN_HAND_LANE + sizeof(Outfit::HeldLane), + "the pane's two lanes are one Outfit::Lanes"); struct PaneState { bool autoDress = true; bool sheathSet = false; // SetSheathed was called; weapons follow the two below bool sheathed = false; bool hideWeapons = false; - const void *laneDisplay[2] = {}; // ItemDisplayInfo row on each lane, or null - uint32_t laneVisual[2] = {}; // enchant ItemVisuals row on each lane, or 0 + Outfit::Weapons weapons; }; std::unordered_map g_panes; @@ -148,12 +128,8 @@ bool g_dressing = false; // Dress() is rebuilding the pane uintptr_t g_mainHandPath = Offsets::TRY_ON_MAIN_HAND_PATH; uintptr_t g_offHandPath = Offsets::TRY_ON_OFF_HAND_PATH; -HeldLane &LaneOf(void *pane, int i) { - return Game::Ref(pane, kLanes[i].offset); -} - -int LaneIndex(uint32_t bodySlot) { - return bodySlot == kBodySlotOffHand ? 1 : 0; +Outfit::Lanes &LanesOf(void *pane) { + return Game::Ref(pane, Offsets::OFF_DRESSUPMODEL_MAIN_HAND_LANE); } // The glow `enchantID` gives an item shown with `display`: none when the @@ -169,42 +145,8 @@ uint32_t EnchantVisual(const void *display, uint32_t enchantID) { : 0; } -// AttachEnchantVisual's step (0x005EED50): the glow on the item hung at -// `attachId`. -void HangVisual(void *model, int attachId, uint32_t visual) { - if (visual == 0 || attachId < 0) - return; - if (void *item = reinterpret_cast(Offsets::FUN_M2MODEL_FIND_CHILD)(model, - attachId)) - M2::AttachItemVisuals(item, visual); -} - -// DetachHeldItemSlot on lane `i`'s item. -void DetachLane(void *model, int i, const HeldLane &lane) { - reinterpret_cast(Offsets::FUN_DETACH_HELD_ITEM_SLOT)( - model, kLanes[i].bodySlot, lane.sheatheType, - lane.inventoryType == Offsets::INVTYPE_SHIELD); -} - -// The unit's AttachHandItem step on each occupied lane: detach it, then hang -// it in the hand or the sheath, or leave it off. void ApplyWeapons(void *pane, const PaneState &s) { - void *model = Pane::Model(pane); - if (model == nullptr) - return; - for (int i = 0; i < 2; ++i) { - const HeldLane &lane = LaneOf(pane, i); - if (lane.itemClass == 0 || s.laneDisplay[i] == nullptr) - continue; - DetachLane(model, i, lane); - if (s.hideWeapons) - continue; - const int attachId = reinterpret_cast(Offsets::FUN_ATTACH_ITEM_MODEL)( - model, s.laneDisplay[i], kLanes[i].bodySlot, lane.sheatheType, s.sheathed, - lane.inventoryType == Offsets::INVTYPE_SHIELD, - lane.inventoryType == Offsets::INVTYPE_RANGEDRIGHT); - HangVisual(model, attachId, s.laneVisual[i]); - } + Outfit::HangWeapons(Pane::Model(pane), LanesOf(pane), s.weapons, s.sheathed, s.hideWeapons); } // Slot 36, after the original has cloned the unit and copied its lanes. @@ -214,30 +156,8 @@ void __fastcall SetModelInstance(void *pane, void * /*edx*/, void *model) { if (model == nullptr) return; PaneState &s = g_panes[pane]; - s.laneDisplay[0] = s.laneDisplay[1] = nullptr; - s.laneVisual[0] = s.laneVisual[1] = 0; // The original filled the lanes from this unit, resolved the same way. - const uint64_t guid = Game::Read(pane, Offsets::OFF_PLAYERMODEL_UNIT_GUID); - if (void *unit = Object::ByGuid(Offsets::TYPEMASK_UNIT, guid)) { - const auto get = Game::Read( - Game::Read(unit, 0), Offsets::VTBL_UNIT_GET_HELD_ITEM_DISPLAY); - s.laneDisplay[0] = get(unit, 0); - s.laneDisplay[1] = get(unit, 1); - } - // AttachHandEnchant's glow (0x005EED00), for players only: the visible - // item in the lane's slot (0xF / 0x10, the same numbers as the body - // slots), and its visual unless the display hangs its own (-1). - if (void *player = Object::ByGuid(Offsets::TYPEMASK_PLAYER, guid)) { - for (int i = 0; i < 2; ++i) { - const void *item = reinterpret_cast( - Offsets::FUN_UNIT_GET_VISIBLE_ITEM)(player, kLanes[i].bodySlot); - if (item == nullptr || Game::Read(item, Offsets::OFF_VISIBLE_ITEM_ITEM_ID) == 0) - continue; - const int visual = - reinterpret_cast(Offsets::FUN_VISIBLE_ITEM_GET_DESC_VISUAL)(item); - s.laneVisual[i] = visual > 0 ? static_cast(visual) : 0; - } - } + Outfit::ReadWeapons(Game::Read(pane, Offsets::OFF_PLAYERMODEL_UNIT_GUID), s.weapons); if (!s.autoDress && !g_dressing) { reinterpret_cast(Offsets::FUN_DRESSUPMODEL_CLEAR_MODEL)(pane); return; @@ -254,16 +174,16 @@ int __fastcall TryOnAttach(void *model, const void *record, uint32_t bodySlot, if (g_tryOnPane == nullptr) return attach(model, record, bodySlot, sheatheType, sheathed, shield, ranged); PaneState &s = g_panes[g_tryOnPane]; - const int lane = LaneIndex(bodySlot); - s.laneDisplay[lane] = record; - s.laneVisual[lane] = EnchantVisual(record, g_tryOnEnchant); + const int lane = Outfit::LaneIndex(bodySlot); + s.weapons.display[lane] = record; + s.weapons.visual[lane] = EnchantVisual(record, g_tryOnEnchant); if (s.sheathSet) { if (s.hideWeapons) return -1; sheathed = s.sheathed; } const int attachId = attach(model, record, bodySlot, sheatheType, sheathed, shield, ranged); - HangVisual(model, attachId, s.laneVisual[lane]); + Outfit::HangVisual(model, attachId, s.weapons.visual[lane]); return attachId; } @@ -273,9 +193,9 @@ int __fastcall TryOnAttach(void *model, const void *record, uint32_t bodySlot, void __fastcall TryOnEvict(void *model, uint32_t bodySlot, uint32_t sheatheType, bool shield) { if (g_tryOnPane != nullptr) { PaneState &s = g_panes[g_tryOnPane]; - const int lane = LaneIndex(bodySlot); - s.laneDisplay[lane] = nullptr; - s.laneVisual[lane] = 0; + const int lane = Outfit::LaneIndex(bodySlot); + s.weapons.display[lane] = nullptr; + s.weapons.visual[lane] = 0; } reinterpret_cast(Offsets::FUN_DETACH_HELD_ITEM_SLOT)(model, bodySlot, sheatheType, shield); @@ -302,33 +222,6 @@ __declspec(naked) void HandChoiceThunk() { } } -// ClearModel's lane step: detach the held item and zero the lane. -void ClearLane(void *pane, int i) { - HeldLane &lane = LaneOf(pane, i); - if (lane.itemClass == 0) - return; - DetachLane(Pane::Model(pane), i, lane); - lane = HeldLane{}; -} - -// Component slot for a 0-based inventory slot, or -1 (4.3.4 UndressSlot). -int ComponentSlot(int slot0) { - switch (slot0) { - case 0: return 0; // head - case 2: return 1; // shoulder - case 3: return 2; // shirt - case 4: return 3; // chest - case 5: return 4; // waist - case 6: return 5; // legs - case 7: return 6; // feet - case 8: return 7; // wrist - case 9: return 8; // hands - case 14: return 10; // back - case 18: return 9; // tabard - default: return -1; - } -} - int __fastcall Script_UndressSlot(void *L) { void *pane = Game::Lua::ResolveDressUpModel(L); if (pane == nullptr) @@ -337,20 +230,9 @@ int __fastcall Script_UndressSlot(void *L) { Game::Lua::Error(L, "Usage: %s:UndressSlot(slotIndex)", Game::Lua::ObjectDisplayName(pane)); return 0; } - const int slot0 = static_cast(Game::Lua::ToNumber(L, 2)) - 1; - void *component = Game::Read(pane, Offsets::OFF_DRESSUPMODEL_COMPONENT); - if (Pane::Model(pane) == nullptr || component == nullptr) - return 0; - if (slot0 + 1 == Offsets::INVSLOT_MAINHAND) { - ClearLane(pane, 0); - } else if (slot0 + 1 == Offsets::INVSLOT_OFFHAND) { - ClearLane(pane, 1); - } else { - const int compSlot = ComponentSlot(slot0); - if (compSlot >= 0) - reinterpret_cast(Offsets::FUN_CHARCOMP_UNEQUIP_SLOT)( - component, static_cast(compSlot)); - } + Outfit::UndressSlot(Pane::Model(pane), + Game::Read(pane, Offsets::OFF_DRESSUPMODEL_COMPONENT), + LanesOf(pane), static_cast(Game::Lua::ToNumber(L, 2))); return 0; } diff --git a/src/model/Outfit.cpp b/src/model/Outfit.cpp new file mode 100644 index 00000000..09a71025 --- /dev/null +++ b/src/model/Outfit.cpp @@ -0,0 +1,191 @@ +// 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 . + +// The outfit calls of a character model copy: DressUpModel's own steps +// (SetModelInstance 0x00504350, ClearModel 0x00504490, 4.3.4's per-slot +// UndressSlot) and the unit's AttachHandItem step, on whatever holds the +// component and the lanes. benilla ui/dressup and charactermodel/equipment. +// +// The slot map is 4.3.4's UndressSlot map, from inventory slot to component +// slot. + +#include "model/Outfit.h" + +#include "Game.h" +#include "Offsets.h" +#include "model/M2.h" +#include "object/Resolve.h" + +namespace Model::Outfit { +namespace { + +using UnequipSlot_t = void(__thiscall *)(void *component, uint32_t slot); +using DetachHeldItem_t = void(__fastcall *)(void *model, uint32_t bodySlot, uint32_t sheatheType, + bool shield); +using AttachItemModel_t = int(__fastcall *)(void *model, const void *record, uint32_t bodySlot, + uint32_t sheatheType, bool sheathed, bool shield, + bool ranged); +using GetHeldItemDisplay_t = const void *(__thiscall *)(void *unit, uint32_t hand); +using GetHeldItemLane_t = const HeldLane *(__thiscall *)(void *unit, uint32_t hand, int one); +using FindChild_t = void *(__thiscall *)(void *model, int attachId); +using GetVisibleItem_t = const void *(__thiscall *)(void *player, uint32_t slot); +using GetDescVisual_t = int(__thiscall *)(const void *visibleItem); +using CharCompCreate_t = void *(__cdecl *)(); +using CharCompClone_t = bool(__thiscall *)(void *component, void *model, void *source); + +constexpr uint32_t kBodySlots[2] = {kBodySlotMainHand, kBodySlotOffHand}; + +// The component slots ClearModel clears (0..11). +constexpr uint32_t kComponentSlots = 12; + +template +Fn VirtualOf(void *object, uintptr_t slot) { + return Game::Read(Game::Read(object, 0), slot); +} + +// DetachHeldItemSlot on lane `i`'s item. +void DetachLane(void *model, int i, const HeldLane &lane) { + reinterpret_cast(Offsets::FUN_DETACH_HELD_ITEM_SLOT)( + model, kBodySlots[i], lane.sheatheType, lane.inventoryType == Offsets::INVTYPE_SHIELD); +} + +// ClearModel's lane step: detach the held item and zero the lane. +void ClearLane(void *model, Lanes &lanes, int i) { + if (lanes[i].itemClass == 0) + return; + DetachLane(model, i, lanes[i]); + lanes[i] = HeldLane{}; +} + +// Component slot for a 0-based inventory slot, or -1. +int ComponentSlot(int slot0) { + switch (slot0) { + case 0: return 0; // head + case 2: return 1; // shoulder + case 3: return 2; // shirt + case 4: return 3; // chest + case 5: return 4; // waist + case 6: return 5; // legs + case 7: return 6; // feet + case 8: return 7; // wrist + case 9: return 8; // hands + case 14: return 10; // back + case 18: return 9; // tabard + default: return -1; + } +} + +} // namespace + +void ReadLanes(void *unit, Lanes &lanes) { + const auto get = VirtualOf(unit, Offsets::VTBL_UNIT_GET_HELD_ITEM_LANE); + for (uint32_t hand = 0; hand < 2; ++hand) { + const HeldLane *lane = get(unit, hand, 1); + lanes[hand] = lane != nullptr ? *lane : HeldLane{}; + } +} + +void ReadWeapons(uint64_t guid, Weapons &weapons) { + weapons = Weapons{}; + if (void *unit = Object::ByGuid(Offsets::TYPEMASK_UNIT, guid)) { + const auto get = + VirtualOf(unit, Offsets::VTBL_UNIT_GET_HELD_ITEM_DISPLAY); + weapons.display[0] = get(unit, 0); + weapons.display[1] = get(unit, 1); + } + // AttachHandEnchant's glow (0x005EED00), for players only: the visible + // item in the lane's slot (0xF / 0x10, the same numbers as the body + // slots), and its visual unless the display hangs its own (-1). + void *player = Object::ByGuid(Offsets::TYPEMASK_PLAYER, guid); + if (player == nullptr) + return; + for (int i = 0; i < 2; ++i) { + const void *item = + reinterpret_cast(Offsets::FUN_UNIT_GET_VISIBLE_ITEM)(player, + kBodySlots[i]); + if (item == nullptr || Game::Read(item, Offsets::OFF_VISIBLE_ITEM_ITEM_ID) == 0) + continue; + const int visual = + reinterpret_cast(Offsets::FUN_VISIBLE_ITEM_GET_DESC_VISUAL)(item); + weapons.visual[i] = visual > 0 ? static_cast(visual) : 0; + } +} + +void *CloneComponent(void *model, void *unit) { + void *source = Game::Read(unit, Offsets::OFF_CGUNIT_CHARACTER_COMPONENT); + if (source == nullptr) + return nullptr; + void *component = reinterpret_cast(Offsets::FUN_CHARCOMP_CREATE)(); + if (component == nullptr) + return nullptr; + M2::AddRef(model); // the component's reference + reinterpret_cast(Offsets::FUN_CHARCOMP_CLONE)(component, model, source); + return component; +} + +void HangVisual(void *model, int attachId, uint32_t visual) { + if (visual == 0 || attachId < 0) + return; + if (void *item = reinterpret_cast(Offsets::FUN_M2MODEL_FIND_CHILD)(model, + attachId)) + M2::AttachItemVisuals(item, visual); +} + +void HangWeapons(void *model, const Lanes &lanes, const Weapons &weapons, bool sheathed, + bool hide) { + if (model == nullptr) + return; + for (int i = 0; i < 2; ++i) { + const HeldLane &lane = lanes[i]; + if (lane.itemClass == 0 || weapons.display[i] == nullptr) + continue; + DetachLane(model, i, lane); + if (hide) + continue; + const int attachId = reinterpret_cast(Offsets::FUN_ATTACH_ITEM_MODEL)( + model, weapons.display[i], kBodySlots[i], lane.sheatheType, sheathed, + lane.inventoryType == Offsets::INVTYPE_SHIELD, + lane.inventoryType == Offsets::INVTYPE_RANGEDRIGHT); + HangVisual(model, attachId, weapons.visual[i]); + } +} + +void UndressSlot(void *model, void *component, Lanes &lanes, int invSlot) { + if (model == nullptr) + return; + if (invSlot == Offsets::INVSLOT_MAINHAND) { + ClearLane(model, lanes, 0); + } else if (invSlot == Offsets::INVSLOT_OFFHAND) { + ClearLane(model, lanes, 1); + } else if (component != nullptr) { + const int compSlot = ComponentSlot(invSlot - 1); + if (compSlot >= 0) + reinterpret_cast(Offsets::FUN_CHARCOMP_UNEQUIP_SLOT)( + component, static_cast(compSlot)); + } +} + +void Undress(void *model, void *component, Lanes &lanes, bool weapons) { + if (model == nullptr) + return; + if (component != nullptr) { + for (uint32_t slot = 0; slot < kComponentSlots; ++slot) + reinterpret_cast(Offsets::FUN_CHARCOMP_UNEQUIP_SLOT)(component, slot); + } + if (weapons) { + ClearLane(model, lanes, 0); + ClearLane(model, lanes, 1); + } +} + +} // namespace Model::Outfit diff --git a/src/model/Outfit.h b/src/model/Outfit.h new file mode 100644 index 00000000..f1a997cb --- /dev/null +++ b/src/model/Outfit.h @@ -0,0 +1,82 @@ +// 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 . + +#pragma once + +#include + +// What a copy of a unit's character model wears, apart from its own model: +// the character component that composites its armor, and the two held-item +// lanes. 1.12's DressUpModel keeps these in the pane (benilla ui/dressup); +// the ModelScene actor keeps its own. Both change them with the calls here. +namespace Model::Outfit { + +// Body slots of the two held lanes. +constexpr uint32_t kBodySlotMainHand = 0xF; +constexpr uint32_t kBodySlotOffHand = 0x10; + +// A held lane, as the unit's VTBL_UNIT_GET_HELD_ITEM_LANE returns it and the +// pane stores it (OFF_DRESSUPMODEL_*_HAND_LANE). +struct HeldLane { + uint8_t itemClass; // 0 = empty + uint8_t unknown1[2]; + uint8_t inventoryType; + uint8_t sheatheType; + uint8_t unknown5[3]; +}; +static_assert(sizeof(HeldLane) == 8, "ClearModel zeroes 8 bytes per lane"); +using Lanes = HeldLane[2]; // main hand, off hand + +// The row and glow each lane's item is hung with. The lanes keep no display +// row, so whoever re-hangs a weapon keeps these beside them. +struct Weapons { + const void *display[2] = {}; // ItemDisplayInfo row on each lane, or null + uint32_t visual[2] = {}; // enchant ItemVisuals row on each lane, or 0 +}; + +// The lane (0 or 1) of a held body slot. +inline int LaneIndex(uint32_t bodySlot) { return bodySlot == kBodySlotOffHand ? 1 : 0; } + +// DressUpModel SetModelInstance's lane copy (0x00504350): the unit's two +// held-item lanes, zero where a hand is empty. +void ReadLanes(void *unit, Lanes &lanes); + +// The rows the unit `guid` hangs its held items with (AttachHandItem's +// source), and, for a player, each one's enchant glow (AttachHandEnchant). +void ReadWeapons(uint64_t guid, Weapons &weapons); + +// A new character component for `model`, cloned from the unit's, as +// DressUpModel SetModelInstance clones it; null when the unit has none. +// It is pumped and released like a dresser (Model::DisplayInfo::PumpDresser, +// DestroyDresser): both are the engine's character component. +void *CloneComponent(void *model, void *unit); + +// AttachEnchantVisual's step (0x005EED50): the glow on the item hung at +// `attachId`. +void HangVisual(void *model, int attachId, uint32_t visual); + +// The unit's AttachHandItem step on each occupied lane: detach it, then hang +// it in the hand or the sheath, or leave it off. +void HangWeapons(void *model, const Lanes &lanes, const Weapons &weapons, bool sheathed, + bool hide); + +// UndressSlot: the item in one inventory slot (1-based) off the model. A +// held lane is detached and zeroed, an armor slot cleared on the component +// (nothing without one). +void UndressSlot(void *model, void *component, Lanes &lanes, int invSlot); + +// DressUpModel ClearModel (0x00504490): component slots 0..11 cleared, and, +// with `weapons`, both lanes detached and zeroed. +void Undress(void *model, void *component, Lanes &lanes, bool weapons); + +} // namespace Model::Outfit diff --git a/src/model/Scene.h b/src/model/Scene.h index 46c5161e..d9c8f1f6 100644 --- a/src/model/Scene.h +++ b/src/model/Scene.h @@ -14,6 +14,7 @@ #pragma once #include "animation/Object.h" +#include "model/Outfit.h" #include #include @@ -45,6 +46,11 @@ class Actor : public Animation::Object { // Shows `model` (one reference, which the actor takes over), or nothing. void SetModel(void *model, const char *path, const uint8_t *dressRecord); + // Shows a copy of the unit's model, dressed and armed per autoDress, + // sheathed and hideWeapons; false (and no model) when it has none. + bool SetUnitModel(uint64_t guid); + // Hangs the held items per sheathed and hideWeapons. + void ApplyWeapons(); void ClearModel(); void PlayAnimation(); // Shows the kit (0 for none) on the model, replacing the one shown. @@ -55,7 +61,16 @@ class Actor : public Animation::Object { void *model = nullptr; std::string modelPath; const uint8_t *dressRecord = nullptr; // CreatureDisplayInfoExtra, until dressed + // The character component dressing the model: a display's dress, or the + // clone of a unit's. void *dresser = nullptr; + // A unit's model (SetModelByUnit): the unit and what the copy holds. + uint64_t unitGuid = 0; + Outfit::Lanes lanes = {}; + Outfit::Weapons weapons; + bool autoDress = true; + bool sheathed = false; + bool hideWeapons = false; bool loaded = false; // OnModelLoaded has run for the model bool animFinished = false; diff --git a/src/model/SceneActor.cpp b/src/model/SceneActor.cpp index 81789c40..5dcb9041 100644 --- a/src/model/SceneActor.cpp +++ b/src/model/SceneActor.cpp @@ -58,6 +58,23 @@ // animation kits (AnimKit data is a later client's), so PlayAnimationKit and // StopAnimationKit change nothing. // +// A unit's model (SetModelByUnit) is 1.12's pane copy, AttachUnitModel +// (0x005059A0) step for step: the unit's own component pumped (forced), a +// copy of the unit's model in the scene (CreateModelFromModel, which copies +// the attachments: helm, shoulders, held items), its effect / spell +// attachments dropped (DetachAllItemModels), then what DressUpModel's +// SetModelInstance (0x00504350) adds: a clone of the unit's character +// component and its two held-item lanes. The hand grips close as the pane +// closes them. The clone is pumped from the update walk, as the DressUpModel +// paint pumps its own (0x00504470), so Undress / UndressSlot show at once; +// OnModelLoaded runs once its first composite is done. Then retail's +// SetModelByUnit arguments: autoDress off undresses it (ClearModel's steps), +// and the held items are hung in the hands or the sheaths, or left off. +// Dress builds the copy again from the unit. The copy shows the unit as it +// looks: a shapeshifted unit shows its form (1.12 keeps no native-form model +// to copy), so usePlayerNativeForm, holdBowString and customRaceID change +// nothing. +// // XML: an element is a template CreateActor names (1.12's file loader // registers every virtual top-level node whatever its tag), loaded the way an // animation group is: the mixins of the whole inherits chain first, then each @@ -72,9 +89,12 @@ #include "Offsets.h" #include "animation/Xml.h" #include "api/Documentation.h" +#include "guid/Guid.h" #include "model/DisplayInfo.h" #include "model/M2.h" #include "model/VisualKit.h" +#include "object/Resolve.h" +#include "unit/Identity.h" #include "xml/FrameLoad.h" #include "xml/Mixin.h" @@ -137,6 +157,9 @@ void ReleaseModel(Actor &actor) { actor.model = nullptr; actor.dresser = nullptr; actor.dressRecord = nullptr; + actor.unitGuid = 0; + actor.lanes[0] = actor.lanes[1] = Outfit::HeldLane{}; + actor.weapons = Outfit::Weapons{}; actor.loaded = false; actor.animFinished = false; actor.modelPath.clear(); @@ -164,6 +187,50 @@ void Actor::SetModel(void *newModel, const char *path, const uint8_t *newDressRe RunScript(S_ONMODELLOADING, 0); } +bool Actor::SetUnitModel(uint64_t guid) { + using CopyModel_t = void *(__thiscall *)(void *scene, void *source, uint32_t flags); + using DetachAll_t = void(__fastcall *)(void *model); + using FindChild_t = void *(__thiscall *)(void *model, int attachId); + using CloseHand_t = void(__fastcall *)(void *model, uint32_t hand); + ClearModel(); + void *unit = guid != 0 ? ::Object::ByGuid(Offsets::TYPEMASK_UNIT, guid) : nullptr; + void *source = unit != nullptr ? Game::Read(unit, Offsets::OFF_CGOBJECT_MODEL) : nullptr; + if (source == nullptr) + return false; + if (void *own = Game::Read(unit, Offsets::OFF_CGUNIT_CHARACTER_COMPONENT)) + Model::DisplayInfo::PumpDresser(own, true); + void *copy = reinterpret_cast(Offsets::FUN_M2SCENE_COPY_MODEL)( + Model::Pane::Scene(SceneFrame()), source, 0); + if (copy == nullptr) + return false; + reinterpret_cast(Offsets::FUN_DETACH_ALL_ITEM_MODELS)(copy); + unitGuid = guid; + dresser = Outfit::CloneComponent(copy, unit); + Outfit::ReadLanes(unit, lanes); + Outfit::ReadWeapons(guid, weapons); + const auto findChild = reinterpret_cast(Offsets::FUN_M2MODEL_FIND_CHILD); + const auto closeHand = reinterpret_cast(Offsets::FUN_CLOSE_HAND); + if (findChild(copy, 2) != nullptr) + closeHand(copy, 1); + if (findChild(copy, 1) != nullptr) + closeHand(copy, 0); + if (autoDress) + Outfit::HangWeapons(copy, lanes, weapons, sheathed, hideWeapons); + else + Outfit::Undress(copy, dresser, lanes, true); + // SetModel keeps the unit fields: there is no model to clear. + SetModel(copy, nullptr, nullptr); + Model::DisplayInfo::LightChildren(SceneFrame(), copy); + return true; +} + +void Actor::ApplyWeapons() { + if (model == nullptr) + return; + Outfit::HangWeapons(model, lanes, weapons, sheathed, hideWeapons); + Model::DisplayInfo::LightChildren(SceneFrame(), model); +} + void Actor::ClearModel() { if (model == nullptr) return; @@ -235,24 +302,24 @@ void Actor::ApplyAlpha() { void Actor::Update(float elapsedSec) { if (model != nullptr && !loaded) { bool ready = M2::IsLoaded(model, 0, 0); - if (ready && dressRecord != nullptr) { - if (dresser == nullptr) { - // A dress the engine rejects shows the model undressed, as a - // unit with no compositor shows. - dresser = Model::DisplayInfo::CreateDresser(model, dressRecord); - if (dresser == nullptr) - dressRecord = nullptr; - else - Model::DisplayInfo::LightChildren(SceneFrame(), model); - } - if (dresser != nullptr && !Model::DisplayInfo::PumpDresser(dresser)) - ready = false; + if (ready && dressRecord != nullptr && dresser == nullptr) { + // A dress the engine rejects shows the model undressed, as a + // unit with no compositor shows. + dresser = Model::DisplayInfo::CreateDresser(model, dressRecord); + if (dresser == nullptr) + dressRecord = nullptr; + else + Model::DisplayInfo::LightChildren(SceneFrame(), model); } + if (ready && dresser != nullptr && !Model::DisplayInfo::PumpDresser(dresser)) + ready = false; if (ready && M2::IsLoaded(model, 0, 1)) { loaded = true; ApplyKit(*this); RunScript(S_ONMODELLOADED, 0); } + } else if (dresser != nullptr) { + Model::DisplayInfo::PumpDresser(dresser); } if (model != nullptr && M2::IsReady(model, 0, 1)) { M2::SetAnimated(model, true); @@ -349,6 +416,104 @@ int __fastcall Script_SetModelByPath(void *L) { return 1; } +// Retail's argument order: unit, sheatheWeapons, autoDress, hideWeapons, +// usePlayerNativeForm, holdBowString, customRaceID. +int __fastcall Script_SetModelByUnit(void *L) { + Actor *actor = Self(L); + if (actor == nullptr) + return 0; + if (!Game::Lua::IsString(L, 2)) { + Game::Lua::Error(L, "Usage: %s:SetModelByUnit(unit [, sheatheWeapons, autoDress, hideWeapons])", + actor->DisplayName()); + return 0; + } + actor->sheathed = Game::Lua::ToBoolean(L, 3) != 0; + actor->autoDress = Game::Lua::Type(L, 4) <= Game::Lua::TYPE_NIL || Game::Lua::ToBoolean(L, 4) != 0; + actor->hideWeapons = Game::Lua::ToBoolean(L, 5) != 0; + const uint64_t guid = Unit::Identity::GuidForToken(Game::Lua::ToString(L, 2)); + Game::Lua::PushBool(L, actor->SetUnitModel(guid)); + return 1; +} + +int __fastcall Script_GetModelUnitGUID(void *L) { + Actor *actor = Self(L); + if (actor == nullptr || actor->unitGuid == 0) + return 0; + char buf[Guid::STRING_SIZE]; + Game::Lua::PushString(L, Guid::FormatAsString(actor->unitGuid, buf, sizeof buf)); + return 1; +} + +// Builds the copy again from the unit, dressed. +int __fastcall Script_Dress(void *L) { + Actor *actor = Self(L); + if (actor == nullptr || actor->unitGuid == 0) + return 0; + const bool autoDress = actor->autoDress; + actor->autoDress = true; + actor->SetUnitModel(actor->unitGuid); + actor->autoDress = autoDress; + return 0; +} + +int __fastcall Script_Undress(void *L) { + Actor *actor = Self(L); + if (actor == nullptr) + return 0; + const bool weapons = Game::Lua::Type(L, 2) <= Game::Lua::TYPE_NIL || Game::Lua::ToBoolean(L, 2) != 0; + Outfit::Undress(actor->model, actor->dresser, actor->lanes, weapons); + return 0; +} + +int __fastcall Script_UndressSlot(void *L) { + Actor *actor = Self(L); + if (actor == nullptr || !NumberArgs(L, 2, 1, "UndressSlot(inventorySlot)", actor)) + return 0; + Outfit::UndressSlot(actor->model, actor->dresser, actor->lanes, + static_cast(Game::Lua::ToNumber(L, 2))); + return 0; +} + +int __fastcall Script_SetSheathed(void *L) { + Actor *actor = Self(L); + if (actor == nullptr) + return 0; + actor->sheathed = Game::Lua::ToBoolean(L, 2) != 0; + actor->hideWeapons = Game::Lua::ToBoolean(L, 3) != 0; + actor->ApplyWeapons(); + return 0; +} + +int __fastcall Script_GetSheathed(void *L) { + Actor *actor = Self(L); + if (actor == nullptr) + return 0; + Game::Lua::PushBool(L, actor->sheathed); + return 1; +} + +int __fastcall Script_SetAutoDress(void *L) { + if (Actor *actor = Self(L)) + actor->autoDress = Game::Lua::ToBoolean(L, 2) != 0; + return 0; +} + +int __fastcall Script_GetAutoDress(void *L) { + Actor *actor = Self(L); + if (actor == nullptr) + return 0; + Game::Lua::PushBool(L, actor->autoDress); + return 1; +} + +int __fastcall Script_IsGeoReady(void *L) { + Actor *actor = Self(L); + if (actor == nullptr) + return 0; + Game::Lua::PushBool(L, actor->model != nullptr && M2::IsLoaded(actor->model, 0, 1)); + return 1; +} + int __fastcall Script_ClearModel(void *L) { if (Actor *actor = Self(L)) actor->ClearModel(); @@ -590,6 +755,16 @@ const Game::Lua::FrameMethodEntry kMethods[] = { {"HookScript", &Animation::Script_HookScript}, {"SetModelByCreatureDisplayID", &Script_SetModelByCreatureDisplayID}, {"SetModelByPath", &Script_SetModelByPath}, + {"SetModelByUnit", &Script_SetModelByUnit}, + {"GetModelUnitGUID", &Script_GetModelUnitGUID}, + {"Dress", &Script_Dress}, + {"Undress", &Script_Undress}, + {"UndressSlot", &Script_UndressSlot}, + {"SetSheathed", &Script_SetSheathed}, + {"GetSheathed", &Script_GetSheathed}, + {"SetAutoDress", &Script_SetAutoDress}, + {"GetAutoDress", &Script_GetAutoDress}, + {"IsGeoReady", &Script_IsGeoReady}, {"ClearModel", &Script_ClearModel}, {"IsLoaded", &Script_IsLoaded}, {"GetModelPath", &Script_GetModelPath}, @@ -701,6 +876,48 @@ const Game::Doc::Function kPlayAnimationKit{ const Game::Doc::Function kStopAnimationKit{ "Has no effect: this client has no animation kits.", {}, {}}; +const Game::Doc::Field kUnitArgs[] = { + Req("unit", "UnitToken"), + Opt("sheatheWeapons", "bool", "false"), + Opt("autoDress", "bool", "true", "False shows the unit with nothing on."), + Opt("hideWeapons", "bool", "false"), + Opt("usePlayerNativeForm", "bool", "true", "Has no effect: the unit shows as it looks."), + Opt("holdBowString", "bool", "false", "Has no effect."), + Opt("customRaceID", "number", nullptr, "Has no effect."), +}; +const Game::Doc::Field kGuidRet[] = { + Req("guid", "WOWGUID", "Nil when the actor shows no unit."), +}; +const Game::Doc::Field kUndressArgs[] = {Opt("includeWeapons", "bool", "true")}; +const Game::Doc::Field kUndressSlotArgs[] = { + Req("inventorySlots", "number", "An inventory slot, 1 (head) to 19 (tabard)."), +}; +const Game::Doc::Field kSheathedArgs[] = { + Req("sheathed", "bool"), + Opt("hidden", "bool", "false", "True hides the held items."), +}; +const Game::Doc::Field kSheathedRet[] = {Req("sheathed", "bool")}; +const Game::Doc::Field kAutoDressArg[] = {Req("autoDress", "bool")}; +const Game::Doc::Field kAutoDressRet[] = {Req("autoDress", "bool")}; +const Game::Doc::Field kGeoReadyRet[] = {Req("isReady", "bool")}; +const Game::Doc::Function kSetModelByUnit{ + "Shows a copy of the unit, dressed in what it wears.", kUnitArgs, kSuccessRet}; +const Game::Doc::Function kGetModelUnitGUID{"Returns the unit the actor shows.", {}, kGuidRet}; +const Game::Doc::Function kDress{"Dresses the actor in what its unit wears.", {}, {}}; +const Game::Doc::Function kUndress{"Takes everything off the actor.", kUndressArgs, {}}; +const Game::Doc::Function kUndressSlot{ + "Takes the item in one inventory slot off the actor.", kUndressSlotArgs, {}}; +const Game::Doc::Function kSetSheathed{ + "Puts the held items in the hands or the sheaths, or hides them.", kSheathedArgs, {}}; +const Game::Doc::Function kGetSheathed{"Returns whether the held items are sheathed.", {}, + kSheathedRet}; +const Game::Doc::Function kSetAutoDress{ + "Sets whether SetModelByUnit dresses the actor.", kAutoDressArg, {}}; +const Game::Doc::Function kGetAutoDress{ + "Returns whether SetModelByUnit dresses the actor.", {}, kAutoDressRet}; +const Game::Doc::Function kIsGeoReady{ + "Returns whether the model and the item models on it have loaded.", {}, kGeoReadyRet}; + const Game::Doc::Function kSetModelByCreatureDisplayID{ "Shows a creature display, dressed like the creature.", kDisplayArgs, kSuccessRet}; const Game::Doc::Function kSetModelByPath{"Shows a model file.", kPathArgs, kSuccessRet}; @@ -745,6 +962,16 @@ const Game::Doc::Function kGetMaxBoundingBox{ const Game::Doc::Method kMethodDocs[] = { {"SetModelByCreatureDisplayID", &kSetModelByCreatureDisplayID}, {"SetModelByPath", &kSetModelByPath}, + {"SetModelByUnit", &kSetModelByUnit}, + {"GetModelUnitGUID", &kGetModelUnitGUID}, + {"Dress", &kDress}, + {"Undress", &kUndress}, + {"UndressSlot", &kUndressSlot}, + {"SetSheathed", &kSetSheathed}, + {"GetSheathed", &kGetSheathed}, + {"SetAutoDress", &kSetAutoDress}, + {"GetAutoDress", &kGetAutoDress}, + {"IsGeoReady", &kIsGeoReady}, {"ClearModel", &kClearModel}, {"IsLoaded", &kIsLoaded}, {"GetModelPath", &kGetModelPath},