From 4de05e2d61b2a5a9dba9a8561d0e3933091a0e46 Mon Sep 17 00:00:00 2001 From: Miepee Date: Sun, 24 Jul 2022 21:34:05 +0200 Subject: [PATCH] Make CLI be able to use non-interactive mode --- AM2RPortHelperCLI/Program.cs | 55 ++++++++++++++++--- .../AM2RPortHelperGUI/MainForm.cs | 2 +- AM2RPortHelperLib/PortHelper.cs | 9 +-- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/AM2RPortHelperCLI/Program.cs b/AM2RPortHelperCLI/Program.cs index c340d16..2ddb63b 100644 --- a/AM2RPortHelperCLI/Program.cs +++ b/AM2RPortHelperCLI/Program.cs @@ -7,38 +7,64 @@ namespace AM2RPortHelper; internal static class Program { - private const string version = "1.3"; - //TODO: add "-l" flag. transfer launcher mods to each other. private static int Main(string[] args) { - Console.WriteLine("AM2RPortHelperCLI v" + version); + Console.WriteLine("AM2RPortHelperCLI v" + PortHelper.Version); //TODO! - var interactiveOption = new Option(new[] {"-i", "--interactive"}, "Use an interactive mode"); + var interactiveOption = new Option(new[] { "-i", "--interactive" }, "Use an interactive mode. This will ignore all other options."); + var fileOption = new Option(new[] { "-f", "--file" }, "The file path to the raw mod that should be ported. *REQUIRED*"); + var linuxOption = new Option(new[] { "-l", "--linux" }, "The output file path for the Linux mod. None given equals to no Linux port."); + var androidOption = new Option(new[] { "-a", "--android" }, "The output file path for the Android mod. None given equals to no Android port."); + var macOption = new Option(new[] { "-m", "--mac" }, "The output file path for the Mac mod. None given equals to no Mac port."); + var nameOption = new Option(new[] { "-n", "--name" }, "The name used for the Mac mod. Required for the Mac option, has no effect on anything else."); RootCommand rootCommand = new RootCommand(); rootCommand.AddOption(interactiveOption); - rootCommand.SetHandler(RootMethod, interactiveOption); + rootCommand.SetHandler(RootMethod, interactiveOption, fileOption, linuxOption, androidOption, macOption, nameOption); return rootCommand.Invoke(args); /* + TODO: show this somewhere? maybe? Console.WriteLine("\n**Make sure to replace the icon.png and splash.png with custom ones if you don't want to have placeholders**\n"); Console.WriteLine("THIS ONLY WORKS FOR MODS BASED ON THE COMMUNITY UPDATES! MODS BASED ON 1.1 WILL NOT WORK!"); - Console.WriteLine("To which platform do you want to port to?\n1 - Linux\n2 - Android\n3 - MacOS"); */ } - private static void RootMethod(bool interactive) + private static void RootMethod(bool interactive, FileInfo inputModPath, FileInfo linuxPath, FileInfo androidPath, FileInfo macPath, string modName) { if (interactive) { RunInteractive(); return; } - var x = 0; + if (!IsValidInputZip(inputModPath)) + { + Console.Error.WriteLine("Input path does not exist, or does not point to a zip file!"); + return; + } + + if (linuxPath is not null) + { + PortHelper.PortWindowsToLinux(inputModPath.FullName, linuxPath.FullName); + } + if (androidPath is not null) + { + PortHelper.PortWindowsToAndroid(inputModPath.FullName, androidPath.FullName); + } + if (macPath is not null) + { + if (modName is null) + { + Console.Error.WriteLine("Mac option was chosen but mod name was not given!"); + return; + } + PortHelper.PortWindowsToMac(inputModPath.FullName, macPath.FullName, modName); + } + Console.WriteLine("Done"); } private static void RunInteractive() { @@ -50,7 +76,7 @@ internal static class Program Console.WriteLine("Please provide the full path to the raw mod zip!"); modZipPath = Console.ReadLine(); - if (modZipPath == null || (!File.Exists(modZipPath) && Path.GetExtension(modZipPath).ToLower() != ".zip")) + if (IsValidInputZip(modZipPath)) Console.WriteLine("Path does not exist, or does not point to a zip file!"); else invalidZip = false; @@ -88,6 +114,7 @@ internal static class Program } } while (invalidOS); + // Port everything string currentDir = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory); string linuxPath = $"{currentDir}/{Path.GetFileNameWithoutExtension(modZipPath)}_LINUX.zip"; string androidPath = $"{currentDir}/{Path.GetFileNameWithoutExtension(modZipPath)}_ANDROID.apk"; @@ -113,4 +140,14 @@ internal static class Program Console.WriteLine("Successfully finished!"); } + + private static bool IsValidInputZip(string path) + { + return path == null || (!File.Exists(path) && Path.GetExtension(path).ToLower() != ".zip"); + } + + private static bool IsValidInputZip(FileInfo path) + { + return path is not null && IsValidInputZip(path.FullName); + } } \ No newline at end of file diff --git a/AM2RPortHelperGUI/AM2RPortHelperGUI/MainForm.cs b/AM2RPortHelperGUI/AM2RPortHelperGUI/MainForm.cs index bb49634..17534d4 100644 --- a/AM2RPortHelperGUI/AM2RPortHelperGUI/MainForm.cs +++ b/AM2RPortHelperGUI/AM2RPortHelperGUI/MainForm.cs @@ -13,7 +13,7 @@ public partial class MainForm : Form { public MainForm() { - Title = "AM2RPortHelper"; + Title = $"AM2RPortHelper - v{PortHelper.Version}"; MinimumSize = new Size(200, 200); diff --git a/AM2RPortHelperLib/PortHelper.cs b/AM2RPortHelperLib/PortHelper.cs index c9bb451..a57a62f 100644 --- a/AM2RPortHelperLib/PortHelper.cs +++ b/AM2RPortHelperLib/PortHelper.cs @@ -7,7 +7,8 @@ using SixLabors.ImageSharp.Processing; namespace AM2RPortHelperLib; public static class PortHelper -{ +{ + public const string Version = "1.3"; public delegate void OutputHandlerDelegate(string output); private static OutputHandlerDelegate outputHandler; @@ -167,6 +168,7 @@ public static class PortHelper Directory.Delete(extractDirectory, true); } + //TODO: try to figure out if its possible to extract the name from the data.win file? They do have a displayname option last time I checked... public static void PortWindowsToMac(string inputRawZipPath, string outputRawZipPath, string modName, OutputHandlerDelegate outputDelegate = null) { outputHandler = outputDelegate; @@ -178,11 +180,6 @@ public static class PortHelper // Get name from user //TODO: handle error on special characters - //TODO: do not have input in lib!!! - - // Rename the .app "file", makes it too difficult to use with modpacker so commented out. - //if (!String.IsNullOrWhiteSpace(input)) - // appDirectory = appDirectory.Replace("AM2R", input); // Check if temp folder exists, delete if yes, copy bare runner to there if (Directory.Exists(baseTempDirectory))