Files
Torch/lib/binarytools/BinaryReader.cpp

224 lines
4.8 KiB
C++
Raw Normal View History

#include "./BinaryReader.h"
#include "./MemoryStream.h"
#include <cmath>
#include <stdexcept>
#include <locale>
LUS::BinaryReader::BinaryReader(char* nBuffer, size_t nBufferSize) {
mStream = std::make_shared<MemoryStream>(nBuffer, nBufferSize);
}
Torch v0.1 (#12) * Splitted version yamls and implemented wip geo and dlist extraction * Fixed linux compilation * Added PoC WSL 2 detection * Add undefined behaviour * Added gfxdis as submodule * update * Removed gfxdis as a submodule * Code works * Replace ByteSwap with BSWAP * Cleanup vertice factory * readability * readability 2 * Cleanup gfxfactory * last one * Rename name to symbol * More implementation * Implemented first PoC of C file generation * Added output formatting on factories * changes * Moved generated dlists onto their respective folder * Migrated more factories onto the new extractor system * Fully ported all factories and added header generation * Some updates * Added Lights factory * fix * Fixed vtx extraction * Lights work now * delete old startup_logo yaml * Generate all sections in single file * Added support to generate header and code for textures * Added O3 for release builds * Added WIP dlist extraction and moved to use f3dold microcode * Added segment support, autogeneration and fixed C generation * Added symbol names on display list extraction * Removed warnings * Moved back to transform and replace * Removed unnecesary macros * Added extraction order and reversed vtx order * Implemented true backwards sort * [WIP] Implemented runtime gbi, more headers and a few config features * Fix CLI11 on WSL and variety of fixes * Fixes * Update DisplayListFactory.h * Update TextureFactory.cpp * wip lights * fix * Correct callback * Added waypoints * Update ceremony_data.yml * Changes * Update TextureFactory.cpp * Update commandline args * Update comment * Remove printf * Update couple commands * Renamed cubeos to torch * Removed unused stuff and added an example config file --------- Co-authored-by: KiritoDv <kiritodev01@gmail.com> Co-authored-by: MegaMech <MegaMech@users.noreply.github.com>
2023-11-15 15:11:15 -06:00
LUS::BinaryReader::BinaryReader(uint8_t* nBuffer, size_t nBufferSize) {
mStream = std::make_shared<MemoryStream>((char*) nBuffer, nBufferSize);
}
LUS::BinaryReader::BinaryReader(Stream* nStream) {
mStream.reset(nStream);
}
LUS::BinaryReader::BinaryReader(std::shared_ptr<Stream> nStream) {
mStream = nStream;
}
void LUS::BinaryReader::Close() {
mStream->Close();
}
2024-05-25 16:31:32 -06:00
void LUS::BinaryReader::SetEndianness(Torch::Endianness endianness) {
this->mEndianness = endianness;
}
2024-05-25 16:31:32 -06:00
Torch::Endianness LUS::BinaryReader::GetEndianness() const {
return mEndianness;
}
void LUS::BinaryReader::Seek(int32_t offset, SeekOffsetType seekType) {
mStream->Seek(offset, seekType);
}
uint32_t LUS::BinaryReader::GetBaseAddress() {
return mStream->GetBaseAddress();
}
void LUS::BinaryReader::Read(int32_t length) {
mStream->Read(length);
}
void LUS::BinaryReader::Read(char* buffer, int32_t length) {
mStream->Read(buffer, length);
}
char LUS::BinaryReader::ReadChar() {
return (char)mStream->ReadByte();
}
int8_t LUS::BinaryReader::ReadInt8() {
return mStream->ReadByte();
}
int16_t LUS::BinaryReader::ReadInt16() {
int16_t result = 0;
mStream->Read((char*)&result, sizeof(int16_t));
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
result = BSWAP16(result);
}
return result;
}
int32_t LUS::BinaryReader::ReadInt32() {
int32_t result = 0;
mStream->Read((char*)&result, sizeof(int32_t));
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
result = BSWAP32(result);
}
return result;
}
uint8_t LUS::BinaryReader::ReadUByte() {
return (uint8_t)mStream->ReadByte();
}
uint16_t LUS::BinaryReader::ReadUInt16() {
uint16_t result = 0;
mStream->Read((char*)&result, sizeof(uint16_t));
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
result = BSWAP16(result);
}
return result;
}
uint32_t LUS::BinaryReader::ReadUInt32() {
uint32_t result = 0;
mStream->Read((char*)&result, sizeof(uint32_t));
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
result = BSWAP32(result);
}
return result;
}
uint64_t LUS::BinaryReader::ReadUInt64() {
uint64_t result = 0;
mStream->Read((char*)&result, sizeof(uint64_t));
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
result = BSWAP64(result);
}
return result;
}
unsigned short LUS::BinaryReader::ReadUShort() {
unsigned short result = 0;
mStream->Read((char*)&result, sizeof(unsigned short));
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
result = BSWAP16(result);
}
return result;
}
short LUS::BinaryReader::ReadShort() {
short result = 0;
mStream->Read((char*)&result, sizeof(short));
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
result = BSWAP16(result);
}
return result;
}
float LUS::BinaryReader::ReadFloat() {
float result = NAN;
mStream->Read((char*)&result, sizeof(float));
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
float tmp;
char* dst = (char*)&tmp;
char* src = (char*)&result;
dst[3] = src[0];
dst[2] = src[1];
dst[1] = src[2];
dst[0] = src[3];
result = tmp;
}
if (std::isnan(result)) {
throw std::runtime_error("BinaryReader::ReadFloat(): Error reading stream");
}
return result;
}
double LUS::BinaryReader::ReadDouble() {
double result = NAN;
mStream->Read((char*)&result, sizeof(double));
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
double tmp;
char* dst = (char*)&tmp;
char* src = (char*)&result;
dst[7] = src[0];
dst[6] = src[1];
dst[5] = src[2];
dst[4] = src[3];
dst[3] = src[4];
dst[2] = src[5];
dst[1] = src[6];
dst[0] = src[7];
result = tmp;
}
if (std::isnan(result)) {
throw std::runtime_error("BinaryReader::ReadDouble(): Error reading stream");
}
return result;
}
std::string LUS::BinaryReader::ReadString() {
std::string res;
int numChars = ReadInt32();
for (int i = 0; i < numChars; i++) {
res += ReadChar();
}
2023-09-03 23:29:11 -06:00
return res;
}
std::string LUS::BinaryReader::ReadCString() {
std::string res;
unsigned char c = 0;
do {
if (mStream->GetBaseAddress() >= mStream->GetLength()) {
break;
}
c = ReadChar();
res += c;
} while (c != '\0');
return res;
}
2023-09-03 23:29:11 -06:00
size_t LUS::BinaryReader::GetLength() {
return mStream->GetLength();
}
std::vector<char> LUS::BinaryReader::ToVector() {
return mStream->ToVector();
2023-09-03 23:29:11 -06:00
}