Skip to content

Commit

Permalink
Allows ObjectAnimation and ValueAnimation resources to load correctly…
Browse files Browse the repository at this point in the history
… from Json format
  • Loading branch information
Okkoma committed Oct 10, 2024
1 parent 69ccdd1 commit bfaeb64
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 20 deletions.
56 changes: 47 additions & 9 deletions Source/Urho3D/Scene/ObjectAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion Source/Urho3D/Scene/ObjectAnimation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
54 changes: 45 additions & 9 deletions Source/Urho3D/Scene/ValueAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion Source/Urho3D/Scene/ValueAnimation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit bfaeb64

Please sign in to comment.