Component.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Node.h"
  25. #include "ReplicationUtils.h"
  26. #include "ResourceCache.h"
  27. #include "Scene.h"
  28. #include "DebugNew.h"
  29. Component::Component(const std::string& name) :
  30. mEntity(0),
  31. mName(name),
  32. mNameHash(name),
  33. mNetFlags(NET_SYNCTOALL)
  34. {
  35. }
  36. void Component::save(Serializer& dest)
  37. {
  38. // Write identification and netflags
  39. dest.writeShortStringHash(getType());
  40. dest.writeString(mName);
  41. dest.writeUByte(mNetFlags);
  42. }
  43. void Component::load(Deserializer& source, ResourceCache* cache)
  44. {
  45. // Entity reads the identification, so do not read here
  46. mNetFlags = source.readUByte();
  47. }
  48. void Component::saveXML(XMLElement& dest)
  49. {
  50. // Write identification and netflags
  51. dest.setString("type", getTypeName());
  52. if (!mName.empty())
  53. dest.setString("name", mName);
  54. dest.setInt("netflags", mNetFlags);
  55. }
  56. void Component::loadXML(const XMLElement& source, ResourceCache* cache)
  57. {
  58. // Entity reads the identification, so do not read here
  59. mNetFlags = source.getInt("netflags");
  60. }
  61. void Component::postLoad(ResourceCache* cache)
  62. {
  63. }
  64. bool Component::writeNetUpdate(Serializer& dest, Serializer& destRevision, Deserializer& baseRevision, const NetUpdateInfo& info)
  65. {
  66. return false; // No data to write
  67. }
  68. void Component::readNetUpdate(Deserializer& source, ResourceCache* cache, const NetUpdateInfo& info)
  69. {
  70. }
  71. void Component::postNetUpdate(ResourceCache* cache)
  72. {
  73. }
  74. void Component::interpolate(bool snapToEnd)
  75. {
  76. }
  77. void Component::getComponentRefs(std::vector<ComponentRef>& dest)
  78. {
  79. }
  80. void Component::getResourceRefs(std::vector<Resource*>& dest)
  81. {
  82. }
  83. void Component::setName(const std::string& name)
  84. {
  85. mName = name;
  86. mNameHash = StringHash(name);
  87. }
  88. void Component::setNetFlags(unsigned char flags)
  89. {
  90. // Respect the authority and proxy flags if they have been set already
  91. if (mNetFlags & NET_MODEFLAGS)
  92. mNetFlags = (mNetFlags & NET_MODEFLAGS) | (flags & ~NET_MODEFLAGS);
  93. else
  94. mNetFlags = flags;
  95. }
  96. unsigned Component::getGroupFlags() const
  97. {
  98. if (!mEntity)
  99. return 0;
  100. return mEntity->getGroupFlags();
  101. }
  102. bool Component::isPlayback() const
  103. {
  104. if (!mEntity)
  105. return false;
  106. return mEntity->isPlayback();
  107. }
  108. bool Component::checkSync(Connection* connection) const
  109. {
  110. if ((!mEntity) || (mNetFlags & NET_SYNCTONONE) || (mEntity->getNetFlags() & NET_SYNCTONONE))
  111. return false;
  112. if ((mNetFlags & NET_SYNCTOOWNER) || (mEntity->getNetFlags() & NET_SYNCTOOWNER))
  113. return mEntity->getOwner() == connection;
  114. return true;
  115. }
  116. bool Component::checkPrediction(Connection* connection) const
  117. {
  118. if ((!mEntity) || (!(mNetFlags & NET_PREDICTIONFLAGS)))
  119. return false;
  120. return mEntity->checkPrediction(connection);
  121. }
  122. ComponentRef::ComponentRef(Component* component, bool forXML) :
  123. mEntityID(0),
  124. mDirty(false)
  125. {
  126. if (!component)
  127. return;
  128. if (component->getEntity())
  129. {
  130. mEntityID = component->getEntity()->getID();
  131. mHash = component->getCombinedHash();
  132. }
  133. else
  134. {
  135. // The component does not directly belong to an entity. However, if it is a scene node,
  136. // check if any of the components in the parent chain belongs to an entity, and use its ID in that case
  137. Node* node = dynamic_cast<Node*>(component);
  138. while (node)
  139. {
  140. node = node->getParent();
  141. if ((node) && (node->getEntity()))
  142. {
  143. mEntityID = node->getEntity()->getID();
  144. mHash = component->getCombinedHash();
  145. break;
  146. }
  147. }
  148. }
  149. if (forXML)
  150. {
  151. mTypeName = component->getTypeName();
  152. mName = component->getName();
  153. }
  154. }
  155. void ComponentRef::write(Serializer& dest) const
  156. {
  157. dest.writeUInt(mEntityID);
  158. if (mEntityID)
  159. dest.writeShortStringHash(mHash);
  160. }
  161. void ComponentRef::read(Deserializer& source)
  162. {
  163. mEntityID = source.readUInt();
  164. if (mEntityID)
  165. mHash = source.readShortStringHash();
  166. else
  167. mHash = ShortStringHash();
  168. mDirty = true;
  169. }
  170. void ComponentRef::writePacked(Serializer& dest) const
  171. {
  172. dest.writeUShort(mEntityID);
  173. if (mEntityID)
  174. dest.writeShortStringHash(mHash);
  175. }
  176. void ComponentRef::readPacked(Deserializer& source)
  177. {
  178. mEntityID = source.readUShort();
  179. if (mEntityID)
  180. mHash = source.readShortStringHash();
  181. else
  182. mHash = ShortStringHash();
  183. mDirty = true;
  184. }
  185. void ComponentRef::writeXML(XMLElement& dest) const
  186. {
  187. if ((mHash.mData) && (mTypeName.empty()))
  188. SAFE_EXCEPTION("Component reference was not initialized with typename and name information");
  189. dest.setInt("id", mEntityID);
  190. dest.setString("type", mTypeName);
  191. if (!mName.empty())
  192. dest.setString("name", mName);
  193. }
  194. void ComponentRef::readXML(const XMLElement& source)
  195. {
  196. if (source)
  197. {
  198. mEntityID = source.getInt("id");
  199. mHash = ShortStringHash(source.getString("type")) + ShortStringHash(source.getString("name"));
  200. }
  201. else
  202. {
  203. mEntityID = 0;
  204. mHash = ShortStringHash();
  205. }
  206. mDirty = true;
  207. }