V-Gears 0
Free Final Fantasy VII engine.
VGearsLZSDataStream.h
Go to the documentation of this file.
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
16#include <OgreDataStream.h>
17#include "common/TypeDefine.h"
18
19namespace VGears{
20
24 template<size_t buffer_size> class RingBuffer{
25
26 public:
27
32
38 void Write(const void *in_buffer){
39 buffer_[position_] = *(static_cast<const uint8*>(in_buffer));
40 ++ position_ %= buffer_size;
41 ++ available_ = std::min(available_, buffer_size);
42 }
43
50 size_t Read(void *out_buffer, size_t count){
51 assert(available_ && "Can't read if no data is available");
52 size_t read(0);
53 uint8 *buffer(static_cast<uint8*>(out_buffer));
54 while (read < count && available_){
55 *(buffer ++)
56 = buffer_[(position_ - available_) % buffer_size];
57 -- available_;
58 ++ read;
59 }
60 return read;
61 }
62
70 uint8 Get(const size_t offset){
71 return buffer_[offset % buffer_size];
72 }
73
79 size_t Available() const{return available_;}
80
81 void clear(){
82 position_ = 0;
83 available_ = 0;
84 memset(buffer_, 0, buffer_size);
85 }
86
87 protected:
88
92 uint8 buffer_[buffer_size];
93
97 size_t position_;
98
103
104 };
105
109 class LZSDataStream : public Ogre::DataStream{
110
111 public:
112
118 LZSDataStream(const Ogre::DataStreamPtr &compressed_stream);
119
127 const String &name, const Ogre::DataStreamPtr &compressed_stream
128 );
129
133 virtual ~LZSDataStream();
134
145 virtual size_t read(void *buf, size_t count) override;
146
152 virtual bool eof() const override;
153
161 virtual void skip(long count) override;
162
170 virtual void seek(size_t pos) override;
171
177 virtual size_t tell() const override;
178
182 virtual void close() override;
183
190
196 size_t AvailableUncompressed() const{return buffer_.Available();}
197
206 void FlipEndian(uint32 &inout_data);
207
208 protected:
209
213 virtual void init();
214
220 virtual void DecompressChunk();
221
222 Ogre::DataStreamPtr compressed_stream_;
224 size_t position_;
226 };
227
228 typedef Ogre::SharedPtr<LZSDataStream> LZSDataStreamPtr;
229}
Handles LZS compressed data streams.
Definition: VGearsLZSDataStream.h:109
virtual void DecompressChunk()
Decompresses the stream.
Definition: VGearsLZSDataStream.cpp:76
size_t AvailableCompressed() const
Checks how many data still remains compresses.
Definition: VGearsLZSDataStream.h:189
virtual void close() override
Closes the stream.
Definition: VGearsLZSDataStream.cpp:147
Ogre::DataStreamPtr compressed_stream_
Definition: VGearsLZSDataStream.h:222
virtual size_t tell() const override
Retrieves the current stream position.
Definition: VGearsLZSDataStream.cpp:145
virtual void skip(long count) override
Unimplemented.
Definition: VGearsLZSDataStream.cpp:125
virtual ~LZSDataStream()
Destructor.
Definition: VGearsLZSDataStream.cpp:37
uint32 available_compressed_
Definition: VGearsLZSDataStream.h:223
void FlipEndian(uint32 &inout_data)
Flips the endian mode of some data.
Definition: VGearsLZSDataStream.cpp:53
virtual size_t read(void *buf, size_t count) override
Reads from the current position of stream into a buffer.
Definition: VGearsLZSDataStream.cpp:61
virtual void seek(size_t pos) override
Unimplemented.
Definition: VGearsLZSDataStream.cpp:133
LZSDataStream(const Ogre::DataStreamPtr &compressed_stream)
Constructor.
Definition: VGearsLZSDataStream.cpp:21
virtual void init()
Initializes the stream and sets instance data.
Definition: VGearsLZSDataStream.cpp:39
virtual bool eof() const override
Indicates if the stream has reached the end.
Definition: VGearsLZSDataStream.cpp:141
size_t position_
Definition: VGearsLZSDataStream.h:224
RingBuffer< 4096 > buffer_
Definition: VGearsLZSDataStream.h:225
size_t AvailableUncompressed() const
Checks how many data still has been uncompressesed.
Definition: VGearsLZSDataStream.h:196
A ring buffer used by LZS streams.
Definition: VGearsLZSDataStream.h:24
size_t available_
Available data to read.
Definition: VGearsLZSDataStream.h:102
uint8 Get(const size_t offset)
Gets data from the buffer at an offset.
Definition: VGearsLZSDataStream.h:70
size_t Available() const
Checks the available data size in the buffer.
Definition: VGearsLZSDataStream.h:79
size_t position_
Current buffer position.
Definition: VGearsLZSDataStream.h:97
void clear()
Definition: VGearsLZSDataStream.h:81
size_t Read(void *out_buffer, size_t count)
Reads data from the buffer and extracts it into other buffer.
Definition: VGearsLZSDataStream.h:50
void Write(const void *in_buffer)
Writes to the buffer from other buffer.
Definition: VGearsLZSDataStream.h:38
uint8 buffer_[buffer_size]
The buffer.
Definition: VGearsLZSDataStream.h:92
RingBuffer()
Constructor.
Definition: VGearsLZSDataStream.h:31
Definition: FF7NameLookup.h:24
Ogre::String String
Definition: TypeDefine.h:37
Ogre::SharedPtr< LZSDataStream > LZSDataStreamPtr
Definition: VGearsLZSDataStream.h:228
Ogre::uint32 uint32
Definition: TypeDefine.h:33
Ogre::uint8 uint8
Definition: TypeDefine.h:31