-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.cs
More file actions
154 lines (121 loc) · 5.32 KB
/
Configuration.cs
File metadata and controls
154 lines (121 loc) · 5.32 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using Microsoft.Extensions.Configuration;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
namespace Rift.ModernRift.Core
{
public class Configuration
{
public string Name { get; } // Mandatory
public string Description { get; } // Optional
public string CreatorName { get; } // Optional
public string Version { get; } // Mandatory
public string WindowTitle { get; } // Optional
public string Prompt { get; } // Optional
public string BackgroundColor { get; } // Optional
public string TextColor { get; } // Optional
private Configuration(ConfigurationBuilder builder)
{
Name = builder.name;
Version = builder.version;
if (builder.description != null) Description = builder.description; else Description = "";
if (builder.creatorName != null) CreatorName = builder.creatorName; else CreatorName = "";
if (builder.windowTitle != null) WindowTitle = builder.windowTitle; else WindowTitle = Name;
if (builder.prompt != null) Prompt = builder.prompt; else Prompt = Name + " > ";
if (builder.backgroundColor != null) BackgroundColor = builder.backgroundColor; else BackgroundColor = "Black";
if (builder.textColor != null) TextColor = builder.textColor; else TextColor = "White";
}
private Configuration() { }
public class ConfigurationBuilder
{
public string name; // Mandatory
public string description; // Optional
public string creatorName; // Optional
public string version; // Mandatory
public string windowTitle; // Optional
public string prompt; // Optional
public string backgroundColor;
public string textColor;
public ConfigurationBuilder(string name, string version)
{
this.name = name;
this.version = version;
}
public ConfigurationBuilder()
{
}
public ConfigurationBuilder Name(string name)
{
this.name = name;
return this;
}
public ConfigurationBuilder Version(string version)
{
this.version = version;
return this;
}
public ConfigurationBuilder Description(string description)
{
this.description = description;
return this;
}
public ConfigurationBuilder CreatorName(string creatorName)
{
this.creatorName = creatorName;
return this;
}
public ConfigurationBuilder WindowTitle(string windowTitle)
{
this.windowTitle = windowTitle;
return this;
}
public ConfigurationBuilder Prompt(string prompt)
{
this.prompt = prompt;
return this;
}
public ConfigurationBuilder BackgroundColor(string backgroundColor)
{
this.backgroundColor = backgroundColor;
return this;
}
public ConfigurationBuilder TextColor(string textColor)
{
this.textColor = textColor;
return this;
}
public ConfigurationBuilder FromConfigFile(string filePath)
{
try
{
var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory).AddJsonFile
(filePath).Build()
.GetSection("Config");
name = (string)config.GetValue(typeof(string), "name");
description = (string)config.GetValue(typeof(string), "description");
creatorName = (string)config.GetValue(typeof(string), "creatorName");
version = (string)config.GetValue(typeof(string), "version");
windowTitle = (string)config.GetValue(typeof(string), "windowTitle");
prompt = (string)config.GetValue(typeof(string), "prompt");
backgroundColor = (string)config.GetValue(typeof(string), "backgroundColor");
textColor = (string)config.GetValue(typeof(string), "textColor");
return this;
} catch (Exception inner)
{
throw new ConfigurationFileNotFoundException("The file path \"" + filePath + "\" was not a valid json file.", inner);
}
}
public Configuration Build()
{
Configuration configuration = new(this);
Validate(configuration);
return configuration;
}
private static void Validate(Configuration configuration)
{
if(configuration.Name == null || configuration.Version == null)
{
throw new ConfigurationIncompleteException("The configuration is incomplete");
}
}
}
}
}