mirror of
https://github.com/brues-code/ClassicAPI.git
synced 2026-11-15 23:50:38 +00:00
* feat(animation): the animation system - AnimationGroup, Animation, Alpha, Scale, Rotation, Translation Later clients animate regions in the renderer; vanilla has none of it, and ported UIs build on it everywhere (Zygor Guides Viewer 2.0: 25 XML groups plus CreateAnimationGroup in its guide window's OnLoad, which stopped there). - Util/Animation.lua (bundled addon): the runtime. Groups and animations with the later-client API (Play/Stop/Pause/Finish, progress and smooth progress, order/startDelay/endDelay/duration timeline, NONE/REPEAT/BOUNCE looping, NONE/IN/OUT/IN_OUT smoothing, maxFramerate, the group and animation scripts). One driver frame ticks playing groups; effects go through the region's own setters and the region's state is restored when the group ends, as later clients never persist them. - animation/Bridge.cpp: CreateAnimationGroup / GetAnimationGroups / StopAnimating on the region base registry, forwarding to the runtime the bundled addon registers via _classicapi_SetAnimationRuntime. - xml loaders: an <Animations> child becomes descriptor tables (attributes, <Scripts> bodies and function= names, <Origin>) handed to runtime.LoadXML before the builder finalizes. Adds OFF_XML_NODE_TEXT. This extends the node-loader hooks of the XML attributes change, which it builds on. Known limits of the 1.12 renderer: Scale pivots on a frame's own anchor point (exact when anchored by its center) and resizes textures anchored by one point; Rotation needs texture:SetRotation (textures, font strings). Group templates (inherits on an AnimationGroup, CreateAnimationGroup's template argument) are not supported yet and are ignored. Verified in-game (1.12.1 client via VanillaFixes, Zygor Guides Viewer 2.0): a probe XML <Animations> group builds with Play(); every AnimationGroup and animation method exists; a Lua-built group plays through 12 updates to progress 1.0 and fires OnFinished; Zygor's step flash group (Alpha) plays and stops, and its guide window finishes its OnLoad. Not checked yet: Scale, Rotation and Translation on screen (Zygor's own groups with them load without error, but no one has watched them play), REPEAT and BOUNCE looping, the smoothing curves, and maxFramerate. * refactor(animation): port 4.3.4's CSimpleAnimGroup/CSimpleAnim to C++ Replace the Lua animation runtime with a C++ port of the 4.3.4 engine classes. Groups and animations are real 1.12 script objects (engine header from ConstructScriptObject, our own vtable), so GetObjectType, IsObjectType, GetName/$parent globals, SetScript/HookScript and the shared __index dispatch all go through the engine's mechanisms. - Group/Anim: the 4.3.4 timeline (orders, delays, looping, cosine smoothing, 1/30 s throttle, pause/stop/finish) and every method. - Region: per-region state ticked from a CSimpleFrame::OnFrameUpdate co-hook (shown frames only); 4.3.4's alpha model (store, reset each tick, add change, restore when the last alpha group ends, children included). CreateAnimationGroup, GetAnimationGroups, StopAnimating. - Xml: <Animations> with 4.3.4's attribute set, inherits/templates, script bodies and function= through SetScriptObjectScript. - Frame::Destruction: a shared hub over the frame destructor; groups die with their frame, and Frame::Alpha moves onto it. Translation, Scale and Rotation keep their values and timing but do not move anything yet. * feat(animation): apply Translation to what regions draw Port 4.3.4's AddAnimTranslation: a frame hands the offset to every texture and font string below it (or only the target), a texture moves the four corners its draw record points at, and a font string moves its text block's string position. Rects, anchors and mouse areas stay put. - StoreAnimPosition / PreOnAnimUpdate / ResetPosition mirror 4.3.4, with the base taken from the engine's own layout (the corner store for textures, inset x layout scale + rect corner for font strings). - Co-hooks on the texture and font string OnFrameSizeChanged callbacks move the base when the layout moves a region mid-animation and keep the animation's offset, as 4.3.4's own legs do. - A frame is marked while it plays a position group, so the end of its last one puts everything below it back. Scale and Rotation still keep their values without drawing. * feat(animation): apply Scale and Rotation to textures Port 4.3.4's AddAnimScale / AddAnimRotation with GetOriginOffset, ApplyScaleAnim and ApplyRotationAnim on the texture's drawn corners: the origin point on the quad, its offset turned along the bottom edge, and a counter-clockwise positive angle. Frames hand both to every texture below them, or only the target. Font strings do not scale or turn yet. * feat(animation): scale and turn font strings Port CSimpleFontString's AddAnimScale / AddAnimRotation: the animated rotation and x scale replace the string's for the update, and the text is baked again when they change (the engine's node invalidate, as 4.3.4 invalidates the CGxString). After the bake, the glyphs are scaled and turned about the node origin, where 4.3.4's InitializeTextLine turns them, with the engine's half-texel UV inset for turned glyphs. The text is stretched rather than drawn at a larger font size. * feat(animation): add Path and ControlPoint Port 4.3.4's CSimplePathAnim and CSimpleControlPoint: a Path moves the region's drawing through its control points with AddAnimTranslation, the points spread evenly over the progress, in straight lines (NONE) or on 4.3.4's Catmull-Rom spline (SMOOTH). ControlPoint is its own script object with offset, order and SetParent; BuildSpline keeps 4.3.4's order shuffle when an order is taken. XML reads `curve` and <ControlPoints>, with templates for CreateControlPoint and `inherits`. One deviation from the decompiled CreateControlPoint: a point made without an order takes the highest order + 1 (as SetParent does), so points made one after another keep their creation order. * fix(texture): keep shape changes made during a position animation Texture:SetRotation and SetVertexOffset wrote the drawn corners, which the next animation update replaced with the corners stored when the group started. While a position group plays they now write the stored corners, so the new shape shows with the animation on top and stays after the group ends. FontString:SetRotation on text an animation has turned or scaled bakes the glyphs again instead of re-turning them in place, so the bake applies both. --------- Co-authored-by: Brues <5278969+brues-code@users.noreply.github.com>
341 lines
13 KiB
C++
341 lines
13 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/>.
|
|
|
|
#include "animation/Object.h"
|
|
|
|
#include "HookSecureFunc.h"
|
|
#include "Offsets.h"
|
|
#include "baselib/Ascii.h"
|
|
#include "frame/ScriptArgs.h"
|
|
|
|
#include <cstring>
|
|
|
|
namespace Animation {
|
|
|
|
namespace {
|
|
|
|
using Construct_t = void(__fastcall *)(void *object);
|
|
using Destruct_t = void(__fastcall *)(void *object);
|
|
using Lock_t = void(__fastcall *)(void *object, void *edx, const char *name);
|
|
using Unlock_t = void(__fastcall *)(void *object, void *edx, const char *name);
|
|
using BaseResolve_t = uint32_t *(__fastcall *)(void *object, void *edx, const char *name);
|
|
using Release_t = void(__stdcall *)(uint32_t *slot);
|
|
using LuaRef_t = int(__fastcall *)(void *L, int t);
|
|
using LuaUnref_t = void(__fastcall *)(void *L, int t, int ref);
|
|
using GetName_t = const char *(__fastcall *)(const void *object);
|
|
|
|
// 4.3.4's CScriptObject type name, which every class's IsA(name) accepts.
|
|
constexpr const char *kObjectTypeName = "Object";
|
|
|
|
Object *Self(void *object) { return static_cast<Object *>(object); }
|
|
|
|
// ---- The vtable (the engine calls these on our objects) --------------------
|
|
|
|
void *__fastcall V_Delete(Object *object, void * /*edx*/, unsigned flags);
|
|
|
|
const char *__fastcall V_GetName(Object *object) { return object->Name(); }
|
|
|
|
// vtable+0x08: push the method `name` of the object's class or a base.
|
|
int __fastcall V_FindMethod(Object *object, void * /*edx*/, void *L, const char *name) {
|
|
for (const Class *c = object->cls; c != nullptr; c = c->base) {
|
|
if (c->methodsRef <= 0)
|
|
continue;
|
|
Game::Lua::RawGetI(L, Game::Lua::REGISTRY_INDEX, c->methodsRef);
|
|
Game::Lua::PushString(L, name);
|
|
Game::Lua::RawGet(L, -2);
|
|
if (Game::Lua::Type(L, -1) != Game::Lua::TYPE_NIL) {
|
|
Game::Lua::Remove(L, -2);
|
|
return 1;
|
|
}
|
|
Game::Lua::SetTop(L, -3);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// vtable+0x0C: 4.3.4's GetScriptByName - the base OnEvent first.
|
|
uint32_t *__fastcall V_ResolveScript(Object *object, void * /*edx*/, const char *name) {
|
|
uint32_t *slot =
|
|
reinterpret_cast<BaseResolve_t>(Offsets::FUN_SCRIPT_OBJECT_BASE_RESOLVE_SCRIPT)(object, nullptr, name);
|
|
return slot != nullptr ? slot : object->ScriptSlot(name);
|
|
}
|
|
|
|
int __fastcall V_IsA(Object *object, void * /*edx*/, int typeId) {
|
|
for (const Class *c = object->cls; c != nullptr; c = c->base)
|
|
if (c->typeId != 0 && c->typeId == typeId)
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
// vtable+0x14 differs per engine class; nothing calls it on ours.
|
|
int __fastcall V_Unused(Object * /*object*/) { return 0; }
|
|
|
|
int __fastcall V_IsAName(Object *object, void * /*edx*/, const char *typeName) {
|
|
if (typeName == nullptr)
|
|
return 0;
|
|
for (const Class *c = object->cls; c != nullptr; c = c->base)
|
|
if (Ascii::EqualCI(typeName, c->typeName))
|
|
return 1;
|
|
return Ascii::EqualCI(typeName, kObjectTypeName) ? 1 : 0;
|
|
}
|
|
|
|
const char *__fastcall V_GetObjectTypeName(Object *object) { return object->cls->typeName; }
|
|
|
|
void *g_vtable[] = {
|
|
reinterpret_cast<void *>(&V_Delete), reinterpret_cast<void *>(&V_GetName),
|
|
reinterpret_cast<void *>(&V_FindMethod), reinterpret_cast<void *>(&V_ResolveScript),
|
|
reinterpret_cast<void *>(&V_IsA), reinterpret_cast<void *>(&V_Unused),
|
|
reinterpret_cast<void *>(&V_IsAName), reinterpret_cast<void *>(&V_GetObjectTypeName),
|
|
};
|
|
|
|
// Slot 0, the engine's deleting destructor. The engine never deletes our
|
|
// objects - their owners (a region, a group) do, by their concrete type.
|
|
void *__fastcall V_Delete(Object *object, void * /*edx*/, unsigned /*flags*/) { return object; }
|
|
|
|
} // namespace
|
|
|
|
// ---- Object -------------------------------------------------------------------
|
|
|
|
Object::Object(const Class &cls, void *parent, const char *name) : cls(&cls), parent(parent) {
|
|
reinterpret_cast<Construct_t>(Offsets::FUN_SCRIPT_OBJECT_CONSTRUCT)(this);
|
|
vtable = g_vtable;
|
|
if (name != nullptr && *name != '\0')
|
|
this->name = CreateName(name, parent);
|
|
reinterpret_cast<Lock_t>(Offsets::FUN_FRAMESCRIPT_OBJECT_SCRIPT_REGISTER)(this, nullptr, Name());
|
|
}
|
|
|
|
bool Object::s_detached = false;
|
|
|
|
Object::~Object() {
|
|
if (s_detached)
|
|
return;
|
|
for (auto &slot : scripts)
|
|
reinterpret_cast<Release_t>(Offsets::FUN_SCRIPT_RELEASE)(slot);
|
|
while (holders > 1)
|
|
reinterpret_cast<Unlock_t>(Offsets::FUN_SCRIPT_OBJECT_UNLOCK)(this, nullptr, nullptr);
|
|
if (holders > 0)
|
|
reinterpret_cast<Unlock_t>(Offsets::FUN_SCRIPT_OBJECT_UNLOCK)(this, nullptr, Name());
|
|
reinterpret_cast<Destruct_t>(Offsets::FUN_SCRIPT_OBJECT_DESTRUCT)(this);
|
|
}
|
|
|
|
uint32_t *Object::ScriptSlot(const char *scriptName) {
|
|
if (scriptName == nullptr)
|
|
return nullptr;
|
|
for (const Class *c = cls; c != nullptr; c = c->base) {
|
|
for (int i = 0; i < c->scriptCount; ++i)
|
|
if (Ascii::EqualCI(scriptName, c->scriptNames[i]))
|
|
return scripts[i];
|
|
if (c->scriptCount > 0)
|
|
break; // only one class in a chain declares the scripts
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void Object::RunScript(int index, int nargs) {
|
|
void *L = Game::Lua::State();
|
|
if (L == nullptr)
|
|
return;
|
|
const int first = Game::Lua::GetTop(L) - nargs + 1;
|
|
if (HasScript(index))
|
|
Frame::ScriptArgs::RunSlotWithValues(L, scripts[index], this, first, nargs);
|
|
Game::Lua::SetTop(L, first - 1);
|
|
}
|
|
|
|
// ---- Helpers --------------------------------------------------------------------
|
|
|
|
int TypeId(Class &cls) { return Game::Lua::FrameScriptTypeId(reinterpret_cast<uintptr_t>(&cls.typeId)); }
|
|
|
|
Object *Resolve(void *L, int idx, Class &cls) {
|
|
return static_cast<Object *>(Game::Lua::ResolveTypedObject(L, idx, reinterpret_cast<uintptr_t>(&cls.typeId)));
|
|
}
|
|
|
|
void Push(void *L, Object *object) { Game::Lua::PushObject(L, object); }
|
|
|
|
bool IsOurs(const void *object) { return object != nullptr && Game::Read<void **>(object, 0) == g_vtable; }
|
|
|
|
std::string CreateName(const char *source, const void *parent) {
|
|
constexpr const char kParent[] = "$parent";
|
|
constexpr size_t kParentLen = sizeof(kParent) - 1;
|
|
if (std::strlen(source) < kParentLen || !Ascii::EqualCI(std::string(source, kParentLen).c_str(), kParent))
|
|
return source;
|
|
std::string name = "Top";
|
|
for (const void *p = parent; p != nullptr;) {
|
|
const char *pname = IsOurs(p) ? static_cast<const Object *>(p)->Name()
|
|
: Game::Read<GetName_t>(Game::Read<void *>(p, 0),
|
|
Offsets::VTBL_SCRIPTOBJECT_GET_NAME)(p);
|
|
if (pname != nullptr && *pname != '\0') {
|
|
name = pname;
|
|
break;
|
|
}
|
|
p = IsOurs(p) ? static_cast<const Object *>(p)->parent : Game::Read<void *>(p, Offsets::OFF_REGION_PARENT);
|
|
}
|
|
return name + (source + kParentLen);
|
|
}
|
|
|
|
void RegisterClass(Class &cls) {
|
|
void *L = Game::Lua::State();
|
|
TypeId(cls);
|
|
Game::Lua::NewTable(L);
|
|
for (int i = 0; i < cls.methodCount; ++i) {
|
|
Game::Lua::PushString(L, cls.methods[i].name);
|
|
Game::Lua::PushCClosure(L, cls.methods[i].func, 0);
|
|
Game::Lua::RawSet(L, -3);
|
|
}
|
|
cls.methodsRef = reinterpret_cast<LuaRef_t>(Offsets::LUA_REF_REF)(L, Game::Lua::REGISTRY_INDEX);
|
|
}
|
|
|
|
// ---- The base methods -----------------------------------------------------------
|
|
|
|
namespace {
|
|
|
|
// The object at self, of any of our classes.
|
|
Object *AnySelf(void *L) {
|
|
if (Game::Lua::Type(L, 1) != Game::Lua::TYPE_TABLE)
|
|
return nullptr;
|
|
void *object = Game::Lua::ResolveObject(L, 1);
|
|
return IsOurs(object) ? Self(object) : nullptr;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int __fastcall Script_GetObjectType(void *L) {
|
|
Object *object = AnySelf(L);
|
|
if (object == nullptr)
|
|
return 0;
|
|
Game::Lua::PushString(L, object->cls->typeName);
|
|
return 1;
|
|
}
|
|
|
|
int __fastcall Script_IsObjectType(void *L) {
|
|
Object *object = AnySelf(L);
|
|
if (object == nullptr)
|
|
return 0;
|
|
if (!Game::Lua::IsString(L, 2)) {
|
|
Game::Lua::Error(L, "Usage: %s:IsObjectType(\"TYPE\")", object->DisplayName());
|
|
return 0;
|
|
}
|
|
if (V_IsAName(object, nullptr, Game::Lua::ToString(L, 2)))
|
|
Game::Lua::PushNumber(L, 1.0);
|
|
else
|
|
Game::Lua::PushNil(L);
|
|
return 1;
|
|
}
|
|
|
|
int __fastcall Script_GetName(void *L) {
|
|
Object *object = AnySelf(L);
|
|
if (object == nullptr)
|
|
return 0;
|
|
Game::Lua::PushString(L, object->Name());
|
|
return 1;
|
|
}
|
|
|
|
int __fastcall Script_GetParent(void *L) {
|
|
Object *object = AnySelf(L);
|
|
if (object == nullptr)
|
|
return 0;
|
|
if (object->parent != nullptr)
|
|
Game::Lua::PushObject(L, object->parent);
|
|
else
|
|
Game::Lua::PushNil(L);
|
|
return 1;
|
|
}
|
|
|
|
// 4.3.4's HasScript: 1 when the class has the script, else nil.
|
|
int __fastcall Script_HasScript(void *L) {
|
|
Object *object = AnySelf(L);
|
|
if (object == nullptr)
|
|
return 0;
|
|
if (!Game::Lua::IsString(L, 2)) {
|
|
Game::Lua::Error(L, "Usage: %s:HasScript(\"type\")", object->DisplayName());
|
|
return 0;
|
|
}
|
|
if (V_ResolveScript(object, nullptr, Game::Lua::ToString(L, 2)) != nullptr)
|
|
Game::Lua::PushNumber(L, 1.0);
|
|
else
|
|
Game::Lua::PushNil(L);
|
|
return 1;
|
|
}
|
|
|
|
// The engine's Script GetScript (0x00774780) on our object: the slot's
|
|
// context becomes current, then the handler (nil for none) is pushed.
|
|
int __fastcall Script_GetScript(void *L) {
|
|
Object *object = AnySelf(L);
|
|
if (object == nullptr)
|
|
return 0;
|
|
if (!Game::Lua::IsString(L, 2)) {
|
|
Game::Lua::Error(L, "Usage: %s:GetScript(\"type\")", object->DisplayName());
|
|
return 0;
|
|
}
|
|
const uint32_t *slot = V_ResolveScript(object, nullptr, Game::Lua::ToString(L, 2));
|
|
if (slot == nullptr) {
|
|
Game::Lua::Error(L, "%s doesn't have a \"%s\" script", object->DisplayName(), Game::Lua::ToString(L, 2));
|
|
return 0;
|
|
}
|
|
if (slot[1] != 0 && Game::Read<int>(Offsets::VAR_FRAMESCRIPT_EXEC_CONTEXT_DEPTH) != 0)
|
|
Game::Ref<uint32_t>(Offsets::VAR_FRAMESCRIPT_EXEC_CONTEXT) = slot[1];
|
|
Game::Lua::RawGetI(L, Game::Lua::REGISTRY_INDEX, static_cast<int>(slot[0]));
|
|
return 1;
|
|
}
|
|
|
|
// The engine's Script SetScript (0x007748D0) on our object: release the old
|
|
// handler, store a ref of the new one (0 for nil) with the current context.
|
|
int __fastcall Script_SetScript(void *L) {
|
|
Object *object = AnySelf(L);
|
|
if (object == nullptr)
|
|
return 0;
|
|
const int handlerType = Game::Lua::Type(L, 3);
|
|
if (!Game::Lua::IsString(L, 2) ||
|
|
(handlerType != Game::Lua::TYPE_FUNCTION && handlerType != Game::Lua::TYPE_NIL &&
|
|
handlerType != -1 /* none */)) {
|
|
Game::Lua::Error(L, "Usage: %s:SetScript(\"type\", function)", object->DisplayName());
|
|
return 0;
|
|
}
|
|
uint32_t *slot = V_ResolveScript(object, nullptr, Game::Lua::ToString(L, 2));
|
|
if (slot == nullptr) {
|
|
Game::Lua::Error(L, "%s doesn't have a \"%s\" script", object->DisplayName(), Game::Lua::ToString(L, 2));
|
|
return 0;
|
|
}
|
|
reinterpret_cast<Release_t>(Offsets::FUN_SCRIPT_RELEASE)(slot);
|
|
Game::Lua::SetTop(L, 3);
|
|
const int ref = reinterpret_cast<LuaRef_t>(Offsets::LUA_REF_REF)(L, Game::Lua::REGISTRY_INDEX);
|
|
slot[0] = ref > 0 ? static_cast<uint32_t>(ref) : 0;
|
|
slot[1] = Game::Read<uint32_t>(Offsets::VAR_FRAMESCRIPT_EXEC_CONTEXT);
|
|
return 0;
|
|
}
|
|
|
|
// Frame HookScript's (frame/Modern.cpp) on our object: no handler yet -> the
|
|
// hook is the handler; else the hooksecurefunc wrapper over (old, hook).
|
|
int __fastcall Script_HookScript(void *L) {
|
|
Object *object = AnySelf(L);
|
|
if (object == nullptr)
|
|
return 0;
|
|
if (!Game::Lua::IsString(L, 2) || Game::Lua::Type(L, 3) != Game::Lua::TYPE_FUNCTION) {
|
|
Game::Lua::Error(L, "Usage: %s:HookScript(\"type\", function)", object->DisplayName());
|
|
return 0;
|
|
}
|
|
Game::Lua::SetTop(L, 3); // (self, type, hook)
|
|
Game::Lua::PushValue(L, 1);
|
|
Game::Lua::PushValue(L, 2);
|
|
Game::Lua::PushCClosure(L, &Script_GetScript, 0);
|
|
Game::Lua::Insert(L, -3);
|
|
Game::Lua::Call(L, 2, 1); // (self, type, hook, old)
|
|
if (Game::Lua::Type(L, 4) == Game::Lua::TYPE_FUNCTION) {
|
|
Game::Lua::Insert(L, 3); // (self, type, old, hook)
|
|
HookSecureFunc::PushWrapper(L); // (self, type, wrapper)
|
|
} else {
|
|
Game::Lua::SetTop(L, 3);
|
|
}
|
|
return Script_SetScript(L);
|
|
}
|
|
|
|
} // namespace Animation
|