forked from enigma-dev/enigma-dev
/
tmx.cpp
310 lines (266 loc) · 12.6 KB
/
tmx.cpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "tmx.h"
#include "tsx.h"
#include "pugixml.hpp"
#include <google/protobuf/text_format.h>
using CppType = google::protobuf::FieldDescriptor::CppType;
namespace egm {
namespace {
class TMXMapLoader : public pugi::xml_tree_walker {
public:
TMXMapLoader(buffers::TreeNode *root, const fs::path &fPath) : tmxPath(fPath) {
root->set_name("/");
nodes.push_back(root);
tiledEnigmaResourceMap["map"] = "room";
// tiledEnigmaResourceMap["tileset"] = "background";
}
private:
std::vector<buffers::TreeNode *> nodes;
fs::path tmxPath;
std::unordered_map<std::string, std::string> tiledEnigmaResourceMap;
std::unordered_map<std::string, buffers::TreeNode *> resourceFolderRefs;
// helps in generating ids for repetable messages present in resources like Room.proto
std::unordered_map<std::string, int> resourceTypeIdCountMap;
std::unordered_map<std::string, buffers::resources::Background *> backgroundNameMsgPtrMap;
std::map<int, std::string> tilesetIdSourcePathMap;
// TODO: Remove this hack with "resource name generator"
int idx=0;
virtual bool for_each(pugi::xml_node &xmlNode) {
if(tiledEnigmaResourceMap.find(xmlNode.name()) == tiledEnigmaResourceMap.end()) {
errStream << "Unsupported resource type: " << xmlNode.name() << std::endl;
return true;
}
std::string resType = tiledEnigmaResourceMap[xmlNode.name()];
std::string name = xmlNode.attribute("name").value();
// TODO: Remove this hack with "resource name generator"
if(name.empty())
name = resType+std::to_string(idx++);
buffers::TreeNode *folderNode;
if(resourceFolderRefs.find(resType) == resourceFolderRefs.end()) {
folderNode = nodes.back()->mutable_folder()->add_children();
std::string fixName = resType + 's'; // add an 's' at the end
fixName[0] -= 32; // make first character capital
folderNode->set_name(fixName);
}
else
folderNode = resourceFolderRefs[resType];
if(folderNode == NULL) {
std::cout << "Folder with name \""<< resType <<"\" not found" << std::endl;
return false;
}
// only visit tileset/background in first iteration, when visited attribute is not set
if(resType == "background" && xmlNode.attribute("visited").as_bool() == false) {
// if image attribute exist then this tileset is contained inside a map and is referring to an external tileset
std::string tilesetSrc = xmlNode.attribute("source").value();
int tilesetFirstGid = xmlNode.attribute("firstgid").as_int(-1);
if(!tilesetSrc.empty() && tilesetFirstGid != -1) {
std::string parentDirPath = tmxPath.parent_path().string()+"/";
std::string tsxPath = parentDirPath + tilesetSrc;
// load tsx xml
pugi::xml_document tilesetDoc;
if(!tilesetDoc.load_file(tsxPath.c_str())) {
std::cout << "Could not found tsx file with path: " << tsxPath << std::endl;
return false;
}
TSXTilesetLoader background_walker(tsxPath, nodes, folderNode, backgroundNameMsgPtrMap);
tilesetDoc.traverse(background_walker);
xmlNode.append_attribute("visited") = true;
tilesetIdSourcePathMap[tilesetFirstGid] = tilesetSrc;
}
else {
std::cout << "Fatal error: Tileset source or firstgid is invalid." << std::endl;
}
}
else if(resType == "room") {
buffers::TreeNode *resNode = folderNode->mutable_folder()->add_children();
resNode->set_name(name);
AddResource(resNode, resType, xmlNode);
// correct width and height, convert from no. of tiles to no. of pixels
resNode->mutable_room()->set_width(resNode->room().width() * resNode->room().hsnap());
resNode->mutable_room()->set_height(resNode->room().height() * resNode->room().vsnap());
xmlNode.append_attribute("visited") = true;
}
return true;
}
void AddResource(buffers::TreeNode *protoNode, std::string resType, pugi::xml_node &xmlNode) {
using buffers::TreeNode;
using FactoryFunction = std::function<google::protobuf::Message *(TreeNode*)>;
using FactoryMap = std::unordered_map<std::string, FactoryFunction>;
static const FactoryMap factoryMap({
{ "sprite", &TreeNode::mutable_sprite },
{ "sound", &TreeNode::mutable_sound },
{ "background", &TreeNode::mutable_background },
{ "path", &TreeNode::mutable_path },
{ "script", &TreeNode::mutable_script },
{ "shader", &TreeNode::mutable_shader },
{ "font", &TreeNode::mutable_font },
{ "timeline", &TreeNode::mutable_timeline },
{ "object", &TreeNode::mutable_object },
{ "room", &TreeNode::mutable_room },
{ "datafile", &TreeNode::mutable_include },
{ "Config", &TreeNode::mutable_settings },
});
auto createFunc = factoryMap.find(resType);
if (createFunc != factoryMap.end()) {
auto *res = createFunc->second(protoNode);
PackRes(xmlNode, res, resType);
return;
}
protoNode->mutable_unknown();
errStream << "Unsupported resource type: " << resType << " " << xmlNode.value() << std::endl;
}
void PackRes(pugi::xml_node &xmlNode, google::protobuf::Message *m, const std::string& resType, std::string fieldPrefix = "") {
const google::protobuf::Descriptor *desc = m->GetDescriptor();
const google::protobuf::Reflection *refl = m->GetReflection();
for (int i = 0; i < desc->field_count(); i++) {
const google::protobuf::FieldDescriptor *field = desc->field(i);
const google::protobuf::OneofDescriptor *oneof = field->containing_oneof();
if (oneof && refl->HasOneof(*m, oneof)) continue;
const google::protobuf::FieldOptions opts = field->options();
// tmx_option_string is used to fetch correct attribute from current xml node
std::string tsxPropertyName = fieldPrefix + opts.GetExtension(buffers::tmx);
pugi::xml_attribute attr;
// TODO: needs improvement
// if forward slash '/' is found in tmx_option_string then correct attribute is present in child node
// for example in Background.proto fields with options [(tmx) = "image/source"] has a forward
// slash in it and it indicates that in .tsx file, source should be fetched from child image node
std::size_t splitPos = tsxPropertyName.find("/");
if(splitPos != std::string::npos) {
const std::string childNodeName = tsxPropertyName.substr(0, splitPos);
const std::string childNodeAttrName = tsxPropertyName.substr(splitPos + 1);
pugi::xml_node child = xmlNode.child(childNodeName.c_str());
if(!child.empty())
attr = child.attribute(childNodeAttrName.c_str());
}
// otherwise get the attribute directly
else {
attr = xmlNode.attribute(tsxPropertyName.c_str());
}
// if(attr.empty())
// continue;
if(field->name() == "id"){
// not sure what Getextension returns in cases when id_start extension is absent
int id = opts.GetExtension(buffers::id_start) + resourceTypeIdCountMap[m->GetTypeName()]++;
outStream << "Setting " << field->name() << " (" << field->type_name() << ") as " << id << std::endl;
refl->SetInt32(m, field, id);
}
else if(field->is_repeated()) {
switch(field->cpp_type()) {
case CppType::CPPTYPE_MESSAGE: {
// only visit repeated message "tiles/tile" when first traversal is done, as we need to create backgrounds before referencing them
if(resType == "room" && xmlNode.attribute("visited").as_bool() == true && field->name() == "tiles") {
std::cout<<"Repeated fields as a message: "<<field->name()<<std::endl;
pugi::xml_object_range<pugi::xml_named_node_iterator> objectGroups = xmlNode.children("objectgroup");
// iterate over all objectGroup
for(pugi::xml_node objectGroupChild : objectGroups) {
// iterate over all children, which is most probably objects, create new tiles out of them
pugi::xml_object_range<pugi::xml_named_node_iterator> objects = objectGroupChild.children("object");
for(pugi::xml_node objectChild : objects) {
google::protobuf::Message *msg = refl->AddMessage(m, field);
PackRes(objectChild, msg, "tiles/tile");
// let's update some important values
buffers::resources::Room::Tile *tile = static_cast<buffers::resources::Room::Tile *>(msg);
unsigned int gid = objectChild.attribute("gid").as_uint();
int localId = ConvertGlobalTileIdToLocal(gid);
std::cout<<"localId: "<<localId<<std::endl;
tile->set_background_name("");
tile->set_xoffset(0);
tile->set_yoffset(0);
}
}
}
break;
}
}
}
else if(xmlNode.attribute("visited").as_bool() == false && !attr.empty()) {
switch (field->cpp_type()) {
case CppType::CPPTYPE_MESSAGE: {
google::protobuf::Message *msg = refl->MutableMessage(m, field);
// TODO: handle message case
// PackRes(resMap, dir, ids, child, msg, depth + 1);
break;
}
case CppType::CPPTYPE_INT32: {
refl->SetInt32(m, field, attr.as_int());
break;
}
case CppType::CPPTYPE_INT64: {
refl->SetInt64(m, field, attr.as_int());
break;
}
case CppType::CPPTYPE_UINT32: {
refl->SetUInt32(m, field, attr.as_uint());
break;
}
case CppType::CPPTYPE_UINT64: {
refl->SetUInt64(m, field, attr.as_uint());
break;
}
case CppType::CPPTYPE_DOUBLE: {
refl->SetDouble(m, field, attr.as_double());
break;
}
case CppType::CPPTYPE_FLOAT: {
refl->SetFloat(m, field, attr.as_float());
break;
}
case CppType::CPPTYPE_BOOL: {
refl->SetBool(m, field, (attr.as_int() != 0));
break;
}
case CppType::CPPTYPE_ENUM: {
refl->SetEnum(m, field, field->enum_type()->FindValueByNumber(attr.as_int()));
break;
}
case CppType::CPPTYPE_STRING: {
const bool isFilePath = opts.GetExtension(buffers::file_path);
std::string value;
if(isFilePath) {
std::string parentDirPath = tmxPath.parent_path().string()+"/";
value = parentDirPath + attr.as_string();
}
else {
value = attr.as_string();
}
refl->SetString(m, field, value);
break;
}
}
}
}
}
int ConvertGlobalTileIdToLocal(const unsigned int& globalTileId) {
const unsigned int FLIPPED_HORIZONTALLY_FLAG = 0X80000000; // tile is horizontally flipped or not
const unsigned int FLIPPED_VERTICALLY_FLAG = 0X40000000; // tile is vertically flipped or not
const unsigned int FLIPPED_DIAGONALLY_FLAG = 0X20000000; // indicates diogonal flip(bot left <-> top right) of tile in ortho/iso maps or
// 60 deg clock rot in case of hexagonal maps
const unsigned int ROTATED_HEXAGONAL_120_FLAG = 0X10000000; // ignored in ortho or iso maps, indicates 120 deg clock rot in hexagonal maps
unsigned int tileId = globalTileId;
bool flippedHorizontally = tileId & FLIPPED_HORIZONTALLY_FLAG;
bool flippedVertically = tileId & FLIPPED_VERTICALLY_FLAG;
bool flippedDiagonally = tileId & FLIPPED_DIAGONALLY_FLAG;
bool rotatedHex120 = tileId & ROTATED_HEXAGONAL_120_FLAG;
tileId &= ~(FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG | FLIPPED_DIAGONALLY_FLAG | ROTATED_HEXAGONAL_120_FLAG);
return tileId;
}
};
} // anonymous namespace
std::unique_ptr<buffers::Project> TMXFileFormat::LoadProject(const fs::path& fPath) const {
pugi::xml_document doc;
if(!doc.load_file(fPath.c_str())) return nullptr;
std::unique_ptr<buffers::Project> proj = std::make_unique<buffers::Project>();
buffers::Game* game = proj->mutable_game();
TMXMapLoader map_walker(game->mutable_root(), fPath);
// traverse the doc twice, first to fetch all individual tiled elements,
// second to use the loaded elements and filling up room messages making a use of them
doc.traverse(map_walker);
doc.traverse(map_walker);
std::string str;
google::protobuf::TextFormat::PrintToString(proj->game(), &str);
std::ofstream out("/home/kash/github/radialgm-stuff/textProtos/test1tmx.txt");
out<<str<<std::endl;
out.close();
std::cout<<"END LOADING TMX"<<std::endl;
return proj;
}
} // namespace egm