|
|
@ -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.
|
|
|
|
|
|
|
|
* 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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if (!send_net) {
|
|
|
|
* use the construction one */
|
|
|
|
/* Prepare for command execution. */
|
|
|
|
_cleared_object_areas.clear();
|
|
|
|
_cleared_object_areas.clear();
|
|
|
|
BasePersistentStorageArray::SwitchMode(PSM_ENTER_COMMAND);
|
|
|
|
BasePersistentStorageArray::SwitchMode(PSM_ENTER_COMMAND);
|
|
|
|
CommandCost res2 = proc(flags | DC_EXEC, tile, p1, p2, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { false, _debug_desync_level >= 1, send_net };
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|