Skip to content

Commit 2b8794f

Browse files
authored
Merge pull request HarbourMasters#17 from Archez/merge-upstream
2 parents dcf264b + 4b6efa9 commit 2b8794f

File tree

15 files changed

+1259
-8
lines changed

15 files changed

+1259
-8
lines changed

ZAPD/CMakeLists.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ set(Source_Files__Z64__ZRoom__Commands
242242
)
243243
source_group("Source Files\\Z64\\ZRoom\\Commands" FILES ${Source_Files__Z64__ZRoom__Commands})
244244

245+
file(GLOB Source_Files__Utils RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "Utils/*.c" "Utils/*.cpp")
246+
source_group("Source Files\\Utils" FILES ${Source_Files__Utils})
247+
248+
file(GLOB Header_Files__Utils RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "Utils/*.h")
249+
source_group("Header Files\\Utils" FILES ${Header_Files__Utils})
250+
245251
set(ALL_FILES
246252
${Header_Files}
247253
${Header_Files__Libraries}
@@ -250,13 +256,15 @@ set(ALL_FILES
250256
${Header_Files__Z64}
251257
${Header_Files__Z64__ZRoom}
252258
${Header_Files__Z64__ZRoom__Commands}
259+
${Header_Files__Utils}
253260
${Resource_Files}
254261
${Source_Files}
255262
${Source_Files__Libraries__libgfxd}
256263
${Source_Files__Yaz0}
257264
${Source_Files__Z64}
258265
${Source_Files__Z64__ZRoom}
259266
${Source_Files__Z64__ZRoom__Commands}
267+
${Source_Files__Utils}
260268
${any__any}
261269
)
262270

@@ -347,10 +355,8 @@ endif()
347355
find_package(PNG REQUIRED)
348356

349357
target_include_directories(${PROJECT_NAME} PRIVATE
350-
${CMAKE_CURRENT_SOURCE_DIR}/../../libultraship/extern/ZAPDUtils
351358
${CMAKE_CURRENT_SOURCE_DIR}/../../libultraship/src/resource
352359
${CMAKE_CURRENT_SOURCE_DIR}/../../libultraship/include
353-
${CMAKE_CURRENT_SOURCE_DIR}/../../ZAPDTR/lib/tinyxml2
354360
${CMAKE_CURRENT_SOURCE_DIR}/../../ZAPDTR/lib/libgfxd
355361
${PNG_PNG_INCLUDE_DIR}/
356362
.
@@ -446,15 +452,13 @@ endif()
446452
################################################################################
447453
add_dependencies(${PROJECT_NAME}
448454
OTRExporter
449-
ZAPDUtils
450455
libultraship
451456
)
452457

453458
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
454459
# if("${CMAKE_VS_PLATFORM_NAME}" STREQUAL "x64")
455460
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
456461
set(ADDITIONAL_LIBRARY_DEPENDENCIES
457-
"ZAPDUtils;"
458462
"-WHOLEARCHIVE:$<TARGET_LINKER_FILE_DIR:OTRExporter>/$<TARGET_LINKER_FILE_NAME:OTRExporter>"
459463
"libultraship;"
460464
storm
@@ -465,7 +469,6 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
465469
set(THREADS_PREFER_PTHREAD_FLAG ON)
466470
find_package(Threads REQUIRED)
467471
set(ADDITIONAL_LIBRARY_DEPENDENCIES
468-
"ZAPDUtils;"
469472
-Wl,-force_load $<TARGET_LINKER_FILE_DIR:OTRExporter>/$<TARGET_LINKER_FILE_NAME:OTRExporter>
470473
"libultraship;"
471474
PNG::PNG
@@ -476,23 +479,20 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "NintendoSwitch")
476479
set(THREADS_PREFER_PTHREAD_FLAG ON)
477480
find_package(Threads REQUIRED)
478481
set(ADDITIONAL_LIBRARY_DEPENDENCIES
479-
"ZAPDUtils;"
480482
-Wl,--whole-archive $<TARGET_LINKER_FILE_DIR:OTRExporter>/$<TARGET_LINKER_FILE_NAME:OTRExporter> -Wl,--no-whole-archive
481483
"libultraship;"
482484
PNG::PNG
483485
Threads::Threads
484486
)
485487
elseif(CMAKE_SYSTEM_NAME STREQUAL "CafeOS")
486488
set(ADDITIONAL_LIBRARY_DEPENDENCIES
487-
"ZAPDUtils;"
488489
"libultraship;"
489490
PNG::PNG
490491
)
491492
else()
492493
set(THREADS_PREFER_PTHREAD_FLAG ON)
493494
find_package(Threads REQUIRED)
494495
set(ADDITIONAL_LIBRARY_DEPENDENCIES
495-
"ZAPDUtils;"
496496
-Wl,--whole-archive $<TARGET_LINKER_FILE_DIR:OTRExporter>/$<TARGET_LINKER_FILE_NAME:OTRExporter> -Wl,--no-whole-archive
497497
"libultraship;"
498498
PNG::PNG

ZAPD/Utils/BinaryReader.cpp

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#include "BinaryReader.h"
2+
#include <cmath>
3+
#include <stdexcept>
4+
#include "Stream.h"
5+
6+
BinaryReader::BinaryReader(Stream* nStream)
7+
{
8+
stream.reset(nStream);
9+
}
10+
11+
BinaryReader::BinaryReader(std::shared_ptr<Stream> nStream)
12+
{
13+
stream = nStream;
14+
}
15+
16+
void BinaryReader::Close()
17+
{
18+
stream->Close();
19+
}
20+
21+
void BinaryReader::SetEndianness(Endianness endianness)
22+
{
23+
this->endianness = endianness;
24+
}
25+
26+
Endianness BinaryReader::GetEndianness() const
27+
{
28+
return endianness;
29+
}
30+
31+
void BinaryReader::Seek(uint32_t offset, SeekOffsetType seekType)
32+
{
33+
stream->Seek(offset, seekType);
34+
}
35+
36+
uint32_t BinaryReader::GetBaseAddress()
37+
{
38+
return stream->GetBaseAddress();
39+
}
40+
41+
void BinaryReader::Read(int32_t length)
42+
{
43+
stream->Read(length);
44+
}
45+
46+
void BinaryReader::Read(char* buffer, int32_t length)
47+
{
48+
stream->Read(buffer, length);
49+
}
50+
51+
char BinaryReader::ReadChar()
52+
{
53+
return (char)stream->ReadByte();
54+
}
55+
56+
int8_t BinaryReader::ReadByte()
57+
{
58+
return stream->ReadByte();
59+
}
60+
61+
uint8_t BinaryReader::ReadUByte()
62+
{
63+
return (uint8_t)stream->ReadByte();
64+
}
65+
66+
int16_t BinaryReader::ReadInt16()
67+
{
68+
int16_t result = 0;
69+
70+
stream->Read((char*)&result, sizeof(int16_t));
71+
72+
if (endianness != Endianness::Native)
73+
result = BSWAP16(result);
74+
75+
return result;
76+
}
77+
78+
int32_t BinaryReader::ReadInt32()
79+
{
80+
int32_t result = 0;
81+
82+
stream->Read((char*)&result, sizeof(int32_t));
83+
84+
if (endianness != Endianness::Native)
85+
result = BSWAP32(result);
86+
87+
return result;
88+
}
89+
90+
uint16_t BinaryReader::ReadUInt16()
91+
{
92+
uint16_t result = 0;
93+
94+
stream->Read((char*)&result, sizeof(uint16_t));
95+
96+
if (endianness != Endianness::Native)
97+
result = BSWAP16(result);
98+
99+
return result;
100+
}
101+
102+
uint32_t BinaryReader::ReadUInt32()
103+
{
104+
uint32_t result = 0;
105+
106+
stream->Read((char*)&result, sizeof(uint32_t));
107+
108+
if (endianness != Endianness::Native)
109+
result = BSWAP32(result);
110+
111+
return result;
112+
}
113+
114+
uint64_t BinaryReader::ReadUInt64()
115+
{
116+
uint64_t result = 0;
117+
118+
stream->Read((char*)&result, sizeof(uint64_t));
119+
120+
if (endianness != Endianness::Native)
121+
result = BSWAP64(result);
122+
123+
return result;
124+
}
125+
126+
float BinaryReader::ReadSingle()
127+
{
128+
float result = NAN;
129+
130+
stream->Read((char*)&result, sizeof(float));
131+
132+
if (endianness != Endianness::Native)
133+
{
134+
float tmp;
135+
char* dst = (char*)&tmp;
136+
char* src = (char*)&result;
137+
dst[3] = src[0]; dst[2] = src[1]; dst[1] = src[2]; dst[0] = src[3];
138+
result = tmp;
139+
}
140+
141+
if (std::isnan(result))
142+
throw std::runtime_error("BinaryReader::ReadSingle(): Error reading stream");
143+
144+
return result;
145+
}
146+
147+
double BinaryReader::ReadDouble()
148+
{
149+
double result = NAN;
150+
151+
stream->Read((char*)&result, sizeof(double));
152+
153+
if (endianness != Endianness::Native)
154+
{
155+
double tmp;
156+
char* dst = (char*)&tmp;
157+
char* src = (char*)&result;
158+
dst[7] = src[0]; dst[6] = src[1]; dst[5] = src[2]; dst[4] = src[3];
159+
dst[3] = src[4]; dst[2] = src[5]; dst[1] = src[6]; dst[0] = src[7];
160+
result = tmp;
161+
}
162+
163+
if (std::isnan(result))
164+
throw std::runtime_error("BinaryReader::ReadDouble(): Error reading stream");
165+
166+
return result;
167+
}
168+
169+
std::string BinaryReader::ReadString()
170+
{
171+
std::string res;
172+
int numChars = ReadInt32();
173+
174+
for (int i = 0; i < numChars; i++)
175+
res += ReadChar();
176+
177+
return res;
178+
}

ZAPD/Utils/BinaryReader.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
#include <array>
4+
#include <memory>
5+
#include <string>
6+
#include <vector>
7+
#include "BitConverter.h"
8+
#include "Stream.h"
9+
10+
class BinaryReader
11+
{
12+
public:
13+
BinaryReader(Stream* nStream);
14+
BinaryReader(std::shared_ptr<Stream> nStream);
15+
16+
void Close();
17+
18+
void SetEndianness(Endianness endianness);
19+
Endianness GetEndianness() const;
20+
21+
void Seek(uint32_t offset, SeekOffsetType seekType);
22+
uint32_t GetBaseAddress();
23+
24+
void Read(int32_t length);
25+
void Read(char* buffer, int32_t length);
26+
char ReadChar();
27+
int8_t ReadByte();
28+
int16_t ReadInt16();
29+
int32_t ReadInt32();
30+
uint8_t ReadUByte();
31+
uint16_t ReadUInt16();
32+
uint32_t ReadUInt32();
33+
uint64_t ReadUInt64();
34+
float ReadSingle();
35+
double ReadDouble();
36+
std::string ReadString();
37+
38+
protected:
39+
std::shared_ptr<Stream> stream;
40+
Endianness endianness = Endianness::Native;
41+
};

0 commit comments

Comments
 (0)