mirror of
https://github.com/HarbourMasters/Shipwright.git
synced 2025-08-21 05:43:42 -07:00
git subrepo clone https://github.com/HarbourMasters/ZAPDTR.git
subrepo: subdir: "ZAPDTR" merged: "a53a53ea4" upstream: origin: "https://github.com/HarbourMasters/ZAPDTR.git" branch: "master" commit: "a53a53ea4" git-subrepo: version: "0.4.1" origin: "???" commit: "???"
This commit is contained in:
parent
f52a2a6406
commit
5dda5762ba
217 changed files with 47152 additions and 0 deletions
148
ZAPDTR/ZAPDUtils/Utils/BinaryReader.cpp
Normal file
148
ZAPDTR/ZAPDUtils/Utils/BinaryReader.cpp
Normal file
|
@ -0,0 +1,148 @@
|
|||
#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::Seek(uint32_t offset, SeekOffsetType seekType)
|
||||
{
|
||||
stream->Seek(offset, seekType);
|
||||
}
|
||||
|
||||
uint32_t BinaryReader::GetBaseAddress()
|
||||
{
|
||||
return stream->GetBaseAddress();
|
||||
}
|
||||
|
||||
void BinaryReader::Read([[maybe_unused]] char* buffer, int32_t length)
|
||||
{
|
||||
stream->Read(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));
|
||||
return result;
|
||||
}
|
||||
|
||||
int32_t BinaryReader::ReadInt32()
|
||||
{
|
||||
int32_t result = 0;
|
||||
|
||||
stream->Read((char*)&result, sizeof(int32_t));
|
||||
return result;
|
||||
}
|
||||
|
||||
uint16_t BinaryReader::ReadUInt16()
|
||||
{
|
||||
uint16_t result = 0;
|
||||
|
||||
stream->Read((char*)&result, sizeof(uint16_t));
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t BinaryReader::ReadUInt32()
|
||||
{
|
||||
uint32_t result = 0;
|
||||
|
||||
stream->Read((char*)&result, sizeof(uint32_t));
|
||||
return result;
|
||||
}
|
||||
|
||||
uint64_t BinaryReader::ReadUInt64()
|
||||
{
|
||||
uint64_t result = 0;
|
||||
|
||||
stream->Read((char*)&result, sizeof(uint64_t));
|
||||
return result;
|
||||
}
|
||||
|
||||
float BinaryReader::ReadSingle()
|
||||
{
|
||||
float result = NAN;
|
||||
|
||||
stream->Read((char*)&result, sizeof(float));
|
||||
|
||||
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 (std::isnan(result))
|
||||
throw std::runtime_error("BinaryReader::ReadDouble(): Error reading stream");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Vec3f BinaryReader::ReadVec3f()
|
||||
{
|
||||
return Vec3f();
|
||||
}
|
||||
|
||||
Vec3s BinaryReader::ReadVec3s()
|
||||
{
|
||||
return Vec3s(0, 0, 0);
|
||||
}
|
||||
|
||||
Vec3s BinaryReader::ReadVec3b()
|
||||
{
|
||||
return Vec3s(0, 0, 0);
|
||||
}
|
||||
|
||||
Vec2f BinaryReader::ReadVec2f()
|
||||
{
|
||||
return Vec2f();
|
||||
}
|
||||
|
||||
Color3b BinaryReader::ReadColor3b()
|
||||
{
|
||||
return Color3b();
|
||||
}
|
||||
|
||||
std::string BinaryReader::ReadString()
|
||||
{
|
||||
std::string res;
|
||||
int numChars = ReadInt32();
|
||||
|
||||
for (int i = 0; i < numChars; i++)
|
||||
res += ReadChar();
|
||||
|
||||
return res;
|
||||
}
|
44
ZAPDTR/ZAPDUtils/Utils/BinaryReader.h
Normal file
44
ZAPDTR/ZAPDUtils/Utils/BinaryReader.h
Normal file
|
@ -0,0 +1,44 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "../Color3b.h"
|
||||
#include "../Vec2f.h"
|
||||
#include "../Vec3f.h"
|
||||
#include "../Vec3s.h"
|
||||
#include "Stream.h"
|
||||
|
||||
class BinaryReader
|
||||
{
|
||||
public:
|
||||
BinaryReader(Stream* nStream);
|
||||
BinaryReader(std::shared_ptr<Stream> nStream);
|
||||
|
||||
void Close();
|
||||
|
||||
void Seek(uint32_t offset, SeekOffsetType seekType);
|
||||
uint32_t GetBaseAddress();
|
||||
|
||||
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();
|
||||
Vec3f ReadVec3f();
|
||||
Vec3s ReadVec3s();
|
||||
Vec3s ReadVec3b();
|
||||
Vec2f ReadVec2f();
|
||||
Color3b ReadColor3b();
|
||||
std::string ReadString();
|
||||
|
||||
protected:
|
||||
std::shared_ptr<Stream> stream;
|
||||
};
|
106
ZAPDTR/ZAPDUtils/Utils/BinaryWriter.cpp
Normal file
106
ZAPDTR/ZAPDUtils/Utils/BinaryWriter.cpp
Normal file
|
@ -0,0 +1,106 @@
|
|||
#include "BinaryWriter.h"
|
||||
|
||||
BinaryWriter::BinaryWriter(Stream* nStream)
|
||||
{
|
||||
stream.reset(nStream);
|
||||
}
|
||||
|
||||
BinaryWriter::BinaryWriter(std::shared_ptr<Stream> nStream)
|
||||
{
|
||||
stream = nStream;
|
||||
}
|
||||
|
||||
void BinaryWriter::Close()
|
||||
{
|
||||
stream->Close();
|
||||
}
|
||||
|
||||
std::shared_ptr<Stream> BinaryWriter::GetStream()
|
||||
{
|
||||
return stream;
|
||||
}
|
||||
|
||||
uint64_t BinaryWriter::GetBaseAddress()
|
||||
{
|
||||
return stream->GetBaseAddress();
|
||||
}
|
||||
|
||||
uint64_t BinaryWriter::GetLength()
|
||||
{
|
||||
return stream->GetLength();
|
||||
}
|
||||
|
||||
void BinaryWriter::Seek(int32_t offset, SeekOffsetType seekType)
|
||||
{
|
||||
stream->Seek(offset, seekType);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(int8_t value)
|
||||
{
|
||||
stream->Write((char*)&value, sizeof(int8_t));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(uint8_t value)
|
||||
{
|
||||
stream->Write((char*)&value, sizeof(uint8_t));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(int16_t value)
|
||||
{
|
||||
stream->Write((char*)&value, sizeof(int16_t));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(uint16_t value)
|
||||
{
|
||||
stream->Write((char*)&value, sizeof(uint16_t));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(int32_t value)
|
||||
{
|
||||
stream->Write((char*)&value, sizeof(int32_t));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(int32_t valueA, int32_t valueB)
|
||||
{
|
||||
Write(valueA);
|
||||
Write(valueB);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(uint32_t value)
|
||||
{
|
||||
stream->Write((char*)&value, sizeof(uint32_t));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(int64_t value)
|
||||
{
|
||||
stream->Write((char*)&value, sizeof(int64_t));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(uint64_t value)
|
||||
{
|
||||
stream->Write((char*)&value, sizeof(uint64_t));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(float value)
|
||||
{
|
||||
stream->Write((char*)&value, sizeof(float));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(double value)
|
||||
{
|
||||
stream->Write((char*)&value, sizeof(double));
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(const std::string& str)
|
||||
{
|
||||
int strLen = str.size();
|
||||
stream->Write((char*)&strLen, sizeof(int));
|
||||
|
||||
for (char c : str)
|
||||
stream->WriteByte(c);
|
||||
}
|
||||
|
||||
void BinaryWriter::Write(char* srcBuffer, size_t length)
|
||||
{
|
||||
stream->Write(srcBuffer, length);
|
||||
}
|
37
ZAPDTR/ZAPDUtils/Utils/BinaryWriter.h
Normal file
37
ZAPDTR/ZAPDUtils/Utils/BinaryWriter.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "Stream.h"
|
||||
|
||||
class BinaryWriter
|
||||
{
|
||||
public:
|
||||
BinaryWriter(Stream* nStream);
|
||||
BinaryWriter(std::shared_ptr<Stream> nStream);
|
||||
|
||||
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);
|
||||
|
||||
protected:
|
||||
std::shared_ptr<Stream> stream;
|
||||
};
|
161
ZAPDTR/ZAPDUtils/Utils/BitConverter.h
Normal file
161
ZAPDTR/ZAPDUtils/Utils/BitConverter.h
Normal file
|
@ -0,0 +1,161 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
73
ZAPDTR/ZAPDUtils/Utils/Directory.h
Normal file
73
ZAPDTR/ZAPDUtils/Utils/Directory.h
Normal file
|
@ -0,0 +1,73 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#if __has_include(<filesystem>)
|
||||
#include <filesystem>
|
||||
namespace fs = std::filesystem;
|
||||
#else
|
||||
#include <experimental/filesystem>
|
||||
namespace fs = std::experimental::filesystem;
|
||||
#endif
|
||||
|
||||
#include "StringHelper.h"
|
||||
|
||||
#undef GetCurrentDirectory
|
||||
#undef CreateDirectory
|
||||
|
||||
class Directory
|
||||
{
|
||||
public:
|
||||
#ifndef PATH_HACK
|
||||
static std::string GetCurrentDirectory() { return fs::current_path().u8string().c_str(); }
|
||||
#endif
|
||||
|
||||
static bool Exists(const fs::path& path) { return fs::exists(path); }
|
||||
|
||||
// Stupid hack because of Windows.h
|
||||
static void MakeDirectory(const std::string& path)
|
||||
{
|
||||
CreateDirectory(path);
|
||||
}
|
||||
|
||||
static void CreateDirectory(const std::string& path)
|
||||
{
|
||||
|
||||
#ifdef _MSC_VER
|
||||
std::string splitChar = "\\";
|
||||
#else
|
||||
std::string splitChar = "/";
|
||||
#endif
|
||||
|
||||
std::string curPath;
|
||||
std::vector<std::string> split = StringHelper::Split(path, splitChar);
|
||||
|
||||
for (std::string s : split)
|
||||
{
|
||||
curPath += s + splitChar;
|
||||
|
||||
if (!Exists(curPath))
|
||||
fs::create_directory(curPath);
|
||||
}
|
||||
|
||||
// fs::create_directory(path);
|
||||
}
|
||||
|
||||
static std::vector<std::string> ListFiles(const std::string& dir)
|
||||
{
|
||||
std::vector<std::string> lst;
|
||||
|
||||
if (Directory::Exists(dir))
|
||||
{
|
||||
for (auto& p : fs::recursive_directory_iterator(dir))
|
||||
{
|
||||
if (!p.is_directory())
|
||||
lst.push_back(p.path().string());
|
||||
}
|
||||
}
|
||||
|
||||
return lst;
|
||||
}
|
||||
};
|
87
ZAPDTR/ZAPDUtils/Utils/File.h
Normal file
87
ZAPDTR/ZAPDUtils/Utils/File.h
Normal file
|
@ -0,0 +1,87 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "Path.h"
|
||||
#include "Utils/StringHelper.h"
|
||||
#include "Utils/Directory.h"
|
||||
|
||||
|
||||
class File
|
||||
{
|
||||
public:
|
||||
static bool Exists(const fs::path& filePath)
|
||||
{
|
||||
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
return file.good();
|
||||
}
|
||||
|
||||
static std::vector<uint8_t> ReadAllBytes(const fs::path& filePath)
|
||||
{
|
||||
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
|
||||
if (!file)
|
||||
return std::vector<uint8_t>();
|
||||
|
||||
int32_t fileSize = (int32_t)file.tellg();
|
||||
file.seekg(0);
|
||||
char* data = new char[fileSize];
|
||||
file.read(data, fileSize);
|
||||
std::vector<uint8_t> result = std::vector<uint8_t>(data, data + fileSize);
|
||||
delete[] data;
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
static std::string ReadAllText(const fs::path& filePath)
|
||||
{
|
||||
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
int32_t fileSize = (int32_t)file.tellg();
|
||||
file.seekg(0);
|
||||
char* data = new char[fileSize + 1];
|
||||
memset(data, 0, fileSize + 1);
|
||||
file.read(data, fileSize);
|
||||
std::string str = std::string((const char*)data);
|
||||
delete[] data;
|
||||
|
||||
return str;
|
||||
};
|
||||
|
||||
static std::vector<std::string> ReadAllLines(const fs::path& filePath)
|
||||
{
|
||||
std::string text = ReadAllText(filePath);
|
||||
std::vector<std::string> lines = StringHelper::Split(text, "\n");
|
||||
|
||||
return lines;
|
||||
};
|
||||
|
||||
static void WriteAllBytes(const fs::path& filePath, const std::vector<uint8_t>& data)
|
||||
{
|
||||
std::ofstream file(filePath, std::ios::binary);
|
||||
file.write((char*)data.data(), data.size());
|
||||
};
|
||||
|
||||
static void WriteAllBytes(const std::string& filePath, const std::vector<char>& data)
|
||||
{
|
||||
if (!Directory::Exists(Path::GetDirectoryName(filePath)))
|
||||
Directory::MakeDirectory(Path::GetDirectoryName(filePath).string());
|
||||
|
||||
std::ofstream file(filePath, std::ios::binary);
|
||||
file.write((char*)data.data(), data.size());
|
||||
};
|
||||
|
||||
static void WriteAllBytes(const std::string& filePath, const char* data, int dataSize)
|
||||
{
|
||||
std::ofstream file(filePath, std::ios::binary);
|
||||
file.write((char*)data, dataSize);
|
||||
};
|
||||
|
||||
static void WriteAllText(const fs::path& filePath, const std::string& text)
|
||||
{
|
||||
std::ofstream file(filePath, std::ios::out);
|
||||
file.write(text.c_str(), text.size());
|
||||
}
|
||||
};
|
97
ZAPDTR/ZAPDUtils/Utils/MemoryStream.cpp
Normal file
97
ZAPDTR/ZAPDUtils/Utils/MemoryStream.cpp
Normal file
|
@ -0,0 +1,97 @@
|
|||
#include "MemoryStream.h"
|
||||
#include <cstring>
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#define memcpy_s(dest, destSize, source, sourceSize) memcpy(dest, source, destSize)
|
||||
#endif
|
||||
|
||||
MemoryStream::MemoryStream()
|
||||
{
|
||||
buffer = std::vector<char>();
|
||||
//buffer.reserve(1024 * 16);
|
||||
bufferSize = 0;
|
||||
baseAddress = 0;
|
||||
}
|
||||
|
||||
MemoryStream::MemoryStream(char* nBuffer, size_t nBufferSize) : MemoryStream()
|
||||
{
|
||||
buffer = std::vector<char>(nBuffer, nBuffer + nBufferSize);
|
||||
bufferSize = nBufferSize;
|
||||
baseAddress = 0;
|
||||
}
|
||||
|
||||
MemoryStream::~MemoryStream()
|
||||
{
|
||||
}
|
||||
|
||||
uint64_t MemoryStream::GetLength()
|
||||
{
|
||||
return buffer.size();
|
||||
}
|
||||
|
||||
void MemoryStream::Seek(int32_t offset, SeekOffsetType seekType)
|
||||
{
|
||||
if (seekType == SeekOffsetType::Start)
|
||||
baseAddress = offset;
|
||||
else if (seekType == SeekOffsetType::Current)
|
||||
baseAddress += offset;
|
||||
else if (seekType == SeekOffsetType::End)
|
||||
baseAddress = bufferSize - 1 - offset;
|
||||
}
|
||||
|
||||
std::unique_ptr<char[]> MemoryStream::Read(size_t length)
|
||||
{
|
||||
std::unique_ptr<char[]> result = std::make_unique<char[]>(length);
|
||||
|
||||
memcpy_s(result.get(), length, &buffer[baseAddress], length);
|
||||
baseAddress += length;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void MemoryStream::Read(const char* dest, size_t length)
|
||||
{
|
||||
memcpy_s((void*)dest, length, &buffer[baseAddress], length);
|
||||
baseAddress += length;
|
||||
}
|
||||
|
||||
int8_t MemoryStream::ReadByte()
|
||||
{
|
||||
return buffer[baseAddress++];
|
||||
}
|
||||
|
||||
void MemoryStream::Write(char* srcBuffer, size_t length)
|
||||
{
|
||||
if (baseAddress + length >= buffer.size())
|
||||
{
|
||||
buffer.resize(baseAddress + length);
|
||||
bufferSize += length;
|
||||
}
|
||||
|
||||
memcpy_s(&buffer[baseAddress], length, srcBuffer, length);
|
||||
baseAddress += length;
|
||||
}
|
||||
|
||||
void MemoryStream::WriteByte(int8_t value)
|
||||
{
|
||||
if (baseAddress >= buffer.size())
|
||||
{
|
||||
buffer.resize(baseAddress + 1);
|
||||
bufferSize = baseAddress;
|
||||
}
|
||||
|
||||
buffer[baseAddress++] = value;
|
||||
}
|
||||
|
||||
std::vector<char> MemoryStream::ToVector()
|
||||
{
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void MemoryStream::Flush()
|
||||
{
|
||||
}
|
||||
|
||||
void MemoryStream::Close()
|
||||
{
|
||||
}
|
33
ZAPDTR/ZAPDUtils/Utils/MemoryStream.h
Normal file
33
ZAPDTR/ZAPDUtils/Utils/MemoryStream.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "Stream.h"
|
||||
|
||||
class MemoryStream : public Stream
|
||||
{
|
||||
public:
|
||||
MemoryStream();
|
||||
MemoryStream(char* nBuffer, size_t nBufferSize);
|
||||
~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();
|
||||
|
||||
void Flush() override;
|
||||
void Close() override;
|
||||
|
||||
protected:
|
||||
std::vector<char> buffer;
|
||||
std::size_t bufferSize;
|
||||
};
|
50
ZAPDTR/ZAPDUtils/Utils/Path.h
Normal file
50
ZAPDTR/ZAPDUtils/Utils/Path.h
Normal file
|
@ -0,0 +1,50 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "Utils/StringHelper.h"
|
||||
|
||||
#if __has_include(<filesystem>)
|
||||
#include <filesystem>
|
||||
namespace fs = std::filesystem;
|
||||
#else
|
||||
#include <experimental/filesystem>
|
||||
namespace fs = std::experimental::filesystem;
|
||||
#endif
|
||||
|
||||
class Path
|
||||
{
|
||||
public:
|
||||
static std::string GetFileName(const fs::path& input)
|
||||
{
|
||||
// https://en.cppreference.com/w/cpp/filesystem/path/filename
|
||||
return input.filename().string();
|
||||
};
|
||||
|
||||
static std::string GetFileNameWithoutExtension(const fs::path& input)
|
||||
{
|
||||
// https://en.cppreference.com/w/cpp/filesystem/path/stem
|
||||
return input.stem().string();
|
||||
};
|
||||
|
||||
static std::string GetFileNameExtension(const std::string& input)
|
||||
{
|
||||
return input.substr(input.find_last_of("."), input.length());
|
||||
};
|
||||
|
||||
static fs::path GetPath(const std::string& input)
|
||||
{
|
||||
std::vector<std::string> split = StringHelper::Split(input, "/");
|
||||
fs::path output;
|
||||
|
||||
for (std::string str : split)
|
||||
{
|
||||
if (str.find_last_of(".") == std::string::npos)
|
||||
output /= str;
|
||||
}
|
||||
|
||||
return output;
|
||||
};
|
||||
|
||||
static fs::path GetDirectoryName(const fs::path& path) { return path.parent_path(); };
|
||||
};
|
41
ZAPDTR/ZAPDUtils/Utils/Stream.h
Normal file
41
ZAPDTR/ZAPDUtils/Utils/Stream.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
||||
enum class SeekOffsetType
|
||||
{
|
||||
Start,
|
||||
Current,
|
||||
End
|
||||
};
|
||||
|
||||
// TODO: Eventually account for endianess in binaryreader and binarywriter
|
||||
enum class Endianess
|
||||
{
|
||||
Little = 0,
|
||||
Big = 1,
|
||||
};
|
||||
|
||||
class Stream
|
||||
{
|
||||
public:
|
||||
virtual ~Stream() = default;
|
||||
virtual uint64_t GetLength() = 0;
|
||||
uint64_t GetBaseAddress() { return baseAddress; }
|
||||
|
||||
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 void Flush() = 0;
|
||||
virtual void Close() = 0;
|
||||
|
||||
protected:
|
||||
uint64_t baseAddress;
|
||||
};
|
126
ZAPDTR/ZAPDUtils/Utils/StringHelper.cpp
Normal file
126
ZAPDTR/ZAPDUtils/Utils/StringHelper.cpp
Normal file
|
@ -0,0 +1,126 @@
|
|||
#include "StringHelper.h"
|
||||
|
||||
#pragma optimize("2", on)
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
std::vector<std::string> StringHelper::Split(std::string s, const std::string& delimiter)
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
|
||||
size_t pos = 0;
|
||||
std::string token;
|
||||
|
||||
while ((pos = s.find(delimiter)) != std::string::npos)
|
||||
{
|
||||
token = s.substr(0, pos);
|
||||
result.push_back(token);
|
||||
s.erase(0, pos + delimiter.length());
|
||||
}
|
||||
|
||||
if (s.length() != 0)
|
||||
result.push_back(s);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string StringHelper::Strip(std::string s, const std::string& delimiter)
|
||||
{
|
||||
size_t pos = 0;
|
||||
std::string token;
|
||||
|
||||
while ((pos = s.find(delimiter)) != std::string::npos)
|
||||
{
|
||||
token = s.substr(0, pos);
|
||||
s.erase(pos, pos + delimiter.length());
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string StringHelper::Replace(std::string str, const std::string& from,
|
||||
const std::string& to)
|
||||
{
|
||||
size_t start_pos = str.find(from);
|
||||
|
||||
while (start_pos != std::string::npos)
|
||||
{
|
||||
str.replace(start_pos, from.length(), to);
|
||||
start_pos = str.find(from);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
void StringHelper::ReplaceOriginal(std::string& str, const std::string& from, const std::string& to)
|
||||
{
|
||||
size_t start_pos = str.find(from);
|
||||
|
||||
while (start_pos != std::string::npos)
|
||||
{
|
||||
str.replace(start_pos, from.length(), to);
|
||||
start_pos = str.find(from);
|
||||
}
|
||||
}
|
||||
|
||||
bool StringHelper::StartsWith(const std::string& s, const std::string& input)
|
||||
{
|
||||
return s.rfind(input, 0) == 0;
|
||||
}
|
||||
|
||||
bool StringHelper::Contains(const std::string& s, const std::string& input)
|
||||
{
|
||||
return s.find(input) != std::string::npos;
|
||||
}
|
||||
|
||||
bool StringHelper::EndsWith(const std::string& s, const std::string& input)
|
||||
{
|
||||
size_t inputLen = strlen(input.c_str());
|
||||
return s.rfind(input) == (s.size() - inputLen);
|
||||
}
|
||||
|
||||
std::string StringHelper::Sprintf(const char* format, ...)
|
||||
{
|
||||
char buffer[32768];
|
||||
// char buffer[2048];
|
||||
std::string output;
|
||||
va_list va;
|
||||
|
||||
va_start(va, format);
|
||||
vsprintf_s(buffer, format, va);
|
||||
va_end(va);
|
||||
|
||||
output = buffer;
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string StringHelper::Implode(std::vector<std::string>& elements,
|
||||
const char* const separator)
|
||||
{
|
||||
return "";
|
||||
|
||||
// return std::accumulate(std::begin(elements), std::end(elements), std::string(),
|
||||
//[separator](std::string& ss, std::string& s) {
|
||||
// return ss.empty() ? s : ss + separator + s;
|
||||
//});
|
||||
}
|
||||
|
||||
int64_t StringHelper::StrToL(const std::string& str, int32_t base)
|
||||
{
|
||||
return std::strtoull(str.c_str(), nullptr, base);
|
||||
}
|
||||
|
||||
std::string StringHelper::BoolStr(bool b)
|
||||
{
|
||||
return b ? "true" : "false";
|
||||
}
|
||||
|
||||
bool StringHelper::HasOnlyDigits(const std::string& str)
|
||||
{
|
||||
return std::all_of(str.begin(), str.end(), ::isdigit);
|
||||
}
|
||||
|
||||
bool StringHelper::IEquals(const std::string& a, const std::string& b)
|
||||
{
|
||||
return std::equal(a.begin(), a.end(), b.begin(), b.end(),
|
||||
[](char a, char b) { return tolower(a) == tolower(b); });
|
||||
}
|
26
ZAPDTR/ZAPDUtils/Utils/StringHelper.h
Normal file
26
ZAPDTR/ZAPDUtils/Utils/StringHelper.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdarg>
|
||||
#include <cstring>
|
||||
#include <numeric>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class StringHelper
|
||||
{
|
||||
public:
|
||||
static std::vector<std::string> Split(std::string s, const std::string& delimiter);
|
||||
static std::string Strip(std::string s, const std::string& delimiter);
|
||||
static std::string Replace(std::string str, const std::string& from, const std::string& to);
|
||||
static void ReplaceOriginal(std::string& str, const std::string& from, const std::string& to);
|
||||
static bool StartsWith(const std::string& s, const std::string& input);
|
||||
static bool Contains(const std::string& s, const std::string& input);
|
||||
static bool EndsWith(const std::string& s, const std::string& input);
|
||||
static std::string Sprintf(const char* format, ...);
|
||||
static std::string Implode(std::vector<std::string>& elements, const char* const separator);
|
||||
static int64_t StrToL(const std::string& str, int32_t base = 10);
|
||||
static std::string BoolStr(bool b);
|
||||
static bool HasOnlyDigits(const std::string& str);
|
||||
static bool IEquals(const std::string& a, const std::string& b);
|
||||
};
|
45
ZAPDTR/ZAPDUtils/Utils/vt.h
Normal file
45
ZAPDTR/ZAPDUtils/Utils/vt.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
#ifndef VT_H
|
||||
#define VT_H
|
||||
|
||||
// clang-format off
|
||||
#define VT_COLOR_BLACK 0
|
||||
#define VT_COLOR_RED 1
|
||||
#define VT_COLOR_GREEN 2
|
||||
#define VT_COLOR_YELLOW 3
|
||||
#define VT_COLOR_BLUE 4
|
||||
#define VT_COLOR_PURPLE 5
|
||||
#define VT_COLOR_CYAN 6
|
||||
#define VT_COLOR_WHITE 7
|
||||
#define VT_COLOR_LIGHTGRAY 8
|
||||
#define VT_COLOR_DARKGRAY 9
|
||||
|
||||
#define VT_COLOR_FOREGROUND 3
|
||||
#define VT_COLOR_BACKGROUND 4
|
||||
// clang-format on
|
||||
|
||||
#define VT_COLOR_EXPAND0(type, color) #type #color
|
||||
#define VT_COLOR_EXPAND1(type, color) VT_COLOR_EXPAND0(type, color)
|
||||
#define VT_COLOR(type, color) VT_COLOR_EXPAND1(VT_COLOR_##type, VT_COLOR_##color)
|
||||
|
||||
#define VT_ESC "\x1b"
|
||||
#define VT_CSI "["
|
||||
#define VT_CUP(x, y) VT_ESC VT_CSI y ";" x "H"
|
||||
#define VT_ED(n) VT_ESC VT_CSI #n "J"
|
||||
#define VT_SGR(n) VT_ESC VT_CSI n "m"
|
||||
|
||||
// Add more macros if necessary
|
||||
#define VT_COL(back, fore) VT_SGR(VT_COLOR(BACKGROUND, back) ";" VT_COLOR(FOREGROUND, fore))
|
||||
#define VT_FGCOL(color) VT_SGR(VT_COLOR(FOREGROUND, color))
|
||||
#define VT_BGCOL(color) VT_SGR(VT_COLOR(BACKGROUND, color))
|
||||
|
||||
// Bold
|
||||
#define VT_BOLD "1"
|
||||
|
||||
// Bold color support
|
||||
#define VT_BOLD_FGCOL(color) VT_SGR(VT_BOLD ";" VT_COLOR(FOREGROUND, color))
|
||||
#define VT_BOLD_BGCOL(color) VT_SGR(VT_BOLD ";" VT_COLOR(BACKGROUND, color))
|
||||
|
||||
#define VT_RST VT_SGR("")
|
||||
#define VT_CLS VT_ED(2)
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue