- Vastly improved the stability of VDF file parsing
This commit is contained in:
pointfeev 2022-01-24 02:36:25 -05:00
parent ffbbeedeff
commit f1b7e04842
4 changed files with 76 additions and 62 deletions

View file

@ -9,7 +9,6 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using Gameloop.Vdf;
using Gameloop.Vdf.Linq; using Gameloop.Vdf.Linq;
namespace CreamInstaller namespace CreamInstaller
@ -78,7 +77,6 @@ namespace CreamInstaller
internal static async Task<VProperty> GetAppInfo(int appId, string branch = "public", int buildId = 0) internal static async Task<VProperty> GetAppInfo(int appId, string branch = "public", int buildId = 0)
{ {
VProperty appInfo = null;
if (Program.Canceled) return null; if (Program.Canceled) return null;
string output; string output;
string appUpdatePath = $@"{AppInfoPath}\{appId}"; string appUpdatePath = $@"{AppInfoPath}\{appId}";
@ -99,8 +97,7 @@ namespace CreamInstaller
} }
} }
if (Program.Canceled || output is null) return null; if (Program.Canceled || output is null) return null;
try { appInfo = VdfConvert.Deserialize(output); } if (!ValveDataFile.TryDeserialize(output, out VProperty appInfo))
catch
{ {
if (Directory.Exists(appUpdatePath)) if (Directory.Exists(appUpdatePath))
{ {
@ -109,12 +106,11 @@ namespace CreamInstaller
} }
} }
if (appInfo.Value is VValue) goto restart; if (appInfo.Value is VValue) goto restart;
if (appInfo is null || appInfo.Value is not VValue && appInfo.Value.Children().ToList().Count == 0) if (appInfo is null || appInfo.Value?.Children()?.ToList()?.Count == 0) return appInfo;
return appInfo; VToken type = appInfo.Value?.TryGet("common")?.TryGet("type");
VToken type = appInfo.Value is VValue ? null : appInfo.Value?["common"]?["type"];
if (type is null || type.ToString() == "Game") if (type is null || type.ToString() == "Game")
{ {
string buildid = appInfo.Value is VValue ? null : appInfo.Value["depots"]?["branches"]?[branch]?["buildid"]?.ToString(); string buildid = appInfo.Value?.TryGet("depots")?.TryGet("branches")?.TryGet(branch)?.TryGet("buildid")?.ToString();
if (buildid is null && type is not null) return appInfo; if (buildid is null && type is not null) return appInfo;
if (type is null || int.Parse(buildid) < buildId) if (type is null || int.Parse(buildid) < buildId)
{ {
@ -135,17 +131,16 @@ namespace CreamInstaller
{ {
List<int> dlcIds = new(); List<int> dlcIds = new();
if (Program.Canceled || appInfo is not VProperty) return dlcIds; if (Program.Canceled || appInfo is not VProperty) return dlcIds;
if (appInfo.Value["extended"] is not null) VToken extended = appInfo.Value.TryGet("extended");
foreach (VProperty property in appInfo.Value["extended"]) if (extended is not null) foreach (VProperty property in extended)
if (property.Key.ToString() == "listofdlc") if (property.Key.ToString() == "listofdlc") foreach (string id in property.Value.ToString().Split(","))
foreach (string id in property.Value.ToString().Split(",")) if (!dlcIds.Contains(int.Parse(id))) dlcIds.Add(int.Parse(id));
if (!dlcIds.Contains(int.Parse(id))) VToken depots = appInfo.Value.TryGet("depots");
dlcIds.Add(int.Parse(id)); if (depots is not null) foreach (VProperty property in depots)
if (appInfo.Value["depots"] is not null) if (int.TryParse(property.Key.ToString(), out int _)
foreach (VProperty _property in appInfo.Value["depots"]) && int.TryParse(property.Value.TryGet("dlcappid")?.ToString(), out int appid)
if (int.TryParse(_property.Key.ToString(), out int _)) && !dlcIds.Contains(appid))
if (int.TryParse(_property.Value?["dlcappid"]?.ToString(), out int appid) && !dlcIds.Contains(appid)) dlcIds.Add(appid);
dlcIds.Add(appid);
return dlcIds; return dlcIds;
}); });

View file

@ -0,0 +1,29 @@
using Gameloop.Vdf;
using Gameloop.Vdf.Linq;
namespace CreamInstaller;
internal static class ValveDataFile
{
internal static bool TryDeserialize(string value, out VProperty result)
{
result = null;
try
{
result = VdfConvert.Deserialize(value);
return true;
}
catch { }
return false;
}
internal static VToken TryGet(this VToken token, string index)
{
try
{
return token[index];
}
catch { }
return null;
}
}

View file

@ -5,7 +5,7 @@
<UseWindowsForms>true</UseWindowsForms> <UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon>Resources\ini.ico</ApplicationIcon> <ApplicationIcon>Resources\ini.ico</ApplicationIcon>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract> <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
<Version>2.2.4.0</Version> <Version>2.2.4.1</Version>
<PackageIcon>Resources\ini.ico</PackageIcon> <PackageIcon>Resources\ini.ico</PackageIcon>
<PackageIconUrl /> <PackageIconUrl />
<Description>Automatically generates and installs CreamAPI files for Steam games on the user's computer. It can also generate and install CreamAPI for the Paradox Launcher should the user select a Paradox Interactive game.</Description> <Description>Automatically generates and installs CreamAPI files for Steam games on the user's computer. It can also generate and install CreamAPI for the Paradox Launcher should the user select a Paradox Interactive game.</Description>

View file

@ -10,7 +10,6 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using Gameloop.Vdf;
using Gameloop.Vdf.Linq; using Gameloop.Vdf.Linq;
using Microsoft.Win32; using Microsoft.Win32;
@ -38,22 +37,18 @@ namespace CreamInstaller
if (Directory.Exists(libraryFolder)) if (Directory.Exists(libraryFolder))
{ {
gameDirectories.Add(libraryFolder); gameDirectories.Add(libraryFolder);
try string libraryFolders = libraryFolder + @"\libraryfolders.vdf";
if (File.Exists(libraryFolders) && ValveDataFile.TryDeserialize(File.ReadAllText(libraryFolders, Encoding.UTF8), out VProperty _result))
{ {
string libraryFolders = libraryFolder + @"\libraryfolders.vdf"; dynamic result = _result;
if (File.Exists(libraryFolders)) foreach (dynamic property in result?.Value) if (int.TryParse(property.Key, out int _))
{ {
dynamic property = VdfConvert.Deserialize(File.ReadAllText(libraryFolders, Encoding.UTF8)).Value; string path = property.Value?.path?.ToString();
foreach (dynamic _property in property) if (string.IsNullOrWhiteSpace(path)) continue;
if (int.TryParse(_property.Key, out int _)) path += @"\steamapps";
{ if (Directory.Exists(path) && !gameDirectories.Contains(path)) gameDirectories.Add(path);
string path = _property.Value.path.ToString() + @"\steamapps"; }
if (string.IsNullOrWhiteSpace(path) || !Directory.Exists(path)) continue;
if (!gameDirectories.Contains(path)) gameDirectories.Add(path);
}
}
} }
catch { }
} }
} }
return gameDirectories; return gameDirectories;
@ -89,28 +84,24 @@ namespace CreamInstaller
foreach (string file in files) foreach (string file in files)
{ {
if (Program.Canceled) return null; if (Program.Canceled) return null;
if (Path.GetExtension(file) == ".acf") if (Path.GetExtension(file) == ".acf" && ValveDataFile.TryDeserialize(File.ReadAllText(file, Encoding.UTF8), out VProperty _result))
{ {
try dynamic result = _result;
{ string _appid = result.Value?.appid?.ToString();
dynamic property = VdfConvert.Deserialize(File.ReadAllText(file, Encoding.UTF8)); string installdir = result.Value?.installdir?.ToString();
string _appid = property.Value.appid.ToString(); string name = result.Value?.name?.ToString();
string installdir = property.Value.installdir.ToString(); string _buildid = result.Value?.buildid?.ToString();
string name = property.Value.name.ToString(); if (string.IsNullOrWhiteSpace(_appid)
string _buildid = property.Value.buildid.ToString(); || string.IsNullOrWhiteSpace(installdir)
if (string.IsNullOrWhiteSpace(_appid) || string.IsNullOrWhiteSpace(name)
|| string.IsNullOrWhiteSpace(installdir) || string.IsNullOrWhiteSpace(_buildid))
|| string.IsNullOrWhiteSpace(name) continue;
|| string.IsNullOrWhiteSpace(_buildid)) string branch = result.Value?.UserConfig?.betakey?.ToString();
continue; if (string.IsNullOrWhiteSpace(branch)) branch = "public";
string branch = property.Value.UserConfig?.betakey?.ToString(); string gameDirectory = libraryDirectory + @"\common\" + installdir;
if (string.IsNullOrWhiteSpace(branch)) branch = "public"; if (!int.TryParse(_appid, out int appid)) continue;
string gameDirectory = libraryDirectory + @"\common\" + installdir; if (!int.TryParse(_buildid, out int buildid)) continue;
if (!int.TryParse(_appid, out int appid)) continue; games.Add(new(appid, name, branch, buildid, gameDirectory));
if (!int.TryParse(_buildid, out int buildid)) continue;
games.Add(new(appid, name, branch, buildid, gameDirectory));
}
catch { }
} }
} }
if (!games.Any()) return null; if (!games.Any()) return null;
@ -182,8 +173,7 @@ namespace CreamInstaller
if (Program.Canceled) return; if (Program.Canceled) return;
string dlcName = null; string dlcName = null;
VProperty dlcAppInfo = await SteamCMD.GetAppInfo(id); VProperty dlcAppInfo = await SteamCMD.GetAppInfo(id);
if (dlcAppInfo is not null) if (dlcAppInfo is not null) dlcName = dlcAppInfo.Value?.TryGet("common")?.TryGet("name")?.ToString();
dlcName = dlcAppInfo?.Value?["common"]?["name"]?.ToString();
if (Program.Canceled) return; if (Program.Canceled) return;
if (string.IsNullOrWhiteSpace(dlcName)) return; //dlcName = "Unknown DLC"; if (string.IsNullOrWhiteSpace(dlcName)) return; //dlcName = "Unknown DLC";
dlc[id] = /*$"[{id}] " +*/ dlcName; dlc[id] = /*$"[{id}] " +*/ dlcName;
@ -211,8 +201,8 @@ namespace CreamInstaller
if (appId == 0) selection.Icon = Program.GetFileIconImage(directory + @"\launcher\bootstrapper-v2.exe"); if (appId == 0) selection.Icon = Program.GetFileIconImage(directory + @"\launcher\bootstrapper-v2.exe");
else else
{ {
selection.IconStaticID = appInfo?.Value?["common"]?["icon"]?.ToString(); selection.IconStaticID = appInfo?.Value?.TryGet("common")?.TryGet("icon")?.ToString();
selection.ClientIconStaticID = appInfo?.Value?["common"]?["clienticon"]?.ToString(); selection.ClientIconStaticID = appInfo?.Value?.TryGet("common")?.TryGet("clienticon")?.ToString();
} }
} }
if (allCheckBox.Checked) selection.Enabled = true; if (allCheckBox.Checked) selection.Enabled = true;
@ -453,14 +443,14 @@ namespace CreamInstaller
foreach (ProgramSelection selection in ProgramSelection.AllUsableEnabled) foreach (ProgramSelection selection in ProgramSelection.AllUsableEnabled)
{ {
if (selection.Name == paradoxLauncher.Name) continue; if (selection.Name == paradoxLauncher.Name) continue;
if (selection.AppInfo.Value["extended"]["publisher"].ToString() != "Paradox Interactive") continue; if (selection.AppInfo.Value?.TryGet("extended")?.TryGet("publisher")?.ToString() != "Paradox Interactive") continue;
paradoxLauncher.ExtraSteamAppIdDlc.Add(new(selection.SteamAppId, selection.Name, selection.SelectedSteamDlc)); paradoxLauncher.ExtraSteamAppIdDlc.Add(new(selection.SteamAppId, selection.Name, selection.SelectedSteamDlc));
} }
if (!paradoxLauncher.ExtraSteamAppIdDlc.Any()) if (!paradoxLauncher.ExtraSteamAppIdDlc.Any())
foreach (ProgramSelection selection in ProgramSelection.AllUsable) foreach (ProgramSelection selection in ProgramSelection.AllUsable)
{ {
if (selection.Name == paradoxLauncher.Name) continue; if (selection.Name == paradoxLauncher.Name) continue;
if (selection.AppInfo.Value["extended"]["publisher"].ToString() != "Paradox Interactive") continue; if (selection.AppInfo.Value?.TryGet("extended")?.TryGet("publisher")?.ToString() != "Paradox Interactive") continue;
paradoxLauncher.ExtraSteamAppIdDlc.Add(new(selection.SteamAppId, selection.Name, selection.AllSteamDlc)); paradoxLauncher.ExtraSteamAppIdDlc.Add(new(selection.SteamAppId, selection.Name, selection.AllSteamDlc));
} }
} }