Skip to content

Commit 0a6909e

Browse files
committed
code cleanup
1 parent e156cd8 commit 0a6909e

File tree

2 files changed

+55
-125
lines changed

2 files changed

+55
-125
lines changed

GPUPrefSwitcher/AppEntrySaveHandler.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,15 @@ public AppEntrySaveHandler()
134134
//currentAppEntries = PreferencesXML.GetAppEntries();
135135
currentAppEntries = DeepCopyAppEntries(prevAppEntries);
136136

137-
//Logger.inst.Log(currentAppEntries.Single(x => x.AppPath == "F:\\SteamLibrary\\steamapps\\common\\Apex Legends\\r5apex.exe").ToString());
138-
//Logger.inst.Log(prevAppEntries.Single(x => x.AppPath == "F:\\SteamLibrary\\steamapps\\common\\Apex Legends\\r5apex.exe").ToString());
139137
}
140138

141139
public void ChangeAppEntryByPath(string path, AppEntry updatedAppEntry)
142140
{
143141

144-
//Logger.inst.Log($"Attempting to change \n {currentAppEntries.Single(x => x.AppPath == path)}\n to \n {updatedAppEntry}");
145-
146-
//Logger.inst.Log($"prev: {CurrentAppEntries[CurrentAppEntries.IndexOf(CurrentAppEntries.Single(x => x.AppPath == path))]}");
147-
148142
int index = currentAppEntries.IndexOf(CurrentAppEntries.Single(x => x.AppPath == path));
149143

150144
currentAppEntries[index] = updatedAppEntry;
151145

152-
//SaveAppEntryChanges_Internal();
153-
154146
//Logger.inst.Log($"new: {CurrentAppEntries[CurrentAppEntries.IndexOf(CurrentAppEntries.Single(x => x.AppPath == path))]}");
155147
}
156148

@@ -177,8 +169,6 @@ public void SaveAppEntryChanges()
177169

178170
private void SaveAppEntryChanges_Internal()
179171
{
180-
//Logger.inst.Log(currentAppEntries.Single(x => x.AppPath == "F:\\SteamLibrary\\steamapps\\common\\Apex Legends\\r5apex.exe").ToString());
181-
//Logger.inst.Log(prevAppEntries.Single(x => x.AppPath == "F:\\SteamLibrary\\steamapps\\common\\Apex Legends\\r5apex.exe").ToString());
182172
List<AppEntry> differences = new();
183173
//differences.AddRange(currentAppEntries.Where(entry => NotSameOrInPrevAppEntries(entry)));
184174
foreach(AppEntry a in currentAppEntries)
@@ -220,9 +210,6 @@ private void SaveAppEntryChanges_Internal()
220210

221211
prevAppEntries = DeepCopyAppEntries(currentAppEntries); //update the saved entries
222212

223-
//Logger.inst.Log(currentAppEntries.Single(x => x.AppPath == "F:\\SteamLibrary\\steamapps\\common\\Apex Legends\\r5apex.exe").ToString());
224-
//Logger.inst.Log(prevAppEntries.Single(x => x.AppPath == "F:\\SteamLibrary\\steamapps\\common\\Apex Legends\\r5apex.exe").ToString());
225-
226213
Logger.inst.Log($"Concluded saving. Differences: {differences.Count} Added: {needToAdd.Count} Removed: {needToRemoveFromXML.Count}");
227214

228215
/*
@@ -238,14 +225,6 @@ private void SaveAppEntryChanges_Internal()
238225

239226
bool NotSameOrInPrevAppEntries(AppEntry appEntry)
240227
{
241-
/*
242-
if (appEntry.AppPath == "F:\\SteamLibrary\\steamapps\\common\\Apex Legends\\r5apex.exe")
243-
{
244-
Logger.inst.Log(prevAppEntries.Single(x => x.AppPath == "F:\\SteamLibrary\\steamapps\\common\\Apex Legends\\r5apex.exe").ToString());
245-
Logger.inst.Log(CurrentAppEntries.Single(x => x.AppPath == "F:\\SteamLibrary\\steamapps\\common\\Apex Legends\\r5apex.exe").ToString());
246-
Logger.inst.Log($"{appEntry} not in prev app entries: {!prevAppEntries.Contains(appEntry)}");
247-
}
248-
*/
249228
return !prevAppEntries.Contains(appEntry); //Contains uses Equals() which is implemented in AppEntry
250229
}
251230
}

GPUPrefSwitcher/PreferencesXML.cs

Lines changed: 55 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,6 @@ namespace GPUPrefSwitcher
1919
/// </summary>
2020
public class PreferencesXML
2121
{
22-
/*
23-
public class ThreadSafeXmlDoc
24-
{
25-
//private XmlDocument xmlDocument = new XmlDocument();
26-
private SemaphoreSlim SemaphoreSlim = new SemaphoreSlim(1);
27-
private XmlDocument xmlDocument = new();
28-
29-
public void Load(string path)
30-
{
31-
SemaphoreSlim.Wait();
32-
try
33-
{
34-
xmlDocument.Load(path);
35-
}
36-
finally { SemaphoreSlim.Release(); }
37-
}
38-
39-
public void Save(string path)
40-
{
41-
SemaphoreSlim.Wait();
42-
try
43-
{
44-
xmlDocument.Save(path);
45-
}
46-
finally { SemaphoreSlim.Release(); }
47-
}
48-
49-
public XmlNodeList GetElementsByTagName(string tagName)
50-
{
51-
SemaphoreSlim.Wait();
52-
try
53-
{
54-
return xmlDocument.GetElementsByTagName(tagName);
55-
}
56-
finally { SemaphoreSlim.Release() ; }
57-
}
58-
}
59-
*/
6022

6123

6224
public static readonly string XML_PREFERENCES_PATH = Program.SavedDataPath + "Preferences.xml";
@@ -160,80 +122,69 @@ internal void ReloadXML()
160122
/// <returns>A readonly list of AppEntries; you must call the modify or save functions to actually change these in the XML document.</returns>
161123
internal List<AppEntry> GetAppEntries()
162124
{
125+
xmlDocument.Load(XML_PREFERENCES_PATH); //performance hog?
163126

164-
//try
165-
//{
166-
//semaphoreSlim.Wait();
167-
168-
xmlDocument.Load(XML_PREFERENCES_PATH); //performance hog?
127+
XmlNodeList xmlAppEntries = xmlDocument.GetElementsByTagName(XML_APP_ENTRY);
169128

170-
XmlNodeList xmlAppEntries = xmlDocument.GetElementsByTagName(XML_APP_ENTRY);
129+
List<AppEntry> appEntries = new List<AppEntry>();
171130

172-
List<AppEntry> appEntries = new List<AppEntry>();
131+
foreach (XmlNode xmlAppEntry in xmlAppEntries)
132+
{
173133

174-
foreach (XmlNode xmlAppEntry in xmlAppEntries)
134+
string appPath;
135+
bool enableSwitcher;
136+
int gpuPrefOnBattery;
137+
int gpuPrefPluggedIn;
138+
bool enableFileSwapper;
139+
string runOnBatteryPath;
140+
string runPluggedInPath;
141+
bool seenInRegistry;
142+
143+
appPath = xmlAppEntry.SelectSingleNode(XML_APP_PATH).InnerText;
144+
//Debug.WriteLine(appPath);
145+
XmlNode xmlGpuPreference = xmlAppEntry.SelectSingleNode(XML_GPU_PREFERENCE);
146+
enableSwitcher = bool.Parse(xmlGpuPreference.Attributes[XML_ATTR_ENABLESWITCHER].Value);
147+
gpuPrefOnBattery = int.Parse(xmlGpuPreference.SelectSingleNode(XML_ON_BATTERY).InnerText);
148+
gpuPrefPluggedIn = int.Parse(xmlGpuPreference.SelectSingleNode(XML_PLUGGED_IN).InnerText);
149+
enableFileSwapper = bool.Parse(xmlGpuPreference.Attributes[XML_ATTR_ENABLEFILESWAPPER].Value);
150+
runOnBatteryPath = xmlGpuPreference.SelectSingleNode(XML_RUN_ON_BATTERY_PATH).InnerText;
151+
runPluggedInPath = xmlGpuPreference.SelectSingleNode(XML_RUN_PLUGGED_IN_PATH).InnerText;
152+
seenInRegistry = bool.Parse(xmlAppEntry.SelectSingleNode(XML_SEEN_IN_REGISTRY).InnerText);
153+
154+
string[] swapPaths = GetAppEntryFileSwapperPaths(xmlAppEntry);
155+
156+
bool pendingAddToRegistry = false;
157+
if (xmlAppEntry.SelectSingleNode(XML_PENDING_ADD) != null)
175158
{
159+
pendingAddToRegistry = true;
160+
}
176161

177-
string appPath;
178-
bool enableSwitcher;
179-
int gpuPrefOnBattery;
180-
int gpuPrefPluggedIn;
181-
bool enableFileSwapper;
182-
string runOnBatteryPath;
183-
string runPluggedInPath;
184-
bool seenInRegistry;
185-
186-
appPath = xmlAppEntry.SelectSingleNode(XML_APP_PATH).InnerText;
187-
//Debug.WriteLine(appPath);
188-
XmlNode xmlGpuPreference = xmlAppEntry.SelectSingleNode(XML_GPU_PREFERENCE);
189-
enableSwitcher = bool.Parse(xmlGpuPreference.Attributes[XML_ATTR_ENABLESWITCHER].Value);
190-
gpuPrefOnBattery = int.Parse(xmlGpuPreference.SelectSingleNode(XML_ON_BATTERY).InnerText);
191-
gpuPrefPluggedIn = int.Parse(xmlGpuPreference.SelectSingleNode(XML_PLUGGED_IN).InnerText);
192-
enableFileSwapper = bool.Parse(xmlGpuPreference.Attributes[XML_ATTR_ENABLEFILESWAPPER].Value);
193-
runOnBatteryPath = xmlGpuPreference.SelectSingleNode(XML_RUN_ON_BATTERY_PATH).InnerText;
194-
runPluggedInPath = xmlGpuPreference.SelectSingleNode(XML_RUN_PLUGGED_IN_PATH).InnerText;
195-
seenInRegistry = bool.Parse(xmlAppEntry.SelectSingleNode(XML_SEEN_IN_REGISTRY).InnerText);
196-
197-
string[] swapPaths = GetAppEntryFileSwapperPaths(xmlAppEntry);
198-
199-
bool pendingAddToRegistry = false;
200-
if (xmlAppEntry.SelectSingleNode(XML_PENDING_ADD) != null)
201-
{
202-
pendingAddToRegistry = true;
203-
}
204-
205-
string[] fileSwapPathStatus = new string[swapPaths.Length];
206-
XmlNodeList swapPathNodes = GetAppEntryFileSwapperXmlNodes(xmlAppEntry);
207-
for (int i = 0; i < swapPaths.Length; i++)
208-
{
209-
string state = swapPathNodes[i].Attributes["state"].Value;
210-
fileSwapPathStatus[i] = state;
211-
}
212-
213-
appEntries.Add(
214-
new AppEntry()
215-
{
216-
AppPath = appPath,
217-
EnableSwitcher = enableSwitcher,
218-
GPUPrefOnBattery = gpuPrefOnBattery,
219-
GPUPrefPluggedIn = gpuPrefPluggedIn,
220-
EnableFileSwapper = enableFileSwapper,
221-
FileSwapperPaths = swapPaths,
222-
SwapperStates = PowerLineStatusConversions.StringArrToPowerLineStatusArr(fileSwapPathStatus),
223-
PendingAddToRegistry = pendingAddToRegistry,
224-
RunOnBatteryPath = runOnBatteryPath,
225-
RunPluggedInPath = runPluggedInPath,
226-
SeenInRegistry = seenInRegistry,
227-
}
228-
);
162+
string[] fileSwapPathStatus = new string[swapPaths.Length];
163+
XmlNodeList swapPathNodes = GetAppEntryFileSwapperXmlNodes(xmlAppEntry);
164+
for (int i = 0; i < swapPaths.Length; i++)
165+
{
166+
string state = swapPathNodes[i].Attributes["state"].Value;
167+
fileSwapPathStatus[i] = state;
229168
}
230-
return appEntries;
231-
/*
232-
} finally
233-
{
234-
semaphoreSlim.Release();
169+
170+
appEntries.Add(
171+
new AppEntry()
172+
{
173+
AppPath = appPath,
174+
EnableSwitcher = enableSwitcher,
175+
GPUPrefOnBattery = gpuPrefOnBattery,
176+
GPUPrefPluggedIn = gpuPrefPluggedIn,
177+
EnableFileSwapper = enableFileSwapper,
178+
FileSwapperPaths = swapPaths,
179+
SwapperStates = PowerLineStatusConversions.StringArrToPowerLineStatusArr(fileSwapPathStatus),
180+
PendingAddToRegistry = pendingAddToRegistry,
181+
RunOnBatteryPath = runOnBatteryPath,
182+
RunPluggedInPath = runPluggedInPath,
183+
SeenInRegistry = seenInRegistry,
184+
}
185+
);
235186
}
236-
*/
187+
return appEntries;
237188

238189
}
239190

0 commit comments

Comments
 (0)