Skip to content

Commit 922bb1e

Browse files
author
Aytackydln
committed
remove instance fields (make them static) from GameState class
1 parent ddc7d19 commit 922bb1e

9 files changed

Lines changed: 49 additions & 25 deletions

File tree

Project-Aurora/Project-Aurora/Nodes/AudioNode.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ namespace AuroraRgb.Nodes;
66
[GameStateDescription(Description)]
77
public class AudioNode : Node
88
{
9+
public static AudioNode Instance { get; } = new();
10+
911
private const string Description = """
1012
Data provided by NAudio
1113
""";
1214

1315
public static PlaybackDeviceNode PlaybackDevice { get; } = new();
1416
public static RecordingDeviceNode RecordingDevice { get; } = new();
17+
18+
private AudioNode()
19+
{
20+
}
1521
}
1622

1723
public class PlaybackDeviceNode : Node

Project-Aurora/Project-Aurora/Nodes/CelestialData.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace AuroraRgb.Nodes;
99
[GameStateDescription(Description)]
1010
public class CelestialData : Node
1111
{
12+
public static CelestialData Instance { get; } = new();
13+
1214
private const string Description = """
1315
Provides the solar noon percentage, which indicates how close the sun is to the zenith.
1416
""";
@@ -24,7 +26,7 @@ public class CelestialData : Node
2426
private Coordinate Coordinate { get; set; } = new(0, 0, El);
2527
private bool _invalidated = true;
2628

27-
public CelestialData()
29+
private CelestialData()
2830
{
2931
Global.SensitiveData.PropertyChanged += (_, propertyChangedEvent) =>
3032
{

Project-Aurora/Project-Aurora/Nodes/DesktopNode.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace AuroraRgb.Nodes;
66

77
public class DesktopNode : Node
88
{
9+
public static DesktopNode Instance { get; } = new();
10+
911
/// <summary>
1012
/// Returns whether or not the device dession is in a locked state.
1113
/// </summary>
@@ -34,7 +36,7 @@ public class DesktopNode : Node
3436
@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\CloudStore\\Store\\DefaultAccount\\Current\\default$windows.data.bluelightreduction.settings\\windows.data.bluelightreduction.settings",
3537
"Data");
3638

37-
public DesktopNode()
39+
private DesktopNode()
3840
{
3941
_accentColorWatcher.RegistryChanged += UpdateAccentColor;
4042
_accentColorWatcher.StartWatching();

Project-Aurora/Project-Aurora/Nodes/DevicesNode.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
public class DevicesNode: Node
44
{
5+
public static readonly DevicesNode Instance = new();
6+
57
public Controllers Controllers { get; } = new();
68
public RazerDevices RazerDevices { get; } = new();
9+
10+
private DevicesNode()
11+
{
12+
}
713
}

Project-Aurora/Project-Aurora/Nodes/LocalPCInformation.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace AuroraRgb.Nodes;
1111
[GameStateDescription(Description)]
1212
public class LocalPcInformation : Node
1313
{
14+
public static LocalPcInformation Instance { get; } = new();
15+
1416
private const string Description = """
1517
Information is mostly provided by LibreHardwareMonitor
1618
@@ -39,4 +41,8 @@ Information is mostly provided by LibreHardwareMonitor
3941
public static BatteryNode Battery => _battery ??= new BatteryNode();
4042

4143
public static List<string> NetworkAdapters => HardwareMonitor.NetworkAdapters;
44+
45+
private LocalPcInformation()
46+
{
47+
}
4248
}

Project-Aurora/Project-Aurora/Nodes/MediaNode.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace AuroraRgb.Nodes;
66
[GameStateDescription(Description)]
77
public class MediaNode : Node
88
{
9+
public static MediaNode Instance { get; } = new();
10+
911
private const string Description = """
1012
Data is provided by Dubya.WindowsMediaController
1113
""";
@@ -14,4 +16,8 @@ Data is provided by Dubya.WindowsMediaController
1416
public static bool HasMedia => MediaMonitor.HasMedia;
1517
public static bool HasNextMedia => MediaMonitor.HasNextMedia;
1618
public static bool HasPreviousMedia => MediaMonitor.HasPreviousMedia;
19+
20+
private MediaNode()
21+
{
22+
}
1723
}

Project-Aurora/Project-Aurora/Nodes/ProcessesNode.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace AuroraRgb.Nodes;
44

55
public class ProcessesNode : Node
66
{
7+
public static readonly ProcessesNode Instance = new();
78
/// <summary>
89
/// Returns focused window's name.
910
/// </summary>
@@ -13,4 +14,7 @@ public class ProcessesNode : Node
1314
/// Returns focused window's process name.
1415
/// </summary>
1516
public static string ActiveProcess => ProcessesModule.ActiveProcessMonitor.Result.ProcessName;
17+
18+
private ProcessesNode()
19+
{}
1620
}

Project-Aurora/Project-Aurora/Profiles/GameState.cs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,30 @@ public interface IGameState {
3232
TEnum GetEnum<TEnum>(VariablePath path) where TEnum : Enum;
3333

3434
FrozenDictionary<string, Func<IGameState, object?>> PropertyMap { get; }
35-
36-
[Obsolete("Used for reflection access to NewtonsoftGameState properties")]
37-
Lazy<ObjectAccessor> LazyObjectAccessor { get; }
3835
}
3936

4037
public abstract class GameState : IGameState
4138
{
42-
private static LocalPcInformation? _localPcInfo;
43-
4439
[PublicAPI] // game profiles can still access this
45-
public static LocalPcInformation LocalPCInfo => _localPcInfo ??= new LocalPcInformation();
40+
public static LocalPcInformation LocalPCInfo => LocalPcInformation.Instance;
4641

47-
private static AudioNode? _audio;
4842
[PublicAPI]
49-
public static AudioNode Audio => _audio ??= new AudioNode();
43+
public static AudioNode Audio => AudioNode.Instance;
5044

51-
private static DevicesNode? _devices;
5245
[PublicAPI]
53-
public static DevicesNode Devices => _devices ??= new DevicesNode();
46+
public static DevicesNode Devices => DevicesNode.Instance;
5447

55-
private DesktopNode? _desktop;
5648
[PublicAPI]
57-
public DesktopNode Desktop => _desktop ??= new DesktopNode();
49+
public static DesktopNode Desktop => DesktopNode.Instance;
5850

59-
private static MediaNode? _media;
6051
[PublicAPI]
61-
public static MediaNode Media => _media ??= new MediaNode();
52+
public static MediaNode Media => MediaNode.Instance;
6253

63-
private CelestialData? _celestialData;
64-
public CelestialData CelestialData => _celestialData ??= new CelestialData();
65-
66-
private static ProcessesNode? _processes;
6754
[PublicAPI]
68-
public static ProcessesNode Processes => _processes ??= new ProcessesNode();
55+
public static CelestialData CelestialData => CelestialData.Instance;
6956

70-
[GameStateIgnore]
71-
public Lazy<ObjectAccessor> LazyObjectAccessor { get; }
57+
[PublicAPI]
58+
public static ProcessesNode Processes => ProcessesNode.Instance;
7259

7360
[JsonIgnore]
7461
[GameStateIgnore]
@@ -79,7 +66,6 @@ public abstract class GameState : IGameState
7966
/// </summary>
8067
protected GameState()
8168
{
82-
LazyObjectAccessor = new Lazy<ObjectAccessor>(() => ObjectAccessor.Create(this));
8369
}
8470

8571
#region GameState path resolution
@@ -132,11 +118,16 @@ public partial class NewtonsoftGameState : GameState
132118
/// </summary>
133119
[GameStateIgnore]
134120
public bool Announce { get; } = true;
121+
122+
[GameStateIgnore]
123+
[Obsolete("Used for reflection access to NewtonsoftGameState properties")]
124+
public Lazy<ObjectAccessor> LazyObjectAccessor { get; }
135125

136126
public NewtonsoftGameState()
137127
{
138128
Json = "{}";
139129
_parsedData = new(() => new JObject());
130+
LazyObjectAccessor = new Lazy<ObjectAccessor>(() => ObjectAccessor.Create(this));
140131
}
141132

142133
public NewtonsoftGameState(string json, bool announce) : this(json)
@@ -151,6 +142,7 @@ public NewtonsoftGameState(string json)
151142

152143
Json = json;
153144
_parsedData = new(() =>JObject.Parse(json));
145+
LazyObjectAccessor = new Lazy<ObjectAccessor>(() => ObjectAccessor.Create(this));
154146
}
155147

156148
/// <summary>

Project-Aurora/Project-Aurora/Utils/FastMemberExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static class FastMemberExtensions {
3737

3838
// Otherwise if this is any other object, use FastMember to access the relevant property/field.
3939
else
40-
curObj = curObj is IGameState gs ? gs.LazyObjectAccessor.Value[part] : ObjectAccessor.Create(curObj)[part];
40+
curObj = curObj is NewtonsoftGameState gs ? gs.LazyObjectAccessor.Value[part] : ObjectAccessor.Create(curObj)[part];
4141
}
4242

4343
return curObj; // If we got here, there is a valid object at this path, return it.

0 commit comments

Comments
 (0)