This repository has been archived by the owner on Mar 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkshopDownloader.cs
177 lines (150 loc) · 5.89 KB
/
WorkshopDownloader.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Steamworks;
// ReSharper disable UnusedMember.Global
namespace SteamWorkshopEmu;
public static class WorkshopDownloader
{
public static async Task<(bool, uint)> InstallAsync(PublishedFileId_t fileId, string targetPath, int timeoutSec = 30)
{
var zip = targetPath + ".zip";
var res = await DownloadAsync(fileId, zip, timeoutSec);
if (!res.Item1)
return res;
try
{
ZipFile.ExtractToDirectory(zip, targetPath);
}
catch (Exception e)
{
Console.WriteLine(e);
return (false, 0);
}
finally
{
try
{
File.Delete(zip);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
return (true, res.Item2);
}
public static async Task<(bool, uint)> DownloadAsync(PublishedFileId_t fileId, string zipPath, int timeoutSec = 30)
{
var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(timeoutSec);
var requestInfo = new RequestWorkshopInfo
{
PublishedFileId = fileId.m_PublishedFileId,
DownloadFormat = "raw"
};
var jsonRequest = JsonSerializer.Serialize(requestInfo);
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
var result = await httpClient.PostAsync("https://node04.steamworkshopdownloader.io/prod/api/download/request", content);
var resultStr = await result.Content.ReadAsStringAsync();
if (JsonSerializer.Deserialize(resultStr, typeof(RequestWorkshopResponse)) is not RequestWorkshopResponse jsonResult)
return (false, 0);
var lastUpdate = DateTime.Now;
var lastState = string.Empty;
var lastProgress = -1;
while (true)
{
await Task.Delay(1000);
try
{
var request = new StatusWorkshopRequest();
request.Uuids.Add(jsonResult.Uuid);
var jsonRequestStatus = JsonSerializer.Serialize(request);
var statusResponse = await httpClient.PostAsync("https://node04.steamworkshopdownloader.io/prod/api/download/status", new StringContent(jsonRequestStatus, Encoding.UTF8, "application/json"));
var statusText = await statusResponse.Content.ReadAsStringAsync();
var statusWorkshop = JsonSerializer.Deserialize(statusText, typeof(Dictionary<string, StatusWorkshopResponse>)) as Dictionary<string, StatusWorkshopResponse>;
if (statusWorkshop == null)
continue;
var gotResult = false;
var appId = 0u;
foreach (var statusWork in statusWorkshop)
{
var key = statusWork.Key;
var value = statusWork.Value;
if (lastState != value.Status || lastProgress != value.Progress)
{
lastUpdate = DateTime.Now;
lastState = value.Status;
lastProgress = value.Progress;
}
if (value.Status != "prepared" || value.Progress < 100) continue;
var fileResponse = await httpClient.GetAsync($"https://{value.StorageNode}/prod//storage/{value.StoragePath}?uuid={key}");
if (fileResponse == null)
continue;
using Stream contentStream = await fileResponse.Content.ReadAsStreamAsync(),
stream = new FileStream(zipPath, FileMode.Create, FileAccess.Write, FileShare.None, 1024, true);
await contentStream.CopyToAsync(stream);
var slash = value.StoragePath.IndexOf('/');
if (slash > 0)
uint.TryParse(value.StoragePath.Substring(0, slash), out appId);
gotResult = true;
}
Console.WriteLine("Status: " + statusText);
if (gotResult)
return (true, appId);
}
catch (Exception e)
{
Console.WriteLine(e);
}
if (DateTime.Now - lastUpdate <= TimeSpan.FromSeconds(timeoutSec)) continue;
Console.WriteLine("Downloading failed for the workshop id: " + fileId.m_PublishedFileId);
return (false, 0);
}
}
}
public class RequestWorkshopInfo
{
public ulong PublishedFileId { get; set; }
public string CollectionId { get; set; }
public bool Hidden { get; set; }
public string DownloadFormat { get; set; }
public bool AutoDownload { get; set; }
}
public class RequestWorkshopResponse
{
[JsonPropertyName("uuid")]
public string Uuid { get; set; }
}
public class StatusWorkshopResponse
{
[JsonPropertyName("age")]
public ulong Age { get; set; }
[JsonPropertyName("bytes_size")]
public ulong ByteSize { get; set; }
[JsonPropertyName("bytes_transmitted")]
public ulong ByteTransmitted { get; set; }
[JsonPropertyName("downloadError")]
public string DownloadError { get; set; }
[JsonPropertyName("progress")]
public int Progress { get; set; }
[JsonPropertyName("progressText")]
public string ProgressText { get; set; }
[JsonPropertyName("status")]
public string Status { get; set; }
[JsonPropertyName("storageNode")]
public string StorageNode { get; set; }
[JsonPropertyName("storagePath")]
public string StoragePath { get; set; }
}
public class StatusWorkshopRequest
{
[JsonPropertyName("uuids")]
public List<string> Uuids { get; set; } = new();
}