mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Template DoCommandPInternal.
parent
6691ee3b96
commit
ccefa76a46
138
src/command.cpp
138
src/command.cpp
|
@ -274,53 +274,26 @@ void CommandHelperBase::InternalPostResult(const CommandCost &res, TileIndex til
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Helper to format command parameters into a hex string. */
|
/** Helper to make a desync log for a command. */
|
||||||
static std::string CommandParametersToHexString(TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
|
void CommandHelperBase::LogCommandExecution(Commands cmd, StringID err_message, TileIndex tile, const CommandDataBuffer &args, bool failed)
|
||||||
{
|
{
|
||||||
return FormatArrayAsHex(EndianBufferWriter<>::FromValue(std::make_tuple(tile, p1, p2, text)));
|
Debug(desync, 1, "{}: {:08x}; {:02x}; {:02x}; {:08x}; {:08x}; {:06x}; {} ({})", failed ? "cmdf" : "cmd", _date, _date_fract, (int)_current_company, cmd, err_message, tile, FormatArrayAsHex(args), GetCommandName(cmd));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/**
|
||||||
* Helper function for the toplevel network safe docommand function for the current company.
|
* Prepare for the test run of a command proc call.
|
||||||
*
|
* @param cmd_flags Command flags.
|
||||||
* @param cmd The command to execute (a CMD_* value)
|
* @param tile Tile of command execution.
|
||||||
* @param err_message Message prefix to show on error
|
* @param[in,out] cur_company Backup of current company at start of command execution.
|
||||||
* @param callback A callback function to call after the command is finished
|
* @return True if test run can go ahead, false on error.
|
||||||
* @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 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 text The text to pass
|
|
||||||
* @return the command cost of this function.
|
|
||||||
*/
|
*/
|
||||||
CommandCost DoCommandPInternal(Commands cmd, StringID err_message, CommandCallback *callback, bool my_cmd, bool estimate_only, bool network_command, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
|
bool CommandHelperBase::InternalExecutePrepTest(CommandFlags cmd_flags, TileIndex tile, Backup<CompanyID> &cur_company)
|
||||||
{
|
{
|
||||||
RecursiveCommandCounter counter{};
|
|
||||||
|
|
||||||
/* Prevent recursion; it gives a mess over the network */
|
|
||||||
assert(counter.IsTopLevel());
|
|
||||||
|
|
||||||
/* Reset the state. */
|
/* Reset the state. */
|
||||||
_additional_cash_required = 0;
|
_additional_cash_required = 0;
|
||||||
|
|
||||||
/* Get pointer to command handler */
|
|
||||||
assert(cmd < _command_proc_table.size());
|
|
||||||
CommandProc *proc = _command_proc_table[cmd].proc;
|
|
||||||
/* Shouldn't happen, but you never know when someone adds
|
|
||||||
* NULLs to the _command_proc_table. */
|
|
||||||
assert(proc != nullptr);
|
|
||||||
|
|
||||||
/* Command flags are used internally */
|
|
||||||
CommandFlags cmd_flags = GetCommandFlags(cmd);
|
|
||||||
/* Flags get send to the DoCommand */
|
|
||||||
DoCommandFlag flags = CommandFlagsToDCFlags(cmd_flags);
|
|
||||||
|
|
||||||
/* Make sure p2 is properly set to a ClientID. */
|
|
||||||
assert(!(cmd_flags & CMD_CLIENT_ID) || p2 != 0);
|
|
||||||
|
|
||||||
/* Do not even think about executing out-of-bounds tile-commands */
|
/* Do not even think about executing out-of-bounds tile-commands */
|
||||||
if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (cmd_flags & CMD_ALL_TILES) == 0))) return CMD_ERROR;
|
if (tile != 0 && (tile >= MapSize() || (!IsValidTile(tile) && (cmd_flags & CMD_ALL_TILES) == 0))) return false;
|
||||||
|
|
||||||
/* Always execute server and spectator commands as spectator */
|
/* Always execute server and spectator commands as spectator */
|
||||||
bool exec_as_spectator = (cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0;
|
bool exec_as_spectator = (cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0;
|
||||||
|
@ -329,62 +302,68 @@ CommandCost DoCommandPInternal(Commands cmd, StringID err_message, CommandCallba
|
||||||
* The server will ditch any server commands a client sends to it, so effectively
|
* The server will ditch any server commands a client sends to it, so effectively
|
||||||
* this guards the server from executing functions for an invalid company. */
|
* this guards the server from executing functions for an invalid company. */
|
||||||
if (_game_mode == GM_NORMAL && !exec_as_spectator && !Company::IsValidID(_current_company) && !(_current_company == OWNER_DEITY && (cmd_flags & CMD_DEITY) != 0)) {
|
if (_game_mode == GM_NORMAL && !exec_as_spectator && !Company::IsValidID(_current_company) && !(_current_company == OWNER_DEITY && (cmd_flags & CMD_DEITY) != 0)) {
|
||||||
return CMD_ERROR;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Backup<CompanyID> cur_company(_current_company, FILE_LINE);
|
|
||||||
if (exec_as_spectator) cur_company.Change(COMPANY_SPECTATOR);
|
if (exec_as_spectator) cur_company.Change(COMPANY_SPECTATOR);
|
||||||
|
|
||||||
bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
|
/* Enter test mode. */
|
||||||
|
|
||||||
/* Test the command. */
|
|
||||||
_cleared_object_areas.clear();
|
_cleared_object_areas.clear();
|
||||||
SetTownRatingTestMode(true);
|
SetTownRatingTestMode(true);
|
||||||
BasePersistentStorageArray::SwitchMode(PSM_ENTER_TESTMODE);
|
BasePersistentStorageArray::SwitchMode(PSM_ENTER_TESTMODE);
|
||||||
CommandCost res = proc(flags, tile, p1, p2, text);
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate result of test run and prepare for real execution.
|
||||||
|
* @param cmd_flags Command flags.
|
||||||
|
* @param[in,out] res Command result of test run, may be modified.
|
||||||
|
* @param estimate_only Is this just cost estimation?
|
||||||
|
* @param network_command Does this command come from the network?
|
||||||
|
* @param[in,out] cur_company Backup of current company at start of command execution.
|
||||||
|
* @return True if test run can go ahead, false on error.
|
||||||
|
*/
|
||||||
|
std::tuple<bool, bool, bool> CommandHelperBase::InternalExecuteValidateTestAndPrepExec(CommandCost &res, CommandFlags cmd_flags, bool estimate_only, bool network_command, Backup<CompanyID> &cur_company)
|
||||||
|
{
|
||||||
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_TESTMODE);
|
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_TESTMODE);
|
||||||
SetTownRatingTestMode(false);
|
SetTownRatingTestMode(false);
|
||||||
|
|
||||||
/* Make sure we're not messing things up here. */
|
/* Make sure we're not messing things up here. */
|
||||||
assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
|
assert((cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0 ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
|
||||||
|
|
||||||
/* If the command fails, we're doing an estimate
|
/* If the command fails, we're doing an estimate
|
||||||
* or the player does not have enough money
|
* or the player does not have enough money
|
||||||
* (unless it's a command where the test and
|
* (unless it's a command where the test and
|
||||||
* execution phase might return different costs)
|
* execution phase might return different costs)
|
||||||
* we bail out here. */
|
* we bail out here. */
|
||||||
if (res.Failed() || estimate_only ||
|
bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
|
||||||
(!test_and_exec_can_differ && !CheckCompanyHasMoney(res))) {
|
if (res.Failed() || estimate_only || (!test_and_exec_can_differ && !CheckCompanyHasMoney(res))) {
|
||||||
if (!_networking || _generating_world || network_command) {
|
return { true, !_networking || _generating_world || network_command, false };
|
||||||
/* Log the failed command as well. Just to be able to be find
|
|
||||||
* causes of desyncs due to bad command test implementations. */
|
|
||||||
Debug(desync, 1, "cmdf: {:08x}; {:02x}; {:02x}; {:08x}; {:08x}; {:06x}; {} ({})", _date, _date_fract, (int)_current_company, cmd, err_message, tile, CommandParametersToHexString(tile, p1, p2, text), GetCommandName(cmd));
|
|
||||||
}
|
|
||||||
cur_company.Restore();
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
bool send_net = _networking && !_generating_world && !network_command;
|
||||||
* If we are in network, and the command is not from the network
|
|
||||||
* send it to the command-queue and abort execution
|
|
||||||
*/
|
|
||||||
if (_networking && !_generating_world && !network_command) {
|
|
||||||
NetworkSendCommand(cmd, err_message, callback, _current_company, tile, p1, p2, text);
|
|
||||||
cur_company.Restore();
|
|
||||||
|
|
||||||
/* Don't return anything special here; no error, no costs.
|
if (!send_net) {
|
||||||
* This way it's not handled by DoCommand and only the
|
/* Prepare for command execution. */
|
||||||
* actual execution of the command causes messages. Also
|
_cleared_object_areas.clear();
|
||||||
* reset the storages as we've not executed the command. */
|
BasePersistentStorageArray::SwitchMode(PSM_ENTER_COMMAND);
|
||||||
return CommandCost();
|
|
||||||
}
|
}
|
||||||
Debug(desync, 1, "cmd: {:08x}; {:02x}; {:02x}; {:08x}; {:08x}; {:06x}; {} ({})", _date, _date_fract, (int)_current_company, cmd, err_message, tile, CommandParametersToHexString(tile, p1, p2, text), GetCommandName(cmd));
|
|
||||||
|
|
||||||
/* Actually try and execute the command. If no cost-type is given
|
return { false, _debug_desync_level >= 1, send_net };
|
||||||
* use the construction one */
|
}
|
||||||
_cleared_object_areas.clear();
|
|
||||||
BasePersistentStorageArray::SwitchMode(PSM_ENTER_COMMAND);
|
/**
|
||||||
CommandCost res2 = proc(flags | DC_EXEC, tile, p1, p2, text);
|
* Process the result of a command test run and execution run.
|
||||||
|
* @param cmd Command that was executed.
|
||||||
|
* @param cmd_flags Command flags.
|
||||||
|
* @param res_test Command result of test run.
|
||||||
|
* @param tes_exec Command result of real run.
|
||||||
|
* @param tile Tile of command execution.
|
||||||
|
* @param[in,out] cur_company Backup of current company at start of command execution.
|
||||||
|
* @return Final command result.
|
||||||
|
*/
|
||||||
|
CommandCost CommandHelperBase::InternalExecuteProcessResult(Commands cmd, CommandFlags cmd_flags, const CommandCost &res_test, const CommandCost &res_exec, TileIndex tile, Backup<CompanyID> &cur_company)
|
||||||
|
{
|
||||||
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_COMMAND);
|
BasePersistentStorageArray::SwitchMode(PSM_LEAVE_COMMAND);
|
||||||
|
|
||||||
if (cmd == CMD_COMPANY_CTRL) {
|
if (cmd == CMD_COMPANY_CTRL) {
|
||||||
|
@ -395,7 +374,7 @@ CommandCost DoCommandPInternal(Commands cmd, StringID err_message, CommandCallba
|
||||||
_current_company = _local_company;
|
_current_company = _local_company;
|
||||||
} else {
|
} else {
|
||||||
/* Make sure nothing bad happened, like changing the current company. */
|
/* Make sure nothing bad happened, like changing the current company. */
|
||||||
assert(exec_as_spectator ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
|
assert((cmd_flags & (CMD_SPECTATOR | CMD_SERVER)) != 0 ? _current_company == COMPANY_SPECTATOR : cur_company.Verify());
|
||||||
cur_company.Restore();
|
cur_company.Restore();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -403,15 +382,16 @@ CommandCost DoCommandPInternal(Commands cmd, StringID err_message, CommandCallba
|
||||||
* return of the command. Otherwise we can check whether the
|
* return of the command. Otherwise we can check whether the
|
||||||
* test and execution have yielded the same result,
|
* test and execution have yielded the same result,
|
||||||
* i.e. cost and error state are the same. */
|
* i.e. cost and error state are the same. */
|
||||||
|
bool test_and_exec_can_differ = (cmd_flags & CMD_NO_TEST) != 0;
|
||||||
if (!test_and_exec_can_differ) {
|
if (!test_and_exec_can_differ) {
|
||||||
assert(res.GetCost() == res2.GetCost() && res.Failed() == res2.Failed()); // sanity check
|
assert(res_test.GetCost() == res_exec.GetCost() && res_test.Failed() == res_exec.Failed()); // sanity check
|
||||||
} else if (res2.Failed()) {
|
} else if (res_exec.Failed()) {
|
||||||
return res2;
|
return res_exec;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If we're needing more money and we haven't done
|
/* If we're needing more money and we haven't done
|
||||||
* anything yet, ask for the money! */
|
* anything yet, ask for the money! */
|
||||||
if (_additional_cash_required != 0 && res2.GetCost() == 0) {
|
if (_additional_cash_required != 0 && res_exec.GetCost() == 0) {
|
||||||
/* It could happen we removed rail, thus gained money, and deleted something else.
|
/* It could happen we removed rail, thus gained money, and deleted something else.
|
||||||
* So make sure the signal buffer is empty even in this case */
|
* So make sure the signal buffer is empty even in this case */
|
||||||
UpdateSignalsInBuffer();
|
UpdateSignalsInBuffer();
|
||||||
|
@ -425,12 +405,12 @@ CommandCost DoCommandPInternal(Commands cmd, StringID err_message, CommandCallba
|
||||||
if (c != nullptr) c->last_build_coordinate = tile;
|
if (c != nullptr) c->last_build_coordinate = tile;
|
||||||
}
|
}
|
||||||
|
|
||||||
SubtractMoneyFromCompany(res2);
|
SubtractMoneyFromCompany(res_exec);
|
||||||
|
|
||||||
/* update signals if needed */
|
/* update signals if needed */
|
||||||
UpdateSignalsInBuffer();
|
UpdateSignalsInBuffer();
|
||||||
|
|
||||||
return res2;
|
return res_exec;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,8 @@
|
||||||
|
|
||||||
#include "command_type.h"
|
#include "command_type.h"
|
||||||
#include "company_type.h"
|
#include "company_type.h"
|
||||||
|
#include "company_func.h"
|
||||||
|
#include "core/backup_type.hpp"
|
||||||
#include "misc/endian_buffer.hpp"
|
#include "misc/endian_buffer.hpp"
|
||||||
#include "tile_map.h"
|
#include "tile_map.h"
|
||||||
|
|
||||||
|
@ -34,9 +36,6 @@ static const CommandCost CMD_ERROR = CommandCost(INVALID_STRING_ID);
|
||||||
*/
|
*/
|
||||||
#define return_cmd_error(errcode) return CommandCost(errcode);
|
#define return_cmd_error(errcode) return CommandCost(errcode);
|
||||||
|
|
||||||
CommandCost DoCommandPInternal(Commands cmd, StringID err_message, CommandCallback *callback, bool my_cmd, bool estimate_only, bool network_command, TileIndex tile, uint32 p1, uint32 p2, const std::string &text);
|
|
||||||
|
|
||||||
void NetworkSendCommand(Commands cmd, StringID err_message, CommandCallback *callback, CompanyID company, TileIndex tile, uint32 p1, uint32 p2, const std::string &text);
|
|
||||||
void NetworkSendCommand(Commands cmd, StringID err_message, CommandCallback *callback, CompanyID company, TileIndex location, const CommandDataBuffer &cmd_data);
|
void NetworkSendCommand(Commands cmd, StringID err_message, CommandCallback *callback, CompanyID company, TileIndex location, const CommandDataBuffer &cmd_data);
|
||||||
|
|
||||||
extern Money _additional_cash_required;
|
extern Money _additional_cash_required;
|
||||||
|
@ -87,6 +86,10 @@ protected:
|
||||||
static void InternalDoAfter(CommandCost &res, DoCommandFlag flags, bool top_level, bool test);
|
static void InternalDoAfter(CommandCost &res, DoCommandFlag flags, bool top_level, bool test);
|
||||||
static std::tuple<bool, bool, bool> InternalPostBefore(Commands cmd, CommandFlags flags, TileIndex tile, StringID err_message, bool network_command);
|
static std::tuple<bool, bool, bool> InternalPostBefore(Commands cmd, CommandFlags flags, TileIndex tile, StringID err_message, bool network_command);
|
||||||
static void InternalPostResult(const CommandCost &res, TileIndex tile, bool estimate_only, bool only_sending, StringID err_message, bool my_cmd);
|
static void InternalPostResult(const CommandCost &res, TileIndex tile, bool estimate_only, bool only_sending, StringID err_message, bool my_cmd);
|
||||||
|
static bool InternalExecutePrepTest(CommandFlags cmd_flags, TileIndex tile, Backup<CompanyID> &cur_company);
|
||||||
|
static std::tuple<bool, bool, bool> InternalExecuteValidateTestAndPrepExec(CommandCost &res, CommandFlags cmd_flags, bool estimate_only, bool network_command, Backup<CompanyID> &cur_company);
|
||||||
|
static CommandCost InternalExecuteProcessResult(Commands cmd, CommandFlags cmd_flags, const CommandCost &res_test, const CommandCost &res_exec, TileIndex tile, Backup<CompanyID> &cur_company);
|
||||||
|
static void LogCommandExecution(Commands cmd, StringID err_message, TileIndex tile, const CommandDataBuffer &args, bool failed);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -186,6 +189,41 @@ public:
|
||||||
return InternalPost(err_message, callback, my_cmd, true, location, std::move(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)
|
||||||
|
{
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
return Execute(err_message, callback, my_cmd, estimate_only, false, location, std::move(args));
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static bool InternalPost(StringID err_message, CommandCallback *callback, bool my_cmd, bool network_command, std::tuple<Targs...> args)
|
static bool InternalPost(StringID err_message, CommandCallback *callback, bool my_cmd, bool network_command, std::tuple<Targs...> args)
|
||||||
{
|
{
|
||||||
|
@ -206,7 +244,7 @@ protected:
|
||||||
/* Only set p2 when the command does not come from the network. */
|
/* Only set p2 when the command does not come from the network. */
|
||||||
if (!network_command && GetCommandFlags<Tcmd>() & CMD_CLIENT_ID && std::get<2>(args) == 0) std::get<2>(args) = CLIENT_ID_SERVER;
|
if (!network_command && GetCommandFlags<Tcmd>() & CMD_CLIENT_ID && std::get<2>(args) == 0) std::get<2>(args) = CLIENT_ID_SERVER;
|
||||||
|
|
||||||
CommandCost res = std::apply(DoCommandPInternal, std::tuple_cat(std::make_tuple(Tcmd, err_message, callback, my_cmd, estimate_only, network_command), args));
|
CommandCost res = Execute(err_message, callback, my_cmd, estimate_only, network_command, tile, args);
|
||||||
InternalPostResult(res, tile, estimate_only, only_sending, err_message, my_cmd);
|
InternalPostResult(res, tile, estimate_only, only_sending, err_message, my_cmd);
|
||||||
|
|
||||||
if (!estimate_only && !only_sending && callback != nullptr) {
|
if (!estimate_only && !only_sending && callback != nullptr) {
|
||||||
|
@ -215,6 +253,56 @@ protected:
|
||||||
|
|
||||||
return res.Succeeded();
|
return res.Succeeded();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static CommandCost Execute(StringID err_message, CommandCallback *callback, bool my_cmd, bool estimate_only, bool network_command, TileIndex tile, std::tuple<Targs...> args)
|
||||||
|
{
|
||||||
|
/* Prevent recursion; it gives a mess over the network */
|
||||||
|
RecursiveCommandCounter counter{};
|
||||||
|
assert(counter.IsTopLevel());
|
||||||
|
|
||||||
|
/* Command flags are used internally */
|
||||||
|
CommandFlags cmd_flags = GetCommandFlags<Tcmd>();
|
||||||
|
|
||||||
|
/* Make sure p2 is properly set to a ClientID also when processing external commands. */
|
||||||
|
assert(!(cmd_flags & CMD_CLIENT_ID) || std::get<2>(args) != 0);
|
||||||
|
|
||||||
|
Backup<CompanyID> cur_company(_current_company, FILE_LINE);
|
||||||
|
if (!InternalExecutePrepTest(cmd_flags, tile, cur_company)) {
|
||||||
|
cur_company.Trash();
|
||||||
|
return CMD_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Test the command. */
|
||||||
|
DoCommandFlag flags = CommandFlagsToDCFlags(cmd_flags);
|
||||||
|
CommandCost res = std::apply(CommandTraits<Tcmd>::proc, std::tuple_cat(std::make_tuple(flags), args));
|
||||||
|
|
||||||
|
auto [exit_test, desync_log, send_net] = InternalExecuteValidateTestAndPrepExec(res, cmd_flags, estimate_only, network_command, cur_company);
|
||||||
|
if (exit_test) {
|
||||||
|
if (desync_log) LogCommandExecution(Tcmd, err_message, tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), true);
|
||||||
|
cur_company.Restore();
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If we are in network, and the command is not from the network
|
||||||
|
* send it to the command-queue and abort execution. */
|
||||||
|
if (send_net) {
|
||||||
|
::NetworkSendCommand(Tcmd, err_message, callback, _current_company, tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args));
|
||||||
|
cur_company.Restore();
|
||||||
|
|
||||||
|
/* Don't return anything special here; no error, no costs.
|
||||||
|
* This way it's not handled by DoCommand and only the
|
||||||
|
* actual execution of the command causes messages. Also
|
||||||
|
* reset the storages as we've not executed the command. */
|
||||||
|
return CommandCost();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (desync_log) LogCommandExecution(Tcmd, err_message, tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), false);
|
||||||
|
|
||||||
|
/* Actually try and execute the command. */
|
||||||
|
CommandCost res2 = std::apply(CommandTraits<Tcmd>::proc, std::tuple_cat(std::make_tuple(flags | DC_EXEC), args));
|
||||||
|
|
||||||
|
return InternalExecuteProcessResult(Tcmd, cmd_flags, res, res2, tile, cur_company);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <Commands Tcmd>
|
template <Commands Tcmd>
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
#include "../company_func.h"
|
#include "../company_func.h"
|
||||||
#include "../company_base.h"
|
#include "../company_base.h"
|
||||||
#include "../company_gui.h"
|
#include "../company_gui.h"
|
||||||
|
#include "../company_cmd.h"
|
||||||
#include "../core/random_func.hpp"
|
#include "../core/random_func.hpp"
|
||||||
#include "../date_func.h"
|
#include "../date_func.h"
|
||||||
#include "../gfx_func.h"
|
#include "../gfx_func.h"
|
||||||
|
@ -839,7 +840,7 @@ NetworkRecvStatus ClientNetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet
|
||||||
* the server will give us a client-id and let us in */
|
* the server will give us a client-id and let us in */
|
||||||
_network_join_status = NETWORK_JOIN_STATUS_REGISTERING;
|
_network_join_status = NETWORK_JOIN_STATUS_REGISTERING;
|
||||||
ShowJoinStatusWindow();
|
ShowJoinStatusWindow();
|
||||||
NetworkSendCommand(CMD_COMPANY_CTRL, STR_NULL, nullptr, _local_company, 0, CCA_NEW, 0, {});
|
Command<CMD_COMPANY_CTRL>::SendNet(STR_NULL, nullptr, _local_company, 0, CCA_NEW, 0, {});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/* take control over an existing company */
|
/* take control over an existing company */
|
||||||
|
|
|
@ -176,22 +176,6 @@ static CommandQueue _local_wait_queue;
|
||||||
/** Local queue of packets waiting for execution. */
|
/** Local queue of packets waiting for execution. */
|
||||||
static CommandQueue _local_execution_queue;
|
static CommandQueue _local_execution_queue;
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 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 text The text to pass
|
|
||||||
*/
|
|
||||||
void NetworkSendCommand(Commands cmd, StringID err_message, CommandCallback *callback, CompanyID company, TileIndex tile, uint32 p1, uint32 p2, const std::string &text)
|
|
||||||
{
|
|
||||||
auto data = EndianBufferWriter<CommandDataBuffer>::FromValue(std::make_tuple(tile, p1, p2, text));
|
|
||||||
NetworkSendCommand(cmd, err_message, callback, company, tile, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare a DoCommand to be send over the network
|
* Prepare a DoCommand to be send over the network
|
||||||
|
|
|
@ -1538,7 +1538,7 @@ private:
|
||||||
if (_network_server) {
|
if (_network_server) {
|
||||||
Command<CMD_COMPANY_CTRL>::Post(0, CCA_NEW, _network_own_client_id, {});
|
Command<CMD_COMPANY_CTRL>::Post(0, CCA_NEW, _network_own_client_id, {});
|
||||||
} else {
|
} else {
|
||||||
NetworkSendCommand(CMD_COMPANY_CTRL, STR_NULL, nullptr, _local_company, 0, CCA_NEW, 0, {});
|
Command<CMD_COMPANY_CTRL>::SendNet(STR_NULL, nullptr, _local_company, 0, CCA_NEW, 0, {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2081,7 +2081,7 @@ void NetworkServerNewCompany(const Company *c, NetworkClientInfo *ci)
|
||||||
/* ci is nullptr when replaying, or for AIs. In neither case there is a client. */
|
/* ci is nullptr when replaying, or for AIs. In neither case there is a client. */
|
||||||
ci->client_playas = c->index;
|
ci->client_playas = c->index;
|
||||||
NetworkUpdateClientInfo(ci->client_id);
|
NetworkUpdateClientInfo(ci->client_id);
|
||||||
NetworkSendCommand(CMD_RENAME_PRESIDENT, STR_NULL, nullptr, c->index, 0, 0, 0, ci->client_name);
|
Command<CMD_RENAME_PRESIDENT>::SendNet(STR_NULL, nullptr, c->index, 0, 0, 0, ci->client_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Announce new company on network. */
|
/* Announce new company on network. */
|
||||||
|
|
|
@ -200,7 +200,7 @@ CommandCost CmdClearOrderBackup(DoCommandFlag flags, TileIndex tile, uint32 p1,
|
||||||
/* We need to circumvent the "prevention" from this command being executed
|
/* 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
|
* 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. */
|
* this command to get its cost estimated when shift is pressed. */
|
||||||
DoCommandPInternal(CMD_CLEAR_ORDER_BACKUP, STR_NULL, nullptr, true, false, false, ob->tile, 0, user, {});
|
Command<CMD_CLEAR_ORDER_BACKUP>::Unsafe(STR_NULL, nullptr, true, false, ob->tile, CommandTraits<CMD_CLEAR_ORDER_BACKUP>::Args{ ob->tile, 0, user, {} });
|
||||||
} else {
|
} else {
|
||||||
/* The command came from the game logic, i.e. the clearing of a tile.
|
/* 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. */
|
* In that case we have no need to actually sync this, just do it. */
|
||||||
|
|
|
@ -371,7 +371,7 @@ bool ScriptObject::ScriptDoCommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Tar
|
||||||
if (!estimate_only && networking) ScriptObject::SetLastCommand(tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), Tcmd);
|
if (!estimate_only && networking) ScriptObject::SetLastCommand(tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), Tcmd);
|
||||||
|
|
||||||
/* Try to perform the command. */
|
/* Try to perform the command. */
|
||||||
CommandCost res = std::apply(&DoCommandPInternal, std::tuple_cat(std::make_tuple(Tcmd, (StringID)0, networking ? ScriptObject::GetDoCommandCallback() : nullptr, false, estimate_only, false), args));
|
CommandCost res = ::Command<Tcmd>::Unsafe((StringID)0, networking ? ScriptObject::GetDoCommandCallback() : nullptr, false, estimate_only, tile, args);
|
||||||
|
|
||||||
return ScriptObject::DoCommandProcessResult(res, callback, estimate_only);
|
return ScriptObject::DoCommandProcessResult(res, callback, estimate_only);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1604,7 +1604,7 @@ void SyncCompanySettings()
|
||||||
const SettingDesc *sd = GetSettingDesc(desc);
|
const SettingDesc *sd = GetSettingDesc(desc);
|
||||||
uint32 old_value = (uint32)sd->AsIntSetting()->Read(new_object);
|
uint32 old_value = (uint32)sd->AsIntSetting()->Read(new_object);
|
||||||
uint32 new_value = (uint32)sd->AsIntSetting()->Read(old_object);
|
uint32 new_value = (uint32)sd->AsIntSetting()->Read(old_object);
|
||||||
if (old_value != new_value) NetworkSendCommand(CMD_CHANGE_COMPANY_SETTING, STR_NULL, nullptr, _local_company, 0, 0, new_value, sd->GetName());
|
if (old_value != new_value) Command<CMD_CHANGE_COMPANY_SETTING>::SendNet(STR_NULL, nullptr, _local_company, 0, 0, new_value, sd->GetName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue