1
0
mirror of https://github.com/Raymonf/whack.git synced 2024-11-27 17:40:48 +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; using WhackTranslationTool;
string? basePath = null; // @"WindowsNoEditor\Mercury\Content\Message"; string? basePath = null; // @"WindowsNoEditor\Mercury\Content\Message";
bool? overwrite = null;
static string ResolveFileOverwrite(string path) static string ResolveFileOverwrite(string path)
{ {
while (true) while (true)
{ {
Console.WriteLine($"A file already exists at '{path}'"); var overwrite = YesNoPrompt.Ask(() =>
Console.Write("Overwrite (y/n)? "); {
Console.WriteLine($"A file already exists at '{path}'");
Console.Write("Overwrite (Y/n)? ");
});
var input = Console.ReadLine()!.Trim().ToLowerInvariant(); if (overwrite)
if (input == "y")
{ {
return path; return path;
} }
else
if (input == "n")
{ {
Console.Write("Enter a new path: '"); Console.Write("Enter a new path: ");
return Console.ReadLine()!.Trim(); 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) if (basePath != null)
Console.WriteLine("Invalid path to Message directory."); 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(); basePath = Console.ReadLine()!.Trim();
continue; 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.WriteLine("reminder: don't screw up");
Console.Write("Action: "); Console.Write("Action: ");
var action = Console.ReadLine()!.Trim().ToLowerInvariant(); var action = Console.ReadLine()!.Trim().ToLowerInvariant();
switch (action) switch (action)
{ {
case "o":
overwrite = !overwrite;
Console.WriteLine($"! Overwrite mode is now {(overwrite.Value ? "ON" : "OFF")}");
break;
case "be":
case "bulk_export": case "bulk_export":
{ {
var files = Directory.GetFiles(basePath, "*.uasset"); var files = Directory.GetFiles(basePath, "*.uasset");
@ -69,39 +87,59 @@ while (true)
break; break;
} }
case "bi":
case "bulk_import": 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); Console.WriteLine(Environment.NewLine + "[Bulk Import Mode]");
var importer = new TableImporter(tableName, Path.Combine(assetFolder, assetFilename)); Console.Write("Enter the path to the directory of TOML files: ");
var outputPath = Path.Combine(outputFolder, assetFilename); var tomlFolder = Console.ReadLine()!;
Console.WriteLine($"Exporting to {outputPath}"); string assetFolder, outputFolder;
importer.Write(outputPath); 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": case "e":
{ {
Console.WriteLine(Environment.NewLine + "[Export Mode]"); Console.WriteLine(Environment.NewLine + "[Export Mode]");
Console.Write("Enter the name of the asset (e.g., 'WelcomeMessage.uasset'): "); Console.Write("Enter the name of the asset (e.g., 'WelcomeMessage.uasset'): ");
var tableName = Console.ReadLine()!.Trim(); 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"); var targetPath = Path.ChangeExtension(tableName, "toml");
if (File.Exists(targetPath)) if (File.Exists(targetPath))
{ {
targetPath = ResolveFileOverwrite(targetPath); targetPath = ResolveFileOverwrite(targetPath);
} }
using StreamWriter writer = File.CreateText(targetPath); using StreamWriter writer = File.CreateText(targetPath);
exporter.WriteTo(writer); exporter.WriteTo(writer);
writer.Flush(); writer.Flush();

View File

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