CommonLibVR
NiBinaryStream.h
Go to the documentation of this file.
1 #pragma once
2 
3 namespace RE
4 {
6  {
7  public:
8  inline static constexpr auto RTTI = RTTI_NiBinaryStream;
9  inline static constexpr auto VTABLE = VTABLE_NiBinaryStream;
10 
11  using int_type = std::int32_t;
12  using pos_type = std::uint32_t;
13  using off_type = std::int32_t;
14 
15  struct BufferInfo
16  {
17  public:
18  // members
19  void* buffer; // 00
20  std::uint32_t totalSize; // 08
21  std::uint32_t bufferAllocSize; // 0C
22  std::uint32_t bufferReadSize; // 10
23  std::uint32_t bufferPos; // 14
24  std::uint32_t streamPos; // 18
25  std::uint32_t pad1C; // 1C
26  };
27  static_assert(sizeof(BufferInfo) == 0x20);
28 
30  virtual ~NiBinaryStream(); // 00
31 
32  // add
33  [[nodiscard]] virtual bool good() const = 0; // 01
34  virtual void seek(std::int32_t a_numBytes) = 0; // 02
35  [[nodiscard]] virtual std::uint32_t tell() const; // 03 - { return _absoluteCurrentPos; }
36  virtual void get_info(BufferInfo& a_buf); // 04
37  virtual void set_endian_swap(bool a_doSwap) = 0; // 05
38 
39  template <class CharT>
40  bool get(CharT& a_ch);
41  template <class CharT>
42  bool read(CharT* a_str, std::uint32_t a_count);
43  template <class CharT>
44  bool put(CharT a_ch);
45  template <class CharT>
46  bool write(const CharT* a_str, std::uint32_t a_count);
47 
48  protected:
49  using ReadFn = std::uint32_t(NiBinaryStream* a_this, void* a_buffer, std::uint32_t a_bytes, std::uint32_t* a_componentSizes, std::uint32_t a_numComponents);
50  using WriteFn = std::uint32_t(NiBinaryStream* a_this, const void* a_buffer, std::uint32_t a_bytes, std::uint32_t* a_componentSizes, std::uint32_t a_numComponents);
51 
52  std::uint32_t binary_read(void* a_buffer, std::uint32_t a_totalBytes, std::uint32_t* a_componentSizes, std::uint32_t a_numComponents = 1);
53  std::uint32_t binary_write(const void* a_buffer, std::uint32_t a_totalBytes, std::uint32_t* a_componentSizes, std::uint32_t a_numComponents = 1);
54 
55  // members
56  std::uint32_t _absoluteCurrentPos; // 08
57  std::uint32_t _pad0C; // 0C
58  ReadFn* _readFn; // 10
59  WriteFn* _writeFn; // 18
60  private:
61  KEEP_FOR_RE()
62  };
63  static_assert(sizeof(NiBinaryStream) == 0x20);
64 
65  template <class CharT>
66  inline bool NiBinaryStream::get(CharT& a_ch)
67  {
68  return read(&a_ch, 1);
69  }
70 
71  template <class CharT>
72  inline bool NiBinaryStream::read(CharT* a_str, std::uint32_t a_count)
73  {
74  std::uint32_t size = sizeof(CharT);
75  std::uint32_t toRead = a_count * size;
76  std::uint32_t bytesRead = binary_read(a_str, toRead, &size);
77  return bytesRead == toRead;
78  }
79 
80  template <class CharT>
81  inline bool NiBinaryStream::put(CharT a_ch)
82  {
83  return write(&a_ch, 1);
84  }
85 
86  template <class CharT>
87  inline bool NiBinaryStream::write(const CharT* a_str, std::uint32_t a_count)
88  {
89  std::uint32_t size = sizeof(CharT);
90  std::uint32_t toWrite = a_count * size;
91  std::uint32_t bytesWritten = binary_write(a_str, toWrite, &size);
92  return bytesWritten == toWrite;
93  }
94 }
95 
96 namespace std
97 {
98  template <class CharT, class Traits, class Allocator>
99  inline bool getline(RE::NiBinaryStream& a_input, std::basic_string<CharT, Traits, Allocator>& a_str)
100  {
101  CharT delim = std::use_facet<std::ctype<CharT>>(std::locale()).widen('\n');
102  return getline(a_input, a_str, delim);
103  }
104 
105  template <class CharT, class Traits, class Allocator>
106  inline bool getline(RE::NiBinaryStream& a_input, std::basic_string<CharT, Traits, Allocator>& a_str, CharT a_delim)
107  {
108  a_str.erase();
109  CharT c;
110  auto notEOF = a_input.get(c);
111  if (notEOF) {
112  do {
113  if (!std::char_traits<CharT>::eq(c, a_delim)) {
114  a_str.push_back(c);
115  } else {
116  break;
117  }
118 
119  if (a_str.size() != a_str.max_size()) {
120  notEOF = a_input.get(c);
121  } else {
122  break;
123  }
124  } while (notEOF);
125  return true;
126  } else {
127  return false;
128  }
129  }
130 }
#define KEEP_FOR_RE()
Definition: PCH.h:713
Definition: NiBinaryStream.h:6
bool get(CharT &a_ch)
Definition: NiBinaryStream.h:66
std::uint32_t(NiBinaryStream *a_this, void *a_buffer, std::uint32_t a_bytes, std::uint32_t *a_componentSizes, std::uint32_t a_numComponents) ReadFn
Definition: NiBinaryStream.h:49
WriteFn * _writeFn
Definition: NiBinaryStream.h:59
static constexpr auto VTABLE
Definition: NiBinaryStream.h:9
virtual void seek(std::int32_t a_numBytes)=0
std::uint32_t(NiBinaryStream *a_this, const void *a_buffer, std::uint32_t a_bytes, std::uint32_t *a_componentSizes, std::uint32_t a_numComponents) WriteFn
Definition: NiBinaryStream.h:50
virtual std::uint32_t tell() const
virtual void set_endian_swap(bool a_doSwap)=0
static constexpr auto RTTI
Definition: NiBinaryStream.h:8
std::uint32_t _pad0C
Definition: NiBinaryStream.h:57
std::uint32_t binary_write(const void *a_buffer, std::uint32_t a_totalBytes, std::uint32_t *a_componentSizes, std::uint32_t a_numComponents=1)
bool write(const CharT *a_str, std::uint32_t a_count)
Definition: NiBinaryStream.h:87
bool read(CharT *a_str, std::uint32_t a_count)
Definition: NiBinaryStream.h:72
virtual void get_info(BufferInfo &a_buf)
std::int32_t off_type
Definition: NiBinaryStream.h:13
virtual ~NiBinaryStream()
virtual bool good() const =0
ReadFn * _readFn
Definition: NiBinaryStream.h:58
std::uint32_t _absoluteCurrentPos
Definition: NiBinaryStream.h:56
bool put(CharT a_ch)
Definition: NiBinaryStream.h:81
std::int32_t int_type
Definition: NiBinaryStream.h:11
std::uint32_t binary_read(void *a_buffer, std::uint32_t a_totalBytes, std::uint32_t *a_componentSizes, std::uint32_t a_numComponents=1)
std::uint32_t pos_type
Definition: NiBinaryStream.h:12
Definition: AbsorbEffect.h:6
constexpr REL::ID RTTI_NiBinaryStream(static_cast< std::uint64_t >(685333))
constexpr std::array< REL::ID, 1 > VTABLE_NiBinaryStream
Definition: Offsets_VTABLE.h:12165
Definition: EffectArchetypes.h:65
bool getline(RE::NiBinaryStream &a_input, std::basic_string< CharT, Traits, Allocator > &a_str)
Definition: NiBinaryStream.h:99
Definition: NiBinaryStream.h:16
std::uint32_t streamPos
Definition: NiBinaryStream.h:24
std::uint32_t bufferPos
Definition: NiBinaryStream.h:23
std::uint32_t bufferAllocSize
Definition: NiBinaryStream.h:21
std::uint32_t totalSize
Definition: NiBinaryStream.h:20
void * buffer
Definition: NiBinaryStream.h:19
std::uint32_t pad1C
Definition: NiBinaryStream.h:25
std::uint32_t bufferReadSize
Definition: NiBinaryStream.h:22