UnknownComponent.cpp 5.5 KB

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