10#include "nlohmann/json.hpp"
13using json = nlohmann::json;
15static json safeGetJson(
const std::filesystem::path& path);
19 saveFileAbsPath = std::filesystem::current_path() /
"Saves" / saveFileName;
24 json Save = safeGetJson(saveFileAbsPath);
25 json StaticActors = Save[
"StaticActors"];
27 bool allSuccessful =
true;
30 __ArchiveType& Archive = __Archive::_GetArchive();
32 LOG_DEFAULT(LogType::INFO,
"Loading static actors from persistent level");
34 for (
const auto& [ClassID, posList] : StaticActors.items()) {
36 auto it = Archive.find(ClassID);
38 if (it == Archive.end()) {
39 LOG_DEFAULT(LogType::WARNING, fmt::format(
"Unkown ClassID in factory: {} - skipped loading", ClassID));
43 auto constructor = it->second;
45 if (constructor ==
nullptr) {
46 LOG_DEFAULT(LogType::WARNING, fmt::format(
"Bad function reference in archive for class: {} - skipped loading", ClassID));
50 LOG_DEFAULT(LogType::INFO, fmt::format(
"Loading static actors of class: {}", ClassID));
52 for (
const auto& pos : posList) {
54 if (!pos.is_array() || pos.size() != 2) {
55 LOG_DEFAULT(LogType::WARNING, fmt::format(
"Invalid position data for class: {} - skipped loading", ClassID));
63 float x = pos[0].get<
float>();
64 float y = pos[1].get<
float>();
67 }
catch (
const json::type_error& e) {
69 LOG_DEFAULT(LogType::WARNING, fmt::format(
"Failed to parse data for actor: {} - skipped loading", e.what()));
70 allSuccessful =
false;
79 LOG_DEFAULT(LogType::WARNING, fmt::format(
"Object could not be constructor for class: {} - skipped loading", ClassID));
80 allSuccessful =
false;
85 addedActor->SetPosition(Position);
87 allSuccessful =
false;
88 LOG_DEFAULT(LogType::WARNING, fmt::format(
"Failed to load static actor when adding to actor pool: {} - skipped loading", ClassID ));
96 LOG_DEFAULT(LogType::INFO, fmt::format(
"Static actors loaded {}", allSuccessful ?
"successfully" :
"unsuccessfully"));
100int PersistentLevel::GetIntData( std::string key )
const {
101 json Save = safeGetJson(saveFileAbsPath);
103 return Save[
"Data"][key].get<
int>();
105float PersistentLevel::GetFloatData( std::string key )
const {
106 json Save = safeGetJson(saveFileAbsPath);
108 return Save[
"Data"][key].get<
float>();
110std::string PersistentLevel::GetStringData( std::string key )
const {
111 json Save = safeGetJson(saveFileAbsPath);
113 return Save[
"Data"][key].get<std::string>();
115Vector2 PersistentLevel::GetVector2Data( std::string key )
const {
116 json Save = safeGetJson(saveFileAbsPath);
118 json vecJson = Save[
"Data"][key];
119 return Vector2(vecJson[0].get<float>(), vecJson[1].get<float>());
122void PersistentLevel::WriteIntData( std::string key,
int value ) {
123 json Save = safeGetJson(saveFileAbsPath);
125 Save[
"Data"][key] = value;
127void PersistentLevel::WriteFloatData( std::string key,
float value ) {
128 json Save = safeGetJson(saveFileAbsPath);
130 Save[
"Data"][key] = value;
132void PersistentLevel::WriteStringData( std::string key, std::string value ) {
133 json Save = safeGetJson(saveFileAbsPath);
135 Save[
"Data"][key] = value;
137void PersistentLevel::WriteVector2Data( std::string key,
const Vector2& value ) {
138 json Save = safeGetJson(saveFileAbsPath);
140 Save[
"Data"][key] = { value.
x, value.
y };
143static json safeGetJson(
const std::filesystem::path& path) {
146 if (!std::filesystem::exists(path)) {
147 LOG_DEFAULT(LogType::ERROR, fmt::format(
"Save file not found at {}", path.c_str()));
151 std::ifstream file(path);
153 if (!file.is_open()) {
154 LOG_DEFAULT(LogType::ERROR, fmt::format(
"Could not open save file at {}", path.c_str()));
159 parsed = json::parse(file);
160 }
catch (
const json::parse_error& e) {
161 LOG_DEFAULT(LogType::ERROR, fmt::format(
"Failed to parse save file at {}: {}", path.c_str(), e.what()));
Logger LOG_DEFAULT
Default log object.
Abstract factory class inherited by all classes that should be archived.
static GameInstance * get()
Gets singleton instance.
World * GetWorld() const
Gets world object.
Local level managing gameplay interactions.
Actor * AddtoPool(Actor *actor)
Attempts to add external actor object to world managing system.
PersistentLevel(const std::string &saveFileName)
Construct level object.
bool LoadStaticActors()
Loads all static actors into the world.
Standard 2-dimensional vector.