Explorar el Código

JsonNode constructors: Take fileName as argument

* Don't print JSON in JsonParser::parse() in case of errors
Alexander Wilms hace 1 año
padre
commit
434371195d

+ 1 - 1
client/globalLobby/GlobalLobbyClient.cpp

@@ -43,7 +43,7 @@ void GlobalLobbyClient::onPacketReceived(const std::shared_ptr<INetworkConnectio
 {
 	boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
 
-	JsonNode json(message.data(), message.size());
+	JsonNode json(message.data(), message.size(), "<lobby network packet>");
 
 	if(json["type"].String() == "accountCreated")
 		return receiveAccountCreated(json);

+ 1 - 1
client/render/CAnimation.cpp

@@ -191,7 +191,7 @@ void CAnimation::init()
 		std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
 		stream->read(textData.get(), stream->getSize());
 
-		const JsonNode config(reinterpret_cast<const std::byte*>(textData.get()), stream->getSize());
+		const JsonNode config(reinterpret_cast<const std::byte*>(textData.get()), stream->getSize(), jsonResource.getName());
 
 		initFromJson(config);
 	}

+ 1 - 1
launcher/jsonutils.cpp

@@ -89,7 +89,7 @@ QVariant JsonFromFile(QString filename)
 	}
 
 	const auto data = file.readAll();
-	JsonNode node(reinterpret_cast<const std::byte*>(data.data()), data.size());
+	JsonNode node(reinterpret_cast<const std::byte*>(data.data()), data.size(), filename.toStdString());
 	return toVariant(node);
 }
 

+ 1 - 1
launcher/updatedialog_moc.cpp

@@ -67,7 +67,7 @@ UpdateDialog::UpdateDialog(bool calledManually, QWidget *parent):
 		}
 		
 		auto byteArray = response->readAll();
-		JsonNode node(reinterpret_cast<const std::byte*>(byteArray.constData()), byteArray.size());
+		JsonNode node(reinterpret_cast<const std::byte*>(byteArray.constData()), byteArray.size(), "<network packet from server at updateConfigUrl>");
 		loadFromJson(node);
 	});
 }

+ 1 - 1
lib/campaign/CampaignHandler.cpp

@@ -46,7 +46,7 @@ void CampaignHandler::readCampaign(Campaign * ret, const std::vector<ui8> & inpu
 	}
 	else // text format (json)
 	{
-		JsonNode jsonCampaign(reinterpret_cast<const std::byte*>(input.data()), input.size());
+		JsonNode jsonCampaign(reinterpret_cast<const std::byte*>(input.data()), input.size(), filename);
 		readHeaderFromJson(*ret, jsonCampaign, filename, modName, encoding);
 
 		for(auto & scenario : jsonCampaign["scenarios"].Vector())

+ 2 - 2
lib/filesystem/Filesystem.cpp

@@ -117,7 +117,7 @@ void CFilesystemGenerator::loadJsonMap(const std::string &mountPoint, const Json
 	if (filename)
 	{
 		auto configData = CResourceHandler::get("initial")->load(JsonPath::builtin(URI))->readAll();
-		const JsonNode configInitial(reinterpret_cast<std::byte *>(configData.first.get()), configData.second);
+		const JsonNode configInitial(reinterpret_cast<std::byte *>(configData.first.get()), configData.second, URI);
 		filesystem->addLoader(new CMappedFileLoader(mountPoint, configInitial), false);
 	}
 }
@@ -212,7 +212,7 @@ void CResourceHandler::load(const std::string &fsConfigURI, bool extractArchives
 {
 	auto fsConfigData = get("initial")->load(JsonPath::builtin(fsConfigURI))->readAll();
 
-	const JsonNode fsConfig(reinterpret_cast<std::byte *>(fsConfigData.first.get()), fsConfigData.second);
+	const JsonNode fsConfig(reinterpret_cast<std::byte *>(fsConfigData.first.get()), fsConfigData.second, fsConfigURI);
 
 	addFilesystem("data", ModScope::scopeBuiltin(), createFileSystem("", fsConfig["filesystem"], extractArchives));
 }

+ 4 - 4
lib/json/JsonNode.cpp

@@ -86,15 +86,15 @@ JsonNode::JsonNode(const std::string & string)
 {
 }
 
-JsonNode::JsonNode(const std::byte * data, size_t datasize)
-	: JsonNode(data, datasize, JsonParsingSettings())
+JsonNode::JsonNode(const std::byte * data, size_t datasize, const std::string fileName)
+	: JsonNode(data, datasize, JsonParsingSettings(), fileName)
 {
 }
 
-JsonNode::JsonNode(const std::byte * data, size_t datasize, const JsonParsingSettings & parserSettings)
+JsonNode::JsonNode(const std::byte * data, size_t datasize, const JsonParsingSettings & parserSettings, const std::string fileName)
 {
 	JsonParser parser(data, datasize, parserSettings);
-	*this = parser.parse("<unknown>");
+	*this = parser.parse(fileName);
 }
 
 JsonNode::JsonNode(const JsonPath & fileURI)

+ 2 - 2
lib/json/JsonNode.h

@@ -72,8 +72,8 @@ public:
 	explicit JsonNode(const std::string & string);
 
 	/// Create tree from Json-formatted input
-	explicit JsonNode(const std::byte * data, size_t datasize);
-	explicit JsonNode(const std::byte * data, size_t datasize, const JsonParsingSettings & parserSettings);
+	explicit JsonNode(const std::byte * data, size_t datasize, const std::string fileName);
+	explicit JsonNode(const std::byte * data, size_t datasize, const JsonParsingSettings & parserSettings, const std::string fileName);
 
 	/// Create tree from JSON file
 	explicit JsonNode(const JsonPath & fileURI);

+ 1 - 2
lib/json/JsonParser.cpp

@@ -55,9 +55,8 @@ JsonNode JsonParser::parse(const std::string & fileName)
 
 	if(!errors.empty())
 	{
-		logMod->warn("File %s is not a valid JSON file!", fileName);
+		logMod->warn("%s is not valid JSON!", fileName);
 		logMod->warn(errors);
-		logMod->warn("%s", input);
 	}
 	return root;
 }

+ 1 - 1
lib/json/JsonUtils.cpp

@@ -269,7 +269,7 @@ JsonNode JsonUtils::assembleFromFiles(const std::string & filename)
 	for(auto & loader : CResourceHandler::get()->getResourcesWithName(resID))
 	{
 		auto textData = loader->load(resID)->readAll();
-		JsonNode section(reinterpret_cast<std::byte *>(textData.first.get()), textData.second);
+		JsonNode section(reinterpret_cast<std::byte *>(textData.first.get()), textData.second, resID.getName());
 		merge(result, section);
 	}
 	return result;

+ 1 - 1
lib/mapping/MapFormatJson.cpp

@@ -813,7 +813,7 @@ JsonNode CMapLoaderJson::getFromArchive(const std::string & archiveFilename)
 
 	auto data = loader.load(resource)->readAll();
 
-	JsonNode res(reinterpret_cast<const std::byte*>(data.first.get()), data.second);
+	JsonNode res(reinterpret_cast<const std::byte*>(data.first.get()), data.second, archiveFilename);
 
 	return res;
 }

+ 2 - 2
lobby/LobbyServer.cpp

@@ -213,7 +213,7 @@ static JsonNode loadLobbyGameRoomToJson(const LobbyGameRoom & gameRoom)
 	jsonEntry["playerLimit"].Integer() = gameRoom.playerLimit;
 	jsonEntry["ageSeconds"].Integer() = gameRoom.age.count();
 	if (!gameRoom.modsJson.empty()) // not present in match history
-		jsonEntry["mods"] = JsonNode(reinterpret_cast<const std::byte *>(gameRoom.modsJson.data()), gameRoom.modsJson.size());
+		jsonEntry["mods"] = JsonNode(reinterpret_cast<const std::byte *>(gameRoom.modsJson.data()), gameRoom.modsJson.size(), "<lobby "+gameRoom.roomID+">");
 
 	for(const auto & account : gameRoom.participants)
 		jsonEntry["participants"].Vector().push_back(loadLobbyAccountToJson(account));
@@ -348,7 +348,7 @@ JsonNode LobbyServer::parseAndValidateMessage(const std::vector<std::byte> & mes
 	JsonNode json;
 	try
 	{
-		JsonNode jsonTemp(message.data(), message.size());
+		JsonNode jsonTemp(message.data(), message.size(), "<lobby message>");
 		json = std::move(jsonTemp);
 	}
 	catch (const JsonFormatException & e)

+ 1 - 1
mapeditor/Animation.cpp

@@ -599,7 +599,7 @@ void Animation::init()
 		std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
 		stream->read(textData.get(), stream->getSize());
 
-		const JsonNode config(reinterpret_cast<const std::byte*>(textData.get()), stream->getSize());
+		const JsonNode config(reinterpret_cast<const std::byte*>(textData.get()), stream->getSize(), resID.getName());
 
 		initFromJson(config);
 	}

+ 1 - 1
mapeditor/jsonutils.cpp

@@ -97,7 +97,7 @@ QVariant JsonFromFile(QString filename)
 	}
 	else
 	{
-		JsonNode node(reinterpret_cast<const std::byte*>(data.data()), data.size());
+		JsonNode node(reinterpret_cast<const std::byte*>(data.data()), data.size(), filename.toStdString());
 		return toVariant(node);
 	}
 }

+ 1 - 1
server/GlobalLobbyProcessor.cpp

@@ -67,7 +67,7 @@ void GlobalLobbyProcessor::onPacketReceived(const std::shared_ptr<INetworkConnec
 {
 	if (connection == controlConnection)
 	{
-		JsonNode json(message.data(), message.size());
+		JsonNode json(message.data(), message.size(), "<lobby network packet>");
 
 		if(json["type"].String() == "operationFailed")
 			return receiveOperationFailed(json);