-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPCH.hpp
executable file
·109 lines (93 loc) · 2.83 KB
/
PCH.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#pragma once
using byte = unsigned char;
using uint = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
#include <Windows.h>
#include <mmsystem.h>
#include <psapi.h>
#include <tlhelp32.h>
#include <winuser.h>
#include <array>
#include <filesystem>
#include <fstream>
#include <future>
#include <list>
#include <map>
#include <memory>
#include <numbers>
#include <queue>
#include <ranges>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "FLCore/Common/CommonMethods.hpp"
#include <rfl.hpp>
#include <rfl/yaml.hpp>
template <typename T, const char* path>
requires std::is_default_constructible_v<T>
struct ConfigHelper
{
ConfigHelper() = delete;
static std::optional<T> Load(const bool fromUserData = false, const bool saveIfNotFound = true)
{
std::ifstream inFile(GetSaveLocation(fromUserData).data());
if (!inFile.is_open())
{
if (saveIfNotFound)
{
Save(T(), fromUserData);
return { T() };
}
return std::nullopt;
}
auto newConfig = rfl::yaml::read<T>(inFile);
if (newConfig.error().has_value())
{
const std::string err =
std::format("Failed to load config file '{}'.\n\n{}\n\nClick 'OK' to reset the config.", path, newConfig.error()->what());
if (MessageBoxA(nullptr, err.c_str(), "Config Load Error", MB_OKCANCEL | MB_ICONWARNING) != IDOK)
{
std::exit(1);
}
Save(T(), fromUserData);
return { T() };
}
return newConfig.value();
}
static bool Save(const T& data, const bool fromUserData = false)
{
std::ofstream outFile(GetSaveLocation(fromUserData).data(), std::ios::trunc);
if (!outFile.is_open())
{
return false;
}
outFile << rfl::yaml::write(static_cast<const T&>(data));
outFile.close();
return true;
}
private:
static std::string_view GetSaveLocation(const bool fromUserData)
{
static std::string savePath;
if (savePath.empty())
{
if (fromUserData)
{
std::array<char, MAX_PATH> totalPath{};
GetUserDataPath(totalPath.data());
savePath = std::format("{}/{}", std::string(totalPath.data()), path);
}
else
{
savePath = std::format("{}", path);
}
}
return savePath;
}
};