2022-01-22 11:47:09 +05:00
|
|
|
|
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;
|
2022-01-18 02:47:56 +05:00
|
|
|
|
using System.Net.Http;
|
2021-11-29 03:14:32 +05:00
|
|
|
|
using System.Text;
|
2022-01-22 02:10:15 +05:00
|
|
|
|
using System.Threading.Tasks;
|
2021-11-12 23:12:21 +05:00
|
|
|
|
using System.Windows.Forms;
|
2021-10-29 13:20:27 +05:00
|
|
|
|
|
2022-01-22 11:47:09 +05:00
|
|
|
|
using Gameloop.Vdf.Linq;
|
|
|
|
|
|
2021-10-29 13:20:27 +05:00
|
|
|
|
namespace CreamInstaller
|
|
|
|
|
{
|
2022-01-22 02:10:15 +05:00
|
|
|
|
internal static class SteamCMD
|
2021-10-29 13:20:27 +05:00
|
|
|
|
{
|
2022-01-22 02:10:15 +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";
|
|
|
|
|
|
2022-01-22 11:47:09 +05:00
|
|
|
|
internal static async Task<string> Run(string command) => await Task.Run(() =>
|
2021-10-29 13:20:27 +05:00
|
|
|
|
{
|
2022-01-22 11:47:09 +05:00
|
|
|
|
if (Program.Canceled) return "";
|
2021-11-05 10:42:25 +05:00
|
|
|
|
List<string> logs = new();
|
|
|
|
|
ProcessStartInfo processStartInfo = new()
|
|
|
|
|
{
|
|
|
|
|
FileName = FilePath,
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
RedirectStandardInput = true,
|
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
Arguments = command,
|
2021-11-29 03:14:32 +05:00
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
StandardInputEncoding = Encoding.UTF8,
|
|
|
|
|
StandardOutputEncoding = Encoding.UTF8,
|
|
|
|
|
StandardErrorEncoding = Encoding.UTF8
|
2021-11-05 10:42:25 +05:00
|
|
|
|
};
|
2022-01-24 10:53:15 +05:00
|
|
|
|
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();
|
2022-01-22 02:10:15 +05:00
|
|
|
|
return string.Join("\r\n", logs);
|
2022-01-22 11:47:09 +05:00
|
|
|
|
});
|
2021-10-29 13:20:27 +05:00
|
|
|
|
|
2022-01-22 02:10:15 +05:00
|
|
|
|
internal static async Task Setup()
|
2021-10-29 13:20:27 +05:00
|
|
|
|
{
|
2022-01-22 02:10:15 +05:00
|
|
|
|
await Kill();
|
2021-10-29 13:20:27 +05:00
|
|
|
|
if (!File.Exists(FilePath))
|
|
|
|
|
{
|
2022-01-18 02:47:56 +05:00
|
|
|
|
using (HttpClient httpClient = new())
|
2021-12-26 06:46:24 +05:00
|
|
|
|
{
|
2022-01-22 02:10:15 +05:00
|
|
|
|
byte[] file = await httpClient.GetByteArrayAsync("https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip");
|
2022-01-18 02:47:56 +05:00
|
|
|
|
file.Write(ArchivePath);
|
2021-12-26 06:46:24 +05:00
|
|
|
|
}
|
2021-10-29 13:20:27 +05:00
|
|
|
|
ZipFile.ExtractToDirectory(ArchivePath, DirectoryPath);
|
|
|
|
|
File.Delete(ArchivePath);
|
|
|
|
|
}
|
2022-01-22 11:47:09 +05:00
|
|
|
|
if (File.Exists(AppCacheAppInfoPath)) File.Delete(AppCacheAppInfoPath);
|
2021-11-29 03:14:32 +05:00
|
|
|
|
if (!File.Exists(AppInfoVersionPath) || !Version.TryParse(File.ReadAllText(AppInfoVersionPath, Encoding.UTF8), out Version version) || version < MinimumAppInfoVersion)
|
2021-11-12 23:12:21 +05:00
|
|
|
|
{
|
2022-01-22 11:47:09 +05:00
|
|
|
|
if (Directory.Exists(AppInfoPath)) Directory.Delete(AppInfoPath, true);
|
2021-11-12 23:12:21 +05:00
|
|
|
|
Directory.CreateDirectory(AppInfoPath);
|
2021-11-29 03:14:32 +05:00
|
|
|
|
File.WriteAllText(AppInfoVersionPath, Application.ProductVersion, Encoding.UTF8);
|
2021-11-12 23:12:21 +05:00
|
|
|
|
}
|
2022-01-22 11:47:09 +05:00
|
|
|
|
if (!File.Exists(DllPath)) await Run($@"+quit");
|
2021-10-29 13:20:27 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-22 11:47:09 +05:00
|
|
|
|
internal static async Task<VProperty> GetAppInfo(int appId, string branch = "public", int buildId = 0)
|
2021-10-29 13:20:27 +05:00
|
|
|
|
{
|
2022-01-22 11:47:09 +05:00
|
|
|
|
if (Program.Canceled) return null;
|
2021-11-06 07:37:54 +05:00
|
|
|
|
string output;
|
2021-11-11 12:29:59 +05:00
|
|
|
|
string appUpdatePath = $@"{AppInfoPath}\{appId}";
|
2021-11-05 10:42:25 +05:00
|
|
|
|
string appUpdateFile = $@"{appUpdatePath}\appinfo.txt";
|
2021-11-12 23:12:21 +05:00
|
|
|
|
restart:
|
2022-01-22 11:47:09 +05:00
|
|
|
|
if (Program.Canceled) return null;
|
2021-12-26 06:46:24 +05:00
|
|
|
|
if (Directory.Exists(appUpdatePath) && File.Exists(appUpdateFile))
|
|
|
|
|
output = File.ReadAllText(appUpdateFile, Encoding.UTF8);
|
2021-11-06 13:55:37 +05:00
|
|
|
|
else
|
|
|
|
|
{
|
2022-01-24 10:53:15 +05:00
|
|
|
|
output = await Run($@"+@ShutdownOnFailedCommand 0 +login anonymous +app_info_print {appId} +force_install_dir {appUpdatePath} +app_update 4 +quit");
|
2021-11-06 13:55:37 +05:00
|
|
|
|
int openBracket = output.IndexOf("{");
|
|
|
|
|
int closeBracket = output.LastIndexOf("}");
|
2021-11-06 14:25:25 +05:00
|
|
|
|
if (openBracket != -1 && closeBracket != -1)
|
|
|
|
|
{
|
2021-11-29 03:14:32 +05:00
|
|
|
|
output = $"\"{appId}\"\n" + output[openBracket..(1 + closeBracket)];
|
|
|
|
|
File.WriteAllText(appUpdateFile, output, Encoding.UTF8);
|
2021-11-06 14:25:25 +05:00
|
|
|
|
}
|
2021-11-06 13:55:37 +05:00
|
|
|
|
}
|
2022-01-22 11:47:09 +05:00
|
|
|
|
if (Program.Canceled || output is null) return null;
|
2022-01-24 12:36:25 +05:00
|
|
|
|
if (!ValveDataFile.TryDeserialize(output, out VProperty appInfo))
|
2021-11-11 12:29:59 +05:00
|
|
|
|
{
|
2021-11-12 23:12:21 +05:00
|
|
|
|
if (Directory.Exists(appUpdatePath))
|
2021-11-11 12:29:59 +05:00
|
|
|
|
{
|
2021-11-12 23:12:21 +05:00
|
|
|
|
Directory.Delete(appUpdatePath, true);
|
2021-11-11 12:29:59 +05:00
|
|
|
|
goto restart;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-22 11:47:09 +05:00
|
|
|
|
if (appInfo.Value is VValue) goto restart;
|
2022-01-24 12:36:25 +05:00
|
|
|
|
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
|
|
|
|
{
|
2022-01-24 12:36:25 +05:00
|
|
|
|
string buildid = appInfo.Value?.TryGet("depots")?.TryGet("branches")?.TryGet(branch)?.TryGet("buildid")?.ToString();
|
2022-01-22 11:47:09 +05:00
|
|
|
|
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
|
|
|
|
{
|
2022-01-22 11:47:09 +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}";
|
2022-01-22 11:47:09 +05:00
|
|
|
|
if (Directory.Exists(dlcAppUpdatePath)) Directory.Delete(dlcAppUpdatePath, true);
|
2021-11-12 23:12:21 +05:00
|
|
|
|
}
|
2022-01-22 11:47:09 +05:00
|
|
|
|
if (Directory.Exists(appUpdatePath)) Directory.Delete(appUpdatePath, true);
|
2021-11-11 12:29:59 +05:00
|
|
|
|
goto restart;
|
2021-10-29 13:20:27 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-22 11:47:09 +05:00
|
|
|
|
return appInfo;
|
2021-10-29 13:20:27 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-22 11:47:09 +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();
|
2022-01-24 10:53:15 +05:00
|
|
|
|
if (Program.Canceled || appInfo is not VProperty) return dlcIds;
|
2022-01-24 12:36:25 +05:00
|
|
|
|
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;
|
2022-01-22 11:47:09 +05:00
|
|
|
|
});
|
2021-11-12 23:12:21 +05:00
|
|
|
|
|
2022-01-22 02:10:15 +05:00
|
|
|
|
internal static async Task Kill()
|
2021-10-29 13:20:27 +05:00
|
|
|
|
{
|
2022-01-22 02:10:15 +05:00
|
|
|
|
List<Task> tasks = new();
|
2021-12-26 06:46:24 +05:00
|
|
|
|
foreach (Process process in Process.GetProcessesByName("steamcmd"))
|
|
|
|
|
{
|
|
|
|
|
process.Kill();
|
2022-01-22 02:10:15 +05:00
|
|
|
|
tasks.Add(Task.Run(() => process.WaitForExit()));
|
|
|
|
|
}
|
2022-01-22 11:47:09 +05:00
|
|
|
|
foreach (Task task in tasks) await task;
|
2021-10-29 13:20:27 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-22 02:10:15 +05:00
|
|
|
|
internal static void Dispose()
|
2021-10-29 13:20:27 +05:00
|
|
|
|
{
|
2022-01-22 02:10:15 +05:00
|
|
|
|
Kill().Wait();
|
2021-12-26 06:46:24 +05:00
|
|
|
|
if (Directory.Exists(DirectoryPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(DirectoryPath, true);
|
|
|
|
|
}
|
2021-10-29 13:20:27 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|