1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Linq ;
3
4
using System . Text ;
4
5
using System . Windows . Forms ;
@@ -8,7 +9,7 @@ namespace GPUPrefSwitcher
8
9
/// <summary>
9
10
/// Represents an App Entry in the Registry along with the user's preferences.
10
11
/// </summary>
11
- public struct AppEntry
12
+ public struct AppEntry : ICloneable
12
13
{
13
14
public required string AppPath { get ; init ; }
14
15
@@ -44,6 +45,7 @@ readonly get
44
45
public required bool SeenInRegistry { get ; init ; }
45
46
public override readonly bool Equals ( object obj )
46
47
{
48
+
47
49
return obj is AppEntry entry &&
48
50
AppPath == entry . AppPath &&
49
51
AppName == entry . AppName &&
@@ -55,35 +57,69 @@ public override readonly bool Equals(object obj)
55
57
GPUPrefPluggedIn == entry . GPUPrefPluggedIn &&
56
58
RunOnBatteryPath == entry . RunOnBatteryPath &&
57
59
RunPluggedInPath == entry . RunPluggedInPath &&
58
- PendingAddToRegistry == entry . PendingAddToRegistry ;
60
+ PendingAddToRegistry == entry . PendingAddToRegistry &&
61
+ SeenInRegistry == entry . SeenInRegistry &&
62
+ SwapperStates . SequenceEqual ( entry . SwapperStates ) ;
63
+
64
+
65
+ /*
66
+ bool yes = obj is AppEntry entry &&
67
+ AppPath == entry.AppPath &&
68
+ AppName == entry.AppName &&
69
+ //appName == entry.appName && //breaks for some reason; null comparison with empty string... let's just exclude this since we're not using it for now
70
+ EnableSwitcher == entry.EnableSwitcher &&
71
+ EnableFileSwapper == entry.EnableFileSwapper &&
72
+ FileSwapperPaths.SequenceEqual(entry.FileSwapperPaths) &&
73
+ GPUPrefOnBattery == entry.GPUPrefOnBattery &&
74
+ GPUPrefPluggedIn == entry.GPUPrefPluggedIn &&
75
+ RunOnBatteryPath == entry.RunOnBatteryPath &&
76
+ RunPluggedInPath == entry.RunPluggedInPath &&
77
+ PendingAddToRegistry == entry.PendingAddToRegistry &&
78
+ SeenInRegistry == entry.SeenInRegistry &&
79
+ SwapperStates.SequenceEqual(entry.SwapperStates);
80
+
81
+ Logger.inst.Log($"Are the same: {yes}: {this} versus {(AppEntry)obj}");
82
+
83
+ return yes;
84
+ */
59
85
}
60
86
61
87
//Equals() is much faster
62
88
public override readonly int GetHashCode ( )
63
89
{
90
+
64
91
int hashCode = - 985154422 ;
65
92
hashCode = hashCode * - 1521134295 + EqualityComparer < string > . Default . GetHashCode ( AppPath ) ;
66
- //hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(appName); //see above comment for appName
93
+ //hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(appName);
67
94
hashCode = hashCode * - 1521134295 + EqualityComparer < string > . Default . GetHashCode ( AppName ) ;
68
95
hashCode = hashCode * - 1521134295 + EnableSwitcher . GetHashCode ( ) ;
69
96
hashCode = hashCode * - 1521134295 + EnableFileSwapper . GetHashCode ( ) ;
70
- hashCode = hashCode * - 1521134295 + GetStringArrHash ( FileSwapperPaths ) ;
71
- hashCode = hashCode * - 1521134295 + GetStringArrHash ( FileSwapperPaths ) ;
97
+ hashCode = hashCode * - 1521134295 + GetArrHash ( FileSwapperPaths ) ;
98
+ hashCode = hashCode * - 1521134295 + GetArrHash ( FileSwapperPaths ) ;
72
99
hashCode = hashCode * - 1521134295 + GPUPrefOnBattery . GetHashCode ( ) ;
73
100
hashCode = hashCode * - 1521134295 + GPUPrefPluggedIn . GetHashCode ( ) ;
74
101
hashCode = hashCode * - 1521134295 + RunOnBatteryPath . GetHashCode ( ) ;
75
102
hashCode = hashCode * - 1521134295 + RunPluggedInPath . GetHashCode ( ) ;
76
103
hashCode = hashCode * - 1521134295 + PendingAddToRegistry . GetHashCode ( ) ;
104
+ hashCode = hashCode * - 1521134295 + SeenInRegistry . GetHashCode ( ) ;
105
+ hashCode = hashCode * - 1521134295 + GetArrHash ( from s in SwapperStates select s . ToString ( ) ) ; //a bit hacky but it should work
106
+
77
107
//TODO: need for AppName
78
108
return hashCode ;
79
109
}
80
110
81
- public static int GetStringArrHash ( string [ ] strings )
111
+ public static int GetArrHash ( IEnumerable < object > objs )
82
112
{
83
113
int hash = - 335392656 ;
84
- for ( int i = 0 ; i < strings . Length ; i ++ )
114
+ /*
115
+ for (int i = 0; i < objs.Length; i++)
116
+ {
117
+ hash = hash * -130699793 + objs[i].GetHashCode();
118
+ }
119
+ */
120
+ foreach ( object obj in objs )
85
121
{
86
- hash = hash * - 130699793 + strings [ i ] . GetHashCode ( ) ;
122
+ hash = hash * - 130699793 + obj . GetHashCode ( ) ;
87
123
}
88
124
return hash ;
89
125
}
@@ -93,6 +129,7 @@ public override string ToString()
93
129
StringBuilder sb = new ( ) ;
94
130
sb . AppendLine ( $ "AppEntry (enabled: { EnableSwitcher } ; appname: { AppName } ): { AppPath } ") ;
95
131
sb . AppendLine ( $ "On Battery: { GPUPrefOnBattery } ; Plugged in: On Battery: { GPUPrefPluggedIn } ") ;
132
+ sb . AppendLine ( $ "Pending add: { PendingAddToRegistry } ") ;
96
133
sb . AppendLine ( $ "File swapper (enabled: { EnableFileSwapper } ):") ;
97
134
for ( int i = 0 ; i < FileSwapperPaths . Length ; i ++ )
98
135
{
@@ -101,6 +138,25 @@ public override string ToString()
101
138
return sb . ToString ( ) ;
102
139
}
103
140
141
+ public object Clone ( )
142
+ {
143
+ return new AppEntry ( )
144
+ {
145
+ AppPath = AppPath ,
146
+ AppName = AppName ,
147
+ EnableSwitcher = EnableSwitcher ,
148
+ EnableFileSwapper = EnableFileSwapper ,
149
+ FileSwapperPaths = ( from s in FileSwapperPaths select s ) . ToArray ( ) ,
150
+ GPUPrefOnBattery = GPUPrefOnBattery ,
151
+ GPUPrefPluggedIn = GPUPrefPluggedIn ,
152
+ RunOnBatteryPath = RunOnBatteryPath ,
153
+ RunPluggedInPath = RunPluggedInPath ,
154
+ PendingAddToRegistry = PendingAddToRegistry ,
155
+ SeenInRegistry = SeenInRegistry ,
156
+ SwapperStates = ( from s in SwapperStates select s ) . ToArray ( ) ,
157
+ } ;
158
+ }
159
+
104
160
public static bool operator == ( AppEntry left , AppEntry right )
105
161
{
106
162
return left . Equals ( right ) ;
0 commit comments