First ROCK consumer
This observer registers one owner, binds Hands for reading and Animation for phase callbacks, and copies the right hand's rendered pose once per frame at the Presented phase. It changes nothing in the game.
HandObserver.cpp
#include <Windows.h>
#include <ROCK/Client.h>
#include <ROCK/Animation.h>
#include <ROCK/Hands.h>
namespace api = rock::api;
class HandObserver {
api::Client client_;
const api::hands::Api* hands_{};
const api::animation::Api* animation_{};
std::uint64_t callback_{};
api::hands::PresentedHandPose lastPose_{};
bool valid_{};
static void ROCK_CALL onPresented(const api::animation::AnimationPhaseContext* context, void* userData) noexcept
{
auto& self = *static_cast<HandObserver*>(userData);
self.valid_ = false;
const auto ready = static_cast<std::uint32_t>(api::animation::AnimationPhaseContextFlag::ProviderReady);
if (!context || (context->flags & ready) == 0) return;
api::hands::PresentedHandPose pose{};
if (self.hands_->getPresentedHandPose(self.client_.owner(), api::Hand::Right, &pose) != api::Status::Ok) return;
const auto required = static_cast<std::uint32_t>(api::hands::PresentedHandPoseFlag::Valid) |
static_cast<std::uint32_t>(api::hands::PresentedHandPoseFlag::HandWorldValid);
if ((pose.flags & required) != required || pose.frameIndex != context->frameIndex) return;
self.lastPose_ = pose;
self.valid_ = true;
}
public:
api::Status start() noexcept
{
auto* module = GetModuleHandleW(L"ROCK.dll");
auto query = module ? reinterpret_cast<api::QueryInterfaceV1>(GetProcAddress(module, api::kQueryExportName)) : nullptr;
if (!query) return api::Status::NotReady; // ROCK absent: run without it
auto status = client_.connect(query, "Example hand observer");
if (status == api::Status::Ok)
status = client_.acquire(static_cast<std::uint32_t>(api::hands::Permission::Read), hands_);
if (status == api::Status::Ok)
status = client_.acquire(static_cast<std::uint32_t>(api::animation::Permission::Callbacks), animation_);
if (status == api::Status::Ok) {
api::animation::PhaseCallbackRegistration registration{};
registration.phaseMask = api::animation::phaseBit(api::animation::AnimationPhase::Presented);
registration.callback = &onPresented;
registration.userData = this;
status = animation_->registerAnimationPhaseCallback(client_.owner(), ®istration, &callback_);
}
if (status != api::Status::Ok) (void)stop();
return status;
}
// Game thread. Releases the owner and everything it holds, the callback included.
api::Status stop() noexcept
{
const auto status = client_.close();
if (status != api::Status::Ok) return status;
hands_ = nullptr;
animation_ = nullptr;
callback_ = 0;
valid_ = false;
return api::Status::Ok;
}
// Game thread; this object is not thread-safe.
bool copyLastPose(api::hands::PresentedHandPose& output) const noexcept
{
if (!valid_) return false;
output = lastPose_;
return true;
}
};
Notes
- Call
start()after all plugins have loaded, for example on F4SE's post-load message. Phase callbacks may be registered before the first frame; they run once FRIK's frames begin. stop()must run on the game thread, and before the object is destroyed:Clienthas no destructor that unregisters. Ifstop()fails, keep the object alive and retry.- The owner name is your mod's identity. A second
connectwith the same name returnsOwnerConflict. - Every record you pass or receive is value-initialized (
{}); itssizeandversiondefaults are what ROCK expects. copyLastPoseis a copy stamped with its frame and generations, not a live view. CompareworldGeneration,skeletonGenerationandproviderGenerationwith a currentSamplebefore combining it with later reads.
ROCK's own build compiles a minimal consumer (tests/ConsumerExample.cpp) against these headers in its public header tests.
Next: discovery and permissions, then the runtime contract.