Skip to content

Commit

Permalink
Implemented saving settings
Browse files Browse the repository at this point in the history
  • Loading branch information
muit committed Mar 1, 2024
1 parent 9c6d455 commit c7818be
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
46 changes: 30 additions & 16 deletions Libs/AST/Include/AST/Utils/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#pragma once

#include "AST/Utils/Settings.h"
#include "Pipe/Files/Paths.h"

#include <Pipe/Core/String.h>
#include <Pipe/Files/Files.h>
#include <Pipe/Serialize/Formats/JsonFormat.h>
Expand All @@ -11,7 +14,7 @@

namespace rift
{
p::String GetUserSettingsPath();
p::String GetUserSettingsPath(p::StringView name);

template<typename T>
T& GetUserSettings()
Expand All @@ -21,26 +24,37 @@ namespace rift
{
instance = p::MakeOwned<T>();

p::String path = GetUserSettingsPath();
if (!p::Exists(path))
{
p::CreateFolder(path);
}

p::AppendToPath(path, p::GetTypeName<T>(false));
path.append(".json");
if (!p::Exists(path))
p::String filePath = GetUserSettingsPath(p::GetTypeName<T>(false));
if (!p::Exists(filePath))
{
p::SaveStringFile(path, "{}");
SaveUserSettings<T>();

Check failure on line 30 in Libs/AST/Include/AST/Utils/Settings.h

View workflow job for this annotation

GitHub Actions / linux (clang-16, Release)

call to function 'SaveUserSettings' that is neither visible in the template definition nor found by argument-dependent lookup

Check failure on line 30 in Libs/AST/Include/AST/Utils/Settings.h

View workflow job for this annotation

GitHub Actions / linux (gcc-13, Release)

there are no arguments to ‘SaveUserSettings’ that depend on a template parameter, so a declaration of ‘SaveUserSettings’ must be available [-fpermissive]

Check failure on line 30 in Libs/AST/Include/AST/Utils/Settings.h

View workflow job for this annotation

GitHub Actions / linux (clang-16, Release)

call to function 'SaveUserSettings' that is neither visible in the template definition nor found by argument-dependent lookup

Check failure on line 30 in Libs/AST/Include/AST/Utils/Settings.h

View workflow job for this annotation

GitHub Actions / linux (gcc-13, Release)

there are no arguments to ‘SaveUserSettings’ that depend on a template parameter, so a declaration of ‘SaveUserSettings’ must be available [-fpermissive]
}

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<typename T>
void SaveUserSettings()
{
auto& instance = GetUserSettings<T>();
p::JsonFormatWriter writer{};
writer.GetWriter().Serialize(instance);

p::String filePath = GetUserSettingsPath(p::GetTypeName<T>(false));
p::StringView folderPath = p::GetParentPath(filePath);
if (!p::Exists(folderPath))
{
p::CreateFolder(folderPath);
}
p::SaveStringFile(filePath, writer.ToString());
}
} // namespace rift
7 changes: 5 additions & 2 deletions Libs/AST/Src/AST/Utils/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions Libs/Editor/Src/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -174,6 +176,10 @@ namespace rift::editor
Editor::Get().bFilesDirty = true;
}));


auto& editorSettings = GetUserSettings<EditorSettings>();
editorSettings.recentProjects.AddUnique(p::String(projectPath));
SaveUserSettings<EditorSettings>();
return true;
}
return false;
Expand Down

0 comments on commit c7818be

Please sign in to comment.