mirror of
https://github.com/borgesdan/xn65
synced 2024-12-29 21:54:47 +01:00
Comentários em ContentReader
This commit is contained in:
parent
b35f48a78a
commit
163b5ba4b5
@ -2,16 +2,19 @@
|
|||||||
#include "content/manager.hpp"
|
#include "content/manager.hpp"
|
||||||
#include "content/lzx/decoderstream.hpp"
|
#include "content/lzx/decoderstream.hpp"
|
||||||
#include "content/typereadermanager.hpp"
|
#include "content/typereadermanager.hpp"
|
||||||
#include "content/manager.hpp"
|
|
||||||
|
|
||||||
namespace xna {
|
namespace xna {
|
||||||
sptr<ContentReader> ContentReader::Create(ContentManager* contentManager, sptr<Stream>& input, String const& assetName)
|
sptr<ContentReader> ContentReader::Create(sptr<xna::ContentManager> const& contentManager, sptr<Stream>& input, String const& assetName)
|
||||||
{
|
{
|
||||||
Int graphicsProfile = 0;
|
Int graphicsProfile = 0;
|
||||||
input = ContentReader::PrepareStream(input, assetName, graphicsProfile);
|
input = ContentReader::PrepareStream(input, assetName, graphicsProfile);
|
||||||
return std::shared_ptr<ContentReader>(new ContentReader(contentManager, input, assetName, graphicsProfile));
|
return std::shared_ptr<ContentReader>(new ContentReader(contentManager, input, assetName, graphicsProfile));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sptr<ContentManager> ContentReader::ContentManager() const {
|
||||||
|
return _contentManager;
|
||||||
|
}
|
||||||
|
|
||||||
Vector2 ContentReader::ReadVector2()
|
Vector2 ContentReader::ReadVector2()
|
||||||
{
|
{
|
||||||
Vector2 vector2;
|
Vector2 vector2;
|
||||||
@ -91,25 +94,22 @@ namespace xna {
|
|||||||
|
|
||||||
std::vector<Byte> ContentReader::ReadByteBuffer(size_t size, xna_error_ptr_arg)
|
std::vector<Byte> ContentReader::ReadByteBuffer(size_t size, xna_error_ptr_arg)
|
||||||
{
|
{
|
||||||
std::vector<Byte>& buffer = _contentManager->byteBuffer;
|
if (byteBuffer.empty() || byteBuffer.size() < size)
|
||||||
|
|
||||||
if (buffer.empty() || buffer.size() < size)
|
|
||||||
{
|
{
|
||||||
buffer = std::vector<Byte>(size);
|
byteBuffer.resize(size);
|
||||||
//_contentManager->byteBuffer = buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Int num = 0;
|
Int num = 0;
|
||||||
for (size_t index = 0; index < size; index += num)
|
for (size_t index = 0; index < size; index += num)
|
||||||
{
|
{
|
||||||
num = Read(buffer, index, size - index);
|
num = Read(byteBuffer, index, size - index);
|
||||||
if (num == 0) {
|
if (num == 0) {
|
||||||
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
|
xna_error_apply(err, XnaErrorCode::FAILED_OPERATION);
|
||||||
return std::vector<Byte>();
|
return std::vector<Byte>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return buffer;
|
return byteBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
sptr<Stream> ContentReader::PrepareStream(sptr<Stream>& input, String const& assetName, Int& graphicsProfile)
|
sptr<Stream> ContentReader::PrepareStream(sptr<Stream>& input, String const& assetName, Int& graphicsProfile)
|
||||||
@ -117,15 +117,16 @@ namespace xna {
|
|||||||
BinaryReader binaryReader = BinaryReader(input);
|
BinaryReader binaryReader = BinaryReader(input);
|
||||||
|
|
||||||
if (binaryReader.ReadByte() != 'X' || binaryReader.ReadByte() != 'N' || binaryReader.ReadByte() != 'B')
|
if (binaryReader.ReadByte() != 'X' || binaryReader.ReadByte() != 'N' || binaryReader.ReadByte() != 'B')
|
||||||
return nullptr;
|
throw std::runtime_error("ContentReader::PrepareStream: Bad xbn platform.");
|
||||||
|
|
||||||
Int num1 = 0;
|
Int num1 = 0;
|
||||||
auto _byte = binaryReader.ReadByte(); //desfazer
|
auto wbyte = binaryReader.ReadByte();
|
||||||
if (_byte == 'w')
|
|
||||||
num1 = binaryReader.ReadUInt16();
|
if(wbyte != 'w')
|
||||||
else
|
|
||||||
throw std::runtime_error("ContentReader::PrepareStream: Bad xbn file.");
|
throw std::runtime_error("ContentReader::PrepareStream: Bad xbn file.");
|
||||||
|
|
||||||
|
num1 = binaryReader.ReadUInt16();
|
||||||
|
|
||||||
graphicsProfile = (num1 & XnbVersionProfileMask) >> XnbVersionProfileShift;
|
graphicsProfile = (num1 & XnbVersionProfileMask) >> XnbVersionProfileShift;
|
||||||
bool flag = false;
|
bool flag = false;
|
||||||
|
|
||||||
@ -138,13 +139,13 @@ namespace xna {
|
|||||||
flag = true;
|
flag = true;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return nullptr;
|
throw std::runtime_error("ContentReader::PrepareStream: Bad xbn version.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto num2 = binaryReader.ReadInt32();
|
const auto num2 = binaryReader.ReadInt32();
|
||||||
|
|
||||||
if ((num2 - 10) > input->Length() - input->Position())
|
if ((static_cast<Long>(num2) - 10) > input->Length() - input->Position())
|
||||||
return nullptr;
|
throw std::runtime_error("ContentReader::PrepareStream: Bad xbn size.");
|
||||||
|
|
||||||
if (!flag)
|
if (!flag)
|
||||||
return input;
|
return input;
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
namespace xna {
|
namespace xna {
|
||||||
//The run-time component which loads managed objects from the binary files produced by the design time content pipeline.
|
//The run-time component which loads managed objects from the binary files produced by the design time content pipeline.
|
||||||
class ContentManager {
|
class ContentManager : public std::enable_shared_from_this<ContentManager> {
|
||||||
public:
|
public:
|
||||||
ContentManager(sptr<IServiceProvider> const& services) :
|
ContentManager(sptr<IServiceProvider> const& services) :
|
||||||
_rootDirectory("") {
|
_rootDirectory("") {
|
||||||
@ -82,7 +82,8 @@ namespace xna {
|
|||||||
if (!input)
|
if (!input)
|
||||||
return XnaHelper::ReturnDefaultOrNull<T>();
|
return XnaHelper::ReturnDefaultOrNull<T>();
|
||||||
|
|
||||||
auto contentReader = ContentReader::Create(this, input, assetName);
|
const auto _this = shared_from_this();
|
||||||
|
auto contentReader = ContentReader::Create(_this, input, assetName);
|
||||||
|
|
||||||
auto asset = contentReader->ReadAsset<T>();
|
auto asset = contentReader->ReadAsset<T>();
|
||||||
return asset;
|
return asset;
|
||||||
@ -94,8 +95,7 @@ namespace xna {
|
|||||||
friend class ContentReader;
|
friend class ContentReader;
|
||||||
friend class Game;
|
friend class Game;
|
||||||
|
|
||||||
String _rootDirectory;
|
String _rootDirectory;
|
||||||
std::vector<Byte> byteBuffer;
|
|
||||||
sptr<IServiceProvider> _services = nullptr;
|
sptr<IServiceProvider> _services = nullptr;
|
||||||
std::map<String, sptr<void>> _loadedAssets;
|
std::map<String, sptr<void>> _loadedAssets;
|
||||||
|
|
||||||
|
@ -10,38 +10,63 @@
|
|||||||
#include <any>
|
#include <any>
|
||||||
|
|
||||||
namespace xna {
|
namespace xna {
|
||||||
|
//A worker object that implements most of ContentManager.Load.
|
||||||
class ContentReader : public BinaryReader, public std::enable_shared_from_this<ContentReader> {
|
class ContentReader : public BinaryReader, public std::enable_shared_from_this<ContentReader> {
|
||||||
public:
|
public:
|
||||||
static sptr<ContentReader> Create(ContentManager* contentManager, sptr<Stream>& input, String const& assetName);
|
static sptr<ContentReader> Create(sptr<ContentManager> const& contentManager, sptr<Stream>& input, String const& assetName);
|
||||||
|
|
||||||
|
// Reads a single object from the current stream.
|
||||||
|
template <typename T>
|
||||||
|
auto ReadObject();
|
||||||
|
|
||||||
|
// Reads a single object from the current stream.
|
||||||
|
template <typename T>
|
||||||
|
auto ReadObject(T existingInstance);
|
||||||
|
|
||||||
|
// Reads a single object from the current stream.
|
||||||
|
template <typename T>
|
||||||
|
auto ReadObject(ContentTypeReader& typeReader);
|
||||||
|
|
||||||
|
// Reads a single object from the current stream.
|
||||||
|
template <typename T>
|
||||||
|
auto ReadObject(ContentTypeReader& typeReader, T existingInstance);
|
||||||
|
|
||||||
|
//Reads a Vector2 value from the current stream.
|
||||||
|
Vector2 ReadVector2();
|
||||||
|
//Reads a Vector3 value from the current stream.
|
||||||
|
Vector3 ReadVector3();
|
||||||
|
//Reads a Vector4 value from the current stream.
|
||||||
|
Vector4 ReadVector4();
|
||||||
|
//Reads a Matrix value from the currently open stream.
|
||||||
|
Matrix ReadMatrix();
|
||||||
|
//Reads a Quaternion value from the current stream.
|
||||||
|
Quaternion ReadQuaternion();
|
||||||
|
//Reads a Color value from the currently open stream.
|
||||||
|
Color ReadColor();
|
||||||
|
//Reads a float value from the currently open stream.
|
||||||
|
float ReadSingle();
|
||||||
|
//Reads a double value from the currently open stream.
|
||||||
|
double ReadDouble();
|
||||||
|
|
||||||
|
//Gets the name of the asset currently being read by this ContentReader.
|
||||||
|
constexpr String AssetName() const {
|
||||||
|
return _assetName;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Gets the ContentManager associated with the ContentReader.
|
||||||
|
sptr<xna::ContentManager> ContentManager() const;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Internal methods
|
||||||
|
//
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
auto ReadAsset();
|
auto ReadAsset();
|
||||||
|
|
||||||
template <typename T>
|
std::vector<Byte> ReadByteBuffer(size_t size, xna_error_nullarg);
|
||||||
auto ReadObject();
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
auto ReadObject(T existingInstance);
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
auto ReadObject(ContentTypeReader& typeReader);
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
auto ReadObject(ContentTypeReader& typeReader, T existingInstance);
|
|
||||||
|
|
||||||
Vector2 ReadVector2();
|
|
||||||
Vector3 ReadVector3();
|
|
||||||
Vector4 ReadVector4();
|
|
||||||
Matrix ReadMatrix();
|
|
||||||
Quaternion ReadQuaternion();
|
|
||||||
Color ReadColor();
|
|
||||||
float ReadSingle();
|
|
||||||
double ReadDouble();
|
|
||||||
|
|
||||||
std::vector<Byte> ReadByteBuffer(size_t size, xna_error_nullarg);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ContentReader(ContentManager* contentManager, sptr<Stream>& input, String const& assetName, Int graphicsProfile)
|
ContentReader(sptr<xna::ContentManager> const& 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);
|
||||||
@ -58,10 +83,11 @@ namespace xna {
|
|||||||
auto InvokeReader(ContentTypeReader& reader, std::any& existingInstance, xna_error_nullarg);
|
auto InvokeReader(ContentTypeReader& reader, std::any& existingInstance, xna_error_nullarg);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ContentManager* _contentManager = nullptr;
|
sptr<xna::ContentManager> _contentManager = nullptr;
|
||||||
String _assetName;
|
String _assetName;
|
||||||
std::vector<sptr<ContentTypeReader>> typeReaders;
|
std::vector<sptr<ContentTypeReader>> typeReaders;
|
||||||
Int graphicsProfile{ 0 };
|
Int graphicsProfile{ 0 };
|
||||||
|
std::vector<Byte> byteBuffer;
|
||||||
|
|
||||||
static constexpr Ushort XnbVersionProfileMask = 32512;
|
static constexpr Ushort XnbVersionProfileMask = 32512;
|
||||||
static constexpr Ushort XnbCompressedVersion = 32773;
|
static constexpr Ushort XnbCompressedVersion = 32773;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user