Skip to content

Commit

Permalink
Add implementation for lua_path different from map path
Browse files Browse the repository at this point in the history
  • Loading branch information
ottml authored and Flow86 committed Nov 6, 2023
1 parent 04b1a1b commit 229a4ed
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
39 changes: 25 additions & 14 deletions libs/s25main/network/GameClient.cpp
Expand Up @@ -54,6 +54,23 @@
#include <helpers/chronoIO.h>
#include <memory>

namespace {
void copyFileIfPathDifferent(const boost::filesystem::path& src_path, const boost::filesystem::path& dst_path)
{
if(src_path != dst_path)
{
boost::system::error_code ignoredEc;
constexpr auto overwrite_existing =
#if BOOST_VERSION >= 107400
boost::filesystem::copy_options::overwrite_existing;
#else
boost::filesystem::copy_option::overwrite_if_exists;
#endif
copy_file(src_path, dst_path, overwrite_existing, ignoredEc);
}
}
} // namespace

void GameClient::ClientConfig::Clear()
{
server.clear();
Expand Down Expand Up @@ -113,24 +130,18 @@ bool GameClient::Connect(const std::string& server, const std::string& password,
return true;
}

bool GameClient::HostGame(const CreateServerInfo& csi, const boost::filesystem::path& map_path, MapType map_type)
bool GameClient::HostGame(const CreateServerInfo& csi, const MapDescription& map)
{
std::string hostPw = createRandString(20);
// Copy the map to the played map folders to avoid having to transmit it from the (local) server
const auto playedMapPath = RTTRCONFIG.ExpandPath(s25::folders::mapsPlayed) / map_path.filename();
if(playedMapPath != map_path)
// Copy the map and lua to the played map folders to avoid having to transmit it from the (local) server
const auto playedMapPath = RTTRCONFIG.ExpandPath(s25::folders::mapsPlayed) / map.map_path.filename();
copyFileIfPathDifferent(map.map_path, playedMapPath);
if(map.lua_path)
{
boost::system::error_code ignoredEc;
constexpr auto overwrite_existing =
#if BOOST_VERSION >= 107400
boost::filesystem::copy_options::overwrite_existing;
#else
boost::filesystem::copy_option::overwrite_if_exists;
#endif
copy_file(map_path, playedMapPath, overwrite_existing, ignoredEc);
const auto playedMapLuaPath = RTTRCONFIG.ExpandPath(s25::folders::mapsPlayed) / map.lua_path->filename();
copyFileIfPathDifferent(*map.lua_path, playedMapLuaPath);
}
return GAMESERVER.Start(csi, map_path, map_type, hostPw)
&& Connect("localhost", hostPw, csi.type, csi.port, true, csi.ipv6);
return GAMESERVER.Start(csi, map, hostPw) && Connect("localhost", hostPw, csi.type, csi.port, true, csi.ipv6);
}

/**
Expand Down
12 changes: 7 additions & 5 deletions libs/s25main/network/GameServer.cpp
Expand Up @@ -112,8 +112,7 @@ GameServer::~GameServer()

///////////////////////////////////////////////////////////////////////////////
// Spiel hosten
bool GameServer::Start(const CreateServerInfo& csi, const boost::filesystem::path& map_path, MapType map_type,
const std::string& hostPw)
bool GameServer::Start(const CreateServerInfo& csi, const MapDescription& map, const std::string& hostPw)
{
Stop();

Expand All @@ -124,8 +123,8 @@ bool GameServer::Start(const CreateServerInfo& csi, const boost::filesystem::pat
config.servertype = csi.type;
config.port = csi.port;
config.ipv6 = csi.ipv6;
mapinfo.type = map_type;
mapinfo.filepath = map_path;
mapinfo.type = map.map_type;
mapinfo.filepath = map.map_path;

// Maps, Random-Maps, Savegames - Header laden und relevante Informationen rausschreiben (Map-Titel, Spieleranzahl)
switch(mapinfo.type)
Expand Down Expand Up @@ -185,7 +184,10 @@ bool GameServer::Start(const CreateServerInfo& csi, const boost::filesystem::pat
if(!mapinfo.mapData.CompressFromFile(mapinfo.filepath, &mapinfo.mapChecksum))
return false;

bfs::path luaFilePath = bfs::path(mapinfo.filepath).replace_extension("lua");
if(map.lua_path.has_value() && !bfs::is_regular_file(*map.lua_path))
return false;

bfs::path luaFilePath = map.lua_path.get_value_or(bfs::path(mapinfo.filepath).replace_extension("lua"));
if(bfs::is_regular_file(luaFilePath))
{
if(!mapinfo.luaData.CompressFromFile(luaFilePath, &mapinfo.luaChecksum))
Expand Down
4 changes: 2 additions & 2 deletions libs/s25main/network/GameServer.h
Expand Up @@ -10,6 +10,7 @@
#include "GlobalGameSettings.h"
#include "JoinPlayerInfo.h"
#include "NWFInfo.h"
#include "gameTypes/MapDescription.h"
#include "gameTypes/MapInfo.h"
#include "gameTypes/ServerType.h"
#include "liblobby/LobbyInterface.h"
Expand Down Expand Up @@ -38,8 +39,7 @@ class GameServer :
~GameServer();

/// Starts the server
bool Start(const CreateServerInfo& csi, const boost::filesystem::path& map_path, MapType map_type,
const std::string& hostPw);
bool Start(const CreateServerInfo& csi, const MapDescription& map, const std::string& hostPw);

void Run();

Expand Down

0 comments on commit 229a4ed

Please sign in to comment.