Files
Torch/lib/binarytools/BinaryWriter.cpp

161 lines
3.7 KiB
C++
Raw Normal View History

#include "./BinaryWriter.h"
#include "./MemoryStream.h"
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
#include <iostream>
LUS::BinaryWriter::BinaryWriter() {
mStream = std::make_shared<MemoryStream>();
}
LUS::BinaryWriter::BinaryWriter(Stream* nStream) {
mStream.reset(nStream);
}
LUS::BinaryWriter::BinaryWriter(std::shared_ptr<Stream> nStream) {
mStream = nStream;
}
2024-05-25 16:31:32 -06:00
void LUS::BinaryWriter::SetEndianness(Torch::Endianness endianness) {
this->mEndianness = endianness;
}
void LUS::BinaryWriter::Close() {
mStream->Close();
}
std::shared_ptr<LUS::Stream> LUS::BinaryWriter::GetStream() {
return mStream;
}
uint64_t LUS::BinaryWriter::GetBaseAddress() {
return mStream->GetBaseAddress();
}
uint64_t LUS::BinaryWriter::GetLength() {
return mStream->GetLength();
}
void LUS::BinaryWriter::Seek(int32_t offset, SeekOffsetType seekType) {
mStream->Seek(offset, seekType);
}
void LUS::BinaryWriter::Write(int8_t value) {
mStream->Write((char*)&value, sizeof(int8_t));
}
void LUS::BinaryWriter::Write(uint8_t value) {
mStream->Write((char*)&value, sizeof(uint8_t));
}
void LUS::BinaryWriter::Write(int16_t value) {
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
value = BSWAP16(value);
}
mStream->Write((char*)&value, sizeof(int16_t));
}
void LUS::BinaryWriter::Write(uint16_t value) {
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
value = BSWAP16(value);
}
mStream->Write((char*)&value, sizeof(uint16_t));
}
void LUS::BinaryWriter::Write(int32_t value) {
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
value = BSWAP32(value);
}
mStream->Write((char*)&value, sizeof(int32_t));
}
void LUS::BinaryWriter::Write(int32_t valueA, int32_t valueB) {
Write(valueA);
Write(valueB);
}
void LUS::BinaryWriter::Write(uint32_t value) {
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
value = BSWAP32(value);
}
mStream->Write((char*)&value, sizeof(uint32_t));
}
void LUS::BinaryWriter::Write(int64_t value) {
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
value = BSWAP64(value);
}
mStream->Write((char*)&value, sizeof(int64_t));
}
void LUS::BinaryWriter::Write(uint64_t value) {
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
value = BSWAP64(value);
}
mStream->Write((char*)&value, sizeof(uint64_t));
}
void LUS::BinaryWriter::Write(float value) {
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
float tmp;
char* dst = (char*)&tmp;
char* src = (char*)&value;
dst[3] = src[0];
dst[2] = src[1];
dst[1] = src[2];
dst[0] = src[3];
value = tmp;
}
mStream->Write((char*)&value, sizeof(float));
}
void LUS::BinaryWriter::Write(double value) {
2024-05-25 16:31:32 -06:00
if (mEndianness != Torch::Endianness::Native) {
double tmp;
char* dst = (char*)&tmp;
char* src = (char*)&value;
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];
value = tmp;
}
mStream->Write((char*)&value, sizeof(double));
}
2023-09-03 20:11:35 -06:00
void LUS::BinaryWriter::Write(const std::string& str, bool writeLength) {
if(writeLength){
int strLen = str.size();
Write(strLen);
}
for (char c : str) {
mStream->WriteByte(c);
}
}
void LUS::BinaryWriter::Write(char* srcBuffer, size_t length) {
mStream->Write(srcBuffer, length);
}
2023-09-03 20:11:35 -06:00
void LUS::BinaryWriter::WriteByte(char value) {
mStream->WriteByte(value);
}
std::vector<char> LUS::BinaryWriter::ToVector() {
return mStream->ToVector();
}
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
void LUS::BinaryWriter::Finish(std::ostream& output) {
auto data = mStream->ToVector();
output.write(data.data(), data.size());
}