forked from HarbourMasters/Ghostship
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.cpp
More file actions
51 lines (38 loc) · 1.43 KB
/
Copy pathFile.cpp
File metadata and controls
51 lines (38 loc) · 1.43 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
#include "Spoiler.h"
#include "port/ui/Notification.h"
#include <fstream>
namespace fs = std::filesystem;
namespace Rando {
namespace Spoiler {
void SaveToFile(const std::string& fileName, nlohmann::json spoiler) {
std::string filePath = Ship::Context::GetPathRelativeToAppDirectory("randomizer/" + fileName, appShortName);
std::ofstream fileStream(filePath);
if (!fileStream.is_open()) {
throw std::runtime_error("Failed to open spoiler file");
}
fileStream << spoiler.dump(4);
}
nlohmann::json LoadFromFile(const std::string& fileName) {
nlohmann::json spoiler;
std::string spoilerFilePath = Ship::Context::GetPathRelativeToAppDirectory("randomizer/" + fileName, appShortName);
std::ifstream fileStream(spoilerFilePath);
if (!fs::exists(spoilerFilePath)) {
return spoiler;
}
if (!fileStream.is_open()) {
Notification::Emit(
{ .message = "Error: Failed to open spoiler file.", .messageColor = ImVec4(0.85f, 0.3f, 0, 1) });
return spoiler;
}
try {
fileStream >> spoiler;
} catch (nlohmann::json::exception& e) {
throw std::runtime_error("Failed to parse spoiler file: " + std::string(e.what()));
}
if (!spoiler.contains("type") || spoiler["type"] != "GHOSTSHIP_RANDO_SPOILER") {
throw std::runtime_error("Spoiler file is not a valid spoiler file");
}
return spoiler;
}
} // namespace Spoiler
} // namespace Rando