diff options
author | Michael Lutz <michi@icosahedron.de> | 2021-11-30 00:52:23 +0100 |
---|---|---|
committer | Michael Lutz <michi@icosahedron.de> | 2021-12-16 22:28:32 +0100 |
commit | 3e85e833a707e6b781d00eae09c9465bacbf1d69 (patch) | |
tree | 096a7b2347255a70783d0470d2f992d50a50d3d8 /src/script | |
parent | 850385465543a9bd589084ec2ac6b0693c481fea (diff) | |
download | openttd-3e85e833a707e6b781d00eae09c9465bacbf1d69.tar.xz |
Codechange: Add support for additional command result values.
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/api/script_object.cpp | 10 | ||||
-rw-r--r-- | src/script/api/script_object.hpp | 36 | ||||
-rw-r--r-- | src/script/script_instance.cpp | 3 | ||||
-rw-r--r-- | src/script/script_instance.hpp | 3 | ||||
-rw-r--r-- | src/script/script_storage.hpp | 1 |
5 files changed, 45 insertions, 8 deletions
diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp index ff109dbdf..dad79309a 100644 --- a/src/script/api/script_object.cpp +++ b/src/script/api/script_object.cpp @@ -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; diff --git a/src/script/api/script_object.hpp b/src/script/api/script_object.hpp index 11d9542c8..f9a66f8a8 100644 --- a/src/script/api/script_object.hpp +++ b/src/script/api/script_object.hpp @@ -62,6 +62,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...)); @@ -181,6 +188,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. */ static VehicleID GetNewVehicleID(); @@ -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 */ diff --git a/src/script/script_instance.cpp b/src/script/script_instance.cpp index ecf846b9f..0dfdd4b2a 100644 --- a/src/script/script_instance.cpp +++ b/src/script/script_instance.cpp @@ -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())); diff --git a/src/script/script_instance.hpp b/src/script/script_instance.hpp index 290568fdd..57f8dcc72 100644 --- a/src/script/script_instance.hpp +++ b/src/script/script_instance.hpp @@ -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. diff --git a/src/script/script_storage.hpp b/src/script/script_storage.hpp index 3735adc1d..b8d1038a0 100644 --- a/src/script/script_storage.hpp +++ b/src/script/script_storage.hpp @@ -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. |