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{
16 private readonly BadSettings m_RootSettings;
17
21 private readonly string[] m_SourceFiles;
22
23 private readonly IFileSystem m_FileSystem;
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>
44 {
46 };
47
48 Queue<string> files = new Queue<string>(m_SourceFiles);
49
50 while (files.Count != 0)
51 {
52 string file = files.Dequeue();
53 BadLogger.Log("Reading settings from file: " + file, "SettingsReader");
54 settings.Add(CreateSettings(ReadJsonFile(file), file));
55 }
56
57 BadSettings s = new BadSettings(string.Empty);
58 s.Populate(true, settings.ToArray());
59
60 BadLogger.Log("Resolving environment variables", "SettingsReader");
62
64
65 return s;
66 }
67
73 private JToken ReadJsonFile(string fileName)
74 {
75 string json = m_FileSystem.ReadAllText(fileName);
76 JToken token = JToken.Parse(json);
77
78 return token;
79 }
80
86 private static BadSettings CreateSettings(JToken? token, string path)
87 {
88 if (token is
89 not
90 {
91 Type: JTokenType.Object,
92 })
93 {
94 return new BadSettings(token, path);
95 }
96
97 Dictionary<string, BadSettings> settings = new Dictionary<string, BadSettings>();
98 JObject obj = (JObject)token;
99
100 foreach (KeyValuePair<string, JToken?> keyValuePair in obj)
101 {
102 settings.Add(keyValuePair.Key, CreateSettings(keyValuePair.Value, path));
103 }
104
105 return new BadSettings(settings, path);
106 }
107
108
113 private void ReadDynamicSettings(BadSettings settings)
114 {
115 BadLogger.Log("Processing Dynamic Includes", "SettingsReader");
116 BadSettings? elems = settings.FindProperty("SettingsBuilder.Include");
117 string[]? includes = elems?.GetValue<string[]>();
118
119 if (includes == null)
120 {
121 return;
122 }
123
124 elems?.SetValue(null);
125
126 do
127 {
128 List<BadSettings> setting = new List<BadSettings>();
129
130 foreach (string include in includes)
131 {
132 if (include.Contains('*'))
133 {
134 bool allDirs = include.Contains("**");
135 string[] parts = include.Split(
136 new[]
137 {
138 '*',
139 },
140 StringSplitOptions.RemoveEmptyEntries
141 );
142 string path = parts[0];
143 string extension = parts[1];
144 IEnumerable<string> files = m_FileSystem.GetFiles(
145 path,
146 extension,
147 allDirs
148 );
149 setting.AddRange(
150 files.Select(
151 f =>
152 {
153 BadLogger.Log("Reading settings from file: " + f, "SettingsReader");
154
155 return CreateSettings(ReadJsonFile(f), f);
156 }
157 )
158 );
159 }
160 else
161 {
162 BadLogger.Log("Reading settings from file: " + include, "SettingsReader");
163 setting.Add(CreateSettings(ReadJsonFile(include), include));
164 }
165 }
166
167 settings.Populate(true, setting.ToArray());
168
169
170 BadLogger.Log("Resolving environment variables", "SettingsReader");
172
173 elems = settings.FindProperty("SettingsBuilder.Include");
174 includes = elems?.GetValue<string[]>();
175 }
176 while (includes != null);
177
178 settings.RemoveProperty("SettingsBuilder");
179 }
180}
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.