diff --git a/Source/Urho3D/Scene/ObjectAnimation.cpp b/Source/Urho3D/Scene/ObjectAnimation.cpp index a81d1bcbb..771793d97 100644 --- a/Source/Urho3D/Scene/ObjectAnimation.cpp +++ b/Source/Urho3D/Scene/ObjectAnimation.cpp @@ -23,6 +23,9 @@ #include "../Precompiled.h" #include "../Core/Context.h" +#include "../IO/AbstractFile.h" +#include "../IO/FileSystem.h" +#include "../IO/Log.h" #include "../Resource/XMLFile.h" #include "../Resource/JSONFile.h" #include "../Scene/ObjectAnimation.h" @@ -57,22 +60,57 @@ void ObjectAnimation::RegisterObject(Context* context) bool ObjectAnimation::BeginLoad(Deserializer& source) { - XMLFile xmlFile(context_); - if (!xmlFile.Load(source)) - return false; + String extension = GetExtension(source.GetName()); - return LoadXML(xmlFile.GetRoot()); + if (extension == ".xml") + { + XMLFile xmlFile(context_); + if (xmlFile.Load(source)) + return LoadXML(xmlFile.GetRoot()); + } + else if (extension == ".json") + { + JSONFile jsonFile(context_); + if (jsonFile.Load(source)) + return LoadJSON(jsonFile.GetRoot()); + } + + return false; } bool ObjectAnimation::Save(Serializer& dest) const { - XMLFile xmlFile(context_); + return Save(dest, ".xml"); +} - XMLElement rootElem = xmlFile.CreateRoot("objectanimation"); - if (!SaveXML(rootElem)) - return false; +bool ObjectAnimation::Save(Serializer& dest, const String& extension) const +{ + if (extension == ".xml") + { + XMLFile xmlFile(context_); + XMLElement rootElem = xmlFile.CreateRoot("objectanimation"); + if (!SaveXML(rootElem)) + { + URHO3D_LOGERROR("ObjectAnimation SaveXML Error"); + return false; + } + + return xmlFile.Save(dest); + } + else if (extension == ".json") + { + JSONFile jsonFile(context_); + jsonFile.GetRoot() = "objectanimation"; + if (!SaveJSON(jsonFile.GetRoot())) + { + URHO3D_LOGERROR("ObjectAnimation SaveJSON Error"); + return false; + } + + return jsonFile.Save(dest); + } - return xmlFile.Save(dest); + return false; } bool ObjectAnimation::LoadXML(const XMLElement& source) diff --git a/Source/Urho3D/Scene/ObjectAnimation.h b/Source/Urho3D/Scene/ObjectAnimation.h index 7efa01916..5520a27fe 100644 --- a/Source/Urho3D/Scene/ObjectAnimation.h +++ b/Source/Urho3D/Scene/ObjectAnimation.h @@ -49,8 +49,10 @@ class URHO3D_API ObjectAnimation : public Resource /// Load resource from stream. May be called from a worker thread. Return true if successful. bool BeginLoad(Deserializer& source) override; - /// Save resource. Return true if successful. + /// Save resource with default extension. Return true if successful. bool Save(Serializer& dest) const override; + /// Save resource with given extension. Return true if successful. + bool Save(Serializer& dest, const String& extension) const; /// Load from XML data. Return true if successful. bool LoadXML(const XMLElement& source); /// Save as XML data. Return true if successful. diff --git a/Source/Urho3D/Scene/ValueAnimation.cpp b/Source/Urho3D/Scene/ValueAnimation.cpp index 1c98af45a..ac78b2bc1 100644 --- a/Source/Urho3D/Scene/ValueAnimation.cpp +++ b/Source/Urho3D/Scene/ValueAnimation.cpp @@ -25,6 +25,7 @@ #include "../Core/Context.h" #include "../Core/StringUtils.h" #include "../IO/Deserializer.h" +#include "../IO/FileSystem.h" #include "../IO/Log.h" #include "../IO/Serializer.h" #include "../Resource/XMLFile.h" @@ -68,22 +69,57 @@ void ValueAnimation::RegisterObject(Context* context) bool ValueAnimation::BeginLoad(Deserializer& source) { - XMLFile xmlFile(context_); - if (!xmlFile.Load(source)) - return false; + String extension = GetExtension(source.GetName()); + + if (extension == ".xml") + { + XMLFile xmlFile(context_); + if (xmlFile.Load(source)) + return LoadXML(xmlFile.GetRoot()); + } + else if (extension == ".json") + { + JSONFile jsonFile(context_); + if (jsonFile.Load(source)) + return LoadJSON(jsonFile.GetRoot()); + } - return LoadXML(xmlFile.GetRoot()); + return false; } bool ValueAnimation::Save(Serializer& dest) const { - XMLFile xmlFile(context_); + return Save(dest, ".xml"); +} - XMLElement rootElem = xmlFile.CreateRoot("valueanimation"); - if (!SaveXML(rootElem)) - return false; +bool ValueAnimation::Save(Serializer& dest, const String& extension) const +{ + if (extension == ".xml") + { + XMLFile xmlFile(context_); + XMLElement rootElem = xmlFile.CreateRoot("valueanimation"); + if (!SaveXML(rootElem)) + { + URHO3D_LOGERROR("ValueAnimation SaveXML Error"); + return false; + } + + return xmlFile.Save(dest); + } + else if (extension == ".json") + { + JSONFile jsonFile(context_); + jsonFile.GetRoot() = "valueanimation"; + if (!SaveJSON(jsonFile.GetRoot())) + { + URHO3D_LOGERROR("ValueAnimation SaveJSON Error"); + return false; + } + + return jsonFile.Save(dest); + } - return xmlFile.Save(dest); + return false; } bool ValueAnimation::LoadXML(const XMLElement& source) diff --git a/Source/Urho3D/Scene/ValueAnimation.h b/Source/Urho3D/Scene/ValueAnimation.h index b167813fd..3596067fc 100644 --- a/Source/Urho3D/Scene/ValueAnimation.h +++ b/Source/Urho3D/Scene/ValueAnimation.h @@ -80,8 +80,10 @@ class URHO3D_API ValueAnimation : public Resource /// Load resource from stream. May be called from a worker thread. Return true if successful. bool BeginLoad(Deserializer& source) override; - /// Save resource. Return true if successful. + /// Save resource with default extension. Return true if successful. bool Save(Serializer& dest) const override; + /// Save resource with given extension. Return true if successful. + bool Save(Serializer& dest, const String& extension) const; /// Load from XML data. Return true if successful. bool LoadXML(const XMLElement& source); /// Save as XML data. Return true if successful.