UnknownComponent.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. xmlAttributes_.Clear();
  79. xmlAttributeInfos_.Clear();
  80. // Assume we are reading from a component data buffer, and the type has already been read
  81. unsigned dataSize = source.GetSize() - source.GetPosition();
  82. binaryAttributes_.Resize(dataSize);
  83. return dataSize ? source.Read(&binaryAttributes_[0], dataSize) == dataSize : true;
  84. }
  85. bool UnknownComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
  86. {
  87. useXML_ = true;
  88. xmlAttributes_.Clear();
  89. xmlAttributeInfos_.Clear();
  90. binaryAttributes_.Clear();
  91. XMLElement attrElem = source.GetChild("attribute");
  92. while (attrElem)
  93. {
  94. AttributeInfo attr;
  95. attr.mode_ = AM_FILE;
  96. attr.name_ = attrElem.GetAttribute("name");
  97. attr.type_ = VAR_STRING;
  98. if (!attr.name_.Empty())
  99. {
  100. String attrValue = attrElem.GetAttribute("value");
  101. attr.defaultValue_ = String::EMPTY;
  102. xmlAttributeInfos_.Push(attr);
  103. xmlAttributes_.Push(attrValue);
  104. }
  105. attrElem = attrElem.GetNext("attribute");
  106. }
  107. // Fix up pointers to the attributes after all have been read
  108. for (unsigned i = 0; i < xmlAttributeInfos_.Size(); ++i)
  109. xmlAttributeInfos_[i].ptr_ = &xmlAttributes_[i];
  110. return true;
  111. }
  112. bool UnknownComponent::Save(Serializer& dest) const
  113. {
  114. if (useXML_)
  115. LOGWARNING("UnknownComponent loaded in XML mode, attributes will be empty for binary save");
  116. // Write type and ID
  117. if (!dest.WriteShortStringHash(GetType()))
  118. return false;
  119. if (!dest.WriteUInt(id_))
  120. return false;
  121. if (!binaryAttributes_.Size())
  122. return true;
  123. else
  124. return dest.Write(&binaryAttributes_[0], binaryAttributes_.Size()) == binaryAttributes_.Size();
  125. }
  126. bool UnknownComponent::SaveXML(XMLElement& dest) const
  127. {
  128. if (dest.IsNull())
  129. {
  130. LOGERROR("Could not save " + GetTypeName() + ", null destination element");
  131. return false;
  132. }
  133. if (!useXML_)
  134. LOGWARNING("UnknownComponent loaded in binary mode, attributes will be empty for XML save");
  135. // Write type and ID
  136. if (!dest.SetString("type", GetTypeName()))
  137. return false;
  138. if (!dest.SetInt("id", id_))
  139. return false;
  140. for (unsigned i = 0; i < xmlAttributeInfos_.Size(); ++i)
  141. {
  142. XMLElement attrElem = dest.CreateChild("attribute");
  143. attrElem.SetAttribute("name", xmlAttributeInfos_[i].name_);
  144. attrElem.SetAttribute("value", xmlAttributes_[i]);
  145. }
  146. return true;
  147. }
  148. void UnknownComponent::SetTypeName(const String& typeName)
  149. {
  150. typeName_ = typeName;
  151. typeHash_ = typeName;
  152. }
  153. void UnknownComponent::SetType(ShortStringHash typeHash)
  154. {
  155. typeName_ = GenerateNameFromType(typeHash);
  156. typeHash_ = typeHash;
  157. }
  158. }