Plugins agregados

This commit is contained in:
2026-03-30 15:33:19 -06:00
parent df2b257d93
commit eefc67e50a
766 changed files with 107667 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
#pragma once
#include <string>
#include <memory>
#include <vector>
#include "endianness.h"
#include "Stream.h"
class BinaryReader;
namespace Ship {
class BinaryReader {
public:
BinaryReader(char* nBuffer, size_t nBufferSize);
BinaryReader(Stream* nStream);
BinaryReader(std::shared_ptr<Stream> nStream);
void Close();
void SetEndianness(Endianness endianness);
Endianness GetEndianness() const;
void Seek(int32_t offset, SeekOffsetType seekType);
uint32_t GetBaseAddress();
void Read(int32_t length);
void Read(char* buffer, int32_t length);
char ReadChar();
int8_t ReadInt8();
int16_t ReadInt16();
int32_t ReadInt32();
int64_t ReadInt64();
uint8_t ReadUByte();
uint16_t ReadUInt16();
uint32_t ReadUInt32();
uint64_t ReadUInt64();
float ReadFloat();
double ReadDouble();
std::string ReadString();
std::string ReadCString();
std::vector<char> ToVector();
protected:
std::shared_ptr<Stream> mStream;
Endianness mEndianness = Endianness::Native;
};
} // namespace Ship

View File

@@ -0,0 +1,45 @@
#pragma once
#include <array>
#include <memory>
#include <string>
#include <vector>
#include "endianness.h"
#include "Stream.h"
namespace Ship {
class BinaryWriter {
public:
BinaryWriter();
BinaryWriter(Stream* nStream);
BinaryWriter(std::shared_ptr<Stream> nStream);
void SetEndianness(Endianness endianness);
std::shared_ptr<Stream> GetStream();
uint64_t GetBaseAddress();
uint64_t GetLength();
void Seek(int32_t offset, SeekOffsetType seekType);
void Close();
void Write(int8_t value);
void Write(uint8_t value);
void Write(int16_t value);
void Write(uint16_t value);
void Write(int32_t value);
void Write(int32_t valueA, int32_t valueB);
void Write(uint32_t value);
void Write(int64_t value);
void Write(uint64_t value);
void Write(float value);
void Write(double value);
void Write(const std::string& str);
void Write(char* srcBuffer, size_t length);
std::vector<char> ToVector();
protected:
std::shared_ptr<Stream> mStream;
Endianness mEndianness = Endianness::Native;
};
} // namespace Ship

View File

@@ -0,0 +1,181 @@
#pragma once
#include <cstdint>
#include <limits>
#include <vector>
#include <cstring>
#ifdef _MSC_VER
#define BSWAP16 _byteswap_ushort
#define BSWAP32 _byteswap_ulong
#define BSWAP64 _byteswap_uint64
#else
#define BSWAP16 __builtin_bswap16
#define BSWAP32 __builtin_bswap32
#define BSWAP64 __builtin_bswap64
#endif
enum class Endianness {
Little = 0,
Big = 1,
#if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) || defined(__BIG_ENDIAN__)
Native = Big,
#else
Native = Little,
#endif
};
class BitConverter {
public:
static inline int8_t ToInt8BE(const uint8_t* data, int32_t offset) {
return (uint8_t)data[offset + 0];
}
static inline int8_t ToInt8BE(const std::vector<uint8_t>& data, int32_t offset) {
return (uint8_t)data[offset + 0];
}
static inline uint8_t ToUInt8BE(const uint8_t* data, int32_t offset) {
return (uint8_t)data[offset + 0];
}
static inline uint8_t ToUInt8BE(const std::vector<uint8_t>& data, int32_t offset) {
return (uint8_t)data[offset + 0];
}
static inline int16_t ToInt16BE(const uint8_t* data, int32_t offset) {
return ((uint16_t)data[offset + 0] << 8) + (uint16_t)data[offset + 1];
}
static inline int16_t ToInt16BE(const std::vector<uint8_t>& data, int32_t offset) {
return ((uint16_t)data[offset + 0] << 8) + (uint16_t)data[offset + 1];
}
static inline uint16_t ToUInt16BE(const uint8_t* data, int32_t offset) {
return ((uint16_t)data[offset + 0] << 8) + (uint16_t)data[offset + 1];
}
static inline uint16_t ToUInt16BE(const std::vector<uint8_t>& data, int32_t offset) {
return ((uint16_t)data[offset + 0] << 8) + (uint16_t)data[offset + 1];
}
static inline int32_t ToInt32BE(const uint8_t* data, int32_t offset) {
return ((uint32_t)data[offset + 0] << 24) + ((uint32_t)data[offset + 1] << 16) +
((uint32_t)data[offset + 2] << 8) + (uint32_t)data[offset + 3];
}
static inline int32_t ToInt32BE(const std::vector<uint8_t>& data, int32_t offset) {
return ((uint32_t)data[offset + 0] << 24) + ((uint32_t)data[offset + 1] << 16) +
((uint32_t)data[offset + 2] << 8) + (uint32_t)data[offset + 3];
}
static inline uint32_t ToUInt32BE(const uint8_t* data, int32_t offset) {
return ((uint32_t)data[offset + 0] << 24) + ((uint32_t)data[offset + 1] << 16) +
((uint32_t)data[offset + 2] << 8) + (uint32_t)data[offset + 3];
}
static inline uint32_t ToUInt32BE(const std::vector<uint8_t>& data, int32_t offset) {
return ((uint32_t)data[offset + 0] << 24) + ((uint32_t)data[offset + 1] << 16) +
((uint32_t)data[offset + 2] << 8) + (uint32_t)data[offset + 3];
}
static inline int64_t ToInt64BE(const uint8_t* data, int32_t offset) {
return ((uint64_t)data[offset + 0] << 56) + ((uint64_t)data[offset + 1] << 48) +
((uint64_t)data[offset + 2] << 40) + ((uint64_t)data[offset + 3] << 32) +
((uint64_t)data[offset + 4] << 24) + ((uint64_t)data[offset + 5] << 16) +
((uint64_t)data[offset + 6] << 8) + ((uint64_t)data[offset + 7]);
}
static inline int64_t ToInt64BE(const std::vector<uint8_t>& data, int32_t offset) {
return ((uint64_t)data[offset + 0] << 56) + ((uint64_t)data[offset + 1] << 48) +
((uint64_t)data[offset + 2] << 40) + ((uint64_t)data[offset + 3] << 32) +
((uint64_t)data[offset + 4] << 24) + ((uint64_t)data[offset + 5] << 16) +
((uint64_t)data[offset + 6] << 8) + ((uint64_t)data[offset + 7]);
}
static inline uint64_t ToUInt64BE(const uint8_t* data, int32_t offset) {
return ((uint64_t)data[offset + 0] << 56) + ((uint64_t)data[offset + 1] << 48) +
((uint64_t)data[offset + 2] << 40) + ((uint64_t)data[offset + 3] << 32) +
((uint64_t)data[offset + 4] << 24) + ((uint64_t)data[offset + 5] << 16) +
((uint64_t)data[offset + 6] << 8) + ((uint64_t)data[offset + 7]);
}
static inline uint64_t ToUInt64BE(const std::vector<uint8_t>& data, int32_t offset) {
return ((uint64_t)data[offset + 0] << 56) + ((uint64_t)data[offset + 1] << 48) +
((uint64_t)data[offset + 2] << 40) + ((uint64_t)data[offset + 3] << 32) +
((uint64_t)data[offset + 4] << 24) + ((uint64_t)data[offset + 5] << 16) +
((uint64_t)data[offset + 6] << 8) + ((uint64_t)data[offset + 7]);
}
static inline float ToFloatBE(const uint8_t* data, int32_t offset) {
float value;
uint32_t floatData = ((uint32_t)data[offset + 0] << 24) + ((uint32_t)data[offset + 1] << 16) +
((uint32_t)data[offset + 2] << 8) + (uint32_t)data[offset + 3];
static_assert(sizeof(uint32_t) == sizeof(float), "expected 32-bit float");
std::memcpy(&value, &floatData, sizeof(value));
return value;
}
static inline float ToFloatBE(const std::vector<uint8_t>& data, int32_t offset) {
float value;
uint32_t floatData = ((uint32_t)data[offset + 0] << 24) + ((uint32_t)data[offset + 1] << 16) +
((uint32_t)data[offset + 2] << 8) + (uint32_t)data[offset + 3];
static_assert(sizeof(uint32_t) == sizeof(float), "expected 32-bit float");
std::memcpy(&value, &floatData, sizeof(value));
return value;
}
static inline double ToDoubleBE(const uint8_t* data, int32_t offset) {
double value;
uint64_t floatData = ((uint64_t)data[offset + 0] << 56) + ((uint64_t)data[offset + 1] << 48) +
((uint64_t)data[offset + 2] << 40) + ((uint64_t)data[offset + 3] << 32) +
((uint64_t)data[offset + 4] << 24) + ((uint64_t)data[offset + 5] << 16) +
((uint64_t)data[offset + 6] << 8) + ((uint64_t)data[offset + 7]);
static_assert(sizeof(uint64_t) == sizeof(double), "expected 64-bit double");
// Checks if the float format on the platform the ZAPD binary is running on supports the
// same float format as the object file.
static_assert(std::numeric_limits<float>::is_iec559, "expected IEC559 floats on host machine");
std::memcpy(&value, &floatData, sizeof(value));
return value;
}
static inline double ToDoubleBE(const std::vector<uint8_t>& data, int32_t offset) {
double value;
uint64_t floatData = ((uint64_t)data[offset + 0] << 56) + ((uint64_t)data[offset + 1] << 48) +
((uint64_t)data[offset + 2] << 40) + ((uint64_t)data[offset + 3] << 32) +
((uint64_t)data[offset + 4] << 24) + ((uint64_t)data[offset + 5] << 16) +
((uint64_t)data[offset + 6] << 8) + ((uint64_t)data[offset + 7]);
static_assert(sizeof(uint64_t) == sizeof(double), "expected 64-bit double");
// Checks if the float format on the platform the ZAPD binary is running on supports the
// same float format as the object file.
static_assert(std::numeric_limits<double>::is_iec559, "expected IEC559 doubles on host machine");
std::memcpy(&value, &floatData, sizeof(value));
return value;
}
// Rewrites the rom data in-place to be in BigEndian/z64 format
static inline void RomToBigEndian(uint8_t* rom, size_t romSize) {
if (romSize <= 0) {
return;
}
// Use the first byte to determine byte order
uint8_t firstByte = rom[0];
switch (firstByte) {
case 0x37: // v64
for (size_t pos = 0; pos < (romSize / 2); pos++) {
((uint16_t*)rom)[pos] = ToUInt16BE(rom, pos * 2);
}
break;
case 0x40: // n64
for (size_t pos = 0; pos < (romSize / 4); pos++) {
((uint32_t*)rom)[pos] = ToUInt32BE(rom, pos * 4);
}
break;
case 0x80: // z64
break; // Already BE, no need to swap
}
}
};

View File

@@ -0,0 +1,35 @@
#pragma once
#include <memory>
#include <vector>
#include "Stream.h"
namespace Ship {
class MemoryStream final : public Stream {
public:
MemoryStream();
MemoryStream(char* nBuffer, size_t nBufferSize);
MemoryStream(std::shared_ptr<std::vector<char>> buffer);
~MemoryStream();
uint64_t GetLength() override;
void Seek(int32_t offset, SeekOffsetType seekType) override;
std::unique_ptr<char[]> Read(size_t length) override;
void Read(const char* dest, size_t length) override;
int8_t ReadByte() override;
void Write(char* srcBuffer, size_t length) override;
void WriteByte(int8_t value) override;
std::vector<char> ToVector() override;
void Flush() override;
void Close() override;
protected:
std::shared_ptr<std::vector<char>> mBuffer;
std::size_t mBufferSize;
};
} // namespace Ship

View File

@@ -0,0 +1,33 @@
#pragma once
#include <cstdint>
#include <memory>
#include <vector>
namespace Ship {
enum class SeekOffsetType { Start, Current, End };
class Stream {
public:
virtual ~Stream() = default;
virtual uint64_t GetLength() = 0;
uint64_t GetBaseAddress();
virtual void Seek(int32_t offset, SeekOffsetType seekType) = 0;
virtual std::unique_ptr<char[]> Read(size_t length) = 0;
virtual void Read(const char* dest, size_t length) = 0;
virtual int8_t ReadByte() = 0;
virtual void Write(char* destBuffer, size_t length) = 0;
virtual void WriteByte(int8_t value) = 0;
virtual std::vector<char> ToVector() = 0;
virtual void Flush() = 0;
virtual void Close() = 0;
protected:
uint64_t mBaseAddress;
};
} // namespace Ship

View File

@@ -0,0 +1,85 @@
#pragma once
#ifdef __cplusplus
namespace Ship {
enum class Endianness {
Little = 0,
Big = 1,
#if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) || defined(__BIG_ENDIAN__)
Native = Big,
#else
Native = Little,
#endif
};
} // namespace Ship
#endif
#ifdef _MSC_VER
#include <stdlib.h>
#define BSWAP16 _byteswap_ushort
#define BSWAP32 _byteswap_ulong
#define BSWAP64 _byteswap_uint64
#define BOMSWAP16 _byteswap_ushort
#define BOMSWAP32 _byteswap_ulong
#define BOMSWAP64 _byteswap_uint64
#define BOMSWAP16_CONST(x) ((((x) >> 8) & 0x00FF) | (((x) << 8) & 0xFF00))
#define BOMSWAP32_CONST(x) \
((((x) >> 24) & 0x000000FF) | (((x) >> 8) & 0x0000FF00) | (((x) << 8) & 0x00FF0000) | (((x) << 24) & 0xFF000000))
#define BOMSWAP64_CONST(x) \
((((x) >> 56) & 0x00000000000000FF) | (((x) >> 40) & 0x000000000000FF00) | (((x) >> 24) & 0x0000000000FF0000) | \
(((x) >> 8) & 0x00000000FF000000) | (((x) << 8) & 0x000000FF00000000) | (((x) << 24) & 0x0000FF0000000000) | \
(((x) << 40) & 0x00FF000000000000) | (((x) << 56) & 0xFF00000000000000))
#else
#define BSWAP16 __builtin_bswap16
#define BSWAP32 __builtin_bswap32
#define BSWAP64 __builtin_bswap64
#define BOMSWAP16 __builtin_bswap16
#define BOMSWAP32 __builtin_bswap32
#define BOMSWAP64 __builtin_bswap64
#define BOMSWAP16_CONST __builtin_bswap16
#define BOMSWAP32_CONST __builtin_bswap32
#define BOMSWAP64_CONST __builtin_bswap64
#endif
#if (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) || defined(__BIG_ENDIAN__)
#ifndef IS_BIGENDIAN
#define IS_BIGENDIAN
#endif
#endif
#ifdef IS_BIGENDIAN
#define BE16SWAP(x) (x)
#define BE32SWAP(x) (x)
#define BE64SWAP(x) (x)
#define LE16SWAP(x) BOMSWAP16(x)
#define LE32SWAP(x) BOMSWAP32(x)
#define LE64SWAP(x) BOMSWAP64(x)
#define BE16SWAP_CONST(x) (x)
#define BE32SWAP_CONST(x) (x)
#define BE64SWAP_CONST(x) (x)
#define LE16SWAP_CONST(x) BOMSWAP16_CONST(x)
#define LE32SWAP_CONST(x) BOMSWAP32_CONST(x)
#define LE64SWAP_CONST(x) BOMSWAP64_CONST(x)
#else
#define BE16SWAP(x) BOMSWAP16(x)
#define BE32SWAP(x) BOMSWAP32(x)
#define BE64SWAP(x) BOMSWAP64(x)
#define LE16SWAP(x) (x)
#define LE32SWAP(x) (x)
#define LE64SWAP(x) (x)
#define BE16SWAP_CONST(x) BOMSWAP16_CONST(x)
#define BE32SWAP_CONST(x) BOMSWAP32_CONST(x)
#define BE64SWAP_CONST(x) BOMSWAP64_CONST(x)
#define LE16SWAP_CONST(x) (x)
#define LE32SWAP_CONST(x) (x)
#define LE64SWAP_CONST(x) (x)
#endif