-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobalSettings.cs
80 lines (68 loc) · 2.25 KB
/
GlobalSettings.cs
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
using System;
using System.Collections.Generic;
namespace _6_xx_1
{
public class GlobalSettings
{
private Dictionary<int, object> settings;
public GlobalSettings()
{
settings = new Dictionary<int, object>();
}
public T Get<T>(int key, Action<T> callbackOnSettingChanged)
{
if (settings.ContainsKey(key))
{
return (T)settings[key];
}
return default(T);
}
public void Set<T>(int key, T value)
{
if (settings.ContainsKey(key))
{
var oldValue = (T)settings[key];
if (!EqualityComparer<T>.Default.Equals(oldValue, value))
{
settings[key] = value;
NotifySettingChanged(key, value);
}
}
else
{
settings.Add(key, value);
NotifySettingChanged(key, value);
}
}
private void NotifySettingChanged<T>(int key, T value)
{
// You can implement the notification mechanism here
Console.WriteLine($"Setting {key} changed to {value}");
// You can also invoke the callback if provided
// Note: Make sure to handle any exceptions in the callback code
// Example:
// callbackOnSettingChanged?.Invoke(value);
}
}
internal class Program
{
static void Main(string[] args)
{
GlobalSettings settings = new GlobalSettings();
// Subscribe to setting changes
settings.Get<bool>(1, value =>
{
Console.WriteLine($"DisableMagicMoments setting changed to {value}");
});
// Get setting value
bool disableMagicMoments = settings.Get<bool>(1, null);
Console.WriteLine($"DisableMagicMoments: {disableMagicMoments}");
// Set setting value
settings.Set(1, true);
// Get updated setting value
disableMagicMoments = settings.Get<bool>(1, null);
Console.WriteLine($"DisableMagicMoments: {disableMagicMoments}");
Console.ReadLine();
}
}
}