-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
AppManifestParser.cs
248 lines (221 loc) · 11 KB
/
AppManifestParser.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
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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using FluentResults;
using GameFinder.StoreHandlers.Steam.Models;
using GameFinder.StoreHandlers.Steam.Models.ValueTypes;
using JetBrains.Annotations;
using NexusMods.Paths;
using ValveKeyValue;
using static GameFinder.StoreHandlers.Steam.Services.ParserHelpers;
namespace GameFinder.StoreHandlers.Steam.Services;
/// <summary>
/// Parser for <c>appmanifest_*.acf</c> files.
/// </summary>
/// <seealso cref="AppManifest"/>
[PublicAPI]
public static class AppManifestParser
{
/// <summary>
/// Parses the <c>appmanifest_*.acf</c> file at the given path.
/// </summary>
public static Result<AppManifest> ParseManifestFile(AbsolutePath manifestPath)
{
if (!manifestPath.FileExists)
{
return Result.Fail(new Error("Manifest file doesn't exist!")
.WithMetadata("Path", manifestPath.GetFullPath())
);
}
try
{
using var stream = manifestPath.Read();
var kv = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
var appState = kv.Deserialize(stream, KVSerializerOptions.DefaultOptions);
if (appState is null)
{
return Result.Fail(
new Error($"{nameof(KVSerializer)} returned null trying to parse the manifest file!")
.WithMetadata("Path", manifestPath.GetFullPath())
);
}
if (!appState.Name.Equals("AppState", StringComparison.Ordinal))
{
return Result.Fail(
new Error("Manifest file is potentially broken because the name doesn't match!")
.WithMetadata("Path", manifestPath.GetFullPath())
.WithMetadata("ExpectedName", "AppState")
.WithMetadata("ActualName", appState.Name)
);
}
// NOTE (@erri120 on 2023-06-02):
// The ValveKeyValue package by SteamDB (https://github.com/SteamDatabase/ValveKeyValue)
// is currently "broken" and has multiple issues regarding parsing values
// of type "uint", "ulong" and "string".
// see the following links for more information:
// - https://github.com/SteamDatabase/ValveKeyValue/pull/47
// - https://github.com/SteamDatabase/ValveKeyValue/issues/53
// - https://github.com/SteamDatabase/ValveKeyValue/issues/73
// Until those issues are resolved or I find another library, parsing is going to be broken.
var appIdResult = ParseRequiredChildObject(appState, "appid", ParseAppId);
var universeResult = ParseOptionalChildObject(appState, "Universe", ParseUInt32, default).Map(x => (SteamUniverse)x);
var nameResult = ParseRequiredChildObject(appState, "name", ParseString);
var stateFlagsResult = ParseRequiredChildObject(appState, "StateFlags", ParseByte).Map(x => (StateFlags)x);
var installationDirectoryNameResult = ParseRequiredChildObject(appState, "installdir", x => ParseRelativePath(x, manifestPath.FileSystem));
var lastUpdatedResult = ParseOptionalChildObject(appState, "LastUpdated", ParseDateTimeOffset, DateTimeOffset.UnixEpoch);
var sizeOnDiskResult = ParseOptionalChildObject(appState, "SizeOnDisk", ParseSize, Size.Zero);
var stagingSizeResult = ParseOptionalChildObject(appState, "StagingSize", ParseSize, Size.Zero);
var buildIdResult = ParseOptionalChildObject(appState, "buildid", ParseBuildId, BuildId.Empty);
var lastOwnerResult = ParseOptionalChildObject(appState, "LastOwner", ParseSteamId, SteamId.Empty);
var updateResult = ParseOptionalChildObject(appState, "UpdateResult", ParseUInt32, default);
var bytesToDownloadResult = ParseOptionalChildObject(appState, "BytesToDownload", ParseSize, Size.Zero);
var bytesDownloadedResult = ParseOptionalChildObject(appState, "BytesDownloaded", ParseSize, Size.Zero);
var bytesToStageResult = ParseOptionalChildObject(appState, "BytesToStage", ParseSize, Size.Zero);
var bytesStagedResult = ParseOptionalChildObject(appState, "BytesStaged", ParseSize, Size.Zero);
var targetBuildIdResult = ParseOptionalChildObject(appState, "TargetBuildID", ParseBuildId, BuildId.Empty);
var autoUpdateBehaviorResult = ParseOptionalChildObject(appState, "AutoUpdateBehavior", ParseByte, default).Map(x => (AutoUpdateBehavior)x);
var backgroundDownloadBehaviorResult = ParseOptionalChildObject(appState, "AllowOtherDownloadsWhileRunning", ParseByte, default).Map(x => (BackgroundDownloadBehavior)x);
var scheduledAutoUpdateResult = ParseOptionalChildObject(appState, "ScheduledAutoUpdate", ParseDateTimeOffset, DateTimeOffset.UnixEpoch);
var fullValidateAfterNextUpdateResult = ParseOptionalChildObject(appState, "FullValidateAfterNextUpdate", ParseBool, default);
var installedDepotsResult = ParseInstalledDepots(appState);
var installScriptsResult = ParseBasicDictionary(
appState,
"InstallScripts",
key => DepotId.From(uint.Parse(key)),
x => ParseRelativePath(x, manifestPath.FileSystem));
var sharedDepotsResult = ParseBasicDictionary(
appState,
"SharedDepots",
key => DepotId.From(uint.Parse(key)),
ParseAppId);
var userConfigResult = ParseBasicDictionary(
appState,
"UserConfig",
key => key,
ParseString,
StringComparer.OrdinalIgnoreCase);
var mountedConfigResult = ParseBasicDictionary(
appState,
"MountedConfig",
key => key,
ParseString,
StringComparer.OrdinalIgnoreCase);
var mergedResults = Result.Merge(
appIdResult,
universeResult,
nameResult,
stateFlagsResult,
installationDirectoryNameResult,
lastUpdatedResult,
sizeOnDiskResult,
stagingSizeResult,
buildIdResult,
lastOwnerResult,
updateResult,
bytesToDownloadResult,
bytesDownloadedResult,
bytesToStageResult,
bytesStagedResult,
targetBuildIdResult,
autoUpdateBehaviorResult,
backgroundDownloadBehaviorResult,
scheduledAutoUpdateResult,
fullValidateAfterNextUpdateResult,
installedDepotsResult,
installScriptsResult,
sharedDepotsResult,
userConfigResult,
mountedConfigResult
);
if (mergedResults.IsFailed) return mergedResults;
return Result.Ok(
new AppManifest
{
ManifestPath = manifestPath,
AppId = appIdResult.Value,
Universe = universeResult.Value,
Name = nameResult.Value,
StateFlags = stateFlagsResult.Value,
InstallationDirectoryName = installationDirectoryNameResult.Value,
LastUpdated = lastUpdatedResult.Value,
SizeOnDisk = sizeOnDiskResult.Value,
StagingSize = stagingSizeResult.Value,
BuildId = buildIdResult.Value,
LastOwner = lastOwnerResult.Value,
UpdateResult = updateResult.Value,
BytesToDownload = bytesToDownloadResult.Value,
BytesDownloaded = bytesDownloadedResult.Value,
BytesToStage = bytesToStageResult.Value,
BytesStaged = bytesStagedResult.Value,
TargetBuildId = targetBuildIdResult.Value,
AutoUpdateBehavior = autoUpdateBehaviorResult.Value,
BackgroundDownloadBehavior = backgroundDownloadBehaviorResult.Value,
ScheduledAutoUpdate = scheduledAutoUpdateResult.Value,
FullValidateAfterNextUpdate = fullValidateAfterNextUpdateResult.Value,
InstalledDepots = installedDepotsResult.Value,
InstallScripts = installScriptsResult.Value,
SharedDepots = sharedDepotsResult.Value,
UserConfig = userConfigResult.Value,
MountedConfig = mountedConfigResult.Value,
}
);
}
catch (Exception ex)
{
return Result.Fail(
new ExceptionalError("Exception was thrown while parsing the manifest file!", ex)
.WithMetadata("Path", manifestPath.GetFullPath())
);
}
}
private static Result<IReadOnlyDictionary<DepotId, InstalledDepot>> ParseInstalledDepots(KVObject appState)
{
var installedDepotsObject = FindOptionalChildObject(appState, "InstalledDepots");
if (installedDepotsObject is null)
{
return Result.Ok(
(IReadOnlyDictionary<DepotId, InstalledDepot>)ImmutableDictionary<DepotId, InstalledDepot>.Empty
);
}
var installedDepotResults = installedDepotsObject.Children
.Select(ParseInstalledDepot)
.ToArray();
var mergedResults = Result.Merge(installedDepotResults);
return mergedResults.Bind(installedDepots =>
Result.Ok(
(IReadOnlyDictionary<DepotId, InstalledDepot>)installedDepots
.ToDictionary(x => x.DepotId, x => x)
)
);
}
private static Result<InstalledDepot> ParseInstalledDepot(KVObject depotObject)
{
if (!uint.TryParse(depotObject.Name, NumberFormatInfo.InvariantInfo, out var rawDepotId))
{
return Result.Fail(
new Error("Unable to parse Depot name as a 32-bit unsigned integer!")
.WithMetadata("OriginalName", depotObject.Name)
);
}
var depotId = DepotId.From(rawDepotId);
var manifestIdResult = ParseRequiredChildObject(depotObject, "manifest", ParseManifestId);
var sizeOnDiskResult = ParseRequiredChildObject(depotObject, "size", ParseSize);
var dlcAppIdResult = ParseOptionalChildObject(depotObject, "dlcappid", ParseAppId, AppId.Empty);
var mergedResults = Result.Merge(
manifestIdResult,
sizeOnDiskResult,
dlcAppIdResult
);
if (mergedResults.IsFailed) return mergedResults;
var installedDepot = new InstalledDepot
{
DepotId = depotId,
ManifestId = manifestIdResult.Value,
SizeOnDisk = sizeOnDiskResult.Value,
DLCAppId = dlcAppIdResult.Value,
};
return Result.Ok(installedDepot);
}
}