2
2
3
3
#pragma once
4
4
5
+ #include " AST/Utils/Settings.h"
6
+ #include " Pipe/Files/Paths.h"
7
+
5
8
#include < Pipe/Core/String.h>
6
9
#include < Pipe/Files/Files.h>
7
10
#include < Pipe/Serialize/Formats/JsonFormat.h>
11
14
12
15
namespace rift
13
16
{
14
- p::String GetUserSettingsPath ();
17
+ p::String GetUserSettingsPath (p::StringView name );
15
18
16
19
template <typename T>
17
20
T& GetUserSettings ()
@@ -21,26 +24,37 @@ namespace rift
21
24
{
22
25
instance = p::MakeOwned<T>();
23
26
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))
33
29
{
34
- p::SaveStringFile (path, " {} " );
30
+ SaveUserSettings<T>( );
35
31
}
36
-
37
- p::String data;
38
- if (p::LoadStringFile (path, data))
32
+ else
39
33
{
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
+ }
42
40
}
43
41
}
44
42
return *instance.Get ();
45
43
}
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
+ }
46
60
} // namespace rift
0 commit comments