-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
437 lines (412 loc) · 15.6 KB
/
Program.cs
File metadata and controls
437 lines (412 loc) · 15.6 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
namespace SteamPatcher
{
class Program
{
static readonly byte[] Data_steam_api = ExtractResource("SteamPatcher.steam_api.dll.gz");
static readonly byte[] Data_steam_api64 = ExtractResource("SteamPatcher.steam_api64.dll.gz");
static int Main(string[] args)
{
var path = Directory.GetCurrentDirectory();
var steamAppsOnly = true;
var showHelp = true;
var mode = ' ';
if (args != null)
{
var options = "";
foreach (var item in args)
{
if (item.Length == 0) { continue; }
if (item[0] == '-') { options += item.Substring(1); }
else { path = item; }
showHelp = false;
}
foreach (char c in options)
{
if (c == 'a') { steamAppsOnly = false; }
if (c == 'l' || c == 'p' || c == 'u' || c == 'i')
{
if (mode == ' ') { mode = c; }
else
{
Console.WriteLine("ERROR: Mode -{0} already specified.", mode);
Console.WriteLine();
showHelp = true;
}
}
}
}
if (showHelp)
{
Console.WriteLine("steampatcher [search_dir] [-a] <-l | -p | -u | -i>");
Console.WriteLine(" search_dir: Root directory to search for Steam games (default: current directory)");
Console.WriteLine(" -a: Search in any directory (default: only \"steamapps\" directories)");
Console.WriteLine(" -l: List Steam games found");
Console.WriteLine(" -p: Patch Steam games found");
Console.WriteLine(" -u: Unpatch Steam games found");
Console.WriteLine(" -i: Interactive mode (ask action for each Steam game found; default mode)");
return -1;
}
if (!Directory.Exists(path))
{
Console.WriteLine("ERROR: Cannot find path \"{0}\"", path);
return 1;
}
if (mode == ' ') { mode = 'i'; }
if (mode == 'l')
{
Console.WriteLine("Listing Steam games ({0})...", steamAppsOnly ? "steamapps directories only" : "any directory");
var games = FindSteamGames(path, !steamAppsOnly);
Console.WriteLine("Found {0} games.", games.Length);
if (games.Length > 0)
{
Console.WriteLine("---");
foreach (var game in games)
{
Console.WriteLine("({0}) {1} at {2} [PATCHED:{3}]",
game.AppId ?? "???",
game.InstallPath.Name,
game.Path.FullName,
game.IsPatched ? "YES" : "NO");
}
Console.WriteLine("---");
}
}
else if (mode == 'p')
{
Console.WriteLine("Mode: Patch All");
if (!ProcessPath(path, steamAppsOnly, "p")) { return 2; }
}
else if (mode == 'u')
{
Console.WriteLine("Mode: Unpatch All");
if (!ProcessPath(path, steamAppsOnly, "u")) { return 2; }
}
else if (mode == 'i')
{
Console.WriteLine("Mode: Interactive");
if (!ProcessPath(path, steamAppsOnly)) { return 2; }
}
return 0;
}
static bool ProcessPath(string path, bool steamAppsOnly, string forceAction = null)
{
int errors = 0, patched = 0, unpatched = 0;
Console.WriteLine("Searching Steam games ({0})...", steamAppsOnly ? "steamapps directories only" : "any directory");
var games = FindSteamGames(path, !steamAppsOnly);
Console.WriteLine("Found {0} games.", games.Length);
if (games.Length > 0)
{
Console.WriteLine("---");
foreach (var game in games)
{
Console.WriteLine("({0}) {1} at {2} [PATCHED:{3}]",
game.AppId ?? "unk",
game.InstallPath.Name,
game.Path.FullName,
game.IsPatched ? "YES" : "NO");
var action = forceAction;
if (action == null)
{
Console.Write("Specify action (p = Patch, u = Unpatch, anything else = Skip): ");
action = Console.ReadLine().Trim().ToLower();
}
if (action == "p")
{
if (!game.IsPatched)
{
if (string.IsNullOrWhiteSpace(game.AppId))
{
Console.WriteLine("ERROR: Cannot find appid - please create steam_appid.txt with the proper id.");
errors++;
}
else
{
Console.WriteLine("Patching...");
try
{
PatchSteamGame(game.Path.FullName, game.AppId);
Console.WriteLine("Patched successfully.");
patched++;
}
catch (Exception error)
{
Console.WriteLine("ERROR: {0}", error.Message);
errors++;
}
}
}
else
{
Console.WriteLine("Already patched.");
}
}
else if (action == "u")
{
if (game.IsPatched)
{
Console.WriteLine("Unpatching...");
try
{
UnpatchSteamGame(game.Path.FullName);
Console.WriteLine("Unpatched successfully.");
unpatched++;
}
catch (Exception error)
{
Console.WriteLine("ERROR: {0}", error.Message);
errors++;
}
}
else
{
Console.WriteLine("Not patched.");
}
}
else
{
Console.WriteLine("Skipping.");
}
}
Console.WriteLine("---");
}
Console.WriteLine("Total patched: {0}", patched);
Console.WriteLine("Total unpatched: {0}", unpatched);
Console.WriteLine("Total errors: {0}", errors);
if (errors > 0) { Console.WriteLine("ERROR: One or more errors encountered."); }
return errors == 0;
}
class SteamGameEntry
{
public DirectoryInfo Path { get; set; }
public DirectoryInfo InstallPath { get; set; }
public string AppId { get; set; }
public bool IsPatched { get; set; }
}
static SteamGameEntry[] FindSteamGames(string path, bool include)
{
var result = new List<SteamGameEntry>();
var dirInfo = new DirectoryInfo(path);
if (!include)
{
try
{
var find = dirInfo;
do
{
if (find.Name == "steamapps") { include = true; break; }
find = find.Parent;
}
while (find != null);
}
catch { }
}
FindSteamGames(dirInfo, result, include);
return result.ToArray();
}
static void FindSteamGames(DirectoryInfo path, List<SteamGameEntry> result, bool include)
{
try
{
if (path.Name == "steamapps") { include = true; }
if (include)
{
var fullPath = path.FullName;
if (IsSteamGame(fullPath))
{
result.Add(new SteamGameEntry()
{
Path = path,
InstallPath = GetInstallPath(path),
AppId = GetSteamAppId(path),
IsPatched = IsPatched(path.FullName)
});
return;
}
}
foreach (var sub in path.GetDirectories())
{
FindSteamGames(sub, result, include);
}
}
catch { }
}
static DirectoryInfo GetInstallPath(DirectoryInfo path)
{
try
{
var find = path;
do
{
var up = find.Parent;
if (up == null) { return path; }
if (up.Name == "common") { return find; }
find = up;
}
while (true);
}
catch { return path; }
}
static string GetSteamAppId(DirectoryInfo path)
{
string result = null;
try
{
var appIdPath = Path.Combine(path.FullName, "steam_appid.txt");
if (File.Exists(appIdPath)) { result = File.ReadAllText(appIdPath); }
if (string.IsNullOrWhiteSpace(result)) { result = null; }
if (result == null)
{
appIdPath = Path.Combine(path.FullName, "steam_settings", "steam_appid.txt");
if (File.Exists(appIdPath)) { result = File.ReadAllText(appIdPath); }
if (string.IsNullOrWhiteSpace(result)) { result = null; }
}
if (result == null)
{
var installPath = GetInstallPath(path);
var findStr = '"' + installPath.Name + '"';
foreach (var file in installPath.Parent.Parent.GetFiles())
{
var fileName = file.Name.ToLower();
if (!fileName.StartsWith("appmanifest_")) { continue; }
if (!fileName.EndsWith(".acf")) { continue; }
var fileStr = File.ReadAllText(file.FullName);
if (fileStr.Contains(findStr)) { return fileName.Substring(12, fileName.Length - 12 - 4); }
}
}
}
catch { }
return result;
}
static void PatchSteamGame(string path, string appId)
{
if (!IsSteamGame(path)) { return; }
var appIdPath = Path.Combine(path, "steam_appid.txt");
if (!File.Exists(appIdPath)) { File.WriteAllText(appIdPath, appId); }
var settingsPath = Path.Combine(path, "steam_settings");
if (!Directory.Exists(settingsPath))
{
Directory.CreateDirectory(settingsPath);
File.WriteAllText(Path.Combine(settingsPath, "steam_appid.txt"), appId);
File.WriteAllText(Path.Combine(settingsPath, "offline.txt"), "1");
File.WriteAllText(Path.Combine(settingsPath, "disable_overlay.txt"), "1");
File.WriteAllText(Path.Combine(settingsPath, "disable_networking.txt"), "1");
}
var patchData = Data_steam_api;
var dllPath = Path.Combine(path, "steam_api.dll");
if (!File.Exists(dllPath))
{
patchData = Data_steam_api64;
dllPath = Path.Combine(path, "steam_api64.dll");
if (!File.Exists(dllPath)) { return; }
}
var existingData = File.ReadAllBytes(dllPath);
if (!ArraysEqual(existingData, patchData))
{
var interfacesPath = Path.Combine(path, "steam_interfaces.txt");
if (!File.Exists(interfacesPath))
{
var dllStr = Encoding.ASCII.GetString(existingData);
var result = new StringBuilder();
foreach (var i in interfaces) { AddInterfaceMatch(result, dllStr, i + "\\d{3}"); }
if (!AddInterfaceMatch(result, dllStr, "STEAMCONTROLLER_INTERFACE_VERSION\\d{3}"))
{
AddInterfaceMatch(result, dllStr, "STEAMCONTROLLER_INTERFACE_VERSION");
}
File.WriteAllText(interfacesPath, result.ToString());
}
File.Copy(dllPath, dllPath + ".old", true);
File.WriteAllBytes(dllPath, patchData);
}
}
static void UnpatchSteamGame(string path)
{
var dllPath = Path.Combine(path, "steam_api.dll");
if (!File.Exists(dllPath + ".old"))
{
dllPath = Path.Combine(path, "steam_api64.dll");
if (!File.Exists(dllPath + ".old")) { return; }
}
File.Move(dllPath + ".old", dllPath, true);
}
static bool IsSteamGame(string path)
{
return File.Exists(Path.Combine(path, "steam_api.dll")) ||
File.Exists(Path.Combine(path, "steam_api64.dll"));
}
static bool IsPatched(string path)
{
try
{
var data = Data_steam_api;
var dll = Path.Combine(path, "steam_api.dll");
if (!File.Exists(dll))
{
data = Data_steam_api64;
dll = Path.Combine(path, "steam_api64.dll");
if (!File.Exists(dll)) { return false; }
}
return ArraysEqual(File.ReadAllBytes(dll), data);
}
catch
{
return false;
}
}
static bool ArraysEqual(byte[] a, byte[] b)
{
if (a.Length != b.Length) { return false; }
for (int i = 0, j = a.Length; i < j; i++) { if (a[i] != b[i]) { return false; } }
return true;
}
static readonly string[] interfaces =
{
"SteamClient",
"SteamGameServer",
"SteamGameServerStats",
"SteamUser",
"SteamFriends",
"SteamUtils",
"SteamMatchMaking",
"SteamMatchMakingServers",
"STEAMUSERSTATS_INTERFACE_VERSION",
"STEAMAPPS_INTERFACE_VERSION",
"SteamNetworking",
"STEAMREMOTESTORAGE_INTERFACE_VERSION",
"STEAMSCREENSHOTS_INTERFACE_VERSION",
"STEAMHTTP_INTERFACE_VERSION",
"STEAMUNIFIEDMESSAGES_INTERFACE_VERSION",
"STEAMUGC_INTERFACE_VERSION",
"STEAMAPPLIST_INTERFACE_VERSION",
"STEAMMUSIC_INTERFACE_VERSION",
"STEAMMUSICREMOTE_INTERFACE_VERSION",
"STEAMHTMLSURFACE_INTERFACE_VERSION_",
"STEAMINVENTORY_INTERFACE_V",
"SteamController",
"SteamMasterServerUpdater",
"STEAMVIDEO_INTERFACE_V"
};
static bool AddInterfaceMatch(StringBuilder result, string str, string regex)
{
var hasMatch = false;
foreach (Match match in new Regex(regex).Matches(str))
{
result.AppendLine(match.Value);
hasMatch = true;
}
return hasMatch;
}
static byte[] ExtractResource(string name)
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
using (var decompress = new GZipStream(stream, CompressionMode.Decompress))
using (var output = new MemoryStream()) { decompress.CopyTo(output); return output.ToArray(); }
}
}
}