Skip to content

Instantly share code, notes, and snippets.

@JSandusky
Last active November 17, 2018 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JSandusky/aac954ee321cc47d4b7e98f7e64854e2 to your computer and use it in GitHub Desktop.
Save JSandusky/aac954ee321cc47d4b7e98f7e64854e2 to your computer and use it in GitHub Desktop.
Urho3D "Bytes" resource for arbitrary binary files in attributes/etc
#include <Urho3D/Resource/Bytes.h>
#include <Urho3D/Core/Context.h>
namespace Urho3D
{
/// Construct.
Bytes::Bytes(Context* context) : Resource(context)
{
}
Bytes::~Bytes()
{
}
void Bytes::RegisterObject(Context* context)
{
context->RegisterFactory<Bytes>();
}
bool Bytes::BeginLoad(Deserializer& source)
{
data_.Resize(source.GetSize());
source.Read(data_.GetBuffer().Buffer(), source.GetSize());
return true;
}
bool Bytes::Save(Serializer& dest) const
{
dest.Write(data_.GetBuffer().Buffer(), data_.GetSize());
return true;
}
}
#include <Urho3D/Resource/Resource.h>
#include <Urho3D/IO/VectorBuffer.h>
namespace Urho3D
{
class URHO3D_API Bytes : public Resource
{
URHO3D_OBJECT(Bytes, Resource);
public:
/// Construct.
explicit Bytes(Context* context);
/// Destruct.
~Bytes() override;
/// Register object factory.
static void RegisterObject(Context* context);
/// Load resource from stream. May be called from a worker thread. Return true if successful.
bool BeginLoad(Deserializer& source) override;
/// Saves the stored buffer data.
virtual bool Save(Serializer& dest) const;
VectorBuffer& GetData() { data_.Seek(0); return data_; }
const VectorBuffer& GetData() const { data_.Seek(0); return data_; }
private:
mutable VectorBuffer data_;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment