-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKnownDepotIds.cs
91 lines (77 loc) · 2.62 KB
/
KnownDepotIds.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
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spectre.Console;
#pragma warning disable CA1031 // Do not catch general exception types
namespace SteamTokenDumper;
internal sealed class KnownDepotIds
{
public readonly HashSet<uint> PreviouslySent = [];
public ImmutableHashSet<uint> Server;
private readonly string KnownDepotIdsPath = Path.Combine(Program.AppPath, "SteamTokenDumper.depots.txt");
public async Task Load(ApiClient apiClient)
{
await LoadKnownDepotIds();
Server = await apiClient.GetBackendKnownDepotIds();
AnsiConsole.WriteLine($"Got {Server.Count} depot ids from the backend to skip.");
}
private async Task LoadKnownDepotIds()
{
if (!File.Exists(KnownDepotIdsPath))
{
return;
}
try
{
await foreach (var line in File.ReadLinesAsync(KnownDepotIdsPath))
{
if (line.Length == 0 || line[0] == ';')
{
continue;
}
PreviouslySent.Add(uint.Parse(line, CultureInfo.InvariantCulture));
}
AnsiConsole.WriteLine($"You have sent {PreviouslySent.Count} depot keys before, they will be skipped.");
}
catch (Exception e)
{
AnsiConsole.Write(
new Panel(new Text($"Failed to load known depot ids: {e.Message}", new Style(Color.Red)))
.BorderColor(Color.Red)
.RoundedBorder()
);
}
}
public async Task SaveKnownDepotIds()
{
if (PreviouslySent.Count == 0)
{
return;
}
try
{
var data = new StringBuilder();
data.AppendLine("; This file stores depot ids which you have already sent keys for,");
data.AppendLine("; so they will not be requested again. Do not modify this file.");
data.AppendLine("");
foreach (var depotId in PreviouslySent.OrderBy(x => x))
{
data.AppendLine(depotId.ToString(CultureInfo.InvariantCulture));
}
await File.WriteAllTextAsync(KnownDepotIdsPath, data.ToString());
}
catch (Exception e)
{
AnsiConsole.Write(
new Panel(new Text($"Failed to save known depot ids: {e.Message}", new Style(Color.Red)))
.BorderColor(Color.Red)
.RoundedBorder()
);
}
}
}