CreamInstaller/CreamInstaller/Classes/SteamCMD.cs

167 lines
7.6 KiB
C#
Raw Normal View History

using System;
2021-10-29 13:20:27 +05:00
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
2021-11-06 14:25:25 +05:00
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
2021-11-12 23:12:21 +05:00
using System.Windows.Forms;
2021-10-29 13:20:27 +05:00
using Gameloop.Vdf.Linq;
2021-10-29 13:20:27 +05:00
namespace CreamInstaller
{
internal static class SteamCMD
2021-10-29 13:20:27 +05:00
{
internal static readonly string DirectoryPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\CreamInstaller";
internal static readonly string FilePath = DirectoryPath + @"\steamcmd.exe";
internal static readonly string ArchivePath = DirectoryPath + @"\steamcmd.zip";
internal static readonly string DllPath = DirectoryPath + @"\steamclient.dll";
internal static readonly string AppCachePath = DirectoryPath + @"\appcache";
internal static readonly string AppCacheAppInfoPath = AppCachePath + @"\appinfo.vdf";
internal static readonly string AppInfoPath = DirectoryPath + @"\appinfo";
internal static readonly Version MinimumAppInfoVersion = Version.Parse("2.0.3.2");
internal static readonly string AppInfoVersionPath = AppInfoPath + @"\version.txt";
internal static async Task<string> Run(string command) => await Task.Run(() =>
2021-10-29 13:20:27 +05:00
{
if (Program.Canceled) return "";
List<string> logs = new();
ProcessStartInfo processStartInfo = new()
{
FileName = FilePath,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
UseShellExecute = false,
Arguments = command,
CreateNoWindow = true,
StandardInputEncoding = Encoding.UTF8,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8
};
using Process process = Process.Start(processStartInfo);
process.OutputDataReceived += (object sender, DataReceivedEventArgs e) => logs.Add(e.Data);
process.BeginOutputReadLine();
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => logs.Add(e.Data);
process.BeginErrorReadLine();
process.WaitForExit();
return string.Join("\r\n", logs);
});
2021-10-29 13:20:27 +05:00
internal static async Task Setup()
2021-10-29 13:20:27 +05:00
{
await Kill();
2021-10-29 13:20:27 +05:00
if (!File.Exists(FilePath))
{
using (HttpClient httpClient = new())
{
byte[] file = await httpClient.GetByteArrayAsync("https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip");
file.Write(ArchivePath);
}
2021-10-29 13:20:27 +05:00
ZipFile.ExtractToDirectory(ArchivePath, DirectoryPath);
File.Delete(ArchivePath);
}
if (File.Exists(AppCacheAppInfoPath)) File.Delete(AppCacheAppInfoPath);
if (!File.Exists(AppInfoVersionPath) || !Version.TryParse(File.ReadAllText(AppInfoVersionPath, Encoding.UTF8), out Version version) || version < MinimumAppInfoVersion)
2021-11-12 23:12:21 +05:00
{
if (Directory.Exists(AppInfoPath)) Directory.Delete(AppInfoPath, true);
2021-11-12 23:12:21 +05:00
Directory.CreateDirectory(AppInfoPath);
File.WriteAllText(AppInfoVersionPath, Application.ProductVersion, Encoding.UTF8);
2021-11-12 23:12:21 +05:00
}
if (!File.Exists(DllPath)) await Run($@"+quit");
2021-10-29 13:20:27 +05:00
}
internal static async Task<VProperty> GetAppInfo(int appId, string branch = "public", int buildId = 0)
2021-10-29 13:20:27 +05:00
{
if (Program.Canceled) return null;
2021-11-06 07:37:54 +05:00
string output;
string appUpdatePath = $@"{AppInfoPath}\{appId}";
string appUpdateFile = $@"{appUpdatePath}\appinfo.txt";
2021-11-12 23:12:21 +05:00
restart:
if (Program.Canceled) return null;
if (Directory.Exists(appUpdatePath) && File.Exists(appUpdateFile))
output = File.ReadAllText(appUpdateFile, Encoding.UTF8);
else
{
output = await Run($@"+@ShutdownOnFailedCommand 0 +login anonymous +app_info_print {appId} +force_install_dir {appUpdatePath} +app_update 4 +quit");
int openBracket = output.IndexOf("{");
int closeBracket = output.LastIndexOf("}");
2021-11-06 14:25:25 +05:00
if (openBracket != -1 && closeBracket != -1)
{
output = $"\"{appId}\"\n" + output[openBracket..(1 + closeBracket)];
File.WriteAllText(appUpdateFile, output, Encoding.UTF8);
2021-11-06 14:25:25 +05:00
}
}
if (Program.Canceled || output is null) return null;
if (!ValveDataFile.TryDeserialize(output, out VProperty appInfo))
{
2021-11-12 23:12:21 +05:00
if (Directory.Exists(appUpdatePath))
{
2021-11-12 23:12:21 +05:00
Directory.Delete(appUpdatePath, true);
goto restart;
}
}
if (appInfo.Value is VValue) goto restart;
if (appInfo is null || appInfo.Value?.Children()?.ToList()?.Count == 0) return appInfo;
VToken type = appInfo.Value?.TryGet("common")?.TryGet("type");
2021-11-06 14:25:25 +05:00
if (type is null || type.ToString() == "Game")
2021-10-29 13:20:27 +05:00
{
string buildid = appInfo.Value?.TryGet("depots")?.TryGet("branches")?.TryGet(branch)?.TryGet("buildid")?.ToString();
if (buildid is null && type is not null) return appInfo;
2021-11-06 15:28:53 +05:00
if (type is null || int.Parse(buildid) < buildId)
2021-10-29 13:20:27 +05:00
{
List<int> dlcAppIds = await ParseDlcAppIds(appInfo);
foreach (int id in dlcAppIds)
2021-11-12 23:12:21 +05:00
{
string dlcAppUpdatePath = $@"{AppInfoPath}\{id}";
if (Directory.Exists(dlcAppUpdatePath)) Directory.Delete(dlcAppUpdatePath, true);
2021-11-12 23:12:21 +05:00
}
if (Directory.Exists(appUpdatePath)) Directory.Delete(appUpdatePath, true);
goto restart;
2021-10-29 13:20:27 +05:00
}
}
return appInfo;
2021-10-29 13:20:27 +05:00
}
internal static async Task<List<int>> ParseDlcAppIds(VProperty appInfo) => await Task.Run(() =>
2021-11-12 23:12:21 +05:00
{
List<int> dlcIds = new();
if (Program.Canceled || appInfo is not VProperty) return dlcIds;
VToken extended = appInfo.Value.TryGet("extended");
if (extended is not null) foreach (VProperty property in extended)
if (property.Key.ToString() == "listofdlc") foreach (string id in property.Value.ToString().Split(","))
if (!dlcIds.Contains(int.Parse(id))) dlcIds.Add(int.Parse(id));
VToken depots = appInfo.Value.TryGet("depots");
if (depots is not null) foreach (VProperty property in depots)
if (int.TryParse(property.Key.ToString(), out int _)
&& int.TryParse(property.Value.TryGet("dlcappid")?.ToString(), out int appid)
&& !dlcIds.Contains(appid))
dlcIds.Add(appid);
2021-11-12 23:12:21 +05:00
return dlcIds;
});
2021-11-12 23:12:21 +05:00
internal static async Task Kill()
2021-10-29 13:20:27 +05:00
{
List<Task> tasks = new();
foreach (Process process in Process.GetProcessesByName("steamcmd"))
{
process.Kill();
tasks.Add(Task.Run(() => process.WaitForExit()));
}
foreach (Task task in tasks) await task;
2021-10-29 13:20:27 +05:00
}
internal static void Dispose()
2021-10-29 13:20:27 +05:00
{
Kill().Wait();
if (Directory.Exists(DirectoryPath))
{
Directory.Delete(DirectoryPath, true);
}
2021-10-29 13:20:27 +05:00
}
}
}