Fix: CmdSetAutoReplace didn't validate group type and engine type match (#9950)
[openttd-github.git] / src / autoreplace.cpp
blobe1b73448ddaae99983e190d177c0eb7153d48ef3
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file autoreplace.cpp Management of replacement lists. */
10 #include "stdafx.h"
11 #include "command_func.h"
12 #include "group.h"
13 #include "autoreplace_base.h"
14 #include "core/bitmath_func.hpp"
15 #include "core/pool_func.hpp"
17 #include "safeguards.h"
19 /** The pool of autoreplace "orders". */
20 EngineRenewPool _enginerenew_pool("EngineRenew");
21 INSTANTIATE_POOL_METHODS(EngineRenew)
23 /**
24 * Retrieves the EngineRenew that specifies the replacement of the given
25 * engine type from the given renewlist
27 static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
29 EngineRenew *er = (EngineRenew *)erl;
31 while (er != nullptr) {
32 if (er->from == engine && GroupIsInGroup(group, er->group_id)) return er;
33 er = er->next;
35 return nullptr;
38 /**
39 * Remove all engine replacement settings for the company.
40 * @param erl The renewlist for a given company.
41 * @return The new renewlist for the company.
43 void RemoveAllEngineReplacement(EngineRenewList *erl)
45 EngineRenew *er = (EngineRenew *)(*erl);
46 EngineRenew *next;
48 while (er != nullptr) {
49 next = er->next;
50 delete er;
51 er = next;
53 *erl = nullptr; // Empty list
56 /**
57 * Retrieve the engine replacement in a given renewlist for an original engine type.
58 * @param erl The renewlist to search in.
59 * @param engine Engine type to be replaced.
60 * @param group The group related to this replacement.
61 * @param[out] replace_when_old Set to true if the replacement should be done when old.
62 * @return The engine type to replace with, or INVALID_ENGINE if no
63 * replacement is in the list.
65 EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group, bool *replace_when_old)
67 const EngineRenew *er = GetEngineReplacement(erl, engine, group);
68 if (er == nullptr && (group == DEFAULT_GROUP || (Group::IsValidID(group) && !HasBit(Group::Get(group)->flags, GroupFlags::GF_REPLACE_PROTECTION)))) {
69 /* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */
70 er = GetEngineReplacement(erl, engine, ALL_GROUP);
72 if (replace_when_old != nullptr) *replace_when_old = er == nullptr ? false : er->replace_when_old;
73 return er == nullptr ? INVALID_ENGINE : er->to;
76 /**
77 * Add an engine replacement to the given renewlist.
78 * @param erl The renewlist to add to.
79 * @param old_engine The original engine type.
80 * @param new_engine The replacement engine type.
81 * @param group The group related to this replacement.
82 * @param replace_when_old Replace when old or always?
83 * @param flags The calling command flags.
84 * @return 0 on success, CMD_ERROR on failure.
86 CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, bool replace_when_old, DoCommandFlag flags)
88 /* Check if the old vehicle is already in the list */
89 EngineRenew *er = GetEngineReplacement(*erl, old_engine, group);
90 if (er != nullptr) {
91 if (flags & DC_EXEC) {
92 er->to = new_engine;
93 er->replace_when_old = replace_when_old;
95 return CommandCost();
98 if (!EngineRenew::CanAllocateItem()) return CMD_ERROR;
100 if (flags & DC_EXEC) {
101 er = new EngineRenew(old_engine, new_engine);
102 er->group_id = group;
103 er->replace_when_old = replace_when_old;
105 /* Insert before the first element */
106 er->next = (EngineRenew *)(*erl);
107 *erl = (EngineRenewList)er;
110 return CommandCost();
114 * Remove an engine replacement from a given renewlist.
115 * @param erl The renewlist from which to remove the replacement
116 * @param engine The original engine type.
117 * @param group The group related to this replacement.
118 * @param flags The calling command flags.
119 * @return 0 on success, CMD_ERROR on failure.
121 CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, DoCommandFlag flags)
123 EngineRenew *er = (EngineRenew *)(*erl);
124 EngineRenew *prev = nullptr;
126 while (er != nullptr) {
127 if (er->from == engine && er->group_id == group) {
128 if (flags & DC_EXEC) {
129 if (prev == nullptr) { // First element
130 /* The second becomes the new first element */
131 *erl = (EngineRenewList)er->next;
132 } else {
133 /* Cut this element out */
134 prev->next = er->next;
136 delete er;
138 return CommandCost();
140 prev = er;
141 er = er->next;
144 return CMD_ERROR;