Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update IAGSStream api, and use base Stream class as its direct implementation #2271

Merged
6 changes: 3 additions & 3 deletions Common/core/assetmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bool SortLibsPriorityLib(const AssetLibInfo *lib1, const AssetLibInfo *lib2)

/* static */ bool AssetManager::IsDataFile(const String &data_file)
{
Stream *in = File::OpenFileCI(data_file, Common::kFile_Open, Common::kFile_Read);
Stream *in = File::OpenFileCI(data_file, Common::kFile_Open, Common::kStream_Read);
if (in)
{
MFLUtil::MFLError err = MFLUtil::TestIsMFL(in, true);
Expand All @@ -67,7 +67,7 @@ bool SortLibsPriorityLib(const AssetLibInfo *lib1, const AssetLibInfo *lib2)

/* static */ AssetError AssetManager::ReadDataFileTOC(const String &data_file, AssetLibInfo &lib)
{
Stream *in = File::OpenFileCI(data_file, Common::kFile_Open, Common::kFile_Read);
Stream *in = File::OpenFileCI(data_file, Common::kFile_Open, Common::kStream_Read);
if (in)
{
MFLUtil::MFLError err = MFLUtil::ReadHeader(lib, in);
Expand Down Expand Up @@ -228,7 +228,7 @@ AssetError AssetManager::RegisterAssetLib(const String &path, AssetLibEx *&out_l
// ...else try open a data library
else
{
Stream *in = File::OpenFileCI(path, Common::kFile_Open, Common::kFile_Read);
Stream *in = File::OpenFileCI(path, Common::kFile_Open, Common::kStream_Read);
if (!in)
return kAssetErrNoLibFile; // can't be opened, return error code

Expand Down
6 changes: 3 additions & 3 deletions Common/gfx/bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Bitmap *CreateBitmapCopy(Bitmap *src, int color_depth)
Bitmap *LoadFromFile(const char *filename)
{
std::unique_ptr<Stream> in (
File::OpenFile(filename, FileOpenMode::kFile_Open, FileWorkMode::kFile_Read));
File::OpenFile(filename, FileOpenMode::kFile_Open, StreamMode::kStream_Read));
if(!in)
return nullptr;

Expand Down Expand Up @@ -1267,13 +1267,13 @@ void SaveBitmap(const String& ext, Stream *out, const Bitmap *bmp, const RGB *pa
bool SaveToFile(Bitmap* bmp, const char *filename, const RGB *pal)
{
std::unique_ptr<Stream> out (
File::OpenFile(filename, FileOpenMode::kFile_CreateAlways, FileWorkMode::kFile_Write));
File::OpenFile(filename, FileOpenMode::kFile_CreateAlways, StreamMode::kStream_Write));
if (!out)
return false;

String ext = Path::GetFileExtension(filename);
SaveBitmap(ext, out.get(), bmp, pal);
return out->HasErrors();
return out->GetError(); // FIXME: should return a result from SaveBitmap --> SaveFmt
}

} // namespace BitmapHelper
Expand Down
34 changes: 17 additions & 17 deletions Common/test/stream_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class FileBasedTest : public ::testing::Test {
TEST_F(FileBasedTest, BufferedStreamRead) {
//-------------------------------------------------------------------------
// Write data into the temp file
FileStream out(DummyFile, kFile_CreateAlways, kFile_Write);
FileStream out(DummyFile, kFile_CreateAlways, kStream_Write);
out.WriteInt32(0);
out.WriteInt32(1);
out.WriteInt32(2);
Expand All @@ -231,7 +231,7 @@ TEST_F(FileBasedTest, BufferedStreamRead) {

//-------------------------------------------------------------------------
// Read data back
BufferedStream in(DummyFile, kFile_Open, kFile_Read);
BufferedStream in(DummyFile, kFile_Open, kStream_Read);
ASSERT_TRUE(in.CanRead());
ASSERT_EQ(in.GetLength(), file_len);
ASSERT_EQ(in.ReadInt32(), 0);
Expand All @@ -255,7 +255,7 @@ TEST_F(FileBasedTest, BufferedStreamRead) {
in.Close();

// Test seeks
BufferedStream in2(DummyFile, kFile_Open, kFile_Read);
BufferedStream in2(DummyFile, kFile_Open, kStream_Read);
ASSERT_TRUE(in2.CanRead());
ASSERT_TRUE(in2.CanSeek());
ASSERT_EQ(in2.GetLength(), file_len);
Expand All @@ -279,7 +279,7 @@ TEST_F(FileBasedTest, BufferedStreamWrite1) {
//-------------------------------------------------------------------------
// Write data
const soff_t file_len = sizeof(int32_t) * 10;
BufferedStream out(DummyFile, kFile_CreateAlways, kFile_Write);
BufferedStream out(DummyFile, kFile_CreateAlways, kStream_Write);
ASSERT_TRUE(out.CanWrite());
out.WriteInt32(0);
out.WriteInt32(1);
Expand All @@ -296,7 +296,7 @@ TEST_F(FileBasedTest, BufferedStreamWrite1) {
out.Close();
//-------------------------------------------------------------------------
// Read data back
FileStream in(DummyFile, kFile_Open, kFile_Read);
FileStream in(DummyFile, kFile_Open, kStream_Read);
ASSERT_TRUE(in.CanRead());
ASSERT_TRUE(in.CanSeek());
ASSERT_EQ(in.GetLength(), file_len);
Expand Down Expand Up @@ -324,7 +324,7 @@ TEST_F(FileBasedTest, BufferedStreamWrite2) {
//-------------------------------------------------------------------------
// Write data
const soff_t file_len = sizeof(int32_t) * 10 + fill_len;
BufferedStream out(DummyFile, kFile_CreateAlways, kFile_Write);
BufferedStream out(DummyFile, kFile_CreateAlways, kStream_Write);
ASSERT_TRUE(out.CanWrite());
out.WriteInt32(0);
out.WriteInt32(1);
Expand All @@ -342,7 +342,7 @@ TEST_F(FileBasedTest, BufferedStreamWrite2) {
out.Close();
//-------------------------------------------------------------------------
// Read data back
FileStream in(DummyFile, kFile_Open, kFile_Read);
FileStream in(DummyFile, kFile_Open, kStream_Read);
ASSERT_TRUE(in.CanRead());
ASSERT_TRUE(in.CanSeek());
ASSERT_EQ(in.GetLength(), file_len);
Expand All @@ -368,7 +368,7 @@ TEST_F(FileBasedTest, BufferedStreamWrite3) {
//-------------------------------------------------------------------------
// Write data
const soff_t file_len = sizeof(int32_t) * 10;
BufferedStream out(DummyFile, kFile_CreateAlways, kFile_Write);
BufferedStream out(DummyFile, kFile_CreateAlways, kStream_Write);
ASSERT_TRUE(out.CanWrite());
ASSERT_TRUE(out.CanSeek());
out.WriteInt32(0);
Expand All @@ -391,7 +391,7 @@ TEST_F(FileBasedTest, BufferedStreamWrite3) {
out.Close();
//-------------------------------------------------------------------------
// Read data back
FileStream in(DummyFile, kFile_Open, kFile_Read);
FileStream in(DummyFile, kFile_Open, kStream_Read);
ASSERT_TRUE(in.CanRead());
ASSERT_EQ(in.GetLength(), file_len);
ASSERT_EQ(in.ReadInt32(), 0);
Expand All @@ -418,7 +418,7 @@ TEST_F(FileBasedTest, BufferedStreamWrite4) {
//-------------------------------------------------------------------------
// Write data
const soff_t file_len = sizeof(int32_t) * 8 + fill_len;
BufferedStream out(DummyFile, kFile_CreateAlways, kFile_Write);
BufferedStream out(DummyFile, kFile_CreateAlways, kStream_Write);
ASSERT_TRUE(out.CanWrite());
out.WriteInt32(0);
out.WriteInt32(1);
Expand All @@ -439,7 +439,7 @@ TEST_F(FileBasedTest, BufferedStreamWrite4) {
out.Close();
//-------------------------------------------------------------------------
// Read data back
FileStream in(DummyFile, kFile_Open, kFile_Read);
FileStream in(DummyFile, kFile_Open, kStream_Read);
ASSERT_TRUE(in.CanRead());
ASSERT_TRUE(in.CanSeek());
ASSERT_EQ(in.GetLength(), file_len);
Expand All @@ -466,7 +466,7 @@ TEST_F(FileBasedTest, BufferedStreamWrite5) {
//-------------------------------------------------------------------------
// Write data
const soff_t file_len = sizeof(int32_t) * 7 + fill_len;
BufferedStream out(DummyFile, kFile_CreateAlways, kFile_Write);
BufferedStream out(DummyFile, kFile_CreateAlways, kStream_Write);
ASSERT_TRUE(out.CanWrite());
out.WriteByteCount(0, fill_len); // fill to (nearly) force buffer flush
out.WriteInt32(0);
Expand All @@ -486,7 +486,7 @@ TEST_F(FileBasedTest, BufferedStreamWrite5) {
out.Close();
//-------------------------------------------------------------------------
// Read data back
FileStream in(DummyFile, kFile_Open, kFile_Read);
FileStream in(DummyFile, kFile_Open, kStream_Read);
ASSERT_TRUE(in.CanRead());
ASSERT_TRUE(in.CanSeek());
ASSERT_EQ(in.GetLength(), file_len);
Expand All @@ -507,7 +507,7 @@ TEST_F(FileBasedTest, BufferedStreamWrite5) {
TEST_F(FileBasedTest, BufferedSectionStream) {
//-------------------------------------------------------------------------
// Write data into the temp file
FileStream out(DummyFile, kFile_CreateAlways, kFile_Write);
FileStream out(DummyFile, kFile_CreateAlways, kStream_Write);
out.WriteInt32(0);
out.WriteInt32(1);
out.WriteInt32(2);
Expand All @@ -530,7 +530,7 @@ TEST_F(FileBasedTest, BufferedSectionStream) {

//-------------------------------------------------------------------------
// Read data back from section 1 and test read limits
BufferedSectionStream in(DummyFile, section1_start, section1_end, kFile_Open, kFile_Read);
BufferedSectionStream in(DummyFile, section1_start, section1_end, kFile_Open, kStream_Read);
ASSERT_TRUE(in.CanRead());
ASSERT_EQ(in.GetPosition(), 0);
ASSERT_EQ(in.GetLength(), section1_end - section1_start);
Expand All @@ -552,7 +552,7 @@ TEST_F(FileBasedTest, BufferedSectionStream) {

// Test limits - reading large chunks: optimized by reading directly
// into the provided user's buffer, without use of internal buffer
BufferedSectionStream in3(DummyFile, section1_start, section1_end, kFile_Open, kFile_Read);
BufferedSectionStream in3(DummyFile, section1_start, section1_end, kFile_Open, kStream_Read);
const size_t try_read = 4 * sizeof(int32_t) + BufferedStream::BufferSize;
const size_t must_read = 4 * sizeof(int32_t);
char buf[try_read];
Expand All @@ -564,7 +564,7 @@ TEST_F(FileBasedTest, BufferedSectionStream) {
in3.Close();

// Test seeks limited to section 1
BufferedSectionStream in2(DummyFile, section1_start, section2_end, kFile_Open, kFile_Read);
BufferedSectionStream in2(DummyFile, section1_start, section2_end, kFile_Open, kStream_Read);
ASSERT_TRUE(in2.CanRead());
ASSERT_TRUE(in2.CanSeek());
ASSERT_EQ(in2.GetPosition(), 0);
Expand Down
27 changes: 5 additions & 22 deletions Common/util/aasset_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,23 @@ namespace AGS
if(_ownHandle) Close();
}

bool AAssetStream::HasErrors() const
{
return !IsValid();
}

void AAssetStream::Close()
{
if(_aAsset) {
AAsset_close(_aAsset);
_aAsset = nullptr;
}
_mode = kStream_None;
}

bool AAssetStream::Flush()
{
return false;
}

bool AAssetStream::IsValid() const
StreamMode AAssetStream::GetMode() const
{
return _aAsset != nullptr;
return _mode;
}

bool AAssetStream::EOS() const
Expand All @@ -100,21 +96,6 @@ namespace AGS
return -1;
}

bool AAssetStream::CanRead() const
{
return IsValid();
}

bool AAssetStream::CanWrite() const
{
return false;
}

bool AAssetStream::CanSeek() const
{
return IsValid();
}

size_t AAssetStream::Read(void *buffer, size_t size)
{
if(!IsValid()) return -1;
Expand Down Expand Up @@ -187,6 +168,8 @@ namespace AGS
_cur_offset = 0;
_start = 0;
_end = AAsset_getLength64(_aAsset);
_assetMode = asset_mode;
_mode = static_cast<StreamMode>(kStream_Read | kStream_Seek);
}


Expand Down
24 changes: 13 additions & 11 deletions Common/util/aasset_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,18 @@ namespace AGS
{ return new AAssetStream(aasset, false, asset_mode, stream_end); }
~AAssetStream() override;

bool HasErrors() const override;
void Close() override;
bool Flush() override;

// Is stream valid (underlying data initialized properly)
bool IsValid() const override;
// Tells the AASSET_MODE this stream is working in.
int GetAssetMode() const { return _assetMode; }
// Tells which mode the stream is working in, which defines
// supported io operations, such as reading, writing, seeking, etc.
// Invalid streams return kStream_None to indicate that they are not functional.
StreamMode GetMode() const override;
// Is end of stream
bool EOS() const override;
// Total length of stream (if known)
soff_t GetLength() const override;
// Current position (if known)
soff_t GetPosition() const override;
bool CanRead() const override;
bool CanWrite() const override;
bool CanSeek() const override;

size_t Read(void *buffer, size_t size) override;
int32_t ReadByte() override;
Expand All @@ -80,6 +77,9 @@ namespace AGS

bool Seek(soff_t offset, StreamSeek origin) override;

bool Flush() override;
void Close() override;

protected:
soff_t _start = 0;
soff_t _end = 0;
Expand All @@ -91,8 +91,10 @@ namespace AGS
void Open(const String &asset_name, int asset_mode);
void OpenSection(const String &asset_name, int asset_mode, soff_t start_pos, soff_t end_pos);

bool _ownHandle;
AAsset *_aAsset = nullptr;
bool _ownHandle = false;
AAsset *_aAsset = nullptr;
int _assetMode = AASSET_MODE_UNKNOWN;
StreamMode _mode = kStream_None;
};

} // namespace Common
Expand Down
8 changes: 4 additions & 4 deletions Common/util/bufferedstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Common
const size_t BufferedStream::BufferSize;

BufferedStream::BufferedStream(const String &file_name, FileOpenMode open_mode,
FileWorkMode work_mode, DataEndianess stream_endianess)
StreamMode work_mode, DataEndianess stream_endianess)
: FileStream(file_name, open_mode, work_mode, stream_endianess)
{
if (FileStream::Seek(0, kSeekEnd))
Expand Down Expand Up @@ -92,14 +92,14 @@ soff_t BufferedStream::GetPosition() const

void BufferedStream::Close()
{
if (GetWorkMode() == kFile_Write)
if (CanWrite())
FlushBuffer(_position);
FileStream::Close();
}

bool BufferedStream::Flush()
{
if (GetWorkMode() == kFile_Write)
if (CanWrite())
FlushBuffer(_position);
return FileStream::Flush();
}
Expand Down Expand Up @@ -203,7 +203,7 @@ bool BufferedStream::Seek(soff_t offset, StreamSeek origin)
//-----------------------------------------------------------------------------

BufferedSectionStream::BufferedSectionStream(const String &file_name, soff_t start_pos, soff_t end_pos,
FileOpenMode open_mode, FileWorkMode work_mode, DataEndianess stream_endianess)
FileOpenMode open_mode, StreamMode work_mode, DataEndianess stream_endianess)
: BufferedStream(file_name, open_mode, work_mode, stream_endianess)
{
assert(start_pos <= end_pos);
Expand Down
4 changes: 2 additions & 2 deletions Common/util/bufferedstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BufferedStream : public FileStream
// - could not determine the length of the stream
// It is recommended to use File::OpenFile to safely construct this object.
BufferedStream(const String &file_name, FileOpenMode open_mode,
FileWorkMode work_mode, DataEndianess stream_endianess = kLittleEndian);
StreamMode work_mode, DataEndianess stream_endianess = kLittleEndian);
~BufferedStream();

// Is end of stream
Expand Down Expand Up @@ -84,7 +84,7 @@ class BufferedSectionStream : public BufferedStream
{
public:
BufferedSectionStream(const String &file_name, soff_t start_pos, soff_t end_pos,
FileOpenMode open_mode, FileWorkMode work_mode, DataEndianess stream_endianess = kLittleEndian);
FileOpenMode open_mode, StreamMode work_mode, DataEndianess stream_endianess = kLittleEndian);
};

} // namespace Common
Expand Down
Loading