Skip to content

Commit

Permalink
Merge branch 'merge-soh-macready-to-master' into merge-soh-macready
Browse files Browse the repository at this point in the history
  • Loading branch information
Archez committed May 3, 2024
2 parents dcf264b + 15b7b3c commit af426bd
Show file tree
Hide file tree
Showing 15 changed files with 1,259 additions and 8 deletions.
16 changes: 8 additions & 8 deletions ZAPD/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ set(Source_Files__Z64__ZRoom__Commands
)
source_group("Source Files\\Z64\\ZRoom\\Commands" FILES ${Source_Files__Z64__ZRoom__Commands})

file(GLOB Source_Files__Utils RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "Utils/*.c" "Utils/*.cpp")
source_group("Source Files\\Utils" FILES ${Source_Files__Utils})

file(GLOB Header_Files__Utils RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "Utils/*.h")
source_group("Header Files\\Utils" FILES ${Header_Files__Utils})

set(ALL_FILES
${Header_Files}
${Header_Files__Libraries}
Expand All @@ -250,13 +256,15 @@ set(ALL_FILES
${Header_Files__Z64}
${Header_Files__Z64__ZRoom}
${Header_Files__Z64__ZRoom__Commands}
${Header_Files__Utils}
${Resource_Files}
${Source_Files}
${Source_Files__Libraries__libgfxd}
${Source_Files__Yaz0}
${Source_Files__Z64}
${Source_Files__Z64__ZRoom}
${Source_Files__Z64__ZRoom__Commands}
${Source_Files__Utils}
${any__any}
)

Expand Down Expand Up @@ -347,10 +355,8 @@ endif()
find_package(PNG REQUIRED)

target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../../libultraship/extern/ZAPDUtils
${CMAKE_CURRENT_SOURCE_DIR}/../../libultraship/src/resource
${CMAKE_CURRENT_SOURCE_DIR}/../../libultraship/include
${CMAKE_CURRENT_SOURCE_DIR}/../../ZAPDTR/lib/tinyxml2
${CMAKE_CURRENT_SOURCE_DIR}/../../ZAPDTR/lib/libgfxd
${PNG_PNG_INCLUDE_DIR}/
.
Expand Down Expand Up @@ -446,15 +452,13 @@ endif()
################################################################################
add_dependencies(${PROJECT_NAME}
OTRExporter
ZAPDUtils
libultraship
)

if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
# if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64")
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(ADDITIONAL_LIBRARY_DEPENDENCIES
"ZAPDUtils;"
"-WHOLEARCHIVE:$<TARGET_LINKER_FILE_DIR:OTRExporter>/$<TARGET_LINKER_FILE_NAME:OTRExporter>"
"libultraship;"
storm
Expand All @@ -465,7 +469,6 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
set(ADDITIONAL_LIBRARY_DEPENDENCIES
"ZAPDUtils;"
-Wl,-force_load $<TARGET_LINKER_FILE_DIR:OTRExporter>/$<TARGET_LINKER_FILE_NAME:OTRExporter>
"libultraship;"
PNG::PNG
Expand All @@ -476,23 +479,20 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "NintendoSwitch")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
set(ADDITIONAL_LIBRARY_DEPENDENCIES
"ZAPDUtils;"
-Wl,--whole-archive $<TARGET_LINKER_FILE_DIR:OTRExporter>/$<TARGET_LINKER_FILE_NAME:OTRExporter> -Wl,--no-whole-archive
"libultraship;"
PNG::PNG
Threads::Threads
)
elseif(CMAKE_SYSTEM_NAME STREQUAL "CafeOS")
set(ADDITIONAL_LIBRARY_DEPENDENCIES
"ZAPDUtils;"
"libultraship;"
PNG::PNG
)
else()
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
set(ADDITIONAL_LIBRARY_DEPENDENCIES
"ZAPDUtils;"
-Wl,--whole-archive $<TARGET_LINKER_FILE_DIR:OTRExporter>/$<TARGET_LINKER_FILE_NAME:OTRExporter> -Wl,--no-whole-archive
"libultraship;"
PNG::PNG
Expand Down
178 changes: 178 additions & 0 deletions ZAPD/Utils/BinaryReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#include "BinaryReader.h"
#include <cmath>
#include <stdexcept>
#include "Stream.h"

BinaryReader::BinaryReader(Stream* nStream)
{
stream.reset(nStream);
}

BinaryReader::BinaryReader(std::shared_ptr<Stream> nStream)
{
stream = nStream;
}

void BinaryReader::Close()
{
stream->Close();
}

void BinaryReader::SetEndianness(Endianness endianness)
{
this->endianness = endianness;
}

Endianness BinaryReader::GetEndianness() const
{
return endianness;
}

void BinaryReader::Seek(uint32_t offset, SeekOffsetType seekType)
{
stream->Seek(offset, seekType);
}

uint32_t BinaryReader::GetBaseAddress()
{
return stream->GetBaseAddress();
}

void BinaryReader::Read(int32_t length)
{
stream->Read(length);
}

void BinaryReader::Read(char* buffer, int32_t length)
{
stream->Read(buffer, length);
}

char BinaryReader::ReadChar()
{
return (char)stream->ReadByte();
}

int8_t BinaryReader::ReadByte()
{
return stream->ReadByte();
}

uint8_t BinaryReader::ReadUByte()
{
return (uint8_t)stream->ReadByte();
}

int16_t BinaryReader::ReadInt16()
{
int16_t result = 0;

stream->Read((char*)&result, sizeof(int16_t));

if (endianness != Endianness::Native)
result = BSWAP16(result);

return result;
}

int32_t BinaryReader::ReadInt32()
{
int32_t result = 0;

stream->Read((char*)&result, sizeof(int32_t));

if (endianness != Endianness::Native)
result = BSWAP32(result);

return result;
}

uint16_t BinaryReader::ReadUInt16()
{
uint16_t result = 0;

stream->Read((char*)&result, sizeof(uint16_t));

if (endianness != Endianness::Native)
result = BSWAP16(result);

return result;
}

uint32_t BinaryReader::ReadUInt32()
{
uint32_t result = 0;

stream->Read((char*)&result, sizeof(uint32_t));

if (endianness != Endianness::Native)
result = BSWAP32(result);

return result;
}

uint64_t BinaryReader::ReadUInt64()
{
uint64_t result = 0;

stream->Read((char*)&result, sizeof(uint64_t));

if (endianness != Endianness::Native)
result = BSWAP64(result);

return result;
}

float BinaryReader::ReadSingle()
{
float result = NAN;

stream->Read((char*)&result, sizeof(float));

if (endianness != 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::ReadSingle(): Error reading stream");

return result;
}

double BinaryReader::ReadDouble()
{
double result = NAN;

stream->Read((char*)&result, sizeof(double));

if (endianness != 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 BinaryReader::ReadString()
{
std::string res;
int numChars = ReadInt32();

for (int i = 0; i < numChars; i++)
res += ReadChar();

return res;
}
41 changes: 41 additions & 0 deletions ZAPD/Utils/BinaryReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <array>
#include <memory>
#include <string>
#include <vector>
#include "BitConverter.h"
#include "Stream.h"

class BinaryReader
{
public:
BinaryReader(Stream* nStream);
BinaryReader(std::shared_ptr<Stream> nStream);

void Close();

void SetEndianness(Endianness endianness);
Endianness GetEndianness() const;

void Seek(uint32_t offset, SeekOffsetType seekType);
uint32_t GetBaseAddress();

void Read(int32_t length);
void Read(char* buffer, int32_t length);
char ReadChar();
int8_t ReadByte();
int16_t ReadInt16();
int32_t ReadInt32();
uint8_t ReadUByte();
uint16_t ReadUInt16();
uint32_t ReadUInt32();
uint64_t ReadUInt64();
float ReadSingle();
double ReadDouble();
std::string ReadString();

protected:
std::shared_ptr<Stream> stream;
Endianness endianness = Endianness::Native;
};
Loading

0 comments on commit af426bd

Please sign in to comment.