CommonLibSSE (powerof3)
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  };
61  static_assert(sizeof(NiBinaryStream) == 0x20);
62 
63  template <class CharT>
64  inline bool NiBinaryStream::get(CharT& a_ch)
65  {
66  return read(&a_ch, 1);
67  }
68 
69  template <class CharT>
70  inline bool NiBinaryStream::read(CharT* a_str, std::uint32_t a_count)
71  {
72  std::uint32_t size = sizeof(CharT);
73  std::uint32_t toRead = a_count * size;
74  std::uint32_t bytesRead = binary_read(a_str, toRead, &size);
75  return bytesRead == toRead;
76  }
77 
78  template <class CharT>
79  inline bool NiBinaryStream::put(CharT a_ch)
80  {
81  return write(&a_ch, 1);
82  }
83 
84  template <class CharT>
85  inline bool NiBinaryStream::write(const CharT* a_str, std::uint32_t a_count)
86  {
87  std::uint32_t size = sizeof(CharT);
88  std::uint32_t toWrite = a_count * size;
89  std::uint32_t bytesWritten = binary_write(a_str, toWrite, &size);
90  return bytesWritten == toWrite;
91  }
92 }
93 
94 namespace std
95 {
96  template <class CharT, class Traits, class Allocator>
97  inline bool getline(RE::NiBinaryStream& a_input, std::basic_string<CharT, Traits, Allocator>& a_str)
98  {
99  CharT delim = std::use_facet<std::ctype<CharT>>(std::locale()).widen('\n');
100  return getline(a_input, a_str, delim);
101  }
102 
103  template <class CharT, class Traits, class Allocator>
104  inline bool getline(RE::NiBinaryStream& a_input, std::basic_string<CharT, Traits, Allocator>& a_str, CharT a_delim)
105  {
106  a_str.erase();
107  CharT c;
108  auto notEOF = a_input.get(c);
109  if (notEOF) {
110  do {
111  if (!std::char_traits<CharT>::eq(c, a_delim)) {
112  a_str.push_back(c);
113  } else {
114  break;
115  }
116 
117  if (a_str.size() != a_str.max_size()) {
118  notEOF = a_input.get(c);
119  } else {
120  break;
121  }
122  } while (notEOF);
123  return true;
124  } else {
125  return false;
126  }
127  }
128 }
Definition: NiBinaryStream.h:6
bool get(CharT &a_ch)
Definition: NiBinaryStream.h:64
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:85
bool read(CharT *a_str, std::uint32_t a_count)
Definition: NiBinaryStream.h:70
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:79
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:97
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