-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cs
More file actions
73 lines (59 loc) · 2.67 KB
/
Config.cs
File metadata and controls
73 lines (59 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using YamlDotNet.Serialization;
namespace RoleBoi;
internal static class Config
{
public static string Token { get; private set; } = "";
public static string PresenceType { get; private set; } = "Playing";
public static string PresenceText { get; private set; } = "";
public static string DatabaseFile { get; private set; } = "./roleboi.db";
public static string ConfigPath { get; private set; } = "./config.yml";
public static string LogPath { get; private set; } = "";
public static bool Initialized { get; private set; } = false;
public static void LoadConfig()
{
if (!string.IsNullOrEmpty(RoleBoi.commandLineArgs.ConfigPath))
{
ConfigPath = RoleBoi.commandLineArgs.ConfigPath;
}
Logger.Log("Loading config \"" + Path.GetFullPath(ConfigPath) + "\"");
// Writes default config to file if it does not already exist
if (!File.Exists(ConfigPath))
{
File.WriteAllText(ConfigPath, Utilities.ReadManifestData("default_config.yml"));
}
// Reads config contents into FileStream
FileStream stream = File.OpenRead(ConfigPath);
// Converts the FileStream into a YAML object
IDeserializer deserializer = new DeserializerBuilder().Build();
object yamlObject = deserializer.Deserialize(new StreamReader(stream));
// Converts the YAML object into a JSON object as the YAML ones do not support traversal or selection of nodes by name
ISerializer serializer = new SerializerBuilder().JsonCompatible().Build();
JObject json = JObject.Parse(serializer.Serialize(yamlObject));
LogPath = json.SelectToken("bot.log-file")?.Value<string>() ?? "";
if (!string.IsNullOrEmpty(RoleBoi.commandLineArgs.LogFilePath))
{
LogPath = RoleBoi.commandLineArgs.LogFilePath;
}
DatabaseFile = json.SelectToken("bot.database-file")?.Value<string>() ?? "./roleboi.db";
if (!string.IsNullOrEmpty(RoleBoi.commandLineArgs.DatabasePath))
{
DatabaseFile = RoleBoi.commandLineArgs.DatabasePath;
}
string stringLogLevel = json.SelectToken("bot.console-log-level")?.Value<string>() ?? "";
if (!Enum.TryParse(stringLogLevel, true, out LogLevel logLevel))
{
logLevel = LogLevel.Information;
Logger.Warn("Log level '" + stringLogLevel + "' is invalid, using 'Information' instead.");
}
Logger.SetLogLevel(logLevel);
Token = json.SelectToken("bot.token")?.Value<string>() ?? "";
PresenceType = json.SelectToken("bot.presence-type")?.Value<string>() ?? "Playing";
PresenceText = json.SelectToken("bot.presence-text")?.Value<string>() ?? "";
Initialized = true;
}
}