mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-10-10 17:38:08 +00:00
Dynamic GM sending proof of concept
This commit is contained in:
@@ -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)
|
||||
|
107
dGame/dGameMessages/GameMessage.cpp
Normal file
107
dGame/dGameMessages/GameMessage.cpp
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
56
dGame/dGameMessages/GameMessage.h
Normal file
56
dGame/dGameMessages/GameMessage.h
Normal 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;
|
||||
};
|
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user