mirror of
https://github.com/Syping/ragephoto-cli.git
synced 2025-12-04 16:51:48 +01:00
impl. image processing and rename update-sign option to update-json
This commit is contained in:
parent
9a70c45c7a
commit
89b37b6ede
8 changed files with 232 additions and 390 deletions
239
Commands.cs
239
Commands.cs
|
|
@ -1,58 +1,46 @@
|
|||
using Microsoft.Win32;
|
||||
using SixLabors.ImageSharp;
|
||||
using System.CommandLine;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
namespace RagePhoto.Cli;
|
||||
|
||||
internal static class Commands {
|
||||
internal static partial class Commands {
|
||||
|
||||
internal static Int32 CreateFunction(String format, String? jpegFile, String? description, String? json, String? title, String outputFile) {
|
||||
internal static Int32 CreateFunction(String format, String? imageFile, String? description, String? json, String? title, String outputFile) {
|
||||
try {
|
||||
using Photo photo = new();
|
||||
|
||||
photo.Format = format.ToLowerInvariant() switch {
|
||||
"gta5" => PhotoFormat.GTA5,
|
||||
"rdr2" => PhotoFormat.RDR2,
|
||||
_ => throw new ArgumentException("Invalid Photo Format", nameof(format))
|
||||
};
|
||||
|
||||
if (photo.Format == PhotoFormat.GTA5) {
|
||||
photo.SetHeader("PHOTO - 10/26/25 02:28:08", 798615001, 0);
|
||||
}
|
||||
else {
|
||||
photo.SetHeader("PHOTO - 10/26/25 02:31:34", 3077307752, 2901366738);
|
||||
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));
|
||||
}
|
||||
|
||||
if (String.IsNullOrEmpty(jpegFile)) {
|
||||
photo.Jpeg = Properties.Resources.EmptyJpeg;
|
||||
Size size;
|
||||
if (String.IsNullOrEmpty(imageFile)) {
|
||||
photo.Jpeg = Jpeg.GetEmptyJpeg(photo.Format, out size);
|
||||
}
|
||||
else {
|
||||
using MemoryStream jpegStream = new();
|
||||
using Stream input = jpegFile == "-" ? Console.OpenStandardInput() : File.OpenRead(jpegFile);
|
||||
input.CopyTo(jpegStream);
|
||||
photo.Jpeg = jpegStream.ToArray();
|
||||
using Stream input = imageFile == "-" ? Console.OpenStandardInput() : File.OpenRead(imageFile);
|
||||
photo.Jpeg = Jpeg.GetJpeg(input, out size);
|
||||
}
|
||||
|
||||
if (photo.Format == PhotoFormat.GTA5) {
|
||||
photo.Json = json == null ?
|
||||
Json.Initialize(PhotoFormat.GTA5, photo, Random.Shared.Next(),
|
||||
DateTimeOffset.FromUnixTimeSeconds(Random.Shared.Next(1356998400, 1388534399))) :
|
||||
Json.UpdateSign(photo, photo.Json);
|
||||
}
|
||||
else {
|
||||
photo.Json = json == null ?
|
||||
Json.Initialize(PhotoFormat.RDR2, photo, Random.Shared.Next(),
|
||||
DateTimeOffset.FromUnixTimeSeconds(Random.Shared.NextInt64(-2240524800, -2208988801))) :
|
||||
Json.UpdateSign(photo, photo.Json);
|
||||
}
|
||||
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 == "-" || outputFile == String.Empty) {
|
||||
using MemoryStream photoStream = new(photo.Save());
|
||||
using Stream output = Console.OpenStandardOutput();
|
||||
photoStream.CopyTo(output);
|
||||
output.Write(photo.Save());
|
||||
}
|
||||
else {
|
||||
String tempFile = Path.GetTempFileName();
|
||||
|
|
@ -100,7 +88,7 @@ internal static class Commands {
|
|||
content = Encoding.UTF8.GetBytes($"{photo.Format switch {
|
||||
PhotoFormat.GTA5 => "gta5",
|
||||
PhotoFormat.RDR2 => "rdr2",
|
||||
_ => "unknown"
|
||||
_ => throw new ArgumentException("Invalid Format")
|
||||
}}\n");
|
||||
break;
|
||||
case "i":
|
||||
|
|
@ -126,15 +114,13 @@ internal static class Commands {
|
|||
}
|
||||
|
||||
if (outputFile == "-" || outputFile == String.Empty) {
|
||||
using MemoryStream contentStream = new(content);
|
||||
using Stream output = Console.OpenStandardOutput();
|
||||
contentStream.CopyTo(output);
|
||||
output.Write(content);
|
||||
}
|
||||
else {
|
||||
String tempFile = Path.GetTempFileName();
|
||||
using (MemoryStream contentStream = new(content)) {
|
||||
using FileStream output = File.Create(tempFile);
|
||||
contentStream.CopyTo(output);
|
||||
using (FileStream output = File.Create(tempFile)) {
|
||||
output.Write(content);
|
||||
}
|
||||
File.Move(tempFile, outputFile, true);
|
||||
}
|
||||
|
|
@ -150,18 +136,16 @@ internal static class Commands {
|
|||
}
|
||||
}
|
||||
|
||||
internal static Int32 SetFunction(String inputFile, String? format, String? jpegFile, String? description, String? json, String? title, bool updateSign, String? outputFile) {
|
||||
if (format == null && jpegFile == null && description == null
|
||||
&& json == null && title == null && !updateSign) {
|
||||
Console.Error.WriteLine("No value has being set");
|
||||
return 1;
|
||||
}
|
||||
else if (inputFile == "-" && jpegFile == "-") {
|
||||
Console.Error.WriteLine("Multiple pipes are not supported");
|
||||
return 1;
|
||||
}
|
||||
|
||||
internal static Int32 SetFunction(String inputFile, String? format, String? imageFile, String? description, String? json, String? title, bool updateJson, String? outputFile) {
|
||||
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) {
|
||||
|
|
@ -178,7 +162,7 @@ internal static class Commands {
|
|||
photo.Format = format.ToLowerInvariant() switch {
|
||||
"gta5" => PhotoFormat.GTA5,
|
||||
"rdr2" => PhotoFormat.RDR2,
|
||||
_ => throw new ArgumentException("Invalid Photo Format", nameof(format))
|
||||
_ => throw new ArgumentException("Invalid Format", nameof(format))
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -191,25 +175,24 @@ internal static class Commands {
|
|||
if (title != null)
|
||||
photo.Title = title;
|
||||
|
||||
if (jpegFile == String.Empty) {
|
||||
photo.Jpeg = Properties.Resources.EmptyJpeg;
|
||||
photo.Json = Json.UpdateSign(photo, photo.Json);
|
||||
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 (jpegFile != null) {
|
||||
using MemoryStream jpegStream = new();
|
||||
using Stream input = jpegFile == "-" ? Console.OpenStandardInput() : File.OpenRead(jpegFile);
|
||||
input.CopyTo(jpegStream);
|
||||
photo.Jpeg = jpegStream.ToArray();
|
||||
photo.Json = Json.UpdateSign(photo, photo.Json);
|
||||
else if (imageFile != null) {
|
||||
using Stream input = imageFile == "-" ? Console.OpenStandardInput() : File.OpenRead(imageFile);
|
||||
photo.Jpeg = Jpeg.GetJpeg(input, out size);
|
||||
photo.Json = Json.Update(photo, size, photo.Json, out Int32 uid);
|
||||
}
|
||||
else if (updateSign) {
|
||||
photo.Json = Json.UpdateSign(photo, photo.Json);
|
||||
else if (updateJson) {
|
||||
size = Jpeg.GetSize(photo.Jpeg);
|
||||
photo.Json = Json.Update(photo, size, photo.Json, out Int32 uid);
|
||||
}
|
||||
|
||||
if (outputFile == "-") {
|
||||
using MemoryStream photoStream = new(photo.Save());
|
||||
using Stream output = Console.OpenStandardOutput();
|
||||
photoStream.CopyTo(output);
|
||||
output.Write(photo.Save());
|
||||
}
|
||||
else {
|
||||
String tempFile = Path.GetTempFileName();
|
||||
|
|
@ -232,52 +215,10 @@ internal static class Commands {
|
|||
}
|
||||
}
|
||||
|
||||
internal static Int32 PathFunction(String command) {
|
||||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
return 0;
|
||||
try {
|
||||
if (command == "register" || command == "unregister") {
|
||||
String appPath = Path.GetDirectoryName(Environment.ProcessPath) ??
|
||||
throw new Exception("Application Path can not be found");
|
||||
String fullAppPath = Path.TrimEndingDirectorySeparator(Path.GetFullPath(appPath));
|
||||
using RegistryKey environmentKey = Registry.LocalMachine.OpenSubKey(
|
||||
@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true) ??
|
||||
throw new Exception("Environment Registry Key can not be opened");
|
||||
String? path = environmentKey.GetValue(
|
||||
"Path", null, RegistryValueOptions.DoNotExpandEnvironmentNames) as String ??
|
||||
throw new Exception("Path Registry Value is invalid");
|
||||
List<String> paths = [.. path.Split(';', StringSplitOptions.RemoveEmptyEntries)];
|
||||
for (Int32 i = 0; i < paths.Count; i++) {
|
||||
if (!String.Equals(
|
||||
fullAppPath,
|
||||
Path.TrimEndingDirectorySeparator(Path.GetFullPath(paths[i])),
|
||||
StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
if (command == "register")
|
||||
return 0;
|
||||
paths.RemoveAt(i);
|
||||
environmentKey.SetValue("Path", String.Join(";", paths), RegistryValueKind.ExpandString);
|
||||
return 0;
|
||||
}
|
||||
if (command == "unregister")
|
||||
return 0;
|
||||
paths.Add(fullAppPath);
|
||||
environmentKey.SetValue("Path", String.Join(";", paths), RegistryValueKind.ExpandString);
|
||||
return 0;
|
||||
}
|
||||
Console.Error.WriteLine("Invalid Path Command");
|
||||
return 1;
|
||||
}
|
||||
catch (Exception exception) {
|
||||
Console.Error.WriteLine(exception.Message);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
internal static Command CreateCommand {
|
||||
get {
|
||||
Argument<String> formatArgument = new("format") {
|
||||
Description = "Photo Format"
|
||||
Description = "Format"
|
||||
};
|
||||
formatArgument.CompletionSources.Add(_ => [
|
||||
new("gta5"),
|
||||
|
|
@ -286,30 +227,30 @@ internal static class Commands {
|
|||
String[] formats = ["gta5", "rdr2"];
|
||||
String format = result.GetValueOrDefault<String>();
|
||||
if (!formats.Contains(format, StringComparer.InvariantCultureIgnoreCase))
|
||||
result.AddError("Invalid Photo Format.");
|
||||
result.AddError("Invalid Format.");
|
||||
});
|
||||
Option<String?> jpegOption = new("--jpeg", "--image", "-i") {
|
||||
Description = "JPEG File"
|
||||
Option<String?> imageOption = new("--image", "-i", "--jpeg") {
|
||||
Description = "Image File"
|
||||
};
|
||||
Option<String?> descriptionOption = new("--description", "-d") {
|
||||
Description = "Photo Description"
|
||||
Description = "Description"
|
||||
};
|
||||
Option<String?> jsonOption = new("--json", "-j") {
|
||||
Description = "Photo JSON"
|
||||
Description = "JSON"
|
||||
};
|
||||
Option<String?> titleOption = new("--title", "-t") {
|
||||
Description = "Photo Title"
|
||||
Description = "Title"
|
||||
};
|
||||
Option<String> outputOption = new("--output", "-o") {
|
||||
Description = "Output File",
|
||||
DefaultValueFactory = _ => "-"
|
||||
};
|
||||
Command createCommand = new("create", "Create a new Photo") {
|
||||
formatArgument, jpegOption, descriptionOption, jsonOption, titleOption, outputOption
|
||||
formatArgument, imageOption, descriptionOption, jsonOption, titleOption, outputOption
|
||||
};
|
||||
createCommand.SetAction(result => Environment.ExitCode = CreateFunction(
|
||||
result.GetRequiredValue(formatArgument),
|
||||
result.GetValue(jpegOption),
|
||||
result.GetValue(imageOption),
|
||||
result.GetValue(descriptionOption),
|
||||
result.GetValue(jsonOption),
|
||||
result.GetValue(titleOption),
|
||||
|
|
@ -323,39 +264,39 @@ internal static class Commands {
|
|||
Argument<String> inputArgument = new("input") {
|
||||
Description = "Input File"
|
||||
};
|
||||
Argument<String> dataTypeArgument = new("dataType") {
|
||||
Argument<String> typeArgument = new("type") {
|
||||
Description = "Data Type",
|
||||
DefaultValueFactory = _ => "jpeg"
|
||||
};
|
||||
dataTypeArgument.CompletionSources.Add(_ => [
|
||||
typeArgument.CompletionSources.Add(_ => [
|
||||
new("description"),
|
||||
new("format"),
|
||||
new("jpeg"),
|
||||
new("json"),
|
||||
new("sign"),
|
||||
new("title")]);
|
||||
dataTypeArgument.Validators.Add(result => {
|
||||
String[] dataTypes = [
|
||||
typeArgument.Validators.Add(result => {
|
||||
String[] types = [
|
||||
"d", "description",
|
||||
"f", "format",
|
||||
"i", "image", "jpeg",
|
||||
"j", "json",
|
||||
"s", "sign",
|
||||
"t", "title"];
|
||||
String dataType = result.GetValueOrDefault<String>();
|
||||
if (!dataTypes.Contains(dataType, StringComparer.InvariantCultureIgnoreCase))
|
||||
result.AddError($"Unknown Data Type: {dataType}.");
|
||||
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, dataTypeArgument, outputOption
|
||||
inputArgument, typeArgument, outputOption
|
||||
};
|
||||
getCommand.SetAction(result => Environment.ExitCode = GetFunction(
|
||||
result.GetRequiredValue(inputArgument),
|
||||
result.GetRequiredValue(dataTypeArgument),
|
||||
result.GetRequiredValue(typeArgument),
|
||||
result.GetRequiredValue(outputOption)));
|
||||
return getCommand;
|
||||
}
|
||||
|
|
@ -367,63 +308,39 @@ internal static class Commands {
|
|||
Description = "Input File"
|
||||
};
|
||||
Option<String?> formatOption = new("--format", "-f") {
|
||||
Description = "Photo Format"
|
||||
Description = "Format"
|
||||
};
|
||||
Option<String?> jpegOption = new("--jpeg", "--image", "-i") {
|
||||
Description = "JPEG File"
|
||||
Option<String?> imageOption = new("--image", "-i", "--jpeg") {
|
||||
Description = "Image File"
|
||||
};
|
||||
Option<String?> descriptionOption = new("--description", "-d") {
|
||||
Description = "Photo Description"
|
||||
Description = "Description"
|
||||
};
|
||||
Option<String?> jsonOption = new("--json", "-j") {
|
||||
Description = "Photo JSON"
|
||||
Description = "JSON"
|
||||
};
|
||||
Option<String?> titleOption = new("--title", "-t") {
|
||||
Description = "Photo Title"
|
||||
Description = "Title"
|
||||
};
|
||||
Option<bool> updateSignOption = new("--update-sign", "-u") {
|
||||
Description = "Update Photo Signature"
|
||||
Option<bool> updateJsonOption = new("--update-json", "-u") {
|
||||
Description = "Update JSON"
|
||||
};
|
||||
Option<String?> outputOption = new("--output", "-o") {
|
||||
Description = "Output File"
|
||||
};
|
||||
Command setCommand = new("set", "Set Data from a Photo") {
|
||||
inputArgument, formatOption, jpegOption, descriptionOption, jsonOption, titleOption, updateSignOption, outputOption
|
||||
inputArgument, formatOption, imageOption, descriptionOption, jsonOption, titleOption, updateJsonOption, outputOption
|
||||
};
|
||||
setCommand.SetAction(result => Environment.ExitCode = SetFunction(
|
||||
result.GetRequiredValue(inputArgument),
|
||||
result.GetValue(formatOption),
|
||||
result.GetValue(jpegOption),
|
||||
result.GetValue(imageOption),
|
||||
result.GetValue(descriptionOption),
|
||||
result.GetValue(jsonOption),
|
||||
result.GetValue(titleOption),
|
||||
result.GetValue(updateSignOption),
|
||||
result.GetValue(updateJsonOption),
|
||||
result.GetValue(outputOption)));
|
||||
return setCommand;
|
||||
}
|
||||
}
|
||||
|
||||
internal static Command PathCommand {
|
||||
get {
|
||||
Argument<String> commandArgument = new("command") {
|
||||
Description = "Path Command"
|
||||
};
|
||||
commandArgument.CompletionSources.Add(_ => [
|
||||
new ("register"),
|
||||
new ("unregister")]);
|
||||
commandArgument.Validators.Add(result => {
|
||||
String[] commands = ["register", "unregister"];
|
||||
String command = result.GetValueOrDefault<String>();
|
||||
if (!commands.Contains(command, StringComparer.InvariantCultureIgnoreCase))
|
||||
result.AddError("Invalid Path Command.");
|
||||
});
|
||||
Command pathCommand = new("path", "Register/Unregister Path") {
|
||||
commandArgument
|
||||
};
|
||||
pathCommand.Hidden = true;
|
||||
pathCommand.SetAction(result => Environment.ExitCode = PathFunction(
|
||||
result.GetRequiredValue(commandArgument)));
|
||||
return pathCommand;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue