mirror of
https://github.com/OpenTTD/OpenTTD
synced 2026-01-16 17:02:37 +01:00
Codechange: Check command callbacks registration even in single player
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
static const CommandCost CMD_ERROR = CommandCost(INVALID_STRING_ID);
|
||||
|
||||
void NetworkSendCommand(Commands cmd, StringID err_message, CommandCallback *callback, CompanyID company, const CommandDataBuffer &cmd_data);
|
||||
bool IsNetworkRegisteredCallback(CommandCallback *callback);
|
||||
|
||||
bool IsValidCommand(Commands cmd);
|
||||
CommandFlags GetCommandFlags(Commands cmd);
|
||||
@@ -196,6 +197,7 @@ public:
|
||||
template <typename Tcallback>
|
||||
static bool Post(StringID err_message, Tcallback *callback, Targs... args)
|
||||
{
|
||||
assert(::IsNetworkRegisteredCallback(reinterpret_cast<CommandCallback *>(reinterpret_cast<void(*)()>(callback))));
|
||||
return InternalPost(err_message, callback, true, false, std::forward_as_tuple(args...));
|
||||
}
|
||||
|
||||
|
||||
@@ -111,6 +111,16 @@ inline auto MakeCallbackTable(std::index_sequence<i...>) noexcept
|
||||
/** Type-erased table of callbacks. */
|
||||
static const auto _callback_table = MakeCallbackTable(std::make_index_sequence<_callback_tuple_size>{});
|
||||
|
||||
/**
|
||||
* Helper function to ensure that callbacks used when Posting commands are actually registered for the network calls.
|
||||
* @param callback The callback to check.
|
||||
* @return \c true when the callback is in the callback table, otherwise \c false.
|
||||
*/
|
||||
bool IsNetworkRegisteredCallback(CommandCallback *callback)
|
||||
{
|
||||
return std::ranges::find(_callback_table, callback) != _callback_table.end();
|
||||
}
|
||||
|
||||
template <typename T> struct CallbackArgsHelper;
|
||||
template <typename... Targs>
|
||||
struct CallbackArgsHelper<void(*const)(Commands, const CommandCost &, Targs...)> {
|
||||
|
||||
Reference in New Issue
Block a user