mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-08-04 09:44:10 +00:00
Add tests for LDF parsing and serialization. Cleanup LDF (#1062)
* Add tests and cleanup LDF header Also implements a speedup by using overloaded operators to put numbers directly to a stream as opposed to doing to_string first. Stage 2 of re-write Reduce scoping Add further optimizations Fix more edge cases Split out tests to many smaller ones Use EXPECT_NO_THROW Add edge cases to test Added these first with the before to confirm they failed, and now will be adding the remaining fixes needed to make the tests pass. Add edge case testing for LDF strings Add further tests Use characters instead of char* Update AMFDeserializeTests.cpp Add null tests * Add Test Fixture for dCommon * Add speed test * Convert to using string_view * Add explanation on early return * Remove "testing" code
This commit is contained in:
@@ -3,122 +3,174 @@
|
||||
// Custom Classes
|
||||
#include "GeneralUtils.h"
|
||||
|
||||
#include "Game.h"
|
||||
#include "dLogger.h"
|
||||
|
||||
// C++
|
||||
#include <sstream>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
using LDFKey = std::string_view;
|
||||
using LDFTypeAndValue = std::string_view;
|
||||
|
||||
using LDFType = std::string_view;
|
||||
using LDFValue = std::string_view;
|
||||
|
||||
//! Returns a pointer to a LDFData value based on string format
|
||||
LDFBaseData* LDFBaseData::DataFromString(const std::string& format) {
|
||||
LDFBaseData* LDFBaseData::DataFromString(const std::string_view& format) {
|
||||
// A valid LDF must be at least 3 characters long (=0:) is the shortest valid LDF (empty UTF-16 key with no initial value)
|
||||
if (format.empty() || format.length() <= 2) return nullptr;
|
||||
auto equalsPosition = format.find('=');
|
||||
// You can have an empty key, just make sure the type and value might exist
|
||||
if (equalsPosition == std::string::npos || equalsPosition == (format.size() - 1)) return nullptr;
|
||||
|
||||
// First, check the format
|
||||
std::istringstream ssFormat(format);
|
||||
std::string token;
|
||||
std::pair<LDFKey, LDFTypeAndValue> keyValue;
|
||||
keyValue.first = format.substr(0, equalsPosition);
|
||||
keyValue.second = format.substr(equalsPosition + 1, format.size());
|
||||
|
||||
std::vector<std::string> keyValueArray;
|
||||
while (std::getline(ssFormat, token, '=')) {
|
||||
keyValueArray.push_back(token);
|
||||
std::u16string key = GeneralUtils::ASCIIToUTF16(keyValue.first);
|
||||
|
||||
auto colonPosition = keyValue.second.find(':');
|
||||
|
||||
// If : is the first thing after an =, then this is an invalid LDF since
|
||||
// we dont have a type to use.
|
||||
if (colonPosition == std::string::npos || colonPosition == 0) return nullptr;
|
||||
|
||||
std::pair<LDFType, LDFValue> ldfTypeAndValue;
|
||||
ldfTypeAndValue.first = keyValue.second.substr(0, colonPosition);
|
||||
ldfTypeAndValue.second = keyValue.second.substr(colonPosition + 1, keyValue.second.size());
|
||||
|
||||
// Only allow empty values for string values.
|
||||
if (ldfTypeAndValue.second.size() == 0 && !(ldfTypeAndValue.first == "0" || ldfTypeAndValue.first == "13")) return nullptr;
|
||||
|
||||
eLDFType type;
|
||||
char* storage;
|
||||
try {
|
||||
type = static_cast<eLDFType>(strtol(ldfTypeAndValue.first.data(), &storage, 10));
|
||||
} catch (std::exception) {
|
||||
Game::logger->Log("LDFFormat", "Attempted to process invalid ldf type (%s) from string (%s)", ldfTypeAndValue.first.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (keyValueArray.size() == 2) {
|
||||
std::u16string key = GeneralUtils::ASCIIToUTF16(keyValueArray[0]);
|
||||
LDFBaseData* returnValue = nullptr;
|
||||
switch (type) {
|
||||
case LDF_TYPE_UTF_16: {
|
||||
std::u16string data = GeneralUtils::UTF8ToUTF16(ldfTypeAndValue.second);
|
||||
returnValue = new LDFData<std::u16string>(key, data);
|
||||
break;
|
||||
}
|
||||
|
||||
std::vector<std::string> dataArray;
|
||||
std::istringstream ssData(keyValueArray[1]);
|
||||
while (std::getline(ssData, token, ':')) {
|
||||
dataArray.push_back(token);
|
||||
case LDF_TYPE_S32: {
|
||||
try {
|
||||
int32_t data = static_cast<int32_t>(strtoul(ldfTypeAndValue.second.data(), &storage, 10));
|
||||
returnValue = new LDFData<int32_t>(key, data);
|
||||
} catch (std::exception) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid int32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (dataArray.size() > 2) { // hacky fix for strings with colons in them
|
||||
std::vector<std::string> newDataArray;
|
||||
newDataArray.push_back(dataArray[0]);
|
||||
std::string value = "";
|
||||
for (size_t i = 1; i < dataArray.size(); ++i) {
|
||||
value += dataArray[i] + ':';
|
||||
}
|
||||
value.pop_back(); // remove last colon
|
||||
newDataArray.push_back(value);
|
||||
dataArray = newDataArray;
|
||||
case LDF_TYPE_FLOAT: {
|
||||
try {
|
||||
float data = strtof(ldfTypeAndValue.second.data(), &storage);
|
||||
returnValue = new LDFData<float>(key, data);
|
||||
} catch (std::exception) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid float value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if ((dataArray[0] == "0" || dataArray[0] == "13") && dataArray.size() == 1) {
|
||||
dataArray.push_back("");
|
||||
case LDF_TYPE_DOUBLE: {
|
||||
try {
|
||||
double data = strtod(ldfTypeAndValue.second.data(), &storage);
|
||||
returnValue = new LDFData<double>(key, data);
|
||||
} catch (std::exception) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid double value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (dataArray.size() == 2) {
|
||||
eLDFType type = static_cast<eLDFType>(stoi(dataArray[0]));
|
||||
case LDF_TYPE_U32:
|
||||
{
|
||||
uint32_t data;
|
||||
|
||||
switch (type) {
|
||||
case LDF_TYPE_UTF_16: {
|
||||
std::u16string data = GeneralUtils::UTF8ToUTF16(dataArray[1]);
|
||||
return new LDFData<std::u16string>(key, data);
|
||||
}
|
||||
|
||||
case LDF_TYPE_S32: {
|
||||
int32_t data = static_cast<int32_t>(stoull(dataArray[1]));
|
||||
return new LDFData<int32_t>(key, data);
|
||||
}
|
||||
|
||||
case LDF_TYPE_FLOAT: {
|
||||
float data = static_cast<float>(stof(dataArray[1]));
|
||||
return new LDFData<float>(key, data);
|
||||
}
|
||||
|
||||
case LDF_TYPE_DOUBLE: {
|
||||
double data = static_cast<float>(stod(dataArray[1]));
|
||||
return new LDFData<double>(key, data);
|
||||
}
|
||||
|
||||
case LDF_TYPE_U32:
|
||||
{
|
||||
uint32_t data;
|
||||
|
||||
if (dataArray[1] == "true") {
|
||||
data = 1;
|
||||
} else if (dataArray[1] == "false") {
|
||||
data = 0;
|
||||
} else {
|
||||
data = static_cast<uint32_t>(stoul(dataArray[1]));
|
||||
}
|
||||
|
||||
return new LDFData<uint32_t>(key, data);
|
||||
}
|
||||
|
||||
case LDF_TYPE_BOOLEAN: {
|
||||
bool data;
|
||||
|
||||
if (dataArray[1] == "true") {
|
||||
data = true;
|
||||
} else if (dataArray[1] == "false") {
|
||||
data = false;
|
||||
} else {
|
||||
data = static_cast<bool>(stoi(dataArray[1]));
|
||||
}
|
||||
|
||||
return new LDFData<bool>(key, data);
|
||||
}
|
||||
|
||||
case LDF_TYPE_U64: {
|
||||
uint64_t data = static_cast<uint64_t>(stoull(dataArray[1]));
|
||||
return new LDFData<uint64_t>(key, data);
|
||||
}
|
||||
|
||||
case LDF_TYPE_OBJID: {
|
||||
LWOOBJID data = static_cast<LWOOBJID>(stoll(dataArray[1]));
|
||||
return new LDFData<LWOOBJID>(key, data);
|
||||
}
|
||||
|
||||
case LDF_TYPE_UTF_8: {
|
||||
std::string data = dataArray[1];
|
||||
return new LDFData<std::string>(key, data);
|
||||
}
|
||||
|
||||
case LDF_TYPE_UNKNOWN: {
|
||||
if (ldfTypeAndValue.second == "true") {
|
||||
data = 1;
|
||||
} else if (ldfTypeAndValue.second == "false") {
|
||||
data = 0;
|
||||
} else {
|
||||
try {
|
||||
data = static_cast<uint32_t>(strtoul(ldfTypeAndValue.second.data(), &storage, 10));
|
||||
} catch (std::exception) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid uint32 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
returnValue = new LDFData<uint32_t>(key, data);
|
||||
break;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
case LDF_TYPE_BOOLEAN: {
|
||||
bool data;
|
||||
|
||||
if (ldfTypeAndValue.second == "true") {
|
||||
data = true;
|
||||
} else if (ldfTypeAndValue.second == "false") {
|
||||
data = false;
|
||||
} else {
|
||||
try {
|
||||
data = static_cast<bool>(strtol(ldfTypeAndValue.second.data(), &storage, 10));
|
||||
} catch (std::exception) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid bool value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
returnValue = new LDFData<bool>(key, data);
|
||||
break;
|
||||
}
|
||||
|
||||
case LDF_TYPE_U64: {
|
||||
try {
|
||||
uint64_t data = static_cast<uint64_t>(strtoull(ldfTypeAndValue.second.data(), &storage, 10));
|
||||
returnValue = new LDFData<uint64_t>(key, data);
|
||||
} catch (std::exception) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid uint64 value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case LDF_TYPE_OBJID: {
|
||||
try {
|
||||
LWOOBJID data = static_cast<LWOOBJID>(strtoll(ldfTypeAndValue.second.data(), &storage, 10));
|
||||
returnValue = new LDFData<LWOOBJID>(key, data);
|
||||
} catch (std::exception) {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid LWOOBJID value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
return nullptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case LDF_TYPE_UTF_8: {
|
||||
std::string data = ldfTypeAndValue.second.data();
|
||||
returnValue = new LDFData<std::string>(key, data);
|
||||
break;
|
||||
}
|
||||
|
||||
case LDF_TYPE_UNKNOWN: {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid unknown value (%s) from string (%s)", ldfTypeAndValue.second.data(), format.data());
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
Game::logger->Log("LDFFormat", "Warning: Attempted to process invalid LDF type (%d) from string (%s)", type, format.data());
|
||||
break;
|
||||
}
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#ifndef __LDFFORMAT__H__
|
||||
#define __LDFFORMAT__H__
|
||||
|
||||
// Custom Classes
|
||||
#include "dCommonVars.h"
|
||||
@@ -6,18 +7,12 @@
|
||||
|
||||
// C++
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <sstream>
|
||||
|
||||
// RakNet
|
||||
#include "BitStream.h"
|
||||
|
||||
#include "../thirdparty/raknet/Source/BitStream.h"
|
||||
|
||||
/*!
|
||||
\file LDFFormat.hpp
|
||||
\brief A collection of LDF format classes
|
||||
*/
|
||||
|
||||
//! An enum for LDF Data Types
|
||||
enum eLDFType {
|
||||
LDF_TYPE_UNKNOWN = -1, //!< Unknown data type
|
||||
LDF_TYPE_UTF_16 = 0, //!< UTF-16 wstring data type
|
||||
@@ -31,36 +26,21 @@ enum eLDFType {
|
||||
LDF_TYPE_UTF_8 = 13, //!< UTF-8 string data type
|
||||
};
|
||||
|
||||
//! A base class for the LDF data
|
||||
class LDFBaseData {
|
||||
public:
|
||||
|
||||
//! Destructor
|
||||
virtual ~LDFBaseData(void) {}
|
||||
virtual ~LDFBaseData() {}
|
||||
|
||||
//! Writes the data to a packet
|
||||
/*!
|
||||
\param packet The packet
|
||||
*/
|
||||
virtual void WriteToPacket(RakNet::BitStream* packet) = 0;
|
||||
|
||||
//! Gets the key
|
||||
/*!
|
||||
\return The key
|
||||
*/
|
||||
virtual const std::u16string& GetKey(void) = 0;
|
||||
virtual const std::u16string& GetKey() = 0;
|
||||
|
||||
//! Gets the value type
|
||||
/*!
|
||||
\return The value type
|
||||
*/
|
||||
virtual eLDFType GetValueType(void) = 0;
|
||||
virtual eLDFType GetValueType() = 0;
|
||||
|
||||
//! Gets a string from the key/value pair
|
||||
/*!
|
||||
\param includeKey Whether or not to include the key in the data
|
||||
\param includeTypeId Whether or not to include the type id in the data
|
||||
\return The string representation of the data
|
||||
/** Gets a string from the key/value pair
|
||||
* @param includeKey Whether or not to include the key in the data
|
||||
* @param includeTypeId Whether or not to include the type id in the data
|
||||
* @return The string representation of the data
|
||||
*/
|
||||
virtual std::string GetString(bool includeKey = true, bool includeTypeId = true) = 0;
|
||||
|
||||
@@ -68,19 +48,15 @@ public:
|
||||
|
||||
virtual LDFBaseData* Copy() = 0;
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
//! Returns a pointer to a LDFData value based on string format
|
||||
/*!
|
||||
\param format The format
|
||||
/**
|
||||
* Given an input string, return the data as a LDF key.
|
||||
*/
|
||||
static LDFBaseData* DataFromString(const std::string& format);
|
||||
static LDFBaseData* DataFromString(const std::string_view& format);
|
||||
|
||||
};
|
||||
|
||||
//! A structure for an LDF key-value pair
|
||||
template<typename T>
|
||||
class LDFData : public LDFBaseData {
|
||||
class LDFData: public LDFBaseData {
|
||||
private:
|
||||
std::u16string key;
|
||||
T value;
|
||||
@@ -164,15 +140,11 @@ public:
|
||||
|
||||
if (includeKey) {
|
||||
const std::string& sKey = GeneralUtils::UTF16ToWTF8(this->key, this->key.size());
|
||||
|
||||
stream << sKey << "=";
|
||||
stream << sKey << '=';
|
||||
}
|
||||
|
||||
if (includeTypeId) {
|
||||
const std::string& sType = std::to_string(this->GetValueType());
|
||||
|
||||
|
||||
stream << sType << ":";
|
||||
stream << this->GetValueType() << ':';
|
||||
}
|
||||
|
||||
const std::string& sData = this->GetValueString();
|
||||
@@ -234,20 +206,18 @@ inline void LDFData<std::string>::WriteValue(RakNet::BitStream* packet) {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: String Data
|
||||
template<> inline std::string LDFData<std::u16string>::GetValueString(void) {
|
||||
//std::string toReturn(this->value.begin(), this->value.end());
|
||||
//return toReturn;
|
||||
|
||||
template<> inline std::string LDFData<std::u16string>::GetValueString() {
|
||||
return GeneralUtils::UTF16ToWTF8(this->value, this->value.size());
|
||||
}
|
||||
|
||||
template<> inline std::string LDFData<int32_t>::GetValueString(void) { return std::to_string(this->value); }
|
||||
template<> inline std::string LDFData<float>::GetValueString(void) { return std::to_string(this->value); }
|
||||
template<> inline std::string LDFData<double>::GetValueString(void) { return std::to_string(this->value); }
|
||||
template<> inline std::string LDFData<uint32_t>::GetValueString(void) { return std::to_string(this->value); }
|
||||
template<> inline std::string LDFData<bool>::GetValueString(void) { return std::to_string(this->value); }
|
||||
template<> inline std::string LDFData<uint64_t>::GetValueString(void) { return std::to_string(this->value); }
|
||||
template<> inline std::string LDFData<LWOOBJID>::GetValueString(void) { return std::to_string(this->value); }
|
||||
template<> inline std::string LDFData<int32_t>::GetValueString() { return std::to_string(this->value); }
|
||||
template<> inline std::string LDFData<float>::GetValueString() { return std::to_string(this->value); }
|
||||
template<> inline std::string LDFData<double>::GetValueString() { return std::to_string(this->value); }
|
||||
template<> inline std::string LDFData<uint32_t>::GetValueString() { return std::to_string(this->value); }
|
||||
template<> inline std::string LDFData<bool>::GetValueString() { return std::to_string(this->value); }
|
||||
template<> inline std::string LDFData<uint64_t>::GetValueString() { return std::to_string(this->value); }
|
||||
template<> inline std::string LDFData<LWOOBJID>::GetValueString() { return std::to_string(this->value); }
|
||||
|
||||
template<> inline std::string LDFData<std::string>::GetValueString(void) { return this->value; }
|
||||
template<> inline std::string LDFData<std::string>::GetValueString() { return this->value; }
|
||||
|
||||
#endif //!__LDFFORMAT__H__
|
||||
|
Reference in New Issue
Block a user