mirror of
https://github.com/borgesdan/xn65
synced 2024-12-29 21:54:47 +01:00
Remove xna/stream e binary.hpp
This commit is contained in:
parent
35ff33b0b5
commit
a1e79518f4
@ -2,7 +2,7 @@
|
||||
#define XNA_CONTENT_LZX_LZXDECODE_HPP
|
||||
|
||||
#include "../../default.hpp"
|
||||
#include "../../csharp/stream.hpp"
|
||||
#include "csharp/io/stream.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
namespace xna {
|
||||
|
@ -2,7 +2,7 @@
|
||||
#define XNA_CONTENT_MANAGER_HPP
|
||||
|
||||
#include "../csharp/service.hpp"
|
||||
#include "../csharp/stream.hpp"
|
||||
#include "csharp/io/stream.hpp"
|
||||
#include "../default.hpp"
|
||||
#include "reader.hpp"
|
||||
|
||||
@ -10,34 +10,34 @@ namespace xna {
|
||||
//The run-time component which loads managed objects from the binary files produced by the design time content pipeline.
|
||||
class ContentManager : public std::enable_shared_from_this<ContentManager> {
|
||||
public:
|
||||
ContentManager(sptr<IServiceProvider> const& services) :
|
||||
ContentManager(std::shared_ptr<IServiceProvider> const& services) :
|
||||
rootDirectory("") {
|
||||
serviceProvider = services;
|
||||
};
|
||||
|
||||
ContentManager(sptr<IServiceProvider> const& services, String const& rootDirectory) :
|
||||
ContentManager(std::shared_ptr<IServiceProvider> const& services, std::string const& rootDirectory) :
|
||||
rootDirectory(rootDirectory){
|
||||
serviceProvider = services;
|
||||
};
|
||||
|
||||
//Gets the service provider associated with the ContentManager.
|
||||
sptr<IServiceProvider> ServiceProvider() const {
|
||||
std::shared_ptr<IServiceProvider> ServiceProvider() const {
|
||||
return serviceProvider;
|
||||
}
|
||||
|
||||
//Gets or sets the root directory associated with this ContentManager.
|
||||
constexpr String RootDirectory() const {
|
||||
constexpr std::string RootDirectory() const {
|
||||
return rootDirectory;
|
||||
}
|
||||
|
||||
//Gets or sets the root directory associated with this ContentManager.
|
||||
void RootDirectory(String const& value) {
|
||||
void RootDirectory(std::string const& value) {
|
||||
rootDirectory = value;
|
||||
}
|
||||
|
||||
//Loads an asset that has been processed by the Content Pipeline.
|
||||
template <typename T>
|
||||
auto Load(String const& assetName) {
|
||||
auto Load(std::string const& assetName) {
|
||||
if (assetName.empty()) {
|
||||
return misc::ReturnDefaultOrNull<T>();
|
||||
}
|
||||
@ -69,13 +69,13 @@ namespace xna {
|
||||
}
|
||||
|
||||
//Gets the service provider associated with the main Game.
|
||||
static sptr<IServiceProvider> GameServiceProvider() {
|
||||
static std::shared_ptr<IServiceProvider> GameServiceProvider() {
|
||||
return mainGameService;
|
||||
}
|
||||
|
||||
protected:
|
||||
template <typename T>
|
||||
auto ReadAsset(String const& assetName) {
|
||||
auto ReadAsset(std::string const& assetName) {
|
||||
auto input = OpenStream(assetName);
|
||||
|
||||
if (!input)
|
||||
@ -88,18 +88,18 @@ namespace xna {
|
||||
return asset;
|
||||
}
|
||||
|
||||
sptr<Stream> OpenStream(String const& assetName) const;
|
||||
std::shared_ptr<csharp::Stream> OpenStream(std::string const& assetName) const;
|
||||
|
||||
private:
|
||||
friend class ContentReader;
|
||||
friend class Game;
|
||||
|
||||
String rootDirectory;
|
||||
sptr<IServiceProvider> serviceProvider = nullptr;
|
||||
std::map<String, sptr<void>> loadedAssets;
|
||||
std::string rootDirectory;
|
||||
std::shared_ptr<IServiceProvider> serviceProvider = nullptr;
|
||||
std::map<std::string, std::shared_ptr<void>> loadedAssets;
|
||||
|
||||
inline static sptr<IServiceProvider> mainGameService = nullptr;
|
||||
inline const static String contentExtension = ".xnb";
|
||||
inline static std::shared_ptr<IServiceProvider> mainGameService = nullptr;
|
||||
inline const static std::string contentExtension = ".xnb";
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -3,17 +3,20 @@
|
||||
|
||||
#include "../common/color.hpp"
|
||||
#include "../common/numerics.hpp"
|
||||
#include "../csharp/binary.hpp"
|
||||
#include "../csharp/type.hpp"
|
||||
#include "../default.hpp"
|
||||
#include "csharp/io/binary.hpp"
|
||||
#include "typereadermanager.hpp"
|
||||
#include <any>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
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 csharp::BinaryReader, public std::enable_shared_from_this<ContentReader> {
|
||||
public:
|
||||
static sptr<ContentReader> Create(sptr<ContentManager> const& contentManager, sptr<Stream>& input, String const& assetName);
|
||||
static std::shared_ptr<ContentReader> Create(std::shared_ptr<ContentManager> const& contentManager, std::shared_ptr<csharp::Stream>& input, std::string const& assetName);
|
||||
|
||||
// Reads a single object from the current stream.
|
||||
template <typename T>
|
||||
@ -49,12 +52,12 @@ namespace xna {
|
||||
double ReadDouble() override;
|
||||
|
||||
//Gets the name of the asset currently being read by this ContentReader.
|
||||
constexpr String AssetName() const {
|
||||
constexpr std::string AssetName() const {
|
||||
return _assetName;
|
||||
}
|
||||
|
||||
//Gets the ContentManager associated with the ContentReader.
|
||||
sptr<xna::ContentManager> ContentManager() const;
|
||||
std::shared_ptr<xna::ContentManager> ContentManager() const;
|
||||
|
||||
//
|
||||
// Internal methods
|
||||
@ -63,15 +66,15 @@ namespace xna {
|
||||
template <typename T>
|
||||
auto ReadAsset();
|
||||
|
||||
std::vector<Byte> ReadByteBuffer(size_t size);
|
||||
std::vector<uint8_t> ReadByteBuffer(size_t size);
|
||||
|
||||
private:
|
||||
ContentReader(sptr<xna::ContentManager> const& contentManager, sptr<Stream>& input, String const& assetName, Int graphicsProfile)
|
||||
: BinaryReader(input), _contentManager(contentManager), _assetName(assetName) {}
|
||||
ContentReader(std::shared_ptr<xna::ContentManager> const& contentManager, std::shared_ptr<csharp::Stream>& input, std::string const& assetName, int32_t graphicsProfile)
|
||||
: csharp::BinaryReader(input), _contentManager(contentManager), _assetName(assetName) {}
|
||||
|
||||
static sptr<Stream> PrepareStream(sptr<Stream>& input, String const& assetName, Int& graphicsProfile);
|
||||
static std::shared_ptr<csharp::Stream> PrepareStream(std::shared_ptr<csharp::Stream>& input, std::string const& assetName, int32_t& graphicsProfile);
|
||||
|
||||
Int ReadHeader();
|
||||
int32_t ReadHeader();
|
||||
|
||||
template <typename T>
|
||||
auto ReadObjectInternal(std::any& existingInstance);
|
||||
@ -83,19 +86,19 @@ namespace xna {
|
||||
auto InvokeReader(ContentTypeReader& reader, Object& existingInstance);
|
||||
|
||||
private:
|
||||
sptr<xna::ContentManager> _contentManager = nullptr;
|
||||
String _assetName;
|
||||
std::vector<sptr<ContentTypeReader>> typeReaders;
|
||||
Int graphicsProfile{ 0 };
|
||||
std::vector<Byte> byteBuffer;
|
||||
std::shared_ptr<xna::ContentManager> _contentManager = nullptr;
|
||||
std::string _assetName;
|
||||
std::vector<std::shared_ptr<ContentTypeReader>> typeReaders;
|
||||
int32_t graphicsProfile{ 0 };
|
||||
std::vector<uint8_t> byteBuffer;
|
||||
|
||||
static constexpr Ushort XnbVersionProfileMask = 32512;
|
||||
static constexpr Ushort XnbCompressedVersion = 32773;
|
||||
static constexpr Ushort XnbVersion = 5;
|
||||
static constexpr Int XnbVersionProfileShift = 8;
|
||||
static constexpr Char PlatformLabel = 'w';
|
||||
static constexpr Int XnbPrologueSize = 10;
|
||||
static constexpr Int XnbCompressedPrologueSize = 14;
|
||||
static constexpr uint16_t XnbVersionProfileMask = 32512;
|
||||
static constexpr uint16_t XnbCompressedVersion = 32773;
|
||||
static constexpr uint16_t XnbVersion = 5;
|
||||
static constexpr int32_t XnbVersionProfileShift = 8;
|
||||
static constexpr char PlatformLabel = 'w';
|
||||
static constexpr int32_t XnbPrologueSize = 10;
|
||||
static constexpr int32_t XnbCompressedPrologueSize = 14;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
@ -1,154 +0,0 @@
|
||||
#ifndef XNA_CSHARP_BINARY_HPP
|
||||
#define XNA_CSHARP_BINARY_HPP
|
||||
|
||||
#include "stream.hpp"
|
||||
#include "../default.hpp"
|
||||
|
||||
namespace xna {
|
||||
//A simplified port of the System.IO.BinaryReader class.
|
||||
class BinaryReader {
|
||||
public:
|
||||
BinaryReader(sptr<Stream> const& input);
|
||||
|
||||
//Returns the next available character and does not advance the byte or character position.
|
||||
Int PeekChar();
|
||||
//Reads bytes from the underlying stream and advances the current position of the stream.
|
||||
virtual Int Read();
|
||||
//Reads a Boolean value from the current stream and advances the current position of the stream by one byte.
|
||||
virtual bool ReadBoolean();
|
||||
//Reads the next byte from the current stream and advances the current position of the stream by one byte.
|
||||
virtual Byte ReadByte();
|
||||
//Reads a signed byte from this stream and advances the current position of the stream by one byte.
|
||||
virtual Sbyte ReadSByte();
|
||||
//Reads the next character from the current stream and advances the current position of the stream.
|
||||
virtual Char ReadChar();
|
||||
//Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes.
|
||||
virtual Short ReadInt16();
|
||||
//Reads a 2-byte unsigned integer from the current stream and advances the position of the stream by two bytes.
|
||||
virtual Ushort ReadUInt16();
|
||||
//Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
|
||||
virtual Int ReadInt32();
|
||||
//Reads a 4-byte unsigned integer from the current stream and advances the position of the stream by four bytes.
|
||||
virtual Uint ReadUInt32();
|
||||
//Reads a 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes.
|
||||
virtual Long ReadInt64();
|
||||
//Reads a 8-byte unsigned integer from the current stream and advances the position of the stream by eight bytes.
|
||||
virtual Ulong ReadUInt64();
|
||||
//Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.
|
||||
virtual float ReadSingle();
|
||||
//Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.
|
||||
virtual double ReadDouble();
|
||||
//Reads a string from the current stream.
|
||||
virtual std::string ReadString();
|
||||
|
||||
//Reads chars from the underlying stream and advances the current position of the stream.
|
||||
virtual Int Read(std::vector<Char>& buffer, size_t index, size_t count);
|
||||
//Reads bytes from the underlying stream and advances the current position of the stream.
|
||||
virtual Int Read(std::vector<Byte>& buffer, size_t index, size_t count);
|
||||
|
||||
// Reads the specified number of bytes from the current stream into a byte array
|
||||
// and advances the current position by that number of bytes.
|
||||
virtual std::vector<Byte> ReadBytes(size_t count);
|
||||
|
||||
// Reads a 32-bit integer in compressed format.
|
||||
// This function may throw a std::format_error exception.
|
||||
Int Read7BitEncodedInt();
|
||||
|
||||
// Reads a 64-bit integer in compressed format.
|
||||
// This function may throw a std::format_error exception.
|
||||
Long Read7BitEncodedInt64();
|
||||
|
||||
//Exposes access to the underlying stream of the BinaryReader.
|
||||
virtual inline sptr<Stream> BaseStream() const {
|
||||
return stream;
|
||||
}
|
||||
|
||||
//Closes the current reader and the underlying stream.
|
||||
virtual inline void Close() {
|
||||
stream = nullptr;
|
||||
}
|
||||
|
||||
protected:
|
||||
Int InternalReadOneChar();
|
||||
void FillBuffer(Int numBytes);
|
||||
Int InternalReadChars(Char* buffer, size_t bufferSize, size_t index, size_t count);
|
||||
|
||||
private:
|
||||
static constexpr int maxCharBytesSize = 128;
|
||||
static constexpr int bufferLength = 16;
|
||||
sptr<Stream> stream = nullptr;
|
||||
std::vector<Byte> charBytes;
|
||||
std::vector<Char> singleChar;
|
||||
std::vector<Byte> buffer;
|
||||
std::vector<Char> charBuffer;
|
||||
};
|
||||
|
||||
//A simplified port of the System.IO.BinaryWriter class.
|
||||
class BinaryWriter {
|
||||
public:
|
||||
BinaryWriter(sptr<Stream> const& stream);
|
||||
|
||||
protected:
|
||||
BinaryWriter() = default;
|
||||
|
||||
//Sets the position within the current stream.
|
||||
Long Seek(Int offset, SeekOrigin origin);
|
||||
|
||||
//
|
||||
// Writes a value to the current stream.
|
||||
//
|
||||
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(bool value);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(Byte value);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(Sbyte value);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(Byte const* buffer, Int bufferLength);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(std::vector<Byte> const& buffer);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(Byte const* buffer, Int bufferLength, Int index, Int count);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(std::vector<Byte> const& buffer, Int index, Int count);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(Char ch);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(double value);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(Short value);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(Ushort value);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(Int value);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(Uint value);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(Long value);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(Ulong value);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(float value);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(std::string const& value);
|
||||
//Writes a value to the current stream.
|
||||
virtual void Write(const char* _string, size_t stringLength);
|
||||
|
||||
//Exposes access to the underlying stream of the BinaryWriter.
|
||||
virtual inline sptr<Stream> BaseStream() const {
|
||||
return OutStream;
|
||||
}
|
||||
|
||||
//Writes a 32-bit integer in a compressed format.
|
||||
void Write7BitEncodedInt(Int value);
|
||||
|
||||
protected:
|
||||
sptr<Stream> OutStream = nullptr;
|
||||
|
||||
private:
|
||||
std::vector<Byte> _buffer;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -1,206 +0,0 @@
|
||||
#ifndef XNA_CSHARP_STREAM_HPP
|
||||
#define XNA_CSHARP_STREAM_HPP
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include "csharp/exception.hpp"
|
||||
|
||||
namespace xna {
|
||||
// Provides seek reference points.
|
||||
// To seek to the end of a stream, call stream.Seek(0, SeekOrigin.End).
|
||||
enum class SeekOrigin {
|
||||
Begin,
|
||||
Current,
|
||||
End,
|
||||
};
|
||||
|
||||
// Contains constants for specifying how the OS should open a file.
|
||||
// These will control whether you overwrite a file, open an existing
|
||||
// file, or some combination thereof.
|
||||
//
|
||||
// To append to a file, use Append (which maps to OpenOrCreate then we seek
|
||||
// to the end of the file). To truncate a file or create it if it doesn't
|
||||
// exist, use Create.
|
||||
enum class FileMode {
|
||||
// Creates a new file. An exception is raised if the file already exists.
|
||||
CreateNew,
|
||||
// Creates a new file. If the file already exists, it is overwritten.
|
||||
Create,
|
||||
// Opens an existing file. An exception is raised if the file does not exist.
|
||||
Open,
|
||||
// Opens the file if it exists. Otherwise, creates a new file.
|
||||
OpenOrCreate,
|
||||
// Opens an existing file. Once opened, the file is truncated so that its
|
||||
// size is zero bytes. The calling process must open the file with at least
|
||||
// WRITE access. An exception is raised if the file does not exist.
|
||||
Truncate,
|
||||
// Opens the file if it exists and seeks to the end. Otherwise,
|
||||
// creates a new file.
|
||||
Append,
|
||||
};
|
||||
|
||||
//A simplified port of the System.IO.Stream.
|
||||
//Provides a generic view of a sequence of bytes. This is an abstract class.
|
||||
class Stream {
|
||||
public:
|
||||
virtual ~Stream(){}
|
||||
|
||||
//When overridden in a derived class, gets a value indicating whether the current stream supports reading.
|
||||
virtual bool CanRead() { return true; }
|
||||
//When overridden in a derived class, gets a value indicating whether the current stream supports writing.
|
||||
virtual bool CanWrite() { return true; }
|
||||
//When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
|
||||
virtual bool CanSeek() { return true; }
|
||||
//Gets a value that determines whether the current stream can time out.
|
||||
virtual bool CanTimeout() { return false; }
|
||||
|
||||
//Gets the length in bytes of the stream.
|
||||
virtual int64_t Length() = 0;
|
||||
//Gets or sets the position within the current stream.
|
||||
virtual int64_t Position() = 0;
|
||||
//Gets or sets the position within the current stream.
|
||||
virtual void Position(int64_t value) { Seek(value, SeekOrigin::Begin); }
|
||||
|
||||
//When overridden in a derived class, sets the length of the current stream.
|
||||
virtual void SetLength(int64_t value) = 0;
|
||||
|
||||
//Closes the current stream and releases any resources
|
||||
virtual void Close() = 0;
|
||||
//Sets the position within the current stream.
|
||||
virtual int64_t Seek(int64_t offset, SeekOrigin const& origin) = 0;
|
||||
|
||||
//Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
|
||||
virtual int32_t Read(uint8_t* buffer, int32_t bufferLength, int32_t offset, int32_t count) = 0;
|
||||
//Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
|
||||
virtual int32_t Read(std::vector<uint8_t>& buffer, int32_t offset, int32_t count) = 0;
|
||||
|
||||
//Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.
|
||||
virtual int32_t ReadByte() = 0;
|
||||
|
||||
//Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
|
||||
virtual void Write(uint8_t const* buffer, int32_t bufferLength, int32_t offset, int32_t count) = 0;
|
||||
//Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
|
||||
virtual void Write(std::vector<uint8_t> const& buffer, int32_t offset, int32_t count) = 0;
|
||||
|
||||
//Writes a byte to the current position in the stream and advances the position within the stream by one byte.
|
||||
virtual void WriteByte(uint8_t value) = 0;
|
||||
};
|
||||
|
||||
//A simplified port of the System.IO.MemoryStream.
|
||||
class MemoryStream : public Stream {
|
||||
public:
|
||||
constexpr MemoryStream(){}
|
||||
|
||||
constexpr MemoryStream(std::vector<uint8_t> const& bytes):
|
||||
_buffer(bytes),
|
||||
_length(static_cast<int64_t>(bytes.size())){}
|
||||
|
||||
~MemoryStream() override {
|
||||
Close();
|
||||
}
|
||||
|
||||
constexpr MemoryStream(int32_t capacity) :
|
||||
_buffer(static_cast<size_t>(capacity)),
|
||||
_length(capacity > 0 ? capacity : 0){}
|
||||
|
||||
virtual constexpr int64_t Length() override {
|
||||
if (_closed)
|
||||
return 0;
|
||||
|
||||
return _length;
|
||||
}
|
||||
|
||||
virtual constexpr void SetLength(int64_t value) override {
|
||||
_buffer.reserve(value);
|
||||
_length = value;
|
||||
}
|
||||
|
||||
virtual constexpr int64_t Position() override {
|
||||
if (_closed)
|
||||
return 0;
|
||||
|
||||
return _position;
|
||||
}
|
||||
|
||||
virtual constexpr void Close() override {
|
||||
_closed = true;
|
||||
_buffer = std::vector<uint8_t>();
|
||||
|
||||
}
|
||||
|
||||
virtual int64_t Seek(int64_t offset, SeekOrigin const& origin) override;
|
||||
virtual int32_t Read(uint8_t* buffer, int32_t bufferLength, int32_t offset, int32_t count) override;
|
||||
virtual int32_t Read(std::vector<uint8_t>& buffer, int32_t offset, int32_t count) override;
|
||||
virtual int32_t ReadByte() override;
|
||||
virtual void Write(uint8_t const* buffer, int32_t bufferLength, int32_t offset, int32_t count) override;
|
||||
virtual void Write(std::vector<uint8_t> const& buffer, int32_t offset, int32_t count) override;
|
||||
virtual void WriteByte(uint8_t value) override;
|
||||
|
||||
public:
|
||||
std::vector<uint8_t> _buffer;
|
||||
|
||||
private:
|
||||
int32_t _position{ 0 };
|
||||
int32_t _origin{ 0 };
|
||||
int64_t _length{ 0 };
|
||||
bool _closed{ false };
|
||||
};
|
||||
|
||||
//A simplified port of the System.IO.FileStream.
|
||||
class FileStream : public Stream {
|
||||
public:
|
||||
FileStream(std::string const& path, FileMode fileMode);
|
||||
FileStream(std::string const& path);
|
||||
|
||||
~FileStream() override {
|
||||
Close();
|
||||
}
|
||||
|
||||
virtual int64_t Length() override;
|
||||
virtual int64_t Position() override;
|
||||
|
||||
virtual void SetLength(int64_t value) override {
|
||||
throw csharp::InvalidOperationException();
|
||||
}
|
||||
|
||||
inline virtual void Close() override {
|
||||
_closed = true;
|
||||
|
||||
if(_fstream.is_open())
|
||||
_fstream.close();
|
||||
}
|
||||
|
||||
virtual int64_t Seek(int64_t offset, SeekOrigin const& origin) override;
|
||||
virtual int32_t Read(uint8_t* buffer, int32_t bufferLength, int32_t offset, int32_t count) override;
|
||||
virtual int32_t Read(std::vector<uint8_t>& buffer, int32_t offset, int32_t count) override;
|
||||
virtual int32_t ReadByte() override;
|
||||
virtual void Write(uint8_t const* buffer, int32_t bufferLength, int32_t offset, int32_t count) override;
|
||||
virtual void Write(std::vector<uint8_t> const& buffer, int32_t offset, int32_t count) override;
|
||||
virtual void WriteByte(uint8_t value) override;
|
||||
|
||||
public:
|
||||
std::fstream _fstream;
|
||||
|
||||
private:
|
||||
std::streampos _filesize{ 0 };
|
||||
bool _closed{ false };
|
||||
bool _truncated{ false };
|
||||
|
||||
int64_t endOfFile() {
|
||||
if (_closed)
|
||||
return 0;
|
||||
|
||||
const auto pos = _fstream.tellg();
|
||||
_fstream.seekg(0, std::ios_base::end);
|
||||
|
||||
const auto end = _fstream.tellg();
|
||||
_fstream.seekg(pos);
|
||||
|
||||
return static_cast<int64_t>(end);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -17,10 +17,8 @@
|
||||
#include "content/readers/default.hpp"
|
||||
#include "content/readers/graphics.hpp"
|
||||
#include "content/typereadermanager.hpp"
|
||||
#include "csharp/binary.hpp"
|
||||
#include "csharp/buffer.hpp"
|
||||
#include "csharp/service.hpp"
|
||||
#include "csharp/stream.hpp"
|
||||
#include "csharp/timespan.hpp"
|
||||
#include "csharp/type.hpp"
|
||||
#include "csharp/screen.hpp"
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef XNA_GRAPHICS_TEXTURE_HPP
|
||||
#define XNA_GRAPHICS_TEXTURE_HPP
|
||||
|
||||
#include "../csharp/stream.hpp"
|
||||
#include "csharp/io/stream.hpp"
|
||||
#include "gresource.hpp"
|
||||
#include "shared.hpp"
|
||||
#include <cstdint>
|
||||
@ -51,7 +51,7 @@ namespace xna {
|
||||
void SetData(int32_t level, Rectangle* rect, std::vector<uint8_t> const& data, size_t startIndex, size_t elementCount);
|
||||
|
||||
//Loads texture data from a stream.
|
||||
static std::shared_ptr<Texture2D> FromStream(GraphicsDevice& device, std::shared_ptr<Stream> const& stream);
|
||||
static std::shared_ptr<Texture2D> FromStream(GraphicsDevice& device, csharp::Stream& stream);
|
||||
//Loads texture data from a file.
|
||||
static std::shared_ptr<Texture2D> FromStream(GraphicsDevice& device, std::string const& fileName);
|
||||
//Loads texture data from a data.
|
||||
|
@ -28,7 +28,7 @@ namespace xna {
|
||||
insertRegisteredReader<BooleanReader>("BooleanReader");
|
||||
insertRegisteredReader<ByteReader>("ByteReader");
|
||||
//insertRegisteredReader<CharReader>("CharReader");
|
||||
insertRegisteredReader<CharReader>("CharReader", "Microsoft.Xna.Framework.Content.CharReader`1[[System.Char");
|
||||
insertRegisteredReader<CharReader>("CharReader", "Microsoft.Xna.Framework.Content.CharReader");
|
||||
insertRegisteredReader<ColorReader>("ColorReader");
|
||||
insertRegisteredReader<DoubleReader>("DoubleReader");
|
||||
insertRegisteredReader<Int16Reader>("Int16Reader");
|
||||
@ -39,7 +39,7 @@ namespace xna {
|
||||
insertRegisteredReader<QuaternionReader>("QuaternionReader");
|
||||
|
||||
//insertRegisteredReader<RectangleReader>("RectangleReader");
|
||||
insertRegisteredReader<RectangleReader>("RectangleReader", "Microsoft.Xna.Framework.Content.RectangleReadericrosoft.Xna.Framework.Rectangle");
|
||||
insertRegisteredReader<RectangleReader>("RectangleReader", "Microsoft.Xna.Framework.Content.RectangleReader");
|
||||
|
||||
insertRegisteredReader<SByteReader>("SByteReader");
|
||||
insertRegisteredReader<SingleReader>("SingleReader");
|
||||
@ -49,7 +49,7 @@ namespace xna {
|
||||
insertRegisteredReader<UInt64Reader>("UInt64Reader");
|
||||
insertRegisteredReader<Vector2Reader>("Vector2Reader");
|
||||
//insertRegisteredReader<Vector3Reader>("Vector3Reader");
|
||||
insertRegisteredReader<Vector3Reader>("Vector3Reader", "Microsoft.Xna.Framework.Content.Vector3Reader[Microsoft.Xna.Framework.Vector3");
|
||||
insertRegisteredReader<Vector3Reader>("Vector3Reader", "Microsoft.Xna.Framework.Content.Vector3Reader");
|
||||
insertRegisteredReader<Vector4Reader>("Vector4Reader");
|
||||
insertRegisteredReader<Texture2DReader>("Texture2DReader");
|
||||
insertRegisteredReader<SoundEffectReader>("SoundEffectReader");
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "xna-dx/framework.hpp"
|
||||
#include "xna/csharp/stream.hpp"
|
||||
#include "csharp/io/stream.hpp"
|
||||
|
||||
using DxSoundEffect = DirectX::SoundEffect;
|
||||
|
||||
@ -35,7 +35,7 @@ namespace xna {
|
||||
return;
|
||||
|
||||
//We expect 'format' to always be 16 bytes
|
||||
MemoryStream stream(format);
|
||||
csharp::MemoryStream stream(format);
|
||||
WORD word = 0;
|
||||
DWORD dword = 0;
|
||||
|
||||
|
@ -217,16 +217,16 @@ namespace xna {
|
||||
throw csharp::InvalidOperationException();
|
||||
}
|
||||
|
||||
P_Texture2D Texture2D::FromStream(GraphicsDevice& device, P_Stream const& stream)
|
||||
P_Texture2D Texture2D::FromStream(GraphicsDevice& device, csharp::Stream& stream)
|
||||
{
|
||||
std::vector<Byte> data;
|
||||
const auto lenght = stream->Length();
|
||||
stream->Read(data, 0, lenght - 1);
|
||||
const auto lenght = stream.Length();
|
||||
stream.Read(data.data(), data.size(), 0, lenght - 1);
|
||||
|
||||
return FromStream(device, data);
|
||||
}
|
||||
|
||||
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, String const& fileName)
|
||||
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, std::string const& fileName)
|
||||
{
|
||||
auto _this = device.shared_from_this();
|
||||
auto texture2d = snew<Texture2D>(_this);
|
||||
|
@ -4,8 +4,6 @@
|
||||
|
||||
# Add source to this project's executable.
|
||||
add_library (Xn65 STATIC
|
||||
"csharp/stream.cpp"
|
||||
"csharp/binary.cpp"
|
||||
"game/component.cpp"
|
||||
"game/servicecontainer.cpp"
|
||||
"content/manager.cpp"
|
||||
|
@ -1,10 +1,10 @@
|
||||
#include "xna/content/manager.hpp"
|
||||
|
||||
namespace xna {
|
||||
sptr<Stream> ContentManager::OpenStream(String const& assetName) const {
|
||||
const String filePath = rootDirectory + "\\" + assetName + contentExtension;
|
||||
const auto stream = snew<FileStream>(filePath, FileMode::Open);
|
||||
std::shared_ptr<csharp::Stream> ContentManager::OpenStream(std::string const& assetName) const {
|
||||
const auto filePath = rootDirectory + "\\" + assetName + contentExtension;
|
||||
const auto stream = snew<csharp::FileStream>(filePath, csharp::FileMode::Open);
|
||||
|
||||
return reinterpret_pointer_cast<Stream>(stream);
|
||||
return reinterpret_pointer_cast<csharp::Stream>(stream);
|
||||
}
|
||||
}
|
@ -3,14 +3,14 @@
|
||||
#include "xna/content/typereadermanager.hpp"
|
||||
|
||||
namespace xna {
|
||||
sptr<ContentReader> ContentReader::Create(sptr<xna::ContentManager> const& contentManager, sptr<Stream>& input, String const& assetName)
|
||||
std::shared_ptr<ContentReader> ContentReader::Create(std::shared_ptr<xna::ContentManager> const& contentManager, std::shared_ptr<csharp::Stream>& input, String const& assetName)
|
||||
{
|
||||
Int graphicsProfile = 0;
|
||||
input = ContentReader::PrepareStream(input, assetName, graphicsProfile);
|
||||
return std::shared_ptr<ContentReader>(new ContentReader(contentManager, input, assetName, graphicsProfile));
|
||||
}
|
||||
|
||||
sptr<ContentManager> ContentReader::ContentManager() const {
|
||||
std::shared_ptr<ContentManager> ContentReader::ContentManager() const {
|
||||
return _contentManager;
|
||||
}
|
||||
|
||||
@ -101,7 +101,7 @@ namespace xna {
|
||||
Int num = 0;
|
||||
for (size_t index = 0; index < size; index += num)
|
||||
{
|
||||
num = Read(byteBuffer, index, size - index);
|
||||
num = Read(byteBuffer.data(), byteBuffer.size(), index, size - index);
|
||||
if (num == 0) {
|
||||
throw std::runtime_error("ContentReader::ReadByteBuffer: Bad xbn.");
|
||||
}
|
||||
@ -110,7 +110,7 @@ namespace xna {
|
||||
return byteBuffer;
|
||||
}
|
||||
|
||||
sptr<Stream> ContentReader::PrepareStream(sptr<Stream>& input, String const& assetName, Int& graphicsProfile)
|
||||
std::shared_ptr<csharp::Stream> ContentReader::PrepareStream(std::shared_ptr<csharp::Stream>& input, String const& assetName, Int& graphicsProfile)
|
||||
{
|
||||
BinaryReader binaryReader = BinaryReader(input);
|
||||
|
||||
|
@ -115,7 +115,10 @@ namespace xna {
|
||||
type = Type::NameOfRegisteredTypes[readerTypeName];
|
||||
|
||||
if (!type) {
|
||||
throw std::runtime_error("ContentTypeReaderManager::InstantiateTypeReader: registered type is null.");
|
||||
|
||||
std::string error("ContentTypeReaderManager::InstantiateTypeReader: registered type is null. ");
|
||||
error.append("TypeName: " + readerTypeName);
|
||||
throw std::runtime_error(error);
|
||||
}
|
||||
|
||||
if (ContentTypeReaderManager::readerTypeToReader.contains(type)) {
|
||||
|
@ -1,600 +0,0 @@
|
||||
#include "xna/csharp/binary.hpp"
|
||||
#include "xna/csharp/buffer.hpp"
|
||||
|
||||
namespace xna {
|
||||
BinaryReader::BinaryReader(sptr<Stream> const& input) {
|
||||
if(!input)
|
||||
throw csharp::ArgumentNullException("input");
|
||||
|
||||
stream = input;
|
||||
buffer = std::vector<Byte>(bufferLength);
|
||||
}
|
||||
|
||||
Int BinaryReader::PeekChar()
|
||||
{
|
||||
const auto position = stream->Position();
|
||||
const auto num = Read();
|
||||
|
||||
stream->Seek(position, SeekOrigin::Begin);
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
Int BinaryReader::Read()
|
||||
{
|
||||
const auto result = InternalReadOneChar();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool BinaryReader::ReadBoolean()
|
||||
{
|
||||
FillBuffer(1);
|
||||
return buffer[0] > 0;
|
||||
}
|
||||
|
||||
Byte BinaryReader::ReadByte()
|
||||
{
|
||||
const auto num = stream->ReadByte();
|
||||
|
||||
return static_cast<Byte>(num);
|
||||
}
|
||||
|
||||
Sbyte BinaryReader::ReadSByte()
|
||||
{
|
||||
FillBuffer(1);
|
||||
return static_cast<Sbyte>(buffer[0]);
|
||||
}
|
||||
|
||||
Char BinaryReader::ReadChar()
|
||||
{
|
||||
auto num = Read();
|
||||
|
||||
if (num == -1)
|
||||
return '\0';
|
||||
|
||||
return static_cast<Char>(num);
|
||||
}
|
||||
|
||||
Short BinaryReader::ReadInt16()
|
||||
{
|
||||
FillBuffer(2);
|
||||
|
||||
return static_cast<Short>(
|
||||
static_cast<Int>(buffer[0])
|
||||
| static_cast<Int>(buffer[1]) << 8);
|
||||
}
|
||||
|
||||
Ushort BinaryReader::ReadUInt16()
|
||||
{
|
||||
FillBuffer(2);
|
||||
|
||||
return static_cast<Ushort>(
|
||||
static_cast<Uint>(buffer[0])
|
||||
| static_cast<Uint>(buffer[1]) << 8);
|
||||
}
|
||||
|
||||
Int BinaryReader::ReadInt32()
|
||||
{
|
||||
FillBuffer(4);
|
||||
|
||||
return static_cast<Int>(buffer[0])
|
||||
| static_cast<Int>(buffer[1]) << 8
|
||||
| static_cast<Int>(buffer[2]) << 16
|
||||
| static_cast<Int>(buffer[3]) << 24;
|
||||
}
|
||||
|
||||
Uint BinaryReader::ReadUInt32()
|
||||
{
|
||||
FillBuffer(4);
|
||||
|
||||
return static_cast<Uint>(
|
||||
static_cast<Int>(buffer[0])
|
||||
| static_cast<Int>(buffer[1]) << 8
|
||||
| static_cast<Int>(buffer[2]) << 16
|
||||
| static_cast<Int>(buffer[3]) << 24);
|
||||
}
|
||||
|
||||
Long BinaryReader::ReadInt64()
|
||||
{
|
||||
FillBuffer(8);
|
||||
|
||||
const auto num1 = static_cast<Uint>(
|
||||
static_cast<Int>(buffer[4])
|
||||
| static_cast<Int>(buffer[5]) << 8
|
||||
| static_cast<Int>(buffer[6]) << 16
|
||||
| static_cast<Int>(buffer[7]) << 24);
|
||||
|
||||
const auto num2 = static_cast<Uint>(
|
||||
static_cast<Int>(buffer[0])
|
||||
| static_cast<Int>(buffer[1]) << 8
|
||||
| static_cast<Int>(buffer[2]) << 16
|
||||
| static_cast<Int>(buffer[3]) << 24);
|
||||
|
||||
return static_cast<Long>(num1) << 32 | static_cast<Long>(num2);
|
||||
}
|
||||
|
||||
Ulong BinaryReader::ReadUInt64()
|
||||
{
|
||||
FillBuffer(8);
|
||||
|
||||
const auto num1 = static_cast<Uint>(
|
||||
static_cast<Int>(buffer[4])
|
||||
| static_cast<Int>(buffer[5]) << 8
|
||||
| static_cast<Int>(buffer[6]) << 16
|
||||
| static_cast<Int>(buffer[7]) << 24);
|
||||
|
||||
const auto num2 = static_cast<Uint>(
|
||||
static_cast<Int>(buffer[0])
|
||||
| static_cast<Int>(buffer[1]) << 8
|
||||
| static_cast<Int>(buffer[2]) << 16
|
||||
| static_cast<Int>(buffer[3]) << 24);
|
||||
|
||||
return static_cast<Ulong>(num1) << 32 | static_cast<Ulong>(num2);
|
||||
}
|
||||
|
||||
float BinaryReader::ReadSingle()
|
||||
{
|
||||
FillBuffer(4);
|
||||
|
||||
const auto num = static_cast<Uint>(
|
||||
static_cast<Int>(buffer[0])
|
||||
| static_cast<Int>(buffer[1]) << 8
|
||||
| static_cast<Int>(buffer[2]) << 16
|
||||
| static_cast<Int>(buffer[3]) << 24);
|
||||
|
||||
return *(float*)#
|
||||
}
|
||||
|
||||
double BinaryReader::ReadDouble()
|
||||
{
|
||||
FillBuffer(8);
|
||||
|
||||
const auto num1 = static_cast<Uint>(
|
||||
static_cast<Int>(buffer[4])
|
||||
| static_cast<Int>(buffer[5]) << 8
|
||||
| static_cast<Int>(buffer[6]) << 16
|
||||
| static_cast<Int>(buffer[7]) << 24);
|
||||
|
||||
const auto num2 = static_cast<Uint>(
|
||||
static_cast<Int>(buffer[0])
|
||||
| static_cast<Int>(buffer[1]) << 8
|
||||
| static_cast<Int>(buffer[2]) << 6
|
||||
| static_cast<Int>(buffer[3]) << 24);
|
||||
|
||||
const auto num3 = static_cast<Ulong>(num1) << 32 | static_cast<Ulong>(num2);
|
||||
|
||||
return *(double*)&num3;
|
||||
}
|
||||
|
||||
std::string BinaryReader::ReadString()
|
||||
{
|
||||
static const auto empty = std::string();
|
||||
|
||||
Int num = 0;
|
||||
auto val1 = Read7BitEncodedInt();
|
||||
|
||||
if (val1 < 0) {
|
||||
//xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
||||
return empty;
|
||||
}
|
||||
|
||||
if (val1 == 0)
|
||||
return empty;
|
||||
|
||||
if (charBytes.empty())
|
||||
charBytes.resize(maxCharBytesSize);
|
||||
|
||||
if (charBuffer.empty())
|
||||
charBuffer.resize(maxCharBytesSize);
|
||||
|
||||
std::string sb;
|
||||
|
||||
do {
|
||||
const auto byteCount = stream->Read(charBytes, 0, val1 - num > 128 ? 128 : val1 - num);
|
||||
|
||||
if (byteCount == 0) {
|
||||
//xna_error_apply(err, XnaErrorCode::END_OF_FILE);
|
||||
return empty;
|
||||
}
|
||||
|
||||
auto data = reinterpret_cast<char*>(charBytes.data());
|
||||
const auto result = std::string(data);
|
||||
|
||||
if (num == 0 && byteCount == val1) {
|
||||
return result;
|
||||
}
|
||||
|
||||
sb.append(result);
|
||||
num += byteCount;
|
||||
|
||||
} while (num < val1);
|
||||
|
||||
return sb;
|
||||
}
|
||||
|
||||
Int BinaryReader::InternalReadOneChar()
|
||||
{
|
||||
Int num1 = 0;
|
||||
Long num2 = 0;
|
||||
Long num3 = stream->Position();
|
||||
|
||||
if (charBytes.empty())
|
||||
charBytes.resize(128);
|
||||
|
||||
if (singleChar.empty())
|
||||
singleChar.resize(1);
|
||||
|
||||
while (num1 == 0)
|
||||
{
|
||||
//auto byteCount = m2BytesPerChar ? 2 : 1;
|
||||
auto byteCount = 1;
|
||||
const auto num4 = stream->ReadByte();
|
||||
|
||||
charBytes[0] = static_cast<Byte>(num4);
|
||||
|
||||
if (num4 == -1)
|
||||
byteCount = 0;
|
||||
|
||||
if (byteCount == 2) {
|
||||
auto num5 = stream->ReadByte();
|
||||
charBytes[1] = static_cast<Byte>(num5);
|
||||
|
||||
if (num5 == -1)
|
||||
byteCount = 1;
|
||||
}
|
||||
|
||||
if (byteCount == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto data = reinterpret_cast<char*>(charBytes.data());
|
||||
const auto result = std::string(data, data + byteCount);
|
||||
|
||||
if (!result.empty())
|
||||
{
|
||||
num1 = static_cast<Int>(result.size());
|
||||
singleChar[0] = result[0];
|
||||
}
|
||||
}
|
||||
|
||||
return num1 == 0 ? -1 : static_cast<Int>(singleChar[0]);
|
||||
}
|
||||
|
||||
void BinaryReader::FillBuffer(Int numBytes)
|
||||
{
|
||||
if (numBytes < 0 || numBytes > buffer.size()) {
|
||||
throw csharp::InvalidOperationException();
|
||||
}
|
||||
|
||||
Int bytesRead = 0;
|
||||
Int n = 0;
|
||||
|
||||
if (numBytes == 1) {
|
||||
n = stream->ReadByte();
|
||||
|
||||
if (n == -1){
|
||||
throw csharp::InvalidOperationException();
|
||||
}
|
||||
|
||||
buffer[0] = static_cast<Byte>(n);
|
||||
return;
|
||||
}
|
||||
|
||||
do {
|
||||
n = stream->Read(buffer, bytesRead, numBytes - bytesRead);
|
||||
|
||||
if (n == 0) {
|
||||
throw csharp::InvalidOperationException();
|
||||
}
|
||||
|
||||
bytesRead += n;
|
||||
} while (bytesRead < numBytes);
|
||||
}
|
||||
|
||||
Int BinaryReader::InternalReadChars(Char* buffer, size_t bufferSize, size_t index, size_t count)
|
||||
{
|
||||
auto charCount = count;
|
||||
|
||||
if (charBytes.empty())
|
||||
charBytes.resize(128);
|
||||
|
||||
while (charCount > 0) {
|
||||
auto count1 = charCount;
|
||||
|
||||
if (count1 > 1)
|
||||
--count1;
|
||||
|
||||
/*if (m2BytesPerChar)
|
||||
count1 <<= 1;*/
|
||||
|
||||
if (count1 > 128)
|
||||
count1 = 128;
|
||||
|
||||
Int position = 0;
|
||||
Int byteCount;
|
||||
|
||||
std::vector<Byte> numArray;
|
||||
|
||||
byteCount = stream->Read(charBytes, 0, static_cast<Int>(count1));
|
||||
numArray = charBytes;
|
||||
|
||||
if (byteCount == 0)
|
||||
return static_cast<Int>(count - charCount);
|
||||
|
||||
if (position < 0 || byteCount < 0 || (position + byteCount) > numArray.size()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (index < 0 || charCount < 0 || (index + charCount) > bufferSize) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto data = reinterpret_cast<char*>(charBytes.data());
|
||||
auto pChars = reinterpret_cast<char*>(buffer);
|
||||
|
||||
//const auto result = std::string((data + position), (pChars + index) + byteCount);
|
||||
const auto result = std::string((data + position), (data + position) + byteCount);
|
||||
Buffer::BlockCopy(result.c_str(), position, pChars, index, byteCount);
|
||||
|
||||
buffer = reinterpret_cast<Char*>(pChars);
|
||||
|
||||
const auto chars = static_cast<Int>(result.size());
|
||||
|
||||
charCount -= chars;
|
||||
index += chars;
|
||||
}
|
||||
|
||||
return static_cast<Int>(count - charCount);
|
||||
}
|
||||
|
||||
Int BinaryReader::Read7BitEncodedInt()
|
||||
{
|
||||
Uint result = 0;
|
||||
Byte byteReadJustNow;
|
||||
|
||||
constexpr Int MaxBytesWithoutOverflow = 4;
|
||||
|
||||
for (size_t shift = 0; shift < MaxBytesWithoutOverflow * 7; shift += 7)
|
||||
{
|
||||
byteReadJustNow = ReadByte();
|
||||
result |= (byteReadJustNow & 0x7Fu) << shift;
|
||||
|
||||
if (byteReadJustNow <= 0x7Fu)
|
||||
{
|
||||
return static_cast<Int>(result);
|
||||
}
|
||||
}
|
||||
|
||||
byteReadJustNow = ReadByte();
|
||||
|
||||
if (byteReadJustNow > 0b1111u) {
|
||||
throw std::format_error("Too many bytes in what should have been a 7-bit encoded integer.");
|
||||
}
|
||||
|
||||
result |= static_cast<Uint>(byteReadJustNow) << (MaxBytesWithoutOverflow * 7);
|
||||
return static_cast<Int>(result);
|
||||
}
|
||||
|
||||
Long BinaryReader::Read7BitEncodedInt64() {
|
||||
Ulong result = 0;
|
||||
Byte byteReadJustNow;
|
||||
|
||||
constexpr Int MaxBytesWithoutOverflow = 9;
|
||||
|
||||
for (size_t shift = 0; shift < MaxBytesWithoutOverflow * 7; shift += 7)
|
||||
{
|
||||
byteReadJustNow = ReadByte();
|
||||
result |= (static_cast<Ulong>(byteReadJustNow & 0x7Ful)) << shift;
|
||||
|
||||
if (byteReadJustNow <= 0x7Fu) {
|
||||
return static_cast<Long>(result);
|
||||
}
|
||||
}
|
||||
|
||||
byteReadJustNow = ReadByte();
|
||||
|
||||
if (byteReadJustNow > 0b1u)
|
||||
{
|
||||
throw std::format_error("Too many bytes in what should have been a 7-bit encoded integer.");
|
||||
}
|
||||
|
||||
result |= static_cast<Ulong>(byteReadJustNow) << (MaxBytesWithoutOverflow * 7);
|
||||
return static_cast<Long>(result);
|
||||
}
|
||||
|
||||
Int BinaryReader::Read(std::vector<Char>& buffer, size_t index, size_t count)
|
||||
{
|
||||
return InternalReadChars(buffer.data(), buffer.size(), index, count);
|
||||
}
|
||||
|
||||
Int BinaryReader::Read(std::vector<Byte>& buffer, size_t index, size_t count)
|
||||
{
|
||||
auto data = reinterpret_cast<Char*>(buffer.data());
|
||||
return InternalReadChars(data, buffer.size(), index, count);
|
||||
}
|
||||
|
||||
std::vector<Byte> BinaryReader::ReadBytes(size_t count)
|
||||
{
|
||||
std::vector<Byte> result(count);
|
||||
Int numRead = 0;
|
||||
|
||||
do {
|
||||
const auto n = stream->Read(result, static_cast<Int>(numRead), static_cast<Int>(count));
|
||||
|
||||
if (n == 0)
|
||||
break;
|
||||
|
||||
numRead += n;
|
||||
count -= n;
|
||||
} while (count > 0);
|
||||
|
||||
if (numRead != result.size()) {
|
||||
std::vector<Byte> copy(numRead);
|
||||
Buffer::BlockCopy(result.data(), 0, copy.data(), 0, numRead);
|
||||
result = copy;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//Binary Writer
|
||||
|
||||
BinaryWriter::BinaryWriter(sptr<Stream> const& stream) {
|
||||
throw csharp::ArgumentNullException("stream");
|
||||
|
||||
OutStream = stream;
|
||||
_buffer = std::vector<Byte>(16);
|
||||
}
|
||||
|
||||
Long BinaryWriter::Seek(Int offset, SeekOrigin origin)
|
||||
{
|
||||
return OutStream->Seek(offset, origin);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(bool value)
|
||||
{
|
||||
_buffer[0] = value ? static_cast<Byte>(1) : static_cast<Byte>(0);
|
||||
OutStream->Write(_buffer, 0, 1);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Byte value)
|
||||
{
|
||||
OutStream->WriteByte(value);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Sbyte value)
|
||||
{
|
||||
OutStream->WriteByte(static_cast<Byte>(value));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Byte const* buffer, Int bufferLength)
|
||||
{
|
||||
OutStream->Write(buffer, bufferLength, 0, bufferLength);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(std::vector<Byte> const& buffer)
|
||||
{
|
||||
OutStream->Write(buffer, 0, static_cast<Int>(buffer.size()));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Byte const* buffer, Int bufferLength, Int index, Int count)
|
||||
{
|
||||
OutStream->Write(buffer, bufferLength, index, count);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(std::vector<Byte> const& buffer, Int index, Int count)
|
||||
{
|
||||
OutStream->Write(buffer, index, count);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Char ch)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(ch);
|
||||
OutStream->Write(_buffer, 0, 1);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(double value)
|
||||
{
|
||||
Ulong num = (Ulong) * (Long*)&value;
|
||||
_buffer[0] = static_cast<Byte>(num);
|
||||
_buffer[1] = static_cast<Byte>(num >> 8);
|
||||
_buffer[2] = static_cast<Byte>(num >> 16);
|
||||
_buffer[3] = static_cast<Byte>(num >> 24);
|
||||
_buffer[4] = static_cast<Byte>(num >> 32);
|
||||
_buffer[5] = static_cast<Byte>(num >> 40);
|
||||
_buffer[6] = static_cast<Byte>(num >> 48);
|
||||
_buffer[7] = static_cast<Byte>(num >> 56);
|
||||
|
||||
OutStream->Write(_buffer, 0, 8);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Short value)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>((Uint)value >> 8);
|
||||
OutStream->Write(_buffer, 0, 2);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Ushort value)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>((Uint)value >> 8);
|
||||
OutStream->Write(_buffer, 0, 2);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Int value)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>(value >> 8);
|
||||
_buffer[2] = static_cast<Byte>(value >> 16);
|
||||
_buffer[3] = static_cast<Byte>(value >> 24);
|
||||
OutStream->Write(_buffer, 0, 4);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Uint value)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>(value >> 8);
|
||||
_buffer[2] = static_cast<Byte>(value >> 16);
|
||||
_buffer[3] = static_cast<Byte>(value >> 24);
|
||||
OutStream->Write(_buffer, 0, 4);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Ulong value)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>(value >> 8);
|
||||
_buffer[2] = static_cast<Byte>(value >> 16);
|
||||
_buffer[3] = static_cast<Byte>(value >> 24);
|
||||
_buffer[4] = static_cast<Byte>(value >> 32);
|
||||
_buffer[5] = static_cast<Byte>(value >> 40);
|
||||
_buffer[6] = static_cast<Byte>(value >> 48);
|
||||
_buffer[7] = static_cast<Byte>(value >> 56);
|
||||
OutStream->Write(_buffer, 0, 8);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(float value)
|
||||
{
|
||||
Uint num = *(Uint*)&value;
|
||||
_buffer[0] = static_cast<Byte>(num);
|
||||
_buffer[1] = static_cast<Byte>(num >> 8);
|
||||
_buffer[2] = static_cast<Byte>(num >> 16);
|
||||
_buffer[3] = static_cast<Byte>(num >> 24);
|
||||
OutStream->Write(_buffer, 0, 4);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(std::string const& value)
|
||||
{
|
||||
Write(value.c_str(), value.size());
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(const char* _string, size_t stringLength)
|
||||
{
|
||||
Write7BitEncodedInt(static_cast<Int>(stringLength));
|
||||
const auto b = reinterpret_cast<const Byte*>(_string);
|
||||
OutStream->Write(b, static_cast<Int>(stringLength), 0, static_cast<Int>(stringLength));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write7BitEncodedInt(Int value)
|
||||
{
|
||||
Uint num;
|
||||
for (num = static_cast<Uint>(value); num >= static_cast<Uint>(128U); num >>= 7)
|
||||
Write(static_cast<Byte>(num | static_cast<Uint>(128U)));
|
||||
|
||||
Write(static_cast<Byte>(num));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(Long value)
|
||||
{
|
||||
_buffer[0] = static_cast<Byte>(value);
|
||||
_buffer[1] = static_cast<Byte>(value >> 8);
|
||||
_buffer[2] = static_cast<Byte>(value >> 16);
|
||||
_buffer[3] = static_cast<Byte>(value >> 24);
|
||||
_buffer[4] = static_cast<Byte>(value >> 32);
|
||||
_buffer[5] = static_cast<Byte>(value >> 40);
|
||||
_buffer[6] = static_cast<Byte>(value >> 48);
|
||||
_buffer[7] = static_cast<Byte>(value >> 56);
|
||||
OutStream->Write(_buffer, 0, 8);
|
||||
}
|
||||
}
|
@ -1,312 +0,0 @@
|
||||
#include "xna/csharp/stream.hpp"
|
||||
#include "xna/csharp/buffer.hpp"
|
||||
#include "csharp/exception.hpp"
|
||||
#include <filesystem>
|
||||
|
||||
namespace xna {
|
||||
int64_t MemoryStream::Seek(int64_t offset, SeekOrigin const& origin) {
|
||||
int64_t p = 0;
|
||||
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin::Begin:
|
||||
p = _origin + offset;
|
||||
|
||||
if (p < _origin) {
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case SeekOrigin::Current:
|
||||
p = _position + offset;
|
||||
|
||||
if (p < _origin) {
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case SeekOrigin::End:
|
||||
p = _length + offset;
|
||||
|
||||
if (p < _origin) {
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
_position = static_cast<int32_t>(p);
|
||||
return _position;
|
||||
}
|
||||
|
||||
int32_t MemoryStream::Read(uint8_t* buffer, int32_t bufferLength, int32_t offset, int32_t count) {
|
||||
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto off = _length - _position;
|
||||
if (off > count) off = count;
|
||||
|
||||
if (off <= 0 || _closed)
|
||||
return 0;
|
||||
|
||||
if (off <= 8) {
|
||||
auto byteCount = off;
|
||||
while (--byteCount >= 0)
|
||||
buffer[offset + byteCount] = _buffer[_position + static_cast<size_t>(byteCount)];
|
||||
}
|
||||
else {
|
||||
Buffer::BlockCopy<uint8_t>(_buffer.data(), _position, buffer, offset, off);
|
||||
}
|
||||
|
||||
_position += off;
|
||||
|
||||
return off;
|
||||
}
|
||||
|
||||
int32_t MemoryStream::Read(std::vector<uint8_t>& buffer, int32_t offset, int32_t count) {
|
||||
return Read(buffer.data(), static_cast<int32_t>(buffer.size()), offset, count);
|
||||
}
|
||||
|
||||
int32_t MemoryStream::ReadByte() {
|
||||
if (!_closed)
|
||||
return 0;
|
||||
|
||||
if (_position >= _length)
|
||||
return -1;
|
||||
|
||||
return _buffer[_position++];
|
||||
}
|
||||
|
||||
void MemoryStream::Write(uint8_t const* buffer, int32_t bufferLength, int32_t offset, int32_t count){
|
||||
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_closed)
|
||||
return;
|
||||
|
||||
auto i = _position + count;
|
||||
|
||||
if (i < 0 || i > _length) {
|
||||
throw csharp::InvalidOperationException("i < 0 || i > _length");
|
||||
}
|
||||
|
||||
if (count <= 8) {
|
||||
auto byteCount = count;
|
||||
while (--byteCount >= 0)
|
||||
_buffer[_position + static_cast<size_t>(byteCount)] = buffer[offset + byteCount];
|
||||
}
|
||||
else
|
||||
Buffer::BlockCopy(buffer, offset, _buffer.data(), _position, count);
|
||||
|
||||
_position = i;
|
||||
}
|
||||
|
||||
void MemoryStream::Write(std::vector<uint8_t> const& buffer, int32_t offset, int32_t count){
|
||||
Write(buffer.data(), static_cast<int32_t>(buffer.size()), offset, count);
|
||||
}
|
||||
|
||||
void MemoryStream::WriteByte(uint8_t value) {
|
||||
if (_closed)
|
||||
return;
|
||||
|
||||
if (_position >= _length) {
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer[_position++] = value;
|
||||
}
|
||||
|
||||
FileStream::FileStream(std::string const& path, FileMode fileMode) {
|
||||
int flags = std::fstream::in
|
||||
| std::fstream::out
|
||||
| std::fstream::binary;
|
||||
|
||||
const auto exists = std::filesystem::exists(path);
|
||||
|
||||
switch (fileMode)
|
||||
{
|
||||
//Especifica se deve abrir um arquivo existente.
|
||||
case FileMode::Open:
|
||||
if (!exists)
|
||||
throw csharp::InvalidOperationException("The specified file does not exist.");
|
||||
break;
|
||||
//Especifica que se deve abrir um arquivo, se existir;
|
||||
// caso contrário, um novo arquivo deverá ser criado.
|
||||
case FileMode::OpenOrCreate:
|
||||
case FileMode::Create:
|
||||
if (!exists)
|
||||
flags |= std::fstream::trunc;
|
||||
break;
|
||||
//Especifica que o sistema operacional deve criar um novo arquivo.
|
||||
//Se o arquivo já existir, não abre o arquivo.
|
||||
case FileMode::CreateNew:
|
||||
if (!exists)
|
||||
flags |= std::fstream::trunc;
|
||||
else
|
||||
throw csharp::InvalidOperationException("The specified file already exists.");
|
||||
break;
|
||||
//Abre o arquivo, se existir, e busca o final do arquivo ou cria um novo arquivo.
|
||||
case FileMode::Append:
|
||||
if (!exists)
|
||||
flags |= std::fstream::trunc;
|
||||
else
|
||||
flags |= std::fstream::app;
|
||||
break;
|
||||
//Especifica que se deve abrir um arquivo existente.
|
||||
//Quando o arquivo for aberto, ele deverá ser truncado
|
||||
//para que seu tamanho seja zero bytes.
|
||||
//Tentativa de ler um arquivo truncado retornará 0;
|
||||
case FileMode::Truncate:
|
||||
if(!exists)
|
||||
throw csharp::InvalidOperationException("The specified file does not exist.");
|
||||
|
||||
flags |= std::fstream::trunc;
|
||||
_truncated = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
_fstream.open(path.c_str(), flags);
|
||||
|
||||
if (!_fstream.good())
|
||||
throw csharp::InvalidOperationException("Failed to open file: " + path);
|
||||
}
|
||||
|
||||
FileStream::FileStream(std::string const& path) {
|
||||
int flags = std::fstream::in
|
||||
| std::fstream::out
|
||||
| std::fstream::binary;
|
||||
|
||||
const auto exists = std::filesystem::exists(path);
|
||||
|
||||
if (!exists)
|
||||
flags |= std::fstream::trunc;
|
||||
|
||||
_fstream.open(path.c_str(), flags);
|
||||
|
||||
if (!_fstream.good())
|
||||
throw csharp::InvalidOperationException("Failed to open file: " + path);
|
||||
}
|
||||
|
||||
int64_t FileStream::Length() {
|
||||
if (_closed)
|
||||
return 0;
|
||||
|
||||
const auto end = endOfFile();
|
||||
return end;
|
||||
}
|
||||
|
||||
int64_t FileStream::Position() {
|
||||
if (_closed)
|
||||
return 0;
|
||||
|
||||
return static_cast<int64_t>(_fstream.tellg());
|
||||
}
|
||||
|
||||
int64_t FileStream::Seek(int64_t offset, SeekOrigin const& origin){
|
||||
if (_closed)
|
||||
return 0;
|
||||
|
||||
int seek;
|
||||
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin::Begin:
|
||||
seek = std::ios_base::beg;
|
||||
break;
|
||||
case SeekOrigin::Current:
|
||||
seek = std::ios_base::cur;
|
||||
break;
|
||||
case SeekOrigin::End:
|
||||
seek = std::ios_base::end;
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
_fstream.seekg(offset, seek);
|
||||
const auto state = _fstream.rdstate();
|
||||
|
||||
if (state != std::fstream::goodbit) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const auto pos = static_cast<int64_t>(_fstream.tellg());
|
||||
return pos;
|
||||
}
|
||||
|
||||
int32_t FileStream::Read(uint8_t* buffer, int32_t bufferLength, int32_t offset, int32_t count){
|
||||
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (_closed || _truncated)
|
||||
return 0;
|
||||
|
||||
auto _buff = reinterpret_cast<char*>(buffer);
|
||||
_fstream.read(_buff + offset, count);
|
||||
|
||||
if (_fstream.rdstate() != std::fstream::goodbit) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return static_cast<int32_t>(_fstream.gcount());
|
||||
}
|
||||
|
||||
int32_t FileStream::Read(std::vector<uint8_t>& buffer, int32_t offset, int32_t count){
|
||||
return Read(buffer.data(), static_cast<int32_t>(buffer.size()), offset, count);
|
||||
}
|
||||
|
||||
int32_t FileStream::ReadByte(){
|
||||
if (_closed || _truncated)
|
||||
return 0;
|
||||
|
||||
char c = 0;
|
||||
|
||||
_fstream.read(&c, 1);
|
||||
|
||||
if (_fstream.rdstate() != std::fstream::goodbit) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const auto result = static_cast<int32_t>(c);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void FileStream::Write(uint8_t const* buffer, int32_t bufferLength, int32_t offset, int32_t count) {
|
||||
if (buffer == nullptr || offset < 0 || count < 0 || bufferLength - offset < count) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_closed)
|
||||
return;
|
||||
|
||||
auto _buff = reinterpret_cast<const char*>(buffer);
|
||||
|
||||
_fstream.write(_buff + offset, count);
|
||||
|
||||
if (_fstream.rdstate() != std::fstream::goodbit) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void FileStream::Write(std::vector<uint8_t> const& buffer, int32_t offset, int32_t count) {
|
||||
Write(buffer.data(), static_cast<int32_t>(buffer.size()), offset, count);
|
||||
}
|
||||
|
||||
void FileStream::WriteByte(uint8_t value) {
|
||||
if (_closed)
|
||||
return;
|
||||
|
||||
const char c = static_cast<char>(value);
|
||||
|
||||
_fstream.write(&c, 1);
|
||||
|
||||
if (_fstream.rdstate() != std::fstream::goodbit) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user