-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.cs
More file actions
97 lines (86 loc) · 3.11 KB
/
Configuration.cs
File metadata and controls
97 lines (86 loc) · 3.11 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
using System.Diagnostics;
using System.Text.Json;
namespace GithubSponsorsWebhook
{
public static class Configuration
{
#region Types.
public class ConfigurationData
{
/// <summary>
/// True if the data has been setup. This must be set manually within the configuration file to eliminate not setup warnings.
/// </summary>
public bool Setup { get; set; }
/// <summary>
/// Minimum amount required to unlock downloads.
/// </summary>
public long MinimumSpendInCent { get; set; }
/// <summary>
/// Amount spent in total.
/// </summary>
public long LifetimeSpendInCent { get; set; }
/// <summary>
/// Amount to contribute to Lifetime when creating a Github Account.
/// </summary>
public long NewAccountIncentiveInCent { get; set; }
/// <summary>
/// Token for GitHub.
/// </summary>
public string? GithubToken { get; set; }
/// <summary>
/// Shhh, I cannot tell you what this one does, it's a SECRET.
/// </summary>
public string? GithubSecret { get; set; }
}
#endregion
#region Private.
/// <summary>
/// Configuration data to use.
/// </summary>
private static ConfigurationData? _data;
#endregion
public static ConfigurationData GetConfiguration()
{
if (_data != null)
return _data;
string configName = "Config.cfg";
//Path.combine would be ideal but it does not work sometimes for unknown reasons.
string path = $"{AppDomain.CurrentDomain.BaseDirectory}{configName}";
//Create if does not exist.
try
{
if (!File.Exists(path))
{
//Serialize as a json into path.
JsonSerializerOptions options = new JsonSerializerOptions();
options.WriteIndented = true;
string json = JsonSerializer.Serialize(new ConfigurationData(), options);
File.WriteAllText(path, json);
}
}
catch (Exception ex)
{
Debug.WriteLine($"Configuration file could not be created at {path}. Ex {ex.Message}");
return null;
}
//Load the file.
try
{
string allText = File.ReadAllText(path);
_data = JsonSerializer.Deserialize<ConfigurationData>(allText);
}
catch (Exception ex)
{
Debug.WriteLine($"Configuration file could not be loaaded as a JSON. Ex {ex.Message}");
return null;
}
//If not initialized.
if (!_data!.Setup)
{
Debug.WriteLine($"Configuration file is not setup.");
return null;
}
return _data;
}
}
}