Skip to content

Commit

Permalink
Merge pull request #928 from openmultiplayer/amir/server-logo
Browse files Browse the repository at this point in the history
Add server logo feature, human readable httplib errors, and bunch of fixes
  • Loading branch information
AmyrAhmady authored Jun 29, 2024
2 parents f5cb8c4 + c98a08e commit 568a108
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
url = https://github.com/openmultiplayer/cmake-conan.git
[submodule "lib/cpp-httplib"]
path = lib/cpp-httplib
url = https://github.com/ksenonadv/cpp-httplib.git
url = https://github.com/openmultiplayer/cpp-httplib.git
30 changes: 22 additions & 8 deletions Server/Components/Dialogs/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,41 @@ class DialogsComponent final : public IDialogsComponent, public PlayerConnectEve
// If the dialog id doesn't match what the server is expecting, ignore it
PlayerDialogData* data = queryExtension<PlayerDialogData>(peer);
if (!data || data->getActiveID() == INVALID_DIALOG_ID || data->getActiveID() != sendDialogResponse.ID)
{
return false;
}

if(sendDialogResponse.Response < 0 || sendDialogResponse.Response > 1)
if (sendDialogResponse.Response < 0 || sendDialogResponse.Response > 1)
{
return false;
}

if((data->style_ == DialogStyle_PASSWORD || data->style_ == DialogStyle_INPUT || data->style_ == DialogStyle_MSGBOX) && sendDialogResponse.ListItem != -1)
if ((data->style_ == DialogStyle_PASSWORD || data->style_ == DialogStyle_INPUT || data->style_ == DialogStyle_MSGBOX) && sendDialogResponse.ListItem != -1)
{
return false;
}

if((data->style_ == DialogStyle_LIST || data->style_ == DialogStyle_TABLIST || data->style_ == DialogStyle_TABLIST_HEADERS) && data->body_.length() > 0)
if ((data->style_ == DialogStyle_LIST || data->style_ == DialogStyle_TABLIST || data->style_ == DialogStyle_TABLIST_HEADERS) && data->body_.length() > 0)
{
unsigned int lines = 0;

for(unsigned int i = 0 ; i < data->body_.length() - 1; i++)
if(data->body_[i] == '\n')
for (unsigned int i = 0; i < data->body_.length() - 1; i++)
{
if (data->body_[i] == '\n')
{
lines++;

if(data->style_ == DialogStyle_TABLIST_HEADERS && lines > 0)
}
}

if (data->style_ == DialogStyle_TABLIST_HEADERS && lines > 0)
{
lines--;
}

if(sendDialogResponse.ListItem < 0 || sendDialogResponse.ListItem > lines)
if (sendDialogResponse.ListItem < 0 || sendDialogResponse.ListItem > lines)
{
return false;
}
}

data->activeId = INVALID_DIALOG_ID;
Expand Down
13 changes: 9 additions & 4 deletions Server/Components/LegacyNetwork/Query/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const size_t MAX_ACCEPTABLE_HOSTNAME_SIZE = 63;
const size_t MAX_ACCEPTABLE_LANGUAGE_SIZE = 39;
const size_t MAX_ACCEPTABLE_GMTEXT_SIZE = 39;
const size_t MAX_ACCEPTABLE_DISCORD_LINK_SIZE = 50;
const size_t MAX_ACCEPTABLE_BANNER_URL_SIZE = 120;
const size_t MAX_ACCEPTABLE_IMAGE_URL_SIZE = 160;

template <typename T>
void writeToBuffer(char* output, size_t& offset, T value)
Expand Down Expand Up @@ -133,10 +133,11 @@ void Query::buildExtraServerInfoBuffer()

// Set discord link length to 0 if it's over acceptable length (max size defined by discord itself)
uint32_t discordLinkLength = MAX_ACCEPTABLE_DISCORD_LINK_SIZE < discordLink.length() ? 0 : discordLink.length();
uint32_t lightBannerUrlLength = std::min(lightBannerUrl.length(), MAX_ACCEPTABLE_BANNER_URL_SIZE);
uint32_t darkBannerUrlLength = std::min(darkBannerUrl.length(), MAX_ACCEPTABLE_BANNER_URL_SIZE);
uint32_t lightBannerUrlLength = std::min(lightBannerUrl.length(), MAX_ACCEPTABLE_IMAGE_URL_SIZE);
uint32_t darkBannerUrlLength = std::min(darkBannerUrl.length(), MAX_ACCEPTABLE_IMAGE_URL_SIZE);
uint32_t logoUrlLength = std::min(logoUrl.length(), MAX_ACCEPTABLE_IMAGE_URL_SIZE);

extraInfoBufferLength = BASE_QUERY_SIZE + sizeof(discordLinkLength) + discordLinkLength + sizeof(lightBannerUrlLength) + lightBannerUrlLength + sizeof(darkBannerUrlLength) + darkBannerUrlLength;
extraInfoBufferLength = BASE_QUERY_SIZE + sizeof(discordLinkLength) + discordLinkLength + sizeof(lightBannerUrlLength) + lightBannerUrlLength + sizeof(darkBannerUrlLength) + darkBannerUrlLength + sizeof(logoUrlLength) + logoUrlLength;
extraInfoBuffer.reset(new char[extraInfoBufferLength]);

size_t offset = QUERY_TYPE_INDEX;
Expand All @@ -155,6 +156,10 @@ void Query::buildExtraServerInfoBuffer()
// Write dark banner url
writeToBuffer(output, offset, darkBannerUrlLength);
writeToBuffer(output, darkBannerUrl.c_str(), offset, darkBannerUrlLength);

// Write logo url
writeToBuffer(output, offset, logoUrlLength);
writeToBuffer(output, logoUrl.c_str(), offset, logoUrlLength);
}

void Query::updateServerInfoBufferPlayerCount(IPlayer* except)
Expand Down
6 changes: 6 additions & 0 deletions Server/Components/LegacyNetwork/Query/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ class Query : NoCopy
darkBannerUrl = String(value);
}

void setLogoUrl(StringView value)
{
logoUrl = String(value);
}

private:
ICore* core = nullptr;
IConsoleComponent* console = nullptr;
Expand All @@ -170,6 +175,7 @@ class Query : NoCopy
String discordLink = "";
String lightBannerUrl = "";
String darkBannerUrl = "";
String logoUrl = "";
bool passworded = false;
bool logQueries = false;
bool rconEnabled = false;
Expand Down
6 changes: 6 additions & 0 deletions Server/Components/LegacyNetwork/legacy_network_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,12 @@ void RakNetLegacyNetwork::update()
query.setDarkBannerUrl(bannerUrl);
}

StringView logoUrl = config.getString("logo");
if (!logoUrl.empty())
{
query.setLogoUrl(logoUrl);
}

query.setRuleValue<false>("worldtime", String(std::to_string(*config.getInt("game.time")) + ":00"));

StringView rconPassword = config.getString("rcon.password");
Expand Down
15 changes: 4 additions & 11 deletions Server/Components/LegacyNetwork/legacy_network_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,17 +398,10 @@ struct AnnounceHTTPResponseHandler final : HTTPResponseHandler
{
if (status != 200)
{
core->printLn("Couldn't announce legacy network to open.mp list.");
if (status < 100)
{
core->printLn("\t[HTTP Client Error] Status: %d", status);
}
else
{
core->printLn("\t[Server Error] Status: %d", status);
core->printLn("\t[Server Error] Message: %.*s", PRINT_VIEW(body));
}
core->printLn("This won't affect the server's behaviour.");
core->logLn(LogLevel::Warning, "Couldn't announce legacy network to open.mp list.");
core->logLn(LogLevel::Warning, "\t Status: %d", status);
core->logLn(LogLevel::Warning, "\t Message: %.*s", PRINT_VIEW(body));
core->logLn(LogLevel::Warning, "This won't affect the server's behaviour.");
}
delete this;
}
Expand Down
4 changes: 2 additions & 2 deletions Server/Components/Menus/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class MenusComponent final : public IMenusComponent, public MenuEventHandler, pu
data->setMenuID(INVALID_MENU_ID);
return false;
}
if(onPlayerSelectedMenuRow.MenuRow >= menuData->getRowCount(0))

if (onPlayerSelectedMenuRow.MenuRow >= menuData->getRowCount(0))
{
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions Server/Source/core_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ static const std::map<String, ConfigStorage> Defaults {
// banners
{ "banners.light", String("") },
{ "banners.dark", String("") },
{ "logo", String("") },
// discord
{ "discord.invite", String("") },
};
Expand Down Expand Up @@ -941,6 +942,10 @@ class HTTPAsyncIO
else
{
params->response = int(res.error());
if (params->response < 100)
{
params->body = httplib::detail::internal_error_to_string(res.error());
}
}

params->finished.store(true);
Expand Down
2 changes: 1 addition & 1 deletion lib/cpp-httplib
Submodule cpp-httplib updated 1 files
+23 −0 httplib.h

0 comments on commit 568a108

Please sign in to comment.