From c7818be33ebe06e9b0a9b61d40586e316d132e42 Mon Sep 17 00:00:00 2001 From: muit Date: Fri, 1 Mar 2024 21:43:58 +0100 Subject: [PATCH] Implemented saving settings --- Libs/AST/Include/AST/Utils/Settings.h | 46 +++++++++++++++++---------- Libs/AST/Src/AST/Utils/Settings.cpp | 7 ++-- Libs/Editor/Src/Editor.cpp | 6 ++++ 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/Libs/AST/Include/AST/Utils/Settings.h b/Libs/AST/Include/AST/Utils/Settings.h index 05b2a920..f04a377a 100644 --- a/Libs/AST/Include/AST/Utils/Settings.h +++ b/Libs/AST/Include/AST/Utils/Settings.h @@ -2,6 +2,9 @@ #pragma once +#include "AST/Utils/Settings.h" +#include "Pipe/Files/Paths.h" + #include #include #include @@ -11,7 +14,7 @@ namespace rift { - p::String GetUserSettingsPath(); + p::String GetUserSettingsPath(p::StringView name); template T& GetUserSettings() @@ -21,26 +24,37 @@ namespace rift { instance = p::MakeOwned(); - p::String path = GetUserSettingsPath(); - if (!p::Exists(path)) - { - p::CreateFolder(path); - } - - p::AppendToPath(path, p::GetTypeName(false)); - path.append(".json"); - if (!p::Exists(path)) + p::String filePath = GetUserSettingsPath(p::GetTypeName(false)); + if (!p::Exists(filePath)) { - p::SaveStringFile(path, "{}"); + SaveUserSettings(); } - - p::String data; - if (p::LoadStringFile(path, data)) + else { - p::JsonFormatReader reader{data}; - reader.GetReader().Serialize(*instance); + p::String data; + if (p::LoadStringFile(filePath, data)) + { + p::JsonFormatReader reader{data}; + reader.GetReader().Serialize(*instance); + } } } return *instance.Get(); } + + template + void SaveUserSettings() + { + auto& instance = GetUserSettings(); + p::JsonFormatWriter writer{}; + writer.GetWriter().Serialize(instance); + + p::String filePath = GetUserSettingsPath(p::GetTypeName(false)); + p::StringView folderPath = p::GetParentPath(filePath); + if (!p::Exists(folderPath)) + { + p::CreateFolder(folderPath); + } + p::SaveStringFile(filePath, writer.ToString()); + } } // namespace rift diff --git a/Libs/AST/Src/AST/Utils/Settings.cpp b/Libs/AST/Src/AST/Utils/Settings.cpp index 67cb9f59..41a1b898 100644 --- a/Libs/AST/Src/AST/Utils/Settings.cpp +++ b/Libs/AST/Src/AST/Utils/Settings.cpp @@ -8,9 +8,12 @@ namespace rift { - p::String GetUserSettingsPath() + p::String GetUserSettingsPath(p::StringView name) { static p::StringView relativeSettingsPath{"Rift"}; - return p::JoinPaths(p::PlatformPaths::GetUserSettingsPath(), relativeSettingsPath); + p::String path = + p::JoinPaths(p::PlatformPaths::GetUserSettingsPath(), relativeSettingsPath, name); + path.append(".json"); + return p::Move(path); } } // namespace rift diff --git a/Libs/Editor/Src/Editor.cpp b/Libs/Editor/Src/Editor.cpp index efa13163..8852b8aa 100644 --- a/Libs/Editor/Src/Editor.cpp +++ b/Libs/Editor/Src/Editor.cpp @@ -4,6 +4,8 @@ #include "AST/Systems/FunctionsSystem.h" #include "AST/Utils/Namespaces.h" +#include "AST/Utils/Settings.h" +#include "Statics/EditorSettings.h" #include "Statics/SEditor.h" #include "Systems/EditorSystem.h" #include "Utils/FunctionGraph.h" @@ -174,6 +176,10 @@ namespace rift::editor Editor::Get().bFilesDirty = true; })); + + auto& editorSettings = GetUserSettings(); + editorSettings.recentProjects.AddUnique(p::String(projectPath)); + SaveUserSettings(); return true; } return false;