-
Notifications
You must be signed in to change notification settings - Fork 803
Expand file tree
/
Copy pathPolicyManager.cs
More file actions
102 lines (86 loc) · 3.12 KB
/
PolicyManager.cs
File metadata and controls
102 lines (86 loc) · 3.12 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
using log4net;
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace NETworkManager.Settings;
/// <summary>
/// Manager for system-wide policies that are loaded from a config.json file
/// in the application directory. These policies override user settings.
/// </summary>
public static class PolicyManager
{
#region Variables
/// <summary>
/// Logger for logging.
/// </summary>
private static readonly ILog Log = LogManager.GetLogger(typeof(PolicyManager));
/// <summary>
/// Config file name.
/// </summary>
private static string ConfigFileName => "config.json";
/// <summary>
/// System-wide policies that are currently loaded.
/// </summary>
public static PolicyInfo Current { get; private set; }
/// <summary>
/// JSON serializer options for consistent serialization/deserialization.
/// </summary>
private static readonly JsonSerializerOptions JsonOptions = new()
{
WriteIndented = true,
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = { new JsonStringEnumConverter() }
};
#endregion
#region Methods
/// <summary>
/// Method to get the config file path in the application directory.
/// </summary>
/// <returns>Config file path.</returns>
private static string GetConfigFilePath()
{
return Path.Combine(AssemblyManager.Current.Location, ConfigFileName);
}
/// <summary>
/// Method to load the system-wide policies from config.json file in the application directory.
/// </summary>
public static void Load()
{
var filePath = GetConfigFilePath();
// Check if config file exists
if (File.Exists(filePath))
{
try
{
Log.Info($"Loading system-wide policies from: {filePath}");
var jsonString = File.ReadAllText(filePath);
// Treat empty or JSON "null" as "no policies" instead of crashing
if (string.IsNullOrWhiteSpace(jsonString))
{
Current = new PolicyInfo();
Log.Info("Config file is empty, no system-wide policies loaded.");
}
else
{
Current = JsonSerializer.Deserialize<PolicyInfo>(jsonString, JsonOptions) ?? new PolicyInfo();
Log.Info("System-wide policies loaded successfully.");
// Log enabled settings
Log.Info($"System-wide policy - Update_CheckForUpdatesAtStartup: {Current.Update_CheckForUpdatesAtStartup?.ToString() ?? "Not set"}");
}
}
catch (Exception ex)
{
Log.Error($"Failed to load system-wide policies from: {filePath}", ex);
Current = new PolicyInfo();
}
}
else
{
Log.Debug($"No system-wide policy file found at: {filePath}");
Current = new PolicyInfo();
}
}
#endregion
}