QGearsLZSDataStream.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-08-18 Tobias Peters <tobias.peters@kreativeffekt.at>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. -----------------------------------------------------------------------------
  21. */
  22. #ifndef __QGearsLZSDataStream_H__
  23. #define __QGearsLZSDataStream_H__
  24. #include <OgreDataStream.h>
  25. #include "common/TypeDefine.h"
  26. namespace QGears
  27. {
  28. template<size_t buffer_size>
  29. class RingBuffer
  30. {
  31. public:
  32. RingBuffer()
  33. {
  34. clear();
  35. }
  36. void write( const void *inbuffer_ )
  37. {
  38. mbuffer_[m_pos] = *( static_cast<const uint8*>( inbuffer_ ) );
  39. ++m_pos %= buffer_size;
  40. ++m_avail = std::min( m_avail, buffer_size );
  41. }
  42. size_t read( void *outbuffer_, size_t count )
  43. {
  44. assert( m_avail && "Can't read if no data is available" );
  45. size_t read( 0 );
  46. uint8 *buffer( static_cast<uint8*>(outbuffer_) );
  47. while( read < count && m_avail )
  48. {
  49. *(buffer++) = mbuffer_[(m_pos - m_avail) % buffer_size];
  50. --m_avail;
  51. ++read;
  52. }
  53. return read;
  54. }
  55. uint8 get( const size_t offset )
  56. {
  57. return mbuffer_[offset % buffer_size];
  58. }
  59. size_t avail( void ) const
  60. {
  61. return m_avail;
  62. }
  63. void clear()
  64. {
  65. m_pos = 0;
  66. m_avail = 0;
  67. memset( mbuffer_, 0, buffer_size );
  68. }
  69. protected:
  70. uint8 mbuffer_[buffer_size];
  71. size_t m_pos, m_avail;
  72. };
  73. //-------------------------------------------------------------------------
  74. class LZSDataStream : public Ogre::DataStream
  75. {
  76. public:
  77. LZSDataStream( const Ogre::DataStreamPtr &compressed_stream );
  78. LZSDataStream( const String &name, const Ogre::DataStreamPtr &compressed_stream );
  79. virtual ~LZSDataStream();
  80. virtual size_t read( void *buf, size_t count ) override;
  81. virtual bool eof( void ) const override;
  82. virtual void skip(long count) override;
  83. virtual void seek( size_t pos ) override;
  84. virtual size_t tell(void) const override;
  85. virtual void close(void) override;
  86. size_t availableCompressed( void ) const { return m_available_compressed; }
  87. size_t availableUncompressed( void ) const { return mbuffer_.avail(); }
  88. void flipEndian( uint32 &inout_data );
  89. protected:
  90. virtual void init();
  91. virtual void decompressChunk();
  92. Ogre::DataStreamPtr m_compressed_stream;
  93. uint32 m_available_compressed;
  94. size_t m_pos;
  95. RingBuffer<4096> mbuffer_;
  96. private:
  97. };
  98. typedef Ogre::SharedPtr<LZSDataStream> LZSDataStreamPtr;
  99. }
  100. #endif // __QGearsLZSDataStream_H__