Dynamic GM sending proof of concept

This commit is contained in:
Jettford 2024-01-10 07:01:02 +00:00
parent e0ddbce8e7
commit e55a2e36e6
9 changed files with 5891 additions and 24 deletions

3
.gitmodules vendored
View File

@ -17,3 +17,6 @@
[submodule "thirdparty/magic_enum"]
path = thirdparty/magic_enum
url = https://github.com/Neargye/magic_enum.git
[submodule "thirdparty/json"]
path = thirdparty/json
url = https://github.com/nlohmann/json

View File

@ -96,7 +96,7 @@ make_directory(${CMAKE_BINARY_DIR}/resServer)
make_directory(${CMAKE_BINARY_DIR}/logs)
# Copy resource files on first build
set(RESOURCE_FILES "sharedconfig.ini" "authconfig.ini" "chatconfig.ini" "worldconfig.ini" "masterconfig.ini" "blacklist.dcf")
set(RESOURCE_FILES "sharedconfig.ini" "authconfig.ini" "chatconfig.ini" "worldconfig.ini" "masterconfig.ini" "blacklist.dcf" "gms.json")
message(STATUS "Checking resource file integrity")
include(Utils)
@ -244,6 +244,7 @@ set(INCLUDED_DIRECTORIES
"thirdparty/SQLite"
"thirdparty/cpplinq"
"thirdparty/cpp-httplib"
"thirdparty/json/include"
"tests"
"tests/dCommonTests"
@ -317,7 +318,7 @@ add_subdirectory(dPhysics)
add_subdirectory(dServer)
# Create a list of common libraries shared between all binaries
set(COMMON_LIBRARIES "dCommon" "dDatabase" "dNet" "raknet" "mariadbConnCpp" "magic_enum")
set(COMMON_LIBRARIES "dCommon" "dDatabase" "dNet" "raknet" "mariadbConnCpp" "magic_enum" "nlohmann_json")
# Add platform specific common libraries
if(UNIX)

View File

@ -2,7 +2,8 @@ set(DGAME_DGAMEMESSAGES_SOURCES
"GameMessageHandler.cpp"
"GameMessages.cpp"
"PropertyDataMessage.cpp"
"PropertySelectQueryProperty.cpp")
"PropertySelectQueryProperty.cpp"
"GameMessage.cpp")
add_library(dGameMessages STATIC ${DGAME_DGAMEMESSAGES_SOURCES})
target_link_libraries(dGameMessages PUBLIC dDatabase)

View File

@ -0,0 +1,107 @@
#include "GameMessage.h"
#include "AssetManager.h"
#include <BinaryPathFinder.h>
#include "dServer.h"
GameMessageStorage::GameMessageStorage() {
auto path = BinaryPathFinder::GetBinaryDir() / "gms.json";
std::ifstream i(path);
i >> m_Storage;
i.close();
}
uint32_t GameMessageStorage::GetGMFromName(std::string name) {
auto messages = m_Storage["messages"];
for (auto& [key, value] : messages.items()) {
if (value["name"] == name) {
return std::stoi(key);
}
}
return 0;
}
nlohmann::json GameMessageStorage::GetGM(uint32_t id) {
auto messages = m_Storage["messages"];
if (messages.contains(std::to_string(id))) {
return messages[std::to_string(id)];
}
return nlohmann::json();
}
GameMessageStorage::~GameMessageStorage() {
}
GameMessage::GameMessage(std::string name) {
auto id = GameMessageStorage::Instance().GetGMFromName(name);
if (id == 0) {
throw std::exception("GameMessage not found");
}
new(this) GameMessage((eGameMessageType)id);
}
GameMessage::GameMessage(eGameMessageType type) {
m_Type = type;
m_Message = GameMessageStorage::Instance().GetGM((uint32_t)type);
m_State = std::map<std::string, std::any>();
}
void GameMessage::Serialize(RakNet::BitStream* bs) {
bs->Write<uint16_t>((uint16_t)m_Type);
for (auto& value : m_Message["params"]) {
bool presentInState = m_State.contains(value["name"]);
if (value["type"] != "bool") {
if (!presentInState) {
if (value.contains("default")) {
LOG("Written type %s as default", value["name"].get<std::string>().c_str());
bs->Write0();
continue;
} else {
throw std::exception("Fuck.");
}
} else {
if (value.contains("default") && value["type"] != "bool") {
bs->Write1();
}
}
} else {
// special case for bool
if (!presentInState) {
if (!value.contains("default")) throw std::exception("uh oh");
m_State.insert(std::make_pair(value["name"].get<std::string>(), std::any(value["default"] == "true" ? true : false)));
}
}
std::string type = value["type"];
if (type == "Vector3") {
auto res = std::any_cast<Vector3>(m_State[value["name"]]);
bs->Write(res.GetX());
bs->Write(res.GetY());
bs->Write(res.GetZ());
LOG("Written type %s with values %f %f %f", value["name"].get<std::string>().c_str(), res.GetX(), res.GetY(), res.GetZ());
} else if (type == "float") {
bs->Write<float>(std::any_cast<float>(m_State[value["name"]]));
LOG("Written type %s with value %s", value["name"].get<std::string>().c_str(), std::to_string(std::any_cast<float>(m_State[value["name"]])).c_str());
} else if (type == "bool") {
bs->Write(std::any_cast<bool>(m_State[value["name"]]));
LOG("Written type %s with value %s", value["name"].get<std::string>().c_str(), std::to_string(std::any_cast<bool>(m_State[value["name"]])).c_str());
}
}
}

View File

@ -0,0 +1,56 @@
#pragma once
#include <any>
#include <nlohmann/json.hpp>
#include "Singleton.h"
#include "eGameMessageType.h"
typedef std::map<std::string, std::any> StateStorage;
class GameMessageStorage : public Singleton<GameMessageStorage> {
public:
GameMessageStorage();
~GameMessageStorage();
uint32_t GetGMFromName(std::string name);
nlohmann::json GetGM(uint32_t id);
private:
nlohmann::json m_Storage;
};
class GameMessage {
public:
GameMessage(std::string name);
GameMessage(eGameMessageType type);
void Serialize(RakNet::BitStream* bs);
template <typename T>
inline void Set(std::string key, T value) {
m_State[key] = std::any(value);
}
template <typename T>
inline T Get(std::string key) {
return m_State[key];
}
inline eGameMessageType GetType() {
return m_Type;
}
inline StateStorage GetState() {
return m_State;
}
inline nlohmann::json GetMessage() {
return m_Message;
}
private:
eGameMessageType m_Type;
nlohmann::json m_Message;
StateStorage m_State;
};

View File

@ -100,6 +100,8 @@
#include "CDComponentsRegistryTable.h"
#include "CDObjectsTable.h"
#include "GameMessage.h"
void GameMessages::SendFireEventClientSide(const LWOOBJID& objectID, const SystemAddress& sysAddr, std::u16string args, const LWOOBJID& object, int64_t param1, int param2, const LWOOBJID& sender) {
CBITSTREAM;
CMSGHEADER;
@ -127,30 +129,15 @@ void GameMessages::SendTeleport(const LWOOBJID& objectID, const NiPoint3& pos, c
CBITSTREAM;
CMSGHEADER;
bitStream.Write(objectID);
bitStream.Write(eGameMessageType::TELEPORT);
bool bIgnoreY = (pos.y == 0.0f);
bool bUseNavmesh = false;
bool bSkipAllChecks = false;
//float w = 1.0f;
//float x = 0.0f;
//float y = 0.0f;
//float z = 0.0f;
GameMessage msg = GameMessage(eGameMessageType::TELEPORT);
bitStream.Write(bIgnoreY);
bitStream.Write(bSetRotation);
bitStream.Write(bSkipAllChecks);
bitStream.Write(pos.x);
bitStream.Write(pos.y);
bitStream.Write(pos.z);
bitStream.Write(bUseNavmesh);
msg.Set("pos", pos);
msg.Set("x", rot.x);
msg.Set("y", rot.y);
msg.Set("z", rot.z);
bitStream.Write(rot.w != 1.0f);
if (rot.w != 1.0f) bitStream.Write(rot.w);
bitStream.Write(rot.x);
bitStream.Write(rot.y);
bitStream.Write(rot.z);
msg.Serialize(&bitStream);
SEND_PACKET;
}

5708
resources/gms.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -34,6 +34,9 @@ include(CMakeMariaDBLists.txt)
# Create our third party library objects
add_subdirectory(raknet)
# Add nlohmann JSON library
add_subdirectory(json)
# Download Backtrace if configured
if(UNIX AND NOT APPLE)
include(FetchContent)

1
thirdparty/json vendored Submodule

@ -0,0 +1 @@
Subproject commit a259ecc51e1951e12f757ce17db958e9881e9c6c