Skip to content

Commit c7818be

Browse files
committed
Implemented saving settings
1 parent 9c6d455 commit c7818be

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

Libs/AST/Include/AST/Utils/Settings.h

+30-16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
#pragma once
44

5+
#include "AST/Utils/Settings.h"
6+
#include "Pipe/Files/Paths.h"
7+
58
#include <Pipe/Core/String.h>
69
#include <Pipe/Files/Files.h>
710
#include <Pipe/Serialize/Formats/JsonFormat.h>
@@ -11,7 +14,7 @@
1114

1215
namespace rift
1316
{
14-
p::String GetUserSettingsPath();
17+
p::String GetUserSettingsPath(p::StringView name);
1518

1619
template<typename T>
1720
T& GetUserSettings()
@@ -21,26 +24,37 @@ namespace rift
2124
{
2225
instance = p::MakeOwned<T>();
2326

24-
p::String path = GetUserSettingsPath();
25-
if (!p::Exists(path))
26-
{
27-
p::CreateFolder(path);
28-
}
29-
30-
p::AppendToPath(path, p::GetTypeName<T>(false));
31-
path.append(".json");
32-
if (!p::Exists(path))
27+
p::String filePath = GetUserSettingsPath(p::GetTypeName<T>(false));
28+
if (!p::Exists(filePath))
3329
{
34-
p::SaveStringFile(path, "{}");
30+
SaveUserSettings<T>();
3531
}
36-
37-
p::String data;
38-
if (p::LoadStringFile(path, data))
32+
else
3933
{
40-
p::JsonFormatReader reader{data};
41-
reader.GetReader().Serialize(*instance);
34+
p::String data;
35+
if (p::LoadStringFile(filePath, data))
36+
{
37+
p::JsonFormatReader reader{data};
38+
reader.GetReader().Serialize(*instance);
39+
}
4240
}
4341
}
4442
return *instance.Get();
4543
}
44+
45+
template<typename T>
46+
void SaveUserSettings()
47+
{
48+
auto& instance = GetUserSettings<T>();
49+
p::JsonFormatWriter writer{};
50+
writer.GetWriter().Serialize(instance);
51+
52+
p::String filePath = GetUserSettingsPath(p::GetTypeName<T>(false));
53+
p::StringView folderPath = p::GetParentPath(filePath);
54+
if (!p::Exists(folderPath))
55+
{
56+
p::CreateFolder(folderPath);
57+
}
58+
p::SaveStringFile(filePath, writer.ToString());
59+
}
4660
} // namespace rift

Libs/AST/Src/AST/Utils/Settings.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
namespace rift
1010
{
11-
p::String GetUserSettingsPath()
11+
p::String GetUserSettingsPath(p::StringView name)
1212
{
1313
static p::StringView relativeSettingsPath{"Rift"};
14-
return p::JoinPaths(p::PlatformPaths::GetUserSettingsPath(), relativeSettingsPath);
14+
p::String path =
15+
p::JoinPaths(p::PlatformPaths::GetUserSettingsPath(), relativeSettingsPath, name);
16+
path.append(".json");
17+
return p::Move(path);
1518
}
1619
} // namespace rift

Libs/Editor/Src/Editor.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "AST/Systems/FunctionsSystem.h"
66
#include "AST/Utils/Namespaces.h"
7+
#include "AST/Utils/Settings.h"
8+
#include "Statics/EditorSettings.h"
79
#include "Statics/SEditor.h"
810
#include "Systems/EditorSystem.h"
911
#include "Utils/FunctionGraph.h"
@@ -174,6 +176,10 @@ namespace rift::editor
174176
Editor::Get().bFilesDirty = true;
175177
}));
176178

179+
180+
auto& editorSettings = GetUserSettings<EditorSettings>();
181+
editorSettings.recentProjects.AddUnique(p::String(projectPath));
182+
SaveUserSettings<EditorSettings>();
177183
return true;
178184
}
179185
return false;

0 commit comments

Comments
 (0)