Update to 13.0-beta1
This commit is contained in:
+266
-62
@@ -14,41 +14,157 @@
|
||||
#include "../command_func.h"
|
||||
#include "../company_func.h"
|
||||
#include "../settings_type.h"
|
||||
#include "../airport_cmd.h"
|
||||
#include "../aircraft_cmd.h"
|
||||
#include "../autoreplace_cmd.h"
|
||||
#include "../company_cmd.h"
|
||||
#include "../depot_cmd.h"
|
||||
#include "../dock_cmd.h"
|
||||
#include "../economy_cmd.h"
|
||||
#include "../engine_cmd.h"
|
||||
#include "../goal_cmd.h"
|
||||
#include "../group_cmd.h"
|
||||
#include "../industry_cmd.h"
|
||||
#include "../landscape_cmd.h"
|
||||
#include "../misc_cmd.h"
|
||||
#include "../news_cmd.h"
|
||||
#include "../object_cmd.h"
|
||||
#include "../order_cmd.h"
|
||||
#include "../rail_cmd.h"
|
||||
#include "../road_cmd.h"
|
||||
#include "../roadveh_cmd.h"
|
||||
#include "../settings_cmd.h"
|
||||
#include "../signs_cmd.h"
|
||||
#include "../station_cmd.h"
|
||||
#include "../story_cmd.h"
|
||||
#include "../subsidy_cmd.h"
|
||||
#include "../terraform_cmd.h"
|
||||
#include "../timetable_cmd.h"
|
||||
#include "../town_cmd.h"
|
||||
#include "../train_cmd.h"
|
||||
#include "../tree_cmd.h"
|
||||
#include "../tunnelbridge_cmd.h"
|
||||
#include "../vehicle_cmd.h"
|
||||
#include "../viewport_cmd.h"
|
||||
#include "../water_cmd.h"
|
||||
#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>{});
|
||||
|
||||
template <typename T> struct CallbackArgsHelper;
|
||||
template <typename... Targs>
|
||||
struct CallbackArgsHelper<void(*const)(Commands, const CommandCost &, Targs...)> {
|
||||
using Args = std::tuple<std::decay_t<Targs>...>;
|
||||
};
|
||||
|
||||
|
||||
/* Helpers to generate the command dispatch table from the command traits. */
|
||||
|
||||
template <Commands Tcmd> static CommandDataBuffer SanitizeCmdStrings(const CommandDataBuffer &data);
|
||||
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);
|
||||
UnpackDispatchT Unpack;
|
||||
};
|
||||
|
||||
template <Commands Tcmd, size_t Tcb>
|
||||
constexpr UnpackNetworkCommandProc MakeUnpackNetworkCommandCallback() noexcept
|
||||
{
|
||||
/* Check if the callback matches with the command arguments. If not, don't generate an Unpack proc. */
|
||||
using Tcallback = std::tuple_element_t<Tcb, decltype(_callback_tuple)>;
|
||||
if constexpr (std::is_same_v<Tcallback, CommandCallback * const> || // Callback type is CommandCallback.
|
||||
std::is_same_v<Tcallback, CommandCallbackData * const> || // Callback type is CommandCallbackData.
|
||||
std::is_same_v<typename CommandTraits<Tcmd>::CbArgs, typename CallbackArgsHelper<Tcallback>::Args> || // Callback proc takes all command return values and parameters.
|
||||
(!std::is_void_v<typename CommandTraits<Tcmd>::RetTypes> && std::is_same_v<typename CallbackArgsHelper<typename CommandTraits<Tcmd>::RetCallbackProc const>::Args, typename CallbackArgsHelper<Tcallback>::Args>)) { // Callback return is more than CommandCost and the proc takes all return values.
|
||||
return &UnpackNetworkCommand<Tcmd, Tcb>;
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
template <Commands Tcmd, size_t... i>
|
||||
constexpr UnpackDispatchT MakeUnpackNetworkCommand(std::index_sequence<i...>) noexcept
|
||||
{
|
||||
return UnpackDispatchT{{ {MakeUnpackNetworkCommandCallback<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)>, MakeUnpackNetworkCommand<static_cast<Commands>(i)>(std::make_index_sequence<_callback_tuple_size>{}) }... }};
|
||||
}
|
||||
/** 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.
|
||||
@@ -123,28 +239,39 @@ 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 tile The tile to perform a command on (see #CommandProc)
|
||||
* @param p1 Additional data for the command (see #CommandProc)
|
||||
* @param p2 Additional data for the command (see #CommandProc)
|
||||
* @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 text The text to pass
|
||||
* @param company The company that wants to send the command
|
||||
* @param location Location of the command (e.g. for error message position)
|
||||
* @param cmd_data The command proc arguments.
|
||||
*/
|
||||
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback, const std::string &text, CompanyID company)
|
||||
void NetworkSendCommand(Commands cmd, StringID err_message, CommandCallback *callback, CompanyID company, TileIndex location, const CommandDataBuffer &cmd_data)
|
||||
{
|
||||
assert((cmd & CMD_FLAGS_MASK) == 0);
|
||||
|
||||
CommandPacket c;
|
||||
c.company = company;
|
||||
c.tile = tile;
|
||||
c.p1 = p1;
|
||||
c.p2 = p2;
|
||||
c.cmd = cmd;
|
||||
c.err_msg = err_message;
|
||||
c.callback = callback;
|
||||
c.text = text;
|
||||
c.tile = location;
|
||||
c.data = cmd_data;
|
||||
|
||||
if (_network_server) {
|
||||
/* If we are the server, we queue the command in our 'special' queue.
|
||||
@@ -207,8 +334,10 @@ void NetworkExecuteLocalCommandQueue()
|
||||
|
||||
/* We can execute this command */
|
||||
_current_company = cp->company;
|
||||
cp->cmd |= CMD_NETWORK_COMMAND;
|
||||
DoCommandP(cp, cp->my_cmd);
|
||||
size_t cb_index = FindCallbackIndex(cp->callback);
|
||||
assert(cb_index < _callback_tuple_size);
|
||||
assert(_cmd_dispatch[cp->cmd].Unpack[cb_index] != nullptr);
|
||||
_cmd_dispatch[cp->cmd].Unpack[cb_index](cp);
|
||||
|
||||
queue.Pop();
|
||||
delete cp;
|
||||
@@ -295,18 +424,15 @@ void NetworkDistributeCommands()
|
||||
const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *cp)
|
||||
{
|
||||
cp->company = (CompanyID)p->Recv_uint8();
|
||||
cp->cmd = p->Recv_uint32();
|
||||
cp->cmd = static_cast<Commands>(p->Recv_uint16());
|
||||
if (!IsValidCommand(cp->cmd)) return "invalid command";
|
||||
if (GetCommandFlags(cp->cmd) & CMD_OFFLINE) return "single-player only command";
|
||||
if ((cp->cmd & CMD_FLAGS_MASK) != 0) return "invalid command flag";
|
||||
|
||||
cp->p1 = p->Recv_uint32();
|
||||
cp->p2 = p->Recv_uint32();
|
||||
cp->err_msg = p->Recv_uint16();
|
||||
cp->tile = p->Recv_uint32();
|
||||
cp->text = p->Recv_string(NETWORK_COMPANY_NAME_LENGTH, (!_network_server && GetCommandFlags(cp->cmd) & CMD_STR_CTRL) != 0 ? SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK : SVS_REPLACE_WITH_QUESTION_MARK);
|
||||
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() || _cmd_dispatch[cp->cmd].Unpack[callback] == nullptr) return "invalid callback";
|
||||
|
||||
cp->callback = _callback_table[callback];
|
||||
return nullptr;
|
||||
@@ -319,21 +445,99 @@ const char *NetworkGameSocketHandler::ReceiveCommand(Packet *p, CommandPacket *c
|
||||
*/
|
||||
void NetworkGameSocketHandler::SendCommand(Packet *p, const CommandPacket *cp)
|
||||
{
|
||||
p->Send_uint8 (cp->company);
|
||||
p->Send_uint32(cp->cmd);
|
||||
p->Send_uint32(cp->p1);
|
||||
p->Send_uint32(cp->p2);
|
||||
p->Send_uint8(cp->company);
|
||||
p->Send_uint16(cp->cmd);
|
||||
p->Send_uint16(cp->err_msg);
|
||||
p->Send_uint32(cp->tile);
|
||||
p->Send_string(cp->text);
|
||||
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 || _cmd_dispatch[cp->cmd].Unpack[callback] == nullptr) {
|
||||
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)
|
||||
{
|
||||
if constexpr (std::is_same_v<ClientID, T>) {
|
||||
data = client_id;
|
||||
}
|
||||
}
|
||||
|
||||
/** Set all invalid ClientID's to the proper value. */
|
||||
template<class Ttuple, size_t... Tindices>
|
||||
static inline void SetClientIds(Ttuple &values, ClientID client_id, std::index_sequence<Tindices...>)
|
||||
{
|
||||
((SetClientIdHelper(std::get<Tindices>(values), client_id)), ...);
|
||||
}
|
||||
|
||||
template <Commands Tcmd>
|
||||
static void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id)
|
||||
{
|
||||
/* Unpack command parameters. */
|
||||
auto params = EndianBufferReader::ToValue<typename CommandTraits<Tcmd>::Args>(cp.data);
|
||||
|
||||
/* Insert client id. */
|
||||
SetClientIds(params, client_id, std::make_index_sequence<std::tuple_size_v<decltype(params)>>{});
|
||||
|
||||
/* Repack command parameters. */
|
||||
cp.data = EndianBufferWriter<CommandDataBuffer>::FromValue(params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a client ID into the command data in a command packet.
|
||||
* @param cp Command packet to modify.
|
||||
* @param client_id Client id to insert.
|
||||
*/
|
||||
void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id)
|
||||
{
|
||||
_cmd_dispatch[cp.cmd].ReplaceClientId(cp, client_id);
|
||||
}
|
||||
|
||||
|
||||
/** Validate a single string argument coming from network. */
|
||||
template <class T>
|
||||
static inline void SanitizeSingleStringHelper([[maybe_unused]] CommandFlags cmd_flags, T &data)
|
||||
{
|
||||
if constexpr (std::is_same_v<std::string, T>) {
|
||||
data = StrMakeValid(data.substr(0, NETWORK_COMPANY_NAME_LENGTH), (!_network_server && cmd_flags & CMD_STR_CTRL) != 0 ? SVS_ALLOW_CONTROL_CODE | SVS_REPLACE_WITH_QUESTION_MARK : SVS_REPLACE_WITH_QUESTION_MARK);
|
||||
}
|
||||
}
|
||||
|
||||
/** Helper function to perform validation on command data strings. */
|
||||
template<class Ttuple, size_t... Tindices>
|
||||
static inline void SanitizeStringsHelper(CommandFlags cmd_flags, Ttuple &values, std::index_sequence<Tindices...>)
|
||||
{
|
||||
((SanitizeSingleStringHelper(cmd_flags, std::get<Tindices>(values))), ...);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate and sanitize strings in command data.
|
||||
* @tparam Tcmd Command this data belongs to.
|
||||
* @param data Command data.
|
||||
* @return Sanitized command data.
|
||||
*/
|
||||
template <Commands Tcmd>
|
||||
CommandDataBuffer SanitizeCmdStrings(const CommandDataBuffer &data)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, std::get<Tcb>(_callback_tuple), cp->my_cmd, cp->tile, args);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user