Compare commits

...

22 Commits

Author SHA1 Message Date
David Markowitz
7324740be6 fix: ghost mis-matched pointer causing objects to not destruct 2025-05-14 21:41:06 -07:00
David Markowitz
e42df5b02e feat: Add implementation for visited levels (#1795)
* feat: Add implementation for visited levels

* update to working code
2025-05-14 22:49:35 -05:00
61921cfb62 feat: refactor web server to be generic and add websockets framework (#1786)
* Break out changes into a smaller subset

* NL@EOF

* fix windows bs
add player ws updates
add websocket docs

* tested everything to make sure it works

* Address Feedback
2025-05-14 22:38:38 -05:00
David Markowitz
91f6b2bf81 fix: weekly leaderboards and shooting gallery high score (#1719)
* fix them again

* name

* Update GameMessages.cpp

* Update SGCannon.cpp

* Use chrono library instead
2025-05-14 03:58:16 -05:00
David Markowitz
01917841cb Invert frame rates (use inactive if 0 players in world, not the other way around) (#1796) 2025-05-14 03:57:29 -05:00
David Markowitz
e18c504ee4 feat: auto reject empty properties (#1794)
Tested that having the config option set to 1 and having an empty property auto-rejected it.  Tested that having a model on the property or having the new config option set to 0 auto approved the property (as per live)
2025-05-07 23:15:10 -05:00
David Markowitz
b6f7b4c092 fix: add null check and character version update (#1793)
* fix: add null check

* Add version update as well
2025-05-05 18:57:05 -05:00
David Markowitz
522299c9ec feat: normalize brick model positions (#1761)
* Add utilities for formats

* Normalize model positions when placing in the world

Have tested that placing a small and very large model both place and are located at the correct position.

* add migration

* Update Logger.cpp

* add some notes and remove some logs

* change arguments and add eof check

Revert "fix: buff station cycling and dying too soon"

This reverts commit 1c6cb2921e10eb2000ac40007d0c2636ba2ac151.

fix: buff station cycling and dying too soon

Tested that the buff station now only cycles after it has been built and has been alive for 25 seconds.
2025-05-05 09:05:12 -05:00
David Markowitz
0e551429d3 chore: move all teams logic to its own namespace to consolidate logic (#1777)
* Add invite initial response msg

re-do team leave logic to send more accurate messages

Players are still able to leave the team with the same results as before, however now the correct messages are sent to team chats (no fixes for local teams).

* chore: move team logic to separate container

Makes it easier to follow team logic when you're not bouncing between 3 classes in 3 files.  Consolidates all team logic to 1 namespace in TeamContainer.  No logic changes were done, only renaming and fixing errors from the moving. TeamData should be replaced with unique_ptrs at some point so the Shutdown method can be removed from TeamContainer.
2025-05-05 09:04:43 -05:00
David Markowitz
c77e9ce33a chore: some zone maintenance (#1778) 2025-05-05 09:04:23 -05:00
David Markowitz
3ebc6709db feat: Property behaviors partially functional (#1759)
* most of gameplay tab works

* smash unsmash and wait working

* Add pausing of models and behaviors

* working basic behaviors

* play sound functioning

* add resetting

* Fix asynchronous actions executing other strips actions

* Add comments, remove dead code etc.

* Skip Smashes if they coincide with a UnSmash

Remove debug logs

Comment on return
2025-05-05 00:17:39 -07:00
Gie "Max" Vanommeslaeghe
841b754b01 Merge pull request #1762 from DarkflameUniverse/landing-anims
fix: Add properties to landing anim excluded zones
2025-05-04 17:36:04 +02:00
Gie "Max" Vanommeslaeghe
62c3f489fe Merge pull request #1781 from DarkflameUniverse/backwards
fix: backwards names for achievement notify
2025-05-04 17:35:30 +02:00
Gie "Max" Vanommeslaeghe
5ccb8357fd Merge pull request #1785 from DarkflameUniverse/worldServerCleanup
chore: some cleanup work on WorldServer and PerformanceManager
2025-05-04 17:35:18 +02:00
Gie "Max" Vanommeslaeghe
4bacb8a2ee Merge pull request #1787 from DarkflameUniverse/1208-retroactively-fix-nexus-force-explorer-missions
fix: nexus force explorer missions
2025-05-04 17:33:55 +02:00
Gie "Max" Vanommeslaeghe
89678c4a05 Merge pull request #1788 from DarkflameUniverse/1770-Kraken-audio-plays-after-shooting-gallery-match
fix: kraken audio plays after shooting gallery match
2025-05-04 17:33:31 +02:00
ElectScholar
4f97ecc073 fix: lastUpdatedTime updating (#1784)
* Create new LastSave() Method for Database and renew LastUpdatedTime in Save()

* Attach UpdateLastSave() to sqlite and mysql

* Fix compilation issues

* Add updateTime functionality to UpdatePropertyDetails()
2025-05-03 20:19:31 -07:00
David Markowitz
c9e4cde68d Update SGCannon.cpp 2025-05-02 18:11:55 -07:00
David Markowitz
0a12672889 fix: kracken audio 2025-05-02 18:10:27 -07:00
David Markowitz
00a69909f8 fix: nexus force explorer missions
Tested that a charcter with the mission requirements met now has the mission completed upon logging in
2025-05-02 17:17:49 -07:00
David Markowitz
7c8ca1c1cb chore: some cleanup work on WorldServer and PerformanceManager 2025-05-02 16:33:28 -07:00
David Markowitz
3d595ce4ac Add properties to landing anim excluded zones
Check this video for footage of no animation playing on landing in a property.
https://www.youtube.com/watch?v=FYqjZBnuBIg
2025-04-11 00:46:45 -07:00
111 changed files with 3173 additions and 1565 deletions

View File

@@ -235,6 +235,8 @@ include_directories(
"dNet"
"dWeb"
"tests"
"tests/dCommonTests"
"tests/dGameTests"
@@ -301,6 +303,7 @@ add_subdirectory(dZoneManager)
add_subdirectory(dNavigation)
add_subdirectory(dPhysics)
add_subdirectory(dServer)
add_subdirectory(dWeb)
# Create a list of common libraries shared between all binaries
set(COMMON_LIBRARIES "dCommon" "dDatabase" "dNet" "raknet" "magic_enum")

View File

@@ -105,7 +105,7 @@ void dChatFilter::ExportWordlistToDCF(const std::string& filepath, bool allowLis
}
}
std::vector<std::pair<uint8_t, uint8_t>> dChatFilter::IsSentenceOkay(const std::string& message, eGameMasterLevel gmLevel, bool allowList) {
std::set<std::pair<uint8_t, uint8_t>> dChatFilter::IsSentenceOkay(const std::string& message, eGameMasterLevel gmLevel, bool allowList) {
if (gmLevel > eGameMasterLevel::FORUM_MODERATOR) return { }; //If anything but a forum mod, return true.
if (message.empty()) return { };
if (!allowList && m_DeniedWords.empty()) return { { 0, message.length() } };
@@ -114,7 +114,7 @@ std::vector<std::pair<uint8_t, uint8_t>> dChatFilter::IsSentenceOkay(const std::
std::string segment;
std::regex reg("(!*|\\?*|\\;*|\\.*|\\,*)");
std::vector<std::pair<uint8_t, uint8_t>> listOfBadSegments = std::vector<std::pair<uint8_t, uint8_t>>();
std::set<std::pair<uint8_t, uint8_t>> listOfBadSegments;
uint32_t position = 0;
@@ -127,17 +127,17 @@ std::vector<std::pair<uint8_t, uint8_t>> dChatFilter::IsSentenceOkay(const std::
size_t hash = CalculateHash(segment);
if (std::find(m_UserUnapprovedWordCache.begin(), m_UserUnapprovedWordCache.end(), hash) != m_UserUnapprovedWordCache.end() && allowList) {
listOfBadSegments.emplace_back(position, originalSegment.length());
listOfBadSegments.emplace(position, originalSegment.length());
}
if (std::find(m_ApprovedWords.begin(), m_ApprovedWords.end(), hash) == m_ApprovedWords.end() && allowList) {
m_UserUnapprovedWordCache.push_back(hash);
listOfBadSegments.emplace_back(position, originalSegment.length());
listOfBadSegments.emplace(position, originalSegment.length());
}
if (std::find(m_DeniedWords.begin(), m_DeniedWords.end(), hash) != m_DeniedWords.end() && !allowList) {
m_UserUnapprovedWordCache.push_back(hash);
listOfBadSegments.emplace_back(position, originalSegment.length());
listOfBadSegments.emplace(position, originalSegment.length());
}
position += originalSegment.length() + 1;

View File

@@ -24,7 +24,7 @@ public:
void ReadWordlistPlaintext(const std::string& filepath, bool allowList);
bool ReadWordlistDCF(const std::string& filepath, bool allowList);
void ExportWordlistToDCF(const std::string& filepath, bool allowList);
std::vector<std::pair<uint8_t, uint8_t>> IsSentenceOkay(const std::string& message, eGameMasterLevel gmLevel, bool allowList = true);
std::set<std::pair<uint8_t, uint8_t>> IsSentenceOkay(const std::string& message, eGameMasterLevel gmLevel, bool allowList = true);
private:
bool m_DontGenerateDCF;

View File

@@ -1,18 +1,19 @@
set(DCHATSERVER_SOURCES
"ChatIgnoreList.cpp"
"ChatPacketHandler.cpp"
"ChatJSONUtils.cpp"
"ChatWeb.cpp"
"PlayerContainer.cpp"
"ChatWebAPI.cpp"
"JSONUtils.cpp"
"TeamContainer.cpp"
)
add_executable(ChatServer "ChatServer.cpp")
target_include_directories(ChatServer PRIVATE "${PROJECT_SOURCE_DIR}/dChatFilter")
target_include_directories(ChatServer PRIVATE "${PROJECT_SOURCE_DIR}/dChatFilter" "${PROJECT_SOURCE_DIR}/dWeb")
add_compile_definitions(ChatServer PRIVATE PROJECT_VERSION="\"${PROJECT_VERSION}\"")
add_library(dChatServer ${DCHATSERVER_SOURCES})
target_include_directories(dChatServer PRIVATE "${PROJECT_SOURCE_DIR}/dServer")
target_include_directories(dChatServer PRIVATE "${PROJECT_SOURCE_DIR}/dServer" "${PROJECT_SOURCE_DIR}/dChatFilter")
target_link_libraries(dChatServer ${COMMON_LIBRARIES} dChatFilter)
target_link_libraries(ChatServer ${COMMON_LIBRARIES} dChatFilter dChatServer dServer mongoose)
target_link_libraries(ChatServer ${COMMON_LIBRARIES} dChatFilter dChatServer dServer mongoose dWeb)

View File

@@ -1,4 +1,4 @@
#include "JSONUtils.h"
#include "ChatJSONUtils.h"
#include "json.hpp"
@@ -18,19 +18,12 @@ void to_json(json& data, const PlayerData& playerData) {
void to_json(json& data, const PlayerContainer& playerContainer) {
data = json::array();
for(auto& playerData : playerContainer.GetAllPlayers()) {
for (auto& playerData : playerContainer.GetAllPlayers()) {
if (playerData.first == LWOOBJID_EMPTY) continue;
data.push_back(playerData.second);
}
}
void to_json(json& data, const TeamContainer& teamContainer) {
for (auto& teamData : Game::playerContainer.GetTeams()) {
if (!teamData) continue;
data.push_back(*teamData);
}
}
void to_json(json& data, const TeamData& teamData) {
data["id"] = teamData.teamID;
data["loot_flag"] = teamData.lootFlag;
@@ -48,15 +41,9 @@ void to_json(json& data, const TeamData& teamData) {
}
}
std::string JSONUtils::CheckRequiredData(const json& data, const std::vector<std::string>& requiredData) {
json check;
check["error"] = json::array();
for (const auto& required : requiredData) {
if (!data.contains(required)) {
check["error"].push_back("Missing Parameter: " + required);
} else if (data[required] == "") {
check["error"].push_back("Empty Parameter: " + required);
}
void TeamContainer::to_json(json& data, const TeamContainer::Data& teamContainer) {
for (auto& teamData : TeamContainer::GetTeams()) {
if (!teamData) continue;
data.push_back(*teamData);
}
return check["error"].empty() ? "" : check.dump();
}

View File

@@ -0,0 +1,18 @@
#ifndef __CHATJSONUTILS_H__
#define __CHATJSONUTILS_H__
#include "json_fwd.hpp"
#include "PlayerContainer.h"
#include "TeamContainer.h"
/* Remember, to_json needs to be in the same namespace as the class its located in */
void to_json(nlohmann::json& data, const PlayerData& playerData);
void to_json(nlohmann::json& data, const PlayerContainer& playerContainer);
void to_json(nlohmann::json& data, const TeamData& teamData);
namespace TeamContainer {
void to_json(nlohmann::json& data, const TeamContainer::Data& teamData);
};
#endif // !__CHATJSONUTILS_H__

View File

@@ -19,6 +19,7 @@
#include "StringifiedEnum.h"
#include "eGameMasterLevel.h"
#include "ChatPackets.h"
#include "TeamContainer.h"
void ChatPacketHandler::HandleFriendlistRequest(Packet* packet) {
//Get from the packet which player we want to do something with:
@@ -447,7 +448,7 @@ void ChatPacketHandler::HandleChatMessage(Packet* packet) {
switch (channel) {
case eChatChannel::TEAM: {
auto* team = Game::playerContainer.GetTeam(playerID);
auto* team = TeamContainer::GetTeam(playerID);
if (team == nullptr) return;
for (const auto memberId : team->memberIDs) {
@@ -563,400 +564,6 @@ void ChatPacketHandler::SendPrivateChatMessage(const PlayerData& sender, const P
SEND_PACKET;
}
void ChatPacketHandler::HandleTeamInvite(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID;
LUWString invitedPlayer;
inStream.Read(playerID);
inStream.IgnoreBytes(4);
inStream.Read(invitedPlayer);
const auto& player = Game::playerContainer.GetPlayerData(playerID);
if (!player) return;
auto* team = Game::playerContainer.GetTeam(playerID);
if (team == nullptr) {
team = Game::playerContainer.CreateTeam(playerID);
}
const auto& other = Game::playerContainer.GetPlayerData(invitedPlayer.GetAsString());
if (!other) return;
if (Game::playerContainer.GetTeam(other.playerID) != nullptr) {
return;
}
if (team->memberIDs.size() > 3) {
// no more teams greater than 4
LOG("Someone tried to invite a 5th player to a team");
return;
}
SendTeamInvite(other, player);
LOG("Got team invite: %llu -> %s", playerID, invitedPlayer.GetAsString().c_str());
bool failed = false;
for (const auto& ignore : other.ignoredPlayers) {
if (ignore.playerId == player.playerID) {
failed = true;
break;
}
}
ChatPackets::TeamInviteInitialResponse response{};
response.inviteFailedToSend = failed;
response.playerName = invitedPlayer.string;
ChatPackets::SendRoutedMsg(response, playerID, player.worldServerSysAddr);
}
void ChatPacketHandler::HandleTeamInviteResponse(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID = LWOOBJID_EMPTY;
inStream.Read(playerID);
uint32_t size = 0;
inStream.Read(size);
char declined = 0;
inStream.Read(declined);
LWOOBJID leaderID = LWOOBJID_EMPTY;
inStream.Read(leaderID);
LOG("Invite reponse received: %llu -> %llu (%d)", playerID, leaderID, declined);
if (declined) {
return;
}
auto* team = Game::playerContainer.GetTeam(leaderID);
if (team == nullptr) {
LOG("Failed to find team for leader (%llu)", leaderID);
team = Game::playerContainer.GetTeam(playerID);
}
if (team == nullptr) {
LOG("Failed to find team for player (%llu)", playerID);
return;
}
Game::playerContainer.AddMember(team, playerID);
}
void ChatPacketHandler::HandleTeamLeave(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID = LWOOBJID_EMPTY;
inStream.Read(playerID);
uint32_t size = 0;
inStream.Read(size);
auto* team = Game::playerContainer.GetTeam(playerID);
LOG("(%llu) leaving team", playerID);
if (team != nullptr) {
Game::playerContainer.RemoveMember(team, playerID, false, false, true);
}
}
void ChatPacketHandler::HandleTeamKick(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID = LWOOBJID_EMPTY;
LUWString kickedPlayer;
inStream.Read(playerID);
inStream.IgnoreBytes(4);
inStream.Read(kickedPlayer);
LOG("(%llu) kicking (%s) from team", playerID, kickedPlayer.GetAsString().c_str());
const auto& kicked = Game::playerContainer.GetPlayerData(kickedPlayer.GetAsString());
LWOOBJID kickedId = LWOOBJID_EMPTY;
if (kicked) {
kickedId = kicked.playerID;
} else {
kickedId = Game::playerContainer.GetId(kickedPlayer.string);
}
if (kickedId == LWOOBJID_EMPTY) return;
auto* team = Game::playerContainer.GetTeam(playerID);
if (team != nullptr) {
if (team->leaderID != playerID || team->leaderID == kickedId) return;
Game::playerContainer.RemoveMember(team, kickedId, false, true, false);
}
}
void ChatPacketHandler::HandleTeamPromote(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID = LWOOBJID_EMPTY;
LUWString promotedPlayer;
inStream.Read(playerID);
inStream.IgnoreBytes(4);
inStream.Read(promotedPlayer);
LOG("(%llu) promoting (%s) to team leader", playerID, promotedPlayer.GetAsString().c_str());
const auto& promoted = Game::playerContainer.GetPlayerData(promotedPlayer.GetAsString());
if (!promoted) return;
auto* team = Game::playerContainer.GetTeam(playerID);
if (team != nullptr) {
if (team->leaderID != playerID) return;
Game::playerContainer.PromoteMember(team, promoted.playerID);
}
}
void ChatPacketHandler::HandleTeamLootOption(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID = LWOOBJID_EMPTY;
inStream.Read(playerID);
uint32_t size = 0;
inStream.Read(size);
char option;
inStream.Read(option);
auto* team = Game::playerContainer.GetTeam(playerID);
if (team != nullptr) {
if (team->leaderID != playerID) return;
team->lootFlag = option;
Game::playerContainer.TeamStatusUpdate(team);
Game::playerContainer.UpdateTeamsOnWorld(team, false);
}
}
void ChatPacketHandler::HandleTeamStatusRequest(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID = LWOOBJID_EMPTY;
inStream.Read(playerID);
auto* team = Game::playerContainer.GetTeam(playerID);
const auto& data = Game::playerContainer.GetPlayerData(playerID);
if (team != nullptr && data) {
LOG_DEBUG("Player %llu is requesting team status", playerID);
if (team->local && data.zoneID.GetMapID() != team->zoneId.GetMapID() && data.zoneID.GetCloneID() != team->zoneId.GetCloneID()) {
Game::playerContainer.RemoveMember(team, playerID, false, false, false, true);
return;
}
if (team->memberIDs.size() <= 1 && !team->local) {
Game::playerContainer.DisbandTeam(team, LWOOBJID_EMPTY, u"");
return;
}
if (!team->local) {
ChatPacketHandler::SendTeamSetLeader(data, team->leaderID);
} else {
ChatPacketHandler::SendTeamSetLeader(data, LWOOBJID_EMPTY);
}
Game::playerContainer.TeamStatusUpdate(team);
const auto leaderName = GeneralUtils::UTF8ToUTF16(data.playerName);
for (const auto memberId : team->memberIDs) {
const auto& otherMember = Game::playerContainer.GetPlayerData(memberId);
if (memberId == playerID) continue;
const auto memberName = Game::playerContainer.GetName(memberId);
if (otherMember) {
ChatPacketHandler::SendTeamSetOffWorldFlag(otherMember, data.playerID, data.zoneID);
}
ChatPacketHandler::SendTeamAddPlayer(data, false, team->local, false, memberId, memberName, otherMember ? otherMember.zoneID : LWOZONEID(0, 0, 0));
}
Game::playerContainer.UpdateTeamsOnWorld(team, false);
}
}
void ChatPacketHandler::SendTeamInvite(const PlayerData& receiver, const PlayerData& sender) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, MessageType::Client::TEAM_INVITE);
bitStream.Write(LUWString(sender.playerName.c_str()));
bitStream.Write(sender.playerID);
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void ChatPacketHandler::SendTeamInviteConfirm(const PlayerData& receiver, bool bLeaderIsFreeTrial, LWOOBJID i64LeaderID, LWOZONEID i64LeaderZoneID, uint8_t ucLootFlag, uint8_t ucNumOfOtherPlayers, uint8_t ucResponseCode, std::u16string wsLeaderName) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
CMSGHEADER;
bitStream.Write(receiver.playerID);
bitStream.Write(MessageType::Game::TEAM_INVITE_CONFIRM);
bitStream.Write(bLeaderIsFreeTrial);
bitStream.Write(i64LeaderID);
bitStream.Write(i64LeaderZoneID);
bitStream.Write<uint32_t>(0); // BinaryBuffe, no clue what's in here
bitStream.Write(ucLootFlag);
bitStream.Write(ucNumOfOtherPlayers);
bitStream.Write(ucResponseCode);
bitStream.Write<uint32_t>(wsLeaderName.size());
for (const auto character : wsLeaderName) {
bitStream.Write(character);
}
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void ChatPacketHandler::SendTeamStatus(const PlayerData& receiver, LWOOBJID i64LeaderID, LWOZONEID i64LeaderZoneID, uint8_t ucLootFlag, uint8_t ucNumOfOtherPlayers, std::u16string wsLeaderName) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
CMSGHEADER;
bitStream.Write(receiver.playerID);
bitStream.Write(MessageType::Game::TEAM_GET_STATUS_RESPONSE);
bitStream.Write(i64LeaderID);
bitStream.Write(i64LeaderZoneID);
bitStream.Write<uint32_t>(0); // BinaryBuffe, no clue what's in here
bitStream.Write(ucLootFlag);
bitStream.Write(ucNumOfOtherPlayers);
bitStream.Write<uint32_t>(wsLeaderName.size());
for (const auto character : wsLeaderName) {
bitStream.Write(character);
}
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void ChatPacketHandler::SendTeamSetLeader(const PlayerData& receiver, LWOOBJID i64PlayerID) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
CMSGHEADER;
bitStream.Write(receiver.playerID);
bitStream.Write(MessageType::Game::TEAM_SET_LEADER);
bitStream.Write(i64PlayerID);
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void ChatPacketHandler::SendTeamAddPlayer(const PlayerData& receiver, bool bIsFreeTrial, bool bLocal, bool bNoLootOnDeath, LWOOBJID i64PlayerID, std::u16string wsPlayerName, LWOZONEID zoneID) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
CMSGHEADER;
bitStream.Write(receiver.playerID);
bitStream.Write(MessageType::Game::TEAM_ADD_PLAYER);
bitStream.Write(bIsFreeTrial);
bitStream.Write(bLocal);
bitStream.Write(bNoLootOnDeath);
bitStream.Write(i64PlayerID);
bitStream.Write<uint32_t>(wsPlayerName.size());
for (const auto character : wsPlayerName) {
bitStream.Write(character);
}
bitStream.Write1();
if (receiver.zoneID.GetCloneID() == zoneID.GetCloneID()) {
zoneID = LWOZONEID(zoneID.GetMapID(), zoneID.GetInstanceID(), 0);
}
bitStream.Write(zoneID);
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void ChatPacketHandler::SendTeamRemovePlayer(const PlayerData& receiver, bool bDisband, bool bIsKicked, bool bIsLeaving, bool bLocal, LWOOBJID i64LeaderID, LWOOBJID i64PlayerID, std::u16string wsPlayerName) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
CMSGHEADER;
bitStream.Write(receiver.playerID);
bitStream.Write(MessageType::Game::TEAM_REMOVE_PLAYER);
bitStream.Write(bDisband);
bitStream.Write(bIsKicked);
bitStream.Write(bIsLeaving);
bitStream.Write(bLocal);
bitStream.Write(i64LeaderID);
bitStream.Write(i64PlayerID);
bitStream.Write<uint32_t>(wsPlayerName.size());
for (const auto character : wsPlayerName) {
bitStream.Write(character);
}
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void ChatPacketHandler::SendTeamSetOffWorldFlag(const PlayerData& receiver, LWOOBJID i64PlayerID, LWOZONEID zoneID) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
CMSGHEADER;
bitStream.Write(receiver.playerID);
bitStream.Write(MessageType::Game::TEAM_SET_OFF_WORLD_FLAG);
bitStream.Write(i64PlayerID);
if (receiver.zoneID.GetCloneID() == zoneID.GetCloneID()) {
zoneID = LWOZONEID(zoneID.GetMapID(), zoneID.GetInstanceID(), 0);
}
bitStream.Write(zoneID);
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void ChatPacketHandler::SendFriendUpdate(const PlayerData& friendData, const PlayerData& playerData, uint8_t notifyType, uint8_t isBestFriend) {
/*chat notification is displayed if log in / out and friend is updated in friends list
[u8] - update type

View File

@@ -35,13 +35,13 @@ enum class eChatChannel : uint8_t {
enum class eChatMessageResponseCode : uint8_t {
SENT = 0,
NOTONLINE,
GENERALERROR,
RECEIVEDNEWWHISPER,
NOTFRIENDS,
SENDERFREETRIAL,
RECEIVERFREETRIAL,
SENT = 0,
NOTONLINE,
GENERALERROR,
RECEIVEDNEWWHISPER,
NOTFRIENDS,
SENDERFREETRIAL,
RECEIVERFREETRIAL,
};
namespace ChatPacketHandler {
@@ -52,33 +52,14 @@ namespace ChatPacketHandler {
void HandleGMLevelUpdate(Packet* packet);
void HandleWho(Packet* packet);
void HandleShowAll(Packet* packet);
void HandleChatMessage(Packet* packet);
void HandlePrivateChatMessage(Packet* packet);
void SendPrivateChatMessage(const PlayerData& sender, const PlayerData& receiver, const PlayerData& routeTo, const LUWString& message, const eChatChannel channel, const eChatMessageResponseCode responseCode);
void HandleTeamInvite(Packet* packet);
void HandleTeamInviteResponse(Packet* packet);
void HandleTeamLeave(Packet* packet);
void HandleTeamKick(Packet* packet);
void HandleTeamPromote(Packet* packet);
void HandleTeamLootOption(Packet* packet);
void HandleTeamStatusRequest(Packet* packet);
void OnAchievementNotify(RakNet::BitStream& bitstream, const SystemAddress& sysAddr);
void SendTeamInvite(const PlayerData& receiver, const PlayerData& sender);
void SendTeamInviteConfirm(const PlayerData& receiver, bool bLeaderIsFreeTrial, LWOOBJID i64LeaderID, LWOZONEID i64LeaderZoneID, uint8_t ucLootFlag, uint8_t ucNumOfOtherPlayers, uint8_t ucResponseCode, std::u16string wsLeaderName);
void SendTeamStatus(const PlayerData& receiver, LWOOBJID i64LeaderID, LWOZONEID i64LeaderZoneID, uint8_t ucLootFlag, uint8_t ucNumOfOtherPlayers, std::u16string wsLeaderName);
void SendTeamSetLeader(const PlayerData& receiver, LWOOBJID i64PlayerID);
void SendTeamAddPlayer(const PlayerData& receiver, bool bIsFreeTrial, bool bLocal, bool bNoLootOnDeath, LWOOBJID i64PlayerID, std::u16string wsPlayerName, LWOZONEID zoneID);
/* Sends a message to the provided `receiver` with information about the updated team. If `i64LeaderID` is not LWOOBJID_EMPTY, the client will update the leader to that new playerID. */
void SendTeamRemovePlayer(const PlayerData& receiver, bool bDisband, bool bIsKicked, bool bIsLeaving, bool bLocal, LWOOBJID i64LeaderID, LWOOBJID i64PlayerID, std::u16string wsPlayerName);
void SendTeamSetOffWorldFlag(const PlayerData& receiver, LWOOBJID i64PlayerID, LWOZONEID zoneID);
//FriendData is the player we're SENDING this stuff to. Player is the friend that changed state.
void SendFriendUpdate(const PlayerData& friendData, const PlayerData& playerData, uint8_t notifyType, uint8_t isBestFriend);
void SendPrivateChatMessage(const PlayerData& sender, const PlayerData& receiver, const PlayerData& routeTo, const LUWString& message, const eChatChannel channel, const eChatMessageResponseCode responseCode);
void SendFriendRequest(const PlayerData& receiver, const PlayerData& sender);
void SendFriendResponse(const PlayerData& receiver, const PlayerData& sender, eAddFriendResponseType responseCode, uint8_t isBestFriendsAlready = 0U, uint8_t isBestFriendRequest = 0U);
void SendRemoveFriend(const PlayerData& receiver, std::string& personToRemove, bool isSuccessful);

View File

@@ -20,6 +20,7 @@
#include "MessageType/World.h"
#include "ChatIgnoreList.h"
#include "StringifiedEnum.h"
#include "TeamContainer.h"
#include "Game.h"
#include "Server.h"
@@ -28,7 +29,7 @@
#include "RakNetDefines.h"
#include "MessageIdentifiers.h"
#include "ChatWebAPI.h"
#include "ChatWeb.h"
namespace Game {
Logger* logger = nullptr;
@@ -92,17 +93,18 @@ int main(int argc, char** argv) {
return EXIT_FAILURE;
}
// seyup the chat api web server
bool web_server_enabled = Game::config->GetValue("web_server_enabled") == "1";
ChatWebAPI chatwebapi;
if (web_server_enabled && !chatwebapi.Startup()){
// if we want the web api and it fails to start, exit
// setup the chat api web server
const uint32_t web_server_port = GeneralUtils::TryParse<uint32_t>(Game::config->GetValue("web_server_port")).value_or(2005);
if (Game::config->GetValue("web_server_enabled") == "1" && !Game::web.Startup("localhost", web_server_port)) {
// if we want the web server and it fails to start, exit
LOG("Failed to start web server, shutting down.");
Database::Destroy("ChatServer");
delete Game::logger;
delete Game::config;
return EXIT_FAILURE;
};
}
if (Game::web.IsEnabled()) ChatWeb::RegisterRoutes();
//Find out the master's IP:
std::string masterIP;
@@ -166,10 +168,8 @@ int main(int argc, char** argv) {
packet = nullptr;
}
//Check and handle web requests:
if (web_server_enabled) {
chatwebapi.ReceiveRequests();
}
// Check and handle web requests:
if (Game::web.IsEnabled()) Game::web.ReceiveRequests();
//Push our log every 30s:
if (framesSinceLastFlush >= logFlushTime) {
@@ -197,6 +197,7 @@ int main(int argc, char** argv) {
std::this_thread::sleep_until(t);
}
Game::playerContainer.Shutdown();
TeamContainer::Shutdown();
//Delete our objects here:
Database::Destroy("ChatServer");
delete Game::server;
@@ -234,7 +235,7 @@ void HandlePacket(Packet* packet) {
break;
case MessageType::Chat::CREATE_TEAM:
Game::playerContainer.CreateTeamServer(packet);
TeamContainer::CreateTeamServer(packet);
break;
case MessageType::Chat::GET_FRIENDS_LIST:
@@ -254,7 +255,7 @@ void HandlePacket(Packet* packet) {
break;
case MessageType::Chat::TEAM_GET_STATUS:
ChatPacketHandler::HandleTeamStatusRequest(packet);
TeamContainer::HandleTeamStatusRequest(packet);
break;
case MessageType::Chat::ADD_FRIEND_REQUEST:
@@ -284,27 +285,27 @@ void HandlePacket(Packet* packet) {
break;
case MessageType::Chat::TEAM_INVITE:
ChatPacketHandler::HandleTeamInvite(packet);
TeamContainer::HandleTeamInvite(packet);
break;
case MessageType::Chat::TEAM_INVITE_RESPONSE:
ChatPacketHandler::HandleTeamInviteResponse(packet);
TeamContainer::HandleTeamInviteResponse(packet);
break;
case MessageType::Chat::TEAM_LEAVE:
ChatPacketHandler::HandleTeamLeave(packet);
TeamContainer::HandleTeamLeave(packet);
break;
case MessageType::Chat::TEAM_SET_LEADER:
ChatPacketHandler::HandleTeamPromote(packet);
TeamContainer::HandleTeamPromote(packet);
break;
case MessageType::Chat::TEAM_KICK:
ChatPacketHandler::HandleTeamKick(packet);
TeamContainer::HandleTeamKick(packet);
break;
case MessageType::Chat::TEAM_SET_LOOT:
ChatPacketHandler::HandleTeamLootOption(packet);
TeamContainer::HandleTeamLootOption(packet);
break;
case MessageType::Chat::GMLEVEL_UPDATE:
ChatPacketHandler::HandleGMLevelUpdate(packet);

133
dChatServer/ChatWeb.cpp Normal file
View File

@@ -0,0 +1,133 @@
#include "ChatWeb.h"
#include "Logger.h"
#include "Game.h"
#include "json.hpp"
#include "dCommonVars.h"
#include "MessageType/Chat.h"
#include "dServer.h"
#include "dConfig.h"
#include "PlayerContainer.h"
#include "GeneralUtils.h"
#include "eHTTPMethod.h"
#include "magic_enum.hpp"
#include "ChatPackets.h"
#include "StringifiedEnum.h"
#include "Database.h"
#include "ChatJSONUtils.h"
#include "JSONUtils.h"
#include "eGameMasterLevel.h"
#include "dChatFilter.h"
#include "TeamContainer.h"
using json = nlohmann::json;
void HandleHTTPPlayersRequest(HTTPReply& reply, std::string body) {
const json data = Game::playerContainer;
reply.status = data.empty() ? eHTTPStatusCode::NO_CONTENT : eHTTPStatusCode::OK;
reply.message = data.empty() ? "{\"error\":\"No Players Online\"}" : data.dump();
}
void HandleHTTPTeamsRequest(HTTPReply& reply, std::string body) {
const json data = TeamContainer::GetTeamContainer();
reply.status = data.empty() ? eHTTPStatusCode::NO_CONTENT : eHTTPStatusCode::OK;
reply.message = data.empty() ? "{\"error\":\"No Teams Online\"}" : data.dump();
}
void HandleHTTPAnnounceRequest(HTTPReply& reply, std::string body) {
auto data = GeneralUtils::TryParse<json>(body);
if (!data) {
reply.status = eHTTPStatusCode::BAD_REQUEST;
reply.message = "{\"error\":\"Invalid JSON\"}";
return;
}
const auto& good_data = data.value();
auto check = JSONUtils::CheckRequiredData(good_data, { "title", "message" });
if (!check.empty()) {
reply.status = eHTTPStatusCode::BAD_REQUEST;
reply.message = check;
} else {
ChatPackets::Announcement announcement;
announcement.title = good_data["title"];
announcement.message = good_data["message"];
announcement.Broadcast();
reply.status = eHTTPStatusCode::OK;
reply.message = "{\"status\":\"Announcement Sent\"}";
}
}
void HandleWSChat(mg_connection* connection, json data) {
auto check = JSONUtils::CheckRequiredData(data, { "user", "message", "gmlevel", "zone" });
if (!check.empty()) {
LOG_DEBUG("Received invalid websocket message: %s", check.c_str());
} else {
const auto user = data["user"].get<std::string>();
const auto message = data["message"].get<std::string>();
const auto gmlevel = GeneralUtils::TryParse<eGameMasterLevel>(data["gmlevel"].get<std::string>()).value_or(eGameMasterLevel::CIVILIAN);
const auto zone = data["zone"].get<uint32_t>();
const auto filter_check = Game::chatFilter->IsSentenceOkay(message, gmlevel);
if (!filter_check.empty()) {
LOG_DEBUG("Chat message \"%s\" from %s was not allowed", message.c_str(), user.c_str());
data["error"] = "Chat message blocked by filter";
data["filtered"] = json::array();
for (const auto& [start, len] : filter_check) {
data["filtered"].push_back(message.substr(start, len));
}
mg_ws_send(connection, data.dump().c_str(), data.dump().size(), WEBSOCKET_OP_TEXT);
return;
}
LOG("%s: %s", user.c_str(), message.c_str());
// TODO: Implement chat message handling from websocket message
}
}
namespace ChatWeb {
void RegisterRoutes() {
// REST API v1 routes
std::string v1_route = "/api/v1/";
Game::web.RegisterHTTPRoute({
.path = v1_route + "players",
.method = eHTTPMethod::GET,
.handle = HandleHTTPPlayersRequest
});
Game::web.RegisterHTTPRoute({
.path = v1_route + "teams",
.method = eHTTPMethod::GET,
.handle = HandleHTTPTeamsRequest
});
Game::web.RegisterHTTPRoute({
.path = v1_route + "announce",
.method = eHTTPMethod::POST,
.handle = HandleHTTPAnnounceRequest
});
// WebSocket Events Handlers
// Game::web.RegisterWSEvent({
// .name = "chat",
// .handle = HandleWSChat
// });
// WebSocket subscriptions
Game::web.RegisterWSSubscription("player");
}
void SendWSPlayerUpdate(const PlayerData& player, eActivityType activityType) {
json data;
data["player_data"] = player;
data["update_type"] = magic_enum::enum_name(activityType);
Game::web.SendWSMessage("player", data);
}
}

19
dChatServer/ChatWeb.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef __CHATWEB_H__
#define __CHATWEB_H__
#include <string>
#include <functional>
#include "Web.h"
#include "PlayerContainer.h"
#include "IActivityLog.h"
#include "ChatPacketHandler.h"
namespace ChatWeb {
void RegisterRoutes();
void SendWSPlayerUpdate(const PlayerData& player, eActivityType activityType);
};
#endif // __CHATWEB_H__

View File

@@ -1,197 +0,0 @@
#include "ChatWebAPI.h"
#include "Logger.h"
#include "Game.h"
#include "json.hpp"
#include "dCommonVars.h"
#include "MessageType/Chat.h"
#include "dServer.h"
#include "dConfig.h"
#include "PlayerContainer.h"
#include "JSONUtils.h"
#include "GeneralUtils.h"
#include "eHTTPMethod.h"
#include "magic_enum.hpp"
#include "ChatPackets.h"
#include "StringifiedEnum.h"
#include "Database.h"
#ifdef DARKFLAME_PLATFORM_WIN32
#pragma push_macro("DELETE")
#undef DELETE
#endif
using json = nlohmann::json;
typedef struct mg_connection mg_connection;
typedef struct mg_http_message mg_http_message;
namespace {
const char* json_content_type = "Content-Type: application/json\r\n";
std::map<std::pair<eHTTPMethod, std::string>, WebAPIHTTPRoute> Routes {};
}
bool ValidateAuthentication(const mg_http_message* http_msg) {
// TO DO: This is just a placeholder for now
// use tokens or something at a later point if we want to implement authentication
// bit using the listen bind address to limit external access is good enough to start with
return true;
}
bool ValidateJSON(std::optional<json> data, HTTPReply& reply) {
if (!data) {
reply.status = eHTTPStatusCode::BAD_REQUEST;
reply.message = "{\"error\":\"Invalid JSON\"}";
return false;
}
return true;
}
void HandlePlayersRequest(HTTPReply& reply, std::string body) {
const json data = Game::playerContainer;
reply.status = data.empty() ? eHTTPStatusCode::NO_CONTENT : eHTTPStatusCode::OK;
reply.message = data.empty() ? "{\"error\":\"No Players Online\"}" : data.dump();
}
void HandleTeamsRequest(HTTPReply& reply, std::string body) {
const json data = Game::playerContainer.GetTeamContainer();
reply.status = data.empty() ? eHTTPStatusCode::NO_CONTENT : eHTTPStatusCode::OK;
reply.message = data.empty() ? "{\"error\":\"No Teams Online\"}" : data.dump();
}
void HandleAnnounceRequest(HTTPReply& reply, std::string body) {
auto data = GeneralUtils::TryParse<json>(body);
if (!ValidateJSON(data, reply)) return;
const auto& good_data = data.value();
auto check = JSONUtils::CheckRequiredData(good_data, { "title", "message" });
if (!check.empty()) {
reply.status = eHTTPStatusCode::BAD_REQUEST;
reply.message = check;
} else {
ChatPackets::Announcement announcement;
announcement.title = good_data["title"];
announcement.message = good_data["message"];
announcement.Send();
reply.status = eHTTPStatusCode::OK;
reply.message = "{\"status\":\"Announcement Sent\"}";
}
}
void HandleInvalidRoute(HTTPReply& reply) {
reply.status = eHTTPStatusCode::NOT_FOUND;
reply.message = "{\"error\":\"Invalid Route\"}";
}
void HandleHTTPMessage(mg_connection* connection, const mg_http_message* http_msg) {
HTTPReply reply;
if (!http_msg) {
reply.status = eHTTPStatusCode::BAD_REQUEST;
reply.message = "{\"error\":\"Invalid Request\"}";
} else if (ValidateAuthentication(http_msg)) {
// convert method from cstring to std string
std::string method_string(http_msg->method.buf, http_msg->method.len);
// get mehtod from mg to enum
const eHTTPMethod method = magic_enum::enum_cast<eHTTPMethod>(method_string).value_or(eHTTPMethod::INVALID);
// convert uri from cstring to std string
std::string uri(http_msg->uri.buf, http_msg->uri.len);
std::transform(uri.begin(), uri.end(), uri.begin(), ::tolower);
// convert body from cstring to std string
std::string body(http_msg->body.buf, http_msg->body.len);
const auto routeItr = Routes.find({method, uri});
if (routeItr != Routes.end()) {
const auto& [_, route] = *routeItr;
route.handle(reply, body);
} else HandleInvalidRoute(reply);
} else {
reply.status = eHTTPStatusCode::UNAUTHORIZED;
reply.message = "{\"error\":\"Unauthorized\"}";
}
mg_http_reply(connection, static_cast<int>(reply.status), json_content_type, reply.message.c_str());
}
void HandleRequests(mg_connection* connection, int request, void* request_data) {
switch (request) {
case MG_EV_HTTP_MSG:
HandleHTTPMessage(connection, static_cast<mg_http_message*>(request_data));
break;
default:
break;
}
}
void ChatWebAPI::RegisterHTTPRoutes(WebAPIHTTPRoute route) {
auto [_, success] = Routes.try_emplace({ route.method, route.path }, route);
if (!success) {
LOG_DEBUG("Failed to register route %s", route.path.c_str());
} else {
LOG_DEBUG("Registered route %s", route.path.c_str());
}
}
ChatWebAPI::ChatWebAPI() {
mg_log_set(MG_LL_NONE);
mg_mgr_init(&mgr); // Initialize event manager
}
ChatWebAPI::~ChatWebAPI() {
mg_mgr_free(&mgr);
}
bool ChatWebAPI::Startup() {
// Make listen address
// std::string listen_ip = Game::config->GetValue("web_server_listen_ip");
// if (listen_ip == "localhost") listen_ip = "127.0.0.1";
const std::string& listen_port = Game::config->GetValue("web_server_listen_port");
// const std::string& listen_address = "http://" + listen_ip + ":" + listen_port;
const std::string& listen_address = "http://localhost:" + listen_port;
LOG("Starting web server on %s", listen_address.c_str());
// Create HTTP listener
if (!mg_http_listen(&mgr, listen_address.c_str(), HandleRequests, NULL)) {
LOG("Failed to create web server listener on %s", listen_port.c_str());
return false;
}
// Register routes
// API v1 routes
std::string v1_route = "/api/v1/";
RegisterHTTPRoutes({
.path = v1_route + "players",
.method = eHTTPMethod::GET,
.handle = HandlePlayersRequest
});
RegisterHTTPRoutes({
.path = v1_route + "teams",
.method = eHTTPMethod::GET,
.handle = HandleTeamsRequest
});
RegisterHTTPRoutes({
.path = v1_route + "announce",
.method = eHTTPMethod::POST,
.handle = HandleAnnounceRequest
});
return true;
}
void ChatWebAPI::ReceiveRequests() {
mg_mgr_poll(&mgr, 15);
}
#ifdef DARKFLAME_PLATFORM_WIN32
#pragma pop_macro("DELETE")
#endif

View File

@@ -1,36 +0,0 @@
#ifndef __CHATWEBAPI_H__
#define __CHATWEBAPI_H__
#include <string>
#include <functional>
#include "mongoose.h"
#include "eHTTPStatusCode.h"
enum class eHTTPMethod;
typedef struct mg_mgr mg_mgr;
struct HTTPReply {
eHTTPStatusCode status = eHTTPStatusCode::NOT_FOUND;
std::string message = "{\"error\":\"Not Found\"}";
};
struct WebAPIHTTPRoute {
std::string path;
eHTTPMethod method;
std::function<void(HTTPReply&, const std::string&)> handle;
};
class ChatWebAPI {
public:
ChatWebAPI();
~ChatWebAPI();
void ReceiveRequests();
void RegisterHTTPRoutes(WebAPIHTTPRoute route);
bool Startup();
private:
mg_mgr mgr;
};
#endif // __CHATWEBAPI_H__

View File

@@ -1,17 +0,0 @@
#ifndef __JSONUTILS_H__
#define __JSONUTILS_H__
#include "json_fwd.hpp"
#include "PlayerContainer.h"
void to_json(nlohmann::json& data, const PlayerData& playerData);
void to_json(nlohmann::json& data, const PlayerContainer& playerContainer);
void to_json(nlohmann::json& data, const TeamContainer& teamData);
void to_json(nlohmann::json& data, const TeamData& teamData);
namespace JSONUtils {
// check required data for reqeust
std::string CheckRequiredData(const nlohmann::json& data, const std::vector<std::string>& requiredData);
}
#endif // __JSONUTILS_H__

View File

@@ -12,6 +12,8 @@
#include "ChatPackets.h"
#include "dConfig.h"
#include "MessageType/Chat.h"
#include "ChatWeb.h"
#include "TeamContainer.h"
void PlayerContainer::Initialize() {
m_MaxNumberOfBestFriends =
@@ -58,8 +60,9 @@ void PlayerContainer::InsertPlayer(Packet* packet) {
m_PlayerCount++;
LOG("Added user: %s (%llu), zone: %i", data.playerName.c_str(), data.playerID, data.zoneID.GetMapID());
ChatWeb::SendWSPlayerUpdate(data, isLogin ? eActivityType::PlayerLoggedIn : eActivityType::PlayerChangedZone);
Database::Get()->UpdateActivityLog(data.playerID, eActivityType::PlayerLoggedIn, data.zoneID.GetMapID());
Database::Get()->UpdateActivityLog(data.playerID, isLogin ? eActivityType::PlayerLoggedIn : eActivityType::PlayerChangedZone, data.zoneID.GetMapID());
m_PlayersToRemove.erase(playerId);
}
@@ -99,7 +102,7 @@ void PlayerContainer::RemovePlayer(const LWOOBJID playerID) {
if (fd) ChatPacketHandler::SendFriendUpdate(fd, player, 0, fr.isBestFriend);
}
auto* team = GetTeam(playerID);
auto* team = TeamContainer::GetTeam(playerID);
if (team != nullptr) {
const auto memberName = GeneralUtils::UTF8ToUTF16(player.playerName);
@@ -109,10 +112,12 @@ void PlayerContainer::RemovePlayer(const LWOOBJID playerID) {
if (!otherMember) continue;
ChatPacketHandler::SendTeamSetOffWorldFlag(otherMember, playerID, { 0, 0, 0 });
TeamContainer::SendTeamSetOffWorldFlag(otherMember, playerID, { 0, 0, 0 });
}
}
ChatWeb::SendWSPlayerUpdate(player, eActivityType::PlayerLoggedOut);
m_PlayerCount--;
LOG("Removed user: %llu", playerID);
m_Players.erase(playerID);
@@ -140,40 +145,6 @@ void PlayerContainer::MuteUpdate(Packet* packet) {
BroadcastMuteUpdate(playerID, expire);
}
void PlayerContainer::CreateTeamServer(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID;
inStream.Read(playerID);
size_t membersSize = 0;
inStream.Read(membersSize);
if (membersSize >= 4) {
LOG("Tried to create a team with more than 4 players");
return;
}
std::vector<LWOOBJID> members;
members.reserve(membersSize);
for (size_t i = 0; i < membersSize; i++) {
LWOOBJID member;
inStream.Read(member);
members.push_back(member);
}
LWOZONEID zoneId;
inStream.Read(zoneId);
auto* team = CreateLocalTeam(members);
if (team != nullptr) {
team->zoneId = zoneId;
UpdateTeamsOnWorld(team, false);
}
}
void PlayerContainer::BroadcastMuteUpdate(LWOOBJID player, time_t time) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::GM_MUTE);
@@ -184,218 +155,6 @@ void PlayerContainer::BroadcastMuteUpdate(LWOOBJID player, time_t time) {
Game::server->Send(bitStream, UNASSIGNED_SYSTEM_ADDRESS, true);
}
TeamData* PlayerContainer::CreateLocalTeam(std::vector<LWOOBJID> members) {
if (members.empty()) {
return nullptr;
}
TeamData* newTeam = nullptr;
for (const auto member : members) {
auto* team = GetTeam(member);
if (team != nullptr) {
RemoveMember(team, member, false, false, true);
}
if (newTeam == nullptr) {
newTeam = CreateTeam(member, true);
} else {
AddMember(newTeam, member);
}
}
newTeam->lootFlag = 1;
TeamStatusUpdate(newTeam);
return newTeam;
}
TeamData* PlayerContainer::CreateTeam(LWOOBJID leader, bool local) {
auto* team = new TeamData();
team->teamID = ++m_TeamIDCounter;
team->leaderID = leader;
team->local = local;
GetTeamsMut().push_back(team);
AddMember(team, leader);
return team;
}
TeamData* PlayerContainer::GetTeam(LWOOBJID playerID) {
for (auto* team : GetTeams()) {
if (std::find(team->memberIDs.begin(), team->memberIDs.end(), playerID) == team->memberIDs.end()) continue;
return team;
}
return nullptr;
}
void PlayerContainer::AddMember(TeamData* team, LWOOBJID playerID) {
if (team->memberIDs.size() >= 4) {
LOG("Tried to add player to team that already had 4 players");
const auto& player = GetPlayerData(playerID);
if (!player) return;
ChatPackets::SendSystemMessage(player.worldServerSysAddr, u"The teams is full! You have not been added to a team!");
return;
}
const auto index = std::find(team->memberIDs.begin(), team->memberIDs.end(), playerID);
if (index != team->memberIDs.end()) return;
team->memberIDs.push_back(playerID);
const auto& leader = GetPlayerData(team->leaderID);
const auto& member = GetPlayerData(playerID);
if (!leader || !member) return;
const auto leaderName = GeneralUtils::UTF8ToUTF16(leader.playerName);
const auto memberName = GeneralUtils::UTF8ToUTF16(member.playerName);
ChatPacketHandler::SendTeamInviteConfirm(member, false, leader.playerID, leader.zoneID, team->lootFlag, 0, 0, leaderName);
if (!team->local) {
ChatPacketHandler::SendTeamSetLeader(member, leader.playerID);
} else {
ChatPacketHandler::SendTeamSetLeader(member, LWOOBJID_EMPTY);
}
UpdateTeamsOnWorld(team, false);
for (const auto memberId : team->memberIDs) {
const auto& otherMember = GetPlayerData(memberId);
if (otherMember == member) continue;
const auto otherMemberName = GetName(memberId);
ChatPacketHandler::SendTeamAddPlayer(member, false, team->local, false, memberId, otherMemberName, otherMember ? otherMember.zoneID : LWOZONEID(0, 0, 0));
if (otherMember) {
ChatPacketHandler::SendTeamAddPlayer(otherMember, false, team->local, false, member.playerID, memberName, member.zoneID);
}
}
}
void PlayerContainer::RemoveMember(TeamData* team, LWOOBJID causingPlayerID, bool disband, bool kicked, bool leaving, bool silent) {
LOG_DEBUG("Player %llu is leaving team %i", causingPlayerID, team->teamID);
const auto index = std::ranges::find(team->memberIDs, causingPlayerID);
if (index == team->memberIDs.end()) return;
team->memberIDs.erase(index);
const auto& member = GetPlayerData(causingPlayerID);
const auto causingMemberName = GetName(causingPlayerID);
if (member && !silent) {
ChatPacketHandler::SendTeamRemovePlayer(member, disband, kicked, leaving, team->local, LWOOBJID_EMPTY, causingPlayerID, causingMemberName);
}
if (team->memberIDs.size() <= 1) {
DisbandTeam(team, causingPlayerID, causingMemberName);
} else /* team has enough members to be a team still */ {
team->leaderID = (causingPlayerID == team->leaderID) ? team->memberIDs[0] : team->leaderID;
for (const auto memberId : team->memberIDs) {
if (silent && memberId == causingPlayerID) {
continue;
}
const auto& otherMember = GetPlayerData(memberId);
if (!otherMember) continue;
ChatPacketHandler::SendTeamRemovePlayer(otherMember, disband, kicked, leaving, team->local, team->leaderID, causingPlayerID, causingMemberName);
}
UpdateTeamsOnWorld(team, false);
}
}
void PlayerContainer::PromoteMember(TeamData* team, LWOOBJID newLeader) {
team->leaderID = newLeader;
for (const auto memberId : team->memberIDs) {
const auto& otherMember = GetPlayerData(memberId);
if (!otherMember) continue;
ChatPacketHandler::SendTeamSetLeader(otherMember, newLeader);
}
}
void PlayerContainer::DisbandTeam(TeamData* team, const LWOOBJID causingPlayerID, const std::u16string& causingPlayerName) {
const auto index = std::ranges::find(GetTeams(), team);
if (index == GetTeams().end()) return;
LOG_DEBUG("Disbanding team %i", (*index)->teamID);
for (const auto memberId : team->memberIDs) {
const auto& otherMember = GetPlayerData(memberId);
if (!otherMember) continue;
ChatPacketHandler::SendTeamSetLeader(otherMember, LWOOBJID_EMPTY);
ChatPacketHandler::SendTeamRemovePlayer(otherMember, true, false, false, team->local, team->leaderID, causingPlayerID, causingPlayerName);
}
UpdateTeamsOnWorld(team, true);
GetTeamsMut().erase(index);
delete team;
}
void PlayerContainer::TeamStatusUpdate(TeamData* team) {
const auto index = std::find(GetTeams().begin(), GetTeams().end(), team);
if (index == GetTeams().end()) return;
const auto& leader = GetPlayerData(team->leaderID);
if (!leader) return;
const auto leaderName = GeneralUtils::UTF8ToUTF16(leader.playerName);
for (const auto memberId : team->memberIDs) {
const auto& otherMember = GetPlayerData(memberId);
if (!otherMember) continue;
if (!team->local) {
ChatPacketHandler::SendTeamStatus(otherMember, team->leaderID, leader.zoneID, team->lootFlag, 0, leaderName);
}
}
UpdateTeamsOnWorld(team, false);
}
void PlayerContainer::UpdateTeamsOnWorld(TeamData* team, bool deleteTeam) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::TEAM_GET_STATUS);
bitStream.Write(team->teamID);
bitStream.Write(deleteTeam);
if (!deleteTeam) {
bitStream.Write(team->lootFlag);
bitStream.Write<char>(team->memberIDs.size());
for (const auto memberID : team->memberIDs) {
bitStream.Write(memberID);
}
}
Game::server->Send(bitStream, UNASSIGNED_SYSTEM_ADDRESS, true);
}
std::u16string PlayerContainer::GetName(LWOOBJID playerID) {
const auto iter = m_Names.find(playerID);
@@ -444,5 +203,4 @@ void PlayerContainer::Shutdown() {
Database::Get()->UpdateActivityLog(id, eActivityType::PlayerLoggedOut, playerData.zoneID.GetMapID());
m_Players.erase(m_Players.begin());
}
for (auto* team : GetTeams()) if (team) delete team;
}

View File

@@ -11,10 +11,6 @@ enum class eGameMasterLevel : uint8_t;
struct TeamData;
struct TeamContainer {
std::vector<TeamData*> mTeams;
};
struct IgnoreData {
IgnoreData(const std::string& name, const LWOOBJID& id) : playerName{ name }, playerId{ id } {}
inline bool operator==(const std::string& other) const noexcept {
@@ -73,7 +69,6 @@ public:
void ScheduleRemovePlayer(Packet* packet);
void RemovePlayer(const LWOOBJID playerID);
void MuteUpdate(Packet* packet);
void CreateTeamServer(Packet* packet);
void BroadcastMuteUpdate(LWOOBJID player, time_t time);
void Shutdown();
@@ -81,34 +76,19 @@ public:
const PlayerData& GetPlayerData(const std::string& playerName);
PlayerData& GetPlayerDataMutable(const LWOOBJID& playerID);
PlayerData& GetPlayerDataMutable(const std::string& playerName);
std::u16string GetName(LWOOBJID playerID);
LWOOBJID GetId(const std::u16string& playerName);
void Update(const float deltaTime);
uint32_t GetPlayerCount() { return m_PlayerCount; };
uint32_t GetSimCount() { return m_SimCount; };
const std::map<LWOOBJID, PlayerData>& GetAllPlayers() const { return m_Players; };
TeamData* CreateLocalTeam(std::vector<LWOOBJID> members);
TeamData* CreateTeam(LWOOBJID leader, bool local = false);
TeamData* GetTeam(LWOOBJID playerID);
void AddMember(TeamData* team, LWOOBJID playerID);
void RemoveMember(TeamData* team, LWOOBJID playerID, bool disband, bool kicked, bool leaving, bool silent = false);
void PromoteMember(TeamData* team, LWOOBJID newLeader);
void DisbandTeam(TeamData* team, const LWOOBJID causingPlayerID, const std::u16string& causingPlayerName);
void TeamStatusUpdate(TeamData* team);
void UpdateTeamsOnWorld(TeamData* team, bool deleteTeam);
std::u16string GetName(LWOOBJID playerID);
LWOOBJID GetId(const std::u16string& playerName);
uint32_t GetMaxNumberOfBestFriends() { return m_MaxNumberOfBestFriends; }
uint32_t GetMaxNumberOfFriends() { return m_MaxNumberOfFriends; }
const TeamContainer& GetTeamContainer() { return m_TeamContainer; }
std::vector<TeamData*>& GetTeamsMut() { return m_TeamContainer.mTeams; };
const std::vector<TeamData*>& GetTeams() { return GetTeamsMut(); };
void Update(const float deltaTime);
bool PlayerBeingRemoved(const LWOOBJID playerID) { return m_PlayersToRemove.contains(playerID); }
private:
LWOOBJID m_TeamIDCounter = 0;
std::map<LWOOBJID, PlayerData> m_Players;
TeamContainer m_TeamContainer{};
std::unordered_map<LWOOBJID, std::u16string> m_Names;
std::map<LWOOBJID, float> m_PlayersToRemove;
uint32_t m_MaxNumberOfBestFriends = 5;

View File

@@ -0,0 +1,669 @@
#include "TeamContainer.h"
#include "ChatPackets.h"
#include "MessageType/Chat.h"
#include "MessageType/Game.h"
#include "ChatPacketHandler.h"
#include "PlayerContainer.h"
namespace {
TeamContainer::Data g_TeamContainer{};
LWOOBJID g_TeamIDCounter = 0;
}
const TeamContainer::Data& TeamContainer::GetTeamContainer() {
return g_TeamContainer;
}
std::vector<TeamData*>& TeamContainer::GetTeamsMut() {
return g_TeamContainer.mTeams;
}
const std::vector<TeamData*>& TeamContainer::GetTeams() {
return GetTeamsMut();
}
void TeamContainer::Shutdown() {
for (auto* team : g_TeamContainer.mTeams) if (team) delete team;
}
void TeamContainer::HandleTeamInvite(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID;
LUWString invitedPlayer;
inStream.Read(playerID);
inStream.IgnoreBytes(4);
inStream.Read(invitedPlayer);
const auto& player = Game::playerContainer.GetPlayerData(playerID);
if (!player) return;
auto* team = GetTeam(playerID);
if (team == nullptr) {
team = CreateTeam(playerID);
}
const auto& other = Game::playerContainer.GetPlayerData(invitedPlayer.GetAsString());
if (!other) return;
if (GetTeam(other.playerID) != nullptr) {
return;
}
if (team->memberIDs.size() > 3) {
// no more teams greater than 4
LOG("Someone tried to invite a 5th player to a team");
return;
}
SendTeamInvite(other, player);
LOG("Got team invite: %llu -> %s", playerID, invitedPlayer.GetAsString().c_str());
bool failed = false;
for (const auto& ignore : other.ignoredPlayers) {
if (ignore.playerId == player.playerID) {
failed = true;
break;
}
}
ChatPackets::TeamInviteInitialResponse response{};
response.inviteFailedToSend = failed;
response.playerName = invitedPlayer.string;
ChatPackets::SendRoutedMsg(response, playerID, player.worldServerSysAddr);
}
void TeamContainer::HandleTeamInviteResponse(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID = LWOOBJID_EMPTY;
inStream.Read(playerID);
uint32_t size = 0;
inStream.Read(size);
char declined = 0;
inStream.Read(declined);
LWOOBJID leaderID = LWOOBJID_EMPTY;
inStream.Read(leaderID);
LOG("Invite reponse received: %llu -> %llu (%d)", playerID, leaderID, declined);
if (declined) {
return;
}
auto* team = GetTeam(leaderID);
if (team == nullptr) {
LOG("Failed to find team for leader (%llu)", leaderID);
team = GetTeam(playerID);
}
if (team == nullptr) {
LOG("Failed to find team for player (%llu)", playerID);
return;
}
AddMember(team, playerID);
}
void TeamContainer::HandleTeamLeave(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID = LWOOBJID_EMPTY;
inStream.Read(playerID);
uint32_t size = 0;
inStream.Read(size);
auto* team = GetTeam(playerID);
LOG("(%llu) leaving team", playerID);
if (team != nullptr) {
RemoveMember(team, playerID, false, false, true);
}
}
void TeamContainer::HandleTeamKick(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID = LWOOBJID_EMPTY;
LUWString kickedPlayer;
inStream.Read(playerID);
inStream.IgnoreBytes(4);
inStream.Read(kickedPlayer);
LOG("(%llu) kicking (%s) from team", playerID, kickedPlayer.GetAsString().c_str());
const auto& kicked = Game::playerContainer.GetPlayerData(kickedPlayer.GetAsString());
LWOOBJID kickedId = LWOOBJID_EMPTY;
if (kicked) {
kickedId = kicked.playerID;
} else {
kickedId = Game::playerContainer.GetId(kickedPlayer.string);
}
if (kickedId == LWOOBJID_EMPTY) return;
auto* team = GetTeam(playerID);
if (team != nullptr) {
if (team->leaderID != playerID || team->leaderID == kickedId) return;
RemoveMember(team, kickedId, false, true, false);
}
}
void TeamContainer::HandleTeamPromote(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID = LWOOBJID_EMPTY;
LUWString promotedPlayer;
inStream.Read(playerID);
inStream.IgnoreBytes(4);
inStream.Read(promotedPlayer);
LOG("(%llu) promoting (%s) to team leader", playerID, promotedPlayer.GetAsString().c_str());
const auto& promoted = Game::playerContainer.GetPlayerData(promotedPlayer.GetAsString());
if (!promoted) return;
auto* team = GetTeam(playerID);
if (team != nullptr) {
if (team->leaderID != playerID) return;
PromoteMember(team, promoted.playerID);
}
}
void TeamContainer::HandleTeamLootOption(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID = LWOOBJID_EMPTY;
inStream.Read(playerID);
uint32_t size = 0;
inStream.Read(size);
char option;
inStream.Read(option);
auto* team = GetTeam(playerID);
if (team != nullptr) {
if (team->leaderID != playerID) return;
team->lootFlag = option;
TeamStatusUpdate(team);
UpdateTeamsOnWorld(team, false);
}
}
void TeamContainer::HandleTeamStatusRequest(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID = LWOOBJID_EMPTY;
inStream.Read(playerID);
auto* team = GetTeam(playerID);
const auto& data = Game::playerContainer.GetPlayerData(playerID);
if (team != nullptr && data) {
LOG_DEBUG("Player %llu is requesting team status", playerID);
if (team->local && data.zoneID.GetMapID() != team->zoneId.GetMapID() && data.zoneID.GetCloneID() != team->zoneId.GetCloneID()) {
RemoveMember(team, playerID, false, false, false, true);
return;
}
if (team->memberIDs.size() <= 1 && !team->local) {
DisbandTeam(team, LWOOBJID_EMPTY, u"");
return;
}
if (!team->local) {
SendTeamSetLeader(data, team->leaderID);
} else {
SendTeamSetLeader(data, LWOOBJID_EMPTY);
}
TeamStatusUpdate(team);
const auto leaderName = GeneralUtils::UTF8ToUTF16(data.playerName);
for (const auto memberId : team->memberIDs) {
const auto& otherMember = Game::playerContainer.GetPlayerData(memberId);
if (memberId == playerID) continue;
const auto memberName = Game::playerContainer.GetName(memberId);
if (otherMember) {
SendTeamSetOffWorldFlag(otherMember, data.playerID, data.zoneID);
}
SendTeamAddPlayer(data, false, team->local, false, memberId, memberName, otherMember ? otherMember.zoneID : LWOZONEID(0, 0, 0));
}
UpdateTeamsOnWorld(team, false);
}
}
void TeamContainer::SendTeamInvite(const PlayerData& receiver, const PlayerData& sender) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, MessageType::Client::TEAM_INVITE);
bitStream.Write(LUWString(sender.playerName.c_str()));
bitStream.Write(sender.playerID);
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void TeamContainer::SendTeamInviteConfirm(const PlayerData& receiver, bool bLeaderIsFreeTrial, LWOOBJID i64LeaderID, LWOZONEID i64LeaderZoneID, uint8_t ucLootFlag, uint8_t ucNumOfOtherPlayers, uint8_t ucResponseCode, std::u16string wsLeaderName) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
CMSGHEADER;
bitStream.Write(receiver.playerID);
bitStream.Write(MessageType::Game::TEAM_INVITE_CONFIRM);
bitStream.Write(bLeaderIsFreeTrial);
bitStream.Write(i64LeaderID);
bitStream.Write(i64LeaderZoneID);
bitStream.Write<uint32_t>(0); // BinaryBuffe, no clue what's in here
bitStream.Write(ucLootFlag);
bitStream.Write(ucNumOfOtherPlayers);
bitStream.Write(ucResponseCode);
bitStream.Write<uint32_t>(wsLeaderName.size());
for (const auto character : wsLeaderName) {
bitStream.Write(character);
}
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void TeamContainer::SendTeamStatus(const PlayerData& receiver, LWOOBJID i64LeaderID, LWOZONEID i64LeaderZoneID, uint8_t ucLootFlag, uint8_t ucNumOfOtherPlayers, std::u16string wsLeaderName) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
CMSGHEADER;
bitStream.Write(receiver.playerID);
bitStream.Write(MessageType::Game::TEAM_GET_STATUS_RESPONSE);
bitStream.Write(i64LeaderID);
bitStream.Write(i64LeaderZoneID);
bitStream.Write<uint32_t>(0); // BinaryBuffe, no clue what's in here
bitStream.Write(ucLootFlag);
bitStream.Write(ucNumOfOtherPlayers);
bitStream.Write<uint32_t>(wsLeaderName.size());
for (const auto character : wsLeaderName) {
bitStream.Write(character);
}
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void TeamContainer::SendTeamSetLeader(const PlayerData& receiver, LWOOBJID i64PlayerID) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
CMSGHEADER;
bitStream.Write(receiver.playerID);
bitStream.Write(MessageType::Game::TEAM_SET_LEADER);
bitStream.Write(i64PlayerID);
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void TeamContainer::SendTeamAddPlayer(const PlayerData& receiver, bool bIsFreeTrial, bool bLocal, bool bNoLootOnDeath, LWOOBJID i64PlayerID, std::u16string wsPlayerName, LWOZONEID zoneID) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
CMSGHEADER;
bitStream.Write(receiver.playerID);
bitStream.Write(MessageType::Game::TEAM_ADD_PLAYER);
bitStream.Write(bIsFreeTrial);
bitStream.Write(bLocal);
bitStream.Write(bNoLootOnDeath);
bitStream.Write(i64PlayerID);
bitStream.Write<uint32_t>(wsPlayerName.size());
for (const auto character : wsPlayerName) {
bitStream.Write(character);
}
bitStream.Write1();
if (receiver.zoneID.GetCloneID() == zoneID.GetCloneID()) {
zoneID = LWOZONEID(zoneID.GetMapID(), zoneID.GetInstanceID(), 0);
}
bitStream.Write(zoneID);
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void TeamContainer::SendTeamRemovePlayer(const PlayerData& receiver, bool bDisband, bool bIsKicked, bool bIsLeaving, bool bLocal, LWOOBJID i64LeaderID, LWOOBJID i64PlayerID, std::u16string wsPlayerName) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
CMSGHEADER;
bitStream.Write(receiver.playerID);
bitStream.Write(MessageType::Game::TEAM_REMOVE_PLAYER);
bitStream.Write(bDisband);
bitStream.Write(bIsKicked);
bitStream.Write(bIsLeaving);
bitStream.Write(bLocal);
bitStream.Write(i64LeaderID);
bitStream.Write(i64PlayerID);
bitStream.Write<uint32_t>(wsPlayerName.size());
for (const auto character : wsPlayerName) {
bitStream.Write(character);
}
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void TeamContainer::SendTeamSetOffWorldFlag(const PlayerData& receiver, LWOOBJID i64PlayerID, LWOZONEID zoneID) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::WORLD_ROUTE_PACKET);
bitStream.Write(receiver.playerID);
//portion that will get routed:
CMSGHEADER;
bitStream.Write(receiver.playerID);
bitStream.Write(MessageType::Game::TEAM_SET_OFF_WORLD_FLAG);
bitStream.Write(i64PlayerID);
if (receiver.zoneID.GetCloneID() == zoneID.GetCloneID()) {
zoneID = LWOZONEID(zoneID.GetMapID(), zoneID.GetInstanceID(), 0);
}
bitStream.Write(zoneID);
SystemAddress sysAddr = receiver.worldServerSysAddr;
SEND_PACKET;
}
void TeamContainer::CreateTeamServer(Packet* packet) {
CINSTREAM_SKIP_HEADER;
LWOOBJID playerID;
inStream.Read(playerID);
size_t membersSize = 0;
inStream.Read(membersSize);
if (membersSize >= 4) {
LOG("Tried to create a team with more than 4 players");
return;
}
std::vector<LWOOBJID> members;
members.reserve(membersSize);
for (size_t i = 0; i < membersSize; i++) {
LWOOBJID member;
inStream.Read(member);
members.push_back(member);
}
LWOZONEID zoneId;
inStream.Read(zoneId);
auto* team = CreateLocalTeam(members);
if (team != nullptr) {
team->zoneId = zoneId;
UpdateTeamsOnWorld(team, false);
}
}
TeamData* TeamContainer::CreateLocalTeam(std::vector<LWOOBJID> members) {
if (members.empty()) {
return nullptr;
}
TeamData* newTeam = nullptr;
for (const auto member : members) {
auto* team = GetTeam(member);
if (team != nullptr) {
RemoveMember(team, member, false, false, true);
}
if (newTeam == nullptr) {
newTeam = CreateTeam(member, true);
} else {
AddMember(newTeam, member);
}
}
newTeam->lootFlag = 1;
TeamStatusUpdate(newTeam);
return newTeam;
}
TeamData* TeamContainer::CreateTeam(LWOOBJID leader, bool local) {
auto* team = new TeamData();
team->teamID = ++g_TeamIDCounter;
team->leaderID = leader;
team->local = local;
GetTeamsMut().push_back(team);
AddMember(team, leader);
return team;
}
TeamData* TeamContainer::GetTeam(LWOOBJID playerID) {
for (auto* team : GetTeams()) {
if (std::find(team->memberIDs.begin(), team->memberIDs.end(), playerID) == team->memberIDs.end()) continue;
return team;
}
return nullptr;
}
void TeamContainer::AddMember(TeamData* team, LWOOBJID playerID) {
if (team->memberIDs.size() >= 4) {
LOG("Tried to add player to team that already had 4 players");
const auto& player = Game::playerContainer.GetPlayerData(playerID);
if (!player) return;
ChatPackets::SendSystemMessage(player.worldServerSysAddr, u"The teams is full! You have not been added to a team!");
return;
}
const auto index = std::find(team->memberIDs.begin(), team->memberIDs.end(), playerID);
if (index != team->memberIDs.end()) return;
team->memberIDs.push_back(playerID);
const auto& leader = Game::playerContainer.GetPlayerData(team->leaderID);
const auto& member = Game::playerContainer.GetPlayerData(playerID);
if (!leader || !member) return;
const auto leaderName = GeneralUtils::UTF8ToUTF16(leader.playerName);
const auto memberName = GeneralUtils::UTF8ToUTF16(member.playerName);
SendTeamInviteConfirm(member, false, leader.playerID, leader.zoneID, team->lootFlag, 0, 0, leaderName);
if (!team->local) {
SendTeamSetLeader(member, leader.playerID);
} else {
SendTeamSetLeader(member, LWOOBJID_EMPTY);
}
UpdateTeamsOnWorld(team, false);
for (const auto memberId : team->memberIDs) {
const auto& otherMember = Game::playerContainer.GetPlayerData(memberId);
if (otherMember == member) continue;
const auto otherMemberName = Game::playerContainer.GetName(memberId);
SendTeamAddPlayer(member, false, team->local, false, memberId, otherMemberName, otherMember ? otherMember.zoneID : LWOZONEID(0, 0, 0));
if (otherMember) {
SendTeamAddPlayer(otherMember, false, team->local, false, member.playerID, memberName, member.zoneID);
}
}
}
void TeamContainer::RemoveMember(TeamData* team, LWOOBJID causingPlayerID, bool disband, bool kicked, bool leaving, bool silent) {
LOG_DEBUG("Player %llu is leaving team %i", causingPlayerID, team->teamID);
const auto index = std::ranges::find(team->memberIDs, causingPlayerID);
if (index == team->memberIDs.end()) return;
team->memberIDs.erase(index);
const auto& member = Game::playerContainer.GetPlayerData(causingPlayerID);
const auto causingMemberName = Game::playerContainer.GetName(causingPlayerID);
if (member && !silent) {
SendTeamRemovePlayer(member, disband, kicked, leaving, team->local, LWOOBJID_EMPTY, causingPlayerID, causingMemberName);
}
if (team->memberIDs.size() <= 1) {
DisbandTeam(team, causingPlayerID, causingMemberName);
} else /* team has enough members to be a team still */ {
team->leaderID = (causingPlayerID == team->leaderID) ? team->memberIDs[0] : team->leaderID;
for (const auto memberId : team->memberIDs) {
if (silent && memberId == causingPlayerID) {
continue;
}
const auto& otherMember = Game::playerContainer.GetPlayerData(memberId);
if (!otherMember) continue;
SendTeamRemovePlayer(otherMember, disband, kicked, leaving, team->local, team->leaderID, causingPlayerID, causingMemberName);
}
UpdateTeamsOnWorld(team, false);
}
}
void TeamContainer::PromoteMember(TeamData* team, LWOOBJID newLeader) {
team->leaderID = newLeader;
for (const auto memberId : team->memberIDs) {
const auto& otherMember = Game::playerContainer.GetPlayerData(memberId);
if (!otherMember) continue;
SendTeamSetLeader(otherMember, newLeader);
}
}
void TeamContainer::DisbandTeam(TeamData* team, const LWOOBJID causingPlayerID, const std::u16string& causingPlayerName) {
const auto index = std::ranges::find(GetTeams(), team);
if (index == GetTeams().end()) return;
LOG_DEBUG("Disbanding team %i", (*index)->teamID);
for (const auto memberId : team->memberIDs) {
const auto& otherMember = Game::playerContainer.GetPlayerData(memberId);
if (!otherMember) continue;
SendTeamSetLeader(otherMember, LWOOBJID_EMPTY);
SendTeamRemovePlayer(otherMember, true, false, false, team->local, team->leaderID, causingPlayerID, causingPlayerName);
}
UpdateTeamsOnWorld(team, true);
GetTeamsMut().erase(index);
delete team;
}
void TeamContainer::TeamStatusUpdate(TeamData* team) {
const auto index = std::find(GetTeams().begin(), GetTeams().end(), team);
if (index == GetTeams().end()) return;
const auto& leader = Game::playerContainer.GetPlayerData(team->leaderID);
if (!leader) return;
const auto leaderName = GeneralUtils::UTF8ToUTF16(leader.playerName);
for (const auto memberId : team->memberIDs) {
const auto& otherMember = Game::playerContainer.GetPlayerData(memberId);
if (!otherMember) continue;
if (!team->local) {
SendTeamStatus(otherMember, team->leaderID, leader.zoneID, team->lootFlag, 0, leaderName);
}
}
UpdateTeamsOnWorld(team, false);
}
void TeamContainer::UpdateTeamsOnWorld(TeamData* team, bool deleteTeam) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::TEAM_GET_STATUS);
bitStream.Write(team->teamID);
bitStream.Write(deleteTeam);
if (!deleteTeam) {
bitStream.Write(team->lootFlag);
bitStream.Write<char>(team->memberIDs.size());
for (const auto memberID : team->memberIDs) {
bitStream.Write(memberID);
}
}
Game::server->Send(bitStream, UNASSIGNED_SYSTEM_ADDRESS, true);
}

View File

@@ -0,0 +1,59 @@
// Darkflame Universe
// Copyright 2025
#ifndef TEAMCONTAINER_H
#define TEAMCONTAINER_H
#include <cstdint>
#include <string>
#include <vector>
#include "dCommonVars.h"
struct Packet;
struct PlayerData;
struct TeamData;
namespace TeamContainer {
struct Data {
std::vector<TeamData*> mTeams;
};
void Shutdown();
void HandleTeamInvite(Packet* packet);
void HandleTeamInviteResponse(Packet* packet);
void HandleTeamLeave(Packet* packet);
void HandleTeamKick(Packet* packet);
void HandleTeamPromote(Packet* packet);
void HandleTeamLootOption(Packet* packet);
void HandleTeamStatusRequest(Packet* packet);
void SendTeamInvite(const PlayerData& receiver, const PlayerData& sender);
void SendTeamInviteConfirm(const PlayerData& receiver, bool bLeaderIsFreeTrial, LWOOBJID i64LeaderID, LWOZONEID i64LeaderZoneID, uint8_t ucLootFlag, uint8_t ucNumOfOtherPlayers, uint8_t ucResponseCode, std::u16string wsLeaderName);
void SendTeamStatus(const PlayerData& receiver, LWOOBJID i64LeaderID, LWOZONEID i64LeaderZoneID, uint8_t ucLootFlag, uint8_t ucNumOfOtherPlayers, std::u16string wsLeaderName);
void SendTeamSetLeader(const PlayerData& receiver, LWOOBJID i64PlayerID);
void SendTeamAddPlayer(const PlayerData& receiver, bool bIsFreeTrial, bool bLocal, bool bNoLootOnDeath, LWOOBJID i64PlayerID, std::u16string wsPlayerName, LWOZONEID zoneID);
/* Sends a message to the provided `receiver` with information about the updated team. If `i64LeaderID` is not LWOOBJID_EMPTY, the client will update the leader to that new playerID. */
void SendTeamRemovePlayer(const PlayerData& receiver, bool bDisband, bool bIsKicked, bool bIsLeaving, bool bLocal, LWOOBJID i64LeaderID, LWOOBJID i64PlayerID, std::u16string wsPlayerName);
void SendTeamSetOffWorldFlag(const PlayerData& receiver, LWOOBJID i64PlayerID, LWOZONEID zoneID);
void CreateTeamServer(Packet* packet);
TeamData* CreateLocalTeam(std::vector<LWOOBJID> members);
TeamData* CreateTeam(LWOOBJID leader, bool local = false);
TeamData* GetTeam(LWOOBJID playerID);
void AddMember(TeamData* team, LWOOBJID playerID);
void RemoveMember(TeamData* team, LWOOBJID playerID, bool disband, bool kicked, bool leaving, bool silent = false);
void PromoteMember(TeamData* team, LWOOBJID newLeader);
void DisbandTeam(TeamData* team, const LWOOBJID causingPlayerID, const std::u16string& causingPlayerName);
void TeamStatusUpdate(TeamData* team);
void UpdateTeamsOnWorld(TeamData* team, bool deleteTeam);
const TeamContainer::Data& GetTeamContainer();
std::vector<TeamData*>& GetTeamsMut();
const std::vector<TeamData*>& GetTeams();
};
#endif //!TEAMCONTAINER_H

View File

@@ -8,6 +8,7 @@
#include "Database.h"
#include "Game.h"
#include "Sd0.h"
#include "ZCompression.h"
#include "Logger.h"
@@ -44,10 +45,10 @@ uint32_t BrickByBrickFix::TruncateBrokenBrickByBrickXml() {
}
// Ignore the valgrind warning about uninitialized values. These are discarded later when we know the actual uncompressed size.
std::unique_ptr<uint8_t[]> uncompressedChunk(new uint8_t[ZCompression::MAX_SD0_CHUNK_SIZE]);
std::unique_ptr<uint8_t[]> uncompressedChunk(new uint8_t[Sd0::MAX_UNCOMPRESSED_CHUNK_SIZE]);
int32_t err{};
int32_t actualUncompressedSize = ZCompression::Decompress(
compressedChunk.get(), chunkSize, uncompressedChunk.get(), ZCompression::MAX_SD0_CHUNK_SIZE, err);
compressedChunk.get(), chunkSize, uncompressedChunk.get(), Sd0::MAX_UNCOMPRESSED_CHUNK_SIZE, err);
if (actualUncompressedSize != -1) {
uint32_t previousSize = completeUncompressedModel.size();
@@ -117,7 +118,7 @@ uint32_t BrickByBrickFix::UpdateBrickByBrickModelsToSd0() {
}
std::string outputString(sd0ConvertedModel.get(), oldLxfmlSizeWithHeader);
std::istringstream outputStringStream(outputString);
std::stringstream outputStringStream(outputString);
try {
Database::Get()->UpdateUgcModelData(model.id, outputStringStream);

View File

@@ -16,6 +16,10 @@ set(DCOMMON_SOURCES
"BrickByBrickFix.cpp"
"BinaryPathFinder.cpp"
"FdbToSqlite.cpp"
"JSONUtils.cpp"
"TinyXmlUtils.cpp"
"Sd0.cpp"
"Lxfml.cpp"
)
# Workaround for compiler bug where the optimized code could result in a memcpy of 0 bytes, even though that isnt possible.

View File

@@ -4,7 +4,7 @@
#include <assert.h>
#ifdef _DEBUG
# define DluAssert(expression) assert(expression)
# define DluAssert(expression) do { assert(expression) } while(0)
#else
# define DluAssert(expression)
#endif

17
dCommon/JSONUtils.cpp Normal file
View File

@@ -0,0 +1,17 @@
#include "JSONUtils.h"
#include "json.hpp"
using json = nlohmann::json;
std::string JSONUtils::CheckRequiredData(const json& data, const std::vector<std::string>& requiredData) {
json check;
check["error"] = json::array();
for (const auto& required : requiredData) {
if (!data.contains(required)) {
check["error"].push_back("Missing Parameter: " + required);
} else if (data[required] == "") {
check["error"].push_back("Empty Parameter: " + required);
}
}
return check["error"].empty() ? "" : check.dump();
}

11
dCommon/JSONUtils.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef _JSONUTILS_H_
#define _JSONUTILS_H_
#include "json_fwd.hpp"
namespace JSONUtils {
// check required fields in json data
std::string CheckRequiredData(const nlohmann::json& data, const std::vector<std::string>& requiredData);
}
#endif // _JSONUTILS_H_

View File

@@ -29,8 +29,8 @@ constexpr const char* GetFileNameFromAbsolutePath(const char* path) {
// they will not be valid constexpr and will be evaluated at runtime instead of compile time!
// The full string is still stored in the binary, however the offset of the filename in the absolute paths
// is used in the instruction instead of the start of the absolute path.
#define LOG(message, ...) do { auto str = FILENAME_AND_LINE; Game::logger->Log(str, message, ##__VA_ARGS__); } while(0)
#define LOG_DEBUG(message, ...) do { auto str = FILENAME_AND_LINE; Game::logger->LogDebug(str, message, ##__VA_ARGS__); } while(0)
#define LOG(message, ...) do { auto str_ = FILENAME_AND_LINE; Game::logger->Log(str_, message, ##__VA_ARGS__); } while(0)
#define LOG_DEBUG(message, ...) do { auto str_ = FILENAME_AND_LINE; Game::logger->LogDebug(str_, message, ##__VA_ARGS__); } while(0)
// Writer class for writing data to files.
class Writer {

115
dCommon/Lxfml.cpp Normal file
View File

@@ -0,0 +1,115 @@
#include "Lxfml.h"
#include "GeneralUtils.h"
#include "StringifiedEnum.h"
#include "TinyXmlUtils.h"
#include <ranges>
Lxfml::Result Lxfml::NormalizePosition(const std::string_view data) {
Result toReturn;
tinyxml2::XMLDocument doc;
const auto err = doc.Parse(data.data());
if (err != tinyxml2::XML_SUCCESS) {
LOG("Failed to parse xml %s.", StringifiedEnum::ToString(err).data());
return toReturn;
}
TinyXmlUtils::DocumentReader reader(doc);
std::map<std::string/* refID */, std::string> transformations;
auto lxfml = reader["LXFML"];
if (!lxfml) {
LOG("Failed to find LXFML element.");
return toReturn;
}
// First get all the positions of bricks
for (const auto& brick : lxfml["Bricks"]) {
const auto* part = brick.FirstChildElement("Part");
if (part) {
const auto* bone = part->FirstChildElement("Bone");
if (bone) {
auto* transformation = bone->Attribute("transformation");
if (transformation) {
auto* refID = bone->Attribute("refID");
if (refID) transformations[refID] = transformation;
}
}
}
}
// These points are well out of bounds for an actual player
NiPoint3 lowest{ 10'000.0f, 10'000.0f, 10'000.0f };
NiPoint3 highest{ -10'000.0f, -10'000.0f, -10'000.0f };
// Calculate the lowest and highest points on the entire model
for (const auto& transformation : transformations | std::views::values) {
auto split = GeneralUtils::SplitString(transformation, ',');
if (split.size() < 12) {
LOG("Not enough in the split?");
continue;
}
auto x = GeneralUtils::TryParse<float>(split[9]).value();
auto y = GeneralUtils::TryParse<float>(split[10]).value();
auto z = GeneralUtils::TryParse<float>(split[11]).value();
if (x < lowest.x) lowest.x = x;
if (y < lowest.y) lowest.y = y;
if (z < lowest.z) lowest.z = z;
if (highest.x < x) highest.x = x;
if (highest.y < y) highest.y = y;
if (highest.z < z) highest.z = z;
}
auto delta = (highest - lowest) / 2.0f;
auto newRootPos = lowest + delta;
// Clamp the Y to the lowest point on the model
newRootPos.y = lowest.y;
// Adjust all positions to account for the new origin
for (auto& transformation : transformations | std::views::values) {
auto split = GeneralUtils::SplitString(transformation, ',');
if (split.size() < 12) {
LOG("Not enough in the split?");
continue;
}
auto x = GeneralUtils::TryParse<float>(split[9]).value() - newRootPos.x;
auto y = GeneralUtils::TryParse<float>(split[10]).value() - newRootPos.y;
auto z = GeneralUtils::TryParse<float>(split[11]).value() - newRootPos.z;
std::stringstream stream;
for (int i = 0; i < 9; i++) {
stream << split[i];
stream << ',';
}
stream << x << ',' << y << ',' << z;
transformation = stream.str();
}
// Finally write the new transformation back into the lxfml
for (auto& brick : lxfml["Bricks"]) {
auto* part = brick.FirstChildElement("Part");
if (part) {
auto* bone = part->FirstChildElement("Bone");
if (bone) {
auto* transformation = bone->Attribute("transformation");
if (transformation) {
auto* refID = bone->Attribute("refID");
if (refID) {
bone->SetAttribute("transformation", transformations[refID].c_str());
}
}
}
}
}
tinyxml2::XMLPrinter printer;
doc.Print(&printer);
toReturn.lxfml = printer.CStr();
toReturn.center = newRootPos;
return toReturn;
}

23
dCommon/Lxfml.h Normal file
View File

@@ -0,0 +1,23 @@
// Darkflame Universe
// Copyright 2025
#ifndef LXFML_H
#define LXFML_H
#include <string>
#include <string_view>
#include "NiPoint3.h"
namespace Lxfml {
struct Result {
std::string lxfml;
NiPoint3 center;
};
// Normalizes a LXFML model to be positioned relative to its local 0, 0, 0 rather than a game worlds 0, 0, 0.
// Returns a struct of its new center and the updated LXFML containing these edits.
[[nodiscard]] Result NormalizePosition(const std::string_view data);
};
#endif //!LXFML_H

150
dCommon/Sd0.cpp Normal file
View File

@@ -0,0 +1,150 @@
#include "Sd0.h"
#include <array>
#include <ranges>
#include "BinaryIO.h"
#include "Game.h"
#include "Logger.h"
#include "ZCompression.h"
// Insert header if on first buffer
void WriteHeader(Sd0::BinaryBuffer& chunk) {
chunk.push_back(Sd0::SD0_HEADER[0]);
chunk.push_back(Sd0::SD0_HEADER[1]);
chunk.push_back(Sd0::SD0_HEADER[2]);
chunk.push_back(Sd0::SD0_HEADER[3]);
chunk.push_back(Sd0::SD0_HEADER[4]);
}
// Write the size of the buffer to a chunk
void WriteSize(Sd0::BinaryBuffer& chunk, uint32_t chunkSize) {
for (int i = 0; i < 4; i++) {
char toPush = chunkSize & 0xff;
chunkSize = chunkSize >> 8;
chunk.push_back(toPush);
}
}
int32_t GetDataOffset(bool firstBuffer) {
return firstBuffer ? 9 : 4;
}
Sd0::Sd0(std::istream& buffer) {
char header[5]{};
// Check if this is an sd0 buffer. It's possible we may be handed a zlib buffer directly due to old code so check for that too.
if (!BinaryIO::BinaryRead(buffer, header) || memcmp(header, SD0_HEADER, sizeof(header)) != 0) {
LOG("Failed to read SD0 header %i %i %i %i %i %i %i", buffer.good(), buffer.tellg(), header[0], header[1], header[2], header[3], header[4]);
LOG_DEBUG("This may be a zlib buffer directly? Trying again assuming its a zlib buffer.");
auto& firstChunk = m_Chunks.emplace_back();
WriteHeader(firstChunk);
buffer.seekg(0, std::ios::end);
uint32_t bufferSize = buffer.tellg();
buffer.seekg(0, std::ios::beg);
WriteSize(firstChunk, bufferSize);
firstChunk.resize(firstChunk.size() + bufferSize);
auto* dataStart = reinterpret_cast<char*>(firstChunk.data() + GetDataOffset(true));
if (!buffer.read(dataStart, bufferSize)) {
m_Chunks.pop_back();
LOG("Failed to read %u bytes from chunk %i", bufferSize, m_Chunks.size() - 1);
}
return;
}
while (buffer && buffer.peek() != std::istream::traits_type::eof()) {
uint32_t chunkSize{};
if (!BinaryIO::BinaryRead(buffer, chunkSize)) {
LOG("Failed to read chunk size from stream %lld %zu", buffer.tellg(), m_Chunks.size());
break;
}
auto& chunk = m_Chunks.emplace_back();
bool firstBuffer = m_Chunks.size() == 1;
auto dataOffset = GetDataOffset(firstBuffer);
// Insert header if on first buffer
if (firstBuffer) {
WriteHeader(chunk);
}
WriteSize(chunk, chunkSize);
chunk.resize(chunkSize + dataOffset);
auto* dataStart = reinterpret_cast<char*>(chunk.data() + dataOffset);
if (!buffer.read(dataStart, chunkSize)) {
m_Chunks.pop_back();
LOG("Failed to read %u bytes from chunk %i", chunkSize, m_Chunks.size() - 1);
break;
}
}
}
void Sd0::FromData(const uint8_t* data, size_t bufferSize) {
const auto originalBufferSize = bufferSize;
if (bufferSize == 0) return;
m_Chunks.clear();
while (bufferSize > 0) {
const auto numToCopy = std::min(MAX_UNCOMPRESSED_CHUNK_SIZE, bufferSize);
const auto* startOffset = data + originalBufferSize - bufferSize;
bufferSize -= numToCopy;
std::array<uint8_t, MAX_UNCOMPRESSED_CHUNK_SIZE> compressedChunk;
const auto compressedSize = ZCompression::Compress(
startOffset, numToCopy,
compressedChunk.data(), compressedChunk.size());
auto& chunk = m_Chunks.emplace_back();
bool firstBuffer = m_Chunks.size() == 1;
auto dataOffset = GetDataOffset(firstBuffer);
if (firstBuffer) {
WriteHeader(chunk);
}
WriteSize(chunk, compressedSize);
chunk.resize(compressedSize + dataOffset);
memcpy(chunk.data() + dataOffset, compressedChunk.data(), compressedSize);
}
}
std::string Sd0::GetAsStringUncompressed() const {
std::string toReturn;
bool first = true;
uint32_t totalSize{};
for (const auto& chunk : m_Chunks) {
auto dataOffset = GetDataOffset(first);
first = false;
const auto chunkSize = chunk.size();
auto oldSize = toReturn.size();
toReturn.resize(oldSize + MAX_UNCOMPRESSED_CHUNK_SIZE);
int32_t error{};
const auto uncompressedSize = ZCompression::Decompress(
chunk.data() + dataOffset, chunkSize - dataOffset,
reinterpret_cast<uint8_t*>(toReturn.data()) + oldSize, MAX_UNCOMPRESSED_CHUNK_SIZE,
error);
totalSize += uncompressedSize;
}
toReturn.resize(totalSize);
return toReturn;
}
std::stringstream Sd0::GetAsStream() const {
std::stringstream toReturn;
for (const auto& chunk : m_Chunks) {
toReturn.write(reinterpret_cast<const char*>(chunk.data()), chunk.size());
}
return toReturn;
}
const std::vector<Sd0::BinaryBuffer>& Sd0::GetAsVector() const {
return m_Chunks;
}

42
dCommon/Sd0.h Normal file
View File

@@ -0,0 +1,42 @@
// Darkflame Universe
// Copyright 2025
#ifndef SD0_H
#define SD0_H
#include <fstream>
#include <vector>
// Sd0 is comprised of multiple zlib compressed buffers stored in a row.
// The format starts with a SD0 header (see SD0_HEADER) followed by the size of a zlib buffer, and then the zlib buffer itself.
// This repeats until end of file
class Sd0 {
public:
using BinaryBuffer = std::vector<uint8_t>;
static inline const char* SD0_HEADER = "sd0\x01\xff";
/**
* @brief Max size of an inflated sd0 zlib chunk
*/
static constexpr inline size_t MAX_UNCOMPRESSED_CHUNK_SIZE = 1024 * 256;
// Read the input buffer into an internal chunk stream to be used later
Sd0(std::istream& buffer);
// Uncompresses the entire Sd0 buffer and returns it as a string
[[nodiscard]] std::string GetAsStringUncompressed() const;
// Gets the Sd0 buffer as a stream in its raw compressed form
[[nodiscard]] std::stringstream GetAsStream() const;
// Gets the Sd0 buffer as a vector in its raw compressed form
[[nodiscard]] const std::vector<BinaryBuffer>& GetAsVector() const;
// Compress data into a Sd0 buffer
void FromData(const uint8_t* data, size_t bufferSize);
private:
std::vector<BinaryBuffer> m_Chunks{};
};
#endif //!SD0_H

37
dCommon/TinyXmlUtils.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include "TinyXmlUtils.h"
#include <tinyxml2.h>
using namespace TinyXmlUtils;
Element DocumentReader::operator[](const std::string_view elem) const {
return Element(m_Doc.FirstChildElement(elem.empty() ? nullptr : elem.data()), elem);
}
Element::Element(tinyxml2::XMLElement* xmlElem, const std::string_view elem) :
m_IteratedName{ elem },
m_Elem{ xmlElem } {
}
Element Element::operator[](const std::string_view elem) const {
const auto* usedElem = elem.empty() ? nullptr : elem.data();
auto* toReturn = m_Elem ? m_Elem->FirstChildElement(usedElem) : nullptr;
return Element(toReturn, m_IteratedName);
}
ElementIterator Element::begin() {
return ElementIterator(m_Elem ? m_Elem->FirstChildElement() : nullptr);
}
ElementIterator Element::end() {
return ElementIterator(nullptr);
}
ElementIterator::ElementIterator(tinyxml2::XMLElement* elem) :
m_CurElem{ elem } {
}
ElementIterator& ElementIterator::operator++() {
if (m_CurElem) m_CurElem = m_CurElem->NextSiblingElement();
return *this;
}

66
dCommon/TinyXmlUtils.h Normal file
View File

@@ -0,0 +1,66 @@
// Darkflame Universe
// Copyright 2025
#ifndef TINYXMLUTILS_H
#define TINYXMLUTILS_H
#include <string>
#include "DluAssert.h"
#include <tinyxml2.h>
namespace TinyXmlUtils {
// See cstdlib for iterator technicalities
struct ElementIterator {
ElementIterator(tinyxml2::XMLElement* elem);
ElementIterator& operator++();
[[nodiscard]] tinyxml2::XMLElement* operator->() { DluAssert(m_CurElem); return m_CurElem; }
[[nodiscard]] tinyxml2::XMLElement& operator*() { DluAssert(m_CurElem); return *m_CurElem; }
bool operator==(const ElementIterator& other) const { return other.m_CurElem == m_CurElem; }
private:
tinyxml2::XMLElement* m_CurElem{ nullptr };
};
// Wrapper class to act as an iterator over xml elements.
// All the normal rules that apply to Iterators in the std library apply here.
class Element {
public:
Element(tinyxml2::XMLElement* xmlElem, const std::string_view elem);
// The first child element of this element.
[[nodiscard]] ElementIterator begin();
// Always returns an ElementIterator which points to nullptr.
// TinyXml2 return NULL when you've reached the last child element so
// you can't do any funny one past end logic here.
[[nodiscard]] ElementIterator end();
// Get a child element
[[nodiscard]] Element operator[](const std::string_view elem) const;
[[nodiscard]] Element operator[](const char* elem) const { return operator[](std::string_view(elem)); };
// Whether or not data exists for this element
operator bool() const { return m_Elem != nullptr; }
[[nodiscard]] const tinyxml2::XMLElement* operator->() const { return m_Elem; }
private:
const char* GetElementName() const { return m_IteratedName.empty() ? nullptr : m_IteratedName.c_str(); }
const std::string m_IteratedName;
tinyxml2::XMLElement* m_Elem;
};
class DocumentReader {
public:
DocumentReader(tinyxml2::XMLDocument& doc) : m_Doc{ doc } {}
[[nodiscard]] Element operator[](const std::string_view elem) const;
private:
tinyxml2::XMLDocument& m_Doc;
};
};
#endif //!TINYXMLUTILS_H

View File

@@ -8,11 +8,5 @@ namespace ZCompression {
int32_t Compress(const uint8_t* abSrc, int32_t nLenSrc, uint8_t* abDst, int32_t nLenDst);
int32_t Decompress(const uint8_t* abSrc, int32_t nLenSrc, uint8_t* abDst, int32_t nLenDst, int32_t& nErr);
/**
* @brief Max size of an inflated sd0 zlib chunk
*
*/
constexpr uint32_t MAX_SD0_CHUNK_SIZE = 1024 * 256;
}

View File

@@ -1,6 +1,7 @@
#include "Pack.h"
#include "BinaryIO.h"
#include "Sd0.h"
#include "ZCompression.h"
Pack::Pack(const std::filesystem::path& filePath) {
@@ -106,7 +107,7 @@ bool Pack::ReadFileFromPack(const uint32_t crc, char** data, uint32_t* len) cons
pos += size; // Move pointer position the amount of bytes read to the right
int32_t err;
currentReadPos += ZCompression::Decompress(reinterpret_cast<uint8_t*>(chunk), size, reinterpret_cast<uint8_t*>(decompressedData + currentReadPos), ZCompression::MAX_SD0_CHUNK_SIZE, err);
currentReadPos += ZCompression::Decompress(reinterpret_cast<uint8_t*>(chunk), size, reinterpret_cast<uint8_t*>(decompressedData + currentReadPos), Sd0::MAX_UNCOMPRESSED_CHUNK_SIZE, err);
free(chunk);
}

View File

@@ -3,13 +3,14 @@
#ifndef __DCOMMONVARS__H__
#define __DCOMMONVARS__H__
#include <compare>
#include <cstdint>
#include <string>
#include <set>
#include <string>
#include "BitStream.h"
#include "eConnectionType.h"
#include "MessageType/Client.h"
#include "BitStreamUtils.h"
#include "MessageType/Client.h"
#include "eConnectionType.h"
#pragma warning (disable:4251) //Disables SQL warnings
@@ -99,6 +100,7 @@ public:
constexpr LWOZONEID(const LWOMAPID& mapID, const LWOINSTANCEID& instanceID, const LWOCLONEID& cloneID) noexcept { m_MapID = mapID; m_InstanceID = instanceID; m_CloneID = cloneID; }
constexpr LWOZONEID(const LWOZONEID& replacement) noexcept { *this = replacement; }
constexpr bool operator==(const LWOZONEID&) const = default;
constexpr auto operator<=>(const LWOZONEID&) const = default;
private:
LWOMAPID m_MapID = LWOMAPID_INVALID; //1000 for VE, 1100 for AG, etc...

View File

@@ -16,7 +16,9 @@ enum class eCharacterVersion : uint32_t {
VAULT_SIZE,
// Fixes speed base value in level component
SPEED_BASE,
UP_TO_DATE, // will become NJ_JAYMISSIONS
// Fixes nexus force explorer missions
NJ_JAYMISSIONS,
UP_TO_DATE, // will become NEXUS_FORCE_EXPLORER
};
#endif //!__ECHARACTERVERSION__H__

View File

@@ -1,6 +1,8 @@
#ifndef __EHTTPMETHODS__H__
#define __EHTTPMETHODS__H__
#include "dPlatforms.h"
#ifdef DARKFLAME_PLATFORM_WIN32
#pragma push_macro("DELETE")
#undef DELETE

View File

@@ -102,7 +102,6 @@ DEFINE_TABLE_STORAGE(CDScriptComponentTable);
DEFINE_TABLE_STORAGE(CDSkillBehaviorTable);
DEFINE_TABLE_STORAGE(CDTamingBuildPuzzleTable);
DEFINE_TABLE_STORAGE(CDVendorComponentTable);
DEFINE_TABLE_STORAGE(CDZoneTableTable);
void CDClientManager::LoadValuesFromDatabase() {
if (!CDClientDatabase::isConnected) {
@@ -149,7 +148,7 @@ void CDClientManager::LoadValuesFromDatabase() {
CDSkillBehaviorTable::Instance().LoadValuesFromDatabase();
CDTamingBuildPuzzleTable::Instance().LoadValuesFromDatabase();
CDVendorComponentTable::Instance().LoadValuesFromDatabase();
CDZoneTableTable::Instance().LoadValuesFromDatabase();
CDZoneTableTable::LoadValuesFromDatabase();
}
void CDClientManager::LoadValuesFromDefaults() {

View File

@@ -1,67 +1,53 @@
#include "CDZoneTableTable.h"
void CDZoneTableTable::LoadValuesFromDatabase() {
namespace CDZoneTableTable {
Table entries;
// First, get the size of the table
uint32_t size = 0;
auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM ZoneTable");
while (!tableSize.eof()) {
size = tableSize.getIntField(0, 0);
void LoadValuesFromDatabase() {
// Get the data from the database
auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM ZoneTable");
while (!tableData.eof()) {
CDZoneTable entry;
entry.zoneID = tableData.getIntField("zoneID", -1);
entry.locStatus = tableData.getIntField("locStatus", -1);
entry.zoneName = tableData.getStringField("zoneName", "");
entry.scriptID = tableData.getIntField("scriptID", -1);
entry.ghostdistance_min = tableData.getFloatField("ghostdistance_min", -1.0f);
entry.ghostdistance = tableData.getFloatField("ghostdistance", -1.0f);
entry.population_soft_cap = tableData.getIntField("population_soft_cap", -1);
entry.population_hard_cap = tableData.getIntField("population_hard_cap", -1);
UNUSED(entry.DisplayDescription = tableData.getStringField("DisplayDescription", ""));
UNUSED(entry.mapFolder = tableData.getStringField("mapFolder", ""));
entry.smashableMinDistance = tableData.getFloatField("smashableMinDistance", -1.0f);
entry.smashableMaxDistance = tableData.getFloatField("smashableMaxDistance", -1.0f);
UNUSED(entry.mixerProgram = tableData.getStringField("mixerProgram", ""));
UNUSED(entry.clientPhysicsFramerate = tableData.getStringField("clientPhysicsFramerate", ""));
entry.serverPhysicsFramerate = tableData.getStringField("serverPhysicsFramerate", "");
entry.zoneControlTemplate = tableData.getIntField("zoneControlTemplate", -1);
entry.widthInChunks = tableData.getIntField("widthInChunks", -1);
entry.heightInChunks = tableData.getIntField("heightInChunks", -1);
entry.petsAllowed = tableData.getIntField("petsAllowed", -1) == 1 ? true : false;
entry.localize = tableData.getIntField("localize", -1) == 1 ? true : false;
entry.fZoneWeight = tableData.getFloatField("fZoneWeight", -1.0f);
UNUSED(entry.thumbnail = tableData.getStringField("thumbnail", ""));
entry.PlayerLoseCoinsOnDeath = tableData.getIntField("PlayerLoseCoinsOnDeath", -1) == 1 ? true : false;
entry.disableSaveLoc = tableData.getIntField("disableSaveLoc", -1) == 1 ? true : false;
entry.teamRadius = tableData.getFloatField("teamRadius", -1.0f);
UNUSED(entry.gate_version = tableData.getStringField("gate_version", ""));
entry.mountsAllowed = tableData.getIntField("mountsAllowed", -1) == 1 ? true : false;
tableSize.nextRow();
entries[entry.zoneID] = entry;
tableData.nextRow();
}
}
tableSize.finalize();
//! Queries the table with a zoneID to find.
const CDZoneTable* Query(uint32_t zoneID) {
const auto& iter = entries.find(zoneID);
if (iter != entries.end()) {
return &iter->second;
}
// Now get the data
auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM ZoneTable");
auto& entries = GetEntriesMutable();
while (!tableData.eof()) {
CDZoneTable entry;
entry.zoneID = tableData.getIntField("zoneID", -1);
entry.locStatus = tableData.getIntField("locStatus", -1);
entry.zoneName = tableData.getStringField("zoneName", "");
entry.scriptID = tableData.getIntField("scriptID", -1);
entry.ghostdistance_min = tableData.getFloatField("ghostdistance_min", -1.0f);
entry.ghostdistance = tableData.getFloatField("ghostdistance", -1.0f);
entry.population_soft_cap = tableData.getIntField("population_soft_cap", -1);
entry.population_hard_cap = tableData.getIntField("population_hard_cap", -1);
UNUSED(entry.DisplayDescription = tableData.getStringField("DisplayDescription", ""));
UNUSED(entry.mapFolder = tableData.getStringField("mapFolder", ""));
entry.smashableMinDistance = tableData.getFloatField("smashableMinDistance", -1.0f);
entry.smashableMaxDistance = tableData.getFloatField("smashableMaxDistance", -1.0f);
UNUSED(entry.mixerProgram = tableData.getStringField("mixerProgram", ""));
UNUSED(entry.clientPhysicsFramerate = tableData.getStringField("clientPhysicsFramerate", ""));
entry.serverPhysicsFramerate = tableData.getStringField("serverPhysicsFramerate", "");
entry.zoneControlTemplate = tableData.getIntField("zoneControlTemplate", -1);
entry.widthInChunks = tableData.getIntField("widthInChunks", -1);
entry.heightInChunks = tableData.getIntField("heightInChunks", -1);
entry.petsAllowed = tableData.getIntField("petsAllowed", -1) == 1 ? true : false;
entry.localize = tableData.getIntField("localize", -1) == 1 ? true : false;
entry.fZoneWeight = tableData.getFloatField("fZoneWeight", -1.0f);
UNUSED(entry.thumbnail = tableData.getStringField("thumbnail", ""));
entry.PlayerLoseCoinsOnDeath = tableData.getIntField("PlayerLoseCoinsOnDeath", -1) == 1 ? true : false;
entry.disableSaveLoc = tableData.getIntField("disableSaveLoc", -1) == 1 ? true : false;
entry.teamRadius = tableData.getFloatField("teamRadius", -1.0f);
UNUSED(entry.gate_version = tableData.getStringField("gate_version", ""));
entry.mountsAllowed = tableData.getIntField("mountsAllowed", -1) == 1 ? true : false;
entries.insert(std::make_pair(entry.zoneID, entry));
tableData.nextRow();
return nullptr;
}
tableData.finalize();
}
//! Queries the table with a zoneID to find.
const CDZoneTable* CDZoneTableTable::Query(uint32_t zoneID) {
auto& m_Entries = GetEntries();
const auto& iter = m_Entries.find(zoneID);
if (iter != m_Entries.end()) {
return &iter->second;
}
return nullptr;
}

View File

@@ -33,8 +33,8 @@ struct CDZoneTable {
bool mountsAllowed; //!< Whether or not mounts are allowed
};
class CDZoneTableTable : public CDTable<CDZoneTableTable, std::map<uint32_t, CDZoneTable>> {
public:
namespace CDZoneTableTable {
using Table = std::map<uint32_t, CDZoneTable>;
void LoadValuesFromDatabase();
// Queries the table with a zoneID to find.

View File

@@ -1,7 +1,7 @@
add_subdirectory(CDClientDatabase)
add_subdirectory(GameDatabase)
add_library(dDatabase STATIC "MigrationRunner.cpp")
add_library(dDatabase STATIC "MigrationRunner.cpp" "ModelNormalizeMigration.cpp")
add_custom_target(conncpp_dylib
${CMAKE_COMMAND} -E copy $<TARGET_FILE:MariaDB::ConnCpp> ${PROJECT_BINARY_DIR})

View File

@@ -8,6 +8,7 @@
enum class eActivityType : uint32_t {
PlayerLoggedIn,
PlayerLoggedOut,
PlayerChangedZone
};
class IActivityLog {

View File

@@ -53,6 +53,9 @@ public:
// Update the property details for the given property id.
virtual void UpdatePropertyDetails(const IProperty::Info& info) = 0;
// Update the last updated time for the given property id.
virtual void UpdateLastSave(const IProperty::Info& info) = 0;
// Update the property performance cost for the given property id.
virtual void UpdatePerformanceCost(const LWOZONEID& zoneId, const float performanceCost) = 0;

View File

@@ -22,7 +22,7 @@ public:
// Inserts a new UGC model into the database.
virtual void InsertNewUgcModel(
std::istringstream& sd0Data,
std::stringstream& sd0Data,
const uint32_t blueprintId,
const uint32_t accountId,
const uint32_t characterId) = 0;
@@ -34,9 +34,17 @@ public:
virtual void InsertNewPropertyModel(const LWOOBJID& propertyId, const IPropertyContents::Model& model, const std::string_view name) = 0;
// Update the model position and rotation for the given property id.
virtual void UpdateModel(const LWOOBJID& propertyId, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) = 0;
virtual void UpdateModel(const LWOOBJID& modelID, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) = 0;
virtual void UpdateModel(const LWOOBJID& modelID, const NiPoint3& position, const NiQuaternion& rotation, const std::array<int32_t, 5> behaviorIDs) {
std::array<std::pair<int32_t, std::string>, 5> behaviors;
for (int32_t i = 0; i < behaviors.size(); i++) behaviors[i].first = behaviorIDs[i];
UpdateModel(modelID, position, rotation, behaviors);
}
// Remove the model for the given property id.
virtual void RemoveModel(const LWOOBJID& modelId) = 0;
// Gets a model by ID
virtual Model GetModel(const LWOOBJID modelID) = 0;
};
#endif //!__IPROPERTIESCONTENTS__H__

View File

@@ -12,6 +12,7 @@ public:
struct Model {
std::stringstream lxfmlData;
LWOOBJID id{};
LWOOBJID modelID{};
};
// Gets all UGC models for the given property id.
@@ -27,6 +28,6 @@ public:
virtual void DeleteUgcModelData(const LWOOBJID& modelId) = 0;
// Inserts a new UGC model into the database.
virtual void UpdateUgcModelData(const LWOOBJID& modelId, std::istringstream& lxfml) = 0;
virtual void UpdateUgcModelData(const LWOOBJID& modelId, std::stringstream& lxfml) = 0;
};
#endif //!__IUGC__H__

View File

@@ -48,7 +48,7 @@ public:
void RemoveFriend(const uint32_t playerAccountId, const uint32_t friendAccountId) override;
void UpdateActivityLog(const uint32_t characterId, const eActivityType activityType, const LWOMAPID mapId) override;
void DeleteUgcModelData(const LWOOBJID& modelId) override;
void UpdateUgcModelData(const LWOOBJID& modelId, std::istringstream& lxfml) override;
void UpdateUgcModelData(const LWOOBJID& modelId, std::stringstream& lxfml) override;
std::vector<IUgc::Model> GetAllUgcModels() override;
void CreateMigrationHistoryTable() override;
bool IsMigrationRun(const std::string_view str) override;
@@ -70,18 +70,19 @@ public:
std::optional<IProperty::Info> GetPropertyInfo(const LWOMAPID mapId, const LWOCLONEID cloneId) override;
void UpdatePropertyModerationInfo(const IProperty::Info& info) override;
void UpdatePropertyDetails(const IProperty::Info& info) override;
void UpdateLastSave(const IProperty::Info& info) override;
void InsertNewProperty(const IProperty::Info& info, const uint32_t templateId, const LWOZONEID& zoneId) override;
std::vector<IPropertyContents::Model> GetPropertyModels(const LWOOBJID& propertyId) override;
void RemoveUnreferencedUgcModels() override;
void InsertNewPropertyModel(const LWOOBJID& propertyId, const IPropertyContents::Model& model, const std::string_view name) override;
void UpdateModel(const LWOOBJID& propertyId, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) override;
void UpdateModel(const LWOOBJID& modelID, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) override;
void RemoveModel(const LWOOBJID& modelId) override;
void UpdatePerformanceCost(const LWOZONEID& zoneId, const float performanceCost) override;
void InsertNewBugReport(const IBugReports::Info& info) override;
void InsertCheatDetection(const IPlayerCheatDetections::Info& info) override;
void InsertNewMail(const MailInfo& mail) override;
void InsertNewUgcModel(
std::istringstream& sd0Data,
std::stringstream& sd0Data,
const uint32_t blueprintId,
const uint32_t accountId,
const uint32_t characterId) override;
@@ -126,6 +127,7 @@ public:
void DeleteUgcBuild(const LWOOBJID bigId) override;
uint32_t GetAccountCount() override;
bool IsNameInUse(const std::string_view name) override;
IPropertyContents::Model GetModel(const LWOOBJID modelID) override;
sql::PreparedStatement* CreatePreppedStmt(const std::string& query);
private:

View File

@@ -173,6 +173,10 @@ void MySQLDatabase::UpdatePropertyDetails(const IProperty::Info& info) {
ExecuteUpdate("UPDATE properties SET name = ?, description = ? WHERE id = ? LIMIT 1;", info.name, info.description, info.id);
}
void MySQLDatabase::UpdateLastSave(const IProperty::Info& info) {
ExecuteUpdate("UPDATE properties SET last_updated = ? WHERE id = ?;", info.lastUpdatedTime, info.id);
}
void MySQLDatabase::UpdatePerformanceCost(const LWOZONEID& zoneId, const float performanceCost) {
ExecuteUpdate("UPDATE properties SET performance_cost = ? WHERE zone_id = ? AND clone_id = ? LIMIT 1;", performanceCost, zoneId.GetMapID(), zoneId.GetCloneID());
}

View File

@@ -52,14 +52,39 @@ void MySQLDatabase::InsertNewPropertyModel(const LWOOBJID& propertyId, const IPr
}
}
void MySQLDatabase::UpdateModel(const LWOOBJID& propertyId, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) {
void MySQLDatabase::UpdateModel(const LWOOBJID& modelID, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) {
ExecuteUpdate(
"UPDATE properties_contents SET x = ?, y = ?, z = ?, rx = ?, ry = ?, rz = ?, rw = ?, "
"behavior_1 = ?, behavior_2 = ?, behavior_3 = ?, behavior_4 = ?, behavior_5 = ? WHERE id = ?;",
position.x, position.y, position.z, rotation.x, rotation.y, rotation.z, rotation.w,
behaviors[0].first, behaviors[1].first, behaviors[2].first, behaviors[3].first, behaviors[4].first, propertyId);
behaviors[0].first, behaviors[1].first, behaviors[2].first, behaviors[3].first, behaviors[4].first, modelID);
}
void MySQLDatabase::RemoveModel(const LWOOBJID& modelId) {
ExecuteDelete("DELETE FROM properties_contents WHERE id = ?;", modelId);
}
IPropertyContents::Model MySQLDatabase::GetModel(const LWOOBJID modelID) {
auto result = ExecuteSelect("SELECT * FROM properties_contents WHERE id = ?", modelID);
IPropertyContents::Model model{};
while (result->next()) {
model.id = result->getUInt64("id");
model.lot = static_cast<LOT>(result->getUInt("lot"));
model.position.x = result->getFloat("x");
model.position.y = result->getFloat("y");
model.position.z = result->getFloat("z");
model.rotation.w = result->getFloat("rw");
model.rotation.x = result->getFloat("rx");
model.rotation.y = result->getFloat("ry");
model.rotation.z = result->getFloat("rz");
model.ugcId = result->getUInt64("ugc_id");
model.behaviors[0] = result->getInt("behavior_1");
model.behaviors[1] = result->getInt("behavior_2");
model.behaviors[2] = result->getInt("behavior_3");
model.behaviors[3] = result->getInt("behavior_4");
model.behaviors[4] = result->getInt("behavior_5");
}
return model;
}

View File

@@ -2,7 +2,7 @@
std::vector<IUgc::Model> MySQLDatabase::GetUgcModels(const LWOOBJID& propertyId) {
auto result = ExecuteSelect(
"SELECT lxfml, u.id FROM ugc AS u JOIN properties_contents AS pc ON u.id = pc.ugc_id WHERE lot = 14 AND property_id = ? AND pc.ugc_id IS NOT NULL;",
"SELECT lxfml, u.id as ugcID, pc.id as modelID FROM ugc AS u JOIN properties_contents AS pc ON u.id = pc.ugc_id WHERE lot = 14 AND property_id = ? AND pc.ugc_id IS NOT NULL;",
propertyId);
std::vector<IUgc::Model> toReturn;
@@ -13,7 +13,8 @@ std::vector<IUgc::Model> MySQLDatabase::GetUgcModels(const LWOOBJID& propertyId)
// blob is owned by the query, so we need to do a deep copy :/
std::unique_ptr<std::istream> blob(result->getBlob("lxfml"));
model.lxfmlData << blob->rdbuf();
model.id = result->getUInt64("id");
model.id = result->getUInt64("ugcID");
model.modelID = result->getUInt64("modelID");
toReturn.push_back(std::move(model));
}
@@ -21,13 +22,14 @@ std::vector<IUgc::Model> MySQLDatabase::GetUgcModels(const LWOOBJID& propertyId)
}
std::vector<IUgc::Model> MySQLDatabase::GetAllUgcModels() {
auto result = ExecuteSelect("SELECT id, lxfml FROM ugc;");
auto result = ExecuteSelect("SELECT u.id AS ugcID, lxfml, pc.id AS modelID FROM ugc AS u JOIN properties_contents AS pc ON pc.ugc_id = u.id WHERE pc.lot = 14 AND pc.ugc_id IS NOT NULL;");
std::vector<IUgc::Model> models;
models.reserve(result->rowsCount());
while (result->next()) {
IUgc::Model model;
model.id = result->getInt64("id");
model.id = result->getInt64("ugcID");
model.modelID = result->getUInt64("modelID");
// blob is owned by the query, so we need to do a deep copy :/
std::unique_ptr<std::istream> blob(result->getBlob("lxfml"));
@@ -43,7 +45,7 @@ void MySQLDatabase::RemoveUnreferencedUgcModels() {
}
void MySQLDatabase::InsertNewUgcModel(
std::istringstream& sd0Data, // cant be const sad
std:: stringstream& sd0Data, // cant be const sad
const uint32_t blueprintId,
const uint32_t accountId,
const uint32_t characterId) {
@@ -65,7 +67,7 @@ void MySQLDatabase::DeleteUgcModelData(const LWOOBJID& modelId) {
ExecuteDelete("DELETE FROM properties_contents WHERE ugc_id = ?;", modelId);
}
void MySQLDatabase::UpdateUgcModelData(const LWOOBJID& modelId, std::istringstream& lxfml) {
void MySQLDatabase::UpdateUgcModelData(const LWOOBJID& modelId, std::stringstream& lxfml) {
const std::istream stream(lxfml.rdbuf());
ExecuteUpdate("UPDATE ugc SET lxfml = ? WHERE id = ?;", &stream, modelId);
}

View File

@@ -46,7 +46,7 @@ public:
void RemoveFriend(const uint32_t playerAccountId, const uint32_t friendAccountId) override;
void UpdateActivityLog(const uint32_t characterId, const eActivityType activityType, const LWOMAPID mapId) override;
void DeleteUgcModelData(const LWOOBJID& modelId) override;
void UpdateUgcModelData(const LWOOBJID& modelId, std::istringstream& lxfml) override;
void UpdateUgcModelData(const LWOOBJID& modelId, std::stringstream& lxfml) override;
std::vector<IUgc::Model> GetAllUgcModels() override;
void CreateMigrationHistoryTable() override;
bool IsMigrationRun(const std::string_view str) override;
@@ -68,18 +68,19 @@ public:
std::optional<IProperty::Info> GetPropertyInfo(const LWOMAPID mapId, const LWOCLONEID cloneId) override;
void UpdatePropertyModerationInfo(const IProperty::Info& info) override;
void UpdatePropertyDetails(const IProperty::Info& info) override;
void UpdateLastSave(const IProperty::Info& info) override;
void InsertNewProperty(const IProperty::Info& info, const uint32_t templateId, const LWOZONEID& zoneId) override;
std::vector<IPropertyContents::Model> GetPropertyModels(const LWOOBJID& propertyId) override;
void RemoveUnreferencedUgcModels() override;
void InsertNewPropertyModel(const LWOOBJID& propertyId, const IPropertyContents::Model& model, const std::string_view name) override;
void UpdateModel(const LWOOBJID& propertyId, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) override;
void UpdateModel(const LWOOBJID& modelID, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) override;
void RemoveModel(const LWOOBJID& modelId) override;
void UpdatePerformanceCost(const LWOZONEID& zoneId, const float performanceCost) override;
void InsertNewBugReport(const IBugReports::Info& info) override;
void InsertCheatDetection(const IPlayerCheatDetections::Info& info) override;
void InsertNewMail(const MailInfo& mail) override;
void InsertNewUgcModel(
std::istringstream& sd0Data,
std::stringstream& sd0Data,
const uint32_t blueprintId,
const uint32_t accountId,
const uint32_t characterId) override;
@@ -124,6 +125,7 @@ public:
void DeleteUgcBuild(const LWOOBJID bigId) override;
uint32_t GetAccountCount() override;
bool IsNameInUse(const std::string_view name) override;
IPropertyContents::Model GetModel(const LWOOBJID modelID) override;
private:
CppSQLite3Statement CreatePreppedStmt(const std::string& query);

View File

@@ -175,6 +175,10 @@ void SQLiteDatabase::UpdatePropertyDetails(const IProperty::Info& info) {
ExecuteUpdate("UPDATE properties SET name = ?, description = ? WHERE id = ?;", info.name, info.description, info.id);
}
void SQLiteDatabase::UpdateLastSave(const IProperty::Info& info) {
ExecuteUpdate("UPDATE properties SET last_updated = ? WHERE id = ?;", info.lastUpdatedTime, info.id);
}
void SQLiteDatabase::UpdatePerformanceCost(const LWOZONEID& zoneId, const float performanceCost) {
ExecuteUpdate("UPDATE properties SET performance_cost = ? WHERE zone_id = ? AND clone_id = ?;", performanceCost, zoneId.GetMapID(), zoneId.GetCloneID());
}

View File

@@ -52,14 +52,41 @@ void SQLiteDatabase::InsertNewPropertyModel(const LWOOBJID& propertyId, const IP
}
}
void SQLiteDatabase::UpdateModel(const LWOOBJID& propertyId, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) {
void SQLiteDatabase::UpdateModel(const LWOOBJID& modelID, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) {
ExecuteUpdate(
"UPDATE properties_contents SET x = ?, y = ?, z = ?, rx = ?, ry = ?, rz = ?, rw = ?, "
"behavior_1 = ?, behavior_2 = ?, behavior_3 = ?, behavior_4 = ?, behavior_5 = ? WHERE id = ?;",
position.x, position.y, position.z, rotation.x, rotation.y, rotation.z, rotation.w,
behaviors[0].first, behaviors[1].first, behaviors[2].first, behaviors[3].first, behaviors[4].first, propertyId);
behaviors[0].first, behaviors[1].first, behaviors[2].first, behaviors[3].first, behaviors[4].first, modelID);
}
void SQLiteDatabase::RemoveModel(const LWOOBJID& modelId) {
ExecuteDelete("DELETE FROM properties_contents WHERE id = ?;", modelId);
}
IPropertyContents::Model SQLiteDatabase::GetModel(const LWOOBJID modelID) {
auto [_, result] = ExecuteSelect("SELECT * FROM properties_contents WHERE id = ?", modelID);
IPropertyContents::Model model{};
if (!result.eof()) {
do {
model.id = result.getInt64Field("id");
model.lot = static_cast<LOT>(result.getIntField("lot"));
model.position.x = result.getFloatField("x");
model.position.y = result.getFloatField("y");
model.position.z = result.getFloatField("z");
model.rotation.w = result.getFloatField("rw");
model.rotation.x = result.getFloatField("rx");
model.rotation.y = result.getFloatField("ry");
model.rotation.z = result.getFloatField("rz");
model.ugcId = result.getInt64Field("ugc_id");
model.behaviors[0] = result.getIntField("behavior_1");
model.behaviors[1] = result.getIntField("behavior_2");
model.behaviors[2] = result.getIntField("behavior_3");
model.behaviors[3] = result.getIntField("behavior_4");
model.behaviors[4] = result.getIntField("behavior_5");
} while (result.nextRow());
}
return model;
}

View File

@@ -2,7 +2,7 @@
std::vector<IUgc::Model> SQLiteDatabase::GetUgcModels(const LWOOBJID& propertyId) {
auto [_, result] = ExecuteSelect(
"SELECT lxfml, u.id FROM ugc AS u JOIN properties_contents AS pc ON u.id = pc.ugc_id WHERE lot = 14 AND property_id = ? AND pc.ugc_id IS NOT NULL;",
"SELECT lxfml, u.id AS ugcID, pc.id AS modelID FROM ugc AS u JOIN properties_contents AS pc ON u.id = pc.ugc_id WHERE lot = 14 AND property_id = ? AND pc.ugc_id IS NOT NULL;",
propertyId);
std::vector<IUgc::Model> toReturn;
@@ -13,7 +13,8 @@ std::vector<IUgc::Model> SQLiteDatabase::GetUgcModels(const LWOOBJID& propertyId
int blobSize{};
const auto* blob = result.getBlobField("lxfml", blobSize);
model.lxfmlData << std::string(reinterpret_cast<const char*>(blob), blobSize);
model.id = result.getInt64Field("id");
model.id = result.getInt64Field("ugcID");
model.modelID = result.getInt64Field("modelID");
toReturn.push_back(std::move(model));
result.nextRow();
}
@@ -22,12 +23,13 @@ std::vector<IUgc::Model> SQLiteDatabase::GetUgcModels(const LWOOBJID& propertyId
}
std::vector<IUgc::Model> SQLiteDatabase::GetAllUgcModels() {
auto [_, result] = ExecuteSelect("SELECT id, lxfml FROM ugc;");
auto [_, result] = ExecuteSelect("SELECT u.id AS ugcID, pc.id AS modelID, lxfml FROM ugc AS u JOIN properties_contents AS pc ON pc.id = u.id;");
std::vector<IUgc::Model> models;
while (!result.eof()) {
IUgc::Model model;
model.id = result.getInt64Field("id");
model.id = result.getInt64Field("ugcID");
model.modelID = result.getInt64Field("modelID");
int blobSize{};
const auto* blob = result.getBlobField("lxfml", blobSize);
@@ -44,7 +46,7 @@ void SQLiteDatabase::RemoveUnreferencedUgcModels() {
}
void SQLiteDatabase::InsertNewUgcModel(
std::istringstream& sd0Data, // cant be const sad
std::stringstream& sd0Data, // cant be const sad
const uint32_t blueprintId,
const uint32_t accountId,
const uint32_t characterId) {
@@ -66,7 +68,7 @@ void SQLiteDatabase::DeleteUgcModelData(const LWOOBJID& modelId) {
ExecuteDelete("DELETE FROM properties_contents WHERE ugc_id = ?;", modelId);
}
void SQLiteDatabase::UpdateUgcModelData(const LWOOBJID& modelId, std::istringstream& lxfml) {
void SQLiteDatabase::UpdateUgcModelData(const LWOOBJID& modelId, std::stringstream& lxfml) {
const std::istream stream(lxfml.rdbuf());
ExecuteUpdate("UPDATE ugc SET lxfml = ? WHERE id = ?;", &stream, modelId);
}

View File

@@ -60,7 +60,7 @@ void TestSQLDatabase::DeleteUgcModelData(const LWOOBJID& modelId) {
}
void TestSQLDatabase::UpdateUgcModelData(const LWOOBJID& modelId, std::istringstream& lxfml) {
void TestSQLDatabase::UpdateUgcModelData(const LWOOBJID& modelId, std::stringstream& lxfml) {
}
@@ -148,6 +148,10 @@ void TestSQLDatabase::UpdatePropertyDetails(const IProperty::Info& info) {
}
void TestSQLDatabase::UpdateLastSave(const IProperty::Info& info) {
}
void TestSQLDatabase::InsertNewProperty(const IProperty::Info& info, const uint32_t templateId, const LWOZONEID& zoneId) {
}
@@ -164,7 +168,7 @@ void TestSQLDatabase::InsertNewPropertyModel(const LWOOBJID& propertyId, const I
}
void TestSQLDatabase::UpdateModel(const LWOOBJID& propertyId, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) {
void TestSQLDatabase::UpdateModel(const LWOOBJID& modelID, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) {
}
@@ -188,7 +192,7 @@ void TestSQLDatabase::InsertNewMail(const MailInfo& mail) {
}
void TestSQLDatabase::InsertNewUgcModel(std::istringstream& sd0Data, const uint32_t blueprintId, const uint32_t accountId, const uint32_t characterId) {
void TestSQLDatabase::InsertNewUgcModel(std::stringstream& sd0Data, const uint32_t blueprintId, const uint32_t accountId, const uint32_t characterId) {
}

View File

@@ -25,7 +25,7 @@ class TestSQLDatabase : public GameDatabase {
void RemoveFriend(const uint32_t playerAccountId, const uint32_t friendAccountId) override;
void UpdateActivityLog(const uint32_t characterId, const eActivityType activityType, const LWOMAPID mapId) override;
void DeleteUgcModelData(const LWOOBJID& modelId) override;
void UpdateUgcModelData(const LWOOBJID& modelId, std::istringstream& lxfml) override;
void UpdateUgcModelData(const LWOOBJID& modelId, std::stringstream& lxfml) override;
std::vector<IUgc::Model> GetAllUgcModels() override;
void CreateMigrationHistoryTable() override;
bool IsMigrationRun(const std::string_view str) override;
@@ -47,18 +47,19 @@ class TestSQLDatabase : public GameDatabase {
std::optional<IProperty::Info> GetPropertyInfo(const LWOMAPID mapId, const LWOCLONEID cloneId) override;
void UpdatePropertyModerationInfo(const IProperty::Info& info) override;
void UpdatePropertyDetails(const IProperty::Info& info) override;
void UpdateLastSave(const IProperty::Info& info) override;
void InsertNewProperty(const IProperty::Info& info, const uint32_t templateId, const LWOZONEID& zoneId) override;
std::vector<IPropertyContents::Model> GetPropertyModels(const LWOOBJID& propertyId) override;
void RemoveUnreferencedUgcModels() override;
void InsertNewPropertyModel(const LWOOBJID& propertyId, const IPropertyContents::Model& model, const std::string_view name) override;
void UpdateModel(const LWOOBJID& propertyId, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) override;
void UpdateModel(const LWOOBJID& modelID, const NiPoint3& position, const NiQuaternion& rotation, const std::array<std::pair<int32_t, std::string>, 5>& behaviors) override;
void RemoveModel(const LWOOBJID& modelId) override;
void UpdatePerformanceCost(const LWOZONEID& zoneId, const float performanceCost) override;
void InsertNewBugReport(const IBugReports::Info& info) override;
void InsertCheatDetection(const IPlayerCheatDetections::Info& info) override;
void InsertNewMail(const MailInfo& mail) override;
void InsertNewUgcModel(
std::istringstream& sd0Data,
std::stringstream& sd0Data,
const uint32_t blueprintId,
const uint32_t accountId,
const uint32_t characterId) override;
@@ -104,6 +105,7 @@ class TestSQLDatabase : public GameDatabase {
uint32_t GetAccountCount() override { return 0; };
bool IsNameInUse(const std::string_view name) override { return false; };
IPropertyContents::Model GetModel(const LWOOBJID modelID) override { return {}; }
};
#endif //!TESTSQLDATABASE_H

View File

@@ -7,6 +7,7 @@
#include "GeneralUtils.h"
#include "Logger.h"
#include "BinaryPathFinder.h"
#include "ModelNormalizeMigration.h"
#include <fstream>
@@ -35,7 +36,7 @@ void MigrationRunner::RunMigrations() {
Database::Get()->CreateMigrationHistoryTable();
// has to be here because when moving the files to the new folder, the migration_history table is not updated so it will run them all again.
const auto migrationFolder = Database::GetMigrationFolder();
if (!Database::Get()->IsMigrationRun("17_migration_for_migrations.sql") && migrationFolder == "mysql") {
LOG("Running migration: 17_migration_for_migrations.sql");
@@ -45,6 +46,7 @@ void MigrationRunner::RunMigrations() {
std::string finalSQL = "";
bool runSd0Migrations = false;
bool runNormalizeMigrations = false;
for (const auto& entry : GeneralUtils::GetSqlFileNamesFromFolder((BinaryPathFinder::GetBinaryDir() / "./migrations/dlu/" / migrationFolder).string())) {
auto migration = LoadMigration("dlu/" + migrationFolder + "/", entry);
@@ -57,6 +59,8 @@ void MigrationRunner::RunMigrations() {
LOG("Running migration: %s", migration.name.c_str());
if (migration.name == "5_brick_model_sd0.sql") {
runSd0Migrations = true;
} else if (migration.name.ends_with("_normalize_model_positions.sql")) {
runNormalizeMigrations = true;
} else {
finalSQL.append(migration.data.c_str());
}
@@ -64,7 +68,7 @@ void MigrationRunner::RunMigrations() {
Database::Get()->InsertMigration(migration.name);
}
if (finalSQL.empty() && !runSd0Migrations) {
if (finalSQL.empty() && !runSd0Migrations && !runNormalizeMigrations) {
LOG("Server database is up to date.");
return;
}
@@ -88,6 +92,10 @@ void MigrationRunner::RunMigrations() {
uint32_t numberOfTruncatedModels = BrickByBrickFix::TruncateBrokenBrickByBrickXml();
LOG("%i models were truncated from the database.", numberOfTruncatedModels);
}
if (runNormalizeMigrations) {
ModelNormalizeMigration::Run();
}
}
void MigrationRunner::RunSQLiteMigrations() {

View File

@@ -0,0 +1,30 @@
#include "ModelNormalizeMigration.h"
#include "Database.h"
#include "Lxfml.h"
#include "Sd0.h"
void ModelNormalizeMigration::Run() {
const auto oldCommit = Database::Get()->GetAutoCommit();
Database::Get()->SetAutoCommit(false);
for (auto& [lxfmlData, id, modelID] : Database::Get()->GetAllUgcModels()) {
const auto model = Database::Get()->GetModel(modelID);
// only BBB models (lot 14) and models with a position of NiPoint3::ZERO need to have their position fixed.
if (model.position != NiPoint3Constant::ZERO || model.lot != 14) continue;
Sd0 sd0(lxfmlData);
const auto asStr = sd0.GetAsStringUncompressed();
const auto [newLxfml, newCenter] = Lxfml::NormalizePosition(asStr);
if (newCenter == NiPoint3Constant::ZERO) {
LOG("Failed to update model %llu due to failure reading xml.");
continue;
}
LOG("Updated model %llu to have a center of %f %f %f", modelID, newCenter.x, newCenter.y, newCenter.z);
sd0.FromData(reinterpret_cast<const uint8_t*>(newLxfml.data()), newLxfml.size());
auto asStream = sd0.GetAsStream();
Database::Get()->UpdateModel(model.id, newCenter, model.rotation, model.behaviors);
Database::Get()->UpdateUgcModelData(id, asStream);
}
Database::Get()->SetAutoCommit(oldCommit);
}

View File

@@ -0,0 +1,11 @@
// Darkflame Universe
// Copyright 2025
#ifndef MODELNORMALIZEMIGRATION_H
#define MODELNORMALIZEMIGRATION_H
namespace ModelNormalizeMigration {
void Run();
};
#endif //!MODELNORMALIZEMIGRATION_H

View File

@@ -545,9 +545,8 @@ void Entity::Initialize() {
// ZoneControl script
if (m_TemplateID == 2365) {
CDZoneTableTable* zoneTable = CDClientManager::GetTable<CDZoneTableTable>();
const auto zoneID = Game::zoneManager->GetZoneID();
const CDZoneTable* zoneData = zoneTable->Query(zoneID.GetMapID());
const CDZoneTable* zoneData = CDZoneTableTable::Query(zoneID.GetMapID());
if (zoneData != nullptr) {
int zoneScriptID = zoneData->scriptID;

View File

@@ -99,7 +99,7 @@ Entity* EntityManager::CreateEntity(EntityInfo info, User* user, Entity* parentE
}
// Exclude the zone control object from any flags
if (!controller && info.lot != 14) {
if (!controller) {
// The client flags means the client should render the entity
GeneralUtils::SetBit(id, eObjectBits::CLIENT);
@@ -320,7 +320,7 @@ const std::unordered_map<std::string, LWOOBJID>& EntityManager::GetSpawnPointEnt
return m_SpawnPoints;
}
void EntityManager::ConstructEntity(Entity* entity, const SystemAddress& sysAddr, const bool skipChecks) {
void EntityManager::ConstructEntity(Entity* entity, const SystemAddress& sysAddr) {
if (!entity) {
LOG("Attempted to construct null entity");
return;
@@ -363,16 +363,12 @@ void EntityManager::ConstructEntity(Entity* entity, const SystemAddress& sysAddr
entity->WriteComponents(stream, eReplicaPacketType::CONSTRUCTION);
if (sysAddr == UNASSIGNED_SYSTEM_ADDRESS) {
if (skipChecks) {
Game::server->Send(stream, UNASSIGNED_SYSTEM_ADDRESS, true);
} else {
for (auto* player : PlayerManager::GetAllPlayers()) {
if (player->GetPlayerReadyForUpdates()) {
Game::server->Send(stream, player->GetSystemAddress(), false);
} else {
auto* ghostComponent = player->GetComponent<GhostComponent>();
if (ghostComponent) ghostComponent->AddLimboConstruction(entity->GetObjectID());
}
for (auto* player : PlayerManager::GetAllPlayers()) {
if (player->GetPlayerReadyForUpdates()) {
Game::server->Send(stream, player->GetSystemAddress(), false);
} else {
auto* ghostComponent = player->GetComponent<GhostComponent>();
if (ghostComponent) ghostComponent->AddLimboConstruction(entity->GetObjectID());
}
}
} else {

View File

@@ -42,7 +42,7 @@ public:
const std::unordered_map<LWOOBJID, Entity*> GetAllEntities() const { return m_Entities; }
#endif
void ConstructEntity(Entity* entity, const SystemAddress& sysAddr = UNASSIGNED_SYSTEM_ADDRESS, bool skipChecks = false);
void ConstructEntity(Entity* entity, const SystemAddress& sysAddr = UNASSIGNED_SYSTEM_ADDRESS);
void DestructEntity(Entity* entity, const SystemAddress& sysAddr = UNASSIGNED_SYSTEM_ADDRESS);
void SerializeEntity(Entity* entity);
void SerializeEntity(const Entity& entity);

View File

@@ -24,12 +24,13 @@ namespace LeaderboardManager {
std::map<GameID, Leaderboard::Type> leaderboardCache;
}
Leaderboard::Leaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, LWOOBJID relatedPlayer, const Leaderboard::Type leaderboardType) {
Leaderboard::Leaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, LWOOBJID relatedPlayer, const uint32_t numResults, const Leaderboard::Type leaderboardType) {
this->gameID = gameID;
this->weekly = weekly;
this->infoType = infoType;
this->leaderboardType = leaderboardType;
this->relatedPlayer = relatedPlayer;
this->numResults = numResults;
}
Leaderboard::~Leaderboard() {
@@ -144,7 +145,7 @@ void QueryToLdf(Leaderboard& leaderboard, const std::vector<ILeaderboard::Entry>
}
}
std::vector<ILeaderboard::Entry> FilterTo10(const std::vector<ILeaderboard::Entry>& leaderboard, const uint32_t relatedPlayer, const Leaderboard::InfoType infoType) {
std::vector<ILeaderboard::Entry> FilterToNumResults(const std::vector<ILeaderboard::Entry>& leaderboard, const uint32_t relatedPlayer, const Leaderboard::InfoType infoType, const uint32_t numResults) {
std::vector<ILeaderboard::Entry> toReturn;
int32_t index = 0;
@@ -155,18 +156,19 @@ std::vector<ILeaderboard::Entry> FilterTo10(const std::vector<ILeaderboard::Entr
}
}
if (leaderboard.size() < 10) {
if (leaderboard.size() < numResults) {
toReturn.assign(leaderboard.begin(), leaderboard.end());
index = 0;
} else if (index < 10) {
toReturn.assign(leaderboard.begin(), leaderboard.begin() + 10); // get the top 10 since we are in the top 10
} else if (index < numResults) {
toReturn.assign(leaderboard.begin(), leaderboard.begin() + numResults); // get the top 10 since we are in the top 10
index = 0;
} else if (index > leaderboard.size() - 10) {
toReturn.assign(leaderboard.end() - 10, leaderboard.end()); // get the bottom 10 since we are in the bottom 10
index = leaderboard.size() - 10;
} else if (index > leaderboard.size() - numResults) {
toReturn.assign(leaderboard.end() - numResults, leaderboard.end()); // get the bottom 10 since we are in the bottom 10
index = leaderboard.size() - numResults;
} else {
toReturn.assign(leaderboard.begin() + index - 5, leaderboard.begin() + index + 5); // get the 5 above and below
index -= 5;
auto half = numResults / 2;
toReturn.assign(leaderboard.begin() + index - half, leaderboard.begin() + index + half); // get the 5 above and below
index -= half;
}
int32_t i = index;
@@ -178,14 +180,16 @@ std::vector<ILeaderboard::Entry> FilterTo10(const std::vector<ILeaderboard::Entr
}
std::vector<ILeaderboard::Entry> FilterWeeklies(const std::vector<ILeaderboard::Entry>& leaderboard) {
using namespace std::chrono;
// Filter the leaderboard to only include entries from the last week
const auto currentTime = std::chrono::system_clock::now();
auto epochTime = currentTime.time_since_epoch().count();
constexpr auto SECONDS_IN_A_WEEK = 60 * 60 * 24 * 7; // if you think im taking leap seconds into account thats cute.
const auto epochTime = system_clock::now();
constexpr auto oneWeek = weeks(1);
std::vector<ILeaderboard::Entry> weeklyLeaderboard;
for (const auto& entry : leaderboard) {
if (epochTime - entry.lastPlayedTimestamp < SECONDS_IN_A_WEEK) {
const sys_time<seconds> asSysTime(seconds(entry.lastPlayedTimestamp));
const auto timeDiff = epochTime - asSysTime;
if (timeDiff < oneWeek) {
weeklyLeaderboard.push_back(entry);
}
}
@@ -213,14 +217,15 @@ std::vector<ILeaderboard::Entry> ProcessLeaderboard(
const std::vector<ILeaderboard::Entry>& leaderboard,
const bool weekly,
const Leaderboard::InfoType infoType,
const uint32_t relatedPlayer) {
const uint32_t relatedPlayer,
const uint32_t numResults) {
std::vector<ILeaderboard::Entry> toReturn;
if (infoType == Leaderboard::InfoType::Friends) {
const auto friendsLeaderboard = FilterFriends(leaderboard, relatedPlayer);
toReturn = FilterTo10(weekly ? FilterWeeklies(friendsLeaderboard) : friendsLeaderboard, relatedPlayer, infoType);
toReturn = FilterToNumResults(weekly ? FilterWeeklies(friendsLeaderboard) : friendsLeaderboard, relatedPlayer, infoType, numResults);
} else {
toReturn = FilterTo10(weekly ? FilterWeeklies(leaderboard) : leaderboard, relatedPlayer, infoType);
toReturn = FilterToNumResults(weekly ? FilterWeeklies(leaderboard) : leaderboard, relatedPlayer, infoType, numResults);
}
return toReturn;
@@ -255,7 +260,7 @@ void Leaderboard::SetupLeaderboard(bool weekly) {
break;
}
const auto processedLeaderboard = ProcessLeaderboard(leaderboardRes, weekly, infoType, relatedPlayer);
const auto processedLeaderboard = ProcessLeaderboard(leaderboardRes, weekly, infoType, relatedPlayer, numResults);
QueryToLdf(*this, processedLeaderboard);
}
@@ -301,8 +306,8 @@ void LeaderboardManager::SaveScore(const LWOOBJID& playerID, const GameID activi
}
}
void LeaderboardManager::SendLeaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, const LWOOBJID playerID, const LWOOBJID targetID) {
Leaderboard leaderboard(gameID, infoType, weekly, playerID, GetLeaderboardType(gameID));
void LeaderboardManager::SendLeaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, const LWOOBJID playerID, const LWOOBJID targetID, const uint32_t numResults) {
Leaderboard leaderboard(gameID, infoType, weekly, playerID, numResults, GetLeaderboardType(gameID));
leaderboard.SetupLeaderboard(weekly);
leaderboard.Send(targetID);
}

View File

@@ -37,7 +37,7 @@ public:
None
};
Leaderboard() = delete;
Leaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, LWOOBJID relatedPlayer, const Leaderboard::Type = None);
Leaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, LWOOBJID relatedPlayer, const uint32_t numResults, const Leaderboard::Type = None);
~Leaderboard();
@@ -79,6 +79,7 @@ private:
InfoType infoType;
Leaderboard::Type leaderboardType;
bool weekly;
uint32_t numResults;
public:
LeaderboardEntry& PushBackEntry() {
return entries.emplace_back();
@@ -90,7 +91,7 @@ public:
};
namespace LeaderboardManager {
void SendLeaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, const LWOOBJID playerID, const LWOOBJID targetID);
void SendLeaderboard(const GameID gameID, const Leaderboard::InfoType infoType, const bool weekly, const LWOOBJID playerID, const LWOOBJID targetID, const uint32_t numResults);
void SaveScore(const LWOOBJID& playerID, const GameID activityId, const float primaryScore, const float secondaryScore = 0, const float tertiaryScore = 0);

View File

@@ -29,6 +29,7 @@
#include "MessageType/Chat.h"
#include "BitStreamUtils.h"
#include "CheatDetection.h"
#include "CharacterComponent.h"
UserManager* UserManager::m_Address = nullptr;
@@ -340,7 +341,10 @@ void UserManager::CreateCharacter(const SystemAddress& sysAddr, Packet* packet)
xml << "<char acct=\"" << u->GetAccountID() << "\" cc=\"0\" gm=\"0\" ft=\"0\" llog=\"" << time(NULL) << "\" ";
xml << "ls=\"0\" lzx=\"-626.5847\" lzy=\"613.3515\" lzz=\"-28.6374\" lzrx=\"0.0\" lzry=\"0.7015\" lzrz=\"0.0\" lzrw=\"0.7126\" ";
xml << "stt=\"0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;\"></char>";
xml << "stt=\"0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;\">";
xml << "<vl><l id=\"1000\" cid=\"0\"/></vl>";
xml << "</char>";
xml << "<dest hm=\"4\" hc=\"4\" im=\"0\" ic=\"0\" am=\"0\" ac=\"0\" d=\"0\"/>";
@@ -522,6 +526,13 @@ void UserManager::LoginCharacter(const SystemAddress& sysAddr, uint32_t playerID
ZoneInstanceManager::Instance()->RequestZoneTransfer(Game::server, zoneID, character->GetZoneClone(), false, [=](bool mythranShift, uint32_t zoneID, uint32_t zoneInstance, uint32_t zoneClone, std::string serverIP, uint16_t serverPort) {
LOG("Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i", character->GetName().c_str(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort);
if (character) {
auto* entity = character->GetEntity();
if (entity) {
auto* characterComponent = entity->GetComponent<CharacterComponent>();
if (characterComponent) {
characterComponent->AddVisitedLevel(LWOZONEID(zoneID, LWOINSTANCEID_INVALID, zoneClone));
}
}
character->SetZoneID(zoneID);
character->SetZoneInstance(zoneInstance);
character->SetZoneClone(zoneClone);

View File

@@ -20,7 +20,7 @@ void BasicAttackBehavior::Handle(BehaviorContext* context, RakNet::BitStream& bi
//Handle player damage cooldown
if (entity->IsPlayer() && !this->m_DontApplyImmune) {
const float immunityTime = Game::zoneManager->GetWorldConfig()->globalImmunityTime;
const float immunityTime = Game::zoneManager->GetWorldConfig().globalImmunityTime;
destroyableComponent->SetDamageCooldownTimer(immunityTime);
}
}
@@ -214,7 +214,7 @@ void BasicAttackBehavior::DoBehaviorCalculation(BehaviorContext* context, RakNet
//Handle player damage cooldown
if (isSuccess && targetEntity->IsPlayer() && !this->m_DontApplyImmune) {
destroyableComponent->SetDamageCooldownTimer(Game::zoneManager->GetWorldConfig()->globalImmunityTime);
destroyableComponent->SetDamageCooldownTimer(Game::zoneManager->GetWorldConfig().globalImmunityTime);
}
eBasicAttackSuccessTypes successState = eBasicAttackSuccessTypes::FAILIMMUNE;

View File

@@ -42,10 +42,15 @@ void PropertyTeleportBehavior::Handle(BehaviorContext* context, RakNet::BitStrea
LOG("Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i", sysAddr.ToString(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort);
if (entity->GetCharacter()) {
auto* characterComponent = entity->GetComponent<CharacterComponent>();
if (characterComponent) {
characterComponent->AddVisitedLevel(LWOZONEID(zoneID, LWOINSTANCEID_INVALID, zoneClone));
characterComponent->SetLastRocketConfig(u"");
}
entity->GetCharacter()->SetZoneID(zoneID);
entity->GetCharacter()->SetZoneInstance(zoneInstance);
entity->GetCharacter()->SetZoneClone(zoneClone);
entity->GetComponent<CharacterComponent>()->SetLastRocketConfig(u"");
}
entity->GetCharacter()->SaveXMLToDatabase();

View File

@@ -27,6 +27,7 @@
#include "CDActivityRewardsTable.h"
#include "CDActivitiesTable.h"
#include "LeaderboardManager.h"
#include "CharacterComponent.h"
ActivityComponent::ActivityComponent(Entity* parent, int32_t activityID) : Component(parent) {
/*
@@ -526,6 +527,11 @@ void ActivityInstance::StartZone() {
LOG("Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i", player->GetCharacter()->GetName().c_str(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort);
if (player->GetCharacter()) {
auto* characterComponent = player->GetComponent<CharacterComponent>();
if (characterComponent) {
characterComponent->AddVisitedLevel(LWOZONEID(zoneID, LWOINSTANCEID_INVALID, zoneClone));
}
player->GetCharacter()->SetZoneID(zoneID);
player->GetCharacter()->SetZoneInstance(zoneInstance);
player->GetCharacter()->SetZoneClone(zoneClone);

View File

@@ -95,15 +95,21 @@ bool CharacterComponent::LandingAnimDisabled(int zoneID) {
case 556:
case 1101:
case 1202:
case 1150:
case 1151:
case 1203:
case 1204:
case 1250:
case 1251:
case 1261:
case 1301:
case 1302:
case 1303:
case 1350:
case 1401:
case 1402:
case 1403:
case 1450:
case 1603:
case 2001:
return true;
@@ -239,6 +245,8 @@ void CharacterComponent::LoadFromXml(const tinyxml2::XMLDocument& doc) {
SetReputation(0);
}
auto* vl = character->FirstChildElement("vl");
if (vl) LoadVisitedLevelsXml(*vl);
character->QueryUnsigned64Attribute("co", &m_ClaimCodes[0]);
character->QueryUnsigned64Attribute("co1", &m_ClaimCodes[1]);
character->QueryUnsigned64Attribute("co2", &m_ClaimCodes[2]);
@@ -368,6 +376,10 @@ void CharacterComponent::UpdateXml(tinyxml2::XMLDocument& doc) {
return;
}
auto* vl = character->FirstChildElement("vl");
if (!vl) vl = character->InsertNewChildElement("vl");
UpdateVisitedLevelsXml(*vl);
if (m_ClaimCodes[0] != 0) character->SetAttribute("co", m_ClaimCodes[0]);
if (m_ClaimCodes[1] != 0) character->SetAttribute("co1", m_ClaimCodes[1]);
if (m_ClaimCodes[2] != 0) character->SetAttribute("co2", m_ClaimCodes[2]);
@@ -849,8 +861,9 @@ void CharacterComponent::SendToZone(LWOMAPID zoneId, LWOCLONEID cloneId) const {
character->SetZoneID(zoneID);
character->SetZoneInstance(zoneInstance);
character->SetZoneClone(zoneClone);
characterComponent->SetLastRocketConfig(u"");
characterComponent->AddVisitedLevel(LWOZONEID(zoneID, LWOINSTANCEID_INVALID, zoneClone));
character->SaveXMLToDatabase();
}
@@ -877,3 +890,30 @@ void CharacterComponent::SetRespawnPos(const NiPoint3& position) {
void CharacterComponent::SetRespawnRot(const NiQuaternion& rotation) {
m_respawnRot = rotation;
}
void CharacterComponent::AddVisitedLevel(const LWOZONEID zoneID) {
LWOZONEID toInsert(zoneID.GetMapID(), LWOINSTANCEID_INVALID, zoneID.GetCloneID());
m_VisitedLevels.insert(toInsert);
}
void CharacterComponent::UpdateVisitedLevelsXml(tinyxml2::XMLElement& vl) {
vl.DeleteChildren();
// <vl>
for (const auto zoneID : m_VisitedLevels) {
// <l id=\"1100\" cid=\"0\"/>
auto* l = vl.InsertNewChildElement("l");
l->SetAttribute("id", zoneID.GetMapID());
l->SetAttribute("cid", zoneID.GetCloneID());
}
// </vl>
}
void CharacterComponent::LoadVisitedLevelsXml(const tinyxml2::XMLElement& vl) {
// <vl>
for (const auto* l = vl.FirstChildElement("l"); l != nullptr; l = l->NextSiblingElement("l")) {
// <l id=\"1100\" cid=\"0\"/>
LWOZONEID toInsert(l->IntAttribute("id"), LWOINSTANCEID_INVALID, l->IntAttribute("cid"));
m_VisitedLevels.insert(toInsert);
}
// </vl>
}

View File

@@ -10,6 +10,7 @@
#include "tinyxml2.h"
#include "eReplicaComponentType.h"
#include <array>
#include <set>
#include "Loot.h"
enum class eGameActivity : uint32_t;
@@ -321,6 +322,13 @@ public:
* Character info regarding this character, including clothing styles, etc.
*/
Character* m_Character;
/* Saves the provided zoneID as a visited level. Ignores InstanceID */
void AddVisitedLevel(const LWOZONEID zoneID);
/* Updates the VisitedLevels (vl) node of the charxml */
void UpdateVisitedLevelsXml(tinyxml2::XMLElement& doc);
/* Reads the VisitedLevels (vl) node of the charxml */
void LoadVisitedLevelsXml(const tinyxml2::XMLElement& doc);
private:
bool OnRequestServerObjectInfo(GameMessages::GameMsg& msg);
@@ -619,6 +627,8 @@ private:
std::map<LWOOBJID, Loot::Info> m_DroppedLoot;
uint64_t m_DroppedCoins = 0;
std::set<LWOZONEID> m_VisitedLevels;
};
#endif // CHARACTERCOMPONENT_H

View File

@@ -774,10 +774,10 @@ void DestroyableComponent::Smash(const LWOOBJID source, const eKillType killType
if (Game::zoneManager->GetPlayerLoseCoinOnDeath()) {
auto* character = m_Parent->GetCharacter();
uint64_t coinsTotal = character->GetCoins();
const uint64_t minCoinsToLose = Game::zoneManager->GetWorldConfig()->coinsLostOnDeathMin;
const uint64_t minCoinsToLose = Game::zoneManager->GetWorldConfig().coinsLostOnDeathMin;
if (coinsTotal >= minCoinsToLose) {
const uint64_t maxCoinsToLose = Game::zoneManager->GetWorldConfig()->coinsLostOnDeathMax;
const float coinPercentageToLose = Game::zoneManager->GetWorldConfig()->coinsLostOnDeathPercent;
const uint64_t maxCoinsToLose = Game::zoneManager->GetWorldConfig().coinsLostOnDeathMax;
const float coinPercentageToLose = Game::zoneManager->GetWorldConfig().coinsLostOnDeathPercent;
uint64_t coinsToLose = std::max(static_cast<uint64_t>(coinsTotal * coinPercentageToLose), minCoinsToLose);
coinsToLose = std::min(maxCoinsToLose, coinsToLose);

View File

@@ -10,12 +10,50 @@
#include "SimplePhysicsComponent.h"
#include "Database.h"
#include "DluAssert.h"
ModelComponent::ModelComponent(Entity* parent) : Component(parent) {
m_OriginalPosition = m_Parent->GetDefaultPosition();
m_OriginalRotation = m_Parent->GetDefaultRotation();
m_IsPaused = false;
m_NumListeningInteract = 0;
m_userModelID = m_Parent->GetVarAs<LWOOBJID>(u"userModelID");
RegisterMsg(MessageType::Game::REQUEST_USE, this, &ModelComponent::OnRequestUse);
RegisterMsg(MessageType::Game::RESET_MODEL_TO_DEFAULTS, this, &ModelComponent::OnResetModelToDefaults);
}
bool ModelComponent::OnResetModelToDefaults(GameMessages::GameMsg& msg) {
auto& reset = static_cast<GameMessages::ResetModelToDefaults&>(msg);
for (auto& behavior : m_Behaviors) behavior.HandleMsg(reset);
GameMessages::UnSmash unsmash;
unsmash.target = GetParent()->GetObjectID();
unsmash.duration = 0.0f;
unsmash.Send(UNASSIGNED_SYSTEM_ADDRESS);
m_NumListeningInteract = 0;
m_NumActiveUnSmash = 0;
m_Dirty = true;
Game::entityManager->SerializeEntity(GetParent());
return true;
}
bool ModelComponent::OnRequestUse(GameMessages::GameMsg& msg) {
bool toReturn = false;
if (!m_IsPaused) {
auto& requestUse = static_cast<GameMessages::RequestUse&>(msg);
for (auto& behavior : m_Behaviors) behavior.HandleMsg(requestUse);
toReturn = true;
}
return toReturn;
}
void ModelComponent::Update(float deltaTime) {
if (m_IsPaused) return;
for (auto& behavior : m_Behaviors) {
behavior.Update(deltaTime, *this);
}
}
void ModelComponent::LoadBehaviors() {
@@ -29,9 +67,9 @@ void ModelComponent::LoadBehaviors() {
LOG_DEBUG("Loading behavior %d", behaviorId.value());
auto& inserted = m_Behaviors.emplace_back();
inserted.SetBehaviorId(*behaviorId);
const auto behaviorStr = Database::Get()->GetBehavior(behaviorId.value());
tinyxml2::XMLDocument behaviorXml;
auto res = behaviorXml.Parse(behaviorStr.c_str(), behaviorStr.size());
LOG_DEBUG("Behavior %i %d: %s", res, behaviorId.value(), behaviorStr.c_str());
@@ -45,6 +83,11 @@ void ModelComponent::LoadBehaviors() {
}
}
void ModelComponent::Resume() {
m_Dirty = true;
m_IsPaused = false;
}
void ModelComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
// ItemComponent Serialization. Pets do not get this serialization.
if (!m_Parent->HasComponent(eReplicaComponentType::PET)) {
@@ -56,14 +99,14 @@ void ModelComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialU
//actual model component:
outBitStream.Write1(); // Yes we are writing model info
outBitStream.Write0(); // Is pickable
outBitStream.Write(m_NumListeningInteract > 0); // Is pickable
outBitStream.Write<uint32_t>(2); // Physics type
outBitStream.Write(m_OriginalPosition); // Original position
outBitStream.Write(m_OriginalRotation); // Original rotation
outBitStream.Write1(); // We are writing behavior info
outBitStream.Write<uint32_t>(0); // Number of behaviors
outBitStream.Write1(); // Is this model paused
outBitStream.Write<uint32_t>(m_Behaviors.size()); // Number of behaviors
outBitStream.Write(m_IsPaused); // Is this model paused
if (bIsInitialUpdate) outBitStream.Write0(); // We are not writing model editing info
}
@@ -135,3 +178,28 @@ std::array<std::pair<int32_t, std::string>, 5> ModelComponent::GetBehaviorsForSa
}
return toReturn;
}
void ModelComponent::AddInteract() {
LOG_DEBUG("Adding interact %i", m_NumListeningInteract);
m_Dirty = true;
m_NumListeningInteract++;
}
void ModelComponent::RemoveInteract() {
DluAssert(m_NumListeningInteract > 0);
LOG_DEBUG("Removing interact %i", m_NumListeningInteract);
m_Dirty = true;
m_NumListeningInteract--;
}
void ModelComponent::AddUnSmash() {
LOG_DEBUG("Adding UnSmash %i", m_NumActiveUnSmash);
m_NumActiveUnSmash++;
}
void ModelComponent::RemoveUnSmash() {
// Players can assign an UnSmash without a Smash so an assert would be bad here
if (m_NumActiveUnSmash == 0) return;
LOG_DEBUG("Removing UnSmash %i", m_NumActiveUnSmash);
m_NumActiveUnSmash--;
}

View File

@@ -30,6 +30,10 @@ public:
ModelComponent(Entity* parent);
void LoadBehaviors();
void Update(float deltaTime) override;
bool OnRequestUse(GameMessages::GameMsg& msg);
bool OnResetModelToDefaults(GameMessages::GameMsg& msg);
void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) override;
@@ -59,7 +63,7 @@ public:
/**
* Main gateway for all behavior messages to be passed to their respective behaviors.
*
*
* @tparam Msg The message type to pass
* @param args the arguments of the message to be deserialized
*/
@@ -68,7 +72,7 @@ public:
static_assert(std::is_base_of_v<BehaviorMessageBase, Msg>, "Msg must be a BehaviorMessageBase");
Msg msg{ args };
for (auto&& behavior : m_Behaviors) {
if (behavior.GetBehaviorId() == msg.GetBehaviorId()) {
if (behavior.GetBehaviorId() == msg.GetBehaviorId()) {
behavior.HandleMsg(msg);
return;
}
@@ -109,12 +113,35 @@ public:
void SendBehaviorListToClient(AMFArrayValue& args) const;
void SendBehaviorBlocksToClient(int32_t behaviorToSend, AMFArrayValue& args) const;
void VerifyBehaviors();
std::array<std::pair<int32_t, std::string>, 5> GetBehaviorsForSave() const;
const std::vector<PropertyBehavior>& GetBehaviors() const { return m_Behaviors; };
void AddInteract();
void RemoveInteract();
void Pause() { m_Dirty = true; m_IsPaused = true; }
void AddUnSmash();
void RemoveUnSmash();
bool IsUnSmashing() const { return m_NumActiveUnSmash != 0; }
void Resume();
private:
// Number of Actions that are awaiting an UnSmash to finish.
uint32_t m_NumActiveUnSmash{};
// Whether or not this component needs to have its extra data serialized.
bool m_Dirty{};
// The number of strips listening for a RequestUse GM to come in.
uint32_t m_NumListeningInteract{};
// Whether or not the model is paused and should reject all interactions regarding behaviors.
bool m_IsPaused{};
/**
* The behaviors of the model
* Note: This is a vector because the order of the behaviors matters when serializing to the client.

View File

@@ -26,6 +26,7 @@
#include <vector>
#include "CppScripts.h"
#include <ranges>
#include "dConfig.h"
PropertyManagementComponent* PropertyManagementComponent::instance = nullptr;
@@ -151,7 +152,11 @@ void PropertyManagementComponent::SetPrivacyOption(PropertyPrivacyOption value)
info.rejectionReason = rejectionReason;
info.modApproved = 0;
Database::Get()->UpdatePropertyModerationInfo(info);
if (models.empty() && Game::config->GetValue("auto_reject_empty_properties") == "1") {
UpdateApprovedStatus(false, "Your property is empty. Please place a model to have a public property.");
} else {
Database::Get()->UpdatePropertyModerationInfo(info);
}
}
void PropertyManagementComponent::UpdatePropertyDetails(std::string name, std::string description) {
@@ -165,7 +170,9 @@ void PropertyManagementComponent::UpdatePropertyDetails(std::string name, std::s
info.id = propertyId;
info.name = propertyName;
info.description = propertyDescription;
info.lastUpdatedTime = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
Database::Get()->UpdateLastSave(info);
Database::Get()->UpdatePropertyDetails(info);
OnQueryPropertyData(GetOwner(), UNASSIGNED_SYSTEM_ADDRESS);
@@ -253,6 +260,18 @@ void PropertyManagementComponent::OnStartBuilding() {
// Push equipped items
if (inventoryComponent) inventoryComponent->PushEquippedItems();
for (auto modelID : models | std::views::keys) {
auto* model = Game::entityManager->GetEntity(modelID);
if (model) {
auto* modelComponent = model->GetComponent<ModelComponent>();
if (modelComponent) modelComponent->Pause();
Game::entityManager->SerializeEntity(model);
GameMessages::ResetModelToDefaults reset;
reset.target = modelID;
model->HandleMsg(reset);
}
}
}
void PropertyManagementComponent::OnFinishBuilding() {
@@ -265,6 +284,18 @@ void PropertyManagementComponent::OnFinishBuilding() {
UpdateApprovedStatus(false);
Save();
for (auto modelID : models | std::views::keys) {
auto* model = Game::entityManager->GetEntity(modelID);
if (model) {
auto* modelComponent = model->GetComponent<ModelComponent>();
if (modelComponent) modelComponent->Resume();
Game::entityManager->SerializeEntity(model);
GameMessages::ResetModelToDefaults reset;
reset.target = modelID;
model->HandleMsg(reset);
}
}
}
void PropertyManagementComponent::UpdateModelPosition(const LWOOBJID id, const NiPoint3 position, NiQuaternion rotation) {
@@ -316,6 +347,8 @@ void PropertyManagementComponent::UpdateModelPosition(const LWOOBJID id, const N
Entity* newEntity = Game::entityManager->CreateEntity(info);
if (newEntity != nullptr) {
Game::entityManager->ConstructEntity(newEntity);
auto* modelComponent = newEntity->GetComponent<ModelComponent>();
if (modelComponent) modelComponent->Pause();
// Make sure the propMgmt doesn't delete our model after the server dies
// Trying to do this after the entity is constructed. Shouldn't really change anything but
@@ -361,6 +394,8 @@ void PropertyManagementComponent::UpdateModelPosition(const LWOOBJID id, const N
info.nodes[0]->config.push_back(new LDFData<int>(u"componentWhitelist", 1));
auto* model = spawner->Spawn();
auto* modelComponent = model->GetComponent<ModelComponent>();
if (modelComponent) modelComponent->Pause();
models.insert_or_assign(model->GetObjectID(), spawnerId);
@@ -535,14 +570,14 @@ void PropertyManagementComponent::DeleteModel(const LWOOBJID id, const int delet
}
}
void PropertyManagementComponent::UpdateApprovedStatus(const bool value) {
void PropertyManagementComponent::UpdateApprovedStatus(const bool value, const std::string& rejectionReason) {
if (owner == LWOOBJID_EMPTY) return;
IProperty::Info info;
info.id = propertyId;
info.modApproved = value;
info.privacyOption = static_cast<uint32_t>(privacyOption);
info.rejectionReason = "";
info.rejectionReason = rejectionReason;
Database::Get()->UpdatePropertyModerationInfo(info);
}
@@ -688,6 +723,10 @@ void PropertyManagementComponent::Save() {
Database::Get()->RemoveModel(model.id);
}
IProperty::Info info;
info.id = propertyId;
info.lastUpdatedTime = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
Database::Get()->UpdateLastSave(info);
}
void PropertyManagementComponent::AddModel(LWOOBJID modelId, LWOOBJID spawnerId) {

View File

@@ -135,7 +135,7 @@ public:
* Updates whether or not this property is approved by a moderator
* @param value true if the property should be approved, false otherwise
*/
void UpdateApprovedStatus(bool value);
void UpdateApprovedStatus(bool value, const std::string& rejectionReason = "");
/**
* Loads all the models on this property from the database

View File

@@ -101,7 +101,7 @@ void VendorComponent::SetupConstants() {
std::vector<CDVendorComponent> vendorComps = vendorComponentTable->Query([=](CDVendorComponent entry) { return (entry.id == componentID); });
if (vendorComps.empty()) return;
auto vendorData = vendorComps.at(0);
if (vendorData.buyScalar == 0.0) m_BuyScalar = Game::zoneManager->GetWorldConfig()->vendorBuyMultiplier;
if (vendorData.buyScalar == 0.0) m_BuyScalar = Game::zoneManager->GetWorldConfig().vendorBuyMultiplier;
else m_BuyScalar = vendorData.buyScalar;
m_SellScalar = vendorData.sellScalar;
m_RefreshTimeSeconds = vendorData.refreshTimeSeconds;

View File

@@ -45,6 +45,7 @@ namespace {
using namespace GameMessages;
using MessageCreator = std::function<std::unique_ptr<GameMessages::GameMsg>()>;
std::map<MessageType::Game, MessageCreator> g_MessageHandlers = {
{ REQUEST_USE, []() { return std::make_unique<RequestUse>(); }},
{ REQUEST_SERVER_OBJECT_INFO, []() { return std::make_unique<RequestServerObjectInfo>(); } },
{ SHOOTING_GALLERY_FIRE, []() { return std::make_unique<ShootingGalleryFire>(); } },
};
@@ -118,11 +119,6 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream& inStream, const System
break;
}
case MessageType::Game::REQUEST_USE: {
GameMessages::HandleRequestUse(inStream, entity, sysAddr);
break;
}
case MessageType::Game::SET_FLAG: {
GameMessages::HandleSetFlag(inStream, entity);
break;

View File

@@ -102,6 +102,8 @@
#include "CDComponentsRegistryTable.h"
#include "CDObjectsTable.h"
#include "eItemType.h"
#include "Lxfml.h"
#include "Sd0.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;
@@ -1696,7 +1698,8 @@ void GameMessages::HandleRequestActivitySummaryLeaderboardData(RakNet::BitStream
bool weekly = inStream.ReadBit();
LeaderboardManager::SendLeaderboard(gameID, queryType, weekly, entity->GetObjectID(), entity->GetObjectID());
// The client won't accept more than 10 results even if we wanted it to
LeaderboardManager::SendLeaderboard(gameID, queryType, weekly, entity->GetObjectID(), entity->GetObjectID(), 10);
}
void GameMessages::HandleActivityStateChangeRequest(RakNet::BitStream& inStream, Entity* entity) {
@@ -2574,18 +2577,6 @@ void GameMessages::HandleBBBSaveRequest(RakNet::BitStream& inStream, Entity* ent
TODO Apparently the bricks are supposed to be taken via MoveInventoryBatch?
*/
////Decompress the SD0 from the client so we can process the lxfml properly
//uint8_t* outData = new uint8_t[327680];
//int32_t error;
//int32_t size = ZCompression::Decompress(inData, lxfmlSize, outData, 327680, error);
//if (size == -1) {
// LOG("Failed to decompress LXFML: (%i)", error);
// return;
//}
//
//std::string lxfml(reinterpret_cast<char*>(outData), size); //std::string version of the decompressed data!
//Now, the cave of dragons:
//We runs this in async because the http library here is blocking, meaning it'll halt the thread.
@@ -2613,16 +2604,25 @@ void GameMessages::HandleBBBSaveRequest(RakNet::BitStream& inStream, Entity* ent
LWOOBJID propertyId = LWOOBJID_EMPTY;
if (propertyInfo) propertyId = propertyInfo->id;
//Insert into ugc:
// Save the binary data to the Sd0 buffer
std::string str(sd0Data.get(), sd0Size);
std::istringstream sd0DataStream(str);
Database::Get()->InsertNewUgcModel(sd0DataStream, blueprintIDSmall, entity->GetCharacter()->GetParentUser()->GetAccountID(), entity->GetCharacter()->GetID());
Sd0 sd0(sd0DataStream);
// Uncompress the data and normalize the position
const auto asStr = sd0.GetAsStringUncompressed();
const auto [newLxfml, newCenter] = Lxfml::NormalizePosition(asStr);
// Recompress the data and save to the database
sd0.FromData(reinterpret_cast<const uint8_t*>(newLxfml.data()), newLxfml.size());
auto sd0AsStream = sd0.GetAsStream();
Database::Get()->InsertNewUgcModel(sd0AsStream, blueprintIDSmall, entity->GetCharacter()->GetParentUser()->GetAccountID(), entity->GetCharacter()->GetID());
//Insert into the db as a BBB model:
IPropertyContents::Model model;
model.id = newIDL;
model.ugcId = blueprintIDSmall;
model.position = NiPoint3Constant::ZERO;
model.position = newCenter;
model.rotation = NiQuaternion(0.0f, 0.0f, 0.0f, 0.0f);
model.lot = 14;
Database::Get()->InsertNewPropertyModel(propertyId, model, "Objects_14_name");
@@ -2648,6 +2648,9 @@ void GameMessages::HandleBBBSaveRequest(RakNet::BitStream& inStream, Entity* ent
//}
//Tell the client their model is saved: (this causes us to actually pop out of our current state):
const auto& newSd0 = sd0.GetAsVector();
uint32_t sd0Size{};
for (const auto& chunk : newSd0) sd0Size += chunk.size();
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, MessageType::Client::BLUEPRINT_SAVE_RESPONSE);
bitStream.Write(localId);
@@ -2655,9 +2658,9 @@ void GameMessages::HandleBBBSaveRequest(RakNet::BitStream& inStream, Entity* ent
bitStream.Write<uint32_t>(1);
bitStream.Write(blueprintID);
bitStream.Write<uint32_t>(sd0Size);
bitStream.Write(sd0Size);
bitStream.WriteAlignedBytes(reinterpret_cast<unsigned char*>(sd0Data.get()), sd0Size);
for (const auto& chunk : newSd0) bitStream.WriteAlignedBytes(reinterpret_cast<const unsigned char*>(chunk.data()), chunk.size());
SEND_PACKET;
@@ -2665,7 +2668,7 @@ void GameMessages::HandleBBBSaveRequest(RakNet::BitStream& inStream, Entity* ent
EntityInfo info;
info.lot = 14;
info.pos = {};
info.pos = newCenter;
info.rot = {};
info.spawner = nullptr;
info.spawnerID = entity->GetObjectID();
@@ -4923,6 +4926,11 @@ void GameMessages::HandleFireEventServerSide(RakNet::BitStream& inStream, Entity
LOG("Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i", character->GetName().c_str(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort);
if (character) {
auto* characterComponent = player->GetComponent<CharacterComponent>();
if (characterComponent) {
characterComponent->AddVisitedLevel(LWOZONEID(zoneID, LWOINSTANCEID_INVALID, zoneClone));
}
character->SetZoneID(zoneID);
character->SetZoneInstance(zoneInstance);
character->SetZoneClone(zoneClone);
@@ -4954,54 +4962,6 @@ void GameMessages::HandleQuickBuildCancel(RakNet::BitStream& inStream, Entity* e
quickBuildComponent->CancelQuickBuild(Game::entityManager->GetEntity(userID), eQuickBuildFailReason::CANCELED_EARLY);
}
void GameMessages::HandleRequestUse(RakNet::BitStream& inStream, Entity* entity, const SystemAddress& sysAddr) {
bool bIsMultiInteractUse = false;
unsigned int multiInteractID;
int multiInteractType;
bool secondary;
LWOOBJID objectID;
inStream.Read(bIsMultiInteractUse);
inStream.Read(multiInteractID);
inStream.Read(multiInteractType);
inStream.Read(objectID);
inStream.Read(secondary);
Entity* interactedObject = Game::entityManager->GetEntity(objectID);
if (interactedObject == nullptr) {
LOG("Object %llu tried to interact, but doesn't exist!", objectID);
return;
}
if (interactedObject->GetLOT() == 9524) {
entity->GetCharacter()->SetBuildMode(true);
}
if (bIsMultiInteractUse) {
if (multiInteractType == 0) {
auto* missionOfferComponent = static_cast<MissionOfferComponent*>(interactedObject->GetComponent(eReplicaComponentType::MISSION_OFFER));
if (missionOfferComponent != nullptr) {
missionOfferComponent->OfferMissions(entity, multiInteractID);
}
} else {
interactedObject->OnUse(entity);
}
} else {
interactedObject->OnUse(entity);
}
//Perform use task if possible:
auto missionComponent = static_cast<MissionComponent*>(entity->GetComponent(eReplicaComponentType::MISSION));
if (missionComponent == nullptr) return;
missionComponent->Progress(eMissionTaskType::TALK_TO_NPC, interactedObject->GetLOT(), interactedObject->GetObjectID());
missionComponent->Progress(eMissionTaskType::INTERACT, interactedObject->GetLOT(), interactedObject->GetObjectID());
}
void GameMessages::HandlePlayEmote(RakNet::BitStream& inStream, Entity* entity) {
int emoteID;
LWOOBJID targetID;
@@ -6443,4 +6403,70 @@ namespace GameMessages {
auto* handlingEntity = Game::entityManager->GetEntity(targetForReport);
if (handlingEntity) handlingEntity->HandleMsg(*this);
}
bool RequestUse::Deserialize(RakNet::BitStream& stream) {
if (!stream.Read(bIsMultiInteractUse)) return false;
if (!stream.Read(multiInteractID)) return false;
if (!stream.Read(multiInteractType)) return false;
if (!stream.Read(object)) return false;
if (!stream.Read(secondary)) return false;
return true;
}
void RequestUse::Handle(Entity& entity, const SystemAddress& sysAddr) {
Entity* interactedObject = Game::entityManager->GetEntity(object);
if (interactedObject == nullptr) {
LOG("Object %llu tried to interact, but doesn't exist!", object);
return;
}
if (interactedObject->GetLOT() == 9524) {
entity.GetCharacter()->SetBuildMode(true);
}
if (bIsMultiInteractUse) {
if (multiInteractType == 0) {
auto* missionOfferComponent = static_cast<MissionOfferComponent*>(interactedObject->GetComponent(eReplicaComponentType::MISSION_OFFER));
if (missionOfferComponent != nullptr) {
missionOfferComponent->OfferMissions(&entity, multiInteractID);
}
} else {
interactedObject->OnUse(&entity);
}
} else {
interactedObject->OnUse(&entity);
}
interactedObject->HandleMsg(*this);
//Perform use task if possible:
auto missionComponent = entity.GetComponent<MissionComponent>();
if (!missionComponent) return;
missionComponent->Progress(eMissionTaskType::TALK_TO_NPC, interactedObject->GetLOT(), interactedObject->GetObjectID());
missionComponent->Progress(eMissionTaskType::INTERACT, interactedObject->GetLOT(), interactedObject->GetObjectID());
}
void Smash::Serialize(RakNet::BitStream& stream) const {
stream.Write(bIgnoreObjectVisibility);
stream.Write(force);
stream.Write(ghostCapacity);
stream.Write(killerID);
}
void UnSmash::Serialize(RakNet::BitStream& stream) const {
stream.Write(builderID != LWOOBJID_EMPTY);
if (builderID != LWOOBJID_EMPTY) stream.Write(builderID);
stream.Write(duration != 3.0f);
if (builderID != 3.0f) stream.Write(duration);
}
void PlayBehaviorSound::Serialize(RakNet::BitStream& stream) const {
stream.Write(soundID != -1);
if (soundID != -1) stream.Write(soundID);
}
}

View File

@@ -631,7 +631,6 @@ namespace GameMessages {
void HandleFireEventServerSide(RakNet::BitStream& inStream, Entity* entity, const SystemAddress& sysAddr);
void HandleRequestPlatformResync(RakNet::BitStream& inStream, Entity* entity, const SystemAddress& sysAddr);
void HandleQuickBuildCancel(RakNet::BitStream& inStream, Entity* entity);
void HandleRequestUse(RakNet::BitStream& inStream, Entity* entity, const SystemAddress& sysAddr);
void HandlePlayEmote(RakNet::BitStream& inStream, Entity* entity);
void HandleModularBuildConvertModel(RakNet::BitStream& inStream, Entity* entity, const SystemAddress& sysAddr);
void HandleSetFlag(RakNet::BitStream& inStream, Entity* entity);
@@ -782,6 +781,58 @@ namespace GameMessages {
bool Deserialize(RakNet::BitStream& bitStream) override;
void Handle(Entity& entity, const SystemAddress& sysAddr) override;
};
struct RequestUse : public GameMsg {
RequestUse() : GameMsg(MessageType::Game::REQUEST_USE) {}
bool Deserialize(RakNet::BitStream& stream) override;
void Handle(Entity& entity, const SystemAddress& sysAddr) override;
LWOOBJID object{};
bool secondary{ false };
// Set to true if this coming from a multi-interaction UI on the client.
bool bIsMultiInteractUse{};
// Used only for multi-interaction
unsigned int multiInteractID{};
// Used only for multi-interaction, is of the enum type InteractionType
int multiInteractType{};
};
struct Smash : public GameMsg {
Smash() : GameMsg(MessageType::Game::SMASH) {}
void Serialize(RakNet::BitStream& stream) const;
bool bIgnoreObjectVisibility{};
bool force{};
float ghostCapacity{};
LWOOBJID killerID{};
};
struct UnSmash : public GameMsg {
UnSmash() : GameMsg(MessageType::Game::UN_SMASH) {}
void Serialize(RakNet::BitStream& stream) const;
LWOOBJID builderID{ LWOOBJID_EMPTY };
float duration{ 3.0f };
};
struct PlayBehaviorSound : public GameMsg {
PlayBehaviorSound() : GameMsg(MessageType::Game::PLAY_BEHAVIOR_SOUND) {}
void Serialize(RakNet::BitStream& stream) const;
int32_t soundID{ -1 };
};
struct ResetModelToDefaults : public GameMsg {
ResetModelToDefaults() : GameMsg(MessageType::Game::RESET_MODEL_TO_DEFAULTS) {}
};
};
#endif // GAMEMESSAGES_H

View File

@@ -470,9 +470,9 @@ void Mission::YieldRewards() {
int32_t coinsToSend = 0;
if (info.LegoScore > 0) {
eLootSourceType lootSource = info.isMission ? eLootSourceType::MISSION : eLootSourceType::ACHIEVEMENT;
if (levelComponent->GetLevel() >= Game::zoneManager->GetWorldConfig()->levelCap) {
if (levelComponent->GetLevel() >= Game::zoneManager->GetWorldConfig().levelCap) {
// Since the character is at the level cap we reward them with coins instead of UScore.
coinsToSend += info.LegoScore * Game::zoneManager->GetWorldConfig()->levelCapCurrencyConversion;
coinsToSend += info.LegoScore * Game::zoneManager->GetWorldConfig().levelCapCurrencyConversion;
} else {
characterComponent->SetUScore(characterComponent->GetUScore() + info.LegoScore);
GameMessages::SendModifyLEGOScore(entity, entity->GetSystemAddress(), info.LegoScore, lootSource);

View File

@@ -4,9 +4,13 @@
#include "BehaviorStates.h"
#include "ControlBehaviorMsgs.h"
#include "tinyxml2.h"
#include "ModelComponent.h"
#include <ranges>
PropertyBehavior::PropertyBehavior() {
m_LastEditedState = BehaviorState::HOME_STATE;
m_ActiveState = BehaviorState::HOME_STATE;
}
template<>
@@ -84,6 +88,17 @@ void PropertyBehavior::HandleMsg(AddMessage& msg) {
isLoot = m_BehaviorId != 7965;
};
template<>
void PropertyBehavior::HandleMsg(GameMessages::RequestUse& msg) {
m_States[m_ActiveState].HandleMsg(msg);
}
template<>
void PropertyBehavior::HandleMsg(GameMessages::ResetModelToDefaults& msg) {
m_ActiveState = BehaviorState::HOME_STATE;
for (auto& state : m_States | std::views::values) state.HandleMsg(msg);
}
void PropertyBehavior::SendBehaviorListToClient(AMFArrayValue& args) const {
args.Insert("id", std::to_string(m_BehaviorId));
args.Insert("name", m_Name);
@@ -153,3 +168,7 @@ void PropertyBehavior::Deserialize(const tinyxml2::XMLElement& behavior) {
m_States[static_cast<BehaviorState>(stateId)].Deserialize(*stateElement);
}
}
void PropertyBehavior::Update(float deltaTime, ModelComponent& modelComponent) {
for (auto& state : m_States | std::views::values) state.Update(deltaTime, modelComponent);
}

View File

@@ -10,6 +10,7 @@ namespace tinyxml2 {
enum class BehaviorState : uint32_t;
class AMFArrayValue;
class ModelComponent;
/**
* Represents the Entity of a Property Behavior and holds data associated with the behavior
@@ -31,7 +32,12 @@ public:
void Serialize(tinyxml2::XMLElement& behavior) const;
void Deserialize(const tinyxml2::XMLElement& behavior);
void Update(float deltaTime, ModelComponent& modelComponent);
private:
// The current active behavior state. Behaviors can only be in ONE state at a time.
BehaviorState m_ActiveState;
// The states this behavior has.
std::map<BehaviorState, State> m_States;

View File

@@ -117,6 +117,16 @@ void State::HandleMsg(MigrateActionsMessage& msg) {
}
};
template<>
void State::HandleMsg(GameMessages::RequestUse& msg) {
for (auto& strip : m_Strips) strip.HandleMsg(msg);
}
template<>
void State::HandleMsg(GameMessages::ResetModelToDefaults& msg) {
for (auto& strip : m_Strips) strip.HandleMsg(msg);
}
bool State::IsEmpty() const {
for (const auto& strip : m_Strips) {
if (!strip.IsEmpty()) return false;
@@ -152,3 +162,7 @@ void State::Deserialize(const tinyxml2::XMLElement& state) {
strip.Deserialize(*stripElement);
}
}
void State::Update(float deltaTime, ModelComponent& modelComponent) {
for (auto& strip : m_Strips) strip.Update(deltaTime, modelComponent);
}

View File

@@ -8,6 +8,7 @@ namespace tinyxml2 {
}
class AMFArrayValue;
class ModelComponent;
class State {
public:
@@ -19,7 +20,11 @@ public:
void Serialize(tinyxml2::XMLElement& state) const;
void Deserialize(const tinyxml2::XMLElement& state);
void Update(float deltaTime, ModelComponent& modelComponent);
private:
// The strips contained within this state.
std::vector<Strip> m_Strips;
};

View File

@@ -3,6 +3,11 @@
#include "Amf3.h"
#include "ControlBehaviorMsgs.h"
#include "tinyxml2.h"
#include "dEntity/EntityInfo.h"
#include "ModelComponent.h"
#include "PlayerManager.h"
#include "DluAssert.h"
template <>
void Strip::HandleMsg(AddStripMessage& msg) {
@@ -75,7 +80,138 @@ void Strip::HandleMsg(MigrateActionsMessage& msg) {
} else {
m_Actions.insert(m_Actions.begin() + msg.GetDstActionIndex(), msg.GetMigratedActions().begin(), msg.GetMigratedActions().end());
}
};
}
template<>
void Strip::HandleMsg(GameMessages::RequestUse& msg) {
if (m_PausedTime > 0.0f) return;
if (m_Actions[m_NextActionIndex].GetType() == "OnInteract") {
IncrementAction();
m_WaitingForAction = false;
}
}
template<>
void Strip::HandleMsg(GameMessages::ResetModelToDefaults& msg) {
m_WaitingForAction = false;
m_PausedTime = 0.0f;
m_NextActionIndex = 0;
}
void Strip::IncrementAction() {
if (m_Actions.empty()) return;
m_NextActionIndex++;
m_NextActionIndex %= m_Actions.size();
}
void Strip::Spawn(LOT lot, Entity& entity) {
EntityInfo info{};
info.lot = lot;
info.pos = entity.GetPosition();
info.rot = NiQuaternionConstant::IDENTITY;
info.spawnerID = entity.GetObjectID();
Game::entityManager->ConstructEntity(Game::entityManager->CreateEntity(info, nullptr, &entity));
}
// Spawns a specific drop for all
void Strip::SpawnDrop(LOT dropLOT, Entity& entity) {
for (auto* const player : PlayerManager::GetAllPlayers()) {
GameMessages::SendDropClientLoot(player, entity.GetObjectID(), dropLOT, 0, entity.GetPosition());
}
}
void Strip::ProcNormalAction(float deltaTime, ModelComponent& modelComponent) {
auto& entity = *modelComponent.GetParent();
auto& nextAction = GetNextAction();
auto number = nextAction.GetValueParameterDouble();
auto numberAsInt = static_cast<int32_t>(number);
auto nextActionType = GetNextAction().GetType();
if (nextActionType == "SpawnStromling") {
Spawn(10495, entity); // Stromling property
} else if (nextActionType == "SpawnPirate") {
Spawn(10497, entity); // Maelstrom Pirate property
} else if (nextActionType == "SpawnRonin") {
Spawn(10498, entity); // Dark Ronin property
} else if (nextActionType == "DropImagination") {
for (; numberAsInt > 0; numberAsInt--) SpawnDrop(935, entity); // 1 Imagination powerup
} else if (nextActionType == "DropHealth") {
for (; numberAsInt > 0; numberAsInt--) SpawnDrop(177, entity); // 1 Life powerup
} else if (nextActionType == "DropArmor") {
for (; numberAsInt > 0; numberAsInt--) SpawnDrop(6431, entity); // 1 Armor powerup
} else if (nextActionType == "Smash") {
if (!modelComponent.IsUnSmashing()) {
GameMessages::Smash smash{};
smash.target = entity.GetObjectID();
smash.killerID = entity.GetObjectID();
smash.Send(UNASSIGNED_SYSTEM_ADDRESS);
}
} else if (nextActionType == "UnSmash") {
GameMessages::UnSmash unsmash{};
unsmash.target = entity.GetObjectID();
unsmash.duration = number;
unsmash.builderID = LWOOBJID_EMPTY;
unsmash.Send(UNASSIGNED_SYSTEM_ADDRESS);
modelComponent.AddUnSmash();
m_PausedTime = number;
} else if (nextActionType == "Wait") {
m_PausedTime = number;
} else if (nextActionType == "PlaySound") {
GameMessages::PlayBehaviorSound sound;
sound.target = modelComponent.GetParent()->GetObjectID();
sound.soundID = numberAsInt;
sound.Send(UNASSIGNED_SYSTEM_ADDRESS);
} else {
static std::set<std::string> g_WarnedActions;
if (!g_WarnedActions.contains(nextActionType.data())) {
LOG("Tried to play action (%s) which is not supported.", nextActionType.data());
g_WarnedActions.insert(nextActionType.data());
}
return;
}
IncrementAction();
}
// Decrement references to the previous state if we have progressed to the next one.
void Strip::RemoveStates(ModelComponent& modelComponent) const {
const auto& prevAction = GetPreviousAction();
const auto prevActionType = prevAction.GetType();
if (prevActionType == "OnInteract") {
modelComponent.RemoveInteract();
Game::entityManager->SerializeEntity(modelComponent.GetParent());
} else if (prevActionType == "UnSmash") {
modelComponent.RemoveUnSmash();
}
}
void Strip::Update(float deltaTime, ModelComponent& modelComponent) {
m_PausedTime -= deltaTime;
if (m_PausedTime > 0.0f) return;
m_PausedTime = 0.0f;
if (m_WaitingForAction) return;
auto& entity = *modelComponent.GetParent();
auto& nextAction = GetNextAction();
RemoveStates(modelComponent);
// Check for starting blocks and if not a starting block proc this blocks action
if (m_NextActionIndex == 0) {
if (nextAction.GetType() == "OnInteract") {
modelComponent.AddInteract();
Game::entityManager->SerializeEntity(entity);
m_WaitingForAction = true;
}
} else { // should be a normal block
ProcNormalAction(deltaTime, modelComponent);
}
}
void Strip::SendBehaviorBlocksToClient(AMFArrayValue& args) const {
m_Position.SendBehaviorBlocksToClient(args);
@@ -106,3 +242,13 @@ void Strip::Deserialize(const tinyxml2::XMLElement& strip) {
action.Deserialize(*actionElement);
}
}
const Action& Strip::GetNextAction() const {
DluAssert(m_NextActionIndex < m_Actions.size()); return m_Actions[m_NextActionIndex];
}
const Action& Strip::GetPreviousAction() const {
DluAssert(m_NextActionIndex < m_Actions.size());
size_t index = m_NextActionIndex == 0 ? m_Actions.size() - 1 : m_NextActionIndex - 1;
return m_Actions[index];
}

View File

@@ -11,6 +11,7 @@ namespace tinyxml2 {
}
class AMFArrayValue;
class ModelComponent;
class Strip {
public:
@@ -22,8 +23,30 @@ public:
void Serialize(tinyxml2::XMLElement& strip) const;
void Deserialize(const tinyxml2::XMLElement& strip);
const Action& GetNextAction() const;
const Action& GetPreviousAction() const;
void IncrementAction();
void Spawn(LOT object, Entity& entity);
void Update(float deltaTime, ModelComponent& modelComponent);
void SpawnDrop(LOT dropLOT, Entity& entity);
void ProcNormalAction(float deltaTime, ModelComponent& modelComponent);
void RemoveStates(ModelComponent& modelComponent) const;
private:
// Indicates this Strip is waiting for an action to be taken upon it to progress to its actions
bool m_WaitingForAction{ false };
// The amount of time this strip is paused for. Any interactions with this strip should be bounced if this is greater than 0.
float m_PausedTime{ 0.0f };
// The index of the next action to be played. This should always be within range of [0, m_Actions.size()).
size_t m_NextActionIndex{ 0 };
// The list of actions to be executed on this behavior.
std::vector<Action> m_Actions;
// The location of this strip on the UGBehaviorEditor UI
StripUiPosition m_Position;
};

View File

@@ -81,7 +81,7 @@ namespace Mail {
} else if (GeneralUtils::CaseInsensitiveStringCompare(mailInfo.recipient, character->GetName()) || receiverID->id == character->GetID()) {
response.status = eSendResponse::CannotMailSelf;
} else {
uint32_t mailCost = Game::zoneManager->GetWorldConfig()->mailBaseFee;
uint32_t mailCost = Game::zoneManager->GetWorldConfig().mailBaseFee;
uint32_t stackSize = 0;
auto inventoryComponent = player->GetComponent<InventoryComponent>();
@@ -92,7 +92,7 @@ namespace Mail {
if (hasAttachment) {
item = inventoryComponent->FindItemById(mailInfo.itemID);
if (item) {
mailCost += (item->GetInfo().baseValue * Game::zoneManager->GetWorldConfig()->mailPercentAttachmentFee);
mailCost += (item->GetInfo().baseValue * Game::zoneManager->GetWorldConfig().mailPercentAttachmentFee);
mailInfo.itemLOT = item->GetLot();
}
}

View File

@@ -1049,6 +1049,10 @@ namespace DEVGMCommands {
LOG("Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i", sysAddr.ToString(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort);
if (entity->GetCharacter()) {
auto* characterComponent = entity->GetComponent<CharacterComponent>();
if (characterComponent) {
characterComponent->AddVisitedLevel(LWOZONEID(zoneID, LWOINSTANCEID_INVALID, zoneClone));
}
entity->GetCharacter()->SetZoneID(zoneID);
entity->GetCharacter()->SetZoneInstance(zoneInstance);
entity->GetCharacter()->SetZoneClone(zoneClone);

View File

@@ -168,6 +168,10 @@ namespace GMZeroCommands {
LOG("Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i", entity->GetCharacter()->GetName().c_str(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort);
if (entity->GetCharacter()) {
auto* characterComponent = entity->GetComponent<CharacterComponent>();
if (characterComponent) {
characterComponent->AddVisitedLevel(LWOZONEID(zoneID, LWOINSTANCEID_INVALID, zoneClone));
}
entity->GetCharacter()->SetZoneID(zoneID);
entity->GetCharacter()->SetZoneInstance(zoneInstance);
entity->GetCharacter()->SetZoneClone(zoneClone);
@@ -190,6 +194,10 @@ namespace GMZeroCommands {
LOG("Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i", sysAddr.ToString(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort);
if (entity->GetCharacter()) {
auto* characterComponent = entity->GetComponent<CharacterComponent>();
if (characterComponent) {
characterComponent->AddVisitedLevel(LWOZONEID(zoneID, LWOINSTANCEID_INVALID, zoneClone));
}
entity->GetCharacter()->SetZoneID(zoneID);
entity->GetCharacter()->SetZoneInstance(zoneInstance);
entity->GetCharacter()->SetZoneClone(zoneClone);

View File

@@ -37,9 +37,9 @@ Instance* InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, LW
// If we are shutting down, return a nullptr so a new instance is not created.
if (m_IsShuttingDown) {
LOG("Tried to create a new instance map/instance/clone %i/%i/%i, but Master is shutting down.",
mapID,
m_LastInstanceID + 1,
cloneID);
mapID,
m_LastInstanceID + 1,
cloneID);
return nullptr;
}
//TODO: Update this so that the IP is read from a configuration file instead
@@ -292,9 +292,9 @@ Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID clon
if (m_IsShuttingDown) {
LOG("Tried to create a new private instance map/instance/clone %i/%i/%i, but Master is shutting down.",
mapID,
m_LastInstanceID + 1,
cloneID);
mapID,
m_LastInstanceID + 1,
cloneID);
return nullptr;
}
@@ -333,29 +333,17 @@ Instance* InstanceManager::FindPrivateInstance(const std::string& password) {
}
int InstanceManager::GetSoftCap(LWOMAPID mapID) {
CDZoneTableTable* zoneTable = CDClientManager::GetTable<CDZoneTableTable>();
if (zoneTable) {
const CDZoneTable* zone = zoneTable->Query(mapID);
const CDZoneTable* zone = CDZoneTableTable::Query(mapID);
if (zone != nullptr) {
return zone->population_soft_cap;
}
}
return 8;
// Default to 8 which is the cap for most worlds.
return zone ? zone->population_soft_cap : 8;
}
int InstanceManager::GetHardCap(LWOMAPID mapID) {
CDZoneTableTable* zoneTable = CDClientManager::GetTable<CDZoneTableTable>();
if (zoneTable) {
const CDZoneTable* zone = zoneTable->Query(mapID);
const CDZoneTable* zone = CDZoneTableTable::Query(mapID);
if (zone != nullptr) {
return zone->population_hard_cap;
}
}
return 12;
// Default to 12 which is the cap for most worlds.
return zone ? zone->population_hard_cap : 12;
}
void Instance::SetShutdownComplete(const bool value) {

View File

@@ -61,6 +61,7 @@ struct LUBitStream {
void WriteHeader(RakNet::BitStream& bitStream) const;
bool ReadHeader(RakNet::BitStream& bitStream);
void Send(const SystemAddress& sysAddr) const;
void Broadcast() const { Send(UNASSIGNED_SYSTEM_ADDRESS); };
virtual void Serialize(RakNet::BitStream& bitStream) const {}
virtual bool Deserialize(RakNet::BitStream& bitStream) { return true; }

View File

@@ -98,14 +98,13 @@ void ChatPackets::SendMessageFail(const SystemAddress& sysAddr) {
SEND_PACKET;
}
void ChatPackets::Announcement::Send() {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::GM_ANNOUNCE);
bitStream.Write<uint32_t>(title.size());
bitStream.Write(title);
bitStream.Write<uint32_t>(message.size());
bitStream.Write(message);
SEND_PACKET_BROADCAST;
namespace ChatPackets {
void Announcement::Serialize(RakNet::BitStream& bitStream) const {
bitStream.Write<uint32_t>(title.size());
bitStream.Write(title);
bitStream.Write<uint32_t>(message.size());
bitStream.Write(message);
}
}
void ChatPackets::AchievementNotify::Serialize(RakNet::BitStream& bitstream) const {

View File

@@ -30,10 +30,12 @@ struct FindPlayerRequest{
namespace ChatPackets {
struct Announcement {
struct Announcement : public LUBitStream {
std::string title;
std::string message;
void Send();
Announcement() : LUBitStream(eConnectionType::CHAT, MessageType::Chat::GM_ANNOUNCE) {};
virtual void Serialize(RakNet::BitStream& bitStream) const override;
};
struct AchievementNotify : public LUBitStream {

View File

@@ -134,7 +134,7 @@ void WorldPackets::SendCreateCharacter(const SystemAddress& sysAddr, int64_t rep
LOG("Sent CreateCharacter for ID: %llu", player);
}
void WorldPackets::SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::vector<std::pair<uint8_t, uint8_t>> unacceptedItems) {
void WorldPackets::SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::set<std::pair<uint8_t, uint8_t>> unacceptedItems) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, MessageType::Client::CHAT_MODERATION_STRING);

View File

@@ -32,7 +32,7 @@ namespace WorldPackets {
void SendTransferToWorld(const SystemAddress& sysAddr, const std::string& serverIP, uint32_t serverPort, bool mythranShift);
void SendServerState(const SystemAddress& sysAddr);
void SendCreateCharacter(const SystemAddress& sysAddr, int64_t reputation, LWOOBJID player, const std::string& xmlData, const std::u16string& username, eGameMasterLevel gm);
void SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::vector<std::pair<uint8_t, uint8_t>> unacceptedItems);
void SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::set<std::pair<uint8_t, uint8_t>> unacceptedItems);
void SendGMLevelChange(const SystemAddress& sysAddr, bool success, eGameMasterLevel highestLevel, eGameMasterLevel prevLevel, eGameMasterLevel newLevel);
void SendHTTPMonitorInfo(const SystemAddress& sysAddr, const HTTPMonitorInfo& info);
void SendDebugOuput(const SystemAddress& sysAddr, const std::string& data);

View File

@@ -121,7 +121,7 @@ void ActivityManager::GetLeaderboardData(Entity* self, const LWOOBJID playerID,
auto* sac = self->GetComponent<ScriptedActivityComponent>();
uint32_t gameID = sac != nullptr ? sac->GetActivityID() : self->GetLOT();
// Save the new score to the leaderboard and show the leaderboard to the player
LeaderboardManager::SendLeaderboard(activityID, Leaderboard::InfoType::MyStanding, false, playerID, self->GetObjectID());
LeaderboardManager::SendLeaderboard(activityID, Leaderboard::InfoType::MyStanding, false, playerID, self->GetObjectID(), numResults);
}
void ActivityManager::ActivityTimerStart(Entity* self, const std::string& timerName, const float_t updateInterval,

View File

@@ -342,7 +342,8 @@ void SGCannon::StartGame(Entity* self) {
auto* player = Game::entityManager->GetEntity(self->GetVar<LWOOBJID>(PlayerIDVariable));
if (player != nullptr) {
GetLeaderboardData(self, player->GetObjectID(), GetActivityID(self), 1);
// The client cant accept more than 10 results.
GetLeaderboardData(self, player->GetObjectID(), GetConstants().activityID, 10);
LOG("Sending ActivityStart");
GameMessages::SendActivityStart(self->GetObjectID(), player->GetSystemAddress());
@@ -612,7 +613,7 @@ void SGCannon::StopGame(Entity* self, bool cancel) {
// Destroy all spawners
for (auto* entity : Game::entityManager->GetEntitiesInGroup("SGEnemy")) {
entity->Kill();
entity->Smash(LWOOBJID_EMPTY, eKillType::SILENT);
}
ResetVars(self);

7
dWeb/CMakeLists.txt Normal file
View File

@@ -0,0 +1,7 @@
set(DWEB_SOURCES
"Web.cpp")
add_library(dWeb STATIC ${DWEB_SOURCES})
target_include_directories(dWeb PUBLIC ".")
target_link_libraries(dWeb dCommon mongoose)

301
dWeb/Web.cpp Normal file
View File

@@ -0,0 +1,301 @@
#include "Web.h"
#include "Game.h"
#include "magic_enum.hpp"
#include "json.hpp"
#include "Logger.h"
#include "eHTTPMethod.h"
#include "GeneralUtils.h"
#include "JSONUtils.h"
#include <ranges>
namespace Game {
Web web;
}
namespace {
const char* jsonContentType = "Content-Type: application/json\r\n";
const std::string wsSubscribed = "{\"status\":\"subscribed\"}";
const std::string wsUnsubscribed = "{\"status\":\"unsubscribed\"}";
std::map<std::pair<eHTTPMethod, std::string>, HTTPRoute> g_HTTPRoutes;
std::map<std::string, WSEvent> g_WSEvents;
std::vector<std::string> g_WSSubscriptions;
}
using json = nlohmann::json;
bool ValidateAuthentication(const mg_http_message* http_msg) {
// TO DO: This is just a placeholder for now
// use tokens or something at a later point if we want to implement authentication
// bit using the listen bind address to limit external access is good enough to start with
return true;
}
void HandleHTTPMessage(mg_connection* connection, const mg_http_message* http_msg) {
if (g_HTTPRoutes.empty()) return;
HTTPReply reply;
if (!http_msg) {
reply.status = eHTTPStatusCode::BAD_REQUEST;
reply.message = "{\"error\":\"Invalid Request\"}";
} else if (ValidateAuthentication(http_msg)) {
// convert method from cstring to std string
std::string method_string(http_msg->method.buf, http_msg->method.len);
// get method from mg to enum
const eHTTPMethod method = magic_enum::enum_cast<eHTTPMethod>(method_string).value_or(eHTTPMethod::INVALID);
// convert uri from cstring to std string
std::string uri(http_msg->uri.buf, http_msg->uri.len);
std::transform(uri.begin(), uri.end(), uri.begin(), ::tolower);
// convert body from cstring to std string
std::string body(http_msg->body.buf, http_msg->body.len);
// Special case for websocket
if (uri == "/ws" && method == eHTTPMethod::GET) {
mg_ws_upgrade(connection, const_cast<mg_http_message*>(http_msg), NULL);
LOG_DEBUG("Upgraded connection to websocket: %d.%d.%d.%d:%i", MG_IPADDR_PARTS(&connection->rem.ip), connection->rem.port);
// return cause they are now a websocket
return;
}
// Handle HTTP request
const auto routeItr = g_HTTPRoutes.find({method, uri});
if (routeItr != g_HTTPRoutes.end()) {
const auto& [_, route] = *routeItr;
route.handle(reply, body);
} else {
reply.status = eHTTPStatusCode::NOT_FOUND;
reply.message = "{\"error\":\"Not Found\"}";
}
} else {
reply.status = eHTTPStatusCode::UNAUTHORIZED;
reply.message = "{\"error\":\"Unauthorized\"}";
}
mg_http_reply(connection, static_cast<int>(reply.status), jsonContentType, reply.message.c_str());
}
void HandleWSMessage(mg_connection* connection, const mg_ws_message* ws_msg) {
if (!ws_msg) {
LOG_DEBUG("Received invalid websocket message");
return;
} else {
LOG_DEBUG("Received websocket message: %.*s", static_cast<uint32_t>(ws_msg->data.len), ws_msg->data.buf);
auto data = GeneralUtils::TryParse<json>(std::string(ws_msg->data.buf, ws_msg->data.len));
if (data) {
const auto& good_data = data.value();
auto check = JSONUtils::CheckRequiredData(good_data, { "event" });
if (!check.empty()) {
LOG_DEBUG("Received invalid websocket message: %s", check.c_str());
} else {
const auto event = good_data["event"].get<std::string>();
const auto eventItr = g_WSEvents.find(event);
if (eventItr != g_WSEvents.end()) {
const auto& [_, event] = *eventItr;
event.handle(connection, good_data);
} else {
LOG_DEBUG("Received invalid websocket event: %s", event.c_str());
}
}
} else {
LOG_DEBUG("Received invalid websocket message: %.*s", static_cast<uint32_t>(ws_msg->data.len), ws_msg->data.buf);
}
}
}
// Handle websocket connection subscribing to an event
void HandleWSSubscribe(mg_connection* connection, json data) {
auto check = JSONUtils::CheckRequiredData(data, { "subscription" });
if (!check.empty()) {
LOG_DEBUG("Received invalid websocket message: %s", check.c_str());
} else {
const auto subscription = data["subscription"].get<std::string>();
// check subscription vector
auto subItr = std::ranges::find(g_WSSubscriptions, subscription);
if (subItr != g_WSSubscriptions.end()) {
// get index of subscription
auto index = std::distance(g_WSSubscriptions.begin(), subItr);
connection->data[index] = SubscriptionStatus::SUBSCRIBED;
// send subscribe message
mg_ws_send(connection, wsSubscribed.c_str(), wsSubscribed.size(), WEBSOCKET_OP_TEXT);
LOG_DEBUG("subscription %s subscribed", subscription.c_str());
}
}
}
// Handle websocket connection unsubscribing from an event
void HandleWSUnsubscribe(mg_connection* connection, json data) {
auto check = JSONUtils::CheckRequiredData(data, { "subscription" });
if (!check.empty()) {
LOG_DEBUG("Received invalid websocket message: %s", check.c_str());
} else {
const auto subscription = data["subscription"].get<std::string>();
// check subscription vector
auto subItr = std::ranges::find(g_WSSubscriptions, subscription);
if (subItr != g_WSSubscriptions.end()) {
// get index of subscription
auto index = std::distance(g_WSSubscriptions.begin(), subItr);
connection->data[index] = SubscriptionStatus::UNSUBSCRIBED;
// send unsubscribe message
mg_ws_send(connection, wsUnsubscribed.c_str(), wsUnsubscribed.size(), WEBSOCKET_OP_TEXT);
LOG_DEBUG("subscription %s unsubscribed", subscription.c_str());
}
}
}
void HandleWSGetSubscriptions(mg_connection* connection, json data) {
// list subscribed and non subscribed subscriptions
json response;
// check subscription vector
for (const auto& sub : g_WSSubscriptions) {
auto subItr = std::ranges::find(g_WSSubscriptions, sub);
if (subItr != g_WSSubscriptions.end()) {
// get index of subscription
auto index = std::distance(g_WSSubscriptions.begin(), subItr);
if (connection->data[index] == SubscriptionStatus::SUBSCRIBED) {
response["subscribed"].push_back(sub);
} else {
response["unsubscribed"].push_back(sub);
}
}
}
mg_ws_send(connection, response.dump().c_str(), response.dump().size(), WEBSOCKET_OP_TEXT);
}
void HandleMessages(mg_connection* connection, int message, void* message_data) {
if (!Game::web.IsEnabled()) return;
switch (message) {
case MG_EV_HTTP_MSG:
HandleHTTPMessage(connection, static_cast<mg_http_message*>(message_data));
break;
case MG_EV_WS_MSG:
HandleWSMessage(connection, static_cast<mg_ws_message*>(message_data));
break;
default:
break;
}
}
// Redirect mongoose logs to our logger
static void DLOG(char ch, void *param) {
static char buf[256]{};
static size_t len{};
if (ch != '\n') buf[len++] = ch; // we provide the newline in our logger
if (ch == '\n' || len >= sizeof(buf)) {
LOG_DEBUG("%.*s", static_cast<int>(len), buf);
len = 0;
}
}
void Web::RegisterHTTPRoute(HTTPRoute route) {
if (!Game::web.enabled) {
LOG_DEBUG("Failed to register HTTP route %s: web server not enabled", route.path.c_str());
return;
}
auto [_, success] = g_HTTPRoutes.try_emplace({ route.method, route.path }, route);
if (!success) {
LOG_DEBUG("Failed to register HTTP route %s", route.path.c_str());
} else {
LOG_DEBUG("Registered HTTP route %s", route.path.c_str());
}
}
void Web::RegisterWSEvent(WSEvent event) {
if (!Game::web.enabled) {
LOG_DEBUG("Failed to register WS event %s: web server not enabled", event.name.c_str());
return;
}
auto [_, success] = g_WSEvents.try_emplace(event.name, event);
if (!success) {
LOG_DEBUG("Failed to register WS event %s", event.name.c_str());
} else {
LOG_DEBUG("Registered WS event %s", event.name.c_str());
}
}
void Web::RegisterWSSubscription(const std::string& subscription) {
if (!Game::web.enabled) {
LOG_DEBUG("Failed to register WS subscription %s: web server not enabled", subscription.c_str());
return;
}
// check that subsction is not already in the vector
auto subItr = std::ranges::find(g_WSSubscriptions, subscription);
if (subItr != g_WSSubscriptions.end()) {
LOG_DEBUG("Failed to register WS subscription %s: duplicate", subscription.c_str());
} else {
LOG_DEBUG("Registered WS subscription %s", subscription.c_str());
g_WSSubscriptions.push_back(subscription);
}
}
Web::Web() {
mg_log_set_fn(DLOG, NULL); // Redirect logs to our logger
mg_log_set(MG_LL_DEBUG);
mg_mgr_init(&mgr); // Initialize event manager
}
Web::~Web() {
mg_mgr_free(&mgr);
}
bool Web::Startup(const std::string& listen_ip, const uint32_t listen_port) {
// Make listen address
const std::string listen_address = "http://" + listen_ip + ":" + std::to_string(listen_port);
LOG("Starting web server on %s", listen_address.c_str());
// Create HTTP listener
if (!mg_http_listen(&mgr, listen_address.c_str(), HandleMessages, NULL)) {
LOG("Failed to create web server listener on %s", listen_address.c_str());
return false;
}
// Set enabled flag
Game::web.enabled = true;
// Core WebSocket Events
Game::web.RegisterWSEvent({
.name = "subscribe",
.handle = HandleWSSubscribe
});
Game::web.RegisterWSEvent({
.name = "unsubscribe",
.handle = HandleWSUnsubscribe
});
Game::web.RegisterWSEvent({
.name = "getSubscriptions",
.handle = HandleWSGetSubscriptions
});
return true;
}
void Web::ReceiveRequests() {
mg_mgr_poll(&mgr, 15);
}
void Web::SendWSMessage(const std::string subscription, json& data) {
if (!Game::web.enabled) return; // don't attempt to send if web is not enabled
// find subscription
auto subItr = std::ranges::find(g_WSSubscriptions, subscription);
if (subItr == g_WSSubscriptions.end()) {
LOG_DEBUG("Failed to send WS message: subscription %s not found", subscription.c_str());
return;
}
// tell it the event type
data["event"] = subscription;
auto index = std::distance(g_WSSubscriptions.begin(), subItr);
for (auto *wc = Game::web.mgr.conns; wc != NULL; wc = wc->next) {
if (wc->is_websocket && wc->data[index] == SubscriptionStatus::SUBSCRIBED) {
mg_ws_send(wc, data.dump().c_str(), data.dump().size(), WEBSOCKET_OP_TEXT);
}
}
}

82
dWeb/Web.h Normal file
View File

@@ -0,0 +1,82 @@
#ifndef __WEB_H__
#define __WEB_H__
#include <functional>
#include <string>
#include <optional>
#include "mongoose.h"
#include "json_fwd.hpp"
#include "eHTTPStatusCode.h"
// Forward declarations for game namespace
// so that we can access the data anywhere
class Web;
namespace Game {
extern Web web;
}
enum class eHTTPMethod;
// Forward declaration for mongoose manager
typedef struct mg_mgr mg_mgr;
// For passing HTTP messages between functions
struct HTTPReply {
eHTTPStatusCode status = eHTTPStatusCode::NOT_FOUND;
std::string message = "{\"error\":\"Not Found\"}";
};
// HTTP route structure
// This structure is used to register HTTP routes
// with the server. Each route has a path, method, and a handler function
// that will be called when the route is matched.
struct HTTPRoute {
std::string path;
eHTTPMethod method;
std::function<void(HTTPReply&, const std::string&)> handle;
};
// WebSocket event structure
// This structure is used to register WebSocket events
// with the server. Each event has a name and a handler function
// that will be called when the event is triggered.
struct WSEvent {
std::string name;
std::function<void(mg_connection*, nlohmann::json)> handle;
};
// Subscription status for WebSocket clients
enum SubscriptionStatus {
UNSUBSCRIBED = 0,
SUBSCRIBED = 1
};
class Web {
public:
// Constructor
Web();
// Destructor
~Web();
// Handle incoming messages
void ReceiveRequests();
// Start the web server
// Returns true if the server started successfully
bool Startup(const std::string& listen_ip, const uint32_t listen_port);
// Register HTTP route to be handled by the server
void RegisterHTTPRoute(HTTPRoute route);
// Register WebSocket event to be handled by the server
void RegisterWSEvent(WSEvent event);
// Register WebSocket subscription to be handled by the server
void RegisterWSSubscription(const std::string& subscription);
// Returns if the web server is enabled
bool IsEnabled() const { return enabled; };
// Send a message to all connected WebSocket clients that are subscribed to the given topic
void static SendWSMessage(std::string sub, nlohmann::json& message);
private:
// mongoose manager
mg_mgr mgr;
// If the web server is enabled
bool enabled = false;
};
#endif // !__WEB_H__

View File

@@ -3,106 +3,93 @@
#include "CDClientManager.h"
#include "UserManager.h"
#define SOCIAL { lowFrameDelta }
#define SOCIAL_HUB { mediumFrameDelta } //Added to compensate for the large playercounts in NS and NT
#define BATTLE { highFrameDelta }
#define BATTLE_INSTANCE { mediumFrameDelta }
#define RACE { highFrameDelta }
#define PROPERTY { lowFrameDelta }
#define SOCIAL lowFrameDelta
#define SOCIAL_HUB mediumFrameDelta //Added to compensate for the large playercounts in NS and NT
#define BATTLE highFrameDelta
#define BATTLE_INSTANCE mediumFrameDelta
#define RACE highFrameDelta
#define PROPERTY lowFrameDelta
PerformanceProfile PerformanceManager::m_CurrentProfile = SOCIAL;
namespace {
PerformanceProfile m_CurrentProfile = SOCIAL;
PerformanceProfile m_DefaultProfile = SOCIAL;
PerformanceProfile m_InactiveProfile = lowFrameDelta;
std::map<LWOMAPID, PerformanceProfile> m_Profiles = {
// VE
{ 1000, SOCIAL },
PerformanceProfile PerformanceManager::m_DefaultProfile = SOCIAL;
// AG
{ 1100, BATTLE },
{ 1101, BATTLE_INSTANCE },
{ 1102, BATTLE_INSTANCE },
{ 1150, PROPERTY },
{ 1151, PROPERTY },
PerformanceProfile PerformanceManager::m_InactiveProfile = { lowFrameDelta };
// NS
{ 1200, SOCIAL_HUB },
{ 1201, SOCIAL },
{ 1203, RACE },
{ 1204, BATTLE_INSTANCE },
{ 1250, PROPERTY },
{ 1251, PROPERTY },
std::map<LWOMAPID, PerformanceProfile> PerformanceManager::m_Profiles = {
// VE
{ 1000, SOCIAL },
// GF
{ 1300, BATTLE },
{ 1302, BATTLE_INSTANCE },
{ 1303, BATTLE_INSTANCE },
{ 1350, PROPERTY },
// AG
{ 1100, BATTLE },
{ 1101, BATTLE_INSTANCE },
{ 1102, BATTLE_INSTANCE },
{ 1150, PROPERTY },
{ 1151, PROPERTY },
// FV
{ 1400, BATTLE },
{ 1402, BATTLE_INSTANCE },
{ 1403, RACE },
{ 1450, PROPERTY },
// NS
{ 1200, SOCIAL_HUB },
{ 1201, SOCIAL },
{ 1203, RACE },
{ 1204, BATTLE_INSTANCE },
{ 1250, PROPERTY },
{ 1251, PROPERTY },
// LUP
{ 1600, SOCIAL },
{ 1601, SOCIAL },
{ 1602, SOCIAL },
{ 1603, SOCIAL },
{ 1604, SOCIAL },
// GF
{ 1300, BATTLE },
{ 1302, BATTLE_INSTANCE },
{ 1303, BATTLE_INSTANCE },
{ 1350, PROPERTY },
// LEGO Club
{ 1700, SOCIAL },
// FV
{ 1400, BATTLE },
{ 1402, BATTLE_INSTANCE },
{ 1403, RACE },
{ 1450, PROPERTY },
// AM
{ 1800, BATTLE },
// LUP
{ 1600, SOCIAL },
{ 1601, SOCIAL },
{ 1602, SOCIAL },
{ 1603, SOCIAL },
{ 1604, SOCIAL },
// NT
{ 1900, SOCIAL_HUB },
// LEGO Club
{ 1700, SOCIAL },
// AM
{ 1800, BATTLE },
// NT
{ 1900, SOCIAL_HUB },
// NJ
{ 2000, BATTLE },
{ 2001, BATTLE_INSTANCE },
// NJ
{ 2000, BATTLE },
{ 2001, BATTLE_INSTANCE },
};
};
void PerformanceManager::SelectProfile(LWOMAPID mapID) {
// Try to get it from zoneTable
CDZoneTableTable* zoneTable = CDClientManager::GetTable<CDZoneTableTable>();
if (zoneTable) {
const CDZoneTable* zone = zoneTable->Query(mapID);
if (zone) {
if (zone->serverPhysicsFramerate == "high"){
m_CurrentProfile = { highFrameDelta };
return;
}
if (zone->serverPhysicsFramerate == "medium"){
m_CurrentProfile = { mediumFrameDelta };
return;
}
if (zone->serverPhysicsFramerate == "low"){
m_CurrentProfile = { lowFrameDelta };
return;
}
const CDZoneTable* zone = CDZoneTableTable::Query(mapID);
if (zone) {
if (zone->serverPhysicsFramerate == "high") {
m_CurrentProfile = highFrameDelta;
return;
}
if (zone->serverPhysicsFramerate == "medium") {
m_CurrentProfile = mediumFrameDelta;
return;
}
if (zone->serverPhysicsFramerate == "low") {
m_CurrentProfile = lowFrameDelta;
return;
}
}
// Fall back to hardcoded list and defaults
const auto pair = m_Profiles.find(mapID);
if (pair == m_Profiles.end()) {
m_CurrentProfile = m_DefaultProfile;
return;
}
m_CurrentProfile = pair->second;
m_CurrentProfile = pair == m_Profiles.end() ? m_DefaultProfile : pair->second;
}
uint32_t PerformanceManager::GetServerFrameDelta() {
if (UserManager::Instance()->GetUserCount() == 0) {
return m_InactiveProfile.serverFrameDelta;
}
return m_CurrentProfile.serverFrameDelta;
return UserManager::Instance()->GetUserCount() == 0 ? m_InactiveProfile : m_CurrentProfile;
}

View File

@@ -1,22 +1,16 @@
#pragma once
#include <cstdint>
#include <map>
#include "dCommonVars.h"
struct PerformanceProfile {
uint32_t serverFrameDelta;
};
class PerformanceManager {
public:
static void SelectProfile(LWOMAPID mapID);
static uint32_t GetServerFrameDelta();
private:
static PerformanceProfile m_CurrentProfile;
static PerformanceProfile m_DefaultProfile;
static PerformanceProfile m_InactiveProfile;
static std::map<LWOMAPID, PerformanceProfile> m_Profiles;
using PerformanceProfile = uint32_t;
namespace PerformanceManager {
/* Sets a performance profile for a given world. */
void SelectProfile(LWOMAPID mapID);
/* Gets the frame millisecond delta. Will return a higher value if the zone is empty. */
uint32_t GetServerFrameDelta();
};

View File

@@ -98,9 +98,22 @@ namespace Game {
std::string projectVersion = PROJECT_VERSION;
} // namespace Game
bool chatDisabled = false;
bool chatConnected = false;
bool worldShutdownSequenceComplete = false;
namespace {
struct TempSessionInfo {
SystemAddress sysAddr;
std::string hash;
};
std::map<std::string, TempSessionInfo> g_PendingUsers;
uint32_t g_InstanceID = 0;
uint32_t g_CloneID = 0;
std::string g_DatabaseChecksum = "";
bool g_ChatDisabled = false;
bool g_ChatConnected = false;
bool g_WorldShutdownSequenceComplete = false;
}; // namespace anonymous
void WorldShutdownSequence();
void WorldShutdownProcess(uint32_t zoneId);
void FinalizeShutdown();
@@ -110,16 +123,6 @@ void HandlePacketChat(Packet* packet);
void HandleMasterPacket(Packet* packet);
void HandlePacket(Packet* packet);
struct tempSessionInfo {
SystemAddress sysAddr;
std::string hash;
};
std::map<std::string, tempSessionInfo> m_PendingUsers;
uint32_t instanceID = 0;
uint32_t g_CloneID = 0;
std::string databaseChecksum = "";
int main(int argc, char** argv) {
Diagnostics::SetProcessName("World");
Diagnostics::SetProcessFileName(argv[0]);
@@ -137,27 +140,31 @@ int main(int argc, char** argv) {
uint32_t ourPort = 2007;
//Check our arguments:
for (int32_t i = 0; i < argc; ++i) {
for (int32_t i = 0; (i + 1) < argc; i++) {
std::string argument(argv[i]);
const auto valOptional = GeneralUtils::TryParse<uint32_t>(argv[i + 1]);
if (!valOptional) {
continue;
}
if (argument == "-zone") zoneID = atoi(argv[i + 1]);
if (argument == "-instance") instanceID = atoi(argv[i + 1]);
if (argument == "-clone") cloneID = atoi(argv[i + 1]);
if (argument == "-maxclients") maxClients = atoi(argv[i + 1]);
if (argument == "-port") ourPort = atoi(argv[i + 1]);
if (argument == "-zone") zoneID = valOptional.value_or(1000);
else if (argument == "-instance") g_InstanceID = valOptional.value_or(0);
else if (argument == "-clone") cloneID = valOptional.value_or(0);
else if (argument == "-maxclients") maxClients = valOptional.value_or(8);
else if (argument == "-port") ourPort = valOptional.value_or(2007);
}
Game::config = new dConfig("worldconfig.ini");
//Create all the objects we need to run our service:
Server::SetupLogger("WorldServer_" + std::to_string(zoneID) + "_" + std::to_string(instanceID));
Server::SetupLogger("WorldServer_" + std::to_string(zoneID) + "_" + std::to_string(g_InstanceID));
if (!Game::logger) return EXIT_FAILURE;
LOG("Starting World server...");
LOG("Version: %s", Game::projectVersion.c_str());
LOG("Compiled on: %s", __TIMESTAMP__);
if (Game::config->GetValue("disable_chat") == "1") chatDisabled = true;
g_ChatDisabled = Game::config->GetValue("disable_chat") == "1";
try {
std::string clientPathStr = Game::config->GetValue("client_location");
@@ -167,7 +174,7 @@ int main(int argc, char** argv) {
clientPath = BinaryPathFinder::GetBinaryDir() / clientPath;
}
Game::assetManager = new AssetManager(clientPath);
} catch (std::runtime_error& ex) {
} catch (const std::exception& ex) {
LOG("Got an error while setting up assets: %s", ex.what());
return EXIT_FAILURE;
@@ -176,11 +183,14 @@ int main(int argc, char** argv) {
// Connect to CDClient
try {
CDClientDatabase::Connect((BinaryPathFinder::GetBinaryDir() / "resServer" / "CDServer.sqlite").string());
} catch (CppSQLite3Exception& e) {
} catch (const CppSQLite3Exception& e) {
LOG("Unable to connect to CDServer SQLite Database");
LOG("Error: %s", e.errorMessage());
LOG("Error Code: %i", e.errorCode());
return EXIT_FAILURE;
} catch (const std::exception& e) {
LOG("Caught generic exception %s when connecting to CDClient", e.what());
return EXIT_FAILURE;
}
CDClientManager::LoadValuesFromDatabase();
@@ -194,7 +204,7 @@ int main(int argc, char** argv) {
//Connect to the MySQL Database:
try {
Database::Connect();
} catch (std::exception& ex) {
} catch (const std::exception& ex) {
LOG("Got an error while connecting to the database: %s", ex.what());
return EXIT_FAILURE;
}
@@ -203,7 +213,7 @@ int main(int argc, char** argv) {
std::string masterIP = "localhost";
uint32_t masterPort = 1000;
std::string masterPassword;
auto masterInfo = Database::Get()->GetMasterInfo();
const auto masterInfo = Database::Get()->GetMasterInfo();
if (masterInfo) {
masterIP = masterInfo->ip;
@@ -216,13 +226,25 @@ int main(int argc, char** argv) {
const bool dontGenerateDCF = GeneralUtils::TryParse<bool>(Game::config->GetValue("dont_generate_dcf")).value_or(false);
Game::chatFilter = new dChatFilter(Game::assetManager->GetResPath().string() + "/chatplus_en_us", dontGenerateDCF);
Game::server = new dServer(masterIP, ourPort, instanceID, maxClients, false, true, Game::logger, masterIP, masterPort, ServerType::World, Game::config, &Game::lastSignal, masterPassword, zoneID);
Game::server = new dServer(masterIP,
ourPort,
g_InstanceID,
maxClients,
false /* Is internal */,
true /* Use encryption */,
Game::logger,
masterIP,
masterPort,
ServerType::World,
Game::config,
&Game::lastSignal,
masterPassword,
zoneID);
//Connect to the chat server:
uint32_t chatPort = 1501;
if (Game::config->GetValue("chat_server_port") != "") chatPort = std::atoi(Game::config->GetValue("chat_server_port").c_str());
uint32_t chatPort = GeneralUtils::TryParse<uint32_t>(Game::config->GetValue("chat_server_port")).value_or(1501);
auto chatSock = SocketDescriptor(static_cast<uint16_t>(ourPort + 2), 0);
auto chatSock = SocketDescriptor(static_cast<uint16_t>(ourPort + 2), NULL);
Game::chatServer = RakNetworkFactory::GetRakPeerInterface();
Game::chatServer->Startup(1, 30, &chatSock, 1);
Game::chatServer->Connect(masterIP.c_str(), chatPort, NET_PASSWORD_EXTERNAL, strnlen(NET_PASSWORD_EXTERNAL, sizeof(NET_PASSWORD_EXTERNAL)));
@@ -260,9 +282,8 @@ int main(int argc, char** argv) {
//Load our level:
if (zoneID != 0) {
dpWorld::Initialize(zoneID);
Game::zoneManager->Initialize(LWOZONEID(zoneID, instanceID, cloneID));
Game::zoneManager->Initialize(LWOZONEID(zoneID, g_InstanceID, cloneID));
g_CloneID = cloneID;
} else {
Game::entityManager->Initialize();
}
@@ -286,11 +307,11 @@ int main(int argc, char** argv) {
const char* nullTerminateBuffer = "\0";
md5.update(nullTerminateBuffer, 1); // null terminate the data
md5.finalize();
databaseChecksum = md5.hexdigest();
g_DatabaseChecksum = md5.hexdigest();
LOG("FDB Checksum calculated as: %s", databaseChecksum.c_str());
LOG("FDB Checksum calculated as: %s", g_DatabaseChecksum.c_str());
}
if (databaseChecksum.empty()) {
if (g_DatabaseChecksum.empty()) {
LOG("check_fdb is on but no fdb file found.");
return EXIT_FAILURE;
}
@@ -334,7 +355,7 @@ int main(int argc, char** argv) {
float_t ratioBeforeToAfter = static_cast<float>(currentFrameDelta) / static_cast<float>(newFrameDelta);
currentFrameDelta = newFrameDelta;
currentFramerate = MS_TO_FRAMES(newFrameDelta);
LOG_DEBUG("Framerate for zone/instance/clone %i/%i/%i is now %i", zoneID, instanceID, cloneID, currentFramerate);
LOG_DEBUG("Framerate for zone/instance/clone %i/%i/%i is now %i", zoneID, g_InstanceID, cloneID, currentFramerate);
logFlushTime = 15 * currentFramerate; // 15 seconds in frames
framesSinceLastFlush *= ratioBeforeToAfter;
shutdownTimeout = 10 * 60 * currentFramerate; // 10 minutes in frames
@@ -367,7 +388,7 @@ int main(int argc, char** argv) {
} else framesSinceMasterDisconnect = 0;
// Check if we're still connected to chat:
if (!chatConnected) {
if (!g_ChatConnected) {
framesSinceChatDisconnect++;
if (framesSinceChatDisconnect >= chatReconnectionTime) {
@@ -404,16 +425,18 @@ int main(int argc, char** argv) {
//Check for packets here:
packet = Game::server->ReceiveFromMaster();
if (packet) { //We can get messages not handle-able by the dServer class, so handle them if we returned anything.
while (packet) { //We can get messages not handle-able by the dServer class, so handle them if we returned anything.
HandleMasterPacket(packet);
Game::server->DeallocateMasterPacket(packet);
packet = Game::server->ReceiveFromMaster();
}
//Handle our chat packets:
packet = Game::chatServer->Receive();
if (packet) {
while (packet) {
HandlePacketChat(packet);
Game::chatServer->DeallocatePacket(packet);
packet = Game::chatServer->Receive();
}
//Handle world-specific packets:
@@ -421,7 +444,6 @@ int main(int argc, char** argv) {
UserManager::Instance()->DeletePendingRemovals();
auto t1 = std::chrono::high_resolution_clock::now();
for (uint32_t curPacket = 0; curPacket < maxPacketsToProcess && timeSpent < maxPacketProcessingTime; curPacket++) {
packet = Game::server->Receive();
if (packet) {
@@ -497,20 +519,14 @@ int main(int argc, char** argv) {
Metrics::EndMeasurement(MetricVariable::Sleep);
if (!ready && Game::server->GetIsConnectedToMaster()) {
// Some delay is required here or else we crash the client?
LOG("Finished loading world with zone (%i), ready up!", Game::server->GetZoneID());
framesSinceMasterStatus++;
MasterPackets::SendWorldReady(Game::server, Game::server->GetZoneID(), Game::server->GetInstanceID());
if (framesSinceMasterStatus >= 200) {
LOG("Finished loading world with zone (%i), ready up!", Game::server->GetZoneID());
MasterPackets::SendWorldReady(Game::server, Game::server->GetZoneID(), Game::server->GetInstanceID());
ready = true;
}
ready = true;
}
if (Game::ShouldShutdown() && !worldShutdownSequenceComplete) {
if (Game::ShouldShutdown() && !g_WorldShutdownSequenceComplete) {
WorldShutdownProcess(zoneID);
break;
}
@@ -527,14 +543,14 @@ void HandlePacketChat(Packet* packet) {
if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION || packet->data[0] == ID_CONNECTION_LOST) {
LOG("Lost our connection to chat, zone(%i), instance(%i)", Game::server->GetZoneID(), Game::server->GetInstanceID());
chatConnected = false;
g_ChatConnected = false;
}
if (packet->data[0] == ID_CONNECTION_REQUEST_ACCEPTED) {
LOG("Established connection to chat, zone(%i), instance (%i)", Game::server->GetZoneID(), Game::server->GetInstanceID());
Game::chatSysAddr = packet->systemAddress;
chatConnected = true;
g_ChatConnected = true;
}
if (packet->data[0] == ID_USER_PACKET_ENUM && packet->length >= 4) {
@@ -634,13 +650,13 @@ void HandlePacketChat(Packet* packet) {
inStream.Read(lootOption);
inStream.Read(memberCount);
LOG("Updating team (%llu), (%i), (%i)", teamID, lootOption, memberCount);
LOG("Updating team ID:(%llu), Loot:(%i), #Members:(%i)", teamID, lootOption, memberCount);
for (char i = 0; i < memberCount; i++) {
LWOOBJID member = LWOOBJID_EMPTY;
inStream.Read(member);
members.push_back(member);
LOG("Updating team member (%llu)", member);
LOG("Added member (%llu) to the team", member);
}
TeamManager::Instance()->UpdateTeam(teamID, lootOption, members);
@@ -648,7 +664,7 @@ void HandlePacketChat(Packet* packet) {
break;
}
default:
LOG("Received an unknown chat: %i", int(packet->data[3]));
LOG("Received an unknown chat: %s", StringifiedEnum::ToString(static_cast<MessageType::Chat>(packet->data[3])).data());
}
}
}
@@ -677,8 +693,8 @@ void HandleMasterPacket(Packet* packet) {
inStream.Read(username);
//Find them:
auto it = m_PendingUsers.find(username.GetAsString());
if (it == m_PendingUsers.end()) return;
auto it = g_PendingUsers.find(username.GetAsString());
if (it == g_PendingUsers.end()) return;
//Convert our key:
std::string userHash = std::to_string(sessionKey);
@@ -718,14 +734,14 @@ void HandleMasterPacket(Packet* packet) {
UserManager::Instance()->RequestCharacterList(it->second.sysAddr);
}
m_PendingUsers.erase(username.GetAsString());
g_PendingUsers.erase(username.GetAsString());
//Notify master:
{
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, MessageType::Master::PLAYER_ADDED);
bitStream.Write<LWOMAPID>(Game::server->GetZoneID());
bitStream.Write<LWOINSTANCEID>(instanceID);
bitStream.Write<LWOINSTANCEID>(g_InstanceID);
Game::server->SendToMaster(bitStream);
}
}
@@ -828,7 +844,7 @@ void HandlePacket(Packet* packet) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, MessageType::Master::PLAYER_REMOVED);
bitStream.Write<LWOMAPID>(Game::server->GetZoneID());
bitStream.Write<LWOINSTANCEID>(instanceID);
bitStream.Write<LWOINSTANCEID>(g_InstanceID);
Game::server->SendToMaster(bitStream);
}
@@ -859,7 +875,7 @@ void HandlePacket(Packet* packet) {
inStream.Read(clientDatabaseChecksum);
// If the check is turned on, validate the client's database checksum.
if (Game::config->GetValue("check_fdb") == "1" && !databaseChecksum.empty()) {
if (Game::config->GetValue("check_fdb") == "1" && !g_DatabaseChecksum.empty()) {
auto accountInfo = Database::Get()->GetAccountInfo(username.GetAsString());
if (!accountInfo) {
LOG("Client's account does not exist in the database, aborting connection.");
@@ -868,7 +884,7 @@ void HandlePacket(Packet* packet) {
}
// Developers may skip this check
if (clientDatabaseChecksum.string != databaseChecksum) {
if (clientDatabaseChecksum.string != g_DatabaseChecksum) {
if (accountInfo->maxGmLevel < eGameMasterLevel::DEVELOPER) {
LOG("Client's database checksum does not match the server's, aborting connection.");
@@ -900,10 +916,10 @@ void HandlePacket(Packet* packet) {
Game::server->SendToMaster(bitStream);
//Insert info into our pending list
tempSessionInfo info;
info.sysAddr = SystemAddress(packet->systemAddress);
TempSessionInfo info;
info.sysAddr = packet->systemAddress;
info.hash = sessionKey.GetAsString();
m_PendingUsers.insert(std::make_pair(username.GetAsString(), info));
g_PendingUsers[username.GetAsString()] = info;
break;
}
@@ -918,8 +934,8 @@ void HandlePacket(Packet* packet) {
//This loops prevents users who aren't authenticated to double-request the char list, which
//would make the login screen freeze sometimes.
if (m_PendingUsers.size() > 0) {
for (auto it : m_PendingUsers) {
if (g_PendingUsers.size() > 0) {
for (const auto& it : g_PendingUsers) {
if (it.second.sysAddr == packet->systemAddress) {
return;
}
@@ -1029,7 +1045,7 @@ void HandlePacket(Packet* packet) {
const auto respawnPoint = player->GetCharacter()->GetRespawnPoint(Game::zoneManager->GetZone()->GetWorldID());
Game::entityManager->ConstructEntity(player, UNASSIGNED_SYSTEM_ADDRESS, true);
Game::entityManager->ConstructEntity(player, UNASSIGNED_SYSTEM_ADDRESS);
if (respawnPoint != NiPoint3Constant::ZERO) {
GameMessages::SendPlayerReachedRespawnCheckpoint(player, respawnPoint, NiQuaternionConstant::IDENTITY);
@@ -1042,25 +1058,29 @@ void HandlePacket(Packet* packet) {
// Do charxml fixes here
auto* levelComponent = player->GetComponent<LevelProgressionComponent>();
auto* const inventoryComponent = player->GetComponent<InventoryComponent>();
const auto* const missionComponent = player->GetComponent<MissionComponent>();
auto* const missionComponent = player->GetComponent<MissionComponent>();
if (!levelComponent || !missionComponent || !inventoryComponent) return;
auto version = levelComponent->GetCharacterVersion();
switch (version) {
case eCharacterVersion::RELEASE:
// TODO: Implement, super low priority
[[fallthrough]];
case eCharacterVersion::LIVE:
LOG("Updating Character Flags");
c->SetRetroactiveFlags();
levelComponent->SetCharacterVersion(eCharacterVersion::PLAYER_FACTION_FLAGS);
[[fallthrough]];
case eCharacterVersion::PLAYER_FACTION_FLAGS:
LOG("Updating Vault Size");
player->RetroactiveVaultSize();
levelComponent->SetCharacterVersion(eCharacterVersion::VAULT_SIZE);
[[fallthrough]];
case eCharacterVersion::VAULT_SIZE:
LOG("Updaing Speedbase");
levelComponent->SetRetroactiveBaseSpeed();
levelComponent->SetCharacterVersion(eCharacterVersion::SPEED_BASE);
[[fallthrough]];
case eCharacterVersion::SPEED_BASE: {
LOG("Removing lots from NJ Jay missions bugged at foss");
// https://explorer.lu/missions/1789
@@ -1075,7 +1095,23 @@ void HandlePacket(Packet* packet) {
inventoryComponent->RemoveItem(14493, 1, eInventoryType::ITEMS);
inventoryComponent->RemoveItem(14493, 1, eInventoryType::VAULT_ITEMS);
}
levelComponent->SetCharacterVersion(eCharacterVersion::NJ_JAYMISSIONS);
[[fallthrough]];
}
case eCharacterVersion::NJ_JAYMISSIONS: {
LOG("Fixing Nexus Force Explorer missions");
auto missions = { 502 /* Pet Cove */, 593/* Nimbus Station */, 938/* Avant Gardens */, 284/* Gnarled Forest */, 754/* Forbidden Valley */ };
bool complete = true;
for (auto missionID : missions) {
auto* mission = missionComponent->GetMission(missionID);
if (!mission || !mission->IsComplete()) {
complete = false;
}
}
if (complete) missionComponent->CompleteMission(937 /* Nexus Force explorer */);
levelComponent->SetCharacterVersion(eCharacterVersion::UP_TO_DATE);
[[fallthrough]];
}
case eCharacterVersion::UP_TO_DATE:
break;
@@ -1118,6 +1154,8 @@ void HandlePacket(Packet* packet) {
GeneralUtils::SetBit(blueprintID, eObjectBits::CHARACTER);
GeneralUtils::SetBit(blueprintID, eObjectBits::PERSISTENT);
// Workaround for not having a UGC server to get model LXFML onto the client so it
// can generate the physics and nif for the object.
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CLIENT, MessageType::Client::BLUEPRINT_SAVE_RESPONSE);
bitStream.Write<LWOOBJID>(LWOOBJID_EMPTY); //always zero so that a check on the client passes
@@ -1289,7 +1327,7 @@ void HandlePacket(Packet* packet) {
}
}
std::vector<std::pair<uint8_t, uint8_t>> segments = Game::chatFilter->IsSentenceOkay(request.message, entity->GetGMLevel(), !(isBestFriend && request.chatLevel == 1));
const auto segments = Game::chatFilter->IsSentenceOkay(request.message, entity->GetGMLevel(), !(isBestFriend && request.chatLevel == 1));
bool bAllClean = segments.empty();
@@ -1303,7 +1341,7 @@ void HandlePacket(Packet* packet) {
}
case MessageType::World::GENERAL_CHAT_MESSAGE: {
if (chatDisabled) {
if (g_ChatDisabled) {
ChatPackets::SendMessageFail(packet->systemAddress);
} else {
auto chatMessage = ClientPackets::HandleChatMessage(packet);
@@ -1396,7 +1434,7 @@ void HandlePacket(Packet* packet) {
}
void WorldShutdownProcess(uint32_t zoneId) {
LOG("Saving map %i instance %i", zoneId, instanceID);
LOG("Saving map %i instance %i", zoneId, g_InstanceID);
for (auto i = 0; i < Game::server->GetReplicaManager()->GetParticipantCount(); ++i) {
const auto& player = Game::server->GetReplicaManager()->GetParticipantAtIndex(i);
@@ -1417,11 +1455,10 @@ void WorldShutdownProcess(uint32_t zoneId) {
if (PropertyManagementComponent::Instance() != nullptr) {
LOG("Saving ALL property data for zone %i clone %i!", zoneId, PropertyManagementComponent::Instance()->GetCloneId());
PropertyManagementComponent::Instance()->Save();
Database::Get()->RemoveUnreferencedUgcModels();
LOG("ALL property data saved for zone %i clone %i!", zoneId, PropertyManagementComponent::Instance()->GetCloneId());
}
LOG("ALL DATA HAS BEEN SAVED FOR ZONE %i INSTANCE %i!", zoneId, instanceID);
LOG("ALL DATA HAS BEEN SAVED FOR ZONE %i INSTANCE %i!", zoneId, g_InstanceID);
while (Game::server->GetReplicaManager()->GetParticipantCount() > 0) {
const auto& player = Game::server->GetReplicaManager()->GetParticipantAtIndex(0);
@@ -1432,7 +1469,7 @@ void WorldShutdownProcess(uint32_t zoneId) {
}
void WorldShutdownSequence() {
bool shouldShutdown = Game::ShouldShutdown() || worldShutdownSequenceComplete;
bool shouldShutdown = Game::ShouldShutdown() || g_WorldShutdownSequenceComplete;
Game::lastSignal = -1;
#ifndef DARKFLAME_PLATFORM_WIN32
if (shouldShutdown)
@@ -1443,13 +1480,13 @@ void WorldShutdownSequence() {
if (!Game::logger) return;
LOG("Zone (%i) instance (%i) shutting down outside of main loop!", Game::server->GetZoneID(), instanceID);
LOG("Zone (%i) instance (%i) shutting down outside of main loop!", Game::server->GetZoneID(), g_InstanceID);
WorldShutdownProcess(Game::server->GetZoneID());
FinalizeShutdown();
}
void FinalizeShutdown() {
LOG("Shutdown complete, zone (%i), instance (%i)", Game::server->GetZoneID(), instanceID);
LOG("Shutdown complete, zone (%i), instance (%i)", Game::server->GetZoneID(), g_InstanceID);
//Delete our objects here:
Metrics::Clear();
@@ -1468,7 +1505,7 @@ void FinalizeShutdown() {
if (Game::logger) delete Game::logger;
Game::logger = nullptr;
worldShutdownSequenceComplete = true;
g_WorldShutdownSequenceComplete = true;
exit(EXIT_SUCCESS);
}

Some files were not shown because too many files have changed in this diff Show More