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:
parent
956c9e6ebf
commit
acef4d9787
@ -49,10 +49,13 @@ namespace xna {
|
||||
return obj2;
|
||||
}
|
||||
|
||||
public:
|
||||
protected:
|
||||
template <typename T>
|
||||
sptr<T> ReadAsset(String const& assetName) {
|
||||
auto input = OpenStream(assetName);
|
||||
auto contentReader = ContentReader::Create(this, input, assetName);
|
||||
|
||||
return contentReader->ReadAsset<T>();
|
||||
}
|
||||
|
||||
sptr<Stream> OpenStream(String const& assetName) {
|
||||
|
@ -4,12 +4,91 @@
|
||||
#include "typereadermanager.hpp"
|
||||
|
||||
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);
|
||||
|
||||
@ -54,6 +133,16 @@ namespace xna {
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -3,29 +3,111 @@
|
||||
|
||||
#include "../default.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 {
|
||||
class ContentReader : public BinaryReader{
|
||||
class ContentReader : public BinaryReader, public std::enable_shared_from_this<ContentReader>{
|
||||
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>
|
||||
sptr<T> ReadAsset() {
|
||||
return nullptr;
|
||||
}
|
||||
sptr<T> ReadAsset();
|
||||
|
||||
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:
|
||||
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){}
|
||||
|
||||
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();
|
||||
|
||||
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:
|
||||
ContentManager* _contentManager = nullptr;
|
||||
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
|
@ -82,7 +82,7 @@ namespace xna {
|
||||
std::map<sptr<Type>, sptr<ContentTypeReader>>::iterator it;
|
||||
|
||||
for (it = readerTypeToReader.begin(); it != readerTypeToReader.end(); it++) {
|
||||
if (it->first->FullName == readerTypeName)
|
||||
if (it->first->FullName() == readerTypeName)
|
||||
type = it->first;
|
||||
}
|
||||
|
||||
@ -111,7 +111,8 @@ namespace xna {
|
||||
}
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
@ -139,10 +140,5 @@ namespace xna {
|
||||
targetTypeToReader.insert({ typeof<Object>(), contentTypeReader});
|
||||
readerTypeToReader.insert({ typeof<ObjectReader>(), contentTypeReader});
|
||||
}
|
||||
}
|
||||
|
||||
sptr<void> ObjectReader::Read(ContentReader input, sptr<void> existingInstance)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
}
|
@ -5,42 +5,43 @@
|
||||
#include "../default.hpp"
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <any>
|
||||
|
||||
namespace xna {
|
||||
//-------------------------------------------------------//
|
||||
// ContentTypeReader //
|
||||
//-------------------------------------------------------//
|
||||
class ContentTypeReader : public Object {
|
||||
public:
|
||||
ContentTypeReader(){}
|
||||
|
||||
class ContentTypeReader {
|
||||
public:
|
||||
virtual Int TypeVersion() { return 0; }
|
||||
virtual bool CanDeserializeIntoExistingObject() { return false; }
|
||||
virtual void Initialize(sptr<ContentTypeReaderManager>& manager) {}
|
||||
|
||||
sptr<Type> TargetType() { return _targetType; }
|
||||
|
||||
virtual sptr<Type> GetType() const override {
|
||||
auto type = New<Type>();
|
||||
type->FullName = "xna::ContentTypeReader";
|
||||
type->Namespace = "xna";
|
||||
type->IsClass = true;
|
||||
return type;
|
||||
}
|
||||
sptr<Type> TargetType() { return _targetType; }
|
||||
virtual std::any Read(ContentReader& input, std::any& existingInstance) = 0;
|
||||
|
||||
protected:
|
||||
ContentTypeReader(sptr<Type> const& targetType) : _targetType(targetType)
|
||||
{
|
||||
}
|
||||
|
||||
virtual sptr<void> Read(ContentReader input, sptr<void> existingInstance) = 0;
|
||||
{}
|
||||
|
||||
public:
|
||||
bool TargetIsValueType{ false };
|
||||
|
||||
private:
|
||||
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 //
|
||||
//-------------------------------------------------------//
|
||||
class ObjectReader : public ContentTypeReader {
|
||||
class ObjectReader : public ContentTypeReaderT<Object> {
|
||||
public:
|
||||
ObjectReader() : ContentTypeReader(typeof(this)){
|
||||
ObjectReader() : ContentTypeReaderT(typeof<Object>()) {
|
||||
ContentTypeReaderActivador::SetActivador(typeof(this), []() -> sptr<ContentTypeReader> {
|
||||
auto obj = New <ObjectReader>();
|
||||
return reinterpret_pointer_cast<ContentTypeReader>(obj);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Inherited via ContentTypeReader
|
||||
sptr<void> Read(ContentReader input, sptr<void> existingInstance) override;
|
||||
|
||||
sptr<Type> GetType() const override{
|
||||
auto type = New<Type>();
|
||||
type->FullName = "xna::ObjectReader";
|
||||
type->Namespace = "xna";
|
||||
type->IsClass = true;
|
||||
return type;
|
||||
virtual Object Read(ContentReader& input, Object existingInstance) override {
|
||||
return Object();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -2,15 +2,6 @@
|
||||
#include "type.hpp"
|
||||
|
||||
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 seed = 0;
|
||||
|
@ -6,7 +6,6 @@
|
||||
namespace xna {
|
||||
class Object {
|
||||
public:
|
||||
virtual sptr<Type> GetType() const;
|
||||
virtual size_t GetHashCode() const;
|
||||
};
|
||||
}
|
||||
|
@ -1,27 +1,14 @@
|
||||
#include "type.hpp"
|
||||
|
||||
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 seed = 0;
|
||||
XnaHHashCombine(seed, Namespace);
|
||||
XnaHHashCombine(seed, FullName);
|
||||
XnaHHashCombine(seed, IsInterface);
|
||||
XnaHHashCombine(seed, IsArray);
|
||||
XnaHHashCombine(seed, IsPointer);
|
||||
XnaHHashCombine(seed, IsClass);
|
||||
XnaHHashCombine(seed, IsCOMObject);
|
||||
XnaHHashCombine(seed, IsEnum);
|
||||
XnaHHashCombine(seed, IsValueType);
|
||||
XnaHHashCombine(seed, fullName);
|
||||
XnaHHashCombine(seed, isClass);
|
||||
XnaHHashCombine(seed, isEnum);
|
||||
XnaHHashCombine(seed, isValueType);
|
||||
XnaHHashCombine(seed, isPrimitive);
|
||||
|
||||
return seed;
|
||||
}
|
||||
|
@ -3,59 +3,81 @@
|
||||
|
||||
#include "../default.hpp"
|
||||
#include "object.hpp"
|
||||
#include <type_traits>
|
||||
#include <typeinfo>
|
||||
|
||||
namespace xna {
|
||||
class Type : public Object {
|
||||
public:
|
||||
String Namespace;
|
||||
String FullName;
|
||||
bool IsInterface{ false };
|
||||
bool IsArray{ false };
|
||||
bool IsPointer{ false };
|
||||
bool IsClass{ false };
|
||||
bool IsCOMObject{ false };
|
||||
bool IsEnum{ false };
|
||||
bool IsValueType{ false };
|
||||
constexpr String FullName() const { return fullName; }
|
||||
constexpr bool IsClass() const { return isClass; }
|
||||
constexpr bool IsEnum() const { return isEnum; }
|
||||
constexpr bool IsValueType() const { return isValueType; }
|
||||
constexpr bool IsPrimitive() const { return isPrimitive; }
|
||||
|
||||
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;
|
||||
|
||||
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 {
|
||||
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>
|
||||
inline sptr<Type> typeof() {
|
||||
auto t = New<T>();
|
||||
auto obj = reinterpret_pointer_cast<Object>(t);
|
||||
if (std::is_arithmetic<T>::value) {
|
||||
auto primitiveType = New<Type>();
|
||||
primitiveType->fullName = typeid(T).name();
|
||||
primitiveType->isPrimitive = true;
|
||||
primitiveType->isValueType = true;
|
||||
return primitiveType;
|
||||
}
|
||||
|
||||
if (!obj) return nullptr;
|
||||
|
||||
return obj->GetType();
|
||||
}
|
||||
if (std::is_enum<T>::value) {
|
||||
auto enumType = New<Type>();
|
||||
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>
|
||||
inline sptr<Type> typeof(T const* object) {
|
||||
auto obj = reinterpret_cast<const Object*>(object);
|
||||
return typeof<T>();
|
||||
}
|
||||
|
||||
if (!obj)
|
||||
return nullptr;
|
||||
|
||||
return obj->GetType();
|
||||
template <class T>
|
||||
inline sptr<Type> typeof(T const& object) {
|
||||
return typeof<T>();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user