Skip to content

Commit c8eaf7b

Browse files
committed
Switched to the new VDF parsing library, code cleanup
1 parent 75d2d01 commit c8eaf7b

20 files changed

+344
-246
lines changed

src/LightSteamAccountSwitcher.Core/Services/AppDataHelper.cs renamed to src/LightSteamAccountSwitcher.Core/AppDataHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace LightSteamAccountSwitcher.Core.Services;
1+
namespace LightSteamAccountSwitcher.Core;
22

33
public static class AppDataHelper
44
{

src/LightSteamAccountSwitcher.Core/LightSteamAccountSwitcher.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Gameloop.Vdf" Version="0.6.2"/>
10+
<Folder Include="Services\" />
1111
</ItemGroup>
1212

1313
</Project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Diagnostics;
2+
3+
namespace LightSteamAccountSwitcher.Core;
4+
5+
public static class Logger
6+
{
7+
private static readonly string LogPath = Path.Combine(AppDataHelper.GetAppDataPath(), "app.log");
8+
private static readonly Lock Lock = new();
9+
10+
public static void Info(string message)
11+
{
12+
Write("INFO", message);
13+
}
14+
15+
public static void Warn(string message)
16+
{
17+
Write("WARN", message);
18+
}
19+
20+
public static void Error(string message, Exception? ex = null)
21+
{
22+
var msg = message;
23+
if (ex != null)
24+
{
25+
msg += $"\nException: {ex}";
26+
}
27+
28+
Write("ERROR", msg);
29+
}
30+
31+
private static void Write(string level, string message)
32+
{
33+
var logEntry = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{level}] {message}";
34+
35+
// IDE Output
36+
Debug.WriteLine(logEntry);
37+
38+
// File Output
39+
try
40+
{
41+
lock (Lock)
42+
{
43+
File.AppendAllText(LogPath, logEntry + Environment.NewLine);
44+
}
45+
}
46+
catch (Exception)
47+
{
48+
// Fallback if file is locked or inaccessible
49+
Debug.WriteLine($"[Log Failure] Could not write to log file: {message}");
50+
}
51+
}
52+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace LightSteamAccountSwitcher.Core.Models;
2+
3+
public class AppSettings
4+
{
5+
public int Version { get; set; } = -1;
6+
7+
public bool AutoClose { get; set; }
8+
}

src/LightSteamAccountSwitcher.Core/Services/SettingsService.cs renamed to src/LightSteamAccountSwitcher.Core/SettingsHelper.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
using System.Text.Json;
2+
using LightSteamAccountSwitcher.Core.Models;
23
using LightSteamAccountSwitcher.Core.Utils;
34

4-
namespace LightSteamAccountSwitcher.Core.Services;
5+
namespace LightSteamAccountSwitcher.Core;
56

6-
public class AppSettings
7-
{
8-
public int Version { get; set; } = -1;
9-
public bool AutoClose { get; set; }
10-
}
11-
12-
public static class SettingsService
7+
public static class SettingsHelper
138
{
149
private const int CurrentVersion = 1;
1510
private static readonly string SettingsPath = Path.Combine(AppDataHelper.GetAppDataPath(), "settings.json");
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
using System.Text.Json.Serialization;
22
using LightSteamAccountSwitcher.Core.Models;
3-
using LightSteamAccountSwitcher.Core.Services;
43

54
namespace LightSteamAccountSwitcher.Core.Utils;
65

76
[JsonSerializable(typeof(AppSettings))]
87
[JsonSerializable(typeof(List<SteamProfile>))]
9-
public partial class AppJsonContext : JsonSerializerContext
10-
{
11-
}
8+
public partial class AppJsonContext : JsonSerializerContext;

src/LightSteamAccountSwitcher.Steam/ImageCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.IO;
22
using System.Net.Http;
3-
using LightSteamAccountSwitcher.Core.Services;
3+
using LightSteamAccountSwitcher.Core;
44

55
namespace LightSteamAccountSwitcher.Steam;
66

@@ -75,7 +75,7 @@ public static bool IsCacheValid(string steamId)
7575
}
7676
catch (Exception ex)
7777
{
78-
Console.WriteLine($"Failed to download avatar for {steamId}: {ex.Message}");
78+
Logger.Error($"Failed to download avatar for {steamId}: {ex.Message}");
7979
return null;
8080
}
8181
}

src/LightSteamAccountSwitcher.Steam/LightSteamAccountSwitcher.Steam.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="Gameloop.Vdf" Version="0.6.2"/>
16+
<PackageReference Include="VdfSerializer" Version="1.0.0"/>
1717
</ItemGroup>
1818

1919
</Project>

src/LightSteamAccountSwitcher.Steam/SteamService.cs

Lines changed: 51 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using System.IO;
22
using System.Net.Http;
3-
using System.Text;
43
using System.Xml.Linq;
5-
using Gameloop.Vdf;
6-
using Gameloop.Vdf.Linq;
4+
using LightSteamAccountSwitcher.Core;
75
using LightSteamAccountSwitcher.Core.Models;
8-
using LightSteamAccountSwitcher.Core.Services;
96
using LightSteamAccountSwitcher.Windows;
7+
using VdfSerializer;
8+
using VdfSerializer.Linq;
109

1110
namespace LightSteamAccountSwitcher.Steam;
1211

@@ -29,12 +28,7 @@ public SteamService()
2928
private static string? GetLoginUsersPath()
3029
{
3130
var steamPath = SteamRegistryHelper.GetSteamPath();
32-
if (string.IsNullOrEmpty(steamPath))
33-
{
34-
return null;
35-
}
36-
37-
return Path.Combine(steamPath, "config", "loginusers.vdf");
31+
return string.IsNullOrEmpty(steamPath) ? null : Path.Combine(steamPath, "config", "loginusers.vdf");
3832
}
3933

4034
public static string? GetActiveAccountName()
@@ -77,7 +71,7 @@ public List<SteamAccount> GetSteamUsers()
7771
}
7872
catch (Exception ex)
7973
{
80-
Console.WriteLine($"Error parsing loginusers.vdf: {ex.Message}");
74+
Logger.Error($"Error parsing loginusers.vdf: {ex.Message}");
8175
}
8276

8377
return accounts;
@@ -91,12 +85,12 @@ public List<SteamAccount> GetSteamUsers()
9185
return null;
9286
}
9387

94-
var accountName = userDetails.Value<string>("AccountName");
95-
var personaName = userDetails.Value<string>("PersonaName");
96-
var timestamp = userDetails.Value<string>("Timestamp");
97-
var wantsOffline = userDetails.Value<string>("WantsOfflineMode");
98-
var mostRecent = userDetails.Value<string>("MostRec");
99-
var remember = userDetails.Value<string>("RememberPassword");
88+
var accountName = userDetails.Value<string>("AccountName")!;
89+
var personaName = userDetails.Value<string>("PersonaName")!;
90+
var timestamp = userDetails.Value<string>("Timestamp")!;
91+
var wantsOffline = userDetails.Value<string>("WantsOfflineMode")!;
92+
var mostRecent = userDetails.Value<string>("MostRec")!;
93+
var remember = userDetails.Value<string>("RememberPassword")!;
10094

10195
if (string.IsNullOrEmpty(accountName))
10296
{
@@ -185,7 +179,7 @@ public async Task EnrichAccountInfo(SteamAccount? account)
185179
}
186180
catch (Exception ex)
187181
{
188-
Console.WriteLine($"Error fetching profile for {account.AccountName}: {ex.Message}");
182+
Logger.Error($"Error fetching profile for {account.AccountName}: {ex.Message}");
189183
}
190184
}
191185

@@ -237,7 +231,7 @@ public static void ForgetAccount(string steamId)
237231
}
238232
catch (Exception ex)
239233
{
240-
Console.WriteLine($"Error forgetting account: {ex}");
234+
Logger.Error($"Error forgetting account: {ex}");
241235
}
242236
}
243237

@@ -254,44 +248,46 @@ private void PatchLoginUsers(string targetSteamId, int personaState = -1)
254248
var vdfText = File.ReadAllText(path);
255249
var root = VdfConvert.Deserialize(vdfText);
256250

257-
if (root.Value is VObject usersObj)
251+
if (root.Value is not VObject usersObj)
252+
{
253+
return;
254+
}
255+
256+
foreach (var userProp in usersObj)
258257
{
259-
foreach (var userProp in usersObj)
258+
if (userProp.Value is not VObject userDetails)
259+
{
260+
continue;
261+
}
262+
263+
if (userDetails.ContainsKey("MostRec"))
264+
{
265+
userDetails["MostRec"] = new VValue("0");
266+
}
267+
268+
if (userProp.Key != targetSteamId)
260269
{
261-
if (userProp.Value is not VObject userDetails)
262-
{
263-
continue;
264-
}
265-
266-
if (userDetails.ContainsKey("MostRec"))
267-
{
268-
userDetails["MostRec"] = new VValue("0");
269-
}
270-
271-
if (userProp.Key != targetSteamId)
272-
{
273-
continue;
274-
}
275-
276-
userDetails["MostRec"] = new VValue("1");
277-
userDetails["RememberPassword"] = new VValue("1");
278-
279-
if (personaState == -1)
280-
{
281-
continue;
282-
}
283-
284-
var wantsOffline = personaState == 0 ? "1" : "0";
285-
userDetails["WantsOfflineMode"] = new VValue(wantsOffline);
286-
userDetails["SkipOfflineModeWarning"] = new VValue(wantsOffline);
270+
continue;
287271
}
272+
273+
userDetails["MostRec"] = new VValue("1");
274+
userDetails["RememberPassword"] = new VValue("1");
275+
276+
if (personaState == -1)
277+
{
278+
continue;
279+
}
280+
281+
var wantsOffline = personaState == 0 ? "1" : "0";
282+
userDetails["WantsOfflineMode"] = new VValue(wantsOffline);
283+
userDetails["SkipOfflineModeWarning"] = new VValue(wantsOffline);
288284
}
289285

290286
File.WriteAllText(path, root.ToString());
291287
}
292288
catch (Exception ex)
293289
{
294-
Console.WriteLine($"Error patching vdf: {ex}");
290+
Logger.Error($"Error patching vdf: {ex}");
295291
}
296292
}
297293

@@ -319,34 +315,21 @@ private void SetPersonaState(string steamId64, int state)
319315
}
320316

321317
var localConfigText = File.ReadAllText(localConfigPath);
318+
var root = VdfConvert.Deserialize(localConfigText);
322319

323-
var positionOfVar = localConfigText.IndexOf("\"ePersonaState\"", StringComparison.Ordinal);
324-
if (positionOfVar == -1)
325-
{
326-
return;
327-
}
328-
329-
var valStartQuote = localConfigText.IndexOf('"', positionOfVar + 15);
330-
if (valStartQuote == -1)
320+
if (root.Value is not VObject rootObj ||
321+
!rootObj.ContainsKey("friends") ||
322+
rootObj["friends"] is not VObject friendsObj)
331323
{
332324
return;
333325
}
334326

335-
var valEndQuote = localConfigText.IndexOf('"', valStartQuote + 1);
336-
if (valEndQuote == -1)
337-
{
338-
return;
339-
}
340-
341-
var sb = new StringBuilder(localConfigText);
342-
sb.Remove(valStartQuote + 1, valEndQuote - valStartQuote - 1);
343-
sb.Insert(valStartQuote + 1, state);
344-
345-
File.WriteAllText(localConfigPath, sb.ToString());
327+
friendsObj["ePersonaState"] = new VValue(state.ToString());
328+
File.WriteAllText(localConfigPath, root.ToString());
346329
}
347330
catch (Exception ex)
348331
{
349-
Console.WriteLine($"Error setting persona state: {ex.Message}");
332+
Logger.Error($"Error setting persona state: {ex.Message}");
350333
}
351334
}
352335

src/LightSteamAccountSwitcher.Core/Services/VacStatusCache.cs renamed to src/LightSteamAccountSwitcher.Steam/VacStatusCache.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
using System.IO;
12
using System.Text.Json;
3+
using LightSteamAccountSwitcher.Core;
24
using LightSteamAccountSwitcher.Core.Models;
35
using LightSteamAccountSwitcher.Core.Utils;
46

5-
namespace LightSteamAccountSwitcher.Core.Services;
7+
namespace LightSteamAccountSwitcher.Steam;
68

79
public class VacStatusCache
810
{
@@ -12,18 +14,18 @@ public class VacStatusCache
1214
private List<SteamProfile> _cache = [];
1315
private bool _isLoaded;
1416

15-
public VacStatusCache()
16-
{
17-
}
18-
1917
private void EnsureLoaded()
2018
{
21-
if (_isLoaded) return;
19+
if (_isLoaded)
20+
{
21+
return;
22+
}
23+
2224
LoadCache();
2325
_isLoaded = true;
2426
}
2527

26-
public void LoadCache()
28+
private void LoadCache()
2729
{
2830
if (!File.Exists(_cacheFileName))
2931
{
@@ -50,7 +52,7 @@ private void Save(IEnumerable<SteamProfile> profiles)
5052
}
5153
catch (Exception ex)
5254
{
53-
Console.WriteLine($"Failed to save vac cache: {ex.Message}");
55+
Logger.Error($"Failed to save vac cache: {ex.Message}");
5456
}
5557
}
5658

0 commit comments

Comments
 (0)