Skip to content

Commit

Permalink
Merge branch 'main' into PetFixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jadebenn committed Dec 27, 2023
2 parents c6df078 + ffa2f99 commit 60b3f3c
Show file tree
Hide file tree
Showing 80 changed files with 2,568 additions and 2,235 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
[submodule "thirdparty/AccountManager"]
path = thirdparty/AccountManager
url = https://github.com/DarkflameUniverse/AccountManager
[submodule "thirdparty/magic_enum"]
path = thirdparty/magic_enum
url = https://github.com/Neargye/magic_enum.git
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ set(INCLUDED_DIRECTORIES
"dScripts/ai/GENERAL"
"dScripts/ai/GF"
"dScripts/ai/MINIGAME"
"dScripts/ai/MINIGAME/Objects"
"dScripts/ai/NP"
"dScripts/ai/NS"
"dScripts/ai/PETS"
Expand All @@ -292,6 +293,7 @@ set(INCLUDED_DIRECTORIES
"dScripts/zone/PROPERTY/GF"
"dScripts/zone/PROPERTY/NS"

"thirdparty/magic_enum/include/magic_enum"
"thirdparty/raknet/Source"
"thirdparty/tinyxml2"
"thirdparty/recastnavigation"
Expand Down Expand Up @@ -373,7 +375,7 @@ add_subdirectory(dNavigation)
add_subdirectory(dPhysics)

# Create a list of common libraries shared between all binaries
set(COMMON_LIBRARIES "dCommon" "dDatabase" "dNet" "raknet" "mariadbConnCpp")
set(COMMON_LIBRARIES "dCommon" "dDatabase" "dNet" "raknet" "mariadbConnCpp" "magic_enum")

# Add platform specific common libraries
if (UNIX)
Expand Down
41 changes: 0 additions & 41 deletions dCommon/BinaryIO.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include "BinaryIO.h"
#include <string>

void BinaryIO::WriteString(const std::string& stringToWrite, std::ofstream& outstream) {
//BinaryWrite(outstream, uint32_t(stringToWrite.length()));

for (size_t i = 0; i < size_t(stringToWrite.length()); ++i) {
BinaryIO::BinaryWrite(outstream, stringToWrite[i]);
}
}

//For reading null-terminated strings
std::string BinaryIO::ReadString(std::istream& instream) {
std::string toReturn;
Expand All @@ -23,36 +15,3 @@ std::string BinaryIO::ReadString(std::istream& instream) {

return toReturn;
}

//For reading strings of a specific size
std::string BinaryIO::ReadString(std::istream& instream, size_t size) {
std::string toReturn;
char buffer;

for (size_t i = 0; i < size; ++i) {
BinaryIO::BinaryRead(instream, buffer);
toReturn += buffer;
}

return toReturn;
}

std::string BinaryIO::ReadWString(std::istream& instream) {
size_t size;
BinaryRead(instream, size);
//toReturn.resize(size);
std::string test;
unsigned char buf;

for (size_t i = 0; i < size; ++i) {
//instream.ignore(1);
BinaryRead(instream, buf);
test += buf;
}

//printf("%s\n", test.c_str());

//instream.read((char*)&toReturn[0], size * 2);
//std::string str(toReturn.begin(), toReturn.end());
return test;
}
53 changes: 50 additions & 3 deletions dCommon/BinaryIO.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#pragma once

#ifndef __BINARYIO__H__
#define __BINARYIO__H__

#include <iostream>
#include <fstream>
#include <string>

#include "Game.h"
#include "Logger.h"

namespace BinaryIO {

template<typename T>
std::ostream& BinaryWrite(std::ostream& stream, const T& value) {
return stream.write(reinterpret_cast<const char*>(&value), sizeof(T));
Expand All @@ -15,13 +24,51 @@ namespace BinaryIO {
return stream.read(reinterpret_cast<char*>(&value), sizeof(T));
}

void WriteString(const std::string& stringToWrite, std::ofstream& outstream);
enum class ReadType : int8_t {
WideString = 0,
String = 1,
};

template<typename SizeType>
inline void ReadString(std::istream& stream, std::u16string& value) {
static_assert(std::is_integral<SizeType>::value, "SizeType must be an integral type.");

SizeType size;
BinaryRead(stream, size);

if (!stream.good()) throw std::runtime_error("Failed to read from istream.");
value.resize(size);
stream.read(reinterpret_cast<char*>(value.data()), size * sizeof(uint16_t));
}

template<typename SizeType>
inline void ReadString(std::istream& stream, std::string& value, ReadType readType) {
static_assert(std::is_integral<SizeType>::value, "SizeType must be an integral type.");

SizeType size;
BinaryRead(stream, size);

if (!stream.good()) throw std::runtime_error("Failed to read from istream.");
value.resize(size);
if (readType == ReadType::WideString) {
uint16_t wideChar;

// Faster to do this than to read a u16string and convert it to a string since we only go through allocator once
for (SizeType i = 0; i < size; ++i) {
BinaryRead(stream, wideChar);
value[i] = static_cast<char>(wideChar);
}
} else {
stream.read(value.data(), size);
}
}

std::string ReadString(std::istream& instream);
std::string ReadString(std::istream& instream, size_t size);
std::string ReadWString(std::istream& instream);

inline bool DoesFileExist(const std::string& name) {
std::ifstream f(name.c_str());
return f.good();
}
}

#endif //!__BINARYIO__H__
8 changes: 3 additions & 5 deletions dCommon/FdbToSqlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@ FdbToSqlite::Convert::Convert(std::string binaryOutPath) {
this->m_BinaryOutPath = binaryOutPath;
}

bool FdbToSqlite::Convert::ConvertDatabase(AssetMemoryBuffer& buffer) {
bool FdbToSqlite::Convert::ConvertDatabase(AssetStream& buffer) {
if (m_ConversionStarted) return false;

std::istream cdClientBuffer(&buffer);

this->m_ConversionStarted = true;
try {
CDClientDatabase::Connect(m_BinaryOutPath + "/CDServer.sqlite");

CDClientDatabase::ExecuteQuery("BEGIN TRANSACTION;");

int32_t numberOfTables = ReadInt32(cdClientBuffer);
ReadTables(numberOfTables, cdClientBuffer);
int32_t numberOfTables = ReadInt32(buffer);
ReadTables(numberOfTables, buffer);

CDClientDatabase::ExecuteQuery("COMMIT;");
} catch (CppSQLite3Exception& e) {
Expand Down
4 changes: 2 additions & 2 deletions dCommon/FdbToSqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <iosfwd>
#include <map>

class AssetMemoryBuffer;
#include "AssetManager.h"

enum class eSqliteDataType : int32_t;

Expand All @@ -27,7 +27,7 @@ namespace FdbToSqlite {
*
* @return true if the database was converted properly, false otherwise.
*/
bool ConvertDatabase(AssetMemoryBuffer& buffer);
bool ConvertDatabase(AssetStream& buffer);

/**
* @brief Reads a 32 bit int from the fdb file.
Expand Down
26 changes: 19 additions & 7 deletions dCommon/GeneralUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,30 @@ namespace GeneralUtils {
return T();
}

// on Windows we need to undef these or else they conflict with our numeric limits calls
// DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS
#ifdef _WIN32
#undef min
#undef max
#endif
/**
* Casts the value of an enum entry to its underlying type
* @param entry Enum entry to cast
* @returns The enum entry's value in its underlying type
*/
template <typename eType>
inline constexpr typename std::underlying_type_t<eType> CastUnderlyingType(const eType entry) {
static_assert(std::is_enum_v<eType>, "Not an enum");

return static_cast<typename std::underlying_type_t<eType>>(entry);
}

// on Windows we need to undef these or else they conflict with our numeric limits calls
// DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS DEVELOPERS
#ifdef _WIN32
#undef min
#undef max
#endif

template <typename T>
inline T GenerateRandomNumber() {
// Make sure it is a numeric type
static_assert(std::is_arithmetic<T>::value, "Not an arithmetic type");

return GenerateRandomNumber<T>(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
}
}
7 changes: 3 additions & 4 deletions dCommon/dClient/AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,12 @@ bool AssetManager::GetFile(const char* name, char** data, uint32_t* len) {
return success;
}

AssetMemoryBuffer AssetManager::GetFileAsBuffer(const char* name) {
char* buf;
uint32_t len;
AssetStream AssetManager::GetFile(const char* name) {
char* buf; uint32_t len;

bool success = this->GetFile(name, &buf, &len);

return AssetMemoryBuffer(buf, len, success);
return AssetStream(buf, len, success);
}

uint32_t AssetManager::crc32b(uint32_t base, uint8_t* message, size_t l) {
Expand Down
18 changes: 15 additions & 3 deletions dCommon/dClient/AssetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ struct AssetMemoryBuffer : std::streambuf {
this->setg(base, base, base + n);
}

~AssetMemoryBuffer() {
if (m_Success) free(m_Base);
}

pos_type seekpos(pos_type sp, std::ios_base::openmode which) override {
return seekoff(sp - pos_type(off_type(0)), std::ios_base::beg, which);
}
Expand All @@ -40,9 +44,17 @@ struct AssetMemoryBuffer : std::streambuf {
setg(eback(), eback() + off, egptr());
return gptr() - eback();
}
};

struct AssetStream : std::istream {
AssetStream(char* base, std::ptrdiff_t n, bool success) : std::istream(new AssetMemoryBuffer(base, n, success)) {}

~AssetStream() {
delete rdbuf();
}

void close() {
free(m_Base);
operator bool() {
return reinterpret_cast<AssetMemoryBuffer*>(rdbuf())->m_Success;
}
};

Expand All @@ -56,7 +68,7 @@ class AssetManager {

bool HasFile(const char* name);
bool GetFile(const char* name, char** data, uint32_t* len);
AssetMemoryBuffer GetFileAsBuffer(const char* name);
AssetStream GetFile(const char* name);

private:
void LoadPackIndex();
Expand Down
19 changes: 4 additions & 15 deletions dCommon/dClient/PackIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,10 @@ PackIndex::PackIndex(const std::filesystem::path& filePath) {

BinaryIO::BinaryRead<uint32_t>(m_FileStream, m_Version);
BinaryIO::BinaryRead<uint32_t>(m_FileStream, m_PackPathCount);

for (int i = 0; i < m_PackPathCount; i++) {
uint32_t stringLen = 0;
BinaryIO::BinaryRead<uint32_t>(m_FileStream, stringLen);

std::string path;

for (int j = 0; j < stringLen; j++) {
char inChar;
BinaryIO::BinaryRead<char>(m_FileStream, inChar);

path += inChar;
}

m_PackPaths.push_back(path);

m_PackPaths.resize(m_PackPathCount);
for (auto& item : m_PackPaths) {
BinaryIO::ReadString<uint32_t>(m_FileStream, item, BinaryIO::ReadType::String);
}

BinaryIO::BinaryRead<uint32_t>(m_FileStream, m_PackFileIndexCount);
Expand Down
29 changes: 29 additions & 0 deletions dCommon/dEnums/StringifiedEnum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef __STRINGIFIEDENUM_H__
#define __STRINGIFIEDENUM_H__

#include <string>
#include "magic_enum.hpp"

namespace StringifiedEnum {
template<typename T>
const std::string_view ToString(const T e) {
static_assert(std::is_enum_v<T>, "Not an enum"); // Check type

constexpr auto sv = &magic_enum::enum_entries<T>();
std::string_view output;

const auto it = std::lower_bound(
sv->begin(), sv->end(), e,
[&](const std::pair<T, std::string_view>& lhs, const T rhs) { return lhs.first < rhs; }
);

if (it != sv->end() && it->first == e) {
output = it->second;
} else {
output = "UNKNOWN";
}
return output;
}
}

#endif // !__STRINGIFIEDENUM_H__
Loading

0 comments on commit 60b3f3c

Please sign in to comment.