From 82e787984f9b465353bbb9908a089528485e35b0 Mon Sep 17 00:00:00 2001 From: Aaron Yarborough Date: Tue, 8 Sep 2020 16:44:10 +0200 Subject: [PATCH] Chaaaaangesss --- .vscode/launch.json | 20 +++ .vscode/tasks.json | 24 +++ GraalGmapGenerator/GmapWriter.cs | 31 ++++ GraalGmapGenerator/GraalGmapGenerator.csproj | 3 +- GraalGmapGenerator/Helpers.cs | 17 ++ GraalGmapGenerator/Program.cs | 152 +++++++++++------- .../Validators/GmapPropertyValidators.cs | 22 +++ .../GraalGmapGeneratorTests.csproj | 3 +- 8 files changed, 210 insertions(+), 62 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 GraalGmapGenerator/GmapWriter.cs create mode 100644 GraalGmapGenerator/Helpers.cs create mode 100644 GraalGmapGenerator/Validators/GmapPropertyValidators.cs diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..4e5916f --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,20 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + "program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/GraalGmapGenerator.dll", + "args": [], + "cwd": "${workspaceFolder}", + "stopAtEntry": false, + "console": "internalConsole" + } + + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..31c32bd --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,24 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "shell", + "args": [ + "build", + // Ask dotnet build to generate full paths for file names. + "/property:GenerateFullPaths=true", + // Do not generate summary otherwise it leads to duplicate errors in Problems panel + "/consoleloggerparameters:NoSummary" + ], + "group": "build", + "presentation": { + "reveal": "silent" + }, + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/GraalGmapGenerator/GmapWriter.cs b/GraalGmapGenerator/GmapWriter.cs new file mode 100644 index 0000000..8d718ec --- /dev/null +++ b/GraalGmapGenerator/GmapWriter.cs @@ -0,0 +1,31 @@ +using System.IO; +using GraalGmapGenerator.Enums; + +namespace GraalGmapGenerator +{ + public static class GmapWriter + { + private const string TemplateFile = "template.nw"; + + public static void SaveGmap(string destinationPath, Gmap gmap) + { + if (!Directory.Exists(destinationPath)) + { + Directory.CreateDirectory(destinationPath); + } + + var gmapContentGen = new GmapContentGenerator(LevelType.Nw); + + // Create a new level file for each level + var levelNames = gmapContentGen.GetLevelNames(gmap); + foreach (string levelName in levelNames) + { + File.Copy(TemplateFile, $"{destinationPath}/{levelName}"); + } + + // Create the gmap file + var gmapContent = gmapContentGen.Generate(gmap); + File.AppendAllText($"{destinationPath}/{gmap.Name}.gmap", gmapContent); + } + } +} \ No newline at end of file diff --git a/GraalGmapGenerator/GraalGmapGenerator.csproj b/GraalGmapGenerator/GraalGmapGenerator.csproj index d6d982e..1063c3b 100644 --- a/GraalGmapGenerator/GraalGmapGenerator.csproj +++ b/GraalGmapGenerator/GraalGmapGenerator.csproj @@ -1,8 +1,7 @@ - Exe - netcoreapp2.0 + netcoreapp3.1 diff --git a/GraalGmapGenerator/Helpers.cs b/GraalGmapGenerator/Helpers.cs new file mode 100644 index 0000000..086a45f --- /dev/null +++ b/GraalGmapGenerator/Helpers.cs @@ -0,0 +1,17 @@ +using System; + +namespace GraalGmapGenerator +{ + public static class Helpers + { + public static bool YesNoToBool(string input) + { + var inputLowered = input.ToLower(); + if (input == "y" || input == "yes") + return true; + if (input == "n" || input == "no") + return false; + throw new ArgumentException("Invalid input given.", nameof(input)); + } + } +} \ No newline at end of file diff --git a/GraalGmapGenerator/Program.cs b/GraalGmapGenerator/Program.cs index 133c012..22fea92 100644 --- a/GraalGmapGenerator/Program.cs +++ b/GraalGmapGenerator/Program.cs @@ -1,93 +1,129 @@ -using GraalGmapGenerator.Enums; -using System; -using System.IO; +using System; +using GraalGmapGenerator.Validators; namespace GraalGmapGenerator { class Program { - const string TemplateFile = "template.nw"; - static void Main(string[] args) { + Console.WriteLine( + "Welcome to the GMAP generator. You can use this tool to easily generate a GMAP file " + + "\nwith the accompanying level files. Simply provide values for settings below, hitting enter " + + "\nto move to the next setting." + ); + Console.WriteLine(); + Console.WriteLine( + "If you run into any problems with the tool, drop me an email at me@aaronjy.me and I " + + "\nwill endeavour to respond as soon as possible. " + + "\nThanks for using my software! - Aaron Yarborough" + ); + Console.WriteLine("------------------------"); + var mapBuilder = new GmapBuilder(); Console.WriteLine("Gmap name..."); - mapBuilder.SetName(Console.ReadLine()); + string gmapName = Console.ReadLine(); + mapBuilder.SetName(gmapName); Console.WriteLine("Gmap width (in levels, for example: 8)..."); - // need to handle errors - int width; - do - { - var isValid = int.TryParse(Console.ReadLine(), out width) && width > 0; - if (!isValid) - { - Console.WriteLine("Please enter a valid gmap width!"); - } - } while (width <= 0); + int width = int.Parse( + GetInput( + inputFunc: () => Console.ReadLine(), + validator: (input) => + { + if (!GmapPropertyValidators.IsValidDimension(input)) + { + Console.WriteLine("Please enter a valid gmap width!"); + return false; + } + + return true; + } + ) + ); Console.WriteLine("Gmap height (in levels, for example: 5)..."); - int height; - do - { - var isValid = int.TryParse(Console.ReadLine(), out height) && height > 0; - if (!isValid) - { - Console.WriteLine("Please enter a valid gmap height!"); - } - } while (height <= 0); + int height = int.Parse( + GetInput( + inputFunc: () => Console.ReadLine(), + validator: (input) => + { + if (!GmapPropertyValidators.IsValidDimension(input)) + { + Console.WriteLine("Please enter a valid gmap height!"); + return false; + } + + return true; + } + ) + ); mapBuilder.SetDimensions(width, height); Console.WriteLine("Load full map? (y/n)..."); - if (Console.ReadLine() == "y") - { - mapBuilder.LoadFullMap(true); - } + Console.WriteLine("INFO: Loads all map parts into memory on startup."); + + var loadFullMapStr = GetInput( + inputFunc: () => Console.ReadLine(), + validator: (input) => + { + if (!GmapPropertyValidators.IsValidYesNoInput(input)) + { + Console.WriteLine("Please provide a valid \"y\" or \"n\" value!"); + return false; + } + + return true; + } + ); + + mapBuilder.LoadFullMap(Helpers.YesNoToBool(loadFullMapStr)); Console.WriteLine("No automapping? (y/n)..."); - if (Console.ReadLine() == "y") - { - mapBuilder.NoAutomapping(true); - } + Console.WriteLine("INFO: Disables the assembly of automagical screenshots into a map that is drawn over the MAPIMG image."); + + var noAutoMappingStr = GetInput( + inputFunc: () => Console.ReadLine(), + validator: (input) => + { + if (!GmapPropertyValidators.IsValidYesNoInput(input)) + { + Console.WriteLine("Please provide a valid \"y\" or \"n\" value!"); + return false; + } + return true; + } + ); + + mapBuilder.NoAutomapping(Helpers.YesNoToBool(noAutoMappingStr)); + + Console.WriteLine("Save directory..."); + Console.WriteLine($"INFO: If you do not wish to provide a save directory, you can leave this setting blank (hit ENTER) and the GMAP will be created under \"gmaps/\" in the application directory ({AppDomain.CurrentDomain.BaseDirectory}/gmaps/{gmapName}/)"); + + // string saveDirectory = Console.WriteLine("Generating gmap..."); var gmap = mapBuilder.Build(); Console.WriteLine("Saving gmap..."); - SaveGmap(gmap); + GmapWriter.SaveGmap(gmap); Console.WriteLine("Done!"); Console.ReadLine(); } - static void SaveGmap(Gmap gmap) + private static string GetInput(Func inputFunc, Func validator) { - const string OutputDirRoot = "gmaps"; - var outputDir = $"{OutputDirRoot}/{gmap.Name}"; - - // Create output directory if it doesn't exist - if (!Directory.Exists(OutputDirRoot)) + do { - Directory.CreateDirectory(OutputDirRoot); - } - - // Create gmap output directory - Directory.CreateDirectory(outputDir); - - var gmapContentGen = new GmapContentGenerator(LevelType.Nw); - - // Create a new level file for each level - var levelNames = gmapContentGen.GetLevelNames(gmap); - foreach (var level in levelNames) - { - File.Copy(TemplateFile, $"{outputDir}/{level}"); - } - - // Create the gmap file - var gmapContent = gmapContentGen.Generate(gmap); - File.AppendAllText($"{outputDir}/{gmap.Name}.gmap", gmapContent); + string inputResolved = inputFunc(); + if (validator(inputResolved)) + { + return inputResolved; + } + } while (true); } } } diff --git a/GraalGmapGenerator/Validators/GmapPropertyValidators.cs b/GraalGmapGenerator/Validators/GmapPropertyValidators.cs new file mode 100644 index 0000000..5b8e0b2 --- /dev/null +++ b/GraalGmapGenerator/Validators/GmapPropertyValidators.cs @@ -0,0 +1,22 @@ +using System; + +namespace GraalGmapGenerator.Validators +{ + public static class GmapPropertyValidators + { + public static bool IsValidDimension(string input) + { + return int.TryParse(input, out int width) && width > 0; + } + + public static bool IsValidYesNoInput(string input) + { + var inputLowered = input.ToLower(); + if (inputLowered == "yes" || inputLowered == "y") + return true; + if (inputLowered == "no" || inputLowered == "n") + return true; + return false; + } + } +} \ No newline at end of file diff --git a/GraalGmapGeneratorTests/GraalGmapGeneratorTests.csproj b/GraalGmapGeneratorTests/GraalGmapGeneratorTests.csproj index ebd429b..afea5dc 100644 --- a/GraalGmapGeneratorTests/GraalGmapGeneratorTests.csproj +++ b/GraalGmapGeneratorTests/GraalGmapGeneratorTests.csproj @@ -1,8 +1,7 @@ - netcoreapp3.0 - + netcoreapp3.1 false