From 89b430b62d8ee1de2a19a70fb347c4084c4cd92b Mon Sep 17 00:00:00 2001 From: Syping Date: Fri, 24 Oct 2025 00:52:39 +0200 Subject: [PATCH] impl. set command --- Program.cs | 133 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 110 insertions(+), 23 deletions(-) diff --git a/Program.cs b/Program.cs index a677986..9e7e47e 100644 --- a/Program.cs +++ b/Program.cs @@ -5,30 +5,9 @@ using System.Text; internal static class Program { private static void Main(String[] args) { - - Argument photoArgument = new("photo") { - Description = "Photo File" - }; - Argument dataTypeArgument = new("dataType") { - Description = "Data Type", - DefaultValueFactory = _ => "jpeg" - }; - Option outputOption = new("output", "o") { - Description = "Output", - DefaultValueFactory = _ => "-" - }; - Command getCommand = new("get", "Get Photo Data") { - photoArgument, dataTypeArgument, outputOption - }; - getCommand.SetAction(result => Get( - result.GetRequiredValue(photoArgument), - result.GetRequiredValue(dataTypeArgument), - result.GetRequiredValue(outputOption))); - RootCommand rootCommand = new("ragephoto-cli Application") { - getCommand + GetCommand, SetCommand }; - rootCommand.Parse(args).Invoke(); } @@ -64,7 +43,7 @@ internal static class Program { break; } - Stream output = outputFile == "-" ? Console.OpenStandardOutput() : File.Create(outputFile); + using Stream output = outputFile == "-" ? Console.OpenStandardOutput() : File.Create(outputFile); output.Write(content); } catch (RagePhotoException exception) { @@ -76,4 +55,112 @@ internal static class Program { Environment.Exit(-1); } } + + private static void Set(FileInfo photoFile, String? format, String? jpegFile, String? json, String? title, FileInfo? outputFile) { + if (string.IsNullOrEmpty(format) && string.IsNullOrEmpty(jpegFile) && + 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 (!string.IsNullOrEmpty(format)) { + photo.Format = format.ToLowerInvariant() switch { + "gta5" => PhotoFormat.GTA5, + "rdr2" => PhotoFormat.RDR2, + _ => throw new ArgumentException("Invalid format", nameof(format)) + }; + } + + if (json != null) + photo.Json = json; + + if (title != null) + photo.Title = title; + + if (!string.IsNullOrEmpty(jpegFile)) { + 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); + } + 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") { + Description = "JPEG File" + }; + Option jsonOption = new("json") { + 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, jsonOption, titleOption, outputOption + }; + setCommand.SetAction(result => Set( + result.GetRequiredValue(photoArgument), + result.GetValue(formatOption), + result.GetValue(jpegOption), + result.GetValue(jsonOption), + result.GetValue(titleOption), + result.GetValue(outputOption))); + return setCommand; + } + } }