File: StdSkeletons.cpp

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (223 lines) | stat: -rw-r--r-- 6,020 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Copyright (C) 2021 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. 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 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. 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 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "precompiled.h"

#include "libxml/parser.h"
#include "libxml/xmlerror.h"

#include "StdSkeletons.h"

#include "CommonConvert.h"

#include "FUtils/FUXmlParser.h"

#include <map>

namespace
{
	struct SkeletonMap : public std::map<std::string, const Skeleton*>
	{
		~SkeletonMap()
		{
			for (iterator it = begin(); it != end(); ++it)
				delete it->second;
		}
	};

	SkeletonMap g_StandardSkeletons;
	SkeletonMap g_MappedSkeletons;

	struct Bone
	{
		std::string parent;
		std::string name;
		int targetId;
		int realTargetId;
	};
}

struct Skeleton_impl
{
	std::string title;
	std::vector<Bone> bones;
	const Skeleton* target;
};

Skeleton::Skeleton() : m(new Skeleton_impl) { }
Skeleton::~Skeleton() { }

const Skeleton* Skeleton::FindSkeleton(const std::string& name)
{
	return g_MappedSkeletons[name];
}

int Skeleton::GetBoneID(const std::string& name) const
{
	for (size_t i = 0; i < m->bones.size(); ++i)
		if (m->bones[i].name == name)
			return m->bones[i].targetId;
	return -1;
}

int Skeleton::GetRealBoneID(const std::string& name) const
{
	for (size_t i = 0; i < m->bones.size(); ++i)
		if (m->bones[i].name == name)
			return m->bones[i].realTargetId;
	return -1;
}

int Skeleton::GetBoneCount() const
{
	return (int)m->target->m->bones.size();
}

namespace
{
	bool AlreadyUsedTargetBone(const std::vector<Bone>& bones, int targetId)
	{
		for (size_t i = 0; i < bones.size(); ++i)
			if (bones[i].targetId == targetId)
				return true;
		return false;
	}

	// Recursive helper function used by LoadSkeletonData
	void LoadSkeletonBones(xmlNode* parent, std::vector<Bone>& bones, const Skeleton* targetSkeleton, const std::string& targetName)
	{
		xmlNodeList boneNodes;
		FUXmlParser::FindChildrenByType(parent, "bone", boneNodes);
		for (xmlNodeList::iterator boneNode = boneNodes.begin(); boneNode != boneNodes.end(); ++boneNode)
		{
			std::string name (FUXmlParser::ReadNodeProperty(*boneNode, "name"));

			Bone b;
			b.name = name;

			std::string newTargetName = targetName;

			if (targetSkeleton)
			{
				xmlNode* targetNode = FUXmlParser::FindChildByType(*boneNode, "target");
				if (targetNode)
					newTargetName = FUXmlParser::ReadNodeContentFull(targetNode);
				// else fall back to the parent node's target

				b.targetId = targetSkeleton->GetBoneID(newTargetName);
				REQUIRE(b.targetId != -1, "skeleton bone target matches some standard_skeleton bone name");

				if (AlreadyUsedTargetBone(bones, b.targetId))
					b.realTargetId = -1;
				else
					b.realTargetId = b.targetId;
			}
			else
			{
				// No target - this is a standard skeleton

				b.targetId = (int)bones.size();
				b.realTargetId = b.targetId;
			}

			bones.push_back(b);

			LoadSkeletonBones(*boneNode, bones, targetSkeleton, newTargetName);
		}
	}

	void LoadSkeletonData(xmlNode* root)
	{
		xmlNodeList skeletonNodes;
		FUXmlParser::FindChildrenByType(root, "standard_skeleton", skeletonNodes);
		FUXmlParser::FindChildrenByType(root, "skeleton", skeletonNodes);
		for (xmlNodeList::iterator skeletonNode = skeletonNodes.begin();
			skeletonNode != skeletonNodes.end(); ++skeletonNode)
		{
			std::unique_ptr<Skeleton> skeleton = std::make_unique<Skeleton>();

			std::string title (FUXmlParser::ReadNodeProperty(*skeletonNode, "title"));

			skeleton->m->title = title;

			if (IsEquivalent((*skeletonNode)->name, "standard_skeleton"))
			{
				skeleton->m->target = NULL;

				LoadSkeletonBones(*skeletonNode, skeleton->m->bones, NULL, "");

				std::string id (FUXmlParser::ReadNodeProperty(*skeletonNode, "id"));
				REQUIRE(! id.empty(), "standard_skeleton has id");

				g_StandardSkeletons[id] = skeleton.release();
			}
			else
			{
				// Non-standard skeletons need to choose a standard skeleton
				// as their target to be mapped onto

				std::string target (FUXmlParser::ReadNodeProperty(*skeletonNode, "target"));
				const Skeleton* targetSkeleton = g_StandardSkeletons[target];
				REQUIRE(targetSkeleton != NULL, "skeleton target matches some standard_skeleton id");

				skeleton->m->target = targetSkeleton;

				LoadSkeletonBones(*skeletonNode, skeleton->m->bones, targetSkeleton, "");

				// Currently the only supported identifier is a precise name match,
				// so just look for that
				xmlNode* identifier = FUXmlParser::FindChildByType(*skeletonNode, "identifier");
				REQUIRE(identifier != NULL, "skeleton has <identifier>");
				xmlNode* identRoot = FUXmlParser::FindChildByType(identifier, "root");
				REQUIRE(identRoot != NULL, "skeleton identifier has <root>");
				std::string identRootName (FUXmlParser::ReadNodeContentFull(identRoot));

				g_MappedSkeletons[identRootName] = skeleton.release();
			}
		}
	}
}

void errorHandler(void* ctx, const char* msg, ...);

void Skeleton::LoadSkeletonDataFromXml(const char* xmlData, size_t xmlLength, std::string& xmlErrors)
{
	xmlDoc* doc = NULL;
	try
	{
		xmlSetGenericErrorFunc(&xmlErrors, &errorHandler);
		doc = xmlParseMemory(xmlData, (int)xmlLength);
		if (doc)
		{
			xmlNode* root = xmlDocGetRootElement(doc);
			LoadSkeletonData(root);
			xmlFreeDoc(doc);
			doc = NULL;
		}
		xmlSetGenericErrorFunc(NULL, NULL);
	}
	catch (const ColladaException&)
	{
		if (doc)
			xmlFreeDoc(doc);
		xmlSetGenericErrorFunc(NULL, NULL);
		throw;
	}

	if (! xmlErrors.empty())
		throw ColladaException("XML parsing failed");
}