Codechange: Add support for additional command result values.
This commit is contained in:
@@ -173,6 +173,16 @@ ScriptObject::ActiveInstance::~ActiveInstance()
|
||||
return GetStorage()->last_command_res;
|
||||
}
|
||||
|
||||
/* static */ void ScriptObject::SetLastCommandResData(CommandDataBuffer data)
|
||||
{
|
||||
GetStorage()->last_cmd_ret = std::move(data);
|
||||
}
|
||||
|
||||
/* static */ const CommandDataBuffer &ScriptObject::GetLastCommandResData()
|
||||
{
|
||||
return GetStorage()->last_cmd_ret;
|
||||
}
|
||||
|
||||
/* static */ void ScriptObject::SetNewVehicleID(VehicleID vehicle_id)
|
||||
{
|
||||
GetStorage()->new_vehicle_id = vehicle_id;
|
||||
|
@@ -61,6 +61,12 @@ public:
|
||||
*/
|
||||
static void SetLastCommandRes(bool res);
|
||||
|
||||
/**
|
||||
* Store the extra data return by the last DoCommand.
|
||||
* @param data Extra data return by the command.
|
||||
*/
|
||||
static void SetLastCommandResData(CommandDataBuffer data);
|
||||
|
||||
/**
|
||||
* Get the currently active instance.
|
||||
* @return The instance.
|
||||
@@ -74,10 +80,11 @@ protected:
|
||||
* Templated wrapper that exposes the command parameter arguments
|
||||
* on the various DoCommand calls.
|
||||
* @tparam Tcmd The command-id to execute.
|
||||
* @tparam Tret Return type of the command.
|
||||
* @tparam Targs The command parameter types.
|
||||
*/
|
||||
template <Commands Tcmd, typename... Targs>
|
||||
struct ScriptDoCommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...)> {
|
||||
template <Commands Tcmd, typename Tret, typename... Targs>
|
||||
struct ScriptDoCommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...)> {
|
||||
static bool Do(Script_SuspendCallbackProc *callback, Targs... args)
|
||||
{
|
||||
return Execute(callback, std::forward_as_tuple(args...));
|
||||
@@ -180,6 +187,11 @@ protected:
|
||||
*/
|
||||
static bool GetLastCommandRes();
|
||||
|
||||
/**
|
||||
* Get the extra return data from the last DoCommand.
|
||||
*/
|
||||
static const CommandDataBuffer &GetLastCommandResData();
|
||||
|
||||
/**
|
||||
* Get the latest stored new_vehicle_id.
|
||||
*/
|
||||
@@ -363,10 +375,17 @@ namespace ScriptObjectInternal {
|
||||
{
|
||||
((SetClientIdHelper(std::get<Tindices>(values))), ...);
|
||||
}
|
||||
|
||||
/** Remove the first element of a tuple. */
|
||||
template <template <typename...> typename Tt, typename T1, typename... Ts>
|
||||
static inline Tt<Ts...> RemoveFirstTupleElement(const Tt<T1, Ts...> &tuple)
|
||||
{
|
||||
return std::apply([](auto &&, const auto&... args) { return std::tie(args...); }, tuple);
|
||||
}
|
||||
}
|
||||
|
||||
template <Commands Tcmd, typename... Targs>
|
||||
bool ScriptObject::ScriptDoCommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Targs...)>::Execute(Script_SuspendCallbackProc *callback, std::tuple<Targs...> args)
|
||||
template <Commands Tcmd, typename Tret, typename... Targs>
|
||||
bool ScriptObject::ScriptDoCommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...)>::Execute(Script_SuspendCallbackProc *callback, std::tuple<Targs...> args)
|
||||
{
|
||||
auto [err, estimate_only, networking] = ScriptObject::DoCommandPrep();
|
||||
if (err) return false;
|
||||
@@ -387,9 +406,14 @@ bool ScriptObject::ScriptDoCommandHelper<Tcmd, CommandCost(*)(DoCommandFlag, Tar
|
||||
if (!estimate_only && networking) ScriptObject::SetLastCommand(tile, EndianBufferWriter<CommandDataBuffer>::FromValue(args), Tcmd);
|
||||
|
||||
/* Try to perform the command. */
|
||||
CommandCost res = ::Command<Tcmd>::Unsafe((StringID)0, networking ? ScriptObject::GetDoCommandCallback() : nullptr, false, estimate_only, tile, args);
|
||||
Tret res = ::Command<Tcmd>::Unsafe((StringID)0, networking ? ScriptObject::GetDoCommandCallback() : nullptr, false, estimate_only, tile, args);
|
||||
|
||||
return ScriptObject::DoCommandProcessResult(res, callback, estimate_only);
|
||||
if constexpr (std::is_same_v<Tret, CommandCost>) {
|
||||
return ScriptObject::DoCommandProcessResult(res, callback, estimate_only);
|
||||
} else {
|
||||
ScriptObject::SetLastCommandResData(EndianBufferWriter<CommandDataBuffer>::FromValue(ScriptObjectInternal::RemoveFirstTupleElement(res)));
|
||||
return ScriptObject::DoCommandProcessResult(std::get<0>(res), callback, estimate_only);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* SCRIPT_OBJECT_HPP */
|
||||
|
@@ -687,7 +687,7 @@ SQInteger ScriptInstance::GetOpsTillSuspend()
|
||||
return this->engine->GetOpsTillSuspend();
|
||||
}
|
||||
|
||||
bool ScriptInstance::DoCommandCallback(const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, Commands cmd)
|
||||
bool ScriptInstance::DoCommandCallback(const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd)
|
||||
{
|
||||
ScriptObject::ActiveInstance active(this);
|
||||
|
||||
@@ -697,6 +697,7 @@ bool ScriptInstance::DoCommandCallback(const CommandCost &result, TileIndex tile
|
||||
}
|
||||
|
||||
ScriptObject::SetLastCommandRes(result.Succeeded());
|
||||
ScriptObject::SetLastCommandResData(std::move(result_data));
|
||||
|
||||
if (result.Failed()) {
|
||||
ScriptObject::SetLastError(ScriptError::StringToError(result.GetErrorMessage()));
|
||||
|
@@ -179,10 +179,11 @@ public:
|
||||
* @param result The result of the command.
|
||||
* @param tile The tile on which the command was executed.
|
||||
* @param data Command data as given to DoCommandPInternal.
|
||||
* @param result_data Extra data return from the command.
|
||||
* @param cmd cmd as given to DoCommandPInternal.
|
||||
* @return true if we handled result.
|
||||
*/
|
||||
bool DoCommandCallback(const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, Commands cmd);
|
||||
bool DoCommandCallback(const CommandCost &result, TileIndex tile, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd);
|
||||
|
||||
/**
|
||||
* Insert an event for this script.
|
||||
|
@@ -47,6 +47,7 @@ private:
|
||||
TileIndex last_tile; ///< The last tile passed to a command.
|
||||
CommandDataBuffer last_data; ///< The last data passed to a command.
|
||||
Commands last_cmd; ///< The last cmd passed to a command.
|
||||
CommandDataBuffer last_cmd_ret; ///< The extra data returned by the last command.
|
||||
|
||||
VehicleID new_vehicle_id; ///< The ID of the new Vehicle.
|
||||
SignID new_sign_id; ///< The ID of the new Sign.
|
||||
|
Reference in New Issue
Block a user