-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModCache.cs
160 lines (142 loc) · 5.12 KB
/
ModCache.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System.Text.Json;
using System.Collections.Concurrent;
namespace MyApp
{
public sealed class ModCache
{
class ApiKeysFile
{
public string? steamWebAPI { get; set; }
}
public string? SteamWebAPI { get; private set; } = null;
public Dictionary<uint, ResultEntry> Cache = new Dictionary<uint, ResultEntry>();
private readonly object cachelock = new object();
private ConcurrentDictionary<string, Task<ApiResponse?>> apiQueries = new ConcurrentDictionary<string, Task<ApiResponse?>>();
private ModCache()
{
try
{
var text = File.ReadAllText(getConfigPath());
var config = JsonSerializer.Deserialize<ApiKeysFile>(text);
if (config != null)
{
SteamWebAPI = config.steamWebAPI;
}
}
catch (Exception ex)
{
Console.WriteLine("Exception while trying to read apikeys:\n" + ex);
}
try
{
var text = File.ReadAllText(getCachePath());
var modcache = JsonSerializer.Deserialize<Dictionary<uint, ResultEntry>>(text);
if (modcache != null)
{
Cache = modcache;
}
}
catch (Exception ex)
{
Console.WriteLine("Exception while trying to read modcache:\n" + ex);
}
}
private static string getConfigPath()
{
var configPath = Environment.GetEnvironmentVariable("CONFIGPATH");
var configDir = "config";
if (Directory.Exists(configPath)) configDir = configPath;
return Path.Join(configDir, "apikeys.json");
}
private static string getCachePath()
{
var configPath = Environment.GetEnvironmentVariable("CONFIGPATH");
var configDir = "config";
if (Directory.Exists(configPath)) configDir = configPath;
return Path.Join(configDir, "modcache.json");
}
private void SaveCache()
{
Console.WriteLine("Saving Mod Cache");
JsonSerializerOptions jsonConfig = new()
{
WriteIndented = true,
};
lock (cachelock)
{
var json = JsonSerializer.Serialize(Cache, jsonConfig);
File.WriteAllText(getCachePath(), json);
}
}
class ApiResponse
{
public PublishedFileDetails? response { get; set; }
}
class PublishedFileDetails
{
public List<ResultEntry>? publishedfiledetails { get; set; }
}
public class ResultEntry
{
public int result { get; set; }
public string? publishedfileid { get; set; }
public string? title { get; set; }
public string? file_description { get; set; }
public string? preview_url { get; set; }
}
public async Task<string?> GetModName(uint id)
{
// Look in cache
lock (cachelock)
{
var value = Cache.GetValueOrDefault(id);
if (value != null) return value.title;
}
if (SteamWebAPI == null || SteamWebAPI.Length == 0)
{
return "" + id;
}
// Try to retrieve value
using (var client = new HttpClient())
{
Console.WriteLine("Retriving modinfo for " + id);
// Check if there already is a request in flight for this mod
string url = $"https://api.steampowered.com/IPublishedFileService/GetDetails/v1/?key={SteamWebAPI}&publishedfileids%5B0%5D={id}";
// Guard against multiple concurrent queries for the same url
var task = apiQueries.GetOrAdd(url, client.GetFromJsonAsync<ApiResponse>(url));
var resp = await task;
if (resp != null && resp.response != null && resp.response.publishedfiledetails != null)
{
var list = resp.response.publishedfiledetails;
if (list.Count != 1)
{
Console.WriteLine("Got strange SteamWebAPI response.. " + list.Count);
}
else
{
bool modified = false;
lock (cachelock)
{
modified = Cache.TryAdd(id, list[0]);
}
if (modified) SaveCache();
return list[0].title;
}
}
}
return "" + id;
}
private static ModCache? instance = null;
public static ModCache Instance
{
get
{
if (instance == null)
{
instance = new ModCache();
}
return instance;
}
}
}
}