mirror of
https://github.com/Syping/ragephoto-cli.git
synced 2025-11-09 13:15:17 +01:00
166 lines
6.1 KiB
C#
166 lines
6.1 KiB
C#
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 "format":
|
|
content = Encoding.UTF8.GetBytes(photo.Format switch {
|
|
PhotoFormat.GTA5 => "gta5",
|
|
PhotoFormat.RDR2 => "rdr2",
|
|
_ => "unknown"
|
|
});
|
|
break;
|
|
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? 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<FileInfo> photoArgument = new("photo") {
|
|
Description = "Photo File"
|
|
};
|
|
Argument<String> dataTypeArgument = new("dataType") {
|
|
Description = "Data Type",
|
|
DefaultValueFactory = _ => "jpeg"
|
|
};
|
|
Option<String> 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<FileInfo> photoArgument = new("photo") {
|
|
Description = "Photo File"
|
|
};
|
|
Option<String?> formatOption = new("format", "f") {
|
|
Description = "Photo Format"
|
|
};
|
|
Option<String?> jpegOption = new("jpeg") {
|
|
Description = "JPEG File"
|
|
};
|
|
Option<String?> jsonOption = new("json") {
|
|
Description = "Photo JSON"
|
|
};
|
|
Option<String?> titleOption = new("title", "t") {
|
|
Description = "Photo Title"
|
|
};
|
|
Option<FileInfo?> 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;
|
|
}
|
|
}
|
|
}
|