• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Return-To-The-Roots / s25client / 5111912776

pending completion
5111912776

Pull #1592

github

web-flow
Merge aa25149c0 into 2c095fc86
Pull Request #1592: Update translations

13 of 13 new or added lines in 4 files covered. (100.0%)

21671 of 42980 relevant lines covered (50.42%)

30486.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.38
/libs/s25main/drivers/DriverWrapper.cpp
1
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
2
//
3
// SPDX-License-Identifier: GPL-2.0-or-later
4

5
#include "DriverWrapper.h"
6
#include "ListDir.h"
7
#include "RttrConfig.h"
8
#include "driver/DriverInterfaceVersion.h"
9
#include "driver/Interface.h"
10
#include "files.h"
11
#include "helpers/LSANUtils.h"
12
#include "helpers/containerUtils.h"
13
#include "mygettext/mygettext.h"
14
#include "s25util/Log.h"
15
#include "s25util/error.h"
16
#include "s25util/warningSuppression.h"
17
#include <array>
18

19
namespace dll = boost::dll;
20

21
namespace {
22

23
std::string getName(drivers::DriverType type)
8✔
24
{
25
    return (type == drivers::DriverType::Audio) ? "audio" : "video";
8✔
26
}
27
auto tryLoadLibrary(const bfs::path& filepath)
8✔
28
{
29
    boost::system::error_code ec;
8✔
30
    rttr::ScopedLeakDisabler _;
8✔
31
    return dll::shared_library(filepath, ec, dll::load_mode::rtld_lazy);
16✔
32
}
33
} // namespace
34

35
namespace drivers {
36
DriverWrapper::DriverWrapper() = default;
12✔
37
DriverWrapper::~DriverWrapper() = default;
12✔
38

39
void DriverWrapper::Unload()
30✔
40
{
41
    dll.unload();
30✔
42
}
30✔
43

44
bool DriverWrapper::Load(const DriverType dt, std::string& preference)
2✔
45
{
46
    // ggf. aufräumen vorher
47
    Unload();
2✔
48

49
    /// Verfügbare Treiber auflisten
50
    std::vector<DriverItem> drivers = LoadDriverList(dt);
4✔
51

52
    LOG.write("%u %s drivers found!\n") % drivers.size() % getName(dt);
2✔
53

54
    // Welche gefunden?
55
    if(drivers.empty())
2✔
56
        return false;
×
57

58
    /// Suche, ob der Treiber dabei ist, den wir wünschen
59
    const auto it = helpers::find_if(drivers, [preference](const auto& it) { return it.GetName() == preference; });
4✔
60
    if(it != drivers.end())
2✔
61
    {
62
        // Dann den gleich nehmen
63
        dll = tryLoadLibrary(it->GetFile());
2✔
64
    }
65

66
    // ersten Treiber laden
67
    if(!dll)
2✔
68
    {
69
        dll = tryLoadLibrary(drivers.front().GetFile());
×
70
        // Standardwert zuweisen
71
        preference = drivers.front().GetName();
×
72
    }
73

74
    if(!dll)
2✔
75
    {
76
        s25util::fatal_error("Could not load driver library");
×
77
        return false;
×
78
    }
79

80
    return true;
2✔
81
}
82

83
bool DriverWrapper::IsLoaded() const
×
84
{
85
    return dll.is_loaded();
×
86
}
87

88
bool DriverWrapper::CheckLibrary(const bfs::path& path, DriverType dt, std::string& nameOrError)
6✔
89
{
90
    auto dll = tryLoadLibrary(path);
12✔
91

92
    if(!dll)
6✔
93
    {
94
        nameOrError = _("Failed to load library. Check dependencies!");
×
95
        return false;
×
96
    }
97

98
    auto GetDriverAPIVersion = dll.get<GetDriverAPIVersion_t>("GetDriverAPIVersion");
6✔
99
    if(!GetDriverAPIVersion)
6✔
100
    {
101
        nameOrError = _("Not a RTTR driver library!");
×
102
        return false;
×
103
    }
104
    if(GetDriverAPIVersion() != DRIVERAPIVERSION)
6✔
105
    {
106
        nameOrError = _("Invalid API version!");
×
107
        return false;
×
108
    }
109

110
    auto GetDriverName = dll.get<GetDriverName_t>("GetDriverName");
6✔
111
    std::string createName, freeName;
12✔
112
    if(dt == DriverType::Video)
6✔
113
    {
114
        createName = "CreateVideoInstance";
3✔
115
        freeName = "FreeVideoInstance";
3✔
116
    } else
117
    {
118
        createName = "CreateAudioInstance";
3✔
119
        freeName = "FreeAudioInstance";
3✔
120
    }
121

122
    if(!GetDriverName || !dll.has(createName) || !dll.has(freeName))
6✔
123
    {
124
        nameOrError = _("Missing required API function");
×
125
        return false;
×
126
    }
127

128
    nameOrError = GetDriverName();
6✔
129
    return true;
6✔
130
}
131

132
std::vector<DriverWrapper::DriverItem> DriverWrapper::LoadDriverList(const DriverType dt)
6✔
133
{
134
    std::vector<DriverItem> driver_list;
6✔
135

136
    const auto driverDir = RTTRCONFIG.ExpandPath(s25::folders::driver) / getName(dt);
18✔
137
    std::string extension =
138
#ifdef _WIN32
139
      "dll";
140
#else
141
#    ifdef __APPLE__
142
      "dylib";
143
#    else
144
      "so";
12✔
145
#    endif // !__APPLE__
146
#endif     // !_WIN32
147

148
    LOG.write(_("Searching for drivers in %s\n")) % driverDir;
6✔
149
    const std::vector<boost::filesystem::path> driver_files = ListDir(driverDir, extension, false);
12✔
150

151
    for(const auto& path : driver_files)
12✔
152
    {
153
#ifdef _WIN32
154
        // check filename for "rls_*" / "dbg_*", to allow not specialized drivers (for cygwin builds)
155
        const std::string filename = path.filename().string();
156
        const std::array<std::string, 2> blacklistedPrefixes =
157
#    ifdef _DEBUG
158
          {"rls_", "Release_"};
159
#    else
160
          {"dbg_", "Debug_"};
161
#    endif
162
        const bool isBlacklisted = helpers::contains_if(blacklistedPrefixes, [&filename](const std::string& prefix) {
163
            return filename.substr(0, prefix.length()) == prefix;
164
        });
165
        if(isBlacklisted)
166
            continue;
167
#endif
168
        std::string nameOrError;
12✔
169
        if(!CheckLibrary(path, dt, nameOrError))
6✔
170
            LOG.write(_("Skipping %s: %s\n")) % path % nameOrError;
×
171
        else
172
            driver_list.push_back(DriverItem(path, nameOrError));
6✔
173
    }
174
    return driver_list;
12✔
175
}
176
} // namespace drivers
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc