// // Copyright (c) 2008-2013 the Urho3D project. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // #include "Precompiled.h" #include "Context.h" #include "Deserializer.h" #include "Log.h" #include "Serializer.h" #include "UnknownComponent.h" #include "XMLElement.h" namespace Urho3D { UnknownComponent::UnknownComponent(Context* context) : Component(context), useXML_(false) { } void UnknownComponent::RegisterObject(Context* context) { context->RegisterFactory(); } bool UnknownComponent::Load(Deserializer& source, bool setInstanceDefault) { useXML_ = false; // Assume we are reading from a component data buffer, and the type has already been read unsigned dataSize = source.GetSize() - source.GetPosition(); binaryAttributes_.Resize(dataSize); return dataSize ? source.Read(&binaryAttributes_[0], dataSize) == dataSize : true; } bool UnknownComponent::LoadXML(const XMLElement& source, bool setInstanceDefault) { useXML_ = true; XMLElement attrElem = source.GetChild("attribute"); while (attrElem) { Pair attr; attr.first_ = attrElem.GetAttribute("name"); attr.second_ = attrElem.GetAttribute("value"); if (!attr.first_.Empty()) xmlAttributes_.Push(attr); attrElem = attrElem.GetNext("attribute"); } return true; } bool UnknownComponent::Save(Serializer& dest) const { if (useXML_) LOGWARNING("UnknownComponent loaded in XML mode, attributes will be empty for binary save"); // Write type and ID if (!dest.WriteShortStringHash(GetType())) return false; if (!dest.WriteUInt(id_)) return false; if (!binaryAttributes_.Size()) return true; else return dest.Write(&binaryAttributes_[0], binaryAttributes_.Size()) == binaryAttributes_.Size(); } bool UnknownComponent::SaveXML(XMLElement& dest) const { if (dest.IsNull()) { LOGERROR("Could not save " + GetTypeName() + ", null destination element"); return false; } if (!useXML_) LOGWARNING("UnknownComponent loaded in binary mode, attributes will be empty for XML save"); // Write type and ID if (!dest.SetString("type", GetTypeName())) return false; if (!dest.SetInt("id", id_)) return false; for (unsigned i = 0; i < xmlAttributes_.Size(); ++i) { const Pair& attr = xmlAttributes_[i]; XMLElement attrElem = dest.CreateChild("attribute"); attrElem.SetAttribute("name", attr.first_); attrElem.SetAttribute("value", attr.second_); } return true; } void UnknownComponent::SetTypeName(const String& typeName) { typeName_ = typeName; typeHash_ = typeName; } void UnknownComponent::SetType(ShortStringHash typeHash) { typeName_ = "Unknown " + typeHash.ToString(); typeHash_ = typeHash; } }