Skip to content

Instantly share code, notes, and snippets.

@KartikShrivastava
Created June 21, 2022 09:16
Show Gist options
  • Save KartikShrivastava/9d1cec70fb8ba8b4c2017d4df3f497b1 to your computer and use it in GitHub Desktop.
Save KartikShrivastava/9d1cec70fb8ba8b4c2017d4df3f497b1 to your computer and use it in GitHub Desktop.
#include "tsx.h"
// truncated headers and namespaces
class TSXTilesetLoader : public pugi::xml_tree_walker {
virtual bool for_each(pugi::xml_node& xmlNode) {
// truncated code...
AddResource(protoNode, resType, xmlNode);
return true;
}
void AddResource(buffers::TreeNode *protoNode, std::string resType, pugi::xml_node &xmlNode) {
// truncated code...
if (createFunc != factoryMap.end()) {
auto *res = createFunc->second(protoNode);
PackRes(xmlNode, res, resType);
return;
}
// truncated code...
}
void PackRes(pugi::xml_node &xmlNode, google::protobuf::Message *m, const std::string& resType, std::string fieldPrefix = "") {
const google::protobuf::Reflection *refl = m->GetReflection();
// truncated code...
for (int i = 0; i < desc->field_count(); i++) {
const google::protobuf::FieldDescriptor *field = m->GetDescriptor()->field(i);
// truncated code...
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;
// truncated code...
attr = xmlNode.attribute(tsxPropertyName.c_str());
switch (field->cpp_type()) {
case CppType::CPPTYPE_INT32: {
refl->SetInt32(m, field, attr.as_int());
break;
}
// truncated cases
case CppType::CPPTYPE_STRING: {
const bool isFilePath = opts.GetExtension(buffers::file_path);
std::string value;
if(isFilePath) {
std::string parentDirPath = tsxPath.parent_path().string()+"/";
value = parentDirPath + attr.as_string();
}
else{
value = attr.as_string();
}
refl->SetString(m, field, value);
break;
}
}
}
}
};
// truncated code...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment