UnknownComponent.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. static HashMap<ShortStringHash, String> unknownTypeToName;
  32. static String letters("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
  33. static String GenerateNameFromType(ShortStringHash typeHash)
  34. {
  35. if (unknownTypeToName.Contains(typeHash))
  36. return unknownTypeToName[typeHash];
  37. String test;
  38. // Begin brute-force search
  39. unsigned numLetters = letters.Length();
  40. unsigned combinations = numLetters;
  41. bool found = false;
  42. for (unsigned i = 1; i < 6; ++i)
  43. {
  44. test.Resize(i);
  45. for (unsigned j = 0; j < combinations; ++j)
  46. {
  47. unsigned current = j;
  48. for (unsigned k = 0; k < i; ++k)
  49. {
  50. test[k] = letters[current % numLetters];
  51. current /= numLetters;
  52. }
  53. if (ShortStringHash(test) == typeHash)
  54. {
  55. found = true;
  56. break;
  57. }
  58. }
  59. if (found)
  60. break;
  61. combinations *= numLetters;
  62. }
  63. unknownTypeToName[typeHash] = test;
  64. return test;
  65. }
  66. UnknownComponent::UnknownComponent(Context* context) :
  67. Component(context),
  68. useXML_(false)
  69. {
  70. }
  71. void UnknownComponent::RegisterObject(Context* context)
  72. {
  73. context->RegisterFactory<UnknownComponent>();
  74. }
  75. bool UnknownComponent::Load(Deserializer& source, bool setInstanceDefault)
  76. {
  77. useXML_ = false;
  78. // Assume we are reading from a component data buffer, and the type has already been read
  79. unsigned dataSize = source.GetSize() - source.GetPosition();
  80. binaryAttributes_.Resize(dataSize);
  81. return dataSize ? source.Read(&binaryAttributes_[0], dataSize) == dataSize : true;
  82. }
  83. bool UnknownComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
  84. {
  85. useXML_ = true;
  86. XMLElement attrElem = source.GetChild("attribute");
  87. while (attrElem)
  88. {
  89. Pair<String, String> attr;
  90. attr.first_ = attrElem.GetAttribute("name");
  91. attr.second_ = attrElem.GetAttribute("value");
  92. if (!attr.first_.Empty())
  93. xmlAttributes_.Push(attr);
  94. attrElem = attrElem.GetNext("attribute");
  95. }
  96. return true;
  97. }
  98. bool UnknownComponent::Save(Serializer& dest) const
  99. {
  100. if (useXML_)
  101. LOGWARNING("UnknownComponent loaded in XML mode, attributes will be empty for binary save");
  102. // Write type and ID
  103. if (!dest.WriteShortStringHash(GetType()))
  104. return false;
  105. if (!dest.WriteUInt(id_))
  106. return false;
  107. if (!binaryAttributes_.Size())
  108. return true;
  109. else
  110. return dest.Write(&binaryAttributes_[0], binaryAttributes_.Size()) == binaryAttributes_.Size();
  111. }
  112. bool UnknownComponent::SaveXML(XMLElement& dest) const
  113. {
  114. if (dest.IsNull())
  115. {
  116. LOGERROR("Could not save " + GetTypeName() + ", null destination element");
  117. return false;
  118. }
  119. if (!useXML_)
  120. LOGWARNING("UnknownComponent loaded in binary mode, attributes will be empty for XML save");
  121. // Write type and ID
  122. if (!dest.SetString("type", GetTypeName()))
  123. return false;
  124. if (!dest.SetInt("id", id_))
  125. return false;
  126. for (unsigned i = 0; i < xmlAttributes_.Size(); ++i)
  127. {
  128. const Pair<String, String>& attr = xmlAttributes_[i];
  129. XMLElement attrElem = dest.CreateChild("attribute");
  130. attrElem.SetAttribute("name", attr.first_);
  131. attrElem.SetAttribute("value", attr.second_);
  132. }
  133. return true;
  134. }
  135. void UnknownComponent::SetTypeName(const String& typeName)
  136. {
  137. typeName_ = typeName;
  138. typeHash_ = typeName;
  139. }
  140. void UnknownComponent::SetType(ShortStringHash typeHash)
  141. {
  142. typeName_ = GenerateNameFromType(typeHash);
  143. typeHash_ = typeHash;
  144. }
  145. }