1
0
mirror of https://github.com/Raymonf/whack.git synced 2024-11-24 00:20:10 +01:00

QOL: overwrite mode, minor error handling

yes, I got annoyed.
This commit is contained in:
Raymonf 2022-11-05 14:46:26 -04:00
parent a59a0af2c0
commit 738cff0e05
No known key found for this signature in database
GPG Key ID: 438459BF619B037A
3 changed files with 106 additions and 36 deletions

View File

@ -1,28 +1,30 @@
using WhackTranslationTool;
string? basePath = null; // @"WindowsNoEditor\Mercury\Content\Message";
bool? overwrite = null;
static string ResolveFileOverwrite(string path)
{
while (true)
{
Console.WriteLine($"A file already exists at '{path}'");
Console.Write("Overwrite (y/n)? ");
var overwrite = YesNoPrompt.Ask(() =>
{
Console.WriteLine($"A file already exists at '{path}'");
Console.Write("Overwrite (Y/n)? ");
});
var input = Console.ReadLine()!.Trim().ToLowerInvariant();
if (input == "y")
if (overwrite)
{
return path;
}
if (input == "n")
else
{
Console.Write("Enter a new path: '");
return Console.ReadLine()!.Trim();
Console.Write("Enter a new path: ");
var newPath = Console.ReadLine()!.Trim();
if (newPath.Length < 1)
continue;
return newPath;
}
Console.WriteLine($"Invalid selection!" + Environment.NewLine);
}
}
@ -38,16 +40,32 @@ while (true)
{
if (basePath != null)
Console.WriteLine("Invalid path to Message directory.");
Console.Write("Path to Message directory: ");
Console.Write("Path to the game's Message directory: ");
basePath = Console.ReadLine()!.Trim();
continue;
}
Console.WriteLine("'e' for export, 'i' for import, 'bulk_import', 'bulk_export', 'q' to quit");
Console.WriteLine();
while (overwrite == null)
{
overwrite = YesNoPrompt.Ask(() =>
{
Console.WriteLine("! Overwrite mode will read from and overwrite files in your original Message folder.");
Console.Write("Enable overwrite mode (Y/n)? ");
});
Console.WriteLine();
}
Console.WriteLine("'e' for export, 'i' for import, 'bi' or 'bulk_import', 'be' or 'bulk_export', 'o' to toggle overwrite mode, 'q' to quit");
Console.WriteLine($"overwrite mode: {(overwrite.Value ? "ON" : "OFF")}");
Console.WriteLine("reminder: don't screw up");
Console.Write("Action: ");
var action = Console.ReadLine()!.Trim().ToLowerInvariant();
switch (action)
{
case "o":
overwrite = !overwrite;
Console.WriteLine($"! Overwrite mode is now {(overwrite.Value ? "ON" : "OFF")}");
break;
case "be":
case "bulk_export":
{
var files = Directory.GetFiles(basePath, "*.uasset");
@ -69,39 +87,59 @@ while (true)
break;
}
case "bi":
case "bulk_import":
{
Console.WriteLine(Environment.NewLine + "[Bulk Import Mode]");
Console.Write("Enter the path to the directory of TOML files: ");
var tomlFolder = Console.ReadLine()!;
Console.Write("Enter the path to the uasset directory: ");
var assetFolder = Console.ReadLine()!;
Console.Write("Enter the path to the output uasset directory: ");
var outputFolder = Console.ReadLine()!;
var files = Directory.GetFiles(tomlFolder, "*.toml");
foreach (var tableName in files)
{
var assetFilename = GetAssetFilename(tableName);
var importer = new TableImporter(tableName, Path.Combine(assetFolder, assetFilename));
var outputPath = Path.Combine(outputFolder, assetFilename);
Console.WriteLine($"Exporting to {outputPath}");
importer.Write(outputPath);
Console.WriteLine(Environment.NewLine + "[Bulk Import Mode]");
Console.Write("Enter the path to the directory of TOML files: ");
var tomlFolder = Console.ReadLine()!;
string assetFolder, outputFolder;
if (!overwrite.Value)
{
Console.Write("Enter the path to the uasset directory: ");
assetFolder = Console.ReadLine()!;
Console.Write("Enter the path to the output uasset directory: ");
outputFolder = Console.ReadLine()!;
}
else
{
Console.Write("! Override mode is on: using original Message directory.");
assetFolder = basePath;
outputFolder = basePath;
}
if (assetFolder.Length < 1 || outputFolder.Length < 1)
{
Console.WriteLine($"! Didn't get a value for one of the directories, cancelling.");
break;
}
var files = Directory.GetFiles(tomlFolder, "*.toml");
foreach (var tableName in files)
{
var assetFilename = GetAssetFilename(tableName);
var importer = new TableImporter(tableName, Path.Combine(assetFolder, assetFilename));
var outputPath = Path.Combine(outputFolder, assetFilename);
Console.WriteLine($"Exporting to {outputPath}");
importer.Write(outputPath);
}
break;
}
break;
}
case "e":
{
Console.WriteLine(Environment.NewLine + "[Export Mode]");
Console.Write("Enter the name of the asset (e.g., 'WelcomeMessage.uasset'): ");
var tableName = Console.ReadLine()!.Trim();
var exporter = new TableExporter(Path.Combine(basePath, tableName));
if (tableName.Length < 1)
{
Console.WriteLine("! Didn't get a path to an asset, cancelling.");
break;
}
var exporter = new TableExporter(Path.Combine(basePath, tableName));
var targetPath = Path.ChangeExtension(tableName, "toml");
if (File.Exists(targetPath))
{
targetPath = ResolveFileOverwrite(targetPath);
}
using StreamWriter writer = File.CreateText(targetPath);
exporter.WriteTo(writer);
writer.Flush();

View File

@ -53,12 +53,13 @@ public class TableImporter
}
// prompt to retry so I don't have to keep restarting this thing
Console.Write("Try again (Y/n)? ");
var response = Console.ReadLine()!.Trim().ToLowerInvariant();
if (response is not ("" or "y" or "yes"))
var retry = YesNoPrompt.Ask(() =>
{
Console.Write("Try again (Y/n)?");
});
if (!retry)
break;
}
}
}

31
WTT/WTT/YesNoPrompt.cs Normal file
View File

@ -0,0 +1,31 @@
using System;
namespace WhackTranslationTool
{
internal class YesNoPrompt
{
/// <summary>
/// Call promptMessageFunc and ask for response
/// Default is always Y with no response
/// </summary>
/// <param name="promptMessageFunc">Optional function to call before ReadLine()</param>
/// <returns>The answer</returns>
public static bool Ask(Action? promptMessageFunc = null)
{
while (true)
{
if (promptMessageFunc != null)
promptMessageFunc();
var response = Console.ReadLine()!.Trim().ToLowerInvariant();
if (response is "" or "y" or "yes")
{
return true;
}
else if (response is "n")
{
return false;
}
}
}
}
}