Merge branch 'openttd'
This commit is contained in:
@@ -47,7 +47,7 @@ static const uint16 UDP_MTU = 1460; ///< Numbe
|
||||
static const uint16 TCP_MTU = 32767; ///< Number of bytes we can pack in a single TCP packet
|
||||
static const uint16 COMPAT_MTU = 1460; ///< Number of bytes we can pack in a single packet for backward compatibility
|
||||
|
||||
static const byte NETWORK_GAME_ADMIN_VERSION = 1; ///< What version of the admin network do we use?
|
||||
static const byte NETWORK_GAME_ADMIN_VERSION = 2; ///< What version of the admin network do we use?
|
||||
static const byte NETWORK_GAME_INFO_VERSION = 6; ///< What version of game-info do we use?
|
||||
static const byte NETWORK_COMPANY_INFO_VERSION = 6; ///< What version of company info is this?
|
||||
static const byte NETWORK_COORDINATOR_VERSION = 6; ///< What version of game-coordinator-protocol do we use?
|
||||
|
||||
@@ -185,6 +185,17 @@ void Packet::Send_string(const std::string_view data)
|
||||
this->buffer.emplace_back('\0');
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a sized byte buffer into the packet.
|
||||
* @param data The data to send.
|
||||
*/
|
||||
void Packet::Send_buffer(const std::vector<byte> &data)
|
||||
{
|
||||
assert(this->CanWriteToPacket(sizeof(uint16) + data.size()));
|
||||
this->Send_uint16((uint16)data.size());
|
||||
this->buffer.insert(this->buffer.end(), data.begin(), data.end());
|
||||
}
|
||||
|
||||
/**
|
||||
* Send as many of the bytes as possible in the packet. This can mean
|
||||
* that it is possible that not all bytes are sent. To cope with this
|
||||
@@ -366,6 +377,23 @@ uint64 Packet::Recv_uint64()
|
||||
return n;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a sized byte buffer from the packet.
|
||||
* @return The extracted buffer.
|
||||
*/
|
||||
std::vector<byte> Packet::Recv_buffer()
|
||||
{
|
||||
uint16 size = this->Recv_uint16();
|
||||
if (size == 0 || !this->CanReadFromPacket(size, true)) return {};
|
||||
|
||||
std::vector<byte> data;
|
||||
while (size-- > 0) {
|
||||
data.push_back(this->buffer[this->pos++]);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads characters (bytes) from the packet until it finds a '\0', or reaches a
|
||||
* maximum of \c length characters.
|
||||
|
||||
@@ -72,6 +72,7 @@ public:
|
||||
void Send_uint32(uint32 data);
|
||||
void Send_uint64(uint64 data);
|
||||
void Send_string(const std::string_view data);
|
||||
void Send_buffer(const std::vector<byte> &data);
|
||||
size_t Send_bytes (const byte *begin, const byte *end);
|
||||
|
||||
/* Reading/receiving of packets */
|
||||
@@ -87,6 +88,7 @@ public:
|
||||
uint16 Recv_uint16();
|
||||
uint32 Recv_uint32();
|
||||
uint64 Recv_uint64();
|
||||
std::vector<byte> Recv_buffer();
|
||||
std::string Recv_string(size_t length, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
|
||||
|
||||
size_t RemainingBytesToTransfer() const;
|
||||
|
||||
@@ -78,15 +78,15 @@ private:
|
||||
* lock on the game-state.
|
||||
*/
|
||||
enum class Status {
|
||||
INIT, ///< TCPConnecter is created but resolving hasn't started.
|
||||
RESOLVING, ///< The hostname is being resolved (threaded).
|
||||
FAILURE, ///< Resolving failed.
|
||||
CONNECTING, ///< We are currently connecting.
|
||||
CONNECTED, ///< The connection is established.
|
||||
Init, ///< TCPConnecter is created but resolving hasn't started.
|
||||
Resolving, ///< The hostname is being resolved (threaded).
|
||||
Failure, ///< Resolving failed.
|
||||
Connecting, ///< We are currently connecting.
|
||||
Connected, ///< The connection is established.
|
||||
};
|
||||
|
||||
std::thread resolve_thread; ///< Thread used during resolving.
|
||||
std::atomic<Status> status = Status::INIT; ///< The current status of the connecter.
|
||||
std::atomic<Status> status = Status::Init; ///< The current status of the connecter.
|
||||
std::atomic<bool> killed = false; ///< Whether this connecter is marked as killed.
|
||||
|
||||
addrinfo *ai = nullptr; ///< getaddrinfo() allocated linked-list of resolved addresses.
|
||||
|
||||
@@ -56,10 +56,11 @@ enum PacketAdminType {
|
||||
ADMIN_PACKET_SERVER_RCON, ///< The server's reply to a remove console command.
|
||||
ADMIN_PACKET_SERVER_CONSOLE, ///< The server gives the admin the data that got printed to its console.
|
||||
ADMIN_PACKET_SERVER_CMD_NAMES, ///< The server sends out the names of the DoCommands to the admins.
|
||||
ADMIN_PACKET_SERVER_CMD_LOGGING, ///< The server gives the admin copies of incoming command packets.
|
||||
ADMIN_PACKET_SERVER_CMD_LOGGING_OLD, ///< Used to be the type ID of \c ADMIN_PACKET_SERVER_CMD_LOGGING in \c NETWORK_GAME_ADMIN_VERSION 1.
|
||||
ADMIN_PACKET_SERVER_GAMESCRIPT, ///< The server gives the admin information from the GameScript in JSON.
|
||||
ADMIN_PACKET_SERVER_RCON_END, ///< The server indicates that the remote console command has completed.
|
||||
ADMIN_PACKET_SERVER_PONG, ///< The server replies to a ping request from the admin.
|
||||
ADMIN_PACKET_SERVER_CMD_LOGGING, ///< The server gives the admin copies of incoming command packets.
|
||||
|
||||
INVALID_ADMIN_PACKET = 0xFF, ///< An invalid marker for admin packets.
|
||||
};
|
||||
|
||||
@@ -52,7 +52,7 @@ TCPServerConnecter::TCPServerConnecter(const std::string &connection_string, uin
|
||||
break;
|
||||
|
||||
case SERVER_ADDRESS_INVITE_CODE:
|
||||
this->status = Status::CONNECTING;
|
||||
this->status = Status::Connecting;
|
||||
_network_coordinator_client.ConnectToServer(this->server_address.connection_string, this);
|
||||
break;
|
||||
|
||||
@@ -254,14 +254,14 @@ void TCPConnecter::Resolve()
|
||||
|
||||
if (error != 0) {
|
||||
Debug(net, 0, "Failed to resolve DNS for {}", this->connection_string);
|
||||
this->status = Status::FAILURE;
|
||||
this->status = Status::Failure;
|
||||
return;
|
||||
}
|
||||
|
||||
this->ai = ai;
|
||||
this->OnResolved(ai);
|
||||
|
||||
this->status = Status::CONNECTING;
|
||||
this->status = Status::Connecting;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -281,11 +281,11 @@ bool TCPConnecter::CheckActivity()
|
||||
if (this->killed) return true;
|
||||
|
||||
switch (this->status) {
|
||||
case Status::INIT:
|
||||
case Status::Init:
|
||||
/* Start the thread delayed, so the vtable is loaded. This allows classes
|
||||
* to overload functions used by Resolve() (in case threading is disabled). */
|
||||
if (StartNewThread(&this->resolve_thread, "ottd:resolve", &TCPConnecter::ResolveThunk, this)) {
|
||||
this->status = Status::RESOLVING;
|
||||
this->status = Status::Resolving;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -296,18 +296,18 @@ bool TCPConnecter::CheckActivity()
|
||||
* connection. The rest of this function handles exactly that. */
|
||||
break;
|
||||
|
||||
case Status::RESOLVING:
|
||||
case Status::Resolving:
|
||||
/* Wait till Resolve() comes back with an answer (in case it runs threaded). */
|
||||
return false;
|
||||
|
||||
case Status::FAILURE:
|
||||
case Status::Failure:
|
||||
/* Ensure the OnFailure() is called from the game-thread instead of the
|
||||
* resolve-thread, as otherwise we can get into some threading issues. */
|
||||
this->OnFailure();
|
||||
return true;
|
||||
|
||||
case Status::CONNECTING:
|
||||
case Status::CONNECTED:
|
||||
case Status::Connecting:
|
||||
case Status::Connected:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -403,7 +403,7 @@ bool TCPConnecter::CheckActivity()
|
||||
}
|
||||
|
||||
this->OnConnect(connected_socket);
|
||||
this->status = Status::CONNECTED;
|
||||
this->status = Status::Connected;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -422,11 +422,11 @@ bool TCPServerConnecter::CheckActivity()
|
||||
case SERVER_ADDRESS_INVITE_CODE:
|
||||
/* Check if a result has come in. */
|
||||
switch (this->status) {
|
||||
case Status::FAILURE:
|
||||
case Status::Failure:
|
||||
this->OnFailure();
|
||||
return true;
|
||||
|
||||
case Status::CONNECTED:
|
||||
case Status::Connected:
|
||||
this->OnConnect(this->socket);
|
||||
return true;
|
||||
|
||||
@@ -451,7 +451,7 @@ void TCPServerConnecter::SetConnected(SOCKET sock)
|
||||
assert(sock != INVALID_SOCKET);
|
||||
|
||||
this->socket = sock;
|
||||
this->status = Status::CONNECTED;
|
||||
this->status = Status::Connected;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -459,7 +459,7 @@ void TCPServerConnecter::SetConnected(SOCKET sock)
|
||||
*/
|
||||
void TCPServerConnecter::SetFailure()
|
||||
{
|
||||
this->status = Status::FAILURE;
|
||||
this->status = Status::Failure;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+20
-12
@@ -35,6 +35,7 @@
|
||||
#include "../core/pool_func.hpp"
|
||||
#include "../gfx_func.h"
|
||||
#include "../error.h"
|
||||
#include "../misc_cmd.h"
|
||||
#include <charconv>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
@@ -394,7 +395,7 @@ static void CheckPauseHelper(bool pause, PauseMode pm)
|
||||
{
|
||||
if (pause == ((_pause_mode & pm) != PM_UNPAUSED)) return;
|
||||
|
||||
DoCommandP(0, pm, pause ? 1 : 0, CMD_PAUSE);
|
||||
Command<CMD_PAUSE>::Post(pm, pause);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1064,8 +1065,8 @@ void NetworkGameLoop()
|
||||
while (f != nullptr && !feof(f)) {
|
||||
if (_date == next_date && _date_fract == next_date_fract) {
|
||||
if (cp != nullptr) {
|
||||
NetworkSendCommand(cp->tile, cp->p1, cp->p2, cp->cmd & ~CMD_FLAGS_MASK, nullptr, cp->text, cp->company);
|
||||
Debug(desync, 0, "Injecting: {:08x}; {:02x}; {:02x}; {:06x}; {:08x}; {:08x}; {:08x}; \"{}\" ({})", _date, _date_fract, (int)_current_company, cp->tile, cp->p1, cp->p2, cp->cmd, cp->text, GetCommandName(cp->cmd));
|
||||
NetworkSendCommand(cp->cmd, cp->err_msg, nullptr, cp->company, cp->data);
|
||||
Debug(desync, 0, "Injecting: {:08x}; {:02x}; {:02x}; {:08x}; {:06x}; {} ({})", _date, _date_fract, (int)_current_company, cp->cmd, cp->tile, FormatArrayAsHex(cp->data), GetCommandName(cp->cmd));
|
||||
delete cp;
|
||||
cp = nullptr;
|
||||
}
|
||||
@@ -1103,14 +1104,22 @@ void NetworkGameLoop()
|
||||
if (*p == ' ') p++;
|
||||
cp = new CommandPacket();
|
||||
int company;
|
||||
char buffer[128];
|
||||
int ret = sscanf(p, "%x; %x; %x; %x; %x; %x; %x; \"%127[^\"]\"", &next_date, &next_date_fract, &company, &cp->tile, &cp->p1, &cp->p2, &cp->cmd, buffer);
|
||||
cp->text = buffer;
|
||||
/* There are 8 pieces of data to read, however the last is a
|
||||
* string that might or might not exist. Ignore it if that
|
||||
* string misses because in 99% of the time it's not used. */
|
||||
assert(ret == 8 || ret == 7);
|
||||
uint cmd;
|
||||
char buffer[256];
|
||||
int ret = sscanf(p, "%x; %x; %x; %x; %x; %x; %255s", &next_date, &next_date_fract, &company, &cmd, &cp->err_msg, &cp->tile, buffer);
|
||||
assert(ret == 6);
|
||||
cp->company = (CompanyID)company;
|
||||
cp->cmd = (Commands)cmd;
|
||||
|
||||
/* Parse command data. */
|
||||
std::vector<byte> args;
|
||||
size_t arg_len = strlen(buffer);
|
||||
for (size_t i = 0; i + 1 < arg_len; i += 2) {
|
||||
byte e = 0;
|
||||
std::from_chars(buffer + i, buffer + i + 1, e, 16);
|
||||
args.emplace_back(e);
|
||||
}
|
||||
cp->data = args;
|
||||
} else if (strncmp(p, "join: ", 6) == 0) {
|
||||
/* Manually insert a pause when joining; this way the client can join at the exact right time. */
|
||||
int ret = sscanf(p + 6, "%x; %x", &next_date, &next_date_fract);
|
||||
@@ -1119,8 +1128,7 @@ void NetworkGameLoop()
|
||||
cp = new CommandPacket();
|
||||
cp->company = COMPANY_SPECTATOR;
|
||||
cp->cmd = CMD_PAUSE;
|
||||
cp->p1 = PM_PAUSED_NORMAL;
|
||||
cp->p2 = 1;
|
||||
cp->data = EndianBufferWriter<>::FromValue(CommandTraits<CMD_PAUSE>::Args{ PM_PAUSED_NORMAL, true });
|
||||
_ddc_fastforward = false;
|
||||
} else if (strncmp(p, "sync: ", 6) == 0) {
|
||||
int ret = sscanf(p + 6, "%x; %x; %x; %x", &next_date, &next_date_fract, &sync_state[0], &sync_state[1]);
|
||||
|
||||
@@ -332,8 +332,8 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyInfo(const Company
|
||||
p->Send_bool (c->is_ai);
|
||||
p->Send_uint8 (CeilDiv(c->months_of_bankruptcy, 3)); // send as quarters_of_bankruptcy
|
||||
|
||||
for (size_t i = 0; i < lengthof(c->share_owners); i++) {
|
||||
p->Send_uint8(c->share_owners[i]);
|
||||
for (auto owner : c->share_owners) {
|
||||
p->Send_uint8(owner);
|
||||
}
|
||||
|
||||
this->SendPacket(p);
|
||||
@@ -359,8 +359,8 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCompanyUpdate(const Compa
|
||||
p->Send_bool (NetworkCompanyIsPassworded(c->index));
|
||||
p->Send_uint8 (CeilDiv(c->months_of_bankruptcy, 3)); // send as quarters_of_bankruptcy
|
||||
|
||||
for (size_t i = 0; i < lengthof(c->share_owners); i++) {
|
||||
p->Send_uint8(c->share_owners[i]);
|
||||
for (auto owner : c->share_owners) {
|
||||
p->Send_uint8(owner);
|
||||
}
|
||||
|
||||
this->SendPacket(p);
|
||||
@@ -593,8 +593,8 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCmdNames()
|
||||
{
|
||||
Packet *p = new Packet(ADMIN_PACKET_SERVER_CMD_NAMES);
|
||||
|
||||
for (uint i = 0; i < CMD_END; i++) {
|
||||
const char *cmdname = GetCommandName(i);
|
||||
for (uint16 i = 0; i < CMD_END; i++) {
|
||||
const char *cmdname = GetCommandName(static_cast<Commands>(i));
|
||||
|
||||
/* Should COMPAT_MTU be exceeded, start a new packet
|
||||
* (magic 5: 1 bool "more data" and one uint16 "command id", one
|
||||
@@ -629,11 +629,8 @@ NetworkRecvStatus ServerNetworkAdminSocketHandler::SendCmdLogging(ClientID clien
|
||||
|
||||
p->Send_uint32(client_id);
|
||||
p->Send_uint8 (cp->company);
|
||||
p->Send_uint16(cp->cmd & CMD_ID_MASK);
|
||||
p->Send_uint32(cp->p1);
|
||||
p->Send_uint32(cp->p2);
|
||||
p->Send_uint32(cp->tile);
|
||||
p->Send_string(cp->text);
|
||||
p->Send_uint16(cp->cmd);
|
||||
p->Send_buffer(cp->data);
|
||||
p->Send_uint32(cp->frame);
|
||||
|
||||
this->SendPacket(p);
|
||||
|
||||
@@ -508,7 +508,7 @@ static const NWidgetPart _nested_chat_window_widgets[] = {
|
||||
NWidget(WWT_CLOSEBOX, COLOUR_GREY, WID_NC_CLOSE),
|
||||
NWidget(WWT_PANEL, COLOUR_GREY, WID_NC_BACKGROUND),
|
||||
NWidget(NWID_HORIZONTAL),
|
||||
NWidget(WWT_TEXT, COLOUR_GREY, WID_NC_DESTINATION), SetMinimalSize(62, 12), SetPadding(1, 0, 1, 0), SetTextColour(TC_BLACK), SetAlignment(SA_TOP | SA_RIGHT), SetDataTip(STR_NULL, STR_NULL),
|
||||
NWidget(WWT_TEXT, COLOUR_GREY, WID_NC_DESTINATION), SetMinimalSize(62, 12), SetPadding(1, 0, 1, 0), SetAlignment(SA_TOP | SA_RIGHT), SetDataTip(STR_NULL, STR_NULL),
|
||||
NWidget(WWT_EDITBOX, COLOUR_GREY, WID_NC_TEXTBOX), SetMinimalSize(100, 12), SetPadding(1, 0, 1, 0), SetResize(1, 0),
|
||||
SetDataTip(STR_NETWORK_CHAT_OSKTITLE, STR_NULL),
|
||||
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_NC_SENDBUTTON), SetMinimalSize(62, 12), SetPadding(1, 0, 1, 0), SetDataTip(STR_NETWORK_CHAT_SEND, STR_NULL),
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "../company_func.h"
|
||||
#include "../company_base.h"
|
||||
#include "../company_gui.h"
|
||||
#include "../company_cmd.h"
|
||||
#include "../core/random_func.hpp"
|
||||
#include "../date_func.h"
|
||||
#include "../gfx_func.h"
|
||||
@@ -840,7 +841,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet
|
||||
* the server will give us a client-id and let us in */
|
||||
_network_join_status = NETWORK_JOIN_STATUS_REGISTERING;
|
||||
ShowJoinStatusWindow();
|
||||
NetworkSendCommand(0, CCA_NEW, 0, CMD_COMPANY_CTRL, nullptr, {}, _local_company);
|
||||
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 */
|
||||
|
||||
+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);
|
||||
}
|
||||
|
||||
@@ -559,10 +559,6 @@ public:
|
||||
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
|
||||
{
|
||||
switch (widget) {
|
||||
case WID_NCL_FILTER_CAPT:
|
||||
*size = maxdim(*size, GetStringBoundingBox(STR_CONTENT_FILTER_TITLE));
|
||||
break;
|
||||
|
||||
case WID_NCL_CHECKBOX:
|
||||
size->width = this->checkbox_size.width + WD_MATRIX_RIGHT + WD_MATRIX_LEFT;
|
||||
break;
|
||||
@@ -587,10 +583,6 @@ public:
|
||||
void DrawWidget(const Rect &r, int widget) const override
|
||||
{
|
||||
switch (widget) {
|
||||
case WID_NCL_FILTER_CAPT:
|
||||
DrawString(r.left, r.right, r.top, STR_CONTENT_FILTER_TITLE, TC_FROMSTRING, SA_RIGHT);
|
||||
break;
|
||||
|
||||
case WID_NCL_DETAILS:
|
||||
this->DrawDetails(r);
|
||||
break;
|
||||
@@ -1029,7 +1021,7 @@ static const NWidgetPart _nested_network_content_list_widgets[] = {
|
||||
NWidget(NWID_SPACER), SetMinimalSize(0, 7), SetResize(1, 0),
|
||||
NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(8, 8, 8),
|
||||
/* Top */
|
||||
NWidget(WWT_EMPTY, COLOUR_LIGHT_BLUE, WID_NCL_FILTER_CAPT), SetFill(1, 0), SetResize(1, 0),
|
||||
NWidget(WWT_TEXT, COLOUR_LIGHT_BLUE, WID_NCL_FILTER_CAPT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_CONTENT_FILTER_TITLE, STR_NULL), SetAlignment(SA_RIGHT),
|
||||
NWidget(WWT_EDITBOX, COLOUR_LIGHT_BLUE, WID_NCL_FILTER), SetFill(1, 0), SetResize(1, 0),
|
||||
SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
|
||||
EndContainer(),
|
||||
|
||||
+39
-31
@@ -36,6 +36,7 @@
|
||||
#include "../zoom_func.h"
|
||||
#include "../sprite.h"
|
||||
#include "../settings_internal.h"
|
||||
#include "../company_cmd.h"
|
||||
|
||||
#include "../widgets/network_widget.h"
|
||||
|
||||
@@ -102,8 +103,7 @@ public:
|
||||
|
||||
leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_WHITE, WID_NG_INFO, STR_EMPTY, STR_NETWORK_SERVER_LIST_INFO_ICONS_TOOLTIP);
|
||||
leaf->SetMinimalSize(14 + GetSpriteSize(SPR_LOCK, nullptr, ZOOM_LVL_OUT_4X).width
|
||||
+ GetSpriteSize(SPR_BLOT, nullptr, ZOOM_LVL_OUT_4X).width
|
||||
+ GetSpriteSize(SPR_FLAGS_BASE, nullptr, ZOOM_LVL_OUT_4X).width, 12);
|
||||
+ GetSpriteSize(SPR_BLOT, nullptr, ZOOM_LVL_OUT_4X).width, 12);
|
||||
leaf->SetFill(0, 1);
|
||||
this->Add(leaf);
|
||||
|
||||
@@ -1335,15 +1335,17 @@ static const NWidgetPart _nested_client_list_widgets[] = {
|
||||
NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_CL_CLIENT_NAME_EDIT), SetMinimalSize(12, 14), SetDataTip(SPR_RENAME, STR_NETWORK_CLIENT_LIST_PLAYER_NAME_EDIT_TOOLTIP),
|
||||
EndContainer(),
|
||||
EndContainer(),
|
||||
NWidget(NWID_HORIZONTAL),
|
||||
NWidget(NWID_VERTICAL),
|
||||
NWidget(WWT_MATRIX, COLOUR_GREY, WID_CL_MATRIX), SetMinimalSize(180, 0), SetResize(1, 1), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_NULL), SetScrollbar(WID_CL_SCROLLBAR),
|
||||
EndContainer(),
|
||||
NWidget(NWID_HORIZONTAL),
|
||||
NWidget(NWID_VERTICAL),
|
||||
NWidget(WWT_MATRIX, COLOUR_GREY, WID_CL_MATRIX), SetMinimalSize(180, 0), SetResize(1, 1), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_NULL), SetScrollbar(WID_CL_SCROLLBAR),
|
||||
NWidget(WWT_PANEL, COLOUR_GREY),
|
||||
NWidget(WWT_TEXT, COLOUR_GREY, WID_CL_CLIENT_COMPANY_COUNT), SetFill(1, 0), SetMinimalTextLines(1, 0), SetResize(1, 0), SetPadding(2, 1, 2, 1), SetAlignment(SA_CENTER), SetDataTip(STR_NETWORK_CLIENT_LIST_CLIENT_COMPANY_COUNT, STR_NULL),
|
||||
EndContainer(),
|
||||
NWidget(NWID_VERTICAL),
|
||||
NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_CL_SCROLLBAR),
|
||||
NWidget(WWT_RESIZEBOX, COLOUR_GREY),
|
||||
EndContainer(),
|
||||
EndContainer(),
|
||||
NWidget(NWID_VERTICAL),
|
||||
NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_CL_SCROLLBAR),
|
||||
NWidget(WWT_RESIZEBOX, COLOUR_GREY),
|
||||
EndContainer(),
|
||||
EndContainer(),
|
||||
};
|
||||
@@ -1395,7 +1397,7 @@ static void AdminCompanyResetCallback(Window *w, bool confirmed)
|
||||
{
|
||||
if (confirmed) {
|
||||
if (NetworkCompanyHasClients(_admin_company_id)) return;
|
||||
DoCommandP(0, CCA_DELETE | _admin_company_id << 16 | CRR_MANUAL << 24, 0, CMD_COMPANY_CTRL);
|
||||
Command<CMD_COMPANY_CTRL>::Post(CCA_DELETE, _admin_company_id, CRR_MANUAL, INVALID_CLIENT_ID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1535,9 +1537,9 @@ private:
|
||||
static void OnClickCompanyNew(NetworkClientListWindow *w, Point pt, CompanyID company_id)
|
||||
{
|
||||
if (_network_server) {
|
||||
DoCommandP(0, CCA_NEW, _network_own_client_id, CMD_COMPANY_CTRL);
|
||||
Command<CMD_COMPANY_CTRL>::Post(CCA_NEW, INVALID_COMPANY, CRR_NONE, _network_own_client_id);
|
||||
} else {
|
||||
NetworkSendCommand(0, CCA_NEW, 0, CMD_COMPANY_CTRL, nullptr, {}, _local_company);
|
||||
Command<CMD_COMPANY_CTRL>::SendNet(STR_NULL, _local_company, CCA_NEW, INVALID_COMPANY, CRR_NONE, INVALID_CLIENT_ID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1713,6 +1715,11 @@ public:
|
||||
this->FinishInitNested(window_number);
|
||||
}
|
||||
|
||||
void OnInit() override
|
||||
{
|
||||
RebuildList();
|
||||
}
|
||||
|
||||
void OnInvalidateData(int data = 0, bool gui_scope = true) override
|
||||
{
|
||||
this->RebuildList();
|
||||
@@ -1771,9 +1778,11 @@ public:
|
||||
SetDParam(0, STR_NETWORK_CLIENT_LIST_SERVER_CONNECTION_TYPE_UNKNOWN + _network_server_connection_type);
|
||||
break;
|
||||
|
||||
case WID_CL_CLIENT_NAME:
|
||||
SetDParamStr(0, _settings_client.network.client_name);
|
||||
case WID_CL_CLIENT_NAME: {
|
||||
const NetworkClientInfo *own_ci = NetworkClientInfo::GetByClientID(_network_own_client_id);
|
||||
SetDParamStr(0, own_ci != nullptr ? own_ci->client_name : _settings_client.network.client_name);
|
||||
break;
|
||||
}
|
||||
|
||||
case WID_CL_CLIENT_COMPANY_COUNT:
|
||||
SetDParam(0, NetworkClientInfo::GetNumItems());
|
||||
@@ -1793,12 +1802,13 @@ public:
|
||||
ShowQueryString(STR_JUST_RAW_STRING, STR_NETWORK_CLIENT_LIST_SERVER_NAME_QUERY_CAPTION, NETWORK_NAME_LENGTH, this, CS_ALPHANUMERAL, QSF_LEN_IN_CHARS);
|
||||
break;
|
||||
|
||||
case WID_CL_CLIENT_NAME_EDIT:
|
||||
case WID_CL_CLIENT_NAME_EDIT: {
|
||||
const NetworkClientInfo *own_ci = NetworkClientInfo::GetByClientID(_network_own_client_id);
|
||||
this->query_widget = WID_CL_CLIENT_NAME_EDIT;
|
||||
SetDParamStr(0, _settings_client.network.client_name);
|
||||
SetDParamStr(0, own_ci != nullptr ? own_ci->client_name : _settings_client.network.client_name);
|
||||
ShowQueryString(STR_JUST_RAW_STRING, STR_NETWORK_CLIENT_LIST_PLAYER_NAME_QUERY_CAPTION, NETWORK_CLIENT_NAME_LENGTH, this, CS_ALPHANUMERAL, QSF_LEN_IN_CHARS);
|
||||
break;
|
||||
|
||||
}
|
||||
case WID_CL_SERVER_VISIBILITY:
|
||||
if (!_network_server) break;
|
||||
|
||||
@@ -2106,21 +2116,19 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
virtual void OnMouseLoop() override
|
||||
void OnMouseOver(Point pt, int widget) override
|
||||
{
|
||||
if (GetWidgetFromPos(this, _cursor.pos.x - this->left, _cursor.pos.y - this->top) != WID_CL_MATRIX) {
|
||||
this->hover_index = -1;
|
||||
this->SetDirty();
|
||||
return;
|
||||
}
|
||||
|
||||
NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_CL_MATRIX);
|
||||
int y = _cursor.pos.y - this->top - nwi->pos_y - 2;
|
||||
int index = y / this->line_height;
|
||||
|
||||
if (index != this->hover_index) {
|
||||
this->hover_index = index;
|
||||
this->SetDirty();
|
||||
if (widget != WID_CL_MATRIX) {
|
||||
if (this->hover_index != -1) {
|
||||
this->hover_index = -1;
|
||||
this->SetWidgetDirty(WID_CL_MATRIX);
|
||||
}
|
||||
} else {
|
||||
int index = this->GetRowFromWidget(pt.y, widget, 0, -1);
|
||||
if (index != this->hover_index) {
|
||||
this->hover_index = index;
|
||||
this->SetWidgetDirty(WID_CL_MATRIX);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
#include "core/tcp_game.h"
|
||||
|
||||
#include "../command_type.h"
|
||||
#include "../command_func.h"
|
||||
#include "../misc/endian_buffer.hpp"
|
||||
|
||||
#ifdef RANDOM_DEBUG
|
||||
/**
|
||||
@@ -104,19 +106,26 @@ void UpdateNetworkGameWindow();
|
||||
/**
|
||||
* Everything we need to know about a command to be able to execute it.
|
||||
*/
|
||||
struct CommandPacket : CommandContainer {
|
||||
struct CommandPacket {
|
||||
/** Make sure the pointer is nullptr. */
|
||||
CommandPacket() : next(nullptr), company(INVALID_COMPANY), frame(0), my_cmd(false) {}
|
||||
CommandPacket() : next(nullptr), company(INVALID_COMPANY), frame(0), my_cmd(false), tile(0) {}
|
||||
CommandPacket *next; ///< the next command packet (if in queue)
|
||||
CompanyID company; ///< company that is executing the command
|
||||
uint32 frame; ///< the frame in which this packet is executed
|
||||
bool my_cmd; ///< did the command originate from "me"
|
||||
|
||||
Commands cmd; ///< command being executed.
|
||||
StringID err_msg; ///< string ID of error message to use.
|
||||
CommandCallback *callback; ///< any callback function executed upon successful completion of the command.
|
||||
TileIndex tile; ///< location of the command (for e.g. error message or effect display).
|
||||
CommandDataBuffer data; ///< command parameters.
|
||||
};
|
||||
|
||||
void NetworkDistributeCommands();
|
||||
void NetworkExecuteLocalCommandQueue();
|
||||
void NetworkFreeLocalCommandQueue();
|
||||
void NetworkSyncCommandQueue(NetworkClientSocket *cs);
|
||||
void NetworkReplaceCommandClientId(CommandPacket &cp, ClientID client_id);
|
||||
|
||||
void ShowNetworkError(StringID error_string);
|
||||
void NetworkTextMessage(NetworkAction action, TextColour colour, bool self_send, const std::string &name, const std::string &str = "", int64 data = 0, const std::string &data_str = "");
|
||||
|
||||
@@ -24,10 +24,12 @@
|
||||
#include "../genworld.h"
|
||||
#include "../company_func.h"
|
||||
#include "../company_gui.h"
|
||||
#include "../company_cmd.h"
|
||||
#include "../roadveh.h"
|
||||
#include "../order_backup.h"
|
||||
#include "../core/pool_func.hpp"
|
||||
#include "../core/random_func.hpp"
|
||||
#include "../company_cmd.h"
|
||||
#include "../rev.h"
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
@@ -1043,12 +1045,12 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND(Packet
|
||||
|
||||
|
||||
if ((GetCommandFlags(cp.cmd) & CMD_SERVER) && ci->client_id != CLIENT_ID_SERVER) {
|
||||
IConsolePrint(CC_WARNING, "Kicking client #{} (IP: {}) due to calling a server only command {}.", ci->client_id, this->GetClientIP(), cp.cmd & CMD_ID_MASK);
|
||||
IConsolePrint(CC_WARNING, "Kicking client #{} (IP: {}) due to calling a server only command {}.", ci->client_id, this->GetClientIP(), cp.cmd);
|
||||
return this->SendError(NETWORK_ERROR_KICKED);
|
||||
}
|
||||
|
||||
if ((GetCommandFlags(cp.cmd) & CMD_SPECTATOR) == 0 && !Company::IsValidID(cp.company) && ci->client_id != CLIENT_ID_SERVER) {
|
||||
IConsolePrint(CC_WARNING, "Kicking client #{} (IP: {}) due to calling a non-spectator command {}.", ci->client_id, this->GetClientIP(), cp.cmd & CMD_ID_MASK);
|
||||
IConsolePrint(CC_WARNING, "Kicking client #{} (IP: {}) due to calling a non-spectator command {}.", ci->client_id, this->GetClientIP(), cp.cmd);
|
||||
return this->SendError(NETWORK_ERROR_KICKED);
|
||||
}
|
||||
|
||||
@@ -1057,14 +1059,15 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND(Packet
|
||||
* to match the company in the packet. If it doesn't, the client has done
|
||||
* something pretty naughty (or a bug), and will be kicked
|
||||
*/
|
||||
if (!(cp.cmd == CMD_COMPANY_CTRL && cp.p1 == 0 && ci->client_playas == COMPANY_NEW_COMPANY) && ci->client_playas != cp.company) {
|
||||
CompanyCtrlAction cca = cp.cmd == CMD_COMPANY_CTRL ? std::get<0>(EndianBufferReader::ToValue<CommandTraits<CMD_COMPANY_CTRL>::Args>(cp.data)) : CCA_NEW;
|
||||
if (!(cp.cmd == CMD_COMPANY_CTRL && cca == CCA_NEW && ci->client_playas == COMPANY_NEW_COMPANY) && ci->client_playas != cp.company) {
|
||||
IConsolePrint(CC_WARNING, "Kicking client #{} (IP: {}) due to calling a command as another company {}.",
|
||||
ci->client_playas + 1, this->GetClientIP(), cp.company + 1);
|
||||
return this->SendError(NETWORK_ERROR_COMPANY_MISMATCH);
|
||||
}
|
||||
|
||||
if (cp.cmd == CMD_COMPANY_CTRL) {
|
||||
if (cp.p1 != 0 || cp.company != COMPANY_SPECTATOR) {
|
||||
if (cca != CCA_NEW || cp.company != COMPANY_SPECTATOR) {
|
||||
return this->SendError(NETWORK_ERROR_CHEATER);
|
||||
}
|
||||
|
||||
@@ -1075,7 +1078,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_COMMAND(Packet
|
||||
}
|
||||
}
|
||||
|
||||
if (GetCommandFlags(cp.cmd) & CMD_CLIENT_ID) cp.p2 = this->client_id;
|
||||
if (GetCommandFlags(cp.cmd) & CMD_CLIENT_ID) NetworkReplaceCommandClientId(cp, this->client_id);
|
||||
|
||||
this->incoming_queue.Append(&cp);
|
||||
return NETWORK_RECV_STATUS_OKAY;
|
||||
@@ -1562,7 +1565,7 @@ static void NetworkAutoCleanCompanies()
|
||||
/* Is the company empty for autoclean_unprotected-months, and is there no protection? */
|
||||
if (_settings_client.network.autoclean_unprotected != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_unprotected && _network_company_states[c->index].password.empty()) {
|
||||
/* Shut the company down */
|
||||
DoCommandP(0, CCA_DELETE | c->index << 16 | CRR_AUTOCLEAN << 24, 0, CMD_COMPANY_CTRL);
|
||||
Command<CMD_COMPANY_CTRL>::Post(CCA_DELETE, c->index, CRR_AUTOCLEAN, INVALID_CLIENT_ID);
|
||||
IConsolePrint(CC_INFO, "Auto-cleaned company #{} with no password.", c->index + 1);
|
||||
}
|
||||
/* Is the company empty for autoclean_protected-months, and there is a protection? */
|
||||
@@ -1576,7 +1579,7 @@ static void NetworkAutoCleanCompanies()
|
||||
/* Is the company empty for autoclean_novehicles-months, and has no vehicles? */
|
||||
if (_settings_client.network.autoclean_novehicles != 0 && _network_company_states[c->index].months_empty > _settings_client.network.autoclean_novehicles && vehicles_in_company[c->index] == 0) {
|
||||
/* Shut the company down */
|
||||
DoCommandP(0, CCA_DELETE | c->index << 16 | CRR_AUTOCLEAN << 24, 0, CMD_COMPANY_CTRL);
|
||||
Command<CMD_COMPANY_CTRL>::Post(CCA_DELETE, c->index, CRR_AUTOCLEAN, INVALID_CLIENT_ID);
|
||||
IConsolePrint(CC_INFO, "Auto-cleaned company #{} with no vehicles.", c->index + 1);
|
||||
}
|
||||
} else {
|
||||
@@ -2087,7 +2090,7 @@ void NetworkServerNewCompany(const Company *c, NetworkClientInfo *ci)
|
||||
/* ci is nullptr when replaying, or for AIs. In neither case there is a client. */
|
||||
ci->client_playas = c->index;
|
||||
NetworkUpdateClientInfo(ci->client_id);
|
||||
NetworkSendCommand(0, 0, 0, CMD_RENAME_PRESIDENT, nullptr, ci->client_name, c->index);
|
||||
Command<CMD_RENAME_PRESIDENT>::SendNet(STR_NULL, c->index, ci->client_name);
|
||||
}
|
||||
|
||||
/* Announce new company on network. */
|
||||
|
||||
Reference in New Issue
Block a user