MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / SelectDriverImpl

Method SelectDriverImpl

src/driver.cpp:110–203  ·  view source on GitHub ↗

* Find the requested driver and return its class. * @param name the driver to select. * @param type the type of driver to select * @post Sets the driver so GetCurrentDriver() returns it too. * @return True upon success, otherwise false. */

Source from the content-addressed store, hash-verified

108 * @return True upon success, otherwise false.
109 */
110bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type type)
111{
112 if (GetDrivers().empty()) return false;
113
114 if (name.empty()) {
115 /* Probe for this driver, but do not fall back to dedicated/null! */
116 for (int priority = 10; priority > 0; priority--) {
117 for (auto &it : GetDrivers()) {
118 DriverFactoryBase *d = it.second;
119
120 /* Check driver type */
121 if (d->type != type) continue;
122 if (d->priority != priority) continue;
123
124 if (type == Driver::DT_VIDEO && !_video_hw_accel && d->UsesHardwareAcceleration()) continue;
125
126 if (type == Driver::DT_VIDEO && _video_hw_accel && d->UsesHardwareAcceleration()) {
127 /* Check if we have already tried this driver in last run.
128 * If it is here, it most likely means we crashed. So skip
129 * hardware acceleration. */
130 auto filename = FioFindFullPath(BASE_DIR, HWACCELERATION_TEST_FILE);
131 if (!filename.empty()) {
132 FioRemove(filename);
133
134 Debug(driver, 1, "Probing {} driver '{}' skipped due to earlier crash", GetDriverTypeName(type), d->name);
135
136 _video_hw_accel = false;
137 ErrorMessageData msg(GetEncodedString(STR_VIDEO_DRIVER_ERROR), GetEncodedString(STR_VIDEO_DRIVER_ERROR_HARDWARE_ACCELERATION_CRASH), true);
138 ScheduleErrorMessage(std::move(msg));
139 continue;
140 }
141
142 /* Write empty file to note we are attempting hardware acceleration. */
143 FioFOpenFile(HWACCELERATION_TEST_FILE, "w", BASE_DIR);
144 }
145
146 /* Keep old driver in case we need to switch back, or may still need to process an OS callback. */
147 auto oldd = std::move(GetActiveDriver(type));
148 auto newd = d->CreateInstance();
149
150 auto err = newd->Start({});
151 if (!err) {
152 Debug(driver, 1, "Successfully probed {} driver '{}'", GetDriverTypeName(type), d->name);
153 GetActiveDriver(type) = std::move(newd);
154 return true;
155 }
156
157 GetActiveDriver(type) = std::move(oldd);
158 Debug(driver, 1, "Probing {} driver '{}' failed with error: {}", GetDriverTypeName(type), d->name, *err);
159
160 if (type == Driver::DT_VIDEO && _video_hw_accel && d->UsesHardwareAcceleration()) {
161 _video_hw_accel = false;
162 ErrorMessageData msg(GetEncodedString(STR_VIDEO_DRIVER_ERROR), GetEncodedString(STR_VIDEO_DRIVER_ERROR_NO_HARDWARE_ACCELERATION), true);
163 ScheduleErrorMessage(std::move(msg));
164 }
165 }
166 }
167 UserError("Couldn't find any suitable {} driver", GetDriverTypeName(type));

Callers

nothing calls this directly

Calls 11

FioFindFullPathFunction · 0.85
FioRemoveFunction · 0.85
ScheduleErrorMessageFunction · 0.85
FioFOpenFileFunction · 0.85
StrEqualsIgnoreCaseFunction · 0.85
push_backMethod · 0.80
GetEncodedStringFunction · 0.70
emptyMethod · 0.45
CreateInstanceMethod · 0.45
StartMethod · 0.45

Tested by

no test coverage detected