-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.cs
More file actions
398 lines (375 loc) · 15.3 KB
/
Commands.cs
File metadata and controls
398 lines (375 loc) · 15.3 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using SixLabors.ImageSharp;
using System.CommandLine;
using System.Text;
namespace RagePhoto.Cli;
internal static partial class Commands {
internal static Int32 CreateFunction(String format, String? imageFile, String? description,
String? json, String? title, String? outputFile, Boolean imageAsIs) {
try {
using Photo photo = new();
switch (format.ToLowerInvariant()) {
case "gta5":
photo.Format = PhotoFormat.GTA5;
photo.SetHeader("PHOTO - 10/26/25 02:28:08", 798615001, 0);
break;
case "rdr2":
photo.Format = PhotoFormat.RDR2;
photo.SetHeader("PHOTO - 10/26/25 02:31:34", 3077307752, 2901366738);
break;
default:
throw new ArgumentException("Invalid Format", nameof(format));
}
Size size;
if (String.IsNullOrEmpty(imageFile)) {
photo.Jpeg = Jpeg.GetEmptyJpeg(photo.Format, out size);
}
else {
using Stream input = imageFile == "-" ? Console.OpenStandardInput() : File.OpenRead(imageFile);
photo.Jpeg = Jpeg.GetJpeg(input, imageAsIs, out size);
}
photo.Json = json == null ?
Json.Initialize(photo, size, out Int32 uid) :
Json.Update(photo, size, photo.Json, out uid);
photo.Description = description ?? String.Empty;
photo.Title = title ?? "Custom Photo";
if (outputFile == "-") {
using Stream output = Console.OpenStandardOutput();
output.Write(photo.Save());
}
else {
String tempFile = Path.GetTempFileName();
photo.SaveFile(tempFile);
if (!String.IsNullOrEmpty(outputFile)) {
File.Move(tempFile, outputFile, true);
}
else {
outputFile = photo.Format switch {
PhotoFormat.GTA5 => $"PGTA5{uid}",
PhotoFormat.RDR2 => $"PRDR3{uid}_1",
_ => $"{uid}"
};
File.Move(tempFile, outputFile, true);
Console.WriteLine(outputFile);
}
}
return 0;
}
catch (RagePhotoException exception) {
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(exception.Message);
Console.ResetColor();
return exception.Photo != null ? (Int32)exception.Error + 2 : -1;
}
catch (ArgumentException exception) {
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(exception.Message);
Console.ResetColor();
return 1;
}
catch (Exception exception) {
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(exception.Message);
Console.ResetColor();
return -1;
}
}
internal static Int32 GetFunction(String inputFile, String dataType, String outputFile) {
try {
using Photo photo = new();
if (inputFile == "-" || inputFile == String.Empty) {
using MemoryStream photoStream = new();
using Stream input = Console.OpenStandardInput();
input.CopyTo(photoStream);
photo.Load(photoStream.ToArray());
}
else {
photo.LoadFile(inputFile);
}
Byte[] content = [];
switch (dataType.ToLowerInvariant()) {
case "d":
case "description":
content = Encoding.UTF8.GetBytes($"{photo.Description}\n");
break;
case "f":
case "format":
content = Encoding.UTF8.GetBytes($"{photo.Format switch {
PhotoFormat.GTA5 => "gta5",
PhotoFormat.RDR2 => "rdr2",
_ => throw new ArgumentException("Invalid Format")
}}\n");
break;
case "i":
case "image":
case "jpeg":
content = photo.Jpeg;
break;
case "j":
case "json":
content = Encoding.UTF8.GetBytes($"{photo.Json}\n");
break;
case "s":
case "sign":
content = Encoding.UTF8.GetBytes($"{photo.Sign}\n");
break;
case "t":
case "title":
content = Encoding.UTF8.GetBytes($"{photo.Title}\n");
break;
default:
Console.Error.WriteLine($"Unknown Data Type: {dataType}");
return 1;
}
if (outputFile == "-" || outputFile == String.Empty) {
using Stream output = Console.OpenStandardOutput();
output.Write(content);
}
else {
String tempFile = Path.GetTempFileName();
using (FileStream output = File.Create(tempFile)) {
output.Write(content);
}
File.Move(tempFile, outputFile, true);
}
return 0;
}
catch (RagePhotoException exception) {
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(exception.Message);
Console.ResetColor();
return exception.Photo != null ? (Int32)exception.Error + 2 : -1;
}
catch (Exception exception) {
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(exception.Message);
Console.ResetColor();
return -1;
}
}
internal static Int32 SetFunction(String inputFile, String? format, String? imageFile, String? description,
String? json, String? title, Boolean updateJson, String? outputFile, Boolean imageAsIs) {
try {
if (format == null && imageFile == null && description == null
&& json == null && title == null && !updateJson) {
throw new ArgumentException("No Value has being set");
}
else if ((inputFile == "-" || inputFile == String.Empty) && imageFile == "-") {
throw new ArgumentException("Multiple Pipes are not supported");
}
using Photo photo = new();
if (inputFile == "-" || inputFile == String.Empty) {
using MemoryStream photoStream = new();
using Stream input = Console.OpenStandardInput();
input.CopyTo(photoStream);
photo.Load(photoStream.ToArray());
}
else {
photo.LoadFile(inputFile);
}
if (format != null) {
photo.Format = format.ToLowerInvariant() switch {
"gta5" => PhotoFormat.GTA5,
"rdr2" => PhotoFormat.RDR2,
_ => throw new ArgumentException("Invalid Format", nameof(format))
};
}
if (description != null)
photo.Description = description;
if (json != null)
photo.Json = json;
if (title != null)
photo.Title = title;
Size size;
if (imageFile == String.Empty) {
photo.Jpeg = Jpeg.GetEmptyJpeg(photo.Format, out size);
photo.Json = Json.Update(photo, size, photo.Json, out Int32 uid);
}
else if (imageFile != null) {
using Stream input = imageFile == "-" ? Console.OpenStandardInput() : File.OpenRead(imageFile);
photo.Jpeg = Jpeg.GetJpeg(input, imageAsIs, out size);
photo.Json = Json.Update(photo, size, photo.Json, out Int32 uid);
}
else if (updateJson) {
size = Jpeg.GetSize(photo.Jpeg);
photo.Json = Json.Update(photo, size, photo.Json, out Int32 uid);
}
if (outputFile == "-") {
using Stream output = Console.OpenStandardOutput();
output.Write(photo.Save());
}
else {
String tempFile = Path.GetTempFileName();
photo.SaveFile(tempFile);
File.Move(tempFile, !String.IsNullOrEmpty(outputFile) ? outputFile : inputFile, true);
}
return 0;
}
catch (RagePhotoException exception) {
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(exception.Message);
Console.ResetColor();
return exception.Photo != null ? (Int32)exception.Error + 2 : -1;
}
catch (ArgumentException exception) {
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(exception.Message);
Console.ResetColor();
return 1;
}
catch (Exception exception) {
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(exception.Message);
Console.ResetColor();
return -1;
}
}
internal static Command CreateCommand {
get {
Argument<String> formatArgument = new("format") {
Description = "Format"
};
formatArgument.CompletionSources.Add(_ => [
new("gta5"),
new("rdr2")]);
formatArgument.Validators.Add(result => {
String[] formats = ["gta5", "rdr2"];
String format = result.GetValueOrDefault<String>();
if (!formats.Contains(format, StringComparer.InvariantCultureIgnoreCase))
result.AddError("Invalid Format.");
});
Option<String?> imageOption = new("--image", "-i", "--jpeg") {
Description = "Image File"
};
Option<String?> descriptionOption = new("--description", "-d") {
Description = "Description"
};
Option<String?> jsonOption = new("--json", "-j") {
Description = "JSON"
};
Option<String?> titleOption = new("--title", "-t") {
Description = "Title"
};
Option<String?> outputOption = new("--output", "-o") {
Description = "Output File"
};
Option<Boolean> imageAsIsOption = new("--image-as-is") {
Description = "Force image being set as-is"
};
Command createCommand = new("create", "Create a new Photo") {
formatArgument,
imageOption,
descriptionOption,
jsonOption,
titleOption,
outputOption,
imageAsIsOption
};
createCommand.SetAction(result => Environment.ExitCode = CreateFunction(
result.GetRequiredValue(formatArgument),
result.GetValue(imageOption),
result.GetValue(descriptionOption),
result.GetValue(jsonOption),
result.GetValue(titleOption),
result.GetValue(outputOption),
result.GetValue(imageAsIsOption)));
return createCommand;
}
}
internal static Command GetCommand {
get {
Argument<String> inputArgument = new("input") {
Description = "Input File"
};
Argument<String> typeArgument = new("type") {
Description = "Data Type",
DefaultValueFactory = _ => "jpeg"
};
typeArgument.CompletionSources.Add(_ => [
new("description"),
new("format"),
new("jpeg"),
new("json"),
new("sign"),
new("title")]);
typeArgument.Validators.Add(result => {
String[] types = [
"d", "description",
"f", "format",
"i", "image", "jpeg",
"j", "json",
"s", "sign",
"t", "title"];
String type = result.GetValueOrDefault<String>();
if (!types.Contains(type, StringComparer.InvariantCultureIgnoreCase))
result.AddError($"Unknown Data Type: {type}.");
});
Option<String> outputOption = new("--output", "-o") {
Description = "Output File",
DefaultValueFactory = _ => "-"
};
Command getCommand = new("get", "Get Data from a Photo") {
inputArgument,
typeArgument,
outputOption
};
getCommand.SetAction(result => Environment.ExitCode = GetFunction(
result.GetRequiredValue(inputArgument),
result.GetRequiredValue(typeArgument),
result.GetRequiredValue(outputOption)));
return getCommand;
}
}
internal static Command SetCommand {
get {
Argument<String> inputArgument = new("input") {
Description = "Input File"
};
Option<String?> formatOption = new("--format", "-f") {
Description = "Format"
};
Option<String?> imageOption = new("--image", "-i", "--jpeg") {
Description = "Image File"
};
Option<String?> descriptionOption = new("--description", "-d") {
Description = "Description"
};
Option<String?> jsonOption = new("--json", "-j") {
Description = "JSON"
};
Option<String?> titleOption = new("--title", "-t") {
Description = "Title"
};
Option<Boolean> updateJsonOption = new("--update-json", "-u") {
Description = "Update JSON"
};
Option<String?> outputOption = new("--output", "-o") {
Description = "Output File"
};
Option<Boolean> imageAsIsOption = new("--image-as-is") {
Description = "Force image being set as-is"
};
Command setCommand = new("set", "Set Data from a Photo") {
inputArgument,
formatOption,
imageOption,
descriptionOption,
jsonOption,
titleOption,
updateJsonOption,
outputOption,
imageAsIsOption
};
setCommand.SetAction(result => Environment.ExitCode = SetFunction(
result.GetRequiredValue(inputArgument),
result.GetValue(formatOption),
result.GetValue(imageOption),
result.GetValue(descriptionOption),
result.GetValue(jsonOption),
result.GetValue(titleOption),
result.GetValue(updateJsonOption),
result.GetValue(outputOption),
result.GetValue(imageAsIsOption)));
return setCommand;
}
}
}