V-Gears 0
Free Final Fantasy VII engine.
BinaryReader.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#pragma once
17
18#include <fstream>
19#include <sstream>
20#include <iterator>
21
23
28
29 public:
30
38 static std::vector<unsigned char> ReadAll(std::string file_name){
39 std::ifstream file(file_name.c_str(), std::ios::binary | std::ios::ate);
40 if (file.is_open()){
41 size_t file_size_in_bytes = size_t(file.tellg());
42 std::vector<unsigned char> file_contents(file_size_in_bytes);
43 file.seekg(0, std::ios::beg);
44 file.read(reinterpret_cast<char*>(file_contents.data()), file_contents.size());
45 return file_contents;
46 }
47 else throw std::runtime_error("Can't open file");
48 }
49
55 BinaryReader(std::vector<unsigned char>&& data){
56 size_ = data.size();
57 std::copy(data.begin(), data.end(), std::ostream_iterator<unsigned char>(stream_));
58 stream_.seekg(std::ios::beg);
59 }
60
66 size_t GetSize() const{return size_;}
67
74 void Seek(unsigned int position){
75 if (!stream_.seekg(position)) throw DecompilerException();
76 }
77
83 unsigned int GetPosition(){return static_cast<unsigned int>(stream_.tellg());}
84
93 unsigned int ReadU32(){return InternalRead<unsigned int>();}
94
103 signed int ReadS32(){return InternalRead<signed int>();}
104
113 signed short int ReadS16(){return InternalRead<signed short int>();}
114
123 unsigned short int ReadU16(){return InternalRead<unsigned short int>();}
124
133 unsigned char ReadU8(){return InternalRead<unsigned char>();}
134
143 signed char ReadS8(){return InternalRead<signed char>();}
144
145 private:
146
155 template<class T> T InternalRead(){
156 T r = {};
157 if (!stream_.read(reinterpret_cast<char*>(&r), sizeof(r)))
158 throw DecompilerException();
159 return r;
160 }
161
165 std::stringstream stream_;
166
170 size_t size_ = 0;
171};
float * position
Used to keep track of the skeleton and it's bones positions.
Definition: DrawSkeleton.cpp:33
uint16 r
Red component.
Definition: TxzFileSerializer.h:4
Reader for binary files.
Definition: BinaryReader.h:27
signed int ReadS32()
Reads 32 bits of data as a signed integer.
Definition: BinaryReader.h:103
signed char ReadS8()
Reads 8 bits of data as a signed integer.
Definition: BinaryReader.h:143
T InternalRead()
Reads T bits of data as a signed integer.
Definition: BinaryReader.h:155
size_t GetSize() const
Retrieves the size of the data in the reader.
Definition: BinaryReader.h:66
BinaryReader(std::vector< unsigned char > &&data)
Constructor.
Definition: BinaryReader.h:55
static std::vector< unsigned char > ReadAll(std::string file_name)
Reads a file, from start to end.
Definition: BinaryReader.h:38
std::stringstream stream_
The data stream.
Definition: BinaryReader.h:165
void Seek(unsigned int position)
Moves the stream cursor.
Definition: BinaryReader.h:74
size_t size_
The size of the data, in bytes.
Definition: BinaryReader.h:170
unsigned int ReadU32()
Reads 32 bits of data as an unsigned integer.
Definition: BinaryReader.h:93
signed short int ReadS16()
Reads 16 bits of data as an unsigned integer.
Definition: BinaryReader.h:113
unsigned char ReadU8()
Reads 8 bits of data as an unsigned integer.
Definition: BinaryReader.h:133
unsigned short int ReadU16()
Reads 16 bits of data as a signed integer.
Definition: BinaryReader.h:123
unsigned int GetPosition()
Retrieves the current stream cursor position.
Definition: BinaryReader.h:83
Definition: DecompilerException.h:21