Explorar o código

Always validate messages in debug mode. Fixes for schemas

Ivan Savenko hai 1 ano
pai
achega
c21e5bb0fb

+ 9 - 3
client/globalLobby/GlobalLobbyClient.cpp

@@ -19,11 +19,13 @@
 #include "../windows/InfoWindows.h"
 #include "../CServerHandler.h"
 #include "../mainmenu/CMainMenu.h"
+#include "../CGameInfo.h"
 
 #include "../../lib/CConfigHandler.h"
 #include "../../lib/MetaString.h"
 #include "../../lib/json/JsonUtils.h"
 #include "../../lib/TextOperations.h"
+#include "../../lib/CGeneralTextHandler.h"
 
 GlobalLobbyClient::GlobalLobbyClient() = default;
 GlobalLobbyClient::~GlobalLobbyClient() = default;
@@ -119,7 +121,7 @@ void GlobalLobbyClient::receiveLoginSuccess(const JsonNode & json)
 	if(!loginWindowPtr || !GH.windows().topWindow<GlobalLobbyLoginWindow>())
 		throw std::runtime_error("lobby connection finished without active login window!");
 
-	loginWindowPtr->onConnectionSuccess();
+	loginWindowPtr->onLoginSuccess();
 }
 
 void GlobalLobbyClient::receiveChatHistory(const JsonNode & json)
@@ -231,6 +233,8 @@ void GlobalLobbyClient::sendClientRegister(const std::string & accountName)
 	JsonNode toSend;
 	toSend["type"].String() = "clientRegister";
 	toSend["displayName"].String() = accountName;
+	toSend["language"].String() = CGI->generaltexth->getPreferredLanguage();
+	toSend["version"].String() = VCMI_VERSION_STRING;
 	sendMessage(toSend);
 }
 
@@ -240,6 +244,8 @@ void GlobalLobbyClient::sendClientLogin()
 	toSend["type"].String() = "clientLogin";
 	toSend["accountID"] = settings["lobby"]["accountID"];
 	toSend["accountCookie"] = settings["lobby"]["accountCookie"];
+	toSend["language"].String() = CGI->generaltexth->getPreferredLanguage();
+	toSend["version"].String() = VCMI_VERSION_STRING;
 	sendMessage(toSend);
 }
 
@@ -274,7 +280,7 @@ void GlobalLobbyClient::onDisconnected(const std::shared_ptr<INetworkConnection>
 
 void GlobalLobbyClient::sendMessage(const JsonNode & data)
 {
-	assert(JsonUtils::validate(data, "vcmi:lobbyProtocol/" + data["type"].String(), "network"));
+	assert(JsonUtils::validate(data, "vcmi:lobbyProtocol/" + data["type"].String(), data["type"].String() + " pack"));
 	networkConnection->sendPacket(data.toBytes());
 }
 
@@ -364,6 +370,6 @@ void GlobalLobbyClient::sendProxyConnectionLogin(const NetworkConnectionPtr & ne
 	toSend["accountCookie"] = settings["lobby"]["accountCookie"];
 	toSend["gameRoomID"] = settings["lobby"]["roomID"];
 
-	assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), "network"));
+	assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack"));
 	netConnection->sendPacket(toSend.toBytes());
 }

+ 1 - 1
config/schemas/lobbyProtocol/activateGameRoom.json

@@ -3,7 +3,7 @@
 	"$schema" : "http://json-schema.org/draft-06/schema",
 	"title" : "Lobby protocol: activateGameRoom",
 	"description" : "Sent by client when player wants to activate a game room",
-	"required" : [ "accountID", "accountCookie", "language", "version" ],
+	"required" : [ "type", "hostAccountID", "roomType" ],
 	"additionalProperties" : false,
 
 	"properties" : {

+ 6 - 1
config/schemas/lobbyProtocol/chatMessage.json

@@ -22,10 +22,15 @@
 			"type" : "string",
 			"description" : "ID of account that have sent this message"
 		},
+		"displayName" :
+		{
+			"type" : "string",
+			"description" : "Display name of account that have sent this message"
+		},
 		"roomMode" :
 		{
 			"type" : "string",
-			"const" : "general",
+			"const" : "global",
 			"description" : "Type of room to which this message has been set. Right now can only be 'general'"
 		},
 		"roomName" :

+ 1 - 1
config/schemas/lobbyProtocol/clientRegister.json

@@ -10,7 +10,7 @@
 		"type" :
 		{
 			"type" : "string",
-			"const" : "clientLogin"
+			"const" : "clientRegister"
 		},
 		"displayName" :
 		{

+ 1 - 1
config/schemas/lobbyProtocol/sendChatMessage.json

@@ -3,7 +3,7 @@
 	"$schema" : "http://json-schema.org/draft-06/schema",
 	"title" : "Lobby protocol: sendChatMessage",
 	"description" : "Sent by client when player requests lobby login",
-	"required" : [ "accountID", "accountCookie", "language", "version" ],
+	"required" : [ "type", "messageText" ],
 	"additionalProperties" : false,
 
 	"properties" : {

+ 0 - 1
lib/json/JsonValidator.cpp

@@ -522,7 +522,6 @@ JsonValidator::TValidatorMap createCommonFields()
 	// Not implemented
 	ret["propertyNames"] = notImplementedCheck;
 	ret["contains"] = notImplementedCheck;
-	ret["const"] = notImplementedCheck;
 	ret["examples"] = notImplementedCheck;
 
 	return ret;

+ 6 - 3
lobby/LobbyServer.cpp

@@ -60,7 +60,7 @@ NetworkConnectionPtr LobbyServer::findGameRoom(const std::string & gameRoomID) c
 
 void LobbyServer::sendMessage(const NetworkConnectionPtr & target, const JsonNode & json)
 {
-	assert(JsonUtils::validate(json, "vcmi:lobbyProtocol/" + json["type"].String(), "network"));
+	assert(JsonUtils::validate(json, "vcmi:lobbyProtocol/" + json["type"].String(), json["type"].String() + " pack"));
 	target->sendPacket(json.toBytes());
 }
 
@@ -104,6 +104,7 @@ void LobbyServer::sendChatHistory(const NetworkConnectionPtr & target, const std
 {
 	JsonNode reply;
 	reply["type"].String() = "chatHistory";
+	reply["messages"].Vector(); // force creation of empty vector
 
 	for(const auto & message : boost::adaptors::reverse(history))
 	{
@@ -126,6 +127,7 @@ void LobbyServer::broadcastActiveAccounts()
 
 	JsonNode reply;
 	reply["type"].String() = "activeAccounts";
+	reply["accounts"].Vector(); // force creation of empty vector
 
 	for(const auto & account : activeAccountsStats)
 	{
@@ -145,6 +147,7 @@ JsonNode LobbyServer::prepareActiveGameRooms()
 	auto activeGameRoomStats = database->getActiveGameRooms();
 	JsonNode reply;
 	reply["type"].String() = "activeGameRooms";
+	reply["gameRooms"].Vector(); // force creation of empty vector
 
 	for(const auto & gameRoom : activeGameRoomStats)
 	{
@@ -262,7 +265,7 @@ JsonNode LobbyServer::parseAndValidateMessage(const std::vector<std::byte> & mes
 
 	std::string schemaName = "vcmi:lobbyProtocol/" + messageType;
 
-	if (!JsonUtils::validate(json, schemaName, "network"))
+	if (!JsonUtils::validate(json, schemaName, messageType + " pack"))
 	{
 		logGlobal->info("Json validation error encountered!");
 		assert(0);
@@ -369,7 +372,7 @@ void LobbyServer::receiveClientRegister(const NetworkConnectionPtr & connection,
 	std::string displayName = json["displayName"].String();
 	std::string language = json["language"].String();
 
-	if(isAccountNameValid(displayName))
+	if(!isAccountNameValid(displayName))
 		return sendOperationFailed(connection, "Illegal account name");
 
 	if(database->isAccountNameExists(displayName))

+ 4 - 3
server/GlobalLobbyProcessor.cpp

@@ -47,7 +47,7 @@ void GlobalLobbyProcessor::onDisconnected(const std::shared_ptr<INetworkConnecti
 					message["type"].String() = "leaveGameRoom";
 					message["accountID"].String() = proxy.first;
 
-					assert(JsonUtils::validate(message, "vcmi:lobbyProtocol/" + message["type"].String(), "network"));
+					assert(JsonUtils::validate(message, "vcmi:lobbyProtocol/" + message["type"].String(), message["type"].String() + " pack"));
 					controlConnection->sendPacket(message.toBytes());
 					break;
 				}
@@ -125,8 +125,9 @@ void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetwor
 		toSend["gameRoomID"].String() = owner.uuid;
 		toSend["accountID"] = settings["lobby"]["accountID"];
 		toSend["accountCookie"] = settings["lobby"]["accountCookie"];
+		toSend["version"].String() = VCMI_VERSION_STRING;
 
-		assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), "network"));
+		assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack"));
 		connection->sendPacket(toSend.toBytes());
 	}
 	else
@@ -143,7 +144,7 @@ void GlobalLobbyProcessor::onConnectionEstablished(const std::shared_ptr<INetwor
 		toSend["guestAccountID"].String() = guestAccountID;
 		toSend["accountCookie"] = settings["lobby"]["accountCookie"];
 
-		assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), "network"));
+		assert(JsonUtils::validate(toSend, "vcmi:lobbyProtocol/" + toSend["type"].String(), toSend["type"].String() + " pack"));
 		connection->sendPacket(toSend.toBytes());
 
 		proxyConnections[guestAccountID] = connection;