fix: destroyed skeleton config access on skeleton reload

This commit is contained in:
MihailRis 2026-04-05 10:31:44 +03:00
parent 33db2633a2
commit 9893c09fd5
9 changed files with 35 additions and 22 deletions

View file

@ -7,9 +7,9 @@ using namespace rigging;
NamedSkeletons::NamedSkeletons() = default;
std::shared_ptr<rigging::Skeleton> NamedSkeletons::createSkeleton(
const std::string& name, const SkeletonConfig* config
const std::string& name, std::shared_ptr<const SkeletonConfig> config
) {
auto skeleton = std::make_shared<Skeleton>(config);
auto skeleton = std::make_shared<Skeleton>(std::move(config));
skeletons[name] = skeleton;
return skeleton;
}

View file

@ -14,7 +14,8 @@ public:
NamedSkeletons();
std::shared_ptr<rigging::Skeleton> createSkeleton(
const std::string& name, const rigging::SkeletonConfig* config
const std::string& name,
std::shared_ptr<const rigging::SkeletonConfig> config
);
rigging::Skeleton* getSkeleton(const std::string& name);

View file

@ -113,7 +113,9 @@ WorldRenderer::WorldRenderer(
content.getDefaults()["hand-skeleton"].asString()
);
hands = std::make_unique<HandsRenderer>(
assets, *modelBatch, skeletons->createSkeleton("hand", &skeletonConfig)
assets,
*modelBatch,
skeletons->createSkeleton("hand", skeletonConfig.shared_from_this())
);
shadowMapping = std::make_unique<Shadows>(level);
debugLines = std::make_unique<DebugLinesRenderer>(level);

View file

@ -108,13 +108,13 @@ static int l_set_skeleton(lua::State* L) {
}
if (auto entity = get_entity(L, 1)) {
std::string skeletonName = lua::require_string(L, 2);
auto rigConfig = assets->get<rigging::SkeletonConfig>(skeletonName);
auto rigConfig = assets->getShared<rigging::SkeletonConfig>(skeletonName);
if (rigConfig == nullptr) {
throw std::runtime_error(
"skeleton not found '" + skeletonName + "'"
);
}
entity->setRig(rigConfig);
entity->setRig(std::move(rigConfig));
}
return 0;
}

View file

@ -155,7 +155,7 @@ void Entities::loadEntity(const dv::value& map, Entity entity) {
std::string skeletonName = skeleton->config->getName();
map.at("skeleton-name").get(skeletonName);
if (skeletonName != skeleton->config->getName()) {
skeleton->config = assets->get<rigging::SkeletonConfig>(skeletonName);
skeleton->config = assets->getShared<rigging::SkeletonConfig>(skeletonName);
}
if (auto foundSkeleton = map.at(COMP_SKELETON)) {
skeleton->deserialize(*foundSkeleton);
@ -470,7 +470,7 @@ void Entities::render(
continue;
}
const auto* rigConfig = skeleton.config;
const auto& rigConfig = skeleton.config;
if (rigConfig) {
rigConfig->render(
assets, batch, skeleton, transform.rot, pos, size

View file

@ -37,15 +37,9 @@ rigging::Skeleton* Entity::getSkeleton() const {
return registry.try_get<rigging::Skeleton>(entity);
}
void Entity::setRig(const rigging::SkeletonConfig* rigConfig) {
void Entity::setRig(std::shared_ptr<const rigging::SkeletonConfig> rigConfig) {
auto& skeleton = registry.get<rigging::Skeleton>(entity);
skeleton.config = rigConfig;
skeleton.pose.matrices.resize(
rigConfig->getBones().size(), glm::mat4(1.0f)
);
skeleton.calculated.matrices.resize(
rigConfig->getBones().size(), glm::mat4(1.0f)
);
skeleton.setConfig(std::move(rigConfig));
}
dv::value Entity::serialize() const {

View file

@ -60,7 +60,7 @@ public:
rigging::Skeleton* getSkeleton() const;
void setRig(const rigging::SkeletonConfig* rigConfig);
void setRig(std::shared_ptr<const rigging::SkeletonConfig> rigConfig);
entityid_t getUID() const;

View file

@ -39,7 +39,7 @@ void Bone::setModel(const std::string& name) {
model = {name, nullptr, true};
}
Skeleton::Skeleton(const SkeletonConfig* config)
Skeleton::Skeleton(std::shared_ptr<const SkeletonConfig> config)
: config(config),
pose(config->getBones().size()),
calculated(config->getBones().size()),
@ -86,6 +86,16 @@ void Skeleton::deserialize(const dv::value& root) {
}
}
void Skeleton::setConfig(std::shared_ptr<const SkeletonConfig> rigConfig) {
config = std::move(rigConfig);
pose.matrices.resize(
config->getBones().size(), glm::mat4(1.0f)
);
calculated.matrices.resize(
config->getBones().size(), glm::mat4(1.0f)
);
}
static void get_all_nodes(std::vector<Bone*>& nodes, Bone* node) {
nodes[node->getIndex()] = node;
for (auto& subnode : node->getBones()) {
@ -97,6 +107,7 @@ SkeletonConfig::SkeletonConfig(
const std::string& name, std::unique_ptr<Bone> root, size_t nodesCount
)
: name(name), root(std::move(root)), nodes(nodesCount) {
assert(this->root.get() != nullptr);
get_all_nodes(nodes, this->root.get());
}
@ -159,6 +170,9 @@ void SkeletonConfig::render(
const glm::vec3& position,
const glm::vec3& scale
) const {
if (skeleton.config->root == nullptr) {
return;
}
update(skeleton, rotation, position, scale);
if (!skeleton.visible) {

View file

@ -78,7 +78,7 @@ namespace rigging {
};
struct Skeleton {
const SkeletonConfig* config;
std::shared_ptr<const SkeletonConfig> config;
Pose pose;
Pose calculated;
std::vector<BoneFlags> flags;
@ -89,13 +89,15 @@ namespace rigging {
util::VecInterpolation<3, float> interpolation {false};
Skeleton(const SkeletonConfig* config);
Skeleton(std::shared_ptr<const SkeletonConfig> config);
dv::value serialize(bool saveTextures, bool savePose) const;
void deserialize(const dv::value& root);
void setConfig(std::shared_ptr<const SkeletonConfig> config);
};
class SkeletonConfig {
class SkeletonConfig : public std::enable_shared_from_this<SkeletonConfig> {
std::string name;
std::unique_ptr<Bone> root;
std::unordered_map<std::string, size_t> indices;
@ -135,7 +137,7 @@ namespace rigging {
) const;
Skeleton instance() const {
return Skeleton(this);
return Skeleton(shared_from_this());
}
const Bone* find(std::string_view str) const;