UnknownComponent.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "Deserializer.h"
  25. #include "Log.h"
  26. #include "Serializer.h"
  27. #include "UnknownComponent.h"
  28. #include "XMLElement.h"
  29. namespace Urho3D
  30. {
  31. UnknownComponent::UnknownComponent(Context* context) :
  32. Component(context),
  33. useXML_(false)
  34. {
  35. }
  36. void UnknownComponent::RegisterObject(Context* context)
  37. {
  38. context->RegisterFactory<UnknownComponent>();
  39. }
  40. bool UnknownComponent::Load(Deserializer& source, bool setInstanceDefault)
  41. {
  42. useXML_ = false;
  43. // Assume we are reading from a component data buffer, and the type has already been read
  44. unsigned dataSize = source.GetSize() - source.GetPosition();
  45. binaryAttributes_.Resize(dataSize);
  46. return dataSize ? source.Read(&binaryAttributes_[0], dataSize) == dataSize : true;
  47. }
  48. bool UnknownComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
  49. {
  50. useXML_ = true;
  51. XMLElement attrElem = source.GetChild("attribute");
  52. while (attrElem)
  53. {
  54. Pair<String, String> attr;
  55. attr.first_ = attrElem.GetAttribute("name");
  56. attr.second_ = attrElem.GetAttribute("value");
  57. if (!attr.first_.Empty())
  58. xmlAttributes_.Push(attr);
  59. attrElem = attrElem.GetNext("attribute");
  60. }
  61. return true;
  62. }
  63. bool UnknownComponent::Save(Serializer& dest) const
  64. {
  65. if (useXML_)
  66. LOGWARNING("UnknownComponent loaded in XML mode, attributes will be empty for binary save");
  67. // Write type and ID
  68. if (!dest.WriteShortStringHash(GetType()))
  69. return false;
  70. if (!dest.WriteUInt(id_))
  71. return false;
  72. if (!binaryAttributes_.Size())
  73. return true;
  74. else
  75. return dest.Write(&binaryAttributes_[0], binaryAttributes_.Size()) == binaryAttributes_.Size();
  76. }
  77. bool UnknownComponent::SaveXML(XMLElement& dest) const
  78. {
  79. if (dest.IsNull())
  80. {
  81. LOGERROR("Could not save " + GetTypeName() + ", null destination element");
  82. return false;
  83. }
  84. if (!useXML_)
  85. LOGWARNING("UnknownComponent loaded in binary mode, attributes will be empty for XML save");
  86. // Write type and ID
  87. if (!dest.SetString("type", GetTypeName()))
  88. return false;
  89. if (!dest.SetInt("id", id_))
  90. return false;
  91. for (unsigned i = 0; i < xmlAttributes_.Size(); ++i)
  92. {
  93. const Pair<String, String>& attr = xmlAttributes_[i];
  94. XMLElement attrElem = dest.CreateChild("attribute");
  95. attrElem.SetAttribute("name", attr.first_);
  96. attrElem.SetAttribute("value", attr.second_);
  97. }
  98. return true;
  99. }
  100. void UnknownComponent::SetTypeName(const String& typeName)
  101. {
  102. typeName_ = typeName;
  103. typeHash_ = typeName;
  104. }
  105. void UnknownComponent::SetType(ShortStringHash typeHash)
  106. {
  107. typeName_ = "Unknown " + typeHash.ToString();
  108. typeHash_ = typeHash;
  109. }
  110. }