LCOV - code coverage report
Current view: top level - source/collada - StdSkeletons.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 23 95 24.2 %
Date: 2026-12-03 01:19:39 Functions: 3 10 30.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "StdSkeletons.h"
      21             : 
      22             : #include "CommonConvert.h"
      23             : 
      24             : #include <FUtils/FUXmlParser.h>
      25             : #include <libxml/parser.h>
      26             : #include <libxml/tree.h>
      27             : #include <libxml/xmlerror.h>
      28             : #include <map>
      29             : #include <utility>
      30             : #include <vector>
      31             : 
      32             : namespace
      33             : {
      34             :     struct SkeletonMap : public std::map<std::string, const Skeleton*>
      35             :     {
      36           2 :         ~SkeletonMap()
      37             :         {
      38           2 :             for (iterator it = begin(); it != end(); ++it)
      39           0 :                 delete it->second;
      40           2 :         }
      41             :     };
      42             : 
      43             :     SkeletonMap g_StandardSkeletons;
      44             :     SkeletonMap g_MappedSkeletons;
      45             : 
      46           0 :     struct Bone
      47             :     {
      48             :         std::string parent;
      49             :         std::string name;
      50             :         int targetId;
      51             :         int realTargetId;
      52             :     };
      53             : }
      54             : 
      55           0 : struct Skeleton_impl
      56             : {
      57             :     std::string title;
      58             :     std::vector<Bone> bones;
      59             :     const Skeleton* target;
      60             : };
      61             : 
      62           0 : Skeleton::Skeleton() : m(new Skeleton_impl) { }
      63           0 : Skeleton::~Skeleton() { }
      64             : 
      65           0 : const Skeleton* Skeleton::FindSkeleton(const std::string& name)
      66             : {
      67           0 :     return g_MappedSkeletons[name];
      68             : }
      69             : 
      70           0 : int Skeleton::GetBoneID(const std::string& name) const
      71             : {
      72           0 :     for (size_t i = 0; i < m->bones.size(); ++i)
      73           0 :         if (m->bones[i].name == name)
      74           0 :             return m->bones[i].targetId;
      75             :     return -1;
      76             : }
      77             : 
      78           0 : int Skeleton::GetRealBoneID(const std::string& name) const
      79             : {
      80           0 :     for (size_t i = 0; i < m->bones.size(); ++i)
      81           0 :         if (m->bones[i].name == name)
      82           0 :             return m->bones[i].realTargetId;
      83             :     return -1;
      84             : }
      85             : 
      86           0 : int Skeleton::GetBoneCount() const
      87             : {
      88           0 :     return (int)m->target->m->bones.size();
      89             : }
      90             : 
      91             : namespace
      92             : {
      93           0 :     bool AlreadyUsedTargetBone(const std::vector<Bone>& bones, int targetId)
      94             :     {
      95           0 :         for (size_t i = 0; i < bones.size(); ++i)
      96           0 :             if (bones[i].targetId == targetId)
      97             :                 return true;
      98             :         return false;
      99             :     }
     100             : 
     101             :     // Recursive helper function used by LoadSkeletonData
     102           0 :     void LoadSkeletonBones(xmlNode* parent, std::vector<Bone>& bones, const Skeleton* targetSkeleton, const std::string& targetName)
     103             :     {
     104           0 :         xmlNodeList boneNodes;
     105           0 :         FUXmlParser::FindChildrenByType(parent, "bone", boneNodes);
     106           0 :         for (xmlNodeList::iterator boneNode = boneNodes.begin(); boneNode != boneNodes.end(); ++boneNode)
     107             :         {
     108           0 :             std::string name (FUXmlParser::ReadNodeProperty(*boneNode, "name"));
     109             : 
     110           0 :             Bone b;
     111           0 :             b.name = name;
     112             : 
     113           0 :             std::string newTargetName = targetName;
     114             : 
     115           0 :             if (targetSkeleton)
     116             :             {
     117           0 :                 xmlNode* targetNode = FUXmlParser::FindChildByType(*boneNode, "target");
     118           0 :                 if (targetNode)
     119           0 :                     newTargetName = FUXmlParser::ReadNodeContentFull(targetNode);
     120             :                 // else fall back to the parent node's target
     121             : 
     122           0 :                 b.targetId = targetSkeleton->GetBoneID(newTargetName);
     123           0 :                 REQUIRE(b.targetId != -1, "skeleton bone target matches some standard_skeleton bone name");
     124             : 
     125           0 :                 if (AlreadyUsedTargetBone(bones, b.targetId))
     126           0 :                     b.realTargetId = -1;
     127             :                 else
     128           0 :                     b.realTargetId = b.targetId;
     129             :             }
     130             :             else
     131             :             {
     132             :                 // No target - this is a standard skeleton
     133             : 
     134           0 :                 b.targetId = (int)bones.size();
     135           0 :                 b.realTargetId = b.targetId;
     136             :             }
     137             : 
     138           0 :             bones.push_back(b);
     139             : 
     140           0 :             LoadSkeletonBones(*boneNode, bones, targetSkeleton, newTargetName);
     141           0 :         }
     142           0 :     }
     143             : 
     144           6 :     void LoadSkeletonData(xmlNode* root)
     145             :     {
     146           6 :         xmlNodeList skeletonNodes;
     147           6 :         FUXmlParser::FindChildrenByType(root, "standard_skeleton", skeletonNodes);
     148           6 :         FUXmlParser::FindChildrenByType(root, "skeleton", skeletonNodes);
     149           6 :         for (xmlNodeList::iterator skeletonNode = skeletonNodes.begin();
     150           6 :             skeletonNode != skeletonNodes.end(); ++skeletonNode)
     151             :         {
     152           0 :             std::unique_ptr<Skeleton> skeleton = std::make_unique<Skeleton>();
     153             : 
     154           0 :             std::string title (FUXmlParser::ReadNodeProperty(*skeletonNode, "title"));
     155             : 
     156           0 :             skeleton->m->title = title;
     157             : 
     158           0 :             if (IsEquivalent((*skeletonNode)->name, "standard_skeleton"))
     159             :             {
     160           0 :                 skeleton->m->target = NULL;
     161             : 
     162           0 :                 LoadSkeletonBones(*skeletonNode, skeleton->m->bones, NULL, "");
     163             : 
     164           0 :                 std::string id (FUXmlParser::ReadNodeProperty(*skeletonNode, "id"));
     165           0 :                 REQUIRE(! id.empty(), "standard_skeleton has id");
     166             : 
     167           0 :                 g_StandardSkeletons[id] = skeleton.release();
     168           0 :             }
     169             :             else
     170             :             {
     171             :                 // Non-standard skeletons need to choose a standard skeleton
     172             :                 // as their target to be mapped onto
     173             : 
     174           0 :                 std::string target (FUXmlParser::ReadNodeProperty(*skeletonNode, "target"));
     175           0 :                 const Skeleton* targetSkeleton = g_StandardSkeletons[target];
     176           0 :                 REQUIRE(targetSkeleton != NULL, "skeleton target matches some standard_skeleton id");
     177             : 
     178           0 :                 skeleton->m->target = targetSkeleton;
     179             : 
     180           0 :                 LoadSkeletonBones(*skeletonNode, skeleton->m->bones, targetSkeleton, "");
     181             : 
     182             :                 // Currently the only supported identifier is a precise name match,
     183             :                 // so just look for that
     184           0 :                 xmlNode* identifier = FUXmlParser::FindChildByType(*skeletonNode, "identifier");
     185           0 :                 REQUIRE(identifier != NULL, "skeleton has <identifier>");
     186           0 :                 xmlNode* identRoot = FUXmlParser::FindChildByType(identifier, "root");
     187           0 :                 REQUIRE(identRoot != NULL, "skeleton identifier has <root>");
     188           0 :                 std::string identRootName (FUXmlParser::ReadNodeContentFull(identRoot));
     189             : 
     190           0 :                 g_MappedSkeletons[identRootName] = skeleton.release();
     191           0 :             }
     192           0 :         }
     193           6 :     }
     194             : }
     195             : 
     196             : void errorHandler(void* ctx, const char* msg, ...);
     197             : 
     198           7 : void Skeleton::LoadSkeletonDataFromXml(const char* xmlData, size_t xmlLength, std::string& xmlErrors)
     199             : {
     200           7 :     xmlDoc* doc = NULL;
     201           7 :     try
     202             :     {
     203           7 :         xmlSetGenericErrorFunc(&xmlErrors, &errorHandler);
     204           7 :         doc = xmlParseMemory(xmlData, (int)xmlLength);
     205           7 :         if (doc)
     206             :         {
     207           6 :             xmlNode* root = xmlDocGetRootElement(doc);
     208           6 :             LoadSkeletonData(root);
     209           6 :             xmlFreeDoc(doc);
     210             :             doc = NULL;
     211             :         }
     212           7 :         xmlSetGenericErrorFunc(NULL, NULL);
     213             :     }
     214           0 :     catch (const ColladaException&)
     215             :     {
     216           0 :         if (doc)
     217           0 :             xmlFreeDoc(doc);
     218           0 :         xmlSetGenericErrorFunc(NULL, NULL);
     219           0 :         throw;
     220           0 :     }
     221             : 
     222           7 :     if (! xmlErrors.empty())
     223           2 :         throw ColladaException("XML parsing failed");
     224           6 : }

Generated by: LCOV version 1.16