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

Implementações em Content

This commit is contained in:
Danilo 2024-05-09 09:13:26 -03:00
parent caa9958e9e
commit 676df7e150
6 changed files with 87 additions and 6 deletions

View File

@ -82,6 +82,7 @@ namespace xna {
void Write(Byte const* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) override;
void Write(std::vector<Byte> const& buffer, Int offset, Int count, xna_error_nullarg) override;
void WriteByte(Byte value, xna_error_nullarg) override;
virtual constexpr bool IsClosed() override { return false; }
};
}

View File

@ -44,6 +44,10 @@ namespace xna {
template <typename T>
T ReadAsset(String const& assetName) {
auto input = OpenStream(assetName);
if (input->IsClosed())
return T();
auto contentReader = ContentReader::Create(this, input, assetName);
return contentReader->ReadAsset<T>();
@ -51,7 +55,8 @@ namespace xna {
sptr<Stream> OpenStream(String const& assetName) {
String filePath = _rootDirectory + "\\" + assetName + contentExtension;
const auto stream = New<FileStream>(filePath);
const auto stream = New<FileStream>(filePath, FileMode::Open);
//const auto stream = New<FileStream>(filePath);
return reinterpret_pointer_cast<Stream>(stream);
}

View File

@ -165,7 +165,7 @@ namespace xna {
return -1;
}
if (_closed)
if (_closed || _truncated)
return 0;
auto _buff = reinterpret_cast<char*>(buffer);
@ -184,7 +184,7 @@ namespace xna {
}
Int FileStream::ReadByte(xna_error_ptr_arg){
if (_closed)
if (_closed || _truncated)
return 0;
char c = 0;

View File

@ -14,6 +14,7 @@ namespace xna {
virtual Int Length() = 0;
virtual Long Position() = 0;
virtual void Close() = 0;
virtual bool IsClosed() = 0;
virtual Long Seek(Long offset, SeekOrigin const& origin, xna_error_nullarg) = 0;
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) = 0;
virtual Int Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_nullarg) = 0;
@ -48,6 +49,10 @@ namespace xna {
_buffer = std::vector<Byte>();
}
virtual constexpr bool IsClosed() override {
return _closed;
}
virtual Long Seek(Long offset, SeekOrigin const& origin, xna_error_nullarg) override;
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) override;
virtual Int Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_nullarg) override;
@ -66,7 +71,63 @@ namespace xna {
class FileStream : public Stream {
public:
FileStream(std::string const& path){
FileStream(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) {
_closed = true;
return;
}
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
return;
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:
flags |= std::fstream::trunc;
_truncated = true;
break;
default:
break;
}
_fstream.open(path.c_str(), flags);
if (!_fstream.good())
_closed = true;
}
FileStream(String const& path){
int flags = std::fstream::in
| std::fstream::out
| std::fstream::binary;
@ -79,7 +140,7 @@ namespace xna {
_fstream.open(path.c_str(), flags);
if (!_fstream.is_open())
if (!_fstream.good())
_closed = true;
}
@ -109,6 +170,10 @@ namespace xna {
_fstream.close();
}
virtual constexpr bool IsClosed() override {
return _closed;
}
virtual Long Seek(Long offset, SeekOrigin const& origin, xna_error_nullarg) override;
virtual Int Read(Byte* buffer, Int bufferLength, Int offset, Int count, xna_error_nullarg) override;
virtual Int Read(std::vector<Byte>& buffer, Int offset, Int count, xna_error_nullarg) override;
@ -123,6 +188,7 @@ namespace xna {
private:
std::streampos _filesize{ 0 };
bool _closed{ false };
bool _truncated{ false };
Int endOfFile() {
if (_closed)

View File

@ -165,6 +165,15 @@ namespace xna {
Stretched = 2
};
enum class FileMode {
CreateNew,
Create,
Append,
Open,
OpenOrCreate,
Truncate
};
enum class FillMode
{
WireFrame,

View File

@ -28,7 +28,7 @@ namespace xna {
void LoadContent() override {
spriteBatch = New<SpriteBatch>(*graphicsDevice);
texture = Content()->Load<PTexture2D>("idlse");
texture = Content()->Load<PTexture2D>("idle");
Game::LoadContent();
}