1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementações em ContentReader e Manager

This commit is contained in:
Danilo 2024-05-03 11:49:01 -03:00
parent 956c9e6ebf
commit acef4d9787
9 changed files with 275 additions and 112 deletions

View File

@ -49,10 +49,13 @@ namespace xna {
return obj2; return obj2;
} }
public: protected:
template <typename T> template <typename T>
sptr<T> ReadAsset(String const& assetName) { sptr<T> ReadAsset(String const& assetName) {
auto input = OpenStream(assetName); auto input = OpenStream(assetName);
auto contentReader = ContentReader::Create(this, input, assetName);
return contentReader->ReadAsset<T>();
} }
sptr<Stream> OpenStream(String const& assetName) { sptr<Stream> OpenStream(String const& assetName) {

View File

@ -4,12 +4,91 @@
#include "typereadermanager.hpp" #include "typereadermanager.hpp"
namespace xna { namespace xna {
sptr<ContentReader> ContentReader::Create(ContentManager* contentManager, Stream* input, String const& assetName) sptr<ContentReader> ContentReader::Create(ContentManager* contentManager, sptr<Stream>& input, String const& assetName)
{ {
return sptr<ContentReader>(); Int graphicsProfile = 0;
input = ContentReader::PrepareStream(input, assetName, graphicsProfile);
return std::shared_ptr<ContentReader>(new ContentReader(contentManager, input, assetName, graphicsProfile));
} }
sptr<Stream> ContentReader::PrepareStream(sptr<Stream>& input, String const* assetName, Int& graphicsProfile) Vector2 ContentReader::ReadVector2()
{
Vector2 vector2;
vector2.X = ReadSingle();
vector2.Y = ReadSingle();
return vector2;
}
Vector3 ContentReader::ReadVector3()
{
Vector3 vector3;
vector3.X = ReadSingle();
vector3.Y = ReadSingle();
vector3.Z = ReadSingle();
return vector3;
}
Vector4 ContentReader::ReadVector4()
{
Vector4 vector4;
vector4.X = ReadSingle();
vector4.Y = ReadSingle();
vector4.Z = ReadSingle();
vector4.W = ReadSingle();
return vector4;
}
Matrix ContentReader::ReadMatrix()
{
Matrix matrix;
matrix.M11 = ReadSingle();
matrix.M12 = ReadSingle();
matrix.M13 = ReadSingle();
matrix.M14 = ReadSingle();
matrix.M21 = ReadSingle();
matrix.M22 = ReadSingle();
matrix.M23 = ReadSingle();
matrix.M24 = ReadSingle();
matrix.M31 = ReadSingle();
matrix.M32 = ReadSingle();
matrix.M33 = ReadSingle();
matrix.M34 = ReadSingle();
matrix.M41 = ReadSingle();
matrix.M42 = ReadSingle();
matrix.M43 = ReadSingle();
matrix.M44 = ReadSingle();
return matrix;
}
Quaternion ContentReader::ReadQuaternion()
{
Quaternion quaternion;
quaternion.X = ReadSingle();
quaternion.Y = ReadSingle();
quaternion.Z = ReadSingle();
quaternion.W = ReadSingle();
return quaternion;
}
Color ContentReader::ReadColor()
{
const auto packedValue = ReadUInt32();
return Color(packedValue);
}
float ContentReader::ReadSingle()
{
const auto int32 = ReadUInt32();
return *(float*)&int32;
}
double ContentReader::ReadDouble()
{
const auto int64 = ReadUInt64();
return *(double*)&int64;
}
sptr<Stream> ContentReader::PrepareStream(sptr<Stream>& input, String const& assetName, Int& graphicsProfile)
{ {
BinaryReader binaryReader = BinaryReader(input); BinaryReader binaryReader = BinaryReader(input);
@ -54,6 +133,16 @@ namespace xna {
} }
Int ContentReader::ReadHeader() { Int ContentReader::ReadHeader() {
//typeReaders = ContentTypeReaderManager::ReadTypeManifest(this->Read7BitEncodedInt(), this); auto _this = shared_from_this();
typeReaders = ContentTypeReaderManager::ReadTypeManifest(this->Read7BitEncodedInt(), _this);
auto length = this->Read7BitEncodedInt();
if (length > 0)
{
//TODO: length > 0
}
return length;
} }
} }

View File

@ -3,29 +3,111 @@
#include "../default.hpp" #include "../default.hpp"
#include "../csharp/binary.hpp" #include "../csharp/binary.hpp"
#include "../csharp/type.hpp"
#include "typereadermanager.hpp"
#include <any>
#include "../common/vectors.hpp"
#include "../common/matrix.hpp"
#include "../common/quaternion.hpp"
#include "../common/color.hpp"
namespace xna { namespace xna {
class ContentReader : public BinaryReader{ class ContentReader : public BinaryReader, public std::enable_shared_from_this<ContentReader>{
public: public:
static sptr<ContentReader> Create(ContentManager* contentManager, Stream* input, String const& assetName); static sptr<ContentReader> Create(ContentManager* contentManager, sptr<Stream>& input, String const& assetName);
template <typename T> template <typename T>
sptr<T> ReadAsset() { sptr<T> ReadAsset();
return nullptr;
} template <typename T>
T ReadObject();
template <typename T>
T ReadObject(T existingInstance);
Vector2 ReadVector2();
Vector3 ReadVector3();
Vector4 ReadVector4();
Matrix ReadMatrix();
Quaternion ReadQuaternion();
Color ReadColor();
float ReadSingle();
double ReadDouble();
private: private:
ContentReader(ContentManager* contentManager, sptr<Stream>const& input, String const& assetName) ContentReader(ContentManager* contentManager, sptr<Stream>& input, String const& assetName, Int graphicsProfile)
: BinaryReader(input), _contentManager(contentManager), _assetName(assetName){} : BinaryReader(input), _contentManager(contentManager), _assetName(assetName){}
static sptr<Stream> PrepareStream(sptr<Stream>& input, String const* assetName, Int& graphicsProfile); static sptr<Stream> PrepareStream(sptr<Stream>& input, String const& assetName, Int& graphicsProfile);
Int ReadHeader(); Int ReadHeader();
template <typename T>
T ReadObjectInternal(std::any& existingInstance, xna_error_nullarg);
template <typename T>
T InvokeReader(ContentTypeReader& reader, std::any& existingInstance, xna_error_nullarg);
private: private:
ContentManager* _contentManager = nullptr; ContentManager* _contentManager = nullptr;
String _assetName; String _assetName;
std::vector<sptr<ContentTypeReader>> typeReaders;
Int graphicsProfile{ 0 };
}; };
template<typename T>
inline T ContentReader::ReadObjectInternal(std::any& existingInstance, xna_error_ptr_arg)
{
const auto num = Read7BitEncodedInt();
if (num == 0) {
return T();
}
const auto index = num - 1;
if (index >= typeReaders.size()) {
xna_error_apply(err, XnaErrorCode::ARGUMENT_OUT_OF_RANGE);
return T();
}
return InvokeReader(typeReaders[index], existingInstance);
}
template<typename T>
inline T ContentReader::InvokeReader(ContentTypeReader& reader, std::any& existingInstance, xna_error_ptr_arg)
{
auto contentTypeReader = reinterpret_cast<ContentTypeReaderT<T>*>(&reader);
T objB = T();
if (contentTypeReader) {
T existingInstance1 = existingInstance.has_value() ? std::any_cast<T>(existingInstance) : T();
objB = contentTypeReader->Read(*this, existingInstance1);
}
return T();
}
template<typename T>
inline sptr<T> ContentReader::ReadAsset()
{
const auto sharedResourceCount = ReadHeader();
T obj = ReadObject<T>();
//this.ReadSharedResources(sharedResourceCount);
return obj;
}
template<typename T>
inline T ContentReader::ReadObject()
{
return ReadObjectInternal<T>(nullptr);
}
template<typename T>
inline T ContentReader::ReadObject(T existingInstance)
{
return ReadObjectInternal<T>(std::any(existingInstance));
}
} }
#endif #endif

View File

@ -82,7 +82,7 @@ namespace xna {
std::map<sptr<Type>, sptr<ContentTypeReader>>::iterator it; std::map<sptr<Type>, sptr<ContentTypeReader>>::iterator it;
for (it = readerTypeToReader.begin(); it != readerTypeToReader.end(); it++) { for (it = readerTypeToReader.begin(); it != readerTypeToReader.end(); it++) {
if (it->first->FullName == readerTypeName) if (it->first->FullName() == readerTypeName)
type = it->first; type = it->first;
} }
@ -111,7 +111,8 @@ namespace xna {
} }
ContentTypeReaderManager::targetTypeToReader.insert({ targetType, reader }); ContentTypeReaderManager::targetTypeToReader.insert({ targetType, reader });
ContentTypeReaderManager::readerTypeToReader.insert({ reader->GetType(), reader }); //ContentTypeReaderManager::readerTypeToReader.insert({ reader->GetType(), reader });
ContentTypeReaderManager::readerTypeToReader.insert({ typeof(*reader), reader});
ContentTypeReaderManager::nameToReader.insert({ readerTypeName, reader }); ContentTypeReaderManager::nameToReader.insert({ readerTypeName, reader });
} }
@ -139,10 +140,5 @@ namespace xna {
targetTypeToReader.insert({ typeof<Object>(), contentTypeReader}); targetTypeToReader.insert({ typeof<Object>(), contentTypeReader});
readerTypeToReader.insert({ typeof<ObjectReader>(), contentTypeReader}); readerTypeToReader.insert({ typeof<ObjectReader>(), contentTypeReader});
} }
} }
sptr<void> ObjectReader::Read(ContentReader input, sptr<void> existingInstance)
{
return nullptr;
}
} }

View File

@ -5,42 +5,43 @@
#include "../default.hpp" #include "../default.hpp"
#include <algorithm> #include <algorithm>
#include <map> #include <map>
#include <any>
namespace xna { namespace xna {
//-------------------------------------------------------// //-------------------------------------------------------//
// ContentTypeReader // // ContentTypeReader //
//-------------------------------------------------------// //-------------------------------------------------------//
class ContentTypeReader : public Object { class ContentTypeReader {
public:
ContentTypeReader(){}
public: public:
virtual Int TypeVersion() { return 0; } virtual Int TypeVersion() { return 0; }
virtual bool CanDeserializeIntoExistingObject() { return false; } virtual bool CanDeserializeIntoExistingObject() { return false; }
virtual void Initialize(sptr<ContentTypeReaderManager>& manager) {} virtual void Initialize(sptr<ContentTypeReaderManager>& manager) {}
sptr<Type> TargetType() { return _targetType; } sptr<Type> TargetType() { return _targetType; }
virtual std::any Read(ContentReader& input, std::any& existingInstance) = 0;
virtual sptr<Type> GetType() const override {
auto type = New<Type>();
type->FullName = "xna::ContentTypeReader";
type->Namespace = "xna";
type->IsClass = true;
return type;
}
protected: protected:
ContentTypeReader(sptr<Type> const& targetType) : _targetType(targetType) ContentTypeReader(sptr<Type> const& targetType) : _targetType(targetType)
{ {}
}
virtual sptr<void> Read(ContentReader input, sptr<void> existingInstance) = 0;
public: public:
bool TargetIsValueType{ false }; bool TargetIsValueType{ false };
private: private:
sptr<Type> _targetType = nullptr; sptr<Type> _targetType = nullptr;
};
template <class T>
class ContentTypeReaderT : public ContentTypeReader {
protected:
ContentTypeReaderT(sptr<Type> const& targetType) : ContentTypeReader(targetType){}
public:
virtual std::any Read(ContentReader& input, std::any& existingInstance) override{
return std::any();
}
virtual T Read(ContentReader& input, T existingInstance) = 0;
}; };
//-------------------------------------------------------// //-------------------------------------------------------//
@ -165,24 +166,17 @@ namespace xna {
//-------------------------------------------------------// //-------------------------------------------------------//
// ObjectReader // // ObjectReader //
//-------------------------------------------------------// //-------------------------------------------------------//
class ObjectReader : public ContentTypeReader { class ObjectReader : public ContentTypeReaderT<Object> {
public: public:
ObjectReader() : ContentTypeReader(typeof(this)){ ObjectReader() : ContentTypeReaderT(typeof<Object>()) {
ContentTypeReaderActivador::SetActivador(typeof(this), []() -> sptr<ContentTypeReader> { ContentTypeReaderActivador::SetActivador(typeof(this), []() -> sptr<ContentTypeReader> {
auto obj = New <ObjectReader>(); auto obj = New <ObjectReader>();
return reinterpret_pointer_cast<ContentTypeReader>(obj); return reinterpret_pointer_cast<ContentTypeReader>(obj);
}); });
} }
// Inherited via ContentTypeReader virtual Object Read(ContentReader& input, Object existingInstance) override {
sptr<void> Read(ContentReader input, sptr<void> existingInstance) override; return Object();
sptr<Type> GetType() const override{
auto type = New<Type>();
type->FullName = "xna::ObjectReader";
type->Namespace = "xna";
type->IsClass = true;
return type;
} }
}; };
} }

View File

@ -2,15 +2,6 @@
#include "type.hpp" #include "type.hpp"
namespace xna { namespace xna {
sptr<Type> Object::GetType() const
{
auto type = New<Type>();
type->FullName = "xna::Object";
type->Namespace = "xna";
type->IsClass = true;
return type;
}
size_t Object::GetHashCode() const size_t Object::GetHashCode() const
{ {
size_t seed = 0; size_t seed = 0;

View File

@ -6,7 +6,6 @@
namespace xna { namespace xna {
class Object { class Object {
public: public:
virtual sptr<Type> GetType() const;
virtual size_t GetHashCode() const; virtual size_t GetHashCode() const;
}; };
} }

View File

@ -1,27 +1,14 @@
#include "type.hpp" #include "type.hpp"
namespace xna { namespace xna {
sptr<Type> Type::GetType() const
{
auto type = New<Type>();
type->FullName = "xna::Type";
type->Namespace = "xna";
type->IsClass = true;
return type;
}
size_t Type::GetHashCode() const size_t Type::GetHashCode() const
{ {
size_t seed = 0; size_t seed = 0;
XnaHHashCombine(seed, Namespace); XnaHHashCombine(seed, fullName);
XnaHHashCombine(seed, FullName); XnaHHashCombine(seed, isClass);
XnaHHashCombine(seed, IsInterface); XnaHHashCombine(seed, isEnum);
XnaHHashCombine(seed, IsArray); XnaHHashCombine(seed, isValueType);
XnaHHashCombine(seed, IsPointer); XnaHHashCombine(seed, isPrimitive);
XnaHHashCombine(seed, IsClass);
XnaHHashCombine(seed, IsCOMObject);
XnaHHashCombine(seed, IsEnum);
XnaHHashCombine(seed, IsValueType);
return seed; return seed;
} }

View File

@ -3,59 +3,81 @@
#include "../default.hpp" #include "../default.hpp"
#include "object.hpp" #include "object.hpp"
#include <type_traits>
#include <typeinfo>
namespace xna { namespace xna {
class Type : public Object { class Type : public Object {
public: public:
String Namespace; constexpr String FullName() const { return fullName; }
String FullName; constexpr bool IsClass() const { return isClass; }
bool IsInterface{ false }; constexpr bool IsEnum() const { return isEnum; }
bool IsArray{ false }; constexpr bool IsValueType() const { return isValueType; }
bool IsPointer{ false }; constexpr bool IsPrimitive() const { return isPrimitive; }
bool IsClass{ false };
bool IsCOMObject{ false };
bool IsEnum{ false };
bool IsValueType{ false };
constexpr bool operator==(const Type& other) const
{
return Namespace == other.Namespace
&& FullName == other.FullName
&& IsInterface == other.IsInterface
&& IsArray == other.IsArray
&& IsPointer == other.IsPointer
&& IsClass == other.IsClass
&& IsCOMObject == other.IsCOMObject
&& IsEnum == other.IsEnum
&& IsValueType == other.IsValueType;
}
virtual sptr<Type> GetType() const override;
virtual size_t GetHashCode() const; virtual size_t GetHashCode() const;
constexpr bool operator==(const Type& other) const {
return
fullName == other.fullName
&& isClass == other.isClass
&& isEnum == other.isEnum
&& isValueType == other.isValueType
&& isPrimitive == other.isPrimitive;
}
bool operator()(Type const& t1, Type const& t2) const { bool operator()(Type const& t1, Type const& t2) const {
return t1.GetHashCode() < t2.GetHashCode(); return t1.GetHashCode() < t2.GetHashCode();
} }
};
template <class T>
friend sptr<Type> typeof();
private:
String fullName;
bool isClass{ false };
bool isEnum{ false };
bool isValueType{ false };
bool isPrimitive{ false };
};
template <class T> template <class T>
inline sptr<Type> typeof() { inline sptr<Type> typeof() {
auto t = New<T>(); if (std::is_arithmetic<T>::value) {
auto obj = reinterpret_pointer_cast<Object>(t); auto primitiveType = New<Type>();
primitiveType->fullName = typeid(T).name();
primitiveType->isPrimitive = true;
primitiveType->isValueType = true;
return primitiveType;
}
if (!obj) return nullptr; if (std::is_enum<T>::value) {
auto enumType = New<Type>();
return obj->GetType(); enumType->fullName = typeid(T).name();
} enumType->isValueType = true;
enumType->isEnum = true;
return enumType;
}
if (std::is_class<T>::value) {
auto classType = New<Type>();
classType->fullName = typeid(T).name();
classType->isClass = true;
return classType;
}
return nullptr;
}
template <class T> template <class T>
inline sptr<Type> typeof(T const* object) { inline sptr<Type> typeof(T const* object) {
auto obj = reinterpret_cast<const Object*>(object); return typeof<T>();
}
if (!obj) template <class T>
return nullptr; inline sptr<Type> typeof(T const& object) {
return typeof<T>();
return obj->GetType();
} }
} }