BadScript 2
Loading...
Searching...
No Matches
BadSettingsReader.cs
Go to the documentation of this file.
2using BadScript2.IO;
3
4using Newtonsoft.Json.Linq;
5
6namespace BadScript2.Settings;
7
12{
13 private readonly IFileSystem m_FileSystem;
14
18 private readonly BadSettings m_RootSettings;
19
23 private readonly string[] m_SourceFiles;
24
30 public BadSettingsReader(BadSettings rootSettings, IFileSystem mFileSystem, params string[] sourceFiles)
31 {
32 m_SourceFiles = sourceFiles;
33 m_RootSettings = rootSettings;
34 m_FileSystem = mFileSystem;
35 }
36
42 {
43 List<BadSettings> settings = new List<BadSettings> { m_RootSettings };
44
45 Queue<string> files = new Queue<string>(m_SourceFiles);
46
47 while (files.Count != 0)
48 {
49 string file = files.Dequeue();
50 BadLogger.Log("Reading settings from file: " + file, "SettingsReader");
51 settings.Add(CreateSettings(ReadJsonFile(file), file));
52 }
53
54 BadSettings s = new BadSettings(string.Empty);
55 s.Populate(true, settings.ToArray());
56
57 BadLogger.Log("Resolving environment variables", "SettingsReader");
59
61
62 return s;
63 }
64
70 private JToken ReadJsonFile(string fileName)
71 {
72 string json = m_FileSystem.ReadAllText(fileName);
73 JToken token = JToken.Parse(json);
74
75 return token;
76 }
77
83 private static BadSettings CreateSettings(JToken? token, string path)
84 {
85 if (token is
86 not { Type: JTokenType.Object })
87 {
88 return new BadSettings(token, path);
89 }
90
91 Dictionary<string, BadSettings> settings = new Dictionary<string, BadSettings>();
92 JObject obj = (JObject)token;
93
94 foreach (KeyValuePair<string, JToken?> keyValuePair in obj)
95 {
96 settings.Add(keyValuePair.Key, CreateSettings(keyValuePair.Value, path));
97 }
98
99 return new BadSettings(settings, path);
100 }
101
102
107 private void ReadDynamicSettings(BadSettings settings)
108 {
109 BadLogger.Log("Processing Dynamic Includes", "SettingsReader");
110 BadSettings? elems = settings.FindProperty("SettingsBuilder.Include");
111 string[]? includes = elems?.GetValue<string[]>();
112
113 if (includes == null)
114 {
115 return;
116 }
117
118 elems?.SetValue(null);
119
120 do
121 {
122 List<BadSettings> setting = new List<BadSettings>();
123
124 foreach (string include in includes)
125 {
126 if (include.Contains('*'))
127 {
128 bool allDirs = include.Contains("**");
129
130 string[] parts = include.Split(new[] { '*' },
131 StringSplitOptions.RemoveEmptyEntries
132 );
133 string path = parts[0];
134 string extension = parts[1];
135
136 IEnumerable<string> files = m_FileSystem.GetFiles(path,
137 extension,
138 allDirs
139 );
140
141 setting.AddRange(files.Select(f =>
142 {
143 BadLogger.Log("Reading settings from file: " + f,
144 "SettingsReader"
145 );
146
147 return CreateSettings(ReadJsonFile(f), f);
148 }
149 )
150 );
151 }
152 else
153 {
154 BadLogger.Log("Reading settings from file: " + include, "SettingsReader");
155 setting.Add(CreateSettings(ReadJsonFile(include), include));
156 }
157 }
158
159 settings.Populate(true, setting.ToArray());
160
161 BadLogger.Log("Resolving environment variables", "SettingsReader");
163
164 elems = settings.FindProperty("SettingsBuilder.Include");
165 includes = elems?.GetValue<string[]>();
166 }
167 while (includes != null);
168
169 settings.RemoveProperty("SettingsBuilder");
170 }
171}
Public facing interface for a logger.
Definition BadLogger.cs:7
static void Log(string message)
Writes a Log to the Message Handler.
Definition BadLogger.cs:26
Public Api for the Settings System.
void Populate(bool invokeOnChanged, params BadSettings[] settings)
Populates the current object with the settings provided.
BadSettings? FindProperty(string propertyPath)
Finds a property based on the property path relative to this object.
void SetValue(JToken? value, bool invokeOnChange=true)
Sets the Json Token of the Current Settings Object.
static string ResolveEnvironmentVariables(BadSettings root, BadSettings parent, string str)
Resolves Environment Variables in the current object.
JToken? GetValue()
Returns the Json Token of the Settings Object.
bool RemoveProperty(string propertyName, bool invokeOnChange=true)
Removes the Property with the given Name.
Reads a JSON file and returns the resulting BadSettings Object.
BadSettings ReadSettings()
Returns a new Instance of BadSettings with all source files loaded.
BadSettingsReader(BadSettings rootSettings, IFileSystem mFileSystem, params string[] sourceFiles)
Constructs a new BadSettingsReader.
void ReadDynamicSettings(BadSettings settings)
Processes Dynamic Include Statements inside a settings object.
JToken ReadJsonFile(string fileName)
Parses a JSON File and returns the resulting JObject.
static BadSettings CreateSettings(JToken? token, string path)
Creates a Settings Object from a JToken.
readonly string[] m_SourceFiles
The Source Files that are used to read the Settings.
readonly BadSettings m_RootSettings
The Root Settings Object that all other Settings are added into.
Defines the interface for a file system.
Definition IFileSystem.cs:7
IEnumerable< string > GetFiles(string path, string extension, bool recursive)
Returns all files in the given directory that match the specified extension.
Contains Logging system for the BadScript Runtime.
Definition BadLog.cs:6
Contains IO Implementation for the BadScript2 Runtime.
Contains the Settings Implementation.