FF7NameLookup.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. -----------------------------------------------------------------------------
  3. Copyright (c) 15.10.2013 Tobias Peters <tobias.peters@kreativeffekt.at>
  4. This file is part of Q-Gears
  5. Q-Gears is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, version 2.0 (GPLv2) of the License.
  8. Q-Gears is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. -----------------------------------------------------------------------------
  13. */
  14. #ifndef __FF7NameLookup_H__
  15. #define __FF7NameLookup_H__
  16. #include "common/TypeDefine.h"
  17. #include "common/QGearsStringUtil.h"
  18. #include "core/XmlFile.h"
  19. namespace QGears
  20. {
  21. namespace FF7
  22. {
  23. class FF7FiledModelsAndAnimationMetadata : public XmlFile
  24. {
  25. public:
  26. FF7FiledModelsAndAnimationMetadata(Ogre::String file)
  27. : XmlFile(file)
  28. {
  29. TiXmlNode* node = m_File.RootElement();
  30. if (node == nullptr || node->ValueStr() != "metadata")
  31. {
  32. throw std::runtime_error("FF7FiledModelsAndAnimationMetadata: " + m_File.ValueStr() + " is not a valid metadata file! No <metadata> in root.");
  33. }
  34. node = node->FirstChild();
  35. while (node)
  36. {
  37. if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "models")
  38. {
  39. ReadModels(node->FirstChild());
  40. }
  41. else if (node->Type() == TiXmlNode::TINYXML_ELEMENT && node->ValueStr() == "animations")
  42. {
  43. ReadAnimations(node->FirstChild());
  44. }
  45. node = node->NextSibling();
  46. }
  47. }
  48. const String& Animation(const String &key) const
  49. {
  50. auto it = mAnimations.find(key);
  51. if (it != std::end(mAnimations))
  52. {
  53. return it->second;
  54. }
  55. String base_name;
  56. StringUtil::splitBase(key, base_name);
  57. it = mAnimations.find(base_name);
  58. if (it != std::end(mAnimations))
  59. {
  60. return it->second;
  61. }
  62. return key;
  63. }
  64. const String& Model(const String &key) const
  65. {
  66. auto it = mModels.find(key);
  67. if (it != std::end(mModels))
  68. {
  69. return it->second;
  70. }
  71. String base_name;
  72. StringUtil::splitBase(key, base_name);
  73. it = mModels.find(base_name);
  74. if (it != std::end(mModels))
  75. {
  76. return it->second;
  77. }
  78. return key;
  79. }
  80. private:
  81. void ReadModels(TiXmlNode* node)
  82. {
  83. while (node)
  84. {
  85. const auto src = GetString(node, "name");
  86. const auto dst = GetString(node, "target");
  87. mModels[src] = dst;
  88. node = node->NextSibling();
  89. }
  90. }
  91. void ReadAnimations(TiXmlNode* node)
  92. {
  93. while (node)
  94. {
  95. const auto src = GetString(node, "name");
  96. const auto dst = GetString(node, "target");
  97. mAnimations[src] = dst;
  98. node = node->NextSibling();
  99. }
  100. }
  101. typedef std::map<String, String> LookupMap;
  102. LookupMap mModels;
  103. LookupMap mAnimations;
  104. };
  105. class NameLookup
  106. {
  107. public:
  108. NameLookup() = delete;
  109. static const String& animation(const String &key)
  110. {
  111. return Data().Animation(key);
  112. }
  113. static const String& model(const String &key)
  114. {
  115. return Data().Model(key);
  116. }
  117. static FF7FiledModelsAndAnimationMetadata& Data()
  118. {
  119. static FF7FiledModelsAndAnimationMetadata data("field_models_and_animation_metadata.xml");
  120. return data;
  121. }
  122. };
  123. }
  124. }
  125. #endif // __FF7NameLookup_H__