2018-01-05 19:21:07 +02:00
|
|
|
/*
|
|
|
|
* CVCMIServer.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
2023-11-18 16:34:18 +02:00
|
|
|
#include <boost/program_options.hpp>
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
#include "../lib/filesystem/Filesystem.h"
|
2023-06-25 21:28:24 +02:00
|
|
|
#include "../lib/campaign/CampaignState.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/CThreadHelper.h"
|
|
|
|
#include "../lib/CArtHandler.h"
|
|
|
|
#include "../lib/CGeneralTextHandler.h"
|
|
|
|
#include "../lib/CHeroHandler.h"
|
|
|
|
#include "../lib/CTownHandler.h"
|
|
|
|
#include "../lib/CBuildingHandler.h"
|
|
|
|
#include "../lib/spells/CSpellHandler.h"
|
|
|
|
#include "../lib/CCreatureHandler.h"
|
|
|
|
#include "zlib.h"
|
|
|
|
#include "CVCMIServer.h"
|
|
|
|
#include "../lib/StartInfo.h"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "../lib/mapping/CMapHeader.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/rmg/CMapGenOptions.h"
|
2023-02-12 09:23:39 +02:00
|
|
|
#include "LobbyNetPackVisitors.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#ifdef VCMI_ANDROID
|
2023-01-25 08:59:59 +02:00
|
|
|
#include <jni.h>
|
|
|
|
#include <android/log.h>
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "lib/CAndroidVMHelper.h"
|
|
|
|
#endif
|
|
|
|
#include "../lib/VCMI_Lib.h"
|
|
|
|
#include "../lib/VCMIDirs.h"
|
|
|
|
#include "CGameHandler.h"
|
2023-07-23 23:20:35 +02:00
|
|
|
#include "processors/PlayerMessageProcessor.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/mapping/CMapInfo.h"
|
2023-11-18 16:34:18 +02:00
|
|
|
#include "../lib/network/NetworkServer.h"
|
|
|
|
#include "../lib/network/NetworkClient.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
#include "../lib/GameConstants.h"
|
|
|
|
#include "../lib/logging/CBasicLogConfigurator.h"
|
|
|
|
#include "../lib/CConfigHandler.h"
|
|
|
|
#include "../lib/ScopeGuard.h"
|
2018-04-30 17:09:48 +02:00
|
|
|
#include "../lib/serializer/CMemorySerializer.h"
|
2018-08-13 23:48:00 +02:00
|
|
|
#include "../lib/serializer/Cast.h"
|
2023-11-18 16:34:18 +02:00
|
|
|
#include "../lib/serializer/Connection.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
#include "../lib/UnlockGuard.h"
|
|
|
|
|
|
|
|
// for applier
|
2023-11-11 00:39:08 +02:00
|
|
|
#include "../lib/registerTypes/RegisterTypesLobbyPacks.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-08-23 20:03:40 +02:00
|
|
|
// UUID generation
|
|
|
|
#include <boost/uuid/uuid.hpp>
|
|
|
|
#include <boost/uuid/uuid_io.hpp>
|
|
|
|
#include <boost/uuid/uuid_generators.hpp>
|
|
|
|
|
2023-06-23 17:02:48 +02:00
|
|
|
#include "../lib/gameState/CGameState.h"
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
template<typename T> class CApplyOnServer;
|
|
|
|
|
|
|
|
class CBaseForServerApply
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool applyOnServerBefore(CVCMIServer * srv, void * pack) const =0;
|
|
|
|
virtual void applyOnServerAfter(CVCMIServer * srv, void * pack) const =0;
|
|
|
|
virtual ~CBaseForServerApply() {}
|
|
|
|
template<typename U> static CBaseForServerApply * getApplier(const U * t = nullptr)
|
|
|
|
{
|
|
|
|
return new CApplyOnServer<U>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T> class CApplyOnServer : public CBaseForServerApply
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool applyOnServerBefore(CVCMIServer * srv, void * pack) const override
|
|
|
|
{
|
|
|
|
T * ptr = static_cast<T *>(pack);
|
2023-02-12 09:23:39 +02:00
|
|
|
ClientPermissionsCheckerNetPackVisitor checker(*srv);
|
|
|
|
ptr->visit(checker);
|
|
|
|
|
|
|
|
if(checker.getResult())
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-02-12 09:23:39 +02:00
|
|
|
ApplyOnServerNetPackVisitor applier(*srv);
|
|
|
|
ptr->visit(applier);
|
|
|
|
return applier.getResult();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void applyOnServerAfter(CVCMIServer * srv, void * pack) const override
|
|
|
|
{
|
|
|
|
T * ptr = static_cast<T *>(pack);
|
2023-02-12 09:23:39 +02:00
|
|
|
ApplyOnServerAfterAnnounceNetPackVisitor applier(*srv);
|
|
|
|
ptr->visit(applier);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class CApplyOnServer<CPack> : public CBaseForServerApply
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool applyOnServerBefore(CVCMIServer * srv, void * pack) const override
|
|
|
|
{
|
|
|
|
logGlobal->error("Cannot apply plain CPack!");
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void applyOnServerAfter(CVCMIServer * srv, void * pack) const override
|
|
|
|
{
|
|
|
|
logGlobal->error("Cannot apply plain CPack!");
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
class CVCMIServerPackVisitor : public VCMI_LIB_WRAP_NAMESPACE(ICPackVisitor)
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
CVCMIServer & handler;
|
|
|
|
std::shared_ptr<CGameHandler> gh;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CVCMIServerPackVisitor(CVCMIServer & handler, std::shared_ptr<CGameHandler> gh)
|
|
|
|
:handler(handler), gh(gh)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool callTyped() override { return false; }
|
|
|
|
|
|
|
|
virtual void visitForLobby(CPackForLobby & packForLobby) override
|
|
|
|
{
|
|
|
|
handler.handleReceivedPack(std::unique_ptr<CPackForLobby>(&packForLobby));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visitForServer(CPackForServer & serverPack) override
|
|
|
|
{
|
|
|
|
if (gh)
|
|
|
|
gh->handleReceivedPack(&serverPack);
|
|
|
|
else
|
|
|
|
logNetwork->error("Received pack for game server while in lobby!");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void visitForClient(CPackForClient & clientPack) override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-26 15:16:37 +02:00
|
|
|
std::string SERVER_NAME_AFFIX = "server";
|
|
|
|
std::string SERVER_NAME = GameConstants::VCMI_VERSION + std::string(" (") + SERVER_NAME_AFFIX + ')';
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
CVCMIServer::CVCMIServer(boost::program_options::variables_map & opts)
|
2023-11-18 16:34:18 +02:00
|
|
|
: state(EServerState::LOBBY), cmdLineOptions(opts), currentClientId(1), currentPlayerId(1), restartGameplay(false)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
uuid = boost::uuids::to_string(boost::uuids::random_generator()());
|
|
|
|
logNetwork->trace("CVCMIServer created! UUID: %s", uuid);
|
|
|
|
applier = std::make_shared<CApplier<CBaseForServerApply>>();
|
|
|
|
registerTypesLobbyPacks(*applier);
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
uint16_t port = 3030;
|
2018-01-05 19:21:07 +02:00
|
|
|
if(cmdLineOptions.count("port"))
|
2023-11-18 16:34:18 +02:00
|
|
|
port = cmdLineOptions["port"].as<uint16_t>();
|
2018-01-05 19:21:07 +02:00
|
|
|
logNetwork->info("Port %d will be used", port);
|
2023-11-18 16:34:18 +02:00
|
|
|
|
|
|
|
networkServer = std::make_unique<NetworkServer>(*this);
|
|
|
|
networkServer->start(port);
|
2018-01-05 19:21:07 +02:00
|
|
|
logNetwork->info("Listening for connections at port %d", port);
|
|
|
|
}
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
CVCMIServer::~CVCMIServer() = default;
|
|
|
|
|
|
|
|
void CVCMIServer::onNewConnection(const std::shared_ptr<NetworkConnection> & connection)
|
|
|
|
{
|
|
|
|
if (activeConnections.empty())
|
|
|
|
establishOutgoingConnection();
|
|
|
|
|
|
|
|
if(state == EServerState::LOBBY)
|
2023-12-25 21:23:27 +02:00
|
|
|
{
|
2023-12-26 16:29:06 +02:00
|
|
|
activeConnections.push_back(std::make_shared<CConnection>(connection));
|
2023-12-25 21:23:27 +02:00
|
|
|
activeConnections.back()->enterLobbyConnectionMode();
|
|
|
|
}
|
2023-12-26 16:29:06 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
networkServer->closeConnection(connection);
|
|
|
|
}
|
2023-11-18 16:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CVCMIServer::onPacketReceived(const std::shared_ptr<NetworkConnection> & connection, const std::vector<uint8_t> & message)
|
|
|
|
{
|
|
|
|
std::shared_ptr<CConnection> c = findConnection(connection);
|
2023-12-26 16:29:06 +02:00
|
|
|
auto pack = c->retrievePack(message);
|
2023-11-18 16:34:18 +02:00
|
|
|
pack->c = c;
|
|
|
|
CVCMIServerPackVisitor visitor(*this, this->gh);
|
|
|
|
pack->visit(visitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVCMIServer::onConnectionFailed(const std::string & errorMessage)
|
|
|
|
{
|
|
|
|
//TODO: handle failure to connect to lobby
|
|
|
|
}
|
|
|
|
|
2023-12-25 21:23:27 +02:00
|
|
|
void CVCMIServer::onConnectionEstablished(const std::shared_ptr<NetworkConnection> &)
|
2023-11-18 16:34:18 +02:00
|
|
|
{
|
|
|
|
//TODO: handle connection to lobby - login?
|
|
|
|
}
|
|
|
|
|
2023-08-22 17:45:13 +02:00
|
|
|
void CVCMIServer::setState(EServerState value)
|
|
|
|
{
|
2023-12-26 18:29:13 +02:00
|
|
|
state = value;
|
2023-08-22 17:45:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EServerState CVCMIServer::getState() const
|
|
|
|
{
|
2023-12-26 18:29:13 +02:00
|
|
|
return state;
|
2023-08-22 17:45:13 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
std::shared_ptr<CConnection> CVCMIServer::findConnection(const std::shared_ptr<NetworkConnection> & netConnection)
|
|
|
|
{
|
2023-12-25 21:23:27 +02:00
|
|
|
for (auto const & gameConnection : activeConnections)
|
|
|
|
{
|
|
|
|
if (gameConnection->isMyConnection(netConnection))
|
|
|
|
return gameConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error("Unknown connection received in CVCMIServer::findConnection");
|
2023-11-18 16:34:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void CVCMIServer::run()
|
|
|
|
{
|
2023-11-18 16:34:18 +02:00
|
|
|
#if defined(VCMI_ANDROID) && !defined(SINGLE_PROCESS_APP)
|
2018-01-05 19:21:07 +02:00
|
|
|
if(!restartGameplay)
|
|
|
|
{
|
2022-10-02 13:23:03 +02:00
|
|
|
CAndroidVMHelper vmHelper;
|
|
|
|
vmHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "onServerReady");
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
2023-11-18 16:34:18 +02:00
|
|
|
#endif
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
static const int serverUpdateIntervalMilliseconds = 50;
|
|
|
|
auto clockInitial = std::chrono::steady_clock::now();
|
|
|
|
int64_t msPassedLast = 0;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
while(state != EServerState::SHUTDOWN)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-11-18 16:34:18 +02:00
|
|
|
networkServer->run(std::chrono::milliseconds(serverUpdateIntervalMilliseconds));
|
|
|
|
|
|
|
|
const auto clockNow = std::chrono::steady_clock::now();
|
|
|
|
const auto clockPassed = clockNow - clockInitial;
|
|
|
|
const int64_t msPassedNow = std::chrono::duration_cast<std::chrono::milliseconds>(clockPassed).count();
|
|
|
|
const int64_t msDelta = msPassedNow - msPassedLast;
|
|
|
|
msPassedLast = msPassedNow;
|
|
|
|
|
|
|
|
if (state == EServerState::GAMEPLAY)
|
|
|
|
gh->tick(msDelta);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 18:29:13 +02:00
|
|
|
void CVCMIServer::onTimer()
|
|
|
|
{
|
|
|
|
// FIXME: move GameHandler updates here
|
|
|
|
}
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
void CVCMIServer::establishOutgoingConnection()
|
2022-10-25 03:27:53 +02:00
|
|
|
{
|
2023-11-18 16:34:18 +02:00
|
|
|
if(!cmdLineOptions.count("lobby"))
|
|
|
|
return;
|
2023-08-20 23:55:11 +02:00
|
|
|
|
2022-11-08 02:44:34 +02:00
|
|
|
uuid = cmdLineOptions["lobby-uuid"].as<std::string>();
|
2023-11-18 16:34:18 +02:00
|
|
|
auto address = cmdLineOptions["lobby"].as<std::string>();
|
|
|
|
int port = cmdLineOptions["lobby-port"].as<ui16>();
|
|
|
|
logNetwork->info("Establishing connection to remote at %s:%d with uuid %s", address, port, uuid);
|
2022-10-25 03:27:53 +02:00
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
outgoingConnection = std::make_unique<NetworkClient>(*this);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
outgoingConnection->start(address, port);//, SERVER_NAME, uuid);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
// connections.insert(c);
|
|
|
|
// remoteConnections.insert(c);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2022-09-29 19:08:05 +02:00
|
|
|
void CVCMIServer::prepareToRestart()
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(state == EServerState::GAMEPLAY)
|
|
|
|
{
|
|
|
|
restartGameplay = true;
|
2021-01-20 20:29:50 +02:00
|
|
|
* si = * gh->gs->initialOpts;
|
|
|
|
si->seedToBeUsed = si->seedPostInit = 0;
|
2018-01-05 19:21:07 +02:00
|
|
|
state = EServerState::LOBBY;
|
2023-08-11 15:53:02 +02:00
|
|
|
if (si->campState)
|
|
|
|
{
|
|
|
|
assert(si->campState->currentScenario().has_value());
|
|
|
|
campaignMap = si->campState->currentScenario().value_or(CampaignScenarioID(0));
|
|
|
|
campaignBonus = si->campState->getBonusID(campaignMap).value_or(-1);
|
|
|
|
}
|
2022-09-28 22:35:38 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
for(auto c : activeConnections)
|
2022-09-28 22:35:38 +02:00
|
|
|
c->enterLobbyConnectionMode();
|
2023-12-25 21:23:27 +02:00
|
|
|
|
2022-09-29 19:08:05 +02:00
|
|
|
boost::unique_lock<boost::recursive_mutex> queueLock(mx);
|
|
|
|
gh = nullptr;
|
|
|
|
}
|
2022-09-23 13:02:19 +02:00
|
|
|
|
2022-09-29 19:08:05 +02:00
|
|
|
bool CVCMIServer::prepareToStartGame()
|
|
|
|
{
|
2023-08-22 18:10:20 +02:00
|
|
|
Load::ProgressAccumulator progressTracking;
|
|
|
|
Load::Progress current(1);
|
|
|
|
progressTracking.include(current);
|
2024-01-17 14:50:00 +02:00
|
|
|
auto currentProgress = std::numeric_limits<Load::Type>::max();
|
2023-08-22 18:10:20 +02:00
|
|
|
|
2023-08-23 13:45:00 +02:00
|
|
|
auto progressTrackingThread = boost::thread([this, &progressTracking, ¤tProgress]()
|
2023-08-22 18:10:20 +02:00
|
|
|
{
|
|
|
|
while(!progressTracking.finished())
|
2023-08-21 03:06:58 +02:00
|
|
|
{
|
2023-08-23 13:45:00 +02:00
|
|
|
if(progressTracking.get() != currentProgress)
|
|
|
|
{
|
2023-11-18 16:34:18 +02:00
|
|
|
//FIXME: UNGUARDED MULTITHREADED ACCESS!!!
|
2023-08-23 13:45:00 +02:00
|
|
|
currentProgress = progressTracking.get();
|
|
|
|
std::unique_ptr<LobbyLoadProgress> loadProgress(new LobbyLoadProgress);
|
|
|
|
loadProgress->progress = currentProgress;
|
2023-11-18 16:34:18 +02:00
|
|
|
announcePack(std::move(loadProgress));
|
2023-08-23 13:45:00 +02:00
|
|
|
}
|
2023-08-22 18:10:20 +02:00
|
|
|
boost::this_thread::sleep(boost::posix_time::milliseconds(50));
|
2023-08-21 03:06:58 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-28 21:38:41 +02:00
|
|
|
gh = std::make_shared<CGameHandler>(this);
|
2018-01-05 19:21:07 +02:00
|
|
|
switch(si->mode)
|
|
|
|
{
|
|
|
|
case StartInfo::CAMPAIGN:
|
|
|
|
logNetwork->info("Preparing to start new campaign");
|
2023-10-27 17:10:54 +02:00
|
|
|
si->startTimeIso8601 = vstd::getDateTimeISO8601Basic(std::time(nullptr));
|
2023-09-30 20:03:33 +02:00
|
|
|
si->fileURI = mi->fileURI;
|
2023-06-25 23:01:25 +02:00
|
|
|
si->campState->setCurrentMap(campaignMap);
|
|
|
|
si->campState->setCurrentMapBonus(campaignBonus);
|
2023-08-21 03:06:58 +02:00
|
|
|
gh->init(si.get(), progressTracking);
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case StartInfo::NEW_GAME:
|
|
|
|
logNetwork->info("Preparing to start new game");
|
2023-10-27 17:10:54 +02:00
|
|
|
si->startTimeIso8601 = vstd::getDateTimeISO8601Basic(std::time(nullptr));
|
2023-09-30 20:03:33 +02:00
|
|
|
si->fileURI = mi->fileURI;
|
2023-08-21 03:06:58 +02:00
|
|
|
gh->init(si.get(), progressTracking);
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case StartInfo::LOAD_GAME:
|
|
|
|
logNetwork->info("Preparing to start loaded game");
|
2022-09-23 13:02:19 +02:00
|
|
|
if(!gh->load(si->mapname))
|
2023-08-22 18:10:20 +02:00
|
|
|
{
|
|
|
|
current.finish();
|
|
|
|
progressTrackingThread.join();
|
2022-09-23 13:02:19 +02:00
|
|
|
return false;
|
2023-08-22 18:10:20 +02:00
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logNetwork->error("Wrong mode in StartInfo!");
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
}
|
2022-09-23 13:02:19 +02:00
|
|
|
|
2023-08-22 18:10:20 +02:00
|
|
|
current.finish();
|
|
|
|
progressTrackingThread.join();
|
2023-08-21 03:06:58 +02:00
|
|
|
|
2022-09-23 13:02:19 +02:00
|
|
|
return true;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
void CVCMIServer::startGameImmediately()
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-11-18 16:34:18 +02:00
|
|
|
for(auto c : activeConnections)
|
2018-01-05 19:21:07 +02:00
|
|
|
c->enterGameplayConnectionMode(gh->gs);
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
gh->start(si->mode == StartInfo::LOAD_GAME);
|
2018-01-05 19:21:07 +02:00
|
|
|
state = EServerState::GAMEPLAY;
|
|
|
|
}
|
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
void CVCMIServer::onDisconnected(const std::shared_ptr<NetworkConnection> & connection)
|
2023-02-12 09:23:39 +02:00
|
|
|
{
|
2023-11-18 16:34:18 +02:00
|
|
|
logNetwork->error("Network error receiving a pack. Connection has been closed");
|
2023-02-12 09:23:39 +02:00
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
std::shared_ptr<CConnection> c = findConnection(connection);
|
2023-02-12 09:23:39 +02:00
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
inactiveConnections.push_back(c);
|
|
|
|
vstd::erase(activeConnections, c);
|
2023-02-12 09:23:39 +02:00
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
if(activeConnections.empty() || hostClientId == c->connectionID)
|
|
|
|
state = EServerState::SHUTDOWN;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
if(gh && state == EServerState::GAMEPLAY)
|
2023-06-27 15:27:46 +02:00
|
|
|
{
|
2023-11-18 16:34:18 +02:00
|
|
|
gh->handleClientDisconnection(c);
|
2023-06-27 15:27:46 +02:00
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
boost::unique_lock<boost::recursive_mutex> queueLock(mx);
|
2023-06-27 15:27:46 +02:00
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
// if(c->connected)
|
|
|
|
// {
|
|
|
|
// auto lcd = std::make_unique<LobbyClientDisconnected>();
|
|
|
|
// lcd->c = c;
|
|
|
|
// lcd->clientId = c->connectionID;
|
|
|
|
// handleReceivedPack(std::move(lcd));
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// logNetwork->info("Thread listening for %s ended", c->toString());
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 17:09:48 +02:00
|
|
|
void CVCMIServer::handleReceivedPack(std::unique_ptr<CPackForLobby> pack)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-11-08 22:05:36 +02:00
|
|
|
CBaseForServerApply * apply = applier->getApplier(CTypeList::getInstance().getTypeID(pack.get()));
|
2018-04-30 17:09:48 +02:00
|
|
|
if(apply->applyOnServerBefore(this, pack.get()))
|
2023-11-18 16:34:18 +02:00
|
|
|
announcePack(std::move(pack));
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 17:09:48 +02:00
|
|
|
void CVCMIServer::announcePack(std::unique_ptr<CPackForLobby> pack)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-11-18 16:34:18 +02:00
|
|
|
for(auto c : activeConnections)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2022-07-28 11:29:39 +02:00
|
|
|
// FIXME: we need to avoid sending something to client that not yet get answer for LobbyClientConnected
|
2018-01-05 19:21:07 +02:00
|
|
|
// Until UUID set we only pass LobbyClientConnected to this client
|
2023-12-25 21:23:27 +02:00
|
|
|
//if(c->uuid == uuid && !dynamic_cast<LobbyClientConnected *>(pack.get()))
|
|
|
|
// continue;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2018-04-30 17:09:48 +02:00
|
|
|
c->sendPack(pack.get());
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-11-08 22:05:36 +02:00
|
|
|
applier->getApplier(CTypeList::getInstance().getTypeID(pack.get()))->applyOnServerAfter(this, pack.get());
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 13:02:19 +02:00
|
|
|
void CVCMIServer::announceMessage(const std::string & txt)
|
|
|
|
{
|
|
|
|
logNetwork->info("Show message: %s", txt);
|
2022-12-07 23:36:20 +02:00
|
|
|
auto cm = std::make_unique<LobbyShowMessage>();
|
2022-09-23 13:02:19 +02:00
|
|
|
cm->message = txt;
|
2023-11-18 16:34:18 +02:00
|
|
|
announcePack(std::move(cm));
|
2022-09-23 13:02:19 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void CVCMIServer::announceTxt(const std::string & txt, const std::string & playerName)
|
|
|
|
{
|
|
|
|
logNetwork->info("%s says: %s", playerName, txt);
|
2022-12-07 23:36:20 +02:00
|
|
|
auto cm = std::make_unique<LobbyChatMessage>();
|
2018-01-05 19:21:07 +02:00
|
|
|
cm->playerName = playerName;
|
|
|
|
cm->message = txt;
|
2023-11-18 16:34:18 +02:00
|
|
|
announcePack(std::move(cm));
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CVCMIServer::passHost(int toConnectionId)
|
|
|
|
{
|
2023-11-18 16:34:18 +02:00
|
|
|
for(auto c : activeConnections)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(isClientHost(c->connectionID))
|
|
|
|
continue;
|
|
|
|
if(c->connectionID != toConnectionId)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
hostClientId = c->connectionID;
|
|
|
|
announceTxt(boost::str(boost::format("Pass host to connection %d") % toConnectionId));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVCMIServer::clientConnected(std::shared_ptr<CConnection> c, std::vector<std::string> & names, std::string uuid, StartInfo::EMode mode)
|
|
|
|
{
|
2023-12-26 18:29:13 +02:00
|
|
|
if(state != EServerState::LOBBY)
|
|
|
|
throw std::runtime_error("CVCMIServer::clientConnected called while game is not accepting clients!");
|
|
|
|
|
|
|
|
c->connectionID = currentClientId++;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
if(hostClientId == -1)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
hostClientId = c->connectionID;
|
|
|
|
si->mode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
logNetwork->info("Connection with client %d established. UUID: %s", c->connectionID, c->uuid);
|
2022-10-04 03:09:03 +02:00
|
|
|
|
2023-12-26 18:29:13 +02:00
|
|
|
for(auto & name : names)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-12-26 18:29:13 +02:00
|
|
|
logNetwork->info("Client %d player: %s", c->connectionID, name);
|
|
|
|
ui8 id = currentPlayerId++;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-12-26 18:29:13 +02:00
|
|
|
ClientPlayer cp;
|
|
|
|
cp.connection = c->connectionID;
|
|
|
|
cp.name = name;
|
|
|
|
playerNames.insert(std::make_pair(id, cp));
|
|
|
|
announceTxt(boost::str(boost::format("%s (pid %d cid %d) joins the game") % name % id % c->connectionID));
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-12-26 18:29:13 +02:00
|
|
|
//put new player in first slot with AI
|
|
|
|
for(auto & elem : si->playerInfos)
|
|
|
|
{
|
|
|
|
if(elem.second.isControlledByAI() && !elem.second.compOnly)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-12-26 18:29:13 +02:00
|
|
|
setPlayerConnectedId(elem.second, id);
|
|
|
|
break;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVCMIServer::clientDisconnected(std::shared_ptr<CConnection> c)
|
|
|
|
{
|
2023-11-18 16:34:18 +02:00
|
|
|
vstd::erase(activeConnections, c);
|
|
|
|
|
|
|
|
if(activeConnections.empty() || hostClientId == c->connectionID)
|
2022-09-30 21:08:37 +02:00
|
|
|
{
|
|
|
|
state = EServerState::SHUTDOWN;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-04 23:54:31 +02:00
|
|
|
PlayerReinitInterface startAiPack;
|
|
|
|
startAiPack.playerConnectionId = PlayerSettings::PLAYER_AI;
|
|
|
|
|
2018-08-26 18:09:56 +02:00
|
|
|
for(auto it = playerNames.begin(); it != playerNames.end();)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2018-08-26 18:09:56 +02:00
|
|
|
if(it->second.connection != c->connectionID)
|
|
|
|
{
|
2022-09-30 21:08:37 +02:00
|
|
|
++it;
|
2018-01-05 19:21:07 +02:00
|
|
|
continue;
|
2018-08-26 18:09:56 +02:00
|
|
|
}
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2018-08-26 18:09:56 +02:00
|
|
|
int id = it->first;
|
2022-09-30 21:08:37 +02:00
|
|
|
std::string playerLeftMsgText = boost::str(boost::format("%s (pid %d cid %d) left the game") % id % playerNames[id].name % c->connectionID);
|
|
|
|
announceTxt(playerLeftMsgText); //send lobby text, it will be ignored for non-lobby clients
|
|
|
|
auto * playerSettings = si->getPlayersSettings(id);
|
|
|
|
if(!playerSettings)
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
it = playerNames.erase(it);
|
|
|
|
setPlayerConnectedId(*playerSettings, PlayerSettings::PLAYER_AI);
|
|
|
|
|
|
|
|
if(gh && si && state == EServerState::GAMEPLAY)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-07-12 20:13:17 +02:00
|
|
|
gh->playerMessages->broadcastMessage(playerSettings->color, playerLeftMsgText);
|
2023-11-18 16:34:18 +02:00
|
|
|
// gh->connections[playerSettings->color].insert(hostClient);
|
2022-10-04 23:54:31 +02:00
|
|
|
startAiPack.players.push_back(playerSettings->color);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-04 23:54:31 +02:00
|
|
|
|
|
|
|
if(!startAiPack.players.empty())
|
|
|
|
gh->sendAndApply(&startAiPack);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2022-10-01 16:28:45 +02:00
|
|
|
void CVCMIServer::reconnectPlayer(int connId)
|
|
|
|
{
|
2022-10-04 23:54:31 +02:00
|
|
|
PlayerReinitInterface startAiPack;
|
|
|
|
startAiPack.playerConnectionId = connId;
|
|
|
|
|
2022-10-01 16:28:45 +02:00
|
|
|
if(gh && si && state == EServerState::GAMEPLAY)
|
|
|
|
{
|
|
|
|
for(auto it = playerNames.begin(); it != playerNames.end(); ++it)
|
|
|
|
{
|
|
|
|
if(it->second.connection != connId)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int id = it->first;
|
|
|
|
auto * playerSettings = si->getPlayersSettings(id);
|
|
|
|
if(!playerSettings)
|
|
|
|
continue;
|
|
|
|
|
2022-10-04 22:51:34 +02:00
|
|
|
std::string messageText = boost::str(boost::format("%s (cid %d) is connected") % playerSettings->name % connId);
|
2023-07-12 20:13:17 +02:00
|
|
|
gh->playerMessages->broadcastMessage(playerSettings->color, messageText);
|
2022-10-04 22:51:34 +02:00
|
|
|
|
2022-10-04 23:54:31 +02:00
|
|
|
startAiPack.players.push_back(playerSettings->color);
|
2022-10-01 16:28:45 +02:00
|
|
|
}
|
2022-10-04 23:54:31 +02:00
|
|
|
|
|
|
|
if(!startAiPack.players.empty())
|
|
|
|
gh->sendAndApply(&startAiPack);
|
2022-10-01 16:28:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void CVCMIServer::setPlayerConnectedId(PlayerSettings & pset, ui8 player) const
|
|
|
|
{
|
|
|
|
if(vstd::contains(playerNames, player))
|
|
|
|
pset.name = playerNames.find(player)->second.name;
|
|
|
|
else
|
|
|
|
pset.name = VLC->generaltexth->allTexts[468]; //Computer
|
|
|
|
|
|
|
|
pset.connectedPlayerIDs.clear();
|
|
|
|
if(player != PlayerSettings::PLAYER_AI)
|
|
|
|
pset.connectedPlayerIDs.insert(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVCMIServer::updateStartInfoOnMapChange(std::shared_ptr<CMapInfo> mapInfo, std::shared_ptr<CMapGenOptions> mapGenOpts)
|
|
|
|
{
|
|
|
|
mi = mapInfo;
|
|
|
|
if(!mi)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto namesIt = playerNames.cbegin();
|
|
|
|
si->playerInfos.clear();
|
|
|
|
if(mi->scenarioOptionsOfSave)
|
|
|
|
{
|
2018-04-30 17:09:48 +02:00
|
|
|
si = CMemorySerializer::deepCopy(*mi->scenarioOptionsOfSave);
|
2018-01-05 19:21:07 +02:00
|
|
|
si->mode = StartInfo::LOAD_GAME;
|
|
|
|
if(si->campState)
|
2023-06-25 23:01:25 +02:00
|
|
|
campaignMap = si->campState->currentScenario().value();
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
for(auto & ps : si->playerInfos)
|
|
|
|
{
|
|
|
|
if(!ps.second.compOnly && ps.second.connectedPlayerIDs.size() && namesIt != playerNames.cend())
|
|
|
|
{
|
|
|
|
setPlayerConnectedId(ps.second, namesIt++->first);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setPlayerConnectedId(ps.second, PlayerSettings::PLAYER_AI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(si->mode == StartInfo::NEW_GAME || si->mode == StartInfo::CAMPAIGN)
|
|
|
|
{
|
2023-06-26 00:07:55 +02:00
|
|
|
if(mi->campaign)
|
2018-01-05 19:21:07 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
for(int i = 0; i < mi->mapHeader->players.size(); i++)
|
|
|
|
{
|
|
|
|
const PlayerInfo & pinfo = mi->mapHeader->players[i];
|
|
|
|
|
|
|
|
//neither computer nor human can play - no player
|
|
|
|
if(!(pinfo.canHumanPlay || pinfo.canComputerPlay))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
PlayerSettings & pset = si->playerInfos[PlayerColor(i)];
|
|
|
|
pset.color = PlayerColor(i);
|
|
|
|
if(pinfo.canHumanPlay && namesIt != playerNames.cend())
|
|
|
|
{
|
|
|
|
setPlayerConnectedId(pset, namesIt++->first);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setPlayerConnectedId(pset, PlayerSettings::PLAYER_AI);
|
|
|
|
if(!pinfo.canHumanPlay)
|
|
|
|
{
|
|
|
|
pset.compOnly = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pset.castle = pinfo.defaultCastle();
|
|
|
|
pset.hero = pinfo.defaultHero();
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(pset.hero != HeroTypeID::RANDOM && pinfo.hasCustomMainHero())
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
pset.hero = pinfo.mainCustomHeroId;
|
2023-09-28 00:04:05 +02:00
|
|
|
pset.heroNameTextId = pinfo.mainCustomHeroNameTextId;
|
2018-01-05 19:21:07 +02:00
|
|
|
pset.heroPortrait = pinfo.mainCustomHeroPortrait;
|
|
|
|
}
|
|
|
|
|
|
|
|
pset.handicap = PlayerSettings::NO_HANDICAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mi->isRandomMap && mapGenOpts)
|
|
|
|
si->mapGenOptions = std::shared_ptr<CMapGenOptions>(mapGenOpts);
|
|
|
|
else
|
|
|
|
si->mapGenOptions.reset();
|
|
|
|
}
|
|
|
|
si->mapname = mi->fileURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVCMIServer::updateAndPropagateLobbyState()
|
|
|
|
{
|
|
|
|
// Update player settings for RMG
|
|
|
|
// TODO: find appropriate location for this code
|
|
|
|
if(si->mapGenOptions && si->mode == StartInfo::NEW_GAME)
|
|
|
|
{
|
|
|
|
for(const auto & psetPair : si->playerInfos)
|
|
|
|
{
|
|
|
|
const auto & pset = psetPair.second;
|
|
|
|
si->mapGenOptions->setStartingTownForPlayer(pset.color, pset.castle);
|
2024-01-13 14:08:36 +02:00
|
|
|
si->mapGenOptions->setStartingHeroForPlayer(pset.color, pset.hero);
|
2018-01-05 19:21:07 +02:00
|
|
|
if(pset.isControlledByHuman())
|
|
|
|
{
|
|
|
|
si->mapGenOptions->setPlayerTypeForStandardPlayer(pset.color, EPlayerType::HUMAN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 23:36:20 +02:00
|
|
|
auto lus = std::make_unique<LobbyUpdateState>();
|
2018-01-05 19:21:07 +02:00
|
|
|
lus->state = *this;
|
2023-11-18 16:34:18 +02:00
|
|
|
announcePack(std::move(lus));
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CVCMIServer::setPlayer(PlayerColor clickedColor)
|
|
|
|
{
|
|
|
|
struct PlayerToRestore
|
|
|
|
{
|
|
|
|
PlayerColor color;
|
|
|
|
int id;
|
|
|
|
void reset() { id = -1; color = PlayerColor::CANNOT_DETERMINE; }
|
|
|
|
PlayerToRestore(){ reset(); }
|
|
|
|
} playerToRestore;
|
|
|
|
|
|
|
|
|
|
|
|
PlayerSettings & clicked = si->playerInfos[clickedColor];
|
|
|
|
|
|
|
|
//identify clicked player
|
|
|
|
int clickedNameID = 0; //number of player - zero means AI, assume it initially
|
|
|
|
if(clicked.isControlledByHuman())
|
|
|
|
clickedNameID = *(clicked.connectedPlayerIDs.begin()); //if not AI - set appropiate ID
|
|
|
|
|
|
|
|
if(clickedNameID > 0 && playerToRestore.id == clickedNameID) //player to restore is about to being replaced -> put him back to the old place
|
|
|
|
{
|
|
|
|
PlayerSettings & restPos = si->playerInfos[playerToRestore.color];
|
|
|
|
setPlayerConnectedId(restPos, playerToRestore.id);
|
|
|
|
playerToRestore.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
int newPlayer; //which player will take clicked position
|
|
|
|
|
|
|
|
//who will be put here?
|
|
|
|
if(!clickedNameID) //AI player clicked -> if possible replace computer with unallocated player
|
|
|
|
{
|
|
|
|
newPlayer = getIdOfFirstUnallocatedPlayer();
|
|
|
|
if(!newPlayer) //no "free" player -> get just first one
|
|
|
|
newPlayer = playerNames.begin()->first;
|
|
|
|
}
|
|
|
|
else //human clicked -> take next
|
|
|
|
{
|
|
|
|
auto i = playerNames.find(clickedNameID); //clicked one
|
|
|
|
i++; //player AFTER clicked one
|
|
|
|
|
|
|
|
if(i != playerNames.end())
|
|
|
|
newPlayer = i->first;
|
|
|
|
else
|
|
|
|
newPlayer = 0; //AI if we scrolled through all players
|
|
|
|
}
|
|
|
|
|
|
|
|
setPlayerConnectedId(clicked, newPlayer); //put player
|
|
|
|
|
|
|
|
//if that player was somewhere else, we need to replace him with computer
|
|
|
|
if(newPlayer) //not AI
|
|
|
|
{
|
|
|
|
for(auto i = si->playerInfos.begin(); i != si->playerInfos.end(); i++)
|
|
|
|
{
|
|
|
|
int curNameID = *(i->second.connectedPlayerIDs.begin());
|
|
|
|
if(i->first != clickedColor && curNameID == newPlayer)
|
|
|
|
{
|
|
|
|
assert(i->second.connectedPlayerIDs.size());
|
|
|
|
playerToRestore.color = i->first;
|
|
|
|
playerToRestore.id = newPlayer;
|
|
|
|
setPlayerConnectedId(i->second, PlayerSettings::PLAYER_AI); //set computer
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 23:04:35 +02:00
|
|
|
void CVCMIServer::setPlayerName(PlayerColor color, std::string name)
|
|
|
|
{
|
2023-10-13 23:50:01 +02:00
|
|
|
if(color == PlayerColor::CANNOT_DETERMINE)
|
|
|
|
return;
|
2023-10-13 23:04:35 +02:00
|
|
|
|
2023-10-13 23:50:01 +02:00
|
|
|
PlayerSettings & player = si->playerInfos.at(color);
|
2023-10-13 23:04:35 +02:00
|
|
|
|
2023-10-13 23:50:01 +02:00
|
|
|
if(!player.isControlledByHuman())
|
|
|
|
return;
|
|
|
|
|
2023-10-16 21:24:59 +02:00
|
|
|
if(player.connectedPlayerIDs.empty())
|
2023-10-13 23:50:01 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
int nameID = *(player.connectedPlayerIDs.begin()); //if not AI - set appropiate ID
|
|
|
|
|
|
|
|
playerNames[nameID].name = name;
|
|
|
|
setPlayerConnectedId(player, nameID);
|
2023-10-13 23:04:35 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 19:21:07 +02:00
|
|
|
void CVCMIServer::optionNextCastle(PlayerColor player, int dir)
|
|
|
|
{
|
|
|
|
PlayerSettings & s = si->playerInfos[player];
|
2023-07-11 14:16:02 +02:00
|
|
|
FactionID & cur = s.castle;
|
2023-09-28 18:43:04 +02:00
|
|
|
auto & allowed = getPlayerInfo(player).allowedFactions;
|
|
|
|
const bool allowRandomTown = getPlayerInfo(player).isFactionRandom;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(cur == FactionID::NONE) //no change
|
2018-01-05 19:21:07 +02:00
|
|
|
return;
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(cur == FactionID::RANDOM) //first/last available
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(dir > 0)
|
|
|
|
cur = *allowed.begin(); //id of first town
|
|
|
|
else
|
|
|
|
cur = *allowed.rbegin(); //id of last town
|
|
|
|
|
|
|
|
}
|
|
|
|
else // next/previous available
|
|
|
|
{
|
|
|
|
if((cur == *allowed.begin() && dir < 0) || (cur == *allowed.rbegin() && dir > 0))
|
|
|
|
{
|
|
|
|
if(allowRandomTown)
|
|
|
|
{
|
2023-09-28 18:43:04 +02:00
|
|
|
cur = FactionID::RANDOM;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(dir > 0)
|
|
|
|
cur = *allowed.begin();
|
|
|
|
else
|
|
|
|
cur = *allowed.rbegin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(dir >= -1 && dir <= 1); //othervice std::advance may go out of range
|
2023-07-11 14:16:02 +02:00
|
|
|
auto iter = allowed.find(cur);
|
2018-01-05 19:21:07 +02:00
|
|
|
std::advance(iter, dir);
|
|
|
|
cur = *iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(s.hero.isValid() && !getPlayerInfo(player).hasCustomMainHero()) // remove hero unless it set to fixed one in map editor
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-09-28 18:43:04 +02:00
|
|
|
s.hero = HeroTypeID::RANDOM;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
2023-09-28 18:43:04 +02:00
|
|
|
if(!cur.isValid() && s.bonus == PlayerStartingBonus::RESOURCE)
|
|
|
|
s.bonus = PlayerStartingBonus::RANDOM;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
void CVCMIServer::optionSetCastle(PlayerColor player, FactionID id)
|
2023-08-14 00:08:48 +02:00
|
|
|
{
|
|
|
|
PlayerSettings & s = si->playerInfos[player];
|
|
|
|
FactionID & cur = s.castle;
|
2023-09-28 18:43:04 +02:00
|
|
|
auto & allowed = getPlayerInfo(player).allowedFactions;
|
2023-08-14 00:08:48 +02:00
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(cur == FactionID::NONE) //no change
|
2023-08-14 00:08:48 +02:00
|
|
|
return;
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(allowed.find(id) == allowed.end() && id != FactionID::RANDOM) // valid id
|
2023-08-14 00:08:48 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
cur = static_cast<FactionID>(id);
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(s.hero.isValid() && !getPlayerInfo(player).hasCustomMainHero()) // remove hero unless it set to fixed one in map editor
|
2023-08-14 00:08:48 +02:00
|
|
|
{
|
2023-09-28 18:43:04 +02:00
|
|
|
s.hero = HeroTypeID::RANDOM;
|
2023-08-14 00:08:48 +02:00
|
|
|
}
|
2023-09-28 18:43:04 +02:00
|
|
|
if(!cur.isValid() && s.bonus == PlayerStartingBonus::RESOURCE)
|
|
|
|
s.bonus = PlayerStartingBonus::RANDOM;
|
2023-08-14 00:08:48 +02:00
|
|
|
}
|
|
|
|
|
2023-06-25 20:16:03 +02:00
|
|
|
void CVCMIServer::setCampaignMap(CampaignScenarioID mapId)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
campaignMap = mapId;
|
2023-06-25 23:01:25 +02:00
|
|
|
si->difficulty = si->campState->scenario(mapId).difficulty;
|
2018-01-05 19:21:07 +02:00
|
|
|
campaignBonus = -1;
|
|
|
|
updateStartInfoOnMapChange(si->campState->getMapInfo(mapId));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVCMIServer::setCampaignBonus(int bonusId)
|
|
|
|
{
|
|
|
|
campaignBonus = bonusId;
|
|
|
|
|
2023-06-25 23:01:25 +02:00
|
|
|
const CampaignScenario & scenario = si->campState->scenario(campaignMap);
|
2023-06-25 20:16:03 +02:00
|
|
|
const std::vector<CampaignBonus> & bonDescs = scenario.travelOptions.bonusesToChoose;
|
|
|
|
if(bonDescs[bonusId].type == CampaignBonusType::HERO)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
for(auto & elem : si->playerInfos)
|
|
|
|
{
|
|
|
|
if(elem.first == PlayerColor(bonDescs[bonusId].info1))
|
|
|
|
setPlayerConnectedId(elem.second, 1);
|
|
|
|
else
|
|
|
|
setPlayerConnectedId(elem.second, PlayerSettings::PLAYER_AI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CVCMIServer::optionNextHero(PlayerColor player, int dir)
|
|
|
|
{
|
|
|
|
PlayerSettings & s = si->playerInfos[player];
|
2023-09-28 18:43:04 +02:00
|
|
|
if(!s.castle.isValid() || s.hero == HeroTypeID::NONE)
|
2018-01-05 19:21:07 +02:00
|
|
|
return;
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(s.hero == HeroTypeID::RANDOM) // first/last available
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-09-28 18:43:04 +02:00
|
|
|
if (dir > 0)
|
|
|
|
s.hero = nextAllowedHero(player, HeroTypeID(-1), dir);
|
|
|
|
else
|
|
|
|
s.hero = nextAllowedHero(player, HeroTypeID(VLC->heroh->size()), dir);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-28 18:43:04 +02:00
|
|
|
s.hero = nextAllowedHero(player, s.hero, dir);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
void CVCMIServer::optionSetHero(PlayerColor player, HeroTypeID id)
|
2023-08-14 00:08:48 +02:00
|
|
|
{
|
|
|
|
PlayerSettings & s = si->playerInfos[player];
|
2023-09-28 18:43:04 +02:00
|
|
|
if(!s.castle.isValid() || s.hero == HeroTypeID::NONE)
|
2023-08-14 00:08:48 +02:00
|
|
|
return;
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(id == HeroTypeID::RANDOM)
|
2023-08-14 00:08:48 +02:00
|
|
|
{
|
2023-09-28 18:43:04 +02:00
|
|
|
s.hero = HeroTypeID::RANDOM;
|
2023-08-14 00:08:48 +02:00
|
|
|
}
|
|
|
|
if(canUseThisHero(player, id))
|
|
|
|
s.hero = static_cast<HeroTypeID>(id);
|
|
|
|
}
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
HeroTypeID CVCMIServer::nextAllowedHero(PlayerColor player, HeroTypeID initial, int direction)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-09-28 18:43:04 +02:00
|
|
|
HeroTypeID first(initial.getNum() + direction);
|
|
|
|
|
|
|
|
if(direction > 0)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-09-28 18:43:04 +02:00
|
|
|
for (auto i = first; i.getNum() < VLC->heroh->size(); ++i)
|
2018-01-05 19:21:07 +02:00
|
|
|
if(canUseThisHero(player, i))
|
2023-09-28 18:43:04 +02:00
|
|
|
return i;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-28 18:43:04 +02:00
|
|
|
for (auto i = first; i.getNum() >= 0; --i)
|
2018-01-05 19:21:07 +02:00
|
|
|
if(canUseThisHero(player, i))
|
2023-09-28 18:43:04 +02:00
|
|
|
return i;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
2023-09-28 18:43:04 +02:00
|
|
|
return HeroTypeID::RANDOM;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CVCMIServer::optionNextBonus(PlayerColor player, int dir)
|
|
|
|
{
|
|
|
|
PlayerSettings & s = si->playerInfos[player];
|
2023-09-28 18:43:04 +02:00
|
|
|
PlayerStartingBonus & ret = s.bonus = static_cast<PlayerStartingBonus>(static_cast<int>(s.bonus) + dir);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(s.hero == HeroTypeID::NONE &&
|
|
|
|
!getPlayerInfo(player).heroesNames.size() &&
|
|
|
|
ret == PlayerStartingBonus::ARTIFACT) //no hero - can't be artifact
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(dir < 0)
|
2023-09-28 18:43:04 +02:00
|
|
|
ret = PlayerStartingBonus::RANDOM;
|
2018-01-05 19:21:07 +02:00
|
|
|
else
|
2023-09-28 18:43:04 +02:00
|
|
|
ret = PlayerStartingBonus::GOLD;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(ret > PlayerStartingBonus::RESOURCE)
|
|
|
|
ret = PlayerStartingBonus::RANDOM;
|
|
|
|
if(ret < PlayerStartingBonus::RANDOM)
|
|
|
|
ret = PlayerStartingBonus::RESOURCE;
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(s.castle == FactionID::RANDOM && ret == PlayerStartingBonus::RESOURCE) //random castle - can't be resource
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
if(dir < 0)
|
2023-09-28 18:43:04 +02:00
|
|
|
ret = PlayerStartingBonus::GOLD;
|
2018-01-05 19:21:07 +02:00
|
|
|
else
|
2023-09-28 18:43:04 +02:00
|
|
|
ret = PlayerStartingBonus::RANDOM;
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
void CVCMIServer::optionSetBonus(PlayerColor player, PlayerStartingBonus id)
|
2023-08-14 00:08:48 +02:00
|
|
|
{
|
|
|
|
PlayerSettings & s = si->playerInfos[player];
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(s.hero == HeroTypeID::NONE &&
|
|
|
|
!getPlayerInfo(player).heroesNames.size() &&
|
|
|
|
id == PlayerStartingBonus::ARTIFACT) //no hero - can't be artifact
|
2023-08-14 00:08:48 +02:00
|
|
|
return;
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(id > PlayerStartingBonus::RESOURCE)
|
2023-08-14 00:08:48 +02:00
|
|
|
return;
|
2023-09-28 18:43:04 +02:00
|
|
|
if(id < PlayerStartingBonus::RANDOM)
|
2023-08-14 00:08:48 +02:00
|
|
|
return;
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(s.castle == FactionID::RANDOM && id == PlayerStartingBonus::RESOURCE) //random castle - can't be resource
|
2023-08-14 00:08:48 +02:00
|
|
|
return;
|
|
|
|
|
2024-01-16 21:02:39 +02:00
|
|
|
s.bonus = id;
|
2023-08-14 00:08:48 +02:00
|
|
|
}
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
bool CVCMIServer::canUseThisHero(PlayerColor player, HeroTypeID ID)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
return VLC->heroh->size() > ID
|
|
|
|
&& si->playerInfos[player].castle == VLC->heroh->objects[ID]->heroClass->faction
|
2018-01-05 19:21:07 +02:00
|
|
|
&& !vstd::contains(getUsedHeroes(), ID)
|
2023-11-05 16:35:51 +02:00
|
|
|
&& mi->mapHeader->allowedHeroes.count(ID);
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
std::vector<HeroTypeID> CVCMIServer::getUsedHeroes()
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-09-28 18:43:04 +02:00
|
|
|
std::vector<HeroTypeID> heroIds;
|
2018-01-05 19:21:07 +02:00
|
|
|
for(auto & p : si->playerInfos)
|
|
|
|
{
|
2023-09-28 18:43:04 +02:00
|
|
|
const auto & heroes = getPlayerInfo(p.first).heroesNames;
|
2018-01-05 19:21:07 +02:00
|
|
|
for(auto & hero : heroes)
|
|
|
|
if(hero.heroId >= 0) //in VCMI map format heroId = -1 means random hero
|
|
|
|
heroIds.push_back(hero.heroId);
|
|
|
|
|
2023-09-28 18:43:04 +02:00
|
|
|
if(p.second.hero != HeroTypeID::RANDOM)
|
2018-01-05 19:21:07 +02:00
|
|
|
heroIds.push_back(p.second.hero);
|
|
|
|
}
|
|
|
|
return heroIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui8 CVCMIServer::getIdOfFirstUnallocatedPlayer() const
|
|
|
|
{
|
|
|
|
for(auto i = playerNames.cbegin(); i != playerNames.cend(); i++)
|
|
|
|
{
|
|
|
|
if(!si->getPlayersSettings(i->first))
|
|
|
|
return i->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-26 11:18:24 +02:00
|
|
|
static void handleCommandOptions(int argc, const char * argv[], boost::program_options::variables_map & options)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
po::options_description opts("Allowed options");
|
|
|
|
opts.add_options()
|
|
|
|
("help,h", "display help and exit")
|
|
|
|
("version,v", "display version information and exit")
|
|
|
|
("run-by-client", "indicate that server launched by client on same machine")
|
|
|
|
("uuid", po::value<std::string>(), "")
|
2022-11-08 02:44:34 +02:00
|
|
|
("port", po::value<ui16>(), "port at which server will listen to connections from client")
|
|
|
|
("lobby", po::value<std::string>(), "address to remote lobby")
|
|
|
|
("lobby-port", po::value<ui16>(), "port at which server connect to remote lobby")
|
|
|
|
("lobby-uuid", po::value<std::string>(), "")
|
2022-11-13 23:48:13 +02:00
|
|
|
("connections", po::value<ui16>(), "amount of connections to remote lobby");
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
if(argc > 1)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
po::store(po::parse_command_line(argc, argv, opts), options);
|
|
|
|
}
|
2023-07-28 13:17:06 +02:00
|
|
|
catch(boost::program_options::error & e)
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
|
|
|
std::cerr << "Failure during parsing command-line options:\n" << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2022-12-18 22:21:24 +02:00
|
|
|
|
|
|
|
#ifdef SINGLE_PROCESS_APP
|
|
|
|
options.emplace("run-by-client", po::variable_value{true, true});
|
|
|
|
#endif
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
po::notify(options);
|
2022-07-28 11:40:06 +02:00
|
|
|
|
2022-09-16 15:43:21 +02:00
|
|
|
#ifndef SINGLE_PROCESS_APP
|
2018-01-05 19:21:07 +02:00
|
|
|
if(options.count("help"))
|
|
|
|
{
|
2023-10-27 17:10:54 +02:00
|
|
|
auto time = std::time(nullptr);
|
2018-01-05 19:21:07 +02:00
|
|
|
printf("%s - A Heroes of Might and Magic 3 clone\n", GameConstants::VCMI_VERSION.c_str());
|
|
|
|
printf("Copyright (C) 2007-%d VCMI dev team - see AUTHORS file\n", std::localtime(&time)->tm_year + 1900);
|
|
|
|
printf("This is free software; see the source for copying conditions. There is NO\n");
|
|
|
|
printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
|
|
|
|
printf("\n");
|
|
|
|
std::cout << opts;
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options.count("version"))
|
|
|
|
{
|
|
|
|
printf("%s\n", GameConstants::VCMI_VERSION.c_str());
|
|
|
|
std::cout << VCMIDirs::get().genHelpString();
|
|
|
|
exit(0);
|
|
|
|
}
|
2022-07-26 16:39:07 +02:00
|
|
|
#endif
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2022-09-16 15:43:21 +02:00
|
|
|
#ifdef SINGLE_PROCESS_APP
|
2021-03-05 18:26:35 +02:00
|
|
|
#define main server_main
|
|
|
|
#endif
|
2023-02-26 11:18:24 +02:00
|
|
|
|
|
|
|
#if VCMI_ANDROID_DUAL_PROCESS
|
2023-01-28 12:08:37 +02:00
|
|
|
void CVCMIServer::create()
|
|
|
|
{
|
|
|
|
const int argc = 1;
|
2023-02-26 11:18:24 +02:00
|
|
|
const char * argv[argc] = { "android-server" };
|
2023-01-28 12:08:37 +02:00
|
|
|
#else
|
2023-02-26 11:18:24 +02:00
|
|
|
int main(int argc, const char * argv[])
|
2018-01-05 19:21:07 +02:00
|
|
|
{
|
2023-01-28 12:08:37 +02:00
|
|
|
#endif
|
|
|
|
|
2022-12-05 21:36:02 +02:00
|
|
|
#if !defined(VCMI_ANDROID) && !defined(SINGLE_PROCESS_APP)
|
2018-01-05 19:21:07 +02:00
|
|
|
// Correct working dir executable folder (not bundle folder) so we can use executable relative paths
|
|
|
|
boost::filesystem::current_path(boost::filesystem::system_complete(argv[0]).parent_path());
|
|
|
|
#endif
|
|
|
|
|
2022-09-27 06:05:10 +02:00
|
|
|
#ifndef VCMI_IOS
|
2018-01-05 19:21:07 +02:00
|
|
|
console = new CConsoleHandler();
|
2022-09-27 06:05:10 +02:00
|
|
|
#endif
|
2022-09-17 15:56:01 +02:00
|
|
|
CBasicLogConfigurator logConfig(VCMIDirs::get().userLogsPath() / "VCMI_Server_log.txt", console);
|
2018-01-05 19:21:07 +02:00
|
|
|
logConfig.configureDefault();
|
2022-07-26 15:16:37 +02:00
|
|
|
logGlobal->info(SERVER_NAME);
|
2018-01-05 19:21:07 +02:00
|
|
|
|
|
|
|
boost::program_options::variables_map opts;
|
|
|
|
handleCommandOptions(argc, argv, opts);
|
2024-01-04 23:48:58 +02:00
|
|
|
preinitDLL(console, false);
|
2018-01-05 19:21:07 +02:00
|
|
|
logConfig.configure();
|
|
|
|
|
|
|
|
loadDLLClasses();
|
|
|
|
srand((ui32)time(nullptr));
|
2022-07-26 16:39:07 +02:00
|
|
|
|
2023-12-26 18:29:13 +02:00
|
|
|
CVCMIServer server(opts);
|
|
|
|
|
2022-09-16 15:43:21 +02:00
|
|
|
#ifdef SINGLE_PROCESS_APP
|
2023-02-26 11:18:24 +02:00
|
|
|
boost::condition_variable * cond = reinterpret_cast<boost::condition_variable *>(const_cast<char *>(argv[0]));
|
2022-07-26 16:39:07 +02:00
|
|
|
cond->notify_one();
|
2022-07-26 15:16:37 +02:00
|
|
|
#endif
|
2022-07-26 16:39:07 +02:00
|
|
|
|
2023-11-18 16:34:18 +02:00
|
|
|
server.run();
|
2018-01-05 19:21:07 +02:00
|
|
|
|
2023-02-26 11:18:24 +02:00
|
|
|
#if VCMI_ANDROID_DUAL_PROCESS
|
2018-01-05 19:21:07 +02:00
|
|
|
CAndroidVMHelper envHelper;
|
|
|
|
envHelper.callStaticVoidMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "killServer");
|
|
|
|
#endif
|
2019-05-10 11:35:28 +02:00
|
|
|
logConfig.deconfigure();
|
2018-01-05 19:21:07 +02:00
|
|
|
vstd::clear_pointer(VLC);
|
2023-01-28 12:08:37 +02:00
|
|
|
|
2023-02-26 11:18:24 +02:00
|
|
|
#if !VCMI_ANDROID_DUAL_PROCESS
|
2018-01-05 19:21:07 +02:00
|
|
|
return 0;
|
2023-01-28 12:08:37 +02:00
|
|
|
#endif
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-02-26 11:18:24 +02:00
|
|
|
#if VCMI_ANDROID_DUAL_PROCESS
|
2023-01-25 08:59:59 +02:00
|
|
|
extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_createServer(JNIEnv * env, jclass cls)
|
|
|
|
{
|
|
|
|
__android_log_write(ANDROID_LOG_INFO, "VCMI", "Got jni call to init server");
|
|
|
|
CAndroidVMHelper::cacheVM(env);
|
|
|
|
|
2023-01-28 12:08:37 +02:00
|
|
|
CVCMIServer::create();
|
2018-01-05 19:21:07 +02:00
|
|
|
}
|
2023-01-25 08:59:59 +02:00
|
|
|
|
2023-02-26 09:39:36 +02:00
|
|
|
extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_initClassloader(JNIEnv * baseEnv, jclass cls)
|
|
|
|
{
|
|
|
|
CAndroidVMHelper::initClassloader(baseEnv);
|
|
|
|
}
|
2022-09-16 15:43:21 +02:00
|
|
|
#elif defined(SINGLE_PROCESS_APP)
|
2022-11-08 16:05:47 +02:00
|
|
|
void CVCMIServer::create(boost::condition_variable * cond, const std::vector<std::string> & args)
|
2022-07-26 15:16:37 +02:00
|
|
|
{
|
2022-11-08 16:05:47 +02:00
|
|
|
std::vector<const void *> argv = {cond};
|
|
|
|
for(auto & a : args)
|
|
|
|
argv.push_back(a.c_str());
|
2023-02-26 11:18:24 +02:00
|
|
|
main(argv.size(), reinterpret_cast<const char **>(&*argv.begin()));
|
2022-07-26 15:16:37 +02:00
|
|
|
}
|
2023-02-26 11:19:24 +02:00
|
|
|
|
|
|
|
#ifdef VCMI_ANDROID
|
|
|
|
void CVCMIServer::reuseClientJNIEnv(void * jniEnv)
|
|
|
|
{
|
|
|
|
CAndroidVMHelper::initClassloader(jniEnv);
|
|
|
|
CAndroidVMHelper::alwaysUseLoadedClass = true;
|
|
|
|
}
|
|
|
|
#endif // VCMI_ANDROID
|
|
|
|
#endif // VCMI_ANDROID_DUAL_PROCESS
|