VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
GameSearchPaths.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cctype>
5
6namespace VoltMod
7{
8
9static constexpr std::string_view Blank = " \t\r";
10
11static bool SameKey(std::string_view left, std::string_view right)
12{
13 return std::ranges::equal(left, right, [](char a, char b) {
14 return std::tolower(static_cast<unsigned char>(a)) == std::tolower(static_cast<unsigned char>(b));
15 });
16}
17
18/** Take the next bare or quoted word off the front of @p line. */
19static std::string_view NextWord(std::string_view& line)
20{
21 const size_t start = line.find_first_not_of(Blank);
22 if (start == std::string_view::npos)
23 {
24 line = {};
25 return {};
26 }
27 line.remove_prefix(start);
28
29 const bool quoted = line.front() == '"';
30 const size_t end = quoted ? line.find('"', 1) : line.find_first_of(Blank);
31 const std::string_view word = quoted ? line.substr(1, end - 1) : line.substr(0, end);
32 line.remove_prefix(end == std::string_view::npos ? line.size() : end + (quoted ? 1 : 0));
33 return word;
34}
35
36std::vector<std::string> GameSearchPaths(std::string_view gameinfo)
37{
38 std::vector<std::string> paths;
39 bool inSearchPaths = false;
40 int depth = 0;
41
42 while (!gameinfo.empty())
43 {
44 const size_t lineEnd = gameinfo.find('\n');
45 std::string_view line = gameinfo.substr(0, lineEnd);
46 gameinfo.remove_prefix(lineEnd == std::string_view::npos ? gameinfo.size() : lineEnd + 1);
47 line = line.substr(0, line.find("//"));
48
49 const std::string_view key = NextWord(line);
50 if (key.empty())
51 {
52 continue;
53 }
54 if (!inSearchPaths)
55 {
56 inSearchPaths = SameKey(key, "SearchPaths");
57 continue;
58 }
59
60 if (key == "{")
61 {
62 ++depth;
63 }
64 else if (key == "}")
65 {
66 if (--depth == 0)
67 {
68 break;
69 }
70 }
71 else if (depth == 1 && SameKey(key, "Game"))
72 {
73 paths.emplace_back(NextWord(line));
74 }
75 }
76 return paths;
77}
78
79} // namespace VoltMod
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
static constexpr std::string_view Blank
static std::string_view NextWord(std::string_view &line)
std::vector< std::string > GameSearchPaths(std::string_view gameinfo)
The Game values under SearchPaths in a gameinfo.gi, in file order.
static bool SameKey(std::string_view left, std::string_view right)