QGearsLZSDataStream.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <OgreDataStream.h>
  16. #include "common/TypeDefine.h"
  17. namespace QGears{
  18. /**
  19. * A ring buffer used by LZS streams.
  20. */
  21. template<size_t buffer_size> class RingBuffer{
  22. public:
  23. /**
  24. * Constructor.
  25. */
  26. RingBuffer(){clear();}
  27. /**
  28. * Writes to the buffer from other buffer.
  29. *
  30. * @param in_buffer[in] The buffer to write from.
  31. */
  32. void Write(const void *in_buffer_){
  33. buffer_[position_] = *(static_cast<const uint8*>(in_buffer_));
  34. ++ position_ %= buffer_size;
  35. ++ available_ = std::min(available_, buffer_size);
  36. }
  37. /**
  38. * Reads data from the buffer and extracts it into other buffer.
  39. *
  40. * @param out_buffer[out] Buffer to copy the data from this buffer.
  41. * @param count[in] Number of bytes to read.
  42. */
  43. size_t Read(void *out_buffer, size_t count){
  44. assert(available_ && "Can't read if no data is available");
  45. size_t read(0);
  46. uint8 *buffer(static_cast<uint8*>(out_buffer));
  47. while (read < count && available_){
  48. *(buffer ++)
  49. = buffer_[(position_ - available_) % buffer_size];
  50. -- available_;
  51. ++ read;
  52. }
  53. return read;
  54. }
  55. /**
  56. * Gets data from the buffer at an offset.
  57. *
  58. * @param offset[in] Offset to the data to read.
  59. * @return The data at the offset.
  60. * @todo Explain the size of the returned data and offset.
  61. */
  62. uint8 Get(const size_t offset){
  63. return buffer_[offset % buffer_size];
  64. }
  65. /**
  66. * Checks the available data size in the buffer.
  67. *
  68. * @return How much data is left to read in the buffer.
  69. */
  70. size_t Available() const{return available_;}
  71. void clear(){
  72. position_ = 0;
  73. available_ = 0;
  74. memset(buffer_, 0, buffer_size);
  75. }
  76. protected:
  77. /**
  78. * The buffer.
  79. */
  80. uint8 buffer_[buffer_size];
  81. /**
  82. * Current buffer position.
  83. */
  84. size_t position_;
  85. /**
  86. * Available data to read.
  87. */
  88. size_t available_;
  89. };
  90. /**
  91. * Handles LZS compressed data streams.
  92. */
  93. class LZSDataStream : public Ogre::DataStream{
  94. public:
  95. /**
  96. * Constructor.
  97. *
  98. * @param compressed_stream[in] The compressed stream.
  99. */
  100. LZSDataStream(const Ogre::DataStreamPtr &compressed_stream);
  101. /**
  102. * Constructor.
  103. *
  104. * @param name[in] NAme for th stream.
  105. * @param compressed_stream[in] The compressed stream.
  106. */
  107. LZSDataStream(
  108. const String &name, const Ogre::DataStreamPtr &compressed_stream
  109. );
  110. /**
  111. * Destructor.
  112. */
  113. virtual ~LZSDataStream();
  114. /**
  115. * Reads from the current position of stream into a buffer.
  116. *
  117. * It also advances the stream cursor.
  118. *
  119. * @param buf[out] Buffer to read to.
  120. * @param count[in] Number of data to read. If the end of the
  121. * stream is reached before reading this much, it will stop
  122. * reading.
  123. */
  124. virtual size_t read(void *buf, size_t count) override;
  125. /**
  126. * Indicates if the stream has reached the end.
  127. *
  128. * @return True if the stream is ended, false otherwise.
  129. */
  130. virtual bool eof() const override;
  131. /**
  132. * Unimplemented.
  133. *
  134. * Not required, can use read instead.
  135. *
  136. * @param count[in] Unused.
  137. */
  138. virtual void skip(long count) override;
  139. /**
  140. * Unimplemented.
  141. *
  142. * Not required.
  143. *
  144. * @param count[in] Unused.
  145. */
  146. virtual void seek(size_t pos) override;
  147. /**
  148. * Retrieves the current stream position.
  149. *
  150. * @return The current stream position.
  151. */
  152. virtual size_t tell() const override;
  153. /**
  154. * Closes the stream.
  155. */
  156. virtual void close() override;
  157. /**
  158. * @todo Understand and document.
  159. *
  160. * @return @todo.
  161. */
  162. size_t AvailableCompressed() const {
  163. return available_compressed_;
  164. }
  165. /**
  166. * @todo Understand and document.
  167. *
  168. * @return @todo.
  169. */
  170. size_t AvailableUncompressed() const{
  171. return buffer_.Available();
  172. }
  173. /**
  174. * Flips the endian mode of some data.
  175. *
  176. * Little endian data will be transformed into big endian, and big
  177. * endian data will be transformed into little endian.
  178. *
  179. * @param inout_data[in|out] The data to flip.
  180. */
  181. void FlipEndian(uint32 &inout_data);
  182. protected:
  183. /**
  184. * Initializes the stream and sets instance data.
  185. */
  186. virtual void init();
  187. /**
  188. * Decompresses the stream.
  189. *
  190. * @todo The whole stream?
  191. */
  192. virtual void DecompressChunk();
  193. Ogre::DataStreamPtr compressed_stream_;
  194. uint32 available_compressed_;
  195. size_t position_;
  196. RingBuffer<4096> buffer_;
  197. };
  198. typedef Ogre::SharedPtr<LZSDataStream> LZSDataStreamPtr;
  199. }