-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildOrderStore.cpp
More file actions
127 lines (119 loc) · 3.74 KB
/
BuildOrderStore.cpp
File metadata and controls
127 lines (119 loc) · 3.74 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "BuildOrderStore.h"
#include "BuildOrderJson.h"
#include <fstream>
#include <sstream>
namespace
{
std::string ReadAllTextUtf8(const std::filesystem::path& path, std::string& outError)
{
outError.clear();
std::ifstream in(path, std::ios::binary);
if (!in)
{
outError = "Failed to open file.";
return {};
}
std::ostringstream ss;
ss << in.rdbuf();
return ss.str();
}
std::string SanitizeFileBaseName(std::string name)
{
for (char& c : name)
{
const unsigned char uc = static_cast<unsigned char>(c);
const bool bOk = std::isalnum(uc) != 0 || c == '_' || c == '-';
if (!bOk)
c = '_';
}
while (!name.empty() && name.front() == '_')
name.erase(name.begin());
while (!name.empty() && name.back() == '_')
name.pop_back();
if (name.empty())
name = "custom_build";
return name;
}
} // namespace
BuildOrderStore::BuildOrderStore(std::filesystem::path baseDirectory)
: m_root(std::move(baseDirectory))
{
}
void BuildOrderStore::RefreshFileList()
{
vBuilds.clear();
std::error_code ec;
std::filesystem::create_directories(GetPresetsPath(), ec);
std::filesystem::create_directories(GetUserPath(), ec);
CollectJsonFromDir(GetPresetsPath(), "presets");
CollectJsonFromDir(GetUserPath(), "user");
}
void BuildOrderStore::CollectJsonFromDir(const std::filesystem::path& dir, const char* sRelativePrefix)
{
std::error_code ec;
if (!std::filesystem::exists(dir, ec))
return;
for (const auto& entry : std::filesystem::directory_iterator(dir, ec))
{
if (!entry.is_regular_file(ec))
continue;
if (entry.path().extension() != ".json")
continue;
BuildFileInfo info;
info.sRelativePath = std::string(sRelativePrefix) + "/" + entry.path().filename().string();
info.eRace = BuildRace::Unknown;
BuildOrder tmp;
std::string err;
if (LoadByRelativePath(info.sRelativePath, tmp, err))
{
info.eRace = tmp.eRace;
if (!tmp.sName.empty())
info.sDisplayName = tmp.sName + " (" + info.sRelativePath + ")";
else
info.sDisplayName = entry.path().filename().string();
}
else
info.sDisplayName = entry.path().filename().string();
vBuilds.push_back(std::move(info));
}
}
bool BuildOrderStore::LoadByRelativePath(const std::string& sRelative, BuildOrder& outOrder, std::string& outError) const
{
std::filesystem::path path;
if (sRelative.rfind("presets/", 0) == 0)
path = GetPresetsPath() / std::filesystem::path(sRelative.substr(8));
else if (sRelative.rfind("user/", 0) == 0)
path = GetUserPath() / std::filesystem::path(sRelative.substr(5));
else
{
outError = "Unknown build path prefix.";
return false;
}
std::string readErr;
const std::string json = ReadAllTextUtf8(path, readErr);
if (!readErr.empty())
{
outError = readErr;
return false;
}
return BuildOrderFromJsonString(json, outOrder, outError);
}
bool BuildOrderStore::SaveUserBuild(const BuildOrder& order, const std::string& sFileBaseName, std::string& outError)
{
std::error_code ec;
std::filesystem::create_directories(GetUserPath(), ec);
const std::string safe = SanitizeFileBaseName(sFileBaseName);
const std::filesystem::path path = GetUserPath() / (safe + ".json");
std::string json;
if (!BuildOrderToJsonString(order, json, outError))
return false;
std::ofstream out(path, std::ios::binary | std::ios::trunc);
if (!out)
{
outError = "Failed to write user build.";
return false;
}
out << json;
RefreshFileList();
return true;
}