Changeset - r26121:f66f65c35c74
[Not reviewed]
master
0 7 0
Michael Lutz - 3 years ago 2021-11-28 16:37:04
michi@icosahedron.de
Codechange: Template the command callback function type to allow unpacked arguments.
7 files changed with 153 insertions and 71 deletions:
0 comments (0 inline, 0 general)
src/command_func.h
Show inline comments
 
@@ -75,12 +75,26 @@ struct RecursiveCommandCounter {
 
	/** Are we in the top-level command execution? */
 
	bool IsTopLevel() const { return _counter == 1; }
 
private:
 
	static int _counter;
 
};
 

	
 
#if defined(__GNUC__) && !defined(__clang__)
 
/*
 
 * We cast specialized function pointers to a generic one, but don't use the
 
 * converted value to call the function, which is safe, except that GCC
 
 * helpfully thinks it is not.
 
 *
 
 * "Any pointer to function can be converted to a pointer to a different function type.
 
 * Calling the function through a pointer to a different function type is undefined,
 
 * but converting such pointer back to pointer to the original function type yields
 
 * the pointer to the original function." */
 
#	pragma GCC diagnostic push
 
#	pragma GCC diagnostic ignored "-Wcast-function-type"
 
#	define SILENCE_GCC_FUNCTION_POINTER_CAST
 
#endif
 

	
 
template<Commands TCmd, typename T, bool THasTile> struct CommandHelper;
 

	
 
class CommandHelperBase {
 
protected:
 
	static void InternalDoBefore(bool top_level, bool test);
 
@@ -145,36 +159,38 @@ public:
 

	
 
	/**
 
	 * Shortcut for the long Post when not using a callback.
 
	 * @param err_message Message prefix to show on error
 
	 * @param args Parameters for the command
 
	 */
 
	static inline bool Post(StringID err_message, Targs... args) { return Post(err_message, nullptr, std::forward<Targs>(args)...); }
 
	static inline bool Post(StringID err_message, Targs... args) { return Post<CommandCallback>(err_message, nullptr, std::forward<Targs>(args)...); }
 
	/**
 
	 * Shortcut for the long Post when not using an error message.
 
	 * @param callback A callback function to call after the command is finished
 
	 * @param args Parameters for the command
 
	 */
 
	static inline bool Post(CommandCallback *callback, Targs... args) { return Post((StringID)0, callback, std::forward<Targs>(args)...); }
 
	template <typename Tcallback>
 
	static inline bool Post(Tcallback *callback, Targs... args) { return Post((StringID)0, callback, std::forward<Targs>(args)...); }
 
	/**
 
	 * Shortcut for the long Post when not using a callback or an error message.
 
	 * @param args Parameters for the command
 
	 */
 
	static inline bool Post(Targs... args) { return Post((StringID)0, nullptr, std::forward<Targs>(args)...); }
 
	static inline bool Post(Targs... args) { return Post<CommandCallback>((StringID)0, nullptr, std::forward<Targs>(args)...); }
 

	
 
	/**
 
	 * Top-level network safe command execution for the current company.
 
	 * Must not be called recursively. The callback is called when the
 
	 * command succeeded or failed.
 
	 *
 
	 * @param err_message Message prefix to show on error
 
	 * @param callback A callback function to call after the command is finished
 
	 * @param args Parameters for the command
 
	 * @return \c true if the command succeeded, else \c false.
 
	 */
 
	static bool Post(StringID err_message, CommandCallback *callback, Targs... args)
 
	template <typename Tcallback>
 
	static bool Post(StringID err_message, Tcallback *callback, Targs... args)
 
	{
 
		return InternalPost(err_message, callback, true, false, std::forward_as_tuple(args...));
 
	}
 

	
 
	/**
 
	 * Execute a command coming from the network.
 
@@ -182,50 +198,51 @@ public:
 
	 * @param callback A callback function to call after the command is finished
 
	 * @param my_cmd indicator if the command is from a company or server (to display error messages for a user)
 
	 * @param location Tile location for user feedback.
 
	 * @param args Parameters for the command
 
	 * @return \c true if the command succeeded, else \c false.
 
	 */
 
	static bool PostFromNet(StringID err_message, CommandCallback *callback, bool my_cmd, TileIndex location, std::tuple<Targs...> args)
 
	template <typename Tcallback>
 
	static bool PostFromNet(StringID err_message, Tcallback *callback, bool my_cmd, TileIndex location, std::tuple<Targs...> args)
 
	{
 
		return InternalPost(err_message, callback, my_cmd, true, location, std::move(args));
 
	}
 

	
 
	/**
 
	 * Prepare a command to be send over the network
 
	 * @param cmd The command to execute (a CMD_* value)
 
	 * @param err_message Message prefix to show on error
 
	 * @param callback A callback function to call after the command is finished
 
	 * @param company The company that wants to send the command
 
	 * @param args Parameters for the command
 
	 */
 
	static void SendNet(StringID err_message, CommandCallback *callback, CompanyID company, Targs... args)
 
	static void SendNet(StringID err_message, CompanyID company, Targs... args)
 
	{
 
		auto args_tuple = std::forward_as_tuple(args...);
 

	
 
		TileIndex tile{};
 
		if constexpr (std::is_same_v<TileIndex, std::tuple_element_t<0, decltype(args_tuple)>>) {
 
			tile = std::get<0>(args_tuple);
 
		}
 

	
 
		::NetworkSendCommand(Tcmd, err_message, callback, _current_company, tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args_tuple));
 
		::NetworkSendCommand(Tcmd, err_message, nullptr, _current_company, tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args_tuple));
 
	}
 

	
 
	/**
 
	 * Top-level network safe command execution without safety checks.
 
	 * @param err_message Message prefix to show on error
 
	 * @param callback A callback function to call after the command is finished
 
	 * @param my_cmd indicator if the command is from a company or server (to display error messages for a user)
 
	 * @param estimate_only whether to give only the estimate or also execute the command
 
	 * @param location Tile location for user feedback.
 
	 * @param args Parameters for the command
 
	 * @return the command cost of this function.
 
	 */
 
	static CommandCost Unsafe(StringID err_message, CommandCallback *callback, bool my_cmd, bool estimate_only, TileIndex location, std::tuple<Targs...> args)
 
	template <typename Tcallback>
 
	static CommandCost Unsafe(StringID err_message, Tcallback *callback, bool my_cmd, bool estimate_only, TileIndex location, std::tuple<Targs...> args)
 
	{
 
		return Execute(err_message, callback, my_cmd, estimate_only, false, location, std::move(args));
 
		return Execute(err_message, reinterpret_cast<CommandCallback *>(callback), my_cmd, estimate_only, false, location, std::move(args));
 
	}
 

	
 
protected:
 
	/** Helper to process a single ClientID argument. */
 
	template <class T>
 
	static inline void SetClientIdHelper(T &data)
 
@@ -239,32 +256,34 @@ protected:
 
	template<class Ttuple, size_t... Tindices>
 
	static inline void SetClientIds(Ttuple &values, std::index_sequence<Tindices...>)
 
	{
 
		((SetClientIdHelper(std::get<Tindices>(values))), ...);
 
	}
 

	
 
	static bool InternalPost(StringID err_message, CommandCallback *callback, bool my_cmd, bool network_command, std::tuple<Targs...> args)
 
	template <typename Tcallback>
 
	static bool InternalPost(StringID err_message, Tcallback *callback, bool my_cmd, bool network_command, std::tuple<Targs...> args)
 
	{
 
		/* Where to show the message? */
 
		TileIndex tile{};
 
		if constexpr (std::is_same_v<TileIndex, std::tuple_element_t<0, decltype(args)>>) {
 
			tile = std::get<0>(args);
 
		}
 

	
 
		return InternalPost(err_message, callback, my_cmd, network_command, tile, std::move(args));
 
	}
 

	
 
	static bool InternalPost(StringID err_message, CommandCallback *callback, bool my_cmd, bool network_command, TileIndex tile, std::tuple<Targs...> args)
 
	template <typename Tcallback>
 
	static bool InternalPost(StringID err_message, Tcallback *callback, bool my_cmd, bool network_command, TileIndex tile, std::tuple<Targs...> args)
 
	{
 
		auto [err, estimate_only, only_sending] = InternalPostBefore(Tcmd, GetCommandFlags<Tcmd>(), tile, err_message, network_command);
 
		if (err) return false;
 

	
 
		/* Only set client IDs when the command does not come from the network. */
 
		if (!network_command && GetCommandFlags<Tcmd>() & CMD_CLIENT_ID) SetClientIds(args, std::index_sequence_for<Targs...>{});
 

	
 
		CommandCost res = Execute(err_message, callback, my_cmd, estimate_only, network_command, tile, args);
 
		CommandCost res = Execute(err_message, reinterpret_cast<CommandCallback *>(callback), my_cmd, estimate_only, network_command, tile, args);
 
		InternalPostResult(res, tile, estimate_only, only_sending, err_message, my_cmd);
 

	
 
		if (!estimate_only && !only_sending && callback != nullptr) {
 
			callback(Tcmd, res, tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args));
 
		}
 

	
 
@@ -357,41 +376,46 @@ struct CommandHelper<Tcmd, CommandCost(*
 
	/**
 
	 * Shortcut for Post when not using an error message.
 
	 * @param err_message Message prefix to show on error
 
	 * @param location Tile location for user feedback.
 
	 * @param args Parameters for the command
 
	 */
 
	static inline bool Post(StringID err_message, TileIndex location, Targs... args) { return Post(err_message, nullptr, location, std::forward<Targs>(args)...); }
 
	static inline bool Post(StringID err_message, TileIndex location, Targs... args) { return Post<CommandCallback>(err_message, nullptr, location, std::forward<Targs>(args)...); }
 
	/**
 
	 * Shortcut for Post when not using a callback.
 
	 * @param callback A callback function to call after the command is finished
 
	 * @param location Tile location for user feedback.
 
	 * @param args Parameters for the command
 
	 */
 
	static inline bool Post(CommandCallback *callback, TileIndex location, Targs... args) { return Post((StringID)0, callback, location, std::forward<Targs>(args)...); }
 
	template <typename Tcallback>
 
	static inline bool Post(Tcallback *callback, TileIndex location, Targs... args) { return Post((StringID)0, callback, location, std::forward<Targs>(args)...); }
 
	/**
 
	 * Shortcut for Post when not using a callback or an error message.
 
	 * @param location Tile location for user feedback.
 
	 * @param args Parameters for the command*
 
	 */
 
	static inline bool Post(TileIndex location, Targs... args) { return Post((StringID)0, nullptr, location, std::forward<Targs>(args)...); }
 
	static inline bool Post(TileIndex location, Targs... args) { return Post<CommandCallback>((StringID)0, nullptr, location, std::forward<Targs>(args)...); }
 

	
 
	/**
 
	 * Post variant that takes a TileIndex (for error window location and text effects) for
 
	 * commands that don't take a TileIndex by themselves.
 
	 * @param err_message Message prefix to show on error
 
	 * @param callback A callback function to call after the command is finished
 
	 * @param location Tile location for user feedback.
 
	 * @param args Parameters for the command
 
	 * @return \c true if the command succeeded, else \c false.
 
	 */
 
	static inline bool Post(StringID err_message, CommandCallback *callback, TileIndex location, Targs... args)
 
	template <typename Tcallback>
 
	static inline bool Post(StringID err_message, Tcallback *callback, TileIndex location, Targs... args)
 
	{
 
		return CommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...), true>::InternalPost(err_message, callback, true, false, location, std::forward_as_tuple(args...));
 
	}
 
};
 

	
 
#ifdef SILENCE_GCC_FUNCTION_POINTER_CAST
 
#	pragma GCC diagnostic pop
 
#endif
 

	
 
template <Commands Tcmd>
 
using Command = CommandHelper<Tcmd, typename CommandTraits<Tcmd>::ProcType, std::is_same_v<TileIndex, std::tuple_element_t<0, typename CommandTraits<Tcmd>::Args>>>;
 

	
 
#endif /* COMMAND_FUNC_H */
src/network/network_client.cpp
Show inline comments
 
@@ -837,13 +837,13 @@ NetworkRecvStatus ClientNetworkGameSocke
 

	
 
		if (_network_join.company != COMPANY_SPECTATOR) {
 
			/* We have arrived and ready to start playing; send a command to make a new company;
 
			 * the server will give us a client-id and let us in */
 
			_network_join_status = NETWORK_JOIN_STATUS_REGISTERING;
 
			ShowJoinStatusWindow();
 
			Command<CMD_COMPANY_CTRL>::SendNet(STR_NULL, nullptr, _local_company, CCA_NEW, INVALID_COMPANY, CRR_NONE, INVALID_CLIENT_ID);
 
			Command<CMD_COMPANY_CTRL>::SendNet(STR_NULL, _local_company, CCA_NEW, INVALID_COMPANY, CRR_NONE, INVALID_CLIENT_ID);
 
		}
 
	} else {
 
		/* take control over an existing company */
 
		SetLocalCompany(_network_join.company);
 
	}
 

	
src/network/network_command.cpp
Show inline comments
 
@@ -50,61 +50,101 @@
 
#include "../waypoint_cmd.h"
 
#include "../script/script_cmd.h"
 
#include <array>
 

	
 
#include "../safeguards.h"
 

	
 
/** Table with all the callbacks we'll use for conversion*/
 
static CommandCallback * const _callback_table[] = {
 
	/* 0x00 */ nullptr,
 
	/* 0x01 */ CcBuildPrimaryVehicle,
 
	/* 0x02 */ CcBuildAirport,
 
	/* 0x03 */ CcBuildBridge,
 
	/* 0x04 */ CcPlaySound_CONSTRUCTION_WATER,
 
	/* 0x05 */ CcBuildDocks,
 
	/* 0x06 */ CcFoundTown,
 
	/* 0x07 */ CcBuildRoadTunnel,
 
	/* 0x08 */ CcBuildRailTunnel,
 
	/* 0x09 */ CcBuildWagon,
 
	/* 0x0A */ CcRoadDepot,
 
	/* 0x0B */ CcRailDepot,
 
	/* 0x0C */ CcPlaceSign,
 
	/* 0x0D */ CcPlaySound_EXPLOSION,
 
	/* 0x0E */ CcPlaySound_CONSTRUCTION_OTHER,
 
	/* 0x0F */ CcPlaySound_CONSTRUCTION_RAIL,
 
	/* 0x10 */ CcStation,
 
	/* 0x11 */ CcTerraform,
 
	/* 0x12 */ CcAI,
 
	/* 0x13 */ CcCloneVehicle,
 
	/* 0x14 */ nullptr,
 
	/* 0x15 */ CcCreateGroup,
 
	/* 0x16 */ CcFoundRandomTown,
 
	/* 0x17 */ CcRoadStop,
 
	/* 0x18 */ CcBuildIndustry,
 
	/* 0x19 */ CcStartStopVehicle,
 
	/* 0x1A */ CcGame,
 
	/* 0x1B */ CcAddVehicleNewGroup,
 
};
 
/** Typed list of all possible callbacks. */
 
static constexpr auto _callback_tuple = std::make_tuple(
 
	(CommandCallback *)nullptr, // Make sure this is actually a pointer-to-function.
 
	&CcBuildPrimaryVehicle,
 
	&CcBuildAirport,
 
	&CcBuildBridge,
 
	&CcPlaySound_CONSTRUCTION_WATER,
 
	&CcBuildDocks,
 
	&CcFoundTown,
 
	&CcBuildRoadTunnel,
 
	&CcBuildRailTunnel,
 
	&CcBuildWagon,
 
	&CcRoadDepot,
 
	&CcRailDepot,
 
	&CcPlaceSign,
 
	&CcPlaySound_EXPLOSION,
 
	&CcPlaySound_CONSTRUCTION_OTHER,
 
	&CcPlaySound_CONSTRUCTION_RAIL,
 
	&CcStation,
 
	&CcTerraform,
 
	&CcAI,
 
	&CcCloneVehicle,
 
	&CcCreateGroup,
 
	&CcFoundRandomTown,
 
	&CcRoadStop,
 
	&CcBuildIndustry,
 
	&CcStartStopVehicle,
 
	&CcGame,
 
	&CcAddVehicleNewGroup
 
);
 

	
 
#ifdef SILENCE_GCC_FUNCTION_POINTER_CAST
 
/*
 
 * We cast specialized function pointers to a generic one, but don't use the
 
 * converted value to call the function, which is safe, except that GCC
 
 * helpfully thinks it is not.
 
 *
 
 * "Any pointer to function can be converted to a pointer to a different function type.
 
 * Calling the function through a pointer to a different function type is undefined,
 
 * but converting such pointer back to pointer to the original function type yields
 
 * the pointer to the original function." */
 
#	pragma GCC diagnostic push
 
#	pragma GCC diagnostic ignored "-Wcast-function-type"
 
#endif
 

	
 
/* Helpers to generate the callback table from the callback list. */
 

	
 
inline constexpr size_t _callback_tuple_size = std::tuple_size_v<decltype(_callback_tuple)>;
 

	
 
template <size_t... i>
 
inline auto MakeCallbackTable(std::index_sequence<i...>) noexcept
 
{
 
	return std::array<CommandCallback *, sizeof...(i)>{{ reinterpret_cast<CommandCallback *>(reinterpret_cast<void(*)()>(std::get<i>(_callback_tuple)))... }}; // MingW64 fails linking when casting a pointer to its own type. To work around, cast it to some other type first.
 
}
 

	
 
/** Type-erased table of callbacks. */
 
static auto _callback_table = MakeCallbackTable(std::make_index_sequence<_callback_tuple_size>{});
 

	
 

	
 
/* Helpers to generate the command dispatch table from the command traits. */
 

	
 
template <Commands Tcmd> static CommandDataBuffer SanitizeCmdStrings(const CommandDataBuffer &data);
 
template <Commands Tcmd> static void UnpackNetworkCommand(const CommandPacket *cp);
 
template <Commands Tcmd, size_t cb> static void UnpackNetworkCommand(const CommandPacket *cp);
 
template <Commands Tcmd> static void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id);
 
using UnpackNetworkCommandProc = void (*)(const CommandPacket *);
 
using UnpackDispatchT = std::array<UnpackNetworkCommandProc, _callback_tuple_size>;
 
struct CommandDispatch {
 
	CommandDataBuffer(*Sanitize)(const CommandDataBuffer &);
 
	void (*ReplaceClientId)(CommandPacket &, ClientID);
 
	void (*Unpack)(const CommandPacket *);
 
	UnpackDispatchT Unpack;
 
};
 

	
 
template<typename T, T... i>
 
inline constexpr auto MakeDispatchTable(std::integer_sequence<T, i...>) noexcept
 
template <Commands Tcmd, size_t... i>
 
constexpr UnpackDispatchT MakeUnpackNetworkCommand(std::index_sequence<i...>) noexcept
 
{
 
	return UnpackDispatchT{{ {&UnpackNetworkCommand<Tcmd, i>}... }};
 
}
 

	
 
template <typename T, T... i, size_t... j>
 
inline constexpr auto MakeDispatchTable(std::integer_sequence<T, i...>, std::index_sequence<j...>) noexcept
 
{
 
	return std::array<CommandDispatch, sizeof...(i)>{{ { &SanitizeCmdStrings<static_cast<Commands>(i)>, &NetworkReplaceCommandClientId<static_cast<Commands>(i)>, &UnpackNetworkCommand<static_cast<Commands>(i)> }... }};
 
	return std::array<CommandDispatch, sizeof...(i)>{{ { &SanitizeCmdStrings<static_cast<Commands>(i)>, &NetworkReplaceCommandClientId<static_cast<Commands>(i)>, MakeUnpackNetworkCommand<static_cast<Commands>(i)>(std::make_index_sequence<_callback_tuple_size>{}) }... }};
 
}
 
static constexpr auto _cmd_dispatch = MakeDispatchTable(std::make_integer_sequence<std::underlying_type_t<Commands>, CMD_END>{});
 
/** Command dispatch table. */
 
static constexpr auto _cmd_dispatch = MakeDispatchTable(std::make_integer_sequence<std::underlying_type_t<Commands>, CMD_END>{}, std::make_index_sequence<_callback_tuple_size>{});
 

	
 
#ifdef SILENCE_GCC_FUNCTION_POINTER_CAST
 
#	pragma GCC diagnostic pop
 
#endif
 

	
 

	
 
/**
 
 * Append a CommandPacket at the end of the queue.
 
 * @param p The packet to append to the queue.
 
 * @note A new instance of the CommandPacket will be made.
 
@@ -177,12 +217,26 @@ void CommandQueue::Free()
 
static CommandQueue _local_wait_queue;
 
/** Local queue of packets waiting for execution. */
 
static CommandQueue _local_execution_queue;
 

	
 

	
 
/**
 
 * Find the callback index of a callback pointer.
 
 * @param callback Address of callback to search for.
 
 * @return Callback index or std::numeric_limits<size_t>::max() if the function wasn't found in the callback list.
 
 */
 
static size_t FindCallbackIndex(CommandCallback *callback)
 
{
 
	if (auto it = std::find(std::cbegin(_callback_table), std::cend(_callback_table), callback); it != std::cend(_callback_table)) {
 
		return static_cast<size_t>(std::distance(std::cbegin(_callback_table), it));
 
	}
 

	
 
	return std::numeric_limits<size_t>::max();
 
}
 

	
 
/**
 
 * Prepare a DoCommand to be send over the network
 
 * @param cmd The command to execute (a CMD_* value)
 
 * @param err_message Message prefix to show on error
 
 * @param callback A callback function to call after the command is finished
 
 * @param company The company that wants to send the command
 
 * @param location Location of the command (e.g. for error message position)
 
@@ -256,13 +310,15 @@ void NetworkExecuteLocalCommandQueue()
 
			 * past the command we need to execute. */
 
			error("[net] Trying to execute a packet in the past!");
 
		}
 

	
 
		/* We can execute this command */
 
		_current_company = cp->company;
 
		_cmd_dispatch[cp->cmd].Unpack(cp);
 
		size_t cb_index = FindCallbackIndex(cp->callback);
 
		assert(cb_index < _callback_tuple_size);
 
		_cmd_dispatch[cp->cmd].Unpack[cb_index](cp);
 

	
 
		queue.Pop();
 
		delete cp;
 
	}
 

	
 
	/* Local company may have changed, so we should not restore the old value */
 
@@ -351,13 +407,13 @@ const char *NetworkGameSocketHandler::Re
 
	if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "single-player only command";
 
	cp->err_msg = p->Recv_uint16();
 
	cp->tile    = p->Recv_uint32();
 
	cp->data    = _cmd_dispatch[cp->cmd].Sanitize(p->Recv_buffer());
 

	
 
	byte callback = p->Recv_uint8();
 
	if (callback >= lengthof(_callback_table))  return "invalid callback";
 
	if (callback >= _callback_table.size())  return "invalid callback";
 

	
 
	cp->callback = _callback_table[callback];
 
	return nullptr;
 
}
 

	
 
/**
 
@@ -370,22 +426,18 @@ void NetworkGameSocketHandler::SendComma
 
	p->Send_uint8(cp->company);
 
	p->Send_uint16(cp->cmd);
 
	p->Send_uint16(cp->err_msg);
 
	p->Send_uint32(cp->tile);
 
	p->Send_buffer(cp->data);
 

	
 
	byte callback = 0;
 
	while (callback < lengthof(_callback_table) && _callback_table[callback] != cp->callback) {
 
		callback++;
 
	}
 

	
 
	if (callback == lengthof(_callback_table)) {
 
	size_t callback = FindCallbackIndex(cp->callback);
 
	if (callback > UINT8_MAX) {
 
		Debug(net, 0, "Unknown callback for command; no callback sent (command: {})", cp->cmd);
 
		callback = 0; // _callback_table[0] == nullptr
 
	}
 
	p->Send_uint8 (callback);
 
	p->Send_uint8 ((uint8)callback);
 
}
 

	
 
/** Helper to process a single ClientID argument. */
 
template <class T>
 
static inline void SetClientIdHelper(T &data, [[maybe_unused]] ClientID client_id)
 
{
 
@@ -452,12 +504,18 @@ CommandDataBuffer SanitizeCmdStrings(con
 
{
 
	auto args = EndianBufferReader::ToValue<typename CommandTraits<Tcmd>::Args>(data);
 
	SanitizeStringsHelper(CommandTraits<Tcmd>::flags, args, std::make_index_sequence<std::tuple_size_v<typename CommandTraits<Tcmd>::Args>>{});
 
	return EndianBufferWriter<CommandDataBuffer>::FromValue(args);
 
}
 

	
 
template <Commands Tcmd>
 
void UnpackNetworkCommand(const CommandPacket *cp)
 
/**
 
 * Unpack a generic command packet into its actual typed components.
 
 * @tparam Tcmd Command type to be unpacked.
 
 * @tparam Tcb Index into the callback list.
 
 * @param cp Command packet to unpack.
 
 */
 
template <Commands Tcmd, size_t Tcb>
 
void UnpackNetworkCommand(const CommandPacket* cp)
 
{
 
	auto args = EndianBufferReader::ToValue<typename CommandTraits<Tcmd>::Args>(cp->data);
 
	Command<Tcmd>::PostFromNet(cp->err_msg, cp->callback, cp->my_cmd, cp->tile, args);
 
	Command<Tcmd>::PostFromNet(cp->err_msg, std::get<Tcb>(_callback_tuple), cp->my_cmd, cp->tile, args);
 
}
src/network/network_gui.cpp
Show inline comments
 
@@ -1535,13 +1535,13 @@ private:
 
	 */
 
	static void OnClickCompanyNew(NetworkClientListWindow *w, Point pt, CompanyID company_id)
 
	{
 
		if (_network_server) {
 
			Command<CMD_COMPANY_CTRL>::Post(CCA_NEW, INVALID_COMPANY, CRR_NONE, _network_own_client_id);
 
		} else {
 
			Command<CMD_COMPANY_CTRL>::SendNet(STR_NULL, nullptr, _local_company, CCA_NEW, INVALID_COMPANY, CRR_NONE, INVALID_CLIENT_ID);
 
			Command<CMD_COMPANY_CTRL>::SendNet(STR_NULL, _local_company, CCA_NEW, INVALID_COMPANY, CRR_NONE, INVALID_CLIENT_ID);
 
		}
 
	}
 

	
 
	/**
 
	 * Admin button on a Client is clicked.
 
	 * @param w The instance of this window.
src/network/network_server.cpp
Show inline comments
 
@@ -2078,13 +2078,13 @@ void NetworkServerNewCompany(const Compa
 
	NetworkServerUpdateCompanyPassworded(c->index, false);
 

	
 
	if (ci != nullptr) {
 
		/* ci is nullptr when replaying, or for AIs. In neither case there is a client. */
 
		ci->client_playas = c->index;
 
		NetworkUpdateClientInfo(ci->client_id);
 
		Command<CMD_RENAME_PRESIDENT>::SendNet(STR_NULL, nullptr, c->index, ci->client_name);
 
		Command<CMD_RENAME_PRESIDENT>::SendNet(STR_NULL, c->index, ci->client_name);
 
	}
 

	
 
	/* Announce new company on network. */
 
	NetworkAdminCompanyInfo(c, true);
 

	
 
	if (ci != nullptr) {
src/order_backup.cpp
Show inline comments
 
@@ -195,13 +195,13 @@ CommandCost CmdClearOrderBackup(DoComman
 
		if (t != INVALID_TILE && t != ob->tile) continue;
 

	
 
		if (from_gui) {
 
			/* We need to circumvent the "prevention" from this command being executed
 
			 * while the game is paused, so use the internal method. Nor do we want
 
			 * this command to get its cost estimated when shift is pressed. */
 
			Command<CMD_CLEAR_ORDER_BACKUP>::Unsafe(STR_NULL, nullptr, true, false, ob->tile, CommandTraits<CMD_CLEAR_ORDER_BACKUP>::Args{ ob->tile, static_cast<ClientID>(user) });
 
			Command<CMD_CLEAR_ORDER_BACKUP>::Unsafe<CommandCallback>(STR_NULL, nullptr, true, false, ob->tile, CommandTraits<CMD_CLEAR_ORDER_BACKUP>::Args{ ob->tile, static_cast<ClientID>(user) });
 
		} else {
 
			/* The command came from the game logic, i.e. the clearing of a tile.
 
			 * In that case we have no need to actually sync this, just do it. */
 
			delete ob;
 
		}
 
	}
src/settings.cpp
Show inline comments
 
@@ -1597,13 +1597,13 @@ void SyncCompanySettings()
 
	const void *old_object = &Company::Get(_current_company)->settings;
 
	const void *new_object = &_settings_client.company;
 
	for (auto &desc : _company_settings) {
 
		const SettingDesc *sd = GetSettingDesc(desc);
 
		uint32 old_value = (uint32)sd->AsIntSetting()->Read(new_object);
 
		uint32 new_value = (uint32)sd->AsIntSetting()->Read(old_object);
 
		if (old_value != new_value) Command<CMD_CHANGE_COMPANY_SETTING>::SendNet(STR_NULL, nullptr, _local_company, sd->GetName(), new_value);
 
		if (old_value != new_value) Command<CMD_CHANGE_COMPANY_SETTING>::SendNet(STR_NULL, _local_company, sd->GetName(), new_value);
 
	}
 
}
 

	
 
/**
 
 * Set a setting value with a string.
 
 * @param sd the setting to change.
0 comments (0 inline, 0 general)