2023-01-06 18:52:18 +02:00
|
|
|
/*
|
|
|
|
* ClientCommandManager.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2023-01-07 00:37:57 +02:00
|
|
|
#include "StdInc.h"
|
2023-01-06 18:52:18 +02:00
|
|
|
#include "ClientCommandManager.h"
|
|
|
|
|
|
|
|
#include "Client.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "adventureMap/CInGameConsole.h"
|
2023-01-06 18:52:18 +02:00
|
|
|
#include "CPlayerInterface.h"
|
2023-04-17 12:06:58 +02:00
|
|
|
#include "PlayerLocalState.h"
|
2023-01-06 18:52:18 +02:00
|
|
|
#include "CServerHandler.h"
|
|
|
|
#include "gui/CGuiHandler.h"
|
2023-05-16 14:10:26 +02:00
|
|
|
#include "gui/WindowHandler.h"
|
2023-09-04 17:01:44 +02:00
|
|
|
#include "render/IRenderHandler.h"
|
2023-02-12 09:23:39 +02:00
|
|
|
#include "ClientNetPackVisitors.h"
|
2023-01-06 18:52:18 +02:00
|
|
|
#include "../lib/CConfigHandler.h"
|
2023-06-23 17:02:48 +02:00
|
|
|
#include "../lib/gameState/CGameState.h"
|
2023-01-06 18:52:18 +02:00
|
|
|
#include "../lib/CPlayerState.h"
|
2023-08-19 23:22:31 +02:00
|
|
|
#include "../lib/constants/StringConstants.h"
|
2023-06-25 21:28:24 +02:00
|
|
|
#include "../lib/campaign/CampaignHandler.h"
|
2023-03-14 17:54:37 +02:00
|
|
|
#include "../lib/mapping/CMapService.h"
|
|
|
|
#include "../lib/mapping/CMap.h"
|
2023-01-06 18:52:18 +02:00
|
|
|
#include "windows/CCastleInterface.h"
|
2023-10-23 12:59:15 +02:00
|
|
|
#include "../lib/mapObjects/CGHeroInstance.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "render/CAnimation.h"
|
2023-01-06 18:52:18 +02:00
|
|
|
#include "../CCallback.h"
|
|
|
|
#include "../lib/CGeneralTextHandler.h"
|
2023-03-15 21:34:29 +02:00
|
|
|
#include "../lib/filesystem/Filesystem.h"
|
2023-07-30 19:12:25 +02:00
|
|
|
#include "../lib/modding/CModHandler.h"
|
|
|
|
#include "../lib/modding/ContentTypeHandler.h"
|
|
|
|
#include "../lib/modding/ModUtility.h"
|
2023-01-06 18:52:18 +02:00
|
|
|
#include "../lib/CHeroHandler.h"
|
|
|
|
#include "../lib/VCMIDirs.h"
|
2024-02-03 12:19:48 +02:00
|
|
|
#include "../lib/logging/VisualLogger.h"
|
2023-02-01 20:42:06 +02:00
|
|
|
#include "CMT.h"
|
2024-07-17 16:09:04 +02:00
|
|
|
#include "../lib/serializer/Connection.h"
|
2023-01-06 18:52:18 +02:00
|
|
|
|
|
|
|
#ifdef SCRIPTING_ENABLED
|
|
|
|
#include "../lib/ScriptHandler.h"
|
|
|
|
#endif
|
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
void ClientCommandManager::handleQuitCommand()
|
|
|
|
{
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientCommandManager::handleSaveCommand(std::istringstream & singleWordBuffer)
|
|
|
|
{
|
|
|
|
if(!CSH->client)
|
|
|
|
{
|
|
|
|
printCommandMessage("Game is not in playing state");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string saveFilename;
|
|
|
|
singleWordBuffer >> saveFilename;
|
|
|
|
CSH->client->save(saveFilename);
|
|
|
|
printCommandMessage("Game saved as: " + saveFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientCommandManager::handleLoadCommand(std::istringstream& singleWordBuffer)
|
|
|
|
{
|
|
|
|
// TODO: this code should end the running game and manage to call startGame instead
|
|
|
|
//std::string fname;
|
|
|
|
//singleWordBuffer >> fname;
|
|
|
|
//CSH->client->loadGame(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientCommandManager::handleGoSoloCommand()
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
|
|
|
Settings session = settings.write["session"];
|
|
|
|
|
2023-09-27 17:33:52 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
|
2023-01-06 18:52:18 +02:00
|
|
|
if(!CSH->client)
|
|
|
|
{
|
2023-01-06 22:01:00 +02:00
|
|
|
printCommandMessage("Game is not in playing state");
|
2023-01-06 18:52:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-07-17 16:09:04 +02:00
|
|
|
|
2023-01-06 18:52:18 +02:00
|
|
|
if(session["aiSolo"].Bool())
|
|
|
|
{
|
2024-07-17 16:09:04 +02:00
|
|
|
// unlikely it will work but just in case to be consistent
|
|
|
|
for(auto & color : CSH->getAllClientPlayers(CSH->logicConnection->connectionID))
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2024-07-18 12:37:18 +02:00
|
|
|
if(color.isValidPlayer() && CSH->client->getStartInfo()->playerInfos.at(color).isControlledByHuman())
|
2024-07-17 16:09:04 +02:00
|
|
|
{
|
|
|
|
CSH->client->installNewPlayerInterface(std::make_shared<CPlayerInterface>(color), color);
|
|
|
|
}
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-07-17 16:09:04 +02:00
|
|
|
PlayerColor currentColor = LOCPLINT->playerID;
|
2023-01-11 18:11:44 +02:00
|
|
|
CSH->client->removeGUI();
|
2024-07-17 16:09:04 +02:00
|
|
|
|
|
|
|
for(auto & color : CSH->getAllClientPlayers(CSH->logicConnection->connectionID))
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2024-07-18 12:37:18 +02:00
|
|
|
if(color.isValidPlayer() && CSH->client->getStartInfo()->playerInfos.at(color).isControlledByHuman())
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2024-07-17 16:09:04 +02:00
|
|
|
auto AiToGive = CSH->client->aiNameForPlayer(*CSH->client->getPlayerSettings(color), false, false);
|
|
|
|
printCommandMessage("Player " + color.toString() + " will be lead by " + AiToGive, ELogLevel::INFO);
|
|
|
|
CSH->client->installNewPlayerInterface(CDynLibHandler::getNewAI(AiToGive), color);
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
|
|
|
}
|
2024-07-17 16:09:04 +02:00
|
|
|
|
2023-05-16 14:10:26 +02:00
|
|
|
GH.windows().totalRedraw();
|
2024-07-17 16:09:04 +02:00
|
|
|
giveTurn(currentColor);
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
2024-07-17 16:09:04 +02:00
|
|
|
|
2023-01-06 18:52:18 +02:00
|
|
|
session["aiSolo"].Bool() = !session["aiSolo"].Bool();
|
|
|
|
}
|
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
void ClientCommandManager::handleAutoskipCommand()
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2023-04-16 18:29:31 +02:00
|
|
|
Settings session = settings.write["session"];
|
|
|
|
session["autoSkip"].Bool() = !session["autoSkip"].Bool();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientCommandManager::handleControlaiCommand(std::istringstream& singleWordBuffer)
|
|
|
|
{
|
|
|
|
std::string colorName;
|
|
|
|
singleWordBuffer >> colorName;
|
|
|
|
boost::to_lower(colorName);
|
|
|
|
|
2023-09-27 17:33:52 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
|
2023-01-06 18:52:18 +02:00
|
|
|
if(!CSH->client)
|
|
|
|
{
|
2023-01-06 22:01:00 +02:00
|
|
|
printCommandMessage("Game is not in playing state");
|
2023-01-06 18:52:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
|
2023-01-06 18:52:18 +02:00
|
|
|
PlayerColor color;
|
|
|
|
if(LOCPLINT)
|
|
|
|
color = LOCPLINT->playerID;
|
2023-04-16 18:29:31 +02:00
|
|
|
|
2023-01-06 18:52:18 +02:00
|
|
|
for(auto & elem : CSH->client->gameState()->players)
|
|
|
|
{
|
2024-04-27 12:57:50 +02:00
|
|
|
if(!elem.first.isValidPlayer()
|
|
|
|
|| elem.second.human
|
|
|
|
|| (colorName.length() && elem.first.getNum() != vstd::find_pos(GameConstants::PLAYER_COLOR_NAMES, colorName)))
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-11 18:11:44 +02:00
|
|
|
CSH->client->removeGUI();
|
2023-01-06 18:52:18 +02:00
|
|
|
CSH->client->installNewPlayerInterface(std::make_shared<CPlayerInterface>(elem.first), elem.first);
|
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
|
2023-05-16 14:10:26 +02:00
|
|
|
GH.windows().totalRedraw();
|
2023-01-06 18:52:18 +02:00
|
|
|
if(color != PlayerColor::NEUTRAL)
|
|
|
|
giveTurn(color);
|
|
|
|
}
|
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
void ClientCommandManager::handleSetBattleAICommand(std::istringstream& singleWordBuffer)
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2023-04-16 18:29:31 +02:00
|
|
|
std::string aiName;
|
|
|
|
singleWordBuffer >> aiName;
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
printCommandMessage("Will try loading that AI to see if it is correct name...\n");
|
|
|
|
try
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2023-04-16 18:29:31 +02:00
|
|
|
if(auto ai = CDynLibHandler::getNewBattleAI(aiName)) //test that given AI is indeed available... heavy but it is easy to make a typo and break the game
|
|
|
|
{
|
|
|
|
Settings neutralAI = settings.write["server"]["neutralAI"];
|
|
|
|
neutralAI->String() = aiName;
|
|
|
|
printCommandMessage("Setting changed, from now the battle ai will be " + aiName + "!\n");
|
|
|
|
}
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
catch(std::exception &e)
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2023-04-16 18:29:31 +02:00
|
|
|
printCommandMessage("Failed opening " + aiName + ": " + e.what(), ELogLevel::WARN);
|
|
|
|
printCommandMessage("Setting not changed, AI not found or invalid!", ELogLevel::WARN);
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
}
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
void ClientCommandManager::handleRedrawCommand()
|
|
|
|
{
|
2023-05-16 14:10:26 +02:00
|
|
|
GH.windows().totalRedraw();
|
2023-04-16 18:29:31 +02:00
|
|
|
}
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2024-04-08 12:16:23 +02:00
|
|
|
void ClientCommandManager::handleTranslateGameCommand()
|
2023-04-16 18:29:31 +02:00
|
|
|
{
|
2024-04-08 12:16:23 +02:00
|
|
|
std::map<std::string, std::map<std::string, std::string>> textsByMod;
|
|
|
|
VLC->generaltexth->exportAllTexts(textsByMod);
|
|
|
|
|
|
|
|
const boost::filesystem::path outPath = VCMIDirs::get().userExtractedPath() / "translation";
|
|
|
|
boost::filesystem::create_directories(outPath);
|
|
|
|
|
|
|
|
for(const auto & modEntry : textsByMod)
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2024-04-08 12:16:23 +02:00
|
|
|
JsonNode output;
|
2023-03-14 17:54:37 +02:00
|
|
|
|
2024-04-08 12:16:23 +02:00
|
|
|
for(const auto & stringEntry : modEntry.second)
|
|
|
|
{
|
|
|
|
if(boost::algorithm::starts_with(stringEntry.first, "map."))
|
|
|
|
continue;
|
|
|
|
if(boost::algorithm::starts_with(stringEntry.first, "campaign."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
output[stringEntry.first].String() = stringEntry.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!output.isNull())
|
|
|
|
{
|
|
|
|
const boost::filesystem::path filePath = outPath / (modEntry.first + ".json");
|
|
|
|
std::ofstream file(filePath.c_str());
|
|
|
|
file << output.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printCommandMessage("Translation export complete");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientCommandManager::handleTranslateMapsCommand()
|
|
|
|
{
|
|
|
|
CMapService mapService;
|
|
|
|
|
|
|
|
printCommandMessage("Searching for available maps");
|
|
|
|
std::unordered_set<ResourcePath> mapList = CResourceHandler::get()->getFilteredFiles([&](const ResourcePath & ident)
|
2023-04-16 18:29:31 +02:00
|
|
|
{
|
2024-04-08 12:16:23 +02:00
|
|
|
return ident.getType() == EResType::MAP;
|
2023-04-16 18:29:31 +02:00
|
|
|
});
|
2023-03-14 17:54:37 +02:00
|
|
|
|
2024-04-08 12:16:23 +02:00
|
|
|
std::vector<std::unique_ptr<CMap>> loadedMaps;
|
|
|
|
std::vector<std::shared_ptr<CampaignState>> loadedCampaigns;
|
2023-03-14 17:54:37 +02:00
|
|
|
|
2024-04-08 12:16:23 +02:00
|
|
|
printCommandMessage("Loading maps for export");
|
2023-04-16 18:29:31 +02:00
|
|
|
for (auto const & mapName : mapList)
|
|
|
|
{
|
|
|
|
try
|
2023-03-14 21:48:39 +02:00
|
|
|
{
|
2023-04-16 18:29:31 +02:00
|
|
|
// load and drop loaded map - we only need loader to run over all maps
|
2024-04-08 12:16:23 +02:00
|
|
|
loadedMaps.push_back(mapService.loadMap(mapName, nullptr));
|
2023-03-14 21:48:39 +02:00
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
catch(std::exception & e)
|
2023-03-14 19:46:15 +02:00
|
|
|
{
|
2024-04-08 12:16:23 +02:00
|
|
|
logGlobal->warn("Map %s is invalid. Message: %s", mapName.getName(), e.what());
|
2023-03-14 19:46:15 +02:00
|
|
|
}
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
|
2024-04-08 12:16:23 +02:00
|
|
|
printCommandMessage("Searching for available campaigns");
|
|
|
|
std::unordered_set<ResourcePath> campaignList = CResourceHandler::get()->getFilteredFiles([&](const ResourcePath & ident)
|
|
|
|
{
|
|
|
|
return ident.getType() == EResType::CAMPAIGN;
|
|
|
|
});
|
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
logGlobal->info("Loading campaigns for export");
|
|
|
|
for (auto const & campaignName : campaignList)
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2024-04-08 12:16:23 +02:00
|
|
|
loadedCampaigns.push_back(CampaignHandler::getCampaign(campaignName.getName()));
|
|
|
|
for (auto const & part : loadedCampaigns.back()->allScenarios())
|
|
|
|
loadedCampaigns.back()->getMap(part, nullptr);
|
2023-04-16 18:29:31 +02:00
|
|
|
}
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2024-04-08 12:16:23 +02:00
|
|
|
std::map<std::string, std::map<std::string, std::string>> textsByMod;
|
|
|
|
VLC->generaltexth->exportAllTexts(textsByMod);
|
|
|
|
|
|
|
|
const boost::filesystem::path outPath = VCMIDirs::get().userExtractedPath() / "translation";
|
|
|
|
boost::filesystem::create_directories(outPath);
|
|
|
|
|
|
|
|
for(const auto & modEntry : textsByMod)
|
|
|
|
{
|
|
|
|
JsonNode output;
|
|
|
|
|
|
|
|
for(const auto & stringEntry : modEntry.second)
|
|
|
|
{
|
|
|
|
if(boost::algorithm::starts_with(stringEntry.first, "map."))
|
|
|
|
output[stringEntry.first].String() = stringEntry.second;
|
|
|
|
|
|
|
|
if(boost::algorithm::starts_with(stringEntry.first, "campaign."))
|
|
|
|
output[stringEntry.first].String() = stringEntry.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!output.isNull())
|
|
|
|
{
|
|
|
|
const boost::filesystem::path filePath = outPath / (modEntry.first + ".json");
|
|
|
|
std::ofstream file(filePath.c_str());
|
|
|
|
file << output.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printCommandMessage("Translation export complete");
|
2023-04-16 18:29:31 +02:00
|
|
|
}
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
void ClientCommandManager::handleGetConfigCommand()
|
|
|
|
{
|
|
|
|
printCommandMessage("Command accepted.\t");
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
const boost::filesystem::path outPath = VCMIDirs::get().userExtractedPath() / "configuration";
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
boost::filesystem::create_directories(outPath);
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
const std::vector<std::string> contentNames = { "heroClasses", "artifacts", "creatures", "factions", "objects", "heroes", "spells", "skills" };
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
for(auto contentName : contentNames)
|
|
|
|
{
|
2023-07-30 19:12:25 +02:00
|
|
|
auto const & handler = *VLC->modh->content;
|
|
|
|
auto const & content = handler[contentName];
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
auto contentOutPath = outPath / contentName;
|
|
|
|
boost::filesystem::create_directories(contentOutPath);
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
for(auto& iter : content.modData)
|
|
|
|
{
|
|
|
|
const JsonNode& modData = iter.second.modData;
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
for(auto& nameAndObject : modData.Struct())
|
|
|
|
{
|
|
|
|
const JsonNode& object = nameAndObject.second;
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2024-02-13 14:34:16 +02:00
|
|
|
std::string name = ModUtility::makeFullIdentifier(object.getModScope(), contentName, nameAndObject.first);
|
2023-04-16 18:29:31 +02:00
|
|
|
|
|
|
|
boost::algorithm::replace_all(name, ":", "_");
|
|
|
|
|
|
|
|
const boost::filesystem::path filePath = contentOutPath / (name + ".json");
|
2023-07-28 14:51:14 +02:00
|
|
|
std::ofstream file(filePath.c_str());
|
2024-02-12 01:22:16 +02:00
|
|
|
file << object.toString();
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
|
|
|
|
printCommandMessage("\rExtracting done :)\n");
|
|
|
|
printCommandMessage("Extracted files can be found in " + outPath.string() + " directory\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientCommandManager::handleGetScriptsCommand()
|
|
|
|
{
|
2023-01-06 18:52:18 +02:00
|
|
|
#if SCRIPTING_ENABLED
|
2023-04-16 18:29:31 +02:00
|
|
|
printCommandMessage("Command accepted.\t");
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
const boost::filesystem::path outPath = VCMIDirs::get().userExtractedPath() / "scripts";
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
boost::filesystem::create_directories(outPath);
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2024-01-19 15:12:35 +02:00
|
|
|
for(const auto & kv : VLC->scriptHandler->objects)
|
2023-04-16 18:29:31 +02:00
|
|
|
{
|
|
|
|
std::string name = kv.first;
|
|
|
|
boost::algorithm::replace_all(name,":","_");
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
const scripting::ScriptImpl * script = kv.second.get();
|
|
|
|
boost::filesystem::path filePath = outPath / (name + ".lua");
|
2023-07-28 14:51:14 +02:00
|
|
|
std::ofstream file(filePath.c_str());
|
2023-04-16 18:29:31 +02:00
|
|
|
file << script->getSource();
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
printCommandMessage("\rExtracting done :)\n");
|
|
|
|
printCommandMessage("Extracted files can be found in " + outPath.string() + " directory\n");
|
2023-01-06 18:52:18 +02:00
|
|
|
#endif
|
2023-04-16 18:29:31 +02:00
|
|
|
}
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
void ClientCommandManager::handleGetTextCommand()
|
|
|
|
{
|
|
|
|
printCommandMessage("Command accepted.\t");
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
const boost::filesystem::path outPath =
|
|
|
|
VCMIDirs::get().userExtractedPath();
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
auto list =
|
2023-08-23 14:07:50 +02:00
|
|
|
CResourceHandler::get()->getFilteredFiles([](const ResourcePath & ident)
|
2023-04-16 18:29:31 +02:00
|
|
|
{
|
|
|
|
return ident.getType() == EResType::TEXT && boost::algorithm::starts_with(ident.getName(), "DATA/");
|
|
|
|
});
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
for (auto & filename : list)
|
|
|
|
{
|
|
|
|
const boost::filesystem::path filePath = outPath / (filename.getName() + ".TXT");
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
boost::filesystem::create_directories(filePath.parent_path());
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-07-28 14:51:14 +02:00
|
|
|
std::ofstream file(filePath.c_str());
|
2023-04-16 18:29:31 +02:00
|
|
|
auto text = CResourceHandler::get()->load(filename)->readAll();
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
file.write((char*)text.first.get(), text.second);
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
|
|
|
|
printCommandMessage("\rExtracting done :)\n");
|
|
|
|
printCommandMessage("Extracted files can be found in " + outPath.string() + " directory\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientCommandManager::handleDef2bmpCommand(std::istringstream& singleWordBuffer)
|
|
|
|
{
|
|
|
|
std::string URI;
|
|
|
|
singleWordBuffer >> URI;
|
2023-09-04 17:01:44 +02:00
|
|
|
auto anim = GH.renderHandler().loadAnimation(AnimationPath::builtin(URI));
|
2023-04-16 18:29:31 +02:00
|
|
|
anim->preload();
|
|
|
|
anim->exportBitmaps(VCMIDirs::get().userExtractedPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientCommandManager::handleExtractCommand(std::istringstream& singleWordBuffer)
|
|
|
|
{
|
|
|
|
std::string URI;
|
|
|
|
singleWordBuffer >> URI;
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
if(CResourceHandler::get()->existsResource(ResourcePath(URI)))
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2023-04-16 18:29:31 +02:00
|
|
|
const boost::filesystem::path outPath = VCMIDirs::get().userExtractedPath() / URI;
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
auto data = CResourceHandler::get()->load(ResourcePath(URI))->readAll();
|
2023-04-16 18:29:31 +02:00
|
|
|
|
|
|
|
boost::filesystem::create_directories(outPath.parent_path());
|
2023-07-28 14:51:14 +02:00
|
|
|
std::ofstream outFile(outPath.c_str(), std::ofstream::binary);
|
2023-04-16 18:29:31 +02:00
|
|
|
outFile.write((char*)data.first.get(), data.second);
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
else
|
|
|
|
printCommandMessage("File not found!", ELogLevel::ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientCommandManager::handleBonusesCommand(std::istringstream & singleWordBuffer)
|
|
|
|
{
|
|
|
|
if(currentCallFromIngameConsole)
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2023-04-16 18:29:31 +02:00
|
|
|
printCommandMessage("Output for this command is too large for ingame chat! Please run it from client console.\n");
|
|
|
|
return;
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
|
|
|
|
std::string outputFormat;
|
|
|
|
singleWordBuffer >> outputFormat;
|
|
|
|
|
|
|
|
auto format = [outputFormat](const BonusList & b) -> std::string
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2023-04-16 18:29:31 +02:00
|
|
|
if(outputFormat == "json")
|
2024-02-12 01:22:16 +02:00
|
|
|
return b.toJsonNode().toCompactString();
|
2023-04-16 18:29:31 +02:00
|
|
|
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << b;
|
|
|
|
return ss.str();
|
|
|
|
};
|
2023-04-17 12:06:58 +02:00
|
|
|
printCommandMessage("Bonuses of " + LOCPLINT->localState->getCurrentArmy()->getObjectName() + "\n");
|
2023-05-03 15:09:21 +02:00
|
|
|
printCommandMessage(format(*LOCPLINT->localState->getCurrentArmy()->getAllBonuses(Selector::all, Selector::all)) + "\n");
|
2023-04-16 18:29:31 +02:00
|
|
|
|
|
|
|
printCommandMessage("\nInherited bonuses:\n");
|
|
|
|
TCNodes parents;
|
2023-04-17 12:06:58 +02:00
|
|
|
LOCPLINT->localState->getCurrentArmy()->getParents(parents);
|
2023-04-16 18:29:31 +02:00
|
|
|
for(const CBonusSystemNode *parent : parents)
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2023-04-16 18:29:31 +02:00
|
|
|
printCommandMessage(std::string("\nBonuses from ") + typeid(*parent).name() + "\n" + format(*parent->getAllBonuses(Selector::all, Selector::all)) + "\n");
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientCommandManager::handleTellCommand(std::istringstream& singleWordBuffer)
|
|
|
|
{
|
|
|
|
std::string what;
|
2024-01-10 00:38:54 +02:00
|
|
|
int id1;
|
|
|
|
int id2;
|
2023-04-16 18:29:31 +02:00
|
|
|
singleWordBuffer >> what >> id1 >> id2;
|
|
|
|
|
|
|
|
if(what == "hs")
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2023-04-16 18:29:31 +02:00
|
|
|
for(const CGHeroInstance* h : LOCPLINT->cb->getHeroesInfo())
|
|
|
|
if(h->type->getIndex() == id1)
|
|
|
|
if(const CArtifactInstance* a = h->getArt(ArtifactPosition(id2)))
|
|
|
|
printCommandMessage(a->nodeName());
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
}
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
void ClientCommandManager::handleMpCommand()
|
|
|
|
{
|
2023-04-19 16:38:25 +02:00
|
|
|
if(const CGHeroInstance* h = LOCPLINT->localState->getCurrentHero())
|
2023-06-21 19:38:26 +02:00
|
|
|
printCommandMessage(std::to_string(h->movementPointsRemaining()) + "; max: " + std::to_string(h->movementPointsLimit(true)) + "/" + std::to_string(h->movementPointsLimit(false)) + "\n");
|
2023-04-16 18:29:31 +02:00
|
|
|
}
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
void ClientCommandManager::handleSetCommand(std::istringstream& singleWordBuffer)
|
|
|
|
{
|
2024-01-10 00:38:54 +02:00
|
|
|
std::string what;
|
|
|
|
std::string value;
|
2023-04-16 18:29:31 +02:00
|
|
|
singleWordBuffer >> what;
|
2023-01-06 18:52:18 +02:00
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
Settings config = settings.write["session"][what];
|
|
|
|
|
|
|
|
singleWordBuffer >> value;
|
|
|
|
|
|
|
|
if(value == "on")
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2023-04-16 18:29:31 +02:00
|
|
|
config->Bool() = true;
|
|
|
|
printCommandMessage("Option " + what + " enabled!", ELogLevel::INFO);
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
else if(value == "off")
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2023-04-16 18:29:31 +02:00
|
|
|
config->Bool() = false;
|
|
|
|
printCommandMessage("Option " + what + " disabled!", ELogLevel::INFO);
|
2023-01-06 18:52:18 +02:00
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 22:56:15 +02:00
|
|
|
void ClientCommandManager::handleCrashCommand()
|
2023-01-06 18:52:18 +02:00
|
|
|
{
|
2023-04-16 18:29:31 +02:00
|
|
|
int* ptr = nullptr;
|
|
|
|
*ptr = 666;
|
|
|
|
//disaster!
|
2023-01-06 22:01:00 +02:00
|
|
|
}
|
|
|
|
|
2024-02-03 12:19:48 +02:00
|
|
|
void ClientCommandManager::handleVsLog(std::istringstream & singleWordBuffer)
|
|
|
|
{
|
|
|
|
std::string key;
|
|
|
|
singleWordBuffer >> key;
|
|
|
|
|
|
|
|
logVisual->setKey(key);
|
|
|
|
}
|
|
|
|
|
2023-01-06 22:01:00 +02:00
|
|
|
void ClientCommandManager::printCommandMessage(const std::string &commandMessage, ELogLevel::ELogLevel messageType)
|
|
|
|
{
|
|
|
|
switch(messageType)
|
|
|
|
{
|
|
|
|
case ELogLevel::NOT_SET:
|
|
|
|
std::cout << commandMessage;
|
|
|
|
break;
|
|
|
|
case ELogLevel::TRACE:
|
|
|
|
logGlobal->trace(commandMessage);
|
|
|
|
break;
|
|
|
|
case ELogLevel::DEBUG:
|
|
|
|
logGlobal->debug(commandMessage);
|
|
|
|
break;
|
|
|
|
case ELogLevel::INFO:
|
|
|
|
logGlobal->info(commandMessage);
|
|
|
|
break;
|
|
|
|
case ELogLevel::WARN:
|
|
|
|
logGlobal->warn(commandMessage);
|
|
|
|
break;
|
|
|
|
case ELogLevel::ERROR:
|
|
|
|
logGlobal->error(commandMessage);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
std::cout << commandMessage;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(currentCallFromIngameConsole)
|
|
|
|
{
|
2023-09-27 17:33:52 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
2023-01-06 22:01:00 +02:00
|
|
|
if(LOCPLINT && LOCPLINT->cingconsole)
|
|
|
|
{
|
2024-03-18 19:52:53 +02:00
|
|
|
LOCPLINT->cingconsole->addMessage("", "System", commandMessage);
|
2023-01-06 22:01:00 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-02 13:27:03 +02:00
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
|
|
|
|
void ClientCommandManager::giveTurn(const PlayerColor &colorIdentifier)
|
|
|
|
{
|
2023-09-17 00:13:58 +02:00
|
|
|
PlayerStartsTurn yt;
|
2023-04-16 18:29:31 +02:00
|
|
|
yt.player = colorIdentifier;
|
2023-09-28 18:43:04 +02:00
|
|
|
yt.queryID = QueryID::NONE;
|
2023-04-16 18:29:31 +02:00
|
|
|
|
|
|
|
ApplyClientNetPackVisitor visitor(*CSH->client, *CSH->client->gameState());
|
|
|
|
yt.visit(visitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientCommandManager::processCommand(const std::string & message, bool calledFromIngameConsole)
|
|
|
|
{
|
|
|
|
// split the message into individual words
|
|
|
|
std::istringstream singleWordBuffer;
|
|
|
|
singleWordBuffer.str(message);
|
|
|
|
|
|
|
|
// get command name, to be used for single word commands
|
|
|
|
std::string commandName;
|
|
|
|
singleWordBuffer >> commandName;
|
|
|
|
|
|
|
|
currentCallFromIngameConsole = calledFromIngameConsole;
|
|
|
|
|
|
|
|
if(message == std::string("die, fool"))
|
|
|
|
handleQuitCommand();
|
|
|
|
|
|
|
|
else if(commandName == "save")
|
|
|
|
handleSaveCommand(singleWordBuffer);
|
|
|
|
|
|
|
|
else if(commandName=="load")
|
|
|
|
handleLoadCommand(singleWordBuffer); // not implemented
|
|
|
|
|
|
|
|
else if(commandName == "gosolo")
|
|
|
|
handleGoSoloCommand();
|
|
|
|
|
|
|
|
else if(commandName == "autoskip")
|
|
|
|
handleAutoskipCommand();
|
|
|
|
|
|
|
|
else if(commandName == "controlai")
|
|
|
|
handleControlaiCommand(singleWordBuffer);
|
|
|
|
|
|
|
|
else if(commandName == "setBattleAI")
|
|
|
|
handleSetBattleAICommand(singleWordBuffer);
|
|
|
|
|
|
|
|
else if(commandName == "redraw")
|
|
|
|
handleRedrawCommand();
|
|
|
|
|
2024-04-08 12:16:23 +02:00
|
|
|
else if(message=="translate" || message=="translate game")
|
|
|
|
handleTranslateGameCommand();
|
|
|
|
|
|
|
|
else if(message=="translate maps")
|
|
|
|
handleTranslateMapsCommand();
|
2023-04-16 18:29:31 +02:00
|
|
|
|
|
|
|
else if(message=="get config")
|
|
|
|
handleGetConfigCommand();
|
|
|
|
|
|
|
|
else if(message=="get scripts")
|
|
|
|
handleGetScriptsCommand();
|
|
|
|
|
|
|
|
else if(message=="get txt")
|
|
|
|
handleGetTextCommand();
|
|
|
|
|
|
|
|
else if(commandName == "def2bmp")
|
|
|
|
handleDef2bmpCommand(singleWordBuffer);
|
|
|
|
|
|
|
|
else if(commandName == "extract")
|
|
|
|
handleExtractCommand(singleWordBuffer);
|
|
|
|
|
|
|
|
else if(commandName == "bonuses")
|
|
|
|
handleBonusesCommand(singleWordBuffer);
|
|
|
|
|
|
|
|
else if(commandName == "tell")
|
|
|
|
handleTellCommand(singleWordBuffer);
|
|
|
|
|
2023-04-19 16:38:25 +02:00
|
|
|
else if(commandName == "mp" && LOCPLINT)
|
2023-04-16 18:29:31 +02:00
|
|
|
handleMpCommand();
|
|
|
|
|
|
|
|
else if (commandName == "set")
|
|
|
|
handleSetCommand(singleWordBuffer);
|
|
|
|
|
|
|
|
else if(commandName == "crash")
|
|
|
|
handleCrashCommand();
|
|
|
|
|
2024-02-03 12:19:48 +02:00
|
|
|
else if(commandName == "vslog")
|
|
|
|
handleVsLog(singleWordBuffer);
|
|
|
|
|
2023-04-16 18:29:31 +02:00
|
|
|
else
|
2023-04-16 22:56:15 +02:00
|
|
|
{
|
|
|
|
if (!commandName.empty() && !vstd::iswithin(commandName[0], 0, ' ')) // filter-out debugger/IDE noise
|
|
|
|
printCommandMessage("Command not found :(", ELogLevel::ERROR);
|
|
|
|
}
|
2023-04-16 18:29:31 +02:00
|
|
|
}
|