using RagePhoto; using System.CommandLine; using System.Text; internal static class Program { private static void Main(String[] args) { RootCommand rootCommand = new("ragephoto-cli Application") { GetCommand, SetCommand }; rootCommand.Parse(args).Invoke(); } private static void Get(FileInfo photoFile, String dataType, String outputFile) { try { using Photo photo = new(); photo.LoadFile(photoFile.FullName); Byte[] content = []; switch (dataType.ToLowerInvariant()) { case "description": content = Encoding.UTF8.GetBytes(photo.Description); break; case "format": content = Encoding.UTF8.GetBytes(photo.Format switch { PhotoFormat.GTA5 => "gta5", PhotoFormat.RDR2 => "rdr2", _ => "unknown" }); break; case "image": case "jpeg": content = photo.Jpeg; break; case "json": content = Encoding.UTF8.GetBytes(photo.Json); break; case "sign": content = Encoding.UTF8.GetBytes($"{photo.Sign}"); break; case "title": content = Encoding.UTF8.GetBytes(photo.Title); break; default: Console.Error.WriteLine($"Unknown Content Type: {dataType}"); Environment.Exit(1); break; } using Stream output = outputFile == "-" ? Console.OpenStandardOutput() : File.Create(outputFile); output.Write(content); } catch (RagePhotoException exception) { Console.Error.WriteLine(exception.Message); Environment.Exit(exception.Photo != null ? (Int32)exception.Error + 2 : -1); } catch (Exception exception) { Console.Error.WriteLine(exception.Message); Environment.Exit(-1); } } private static void Set(FileInfo photoFile, String? format, String? jpegFile, String? description, String? json, String? title, FileInfo? outputFile) { if (format == null && jpegFile == null && description == null && json == null && title == null) { Console.Error.WriteLine("No value has being set"); Environment.Exit(1); } try { using Photo photo = new(); photo.LoadFile(photoFile.FullName); 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; if (jpegFile == string.Empty) { photo.Jpeg = new Byte[1]; } else if (jpegFile != null) { using MemoryStream jpegStream = new(); using Stream input = jpegFile == "-" ? Console.OpenStandardInput() : File.OpenRead(jpegFile); input.CopyTo(jpegStream); photo.Jpeg = jpegStream.ToArray(); } String tempFile = Path.GetTempFileName(); photo.SaveFile(tempFile); File.Move(tempFile, outputFile != null ? outputFile.FullName : photoFile.FullName, true); } catch (RagePhotoException exception) { Console.Error.WriteLine(exception.Message); Environment.Exit(exception.Photo != null ? (Int32)exception.Error + 2 : -1); } catch (ArgumentException exception) { Console.Error.WriteLine(exception.Message); Environment.Exit(1); } catch (Exception exception) { Console.Error.WriteLine(exception.Message); Environment.Exit(-1); } } private static Command GetCommand { get { Argument photoArgument = new("photo") { Description = "Photo File" }; Argument dataTypeArgument = new("dataType") { Description = "Data Type", DefaultValueFactory = _ => "jpeg" }; Option outputOption = new("--output", "-o") { Description = "Output File", DefaultValueFactory = _ => "-" }; Command getCommand = new("get", "Get Photo Data") { photoArgument, dataTypeArgument, outputOption }; getCommand.SetAction(result => Get( result.GetRequiredValue(photoArgument), result.GetRequiredValue(dataTypeArgument), result.GetRequiredValue(outputOption))); return getCommand; } } private static Command SetCommand { get { Argument photoArgument = new("photo") { Description = "Photo File" }; Option formatOption = new("--format", "-f") { Description = "Photo Format" }; Option jpegOption = new("--jpeg", "--image", "-i") { Description = "JPEG File" }; Option descriptionOption = new("--description", "-d") { Description = "Photo Description" }; Option jsonOption = new("--json", "-j") { Description = "Photo JSON" }; Option titleOption = new("--title", "-t") { Description = "Photo Title" }; Option outputOption = new("--output", "-o") { Description = "Output File" }; Command setCommand = new("set", "Set Photo Data") { photoArgument, formatOption, jpegOption, descriptionOption, jsonOption, titleOption, outputOption }; setCommand.SetAction(result => Set( result.GetRequiredValue(photoArgument), result.GetValue(formatOption), result.GetValue(jpegOption), result.GetValue(descriptionOption), result.GetValue(jsonOption), result.GetValue(titleOption), result.GetValue(outputOption))); return setCommand; } } }