-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSteam.cs
215 lines (182 loc) · 7.13 KB
/
Steam.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SteamKit2;
using Updater.Consumers;
using static SteamKit2.SteamApps;
using Exception = System.Exception;
namespace Updater
{
public static class Steam
{
public const String LastChangeFile = "last-changenumber.txt";
public static Boolean quitOnDisconnect;
public static UInt32 previousChangeNumber;
public static Boolean isLoggedOn;
public static SteamClient steamClient;
public static CallbackManager manager;
public static System.Timers.Timer timer1;
public static System.Timers.Timer timer2;
public static SteamUser steamUser;
public static SteamApps steamApps;
public static SteamFriends steamFriends;
private static readonly HttpClient httpClient = new HttpClient();
public static void startSteam(Boolean checkChanges, Boolean debug)
{
// Debug
DebugLog.AddListener(new DebugListener());
DebugLog.Enabled = debug;
// Setup client
steamClient = new SteamClient();
manager = new CallbackManager(steamClient);
steamUser = steamClient.GetHandler<SteamUser>();
steamApps = steamClient.GetHandler<SteamApps>();
steamFriends = steamClient.GetHandler<SteamFriends>();
// Callbacks
manager.Subscribe<SteamClient.ConnectedCallback>(OnConnected);
manager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected);
manager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
manager.Subscribe<SteamUser.LoggedOffCallback>(OnLoggedOff);
steamClient.Connect();
timer1 = new System.Timers.Timer();
timer1.Elapsed += RunWaitCallbacks;
timer1.Interval = TimeSpan.FromSeconds(Config.isLocal() ? 1 : 10).TotalMilliseconds;
timer1.Start();
if (checkChanges)
{
timer2 = new System.Timers.Timer();
timer2.Elapsed += CheckForChanges;
timer2.Interval = TimeSpan.FromSeconds(Config.isLocal() ? 5 : 60).TotalMilliseconds;
timer2.Start();
}
}
private static void RunWaitCallbacks(Object obj, EventArgs args)
{
timer1.Stop();
manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
timer1.Start();
}
private static async void CheckForChanges(Object obj, EventArgs args)
{
timer2.Stop();
try
{
// Check logged in
if (steamClient.IsConnected && isLoggedOn)
{
// Get the last change ID
if (previousChangeNumber == 0 && File.Exists(LastChangeFile))
{
var contents = File.ReadAllText(LastChangeFile);
previousChangeNumber = contents == "" ? 0 : UInt32.Parse(contents);
}
// Get latest changes. If more than 5000, returns 0
var JobID = steamApps.PICSGetChangesSince(previousChangeNumber, true, true);
var callback = await JobID;
if (previousChangeNumber < callback.CurrentChangeNumber)
{
Log.Info(
$"{callback.CurrentChangeNumber - callback.LastChangeNumber:N0} changes, {callback.AppChanges.Count} apps, {callback.PackageChanges.Count} packages"
);
// Save apps
foreach (var appID in callback.AppChanges.Keys)
{
var appPayload = AppMessage.create(appID);
AbstractConsumer.Produce(AbstractConsumer.queue_cs_apps, appPayload);
}
// Save packages
foreach (var packageID in callback.PackageChanges.Keys)
{
var packagePayload = PackageMessage.create(packageID);
AbstractConsumer.Produce(AbstractConsumer.queue_cs_packages, packagePayload);
}
// Save changes
var changePayload = ChangeMessage.create(callback.CurrentChangeNumber, callback);
AbstractConsumer.Produce(AbstractConsumer.queue_go_changes, changePayload);
// Update change number
previousChangeNumber = callback.CurrentChangeNumber;
File.WriteAllText(LastChangeFile, previousChangeNumber.ToString());
}
}
}
catch (Exception ex)
{
Log.Error("Failed checking for changes - " + ex.Message);
}
finally
{
timer2.Start();
}
}
private static void OnConnected(SteamClient.ConnectedCallback callback)
{
Log.Info("Connected to Steam");
steamUser.LogOn(new SteamUser.LogOnDetails
{
Username = Config.steamUsername,
Password = Config.steamPassword
});
}
private static void OnDisconnected(SteamClient.DisconnectedCallback callback)
{
Log.Info("Disconnected from Steam");
isLoggedOn = false;
if (quitOnDisconnect)
{
Environment.Exit(0);
}
// Try to reconnect
Thread.Sleep(TimeSpan.FromSeconds(10));
steamClient.Connect();
}
private static void OnLoggedOn(SteamUser.LoggedOnCallback callback)
{
if (callback.Result != EResult.OK)
{
Log.Error($"Unable to logon to Steam: {callback.Result} / {callback.ExtendedResult}");
return;
}
Log.Info("Logged in");
isLoggedOn = true;
}
private static void OnLoggedOff(SteamUser.LoggedOffCallback callback)
{
Log.Info("Logged off");
isLoggedOn = false;
if (quitOnDisconnect)
{
steamClient.Disconnect();
}
}
}
internal class DebugListener : IDebugListener
{
public void WriteLine(String category, String msg)
{
Log.Debug($"Debug - {category}: {msg}");
}
}
public class ChangeMessage
{
[JsonProperty(PropertyName = "id")]
public UInt32 ID;
// ReSharper disable once NotAccessedField.Global
public PICSChangesCallback PICSChanges;
public static BaseMessage create(UInt32 id, PICSChangesCallback pics = null)
{
return new BaseMessage
{
Message = new ChangeMessage
{
ID = id,
PICSChanges = pics
}
};
}
}
}