diff --git a/src/common/Stream.cpp b/src/common/Stream.cpp index d9139e355..01d4c593d 100644 --- a/src/common/Stream.cpp +++ b/src/common/Stream.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2006-2015 LOVE Development Team + * Copyright (c) 2006-2022 LOVE Development Team * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages @@ -20,10 +20,59 @@ // LOVE #include "Stream.h" +#include "Data.h" +#include "data/ByteData.h" +#include "Exception.h" namespace love { love::Type Stream::type("Stream", &Object::type); +Data *Stream::read(int64 size) +{ + int64 max = LOVE_INT64_MAX; + int64 cur = 0; + + if (isSeekable()) + { + max = getSize(); + cur = tell(); + } + + if (cur < 0) + cur = 0; + else if (cur > max) + cur = max; + + if (cur + size > max) + size = max - cur; + + StrongRef dst(new data::ByteData(size, false), Acquire::NORETAIN); + + int64 bytesRead = read(dst->getData(), size); + + if (bytesRead < 0 || (bytesRead == 0 && bytesRead != size)) + throw love::Exception("Could not read from stream."); + + if (bytesRead < size) + dst.set(new data::ByteData(dst->getData(), (size_t) bytesRead), Acquire::NORETAIN); + + dst->retain(); + return dst; +} + +bool Stream::write(Data *src) +{ + return write(src, 0, src->getSize()); +} + +bool Stream::write(Data *src, int64 offset, int64 size) +{ + if (offset < 0 || size < 0 || offset + size > src->getSize()) + throw love::Exception("Offset and size parameters do not fit within the given Data's size."); + + return write((const uint8 *) src->getData() + offset, size); +} + } // love diff --git a/src/common/Stream.h b/src/common/Stream.h index 2b69144e3..6d02dcd1a 100644 --- a/src/common/Stream.h +++ b/src/common/Stream.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2006-2015 LOVE Development Team + * Copyright (c) 2006-2022 LOVE Development Team * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages @@ -24,41 +24,92 @@ // LOVE #include #include "Object.h" +#include "int.h" namespace love { +class Data; + class Stream : public Object { public: + + enum SeekOrigin + { + SEEKORIGIN_BEGIN, + SEEKORIGIN_CURRENT, + SEEKORIGIN_END, + SEEKORIGIN_MAX_ENUM + }; + static love::Type type; virtual ~Stream() {} - // getData and getSize are assumed to talk about - // the buffer + /** + * Creates a new copy of the Stream, with the same settings as the original. + * The seek position will be reset in the copy. + **/ + virtual Stream *clone() = 0; /** - * A callback, gets called when some Stream consumer exhausts the data + * Gets whether read() is supported for this Stream. **/ - virtual void fillBackBuffer() {} + virtual bool isReadable() const = 0; /** - * Get the front buffer, Streams are supposed to be (at least) double-buffered + * Gets whether write() is supported for this Stream. **/ - virtual const void *getFrontBuffer() const = 0; + virtual bool isWritable() const = 0; /** - * Get the size of any (and in particular the front) buffer + * Gets whether seek(), tell(), and getSize() are supported for this Stream. **/ - virtual size_t getSize() const = 0; + virtual bool isSeekable() const = 0; /** - * Swap buffers. Returns true if there is new data in the front buffer, - * false otherwise. - * NOTE: If there is no back buffer ready, this call must be ignored + * Reads data into the destination buffer, and returns the number of bytes + * actually read. **/ - virtual bool swapBuffers() = 0; + virtual int64 read(void *dst, int64 size) = 0; + + /** + * Reads data into a new Data object. + **/ + virtual Data *read(int64 size); + + /** + * Writes data from the source buffer into the Stream. + **/ + virtual bool write(const void *src, int64 size) = 0; + + /** + * Writes data from the source Data object into the Stream. + **/ + virtual bool write(Data *src, int64 offset, int64 size); + bool write(Data *src); + + /** + * Flushes all data written to the Stream. + **/ + virtual bool flush() = 0; + + /** + * Gets the total size of the Stream, if supported. + **/ + virtual int64 getSize() = 0; + + /** + * Sets the current position in the Stream, if supported. + **/ + virtual bool seek(int64 pos, SeekOrigin origin = SEEKORIGIN_BEGIN) = 0; + + /** + * Gets the current position in the Stream, if supported. + **/ + virtual int64 tell() = 0; + }; // Stream } // love diff --git a/src/modules/data/ByteData.cpp b/src/modules/data/ByteData.cpp index 734af92c8..045f9f0e1 100644 --- a/src/modules/data/ByteData.cpp +++ b/src/modules/data/ByteData.cpp @@ -31,11 +31,12 @@ namespace data love::Type ByteData::type("ByteData", &Data::type); -ByteData::ByteData(size_t size) +ByteData::ByteData(size_t size, bool clear) : size(size) { create(); - memset(data, 0, size); + if (clear) + memset(data, 0, size); } ByteData::ByteData(const void *d, size_t size) diff --git a/src/modules/data/ByteData.h b/src/modules/data/ByteData.h index befbe079e..2e101df34 100644 --- a/src/modules/data/ByteData.h +++ b/src/modules/data/ByteData.h @@ -35,7 +35,7 @@ public: static love::Type type; - ByteData(size_t size); + ByteData(size_t size, bool clear = true); ByteData(const void *d, size_t size); ByteData(void *d, size_t size, bool own); ByteData(const ByteData &d); diff --git a/src/modules/video/Video.h b/src/modules/video/Video.h index 3de49f0d8..11d158980 100644 --- a/src/modules/video/Video.h +++ b/src/modules/video/Video.h @@ -23,7 +23,6 @@ // LOVE #include "common/Module.h" -#include "common/Stream.h" #include "filesystem/File.h" #include "VideoStream.h" diff --git a/src/modules/video/VideoStream.cpp b/src/modules/video/VideoStream.cpp index 2edb8707c..bb2fae705 100644 --- a/src/modules/video/VideoStream.cpp +++ b/src/modules/video/VideoStream.cpp @@ -27,7 +27,7 @@ namespace love namespace video { -love::Type VideoStream::type("VideoStream", &Stream::type); +love::Type VideoStream::type("VideoStream", &Object::type); void VideoStream::setSync(VideoStream::FrameSync *frameSync) { diff --git a/src/modules/video/VideoStream.h b/src/modules/video/VideoStream.h index fe507a6d0..10d1fa3ae 100644 --- a/src/modules/video/VideoStream.h +++ b/src/modules/video/VideoStream.h @@ -22,7 +22,7 @@ #define LOVE_VIDEO_VIDEOSTREAM_H // LOVE -#include "common/Stream.h" +#include "common/Object.h" #include "audio/Source.h" #include "thread/threads.h" @@ -31,7 +31,7 @@ namespace love namespace video { -class VideoStream : public Stream +class VideoStream : public love::Object { public: @@ -39,6 +39,28 @@ public: virtual ~VideoStream() {} + /** + * A callback, gets called when some Stream consumer exhausts the data + **/ + virtual void fillBackBuffer() {} + + /** + * Get the front buffer, Streams are supposed to be (at least) double-buffered + **/ + virtual const void* getFrontBuffer() const = 0; + + /** + * Get the size of any (and in particular the front) buffer + **/ + virtual size_t getSize() const = 0; + + /** + * Swap buffers. Returns true if there is new data in the front buffer, + * false otherwise. + * NOTE: If there is no back buffer ready, this call must be ignored + **/ + virtual bool swapBuffers() = 0; + virtual int getWidth() const = 0; virtual int getHeight() const = 0; virtual const std::string &getFilename() const = 0;