LeviLamina
Loading...
Searching...
No Matches
SystemUtils.h
1#pragma once
2
3#include <concepts>
4#include <cstddef>
5#include <filesystem>
6#include <optional>
7#include <span>
8#include <string>
9
10#include "ll/api/base/CompilerPredefine.h"
11#include "ll/api/base/Macro.h"
12#include "ll/api/base/StdInt.h"
13#include "ll/api/data/TmWithMs.h"
14#include "ll/api/data/Version.h"
15
16namespace ll::inline utils::sys_utils {
17
18using HandleT = void*;
19
20[[nodiscard]] inline HandleT getCurrentModuleHandle() { return internal::getCurrentModuleHandle(); }
21
22LLNDAPI HandleT getModuleHandle(void* addr);
23
24LLNDAPI std::optional<std::filesystem::path> getModulePath(HandleT handle, HandleT process = nullptr);
25
26LLNDAPI std::string getModuleFileName(HandleT handle, HandleT process = nullptr);
27
28[[nodiscard]] inline std::string getCallerModuleFileName(void* addr = LL_RETURN_ADDRESS) {
29 return getModuleFileName(getModuleHandle(addr));
30}
31LLNDAPI std::span<std::byte> getImageRange(std::string_view name = "");
32
33LLNDAPI std::string getSystemLocaleCode();
34
35LLNDAPI std::string const& getSystemName();
36
37LLNDAPI data::TmWithMs getLocalTime(); // tm & ms
38
39LLNDAPI std::string getEnvironmentVariable(std::string_view name);
40
41LLAPI bool setEnvironmentVariable(std::string_view name, std::string_view value);
42
43LLAPI bool addOrSetEnvironmentVariable(std::string_view name, std::string_view value);
44
45LLNDAPI bool isStdoutSupportAnsi();
46
47LLNDAPI bool isWine();
48
49LLNDAPI data::Version getSystemVersion();
50
52 HandleT lib = nullptr;
53
54public:
55 [[nodiscard]] DynamicLibrary() {}
56 [[nodiscard]] DynamicLibrary(std::filesystem::path const& path) { load(path); }
58 if (lib) free();
59 }
60
61 DynamicLibrary(DynamicLibrary&&) noexcept = default;
62 DynamicLibrary& operator=(DynamicLibrary&&) noexcept = default;
63 DynamicLibrary(DynamicLibrary const&) = delete;
64 DynamicLibrary& operator=(DynamicLibrary const&) = delete;
65
66 LLAPI std::optional<std::system_error> load(std::filesystem::path const& path) noexcept;
67 LLAPI std::optional<std::system_error> free() noexcept;
68
69 LLNDAPI void* getAddress(char const* name) noexcept;
70
71 template <class T>
72 T getAddress(char const* name) noexcept {
73 return reinterpret_cast<T>(getAddress(name));
74 }
75
76 [[nodiscard]] constexpr HandleT handle() const noexcept { return lib; }
77};
78
79template <std::invocable<wchar_t*, size_t, size_t&> Fn>
80[[nodiscard]] inline std::optional<std::wstring> adaptFixedSizeToAllocatedResult(Fn&& callback) noexcept {
81 constexpr size_t arraySize = 256;
82
83 wchar_t value[arraySize]{};
84 size_t valueLengthNeededWithNull{};
85
86 std::optional<std::wstring> result{std::in_place};
87
88 if (!std::invoke(std::forward<Fn>(callback), value, arraySize, valueLengthNeededWithNull)) {
89 result.reset();
90 return result;
91 }
92 if (valueLengthNeededWithNull <= arraySize) {
93 return std::optional<std::wstring>{std::in_place, value, valueLengthNeededWithNull - 1};
94 }
95 do {
96 result->resize(valueLengthNeededWithNull - 1);
97 if (!std::invoke(std::forward<Fn>(callback), result->data(), result->size() + 1, valueLengthNeededWithNull)) {
98 result.reset();
99 return result;
100 }
101 } while (valueLengthNeededWithNull > result->size() + 1);
102 if (valueLengthNeededWithNull <= result->size()) {
103 result->resize(valueLengthNeededWithNull - 1);
104 }
105 return result;
106}
107} // namespace ll::inline utils::sys_utils
Definition SystemUtils.h:51