Skip to content

Commit 46a57f3

Browse files
committed
Remove dataset processing, allow only conversion
1 parent 8e8a7f7 commit 46a57f3

File tree

14 files changed

+257
-452
lines changed

14 files changed

+257
-452
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace VDFParse.Cli;
5+
6+
public class BytesToBase64JsonConverter : JsonConverter<byte[]>
7+
{
8+
public override byte[] Read(ref Utf8JsonReader reader,
9+
Type typeToConvert, JsonSerializerOptions options)
10+
{
11+
return reader.GetBytesFromBase64();
12+
}
13+
14+
15+
public override void Write(Utf8JsonWriter writer,
16+
byte[] value, JsonSerializerOptions options)
17+
{
18+
writer.WriteBase64StringValue(value);
19+
}
20+
}

VDFparse.Cli/Program.cs

Lines changed: 67 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,118 @@
1-
using System.Text;
21
using Microsoft.Win32;
32
using VDFparse;
3+
using CommandLine;
4+
using System.Text.Json;
45

56
namespace VDFParse.Cli;
67

8+
class Options
9+
{
10+
[Value(0, Required = true, MetaName = "file", HelpText = "The file to read from or `appinfo`/`packageinfo`.")]
11+
public string Path { get; set; } = null!;
12+
13+
[Value(1, MetaName = "id", HelpText = "The ID of the item to query.")]
14+
public IEnumerable<uint> Ids { get; set; } = null!;
15+
16+
[Option('i', "info", HelpText = "Write only info about the specified item or file.")]
17+
public bool Info { get; set; }
18+
}
19+
720
static class Program
821
{
922
static int Main(string[] args)
1023
{
11-
Console.OutputEncoding = Encoding.UTF8;
12-
switch (args.ElementAtOrDefault(0))
13-
{
14-
case "-v":
15-
case "--version":
16-
Console.WriteLine(typeof(Program).Assembly.GetName().Version);
17-
return 0;
18-
case "-?":
19-
case "/?":
20-
case "-h":
21-
case "--help":
22-
case null:
23-
Console.WriteLine(Translations.Get("HelpMessage"),
24-
typeof(Program).Assembly.GetName().Name);
25-
return 0;
26-
default:
27-
break;
28-
}
29-
switch (args[0])
30-
{
31-
case "list":
32-
case "info":
33-
if (args.Length < 2)
34-
{
35-
Console.Error.WriteLine(Translations.Get("ErrorNotEnoughArguments"));
36-
return 4;
37-
}
38-
break;
39-
case "json":
40-
if (args.Length < 3)
41-
{
42-
Console.Error.WriteLine(Translations.Get("ErrorNotEnoughArguments"));
43-
return 4;
44-
}
45-
break;
46-
case "query":
47-
if (args.Length < 4)
48-
{
49-
Console.Error.WriteLine(Translations.Get("ErrorNotEnoughArguments"));
50-
return 4;
51-
}
52-
break;
53-
default:
54-
Console.Error.WriteLine(Translations.Get("ErrorUnknownParameter"), args[0]);
55-
return 3;
56-
}
57-
VDFFile vdfFile = new VDFFile();
58-
string filePath;
59-
if (args[1] == "appinfo" || args[1] == "packageinfo")
24+
return Parser.Default
25+
.ParseArguments<Options>(args)
26+
.MapResult(RunOptions, (_) => 1);
27+
}
28+
29+
static int RunOptions(Options opts)
30+
{
31+
// Automatically determine path of default files
32+
if (opts.Path == "appinfo" || opts.Path == "packageinfo")
6033
{
6134
string? steamPath;
6235
try
6336
{
6437
steamPath = GetSteamPath();
65-
Console.WriteLine(steamPath);
6638
if (steamPath is null)
6739
{
68-
Console.Error.WriteLine(Translations.Get("ErrorCannotFindSteam"));
69-
return 1;
40+
Console.Error.WriteLine("Cannot find Steam.");
41+
return 2;
7042
}
7143
}
7244
catch (PlatformNotSupportedException)
7345
{
74-
Console.Error.WriteLine(Translations.Get("ErrorPlatformUnsupported"));
75-
return 2;
46+
Console.Error.WriteLine("Platform unsupported for automated Steam locating.");
47+
return 3;
7648
}
77-
if (args[1] == "appinfo")
49+
if (opts.Path == "appinfo")
7850
{
79-
filePath = Path.Combine(steamPath, "appcache", "appinfo.vdf");
51+
opts.Path = Path.Combine(steamPath, "appcache", "appinfo.vdf");
8052
}
8153
else
8254
{
83-
filePath = Path.Combine(steamPath, "appcache", "packageinfo.vdf");
55+
opts.Path = Path.Combine(steamPath, "appcache", "packageinfo.vdf");
8456
}
8557
}
86-
else
87-
{
88-
filePath = args[1];
89-
}
90-
vdfFile.Read(filePath);
58+
59+
VDFFile vdfFile;
9160
try
9261
{
93-
vdfFile.Read(filePath);
62+
vdfFile = VDFFile.Read(opts.Path);
9463
}
95-
// TODO: Catch explicit errors
96-
#pragma warning disable CA1031
9764
catch
9865
{
99-
Console.Error.WriteLine(Translations.Get("ErrorParsingFile"), filePath);
100-
return 7;
66+
Console.Error.WriteLine($"Error while trying to parse \"{opts.Path}\"");
67+
return 4;
10168
}
102-
try
69+
70+
// Output the right data
71+
dynamic data;
72+
if (opts.Ids is not null)
10373
{
104-
switch (args[0])
105-
{
106-
case "list":
107-
return List(vdfFile);
108-
case "info":
109-
return Info(vdfFile);
110-
}
111-
List<Dataset> processing;
112-
if (args[2] == "*")
113-
{
114-
processing = vdfFile.Datasets;
115-
}
116-
else
74+
data = new Dictionary<uint, dynamic>();
75+
76+
var ids = opts.Ids.ToHashSet();
77+
foreach (var dataset in vdfFile.Datasets)
11778
{
118-
uint id;
119-
bool success = uint.TryParse(args[2], out id);
120-
if (!success)
121-
{
122-
Console.Error.WriteLine(Translations.Get("ErrorInvalidNumber"), args[2]);
123-
return 5;
124-
}
125-
var dataset = vdfFile.FindByID(id);
126-
if (dataset == null)
79+
if (ids.Contains(dataset.Id))
12780
{
128-
Console.Error.WriteLine(Translations.Get("ErrorNoId"), id);
129-
return 6;
81+
if (opts.Info)
82+
dataset.Data.Clear();
83+
84+
data[dataset.Id] = dataset;
85+
ids.Remove(dataset.Id);
13086
}
131-
processing = new List<Dataset> { dataset };
13287
}
133-
switch (args[0])
88+
if (ids.Count != 0)
13489
{
135-
case "json":
136-
#pragma warning disable CA1806
137-
int.TryParse(args.ElementAtOrDefault(4), out int indent);
138-
return Json(processing, indent: 4);
139-
case "query":
140-
return Query(processing, args.Skip(3).ToArray());
90+
var remaining = String.Join(", ", ids);
91+
Console.Error.WriteLine($"Could not find entries with id(s): {remaining}");
92+
return 5;
14193
}
14294
}
143-
// TODO: catch explicit errors
144-
#pragma warning disable CA1031
145-
catch (Exception e)
95+
else if (opts.Info)
14696
{
147-
Console.Error.WriteLine(Translations.Get("ErrorUnknown"), e.Message);
97+
data = new
98+
{
99+
Path = opts.Path,
100+
EUniverse = vdfFile.EUniverse,
101+
Length = vdfFile.Datasets.Count,
102+
};
148103
}
149-
return 9;
150-
}
151-
152-
static int List(VDFFile source)
153-
{
154-
Console.WriteLine(String.Join("\n", source.Datasets.Select(dataset => dataset.ID)));
155-
return 0;
156-
}
157-
158-
static int Info(VDFFile source)
159-
{
160-
Console.WriteLine(source.Datasets.Count);
161-
return 0;
162-
}
163-
164-
static int Json(List<Dataset> datasets, int indent)
165-
{
166-
foreach (var dataset in datasets)
104+
else
167105
{
168-
Console.WriteLine(dataset.Data.ToJSON(indent: indent));
106+
data = vdfFile;
169107
}
170-
return 0;
171-
}
172108

173-
static int Query(List<Dataset> datasets, string[] queries)
174-
{
175-
foreach (var dataset in datasets)
176-
{
177-
foreach (var query in queries)
178-
{
179-
Console.WriteLine(String.Join("\n", dataset.Data.Search(query)));
180-
}
181-
}
109+
var options = new JsonSerializerOptions();
110+
options.Converters.Add(new BytesToBase64JsonConverter());
111+
112+
Console.WriteLine(JsonSerializer.Serialize(data, options));
182113
return 0;
183114
}
115+
184116
static string? GetSteamPath()
185117
{
186118

VDFparse.Cli/Translations.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

VDFparse.Cli/VDFparse.Cli.csproj

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@
66
<PublishSingleFile>true</PublishSingleFile>
77
</PropertyGroup>
88

9-
<PropertyGroup>
10-
<OutputPath>../bin/Cli</OutputPath>
11-
</PropertyGroup>
12-
139
<ItemGroup>
14-
<ProjectReference Include="../VDFparse.Core/VDFparse.Core.csproj"/>
10+
<ProjectReference Include="../VDFparse.Core/VDFparse.Core.csproj" />
1511
</ItemGroup>
1612

1713
<ItemGroup>
18-
<EmbeddedResource Include="resources/*.resources" />
14+
<PackageReference Include="CommandLineParser" Version="2.8.0" />
1915
</ItemGroup>
2016

2117
<Import Project="../VDFparse.projitems" Label="Shared" />

VDFparse.Cli/languages/language-en.restext

Lines changed: 0 additions & 10 deletions
This file was deleted.
-1.89 KB
Binary file not shown.

0 commit comments

Comments
 (0)