| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /*
- -----------------------------------------------------------------------------
- The MIT License (MIT)
- Copyright (c) 2013-08-18 Tobias Peters <tobias.peters@kreativeffekt.at>
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- -----------------------------------------------------------------------------
- */
- #ifndef __QGearsLZSDataStream_H__
- #define __QGearsLZSDataStream_H__
- #include <OgreDataStream.h>
- #include "common/TypeDefine.h"
- namespace QGears
- {
- template<size_t buffer_size>
- class RingBuffer
- {
- public:
- RingBuffer()
- {
- clear();
- }
- void write( const void *inbuffer_ )
- {
- mbuffer_[m_pos] = *( static_cast<const uint8*>( inbuffer_ ) );
- ++m_pos %= buffer_size;
- ++m_avail = std::min( m_avail, buffer_size );
- }
- size_t read( void *outbuffer_, size_t count )
- {
- assert( m_avail && "Can't read if no data is available" );
- size_t read( 0 );
- uint8 *buffer( static_cast<uint8*>(outbuffer_) );
- while( read < count && m_avail )
- {
- *(buffer++) = mbuffer_[(m_pos - m_avail) % buffer_size];
- --m_avail;
- ++read;
- }
- return read;
- }
- uint8 get( const size_t offset )
- {
- return mbuffer_[offset % buffer_size];
- }
- size_t avail( void ) const
- {
- return m_avail;
- }
- void clear()
- {
- m_pos = 0;
- m_avail = 0;
- memset( mbuffer_, 0, buffer_size );
- }
- protected:
- uint8 mbuffer_[buffer_size];
- size_t m_pos, m_avail;
- };
- //-------------------------------------------------------------------------
- class LZSDataStream : public Ogre::DataStream
- {
- public:
- LZSDataStream( const Ogre::DataStreamPtr &compressed_stream );
- LZSDataStream( const String &name, const Ogre::DataStreamPtr &compressed_stream );
- virtual ~LZSDataStream();
- virtual size_t read( void *buf, size_t count ) override;
- virtual bool eof( void ) const override;
- virtual void skip(long count) override;
- virtual void seek( size_t pos ) override;
- virtual size_t tell(void) const override;
- virtual void close(void) override;
- size_t availableCompressed( void ) const { return m_available_compressed; }
- size_t availableUncompressed( void ) const { return mbuffer_.avail(); }
- void flipEndian( uint32 &inout_data );
- protected:
- virtual void init();
- virtual void decompressChunk();
- Ogre::DataStreamPtr m_compressed_stream;
- uint32 m_available_compressed;
- size_t m_pos;
- RingBuffer<4096> mbuffer_;
- private:
- };
- typedef Ogre::SharedPtr<LZSDataStream> LZSDataStreamPtr;
- }
- #endif // __QGearsLZSDataStream_H__
|