VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
LayoutPath.cpp
Go to the documentation of this file.
1#include "Ui/LayoutPath.hpp"
2
3#include <format>
4
5namespace VoltMod
6{
7
8static constexpr std::string_view SourceExtension = ".xml";
9
10static Error NotSourceXml(std::string_view layout)
11{
12 return Error::Invalid(std::format("'{}' must name the layout's source .xml, not the compiled resource", layout));
13}
14
16{
17 if (layout.empty())
18 {
19 return std::unexpected(Error::Invalid("a layout name is required"));
20 }
21
22 const bool isSourceXml = layout.ends_with(SourceExtension);
23
24 if (!layout.contains('/'))
25 {
26 // Any extension but .xml is a compiled resource name or a typo; appending .xml cannot resolve it.
27 if (layout.contains('.') && !isSourceXml)
28 {
29 return std::unexpected(NotSourceXml(layout));
30 }
31
32 return LayoutPath(std::format("{}{}{}", Directory, layout, isSourceXml ? "" : SourceExtension));
33 }
34
35 if (!layout.starts_with(Directory))
36 {
37 return std::unexpected(Error::Invalid(std::format(
38 "'{}' is outside {}, the only directory the addon whitelist allows layouts in", layout, Directory)));
39 }
40
41 if (!isSourceXml)
42 {
43 return std::unexpected(NotSourceXml(layout));
44 }
45
46 return LayoutPath(std::string(layout));
47}
48
50{
51 std::string_view name = _resource;
52 name.remove_prefix(name.rfind('/') + 1);
53 name.remove_suffix(SourceExtension.size());
54 return name;
55}
56
57} // namespace VoltMod
A layout resource the client will accept, checked before anything is spawned.
static Result< LayoutPath > Parse(std::string_view layout)
static constexpr std::string_view Directory
std::string_view Name() const noexcept
static constexpr std::string_view SourceExtension
Definition LayoutPath.cpp:8
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
static Error NotSourceXml(std::string_view layout)
std::expected< T, Error > Result
Definition Result.hpp:64
One failure: a code to branch on, text for the log, and an optional translation key.
Definition Result.hpp:42
static Error Invalid(std::string detail)
Definition Result.hpp:51