2024-06-03 21:55:09 -03:00
|
|
|
#include "xna/content/reader.hpp"
|
|
|
|
#include "xna/content/manager.hpp"
|
|
|
|
#include "xna/content/typereadermanager.hpp"
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
namespace xna {
|
2024-12-13 17:28:46 -03:00
|
|
|
std::shared_ptr<ContentReader> ContentReader::Create(std::shared_ptr<xna::ContentManager> const& contentManager, std::shared_ptr<csharp::Stream>& input, String const& assetName)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-03 11:49:01 -03:00
|
|
|
Int graphicsProfile = 0;
|
|
|
|
input = ContentReader::PrepareStream(input, assetName, graphicsProfile);
|
|
|
|
return std::shared_ptr<ContentReader>(new ContentReader(contentManager, input, assetName, graphicsProfile));
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
2024-05-01 19:09:43 -03:00
|
|
|
|
2024-12-13 17:28:46 -03:00
|
|
|
std::shared_ptr<ContentManager> ContentReader::ContentManager() const {
|
2024-06-03 10:13:59 -03:00
|
|
|
return _contentManager;
|
|
|
|
}
|
|
|
|
|
2024-05-03 11:49:01 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-06-03 11:05:10 -03:00
|
|
|
std::vector<Byte> ContentReader::ReadByteBuffer(size_t size)
|
2024-05-05 15:50:17 -03:00
|
|
|
{
|
2024-06-03 10:13:59 -03:00
|
|
|
if (byteBuffer.empty() || byteBuffer.size() < size)
|
2024-05-05 15:50:17 -03:00
|
|
|
{
|
2024-06-03 10:13:59 -03:00
|
|
|
byteBuffer.resize(size);
|
2024-05-05 15:50:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
Int num = 0;
|
|
|
|
for (size_t index = 0; index < size; index += num)
|
|
|
|
{
|
2024-12-13 17:28:46 -03:00
|
|
|
num = Read(byteBuffer.data(), byteBuffer.size(), index, size - index);
|
2024-05-05 15:50:17 -03:00
|
|
|
if (num == 0) {
|
2024-06-03 11:05:10 -03:00
|
|
|
throw std::runtime_error("ContentReader::ReadByteBuffer: Bad xbn.");
|
2024-05-05 15:50:17 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-03 10:13:59 -03:00
|
|
|
return byteBuffer;
|
2024-05-05 15:50:17 -03:00
|
|
|
}
|
|
|
|
|
2024-12-13 17:28:46 -03:00
|
|
|
std::shared_ptr<csharp::Stream> ContentReader::PrepareStream(std::shared_ptr<csharp::Stream>& input, String const& assetName, Int& graphicsProfile)
|
2024-05-01 19:09:43 -03:00
|
|
|
{
|
|
|
|
BinaryReader binaryReader = BinaryReader(input);
|
|
|
|
|
|
|
|
if (binaryReader.ReadByte() != 'X' || binaryReader.ReadByte() != 'N' || binaryReader.ReadByte() != 'B')
|
2024-06-03 10:13:59 -03:00
|
|
|
throw std::runtime_error("ContentReader::PrepareStream: Bad xbn platform.");
|
2024-05-01 19:09:43 -03:00
|
|
|
|
|
|
|
Int num1 = 0;
|
2024-06-03 10:13:59 -03:00
|
|
|
auto wbyte = binaryReader.ReadByte();
|
|
|
|
|
|
|
|
if(wbyte != 'w')
|
2024-05-29 15:58:37 -03:00
|
|
|
throw std::runtime_error("ContentReader::PrepareStream: Bad xbn file.");
|
2024-05-01 19:09:43 -03:00
|
|
|
|
2024-06-03 10:13:59 -03:00
|
|
|
num1 = binaryReader.ReadUInt16();
|
|
|
|
|
2024-05-06 09:58:40 -03:00
|
|
|
graphicsProfile = (num1 & XnbVersionProfileMask) >> XnbVersionProfileShift;
|
2024-05-01 19:09:43 -03:00
|
|
|
bool flag = false;
|
|
|
|
|
|
|
|
switch (num1 & -32513)
|
|
|
|
{
|
2024-05-06 09:58:40 -03:00
|
|
|
case XnbVersion:
|
2024-05-01 19:09:43 -03:00
|
|
|
flag = false;
|
|
|
|
break;
|
2024-05-06 09:58:40 -03:00
|
|
|
case XnbCompressedVersion:
|
2024-05-01 19:09:43 -03:00
|
|
|
flag = true;
|
|
|
|
break;
|
|
|
|
default:
|
2024-06-03 10:13:59 -03:00
|
|
|
throw std::runtime_error("ContentReader::PrepareStream: Bad xbn version.");
|
2024-05-01 19:09:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto num2 = binaryReader.ReadInt32();
|
|
|
|
|
2024-06-03 10:13:59 -03:00
|
|
|
if ((static_cast<Long>(num2) - 10) > input->Length() - input->Position())
|
|
|
|
throw std::runtime_error("ContentReader::PrepareStream: Bad xbn size.");
|
2024-05-01 19:09:43 -03:00
|
|
|
|
|
|
|
if (!flag)
|
|
|
|
return input;
|
|
|
|
|
|
|
|
const Int compressedTodo = num2 - 14;
|
|
|
|
const auto decompressedTodo = binaryReader.ReadInt32();
|
|
|
|
|
2024-06-04 14:37:28 -03:00
|
|
|
throw std::runtime_error("ContentReader::PrepareStream: LzxDecoder not implemented.");
|
2024-05-01 19:09:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
Int ContentReader::ReadHeader() {
|
2024-05-03 11:49:01 -03:00
|
|
|
auto _this = shared_from_this();
|
|
|
|
typeReaders = ContentTypeReaderManager::ReadTypeManifest(this->Read7BitEncodedInt(), _this);
|
2024-06-03 11:05:10 -03:00
|
|
|
auto length = this->Read7BitEncodedInt();
|
2024-05-03 11:49:01 -03:00
|
|
|
|
|
|
|
return length;
|
|
|
|
|
2024-05-01 19:09:43 -03:00
|
|
|
}
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|