diff --git a/Minecraft.Client/CMakeLists.txt b/Minecraft.Client/CMakeLists.txt index 4a86df1e..2ba48ae2 100644 --- a/Minecraft.Client/CMakeLists.txt +++ b/Minecraft.Client/CMakeLists.txt @@ -88,6 +88,7 @@ target_link_libraries(Minecraft.Client PRIVATE XInput9_1_0 wsock32 ws2_32 + dnsapi dbghelp legacy_stdio_definitions 4JLibs.${PLATFORM_NAME}.Input diff --git a/Minecraft.Client/Common/Network/SessionInfo.h b/Minecraft.Client/Common/Network/SessionInfo.h index d8ba33da..1a35f574 100644 --- a/Minecraft.Client/Common/Network/SessionInfo.h +++ b/Minecraft.Client/Common/Network/SessionInfo.h @@ -114,6 +114,7 @@ public: GameSessionData data; bool hasPartyMember; bool isDedicatedSession; + std::wstring description; bool hasServerInfo; std::vector serverPlayerNames; #ifdef _WINDOWS64 diff --git a/Minecraft.Client/Common/UI/UIScene_JoinMenu.cpp b/Minecraft.Client/Common/UI/UIScene_JoinMenu.cpp index 361351da..359879a0 100644 --- a/Minecraft.Client/Common/UI/UIScene_JoinMenu.cpp +++ b/Minecraft.Client/Common/UI/UIScene_JoinMenu.cpp @@ -36,50 +36,6 @@ UIScene_JoinMenu::UIScene_JoinMenu(int iPad, void *_initData, UILayer *parentLay // reset the howtoplay state s_textInjected = false; s_injectionDelay = 0; - - // load howtoplay it's like the messagebox xd - if (!s_movieServerDesc) - { - wstring moviePath = L"HowToPlay"; - if (m_loadedResolution == eSceneResolution_1080) moviePath.append(L"1080.swf"); - else if (m_loadedResolution == eSceneResolution_720) moviePath.append(L"720.swf"); - else moviePath.append(L"480.swf"); - - byteArray baFile = ui.getMovieData(moviePath.c_str()); - if (baFile.data) - { - s_movieDataServerDesc.assign((unsigned char*)baFile.data, (unsigned char*)baFile.data + baFile.length); - s_movieServerDesc = IggyPlayerCreateFromMemory(s_movieDataServerDesc.data(), (unsigned int)s_movieDataServerDesc.size(), nullptr); - if (s_movieServerDesc) - { - IggyPlayerInitializeAndTickRS(s_movieServerDesc); - IggyPlayerSetDisplaySize(s_movieServerDesc, m_movieWidth, m_movieHeight); - s_funcLoadPage = IggyPlayerCreateFastName(s_movieServerDesc, (IggyUTF16 *)L"LoadHowToPlayPage", -1); - - // it maintains the resolution at 1080p so that it looks sharp - IggyValuePath *root = IggyPlayerRootPath(s_movieServerDesc); - if (root) - { - IggyValueSetF64RS(root, IggyPlayerCreateFastName(s_movieServerDesc, (IggyUTF16 *)L"x", -1), nullptr, 0.0); - IggyValueSetF64RS(root, IggyPlayerCreateFastName(s_movieServerDesc, (IggyUTF16 *)L"y", -1), nullptr, 0.0); - IggyValueSetF64RS(root, IggyPlayerCreateFastName(s_movieServerDesc, (IggyUTF16 *)L"width", -1), nullptr, (double)m_movieWidth); - IggyValueSetF64RS(root, IggyPlayerCreateFastName(s_movieServerDesc, (IggyUTF16 *)L"height", -1), nullptr, (double)m_movieHeight); - - // it hides the logo and other things that the howtoplay has - const char* elementsToHide[] = { "__id0_", "__id1_", "__id2_" }; - IggyName visibleName = IggyPlayerCreateFastName(s_movieServerDesc, (IggyUTF16 *)L"visible", -1); - for (int i = 0; i < 3; i++) { - IggyValuePath path; - if (IggyValuePathMakeNameRef(&path, root, elementsToHide[i])) { - IggyValueSetBooleanRS(&path, visibleName, nullptr, false); - } - } - - updateServerDescription(); - } - } - } - } #endif } @@ -315,6 +271,8 @@ void UIScene_JoinMenu::tick() updateServerDescription(); } + } else if (!m_selectedSession->description.empty()) { + updateServerDescription(); } #endif @@ -322,6 +280,50 @@ void UIScene_JoinMenu::tick() } void UIScene_JoinMenu::updateServerDescription() { + if (m_selectedSession->description.empty()) return; + + // load howtoplay it's like the messagebox xd + if (!s_movieServerDesc) + { + wstring moviePath = L"HowToPlay"; + if (m_loadedResolution == eSceneResolution_1080) moviePath.append(L"1080.swf"); + else if (m_loadedResolution == eSceneResolution_720) moviePath.append(L"720.swf"); + else moviePath.append(L"480.swf"); + + byteArray baFile = ui.getMovieData(moviePath.c_str()); + if (baFile.data) + { + s_movieDataServerDesc.assign((unsigned char*)baFile.data, (unsigned char*)baFile.data + baFile.length); + s_movieServerDesc = IggyPlayerCreateFromMemory(s_movieDataServerDesc.data(), (unsigned int)s_movieDataServerDesc.size(), nullptr); + if (s_movieServerDesc) + { + IggyPlayerInitializeAndTickRS(s_movieServerDesc); + IggyPlayerSetDisplaySize(s_movieServerDesc, m_movieWidth, m_movieHeight); + s_funcLoadPage = IggyPlayerCreateFastName(s_movieServerDesc, (IggyUTF16*)L"LoadHowToPlayPage", -1); + + // it maintains the resolution at 1080p so that it looks sharp + IggyValuePath* root = IggyPlayerRootPath(s_movieServerDesc); + if (root) + { + IggyValueSetF64RS(root, IggyPlayerCreateFastName(s_movieServerDesc, (IggyUTF16*)L"x", -1), nullptr, 0.0); + IggyValueSetF64RS(root, IggyPlayerCreateFastName(s_movieServerDesc, (IggyUTF16*)L"y", -1), nullptr, 0.0); + IggyValueSetF64RS(root, IggyPlayerCreateFastName(s_movieServerDesc, (IggyUTF16*)L"width", -1), nullptr, (double)m_movieWidth); + IggyValueSetF64RS(root, IggyPlayerCreateFastName(s_movieServerDesc, (IggyUTF16*)L"height", -1), nullptr, (double)m_movieHeight); + + // it hides the logo and other things that the howtoplay has + const char* elementsToHide[] = { "__id0_", "__id1_", "__id2_" }; + IggyName visibleName = IggyPlayerCreateFastName(s_movieServerDesc, (IggyUTF16*)L"visible", -1); + for (int i = 0; i < 3; i++) { + IggyValuePath path; + if (IggyValuePathMakeNameRef(&path, root, elementsToHide[i])) { + IggyValueSetBooleanRS(&path, visibleName, nullptr, false); + } + } + } + } + } + } + IggyValuePath* root = IggyPlayerRootPath(s_movieServerDesc); if (root) { @@ -359,9 +361,8 @@ void UIScene_JoinMenu::updateServerDescription() { args[0].type = IGGY_DATATYPE_number; args[0].number = 0; // 0 is the what's new page on howtoplay don't change it - wstring testText = L"\nNothing yet..."; IggyStringUTF16 iggyStr; - wstring formattedText = app.EscapeHTMLString(testText); + wstring formattedText = app.EscapeHTMLString(m_selectedSession->description); formattedText = app.FormatColoredString(formattedText); iggyStr.string = (IggyUTF16*)formattedText.c_str(); iggyStr.length = (unsigned int)formattedText.length(); diff --git a/Minecraft.Client/Windows64/Network/PlatformNetworkManagerWindows.cpp b/Minecraft.Client/Windows64/Network/PlatformNetworkManagerWindows.cpp index 031db68d..356ededb 100644 --- a/Minecraft.Client/Windows64/Network/PlatformNetworkManagerWindows.cpp +++ b/Minecraft.Client/Windows64/Network/PlatformNetworkManagerWindows.cpp @@ -5,7 +5,9 @@ #include #include #include +#include #include +#include #include "PlatformNetworkManagerWindows.h" #include "WindowsNetworkConfig.h" @@ -26,6 +28,7 @@ char g_Win64MultiplayerIP[256] = "127.0.0.1"; bool g_Win64DedicatedServer = false; int g_Win64DedicatedServerPort = WIN64_NET_DEFAULT_PORT; char g_Win64DedicatedServerBindIP[256] = ""; +char g_Win64DedicatedServerDescription[1024] = ""; bool g_Win64DedicatedServerLanAdvertise = true; namespace { @@ -82,19 +85,101 @@ namespace { return true; } + enum SrvLookupResult { + SRV_LOOKUP_NOT_FOUND, + SRV_LOOKUP_RESOLVED, + SRV_LOOKUP_UNAVAILABLE + }; + + struct SrvEndpoint { + std::string host; + unsigned short port; + WORD weight; + }; + + SrvLookupResult ResolveMinecraftSrv(const char* host, std::string* targetHost, unsigned short* targetPort) { + in_addr numericAddress = {}; + if (host == nullptr || targetHost == nullptr || targetPort == nullptr || inet_pton(AF_INET, host, &numericAddress) == 1) + return SRV_LOOKUP_NOT_FOUND; + + const std::string queryName = std::string("_minecraft._tcp.") + host; + PDNS_RECORD queryRecords = nullptr; + DNS_STATUS status = DnsQuery_A(queryName.c_str(), DNS_TYPE_SRV, DNS_QUERY_STANDARD, nullptr, &queryRecords, nullptr); + if (status != ERROR_SUCCESS || queryRecords == nullptr) { + if (queryRecords != nullptr) DnsRecordListFree(queryRecords, DnsFreeRecordList); + return SRV_LOOKUP_NOT_FOUND; + } + + DNS_RECORDA* records = reinterpret_cast(queryRecords); + std::vector endpoints; + bool serviceUnavailable = false; + WORD bestPriority = 0xFFFF; + for (DNS_RECORDA* record = records; record != nullptr; record = record->pNext) { + if (record->wType != DNS_TYPE_SRV) continue; + + const DNS_SRV_DATAA& srv = record->Data.SRV; + if (srv.pNameTarget == nullptr) continue; + if (srv.wPriority < bestPriority) { + bestPriority = srv.wPriority; + endpoints.clear(); + serviceUnavailable = false; + } + if (srv.wPriority > bestPriority) continue; + + if (strcmp(srv.pNameTarget, ".") == 0) { + serviceUnavailable = true; + endpoints.clear(); + continue; + } + if (srv.wPort == 0 || serviceUnavailable) continue; + endpoints.push_back({ srv.pNameTarget, srv.wPort, srv.wWeight }); + } + DnsRecordListFree(queryRecords, DnsFreeRecordList); + + if (endpoints.empty()) return serviceUnavailable ? SRV_LOOKUP_UNAVAILABLE : SRV_LOOKUP_NOT_FOUND; + + unsigned int totalWeight = 0; + for (size_t index = 0; index < endpoints.size(); ++index) totalWeight += endpoints[index].weight; + + size_t selectedIndex = 0; + if (totalWeight == 0) { + selectedIndex = static_cast(GetTickCount64() % endpoints.size()); + } else { + unsigned int choice = static_cast(GetTickCount64() % totalWeight); + for (size_t index = 0; index < endpoints.size(); ++index) { + if (choice < endpoints[index].weight) { + selectedIndex = index; + break; + } + choice -= endpoints[index].weight; + } + } + + *targetHost = endpoints[selectedIndex].host; + *targetPort = endpoints[selectedIndex].port; + return SRV_LOOKUP_RESOLVED; + } + bool ResolveIPv4(const char* host, unsigned short port, sockaddr_in* address) { if (host == nullptr || host[0] == '\0' || address == nullptr || port == 0) return false; + std::string resolvedHost = host; + unsigned short resolvedPort = port; + if (port == WINDOWS_NETWORK_PORT) { + SrvLookupResult srvResult = ResolveMinecraftSrv(host, &resolvedHost, &resolvedPort); + if (srvResult == SRV_LOOKUP_UNAVAILABLE) return false; + } + addrinfo hints = {}; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; char portText[6] = {}; - sprintf_s(portText, "%u", static_cast(port)); + sprintf_s(portText, "%u", static_cast(resolvedPort)); addrinfo* results = nullptr; - if (getaddrinfo(host, portText, &hints, &results) != 0 || results == nullptr) return false; + if (getaddrinfo(resolvedHost.c_str(), portText, &hints, &results) != 0 || results == nullptr) return false; *address = *reinterpret_cast(results->ai_addr); - address->sin_port = htons(port); + address->sin_port = htons(resolvedPort); freeaddrinfo(results); return true; } @@ -118,6 +203,7 @@ namespace { unsigned long texturePackParentId; unsigned char subTexturePackId; wchar_t displayName[32]; + wchar_t description[1024]; wchar_t playerNames[MINECRAFT_NET_MAX_PLAYERS][XUSER_NAME_SIZE]; }; @@ -139,6 +225,7 @@ namespace { sessionInfo->data.isJoinable = sessionInfo->data.isReadyToJoin && (serverData.maxPlayers == 0 || serverData.playerCount < serverData.maxPlayers); sessionInfo->isDedicatedSession = serverData.isDedicated; + sessionInfo->description = std::wstring(serverData.description); sessionInfo->hasServerInfo = true; sessionInfo->serverPlayerNames.clear(); @@ -271,6 +358,8 @@ CPlatformNetworkManagerWindows::CPlatformNetworkManagerWindows() { m_maxPlayers = static_cast(MINECRAFT_NET_MAX_PLAYERS); m_flagIndexSize = 0; + m_serverDescription = L""; + m_listenSocket = INVALID_SOCKET; m_discoverySocket = INVALID_SOCKET; m_gamePort = WINDOWS_NETWORK_PORT; @@ -463,6 +552,10 @@ void CPlatformNetworkManagerWindows::ProcessPendingClients() { response.ready = m_inSession ? 1 : 0; response.netVersion = MINECRAFT_NET_VERSION; response.gamePort = m_gamePort; + { + const wchar_t* descriptionString = m_serverDescription.c_str(); + wcsncpy_s(response.description, 1024, descriptionString, _TRUNCATE); + } response.hostSettings = app.GetGameHostOption(eGameHostOption_All); response.texturePackParentId = m_sessionData.texturePackParentId; response.subTexturePackId = m_sessionData.subTexturePackId; @@ -743,6 +836,15 @@ void CPlatformNetworkManagerWindows::HostGame(int localUsersMask, bool bOnlineGa port = g_Win64DedicatedServerPort; if (g_Win64DedicatedServerBindIP[0] != '\0') bindIp = g_Win64DedicatedServerBindIP; + + if (g_Win64DedicatedServerDescription[0] != '\0') { + std::string tempDescription = std::string(g_Win64DedicatedServerDescription); + m_serverDescription = convStringToWstring(tempDescription); + } else { + m_serverDescription = L"A Minecraft Server"; + } + } else { + m_serverDescription = L"A Minecraft World"; } m_gamePort = static_cast(port); if (!m_winsockStarted) { diff --git a/Minecraft.Client/Windows64/Network/PlatformNetworkManagerWindows.h b/Minecraft.Client/Windows64/Network/PlatformNetworkManagerWindows.h index ffd95ea2..25bdf70b 100644 --- a/Minecraft.Client/Windows64/Network/PlatformNetworkManagerWindows.h +++ b/Minecraft.Client/Windows64/Network/PlatformNetworkManagerWindows.h @@ -112,6 +112,7 @@ private: bool m_isLocalGame; bool m_isPrivateGame; bool m_isLeaving; + std::wstring m_serverDescription; unsigned char m_nextSmallId; unsigned char m_maxPlayers; int m_flagIndexSize; diff --git a/Minecraft.Client/Windows64/Network/WindowsNetworkConfig.h b/Minecraft.Client/Windows64/Network/WindowsNetworkConfig.h index 6c2a267e..67af67d4 100644 --- a/Minecraft.Client/Windows64/Network/WindowsNetworkConfig.h +++ b/Minecraft.Client/Windows64/Network/WindowsNetworkConfig.h @@ -12,6 +12,7 @@ extern char g_Win64MultiplayerIP[256]; extern bool g_Win64DedicatedServer; extern int g_Win64DedicatedServerPort; extern char g_Win64DedicatedServerBindIP[256]; +extern char g_Win64DedicatedServerDescription[1024]; extern bool g_Win64DedicatedServerLanAdvertise; #endif diff --git a/Minecraft.Server/ServerProperties.cpp b/Minecraft.Server/ServerProperties.cpp index 43ec5ee0..05844d2c 100644 --- a/Minecraft.Server/ServerProperties.cpp +++ b/Minecraft.Server/ServerProperties.cpp @@ -826,6 +826,7 @@ ServerPropertiesConfig LoadServerPropertiesConfig() config.serverPort = ReadNormalizedIntProperty(&merged, "server-port", kDefaultServerPort, 1, 65535, &shouldWrite); config.serverIp = ReadNormalizedStringProperty(&merged, "server-ip", "0.0.0.0", 255, &shouldWrite); + config.description = ReadNormalizedStringProperty(&merged, "motd", "A Minecraft Server", 1024, &shouldWrite); config.lanAdvertise = ReadNormalizedBoolProperty(&merged, kLanAdvertisePropertyKey, false, &shouldWrite); config.whiteListEnabled = ReadNormalizedBoolProperty(&merged, "white-list", false, &shouldWrite); config.serverName = ReadNormalizedStringProperty(&merged, "server-name", "DedicatedServer", 16, &shouldWrite); diff --git a/Minecraft.Server/ServerProperties.h b/Minecraft.Server/ServerProperties.h index b9568300..f0723100 100644 --- a/Minecraft.Server/ServerProperties.h +++ b/Minecraft.Server/ServerProperties.h @@ -19,6 +19,8 @@ namespace ServerRuntime int serverPort; /** `server-ip` */ std::string serverIp; + /** `server-motd` */ + std::string description; /** `lan-advertise` */ bool lanAdvertise; /** `white-list` */ diff --git a/Minecraft.Server/Windows64/ServerMain.cpp b/Minecraft.Server/Windows64/ServerMain.cpp index 3102864d..02dee247 100644 --- a/Minecraft.Server/Windows64/ServerMain.cpp +++ b/Minecraft.Server/Windows64/ServerMain.cpp @@ -69,6 +69,7 @@ struct DedicatedServerConfig { int port; char bindIP[256]; + char description[1024]; char name[17]; int maxPlayers; int worldSize; @@ -316,6 +317,11 @@ static void ApplyServerPropertiesToDedicatedConfig(const ServerPropertiesConfig sizeof(config->name), serverProperties.serverName.empty() ? "DedicatedServer" : serverProperties.serverName.c_str(), _TRUNCATE); + strncpy_s( + config->description, + sizeof(config->description), + serverProperties.description.empty() ? "A Minecraft Server" : serverProperties.description.c_str(), + _TRUNCATE); config->maxPlayers = serverProperties.maxPlayers; config->worldSize = serverProperties.worldSize; config->worldSizeChunks = serverProperties.worldSizeChunks; @@ -365,6 +371,7 @@ int main(int argc, char **argv) DedicatedServerConfig config; config.port = WIN64_NET_DEFAULT_PORT; strncpy_s(config.bindIP, sizeof(config.bindIP), "0.0.0.0", _TRUNCATE); + strncpy_s(config.description, sizeof(config.description), "", _TRUNCATE); strncpy_s(config.name, sizeof(config.name), "DedicatedServer", _TRUNCATE); config.maxPlayers = MINECRAFT_NET_MAX_PLAYERS; config.worldSize = e_worldSize_Classic; @@ -430,6 +437,7 @@ int main(int argc, char **argv) g_Win64DedicatedServer = true; g_Win64DedicatedServerPort = config.port; strncpy_s(g_Win64DedicatedServerBindIP, sizeof(g_Win64DedicatedServerBindIP), config.bindIP, _TRUNCATE); + strncpy_s(g_Win64DedicatedServerDescription, sizeof(g_Win64DedicatedServerDescription), config.description, _TRUNCATE); g_Win64DedicatedServerLanAdvertise = serverProperties.lanAdvertise; LogStartupStep("initializing server log manager"); ServerRuntime::ServerLogManager::Initialize(); diff --git a/cmake/ServerTarget.cmake b/cmake/ServerTarget.cmake index 9ea44f67..3dad91bc 100644 --- a/cmake/ServerTarget.cmake +++ b/cmake/ServerTarget.cmake @@ -78,6 +78,7 @@ function(configure_lce_server_target target) XInput9_1_0 wsock32 ws2_32 + dnsapi dbghelp legacy_stdio_definitions 4JLibs.${PLATFORM_NAME}.Input