Skip to content

Commit 75a84fa

Browse files
committed
Some listfile util additions
1 parent 3fd404f commit 75a84fa

1 file changed

Lines changed: 53 additions & 6 deletions

File tree

TACTSharp/Listfile.cs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,34 @@ public class Listfile
77
private Lock listfileLock = new();
88
private HttpClient client = new();
99
private Dictionary<ulong, uint> nameHashToFDID = new();
10+
private Dictionary<uint, string> fdidToName = new();
1011
private Jenkins96 hasher = new();
1112

1213
private CDN CDN;
1314
private Settings Settings;
15+
private string ListfilePath = "listfile.csv";
1416

1517
public bool Initialized = false;
1618

17-
public void Initialize(CDN cdn, Settings settings)
19+
public void Initialize(CDN cdn, Settings settings, string path = "listfile.csv", bool useExisting = false)
1820
{
1921
CDN = cdn;
2022
Settings = settings;
23+
ListfilePath = path;
2124

2225
lock (listfileLock)
2326
{
27+
if(useExisting && File.Exists(path))
28+
{
29+
Load();
30+
Initialized = true;
31+
return;
32+
}
33+
2434
if (Initialized)
2535
return;
2636

27-
var fileInfo = new FileInfo("listfile.csv");
37+
var fileInfo = new FileInfo(path);
2838
if (!fileInfo.Exists)
2939
{
3040
Console.WriteLine("Downloading listfile.csv");
@@ -71,10 +81,10 @@ private void Download()
7181
return;
7282
}
7383

74-
if (File.Exists("listfile.csv"))
75-
File.Delete("listfile.csv");
84+
if (File.Exists(ListfilePath))
85+
File.Delete(ListfilePath);
7686

77-
using (var file = new FileStream("listfile.csv", FileMode.OpenOrCreate, FileAccess.Write))
87+
using (var file = new FileStream(ListfilePath, FileMode.OpenOrCreate, FileAccess.Write))
7888
listfileResponse.Content.ReadAsStream().CopyTo(file);
7989
}
8090
}
@@ -83,7 +93,7 @@ private void Load()
8393
{
8494
var sw = new Stopwatch();
8595
sw.Start();
86-
using (var file = new StreamReader("listfile.csv"))
96+
using (var file = new StreamReader(ListfilePath))
8797
{
8898
while (!file.EndOfStream)
8999
{
@@ -105,9 +115,46 @@ private void Load()
105115
Console.WriteLine("Loaded " + nameHashToFDID.Count + " listfile entries in " + sw.Elapsed.TotalMilliseconds + "ms");
106116
}
107117

118+
private void LoadFilenames()
119+
{
120+
var sw = new Stopwatch();
121+
sw.Start();
122+
using (var file = new StreamReader(ListfilePath))
123+
{
124+
while (!file.EndOfStream)
125+
{
126+
var line = file.ReadLine();
127+
if (line == null)
128+
continue;
129+
130+
var parts = line.Split(';');
131+
if (parts.Length < 2)
132+
continue;
133+
134+
if (uint.TryParse(parts[0], out var fdid))
135+
fdidToName[fdid] = parts[1];
136+
}
137+
}
138+
139+
sw.Stop();
140+
141+
Console.WriteLine("Loaded " + fdidToName.Count + " listfile filename entries in " + sw.Elapsed.TotalMilliseconds + "ms");
142+
}
143+
108144
public uint GetFDID(string name)
109145
{
110146
return nameHashToFDID.TryGetValue(hasher.ComputeHash(name, true), out var fdid) ? fdid : 0;
111147
}
148+
149+
public string? GetFilename(uint fdid)
150+
{
151+
if (fdidToName.Count == 0)
152+
LoadFilenames();
153+
154+
if (fdidToName.TryGetValue(fdid, out var name))
155+
return name;
156+
else
157+
return null;
158+
}
112159
}
113160
}

0 commit comments

Comments
 (0)