1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 16:49:55 +00:00
openmw-tes3mp/apps/openmw/mwmp/Main.cpp

267 lines
7.1 KiB
C++
Raw Normal View History

2017-04-18 15:51:40 +00:00
#include <cstdlib>
#include <components/openmw-mp/Utils.hpp>
#include <components/openmw-mp/TimedLog.hpp>
2017-04-18 15:51:40 +00:00
#include <components/openmw-mp/Version.hpp>
#include <components/esm/esmwriter.hpp>
#include <components/files/configurationmanager.hpp>
2017-05-28 10:26:48 +00:00
#include <components/files/escape.hpp>
2017-04-18 15:51:40 +00:00
#include "../mwbase/environment.hpp"
2017-04-18 15:51:40 +00:00
#include "../mwclass/creature.hpp"
#include "../mwclass/npc.hpp"
#include "../mwdialogue/dialoguemanagerimp.hpp"
#include "../mwgui/windowmanagerimp.hpp"
2017-04-18 15:51:40 +00:00
#include "../mwinput/inputmanagerimp.hpp"
#include "../mwmechanics/aitravel.hpp"
#include "../mwmechanics/creaturestats.hpp"
#include "../mwmechanics/mechanicsmanagerimp.hpp"
2017-04-18 15:51:40 +00:00
#include "../mwmechanics/spellcasting.hpp"
#include "../mwscript/scriptmanagerimp.hpp"
#include "../mwstate/statemanagerimp.hpp"
#include "../mwworld/cellstore.hpp"
2017-04-18 15:51:40 +00:00
#include "../mwworld/customdata.hpp"
2016-11-17 15:16:25 +00:00
#include "../mwworld/inventorystore.hpp"
2017-04-18 15:51:40 +00:00
#include "../mwworld/manualref.hpp"
#include "../mwworld/player.hpp"
#include "../mwworld/ptr.hpp"
#include "../mwworld/worldimp.hpp"
2017-04-18 15:51:40 +00:00
#include "Main.hpp"
2016-12-16 08:59:15 +00:00
#include "Networking.hpp"
#include "LocalSystem.hpp"
#include "LocalPlayer.hpp"
2016-12-16 08:59:15 +00:00
#include "DedicatedPlayer.hpp"
#include "PlayerList.hpp"
2016-12-16 08:59:15 +00:00
#include "GUIController.hpp"
#include "CellController.hpp"
#include "MechanicsHelper.hpp"
#include "RecordHelper.hpp"
using namespace mwmp;
Main *Main::pMain = 0;
std::string Main::address = "";
std::string Main::serverPassword = TES3MP_DEFAULT_PASSW;
2017-05-28 10:26:48 +00:00
std::string Main::resourceDir = "";
std::string Main::getResDir()
{
return resourceDir;
}
std::string loadSettings(Settings::Manager& settings)
{
Files::ConfigurationManager mCfgMgr;
// Create the settings manager and load default settings file
const std::string localdefault = (mCfgMgr.getLocalPath() / "tes3mp-client-default.cfg").string();
const std::string globaldefault = (mCfgMgr.getGlobalPath() / "tes3mp-client-default.cfg").string();
// prefer local
if (boost::filesystem::exists(localdefault))
settings.loadDefault(localdefault, false);
else if (boost::filesystem::exists(globaldefault))
settings.loadDefault(globaldefault, false);
else
throw std::runtime_error ("No default settings file found! Make sure the file \"tes3mp-client-default.cfg\" was properly installed.");
// load user settings if they exist
const std::string settingspath = (mCfgMgr.getUserConfigPath() / "tes3mp-client.cfg").string();
if (boost::filesystem::exists(settingspath))
settings.loadUser(settingspath);
return settingspath;
}
Main::Main()
{
LOG_MESSAGE_SIMPLE(TimedLog::LOG_INFO, "tes3mp started");
mNetworking = new Networking();
mLocalSystem = new LocalSystem();
mLocalPlayer = new LocalPlayer();
mGUIController = new GUIController();
mCellController = new CellController();
server = "mp.tes3mp.com";
port = 25565;
}
Main::~Main()
{
LOG_MESSAGE_SIMPLE(TimedLog::LOG_INFO, "tes3mp stopped");
delete mNetworking;
delete mLocalSystem;
delete mLocalPlayer;
2017-04-30 05:56:30 +00:00
delete mCellController;
delete mGUIController;
PlayerList::cleanUp();
}
void Main::optionsDesc(boost::program_options::options_description *desc)
2016-08-24 08:15:34 +00:00
{
namespace bpo = boost::program_options;
desc->add_options()
("connect", bpo::value<std::string>()->default_value(""),
"connect to server (e.g. --connect=127.0.0.1:25565)")
("password", bpo::value<std::string>()->default_value(TES3MP_DEFAULT_PASSW),
"сonnect to a secured server. (e.g. --password=AnyPassword");
2016-08-24 08:15:34 +00:00
}
void Main::configure(const boost::program_options::variables_map &variables)
2016-08-24 08:15:34 +00:00
{
Main::address = variables["connect"].as<std::string>();
Main::serverPassword = variables["password"].as<std::string>();
resourceDir = variables["resources"].as<Files::EscapePath>().mPath.string();
2016-08-24 08:15:34 +00:00
}
bool Main::init(std::vector<std::string> &content, Files::Collections &collections)
2016-09-18 03:54:45 +00:00
{
assert(!pMain);
pMain = new Main();
Settings::Manager manager;
loadSettings(manager);
int logLevel = manager.getInt("logLevel", "General");
TimedLog::SetLevel(logLevel);
if (address.empty())
2016-08-24 08:15:34 +00:00
{
pMain->server = manager.getString("destinationAddress", "General");
pMain->port = (unsigned short) manager.getInt("port", "General");
serverPassword = manager.getString("password", "General");
if (serverPassword.empty())
serverPassword = TES3MP_DEFAULT_PASSW;
2016-08-24 08:15:34 +00:00
}
else
{
size_t delimPos = address.find(':');
pMain->server = address.substr(0, delimPos);
pMain->port = atoi(address.substr(delimPos + 1).c_str());
2016-08-24 08:15:34 +00:00
}
get().mLocalSystem->serverPassword = serverPassword;
pMain->mNetworking->connect(pMain->server, pMain->port, content, collections);
2016-12-03 15:36:53 +00:00
return pMain->mNetworking->isConnected();
2016-09-18 03:54:45 +00:00
}
void Main::postInit()
2016-09-18 03:54:45 +00:00
{
pMain->mGUIController->setupChat();
const MWBase::Environment &environment = MWBase::Environment::get();
environment.getStateManager()->newGame(true);
2016-09-18 03:54:45 +00:00
MWBase::Environment::get().getMechanicsManager()->toggleAI();
RecordHelper::createPlaceholderInteriorCell();
}
bool Main::isInitialized()
{
return pMain != nullptr;
}
void Main::destroy()
{
assert(pMain);
delete pMain;
pMain = 0;
}
void Main::frame(float dt)
{
get().getNetworking()->update();
PlayerList::update(dt);
get().getCellController()->updateDedicated(dt);
get().updateWorld(dt);
get().getGUIController()->update(dt);
}
void Main::updateWorld(float dt) const
{
if (!mLocalPlayer->processCharGen())
return;
2016-09-18 03:54:45 +00:00
static bool init = true;
if (init)
{
2016-09-18 03:54:45 +00:00
init = false;
LOG_MESSAGE_SIMPLE(TimedLog::LOG_INFO, "Sending ID_PLAYER_BASEINFO to server");
mNetworking->getPlayerPacket(ID_PLAYER_BASEINFO)->setPlayer(getLocalPlayer());
mNetworking->getPlayerPacket(ID_LOADED)->setPlayer(getLocalPlayer());
mNetworking->getPlayerPacket(ID_PLAYER_BASEINFO)->Send();
mNetworking->getPlayerPacket(ID_LOADED)->Send();
mLocalPlayer->updateStatsDynamic(true);
get().getGUIController()->setChatVisible(true);
}
else
{
mLocalPlayer->update();
mCellController->updateLocal(false);
}
}
const Main &Main::get()
{
return *pMain;
}
Networking *Main::getNetworking() const
{
return mNetworking;
}
LocalSystem *Main::getLocalSystem() const
{
return mLocalSystem;
}
LocalPlayer *Main::getLocalPlayer() const
{
return mLocalPlayer;
}
GUIController *Main::getGUIController() const
{
return mGUIController;
}
CellController *Main::getCellController() const
{
return mCellController;
}
bool Main::isValidPacketScript(std::string scriptId)
{
mwmp::BaseWorldstate *worldstate = get().getNetworking()->getWorldstate();
if (Utils::vectorContains(worldstate->synchronizedClientScriptIds, scriptId))
return true;
return false;
}
bool Main::isValidPacketGlobal(std::string globalId)
{
mwmp::BaseWorldstate *worldstate = get().getNetworking()->getWorldstate();
if (Utils::vectorContains(worldstate->synchronizedClientGlobalIds, globalId))
return true;
return false;
}