From 543ac774542c797df3c2794bfca3ccba6f89cc9d Mon Sep 17 00:00:00 2001 From: Luke Betteridge Date: Sun, 8 Mar 2026 16:27:27 +0000 Subject: [PATCH] Initial Commit --- .vscode/settings.json | 3 ++ MapShuffler.slnx | 2 + MapShuffler/MapShuffler.csproj | 10 +++++ MapShuffler/Program.cs | 70 ++++++++++++++++++++++++++++++++++ MapShuffler/README.md | 52 +++++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 MapShuffler.slnx create mode 100644 MapShuffler/MapShuffler.csproj create mode 100644 MapShuffler/Program.cs create mode 100644 MapShuffler/README.md diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9fde16a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "dotnet.defaultSolution": "MapShuffler.sln" +} \ No newline at end of file diff --git a/MapShuffler.slnx b/MapShuffler.slnx new file mode 100644 index 0000000..ba788ff --- /dev/null +++ b/MapShuffler.slnx @@ -0,0 +1,2 @@ + + diff --git a/MapShuffler/MapShuffler.csproj b/MapShuffler/MapShuffler.csproj new file mode 100644 index 0000000..ed9781c --- /dev/null +++ b/MapShuffler/MapShuffler.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/MapShuffler/Program.cs b/MapShuffler/Program.cs new file mode 100644 index 0000000..488bafe --- /dev/null +++ b/MapShuffler/Program.cs @@ -0,0 +1,70 @@ +using System; +using System.IO; +using System.Linq; +using System.Text; +using System.Collections.Generic; + +try +{ + if (args.Length == 0 || args.Contains("-h") || args.Contains("--help")) + { + Console.WriteLine("Usage: MapShuffler "); + return; + } + + if (args.Length != 2) + { + Console.Error.WriteLine("Error: Expected exactly two arguments."); + Console.WriteLine("Usage: MapShuffler "); + Environment.ExitCode = 1; + return; + } + + var outputPath = args[0]; + var dirPath = args[1]; + + if (!Directory.Exists(dirPath)) + { + Console.Error.WriteLine($"Directory not found: {dirPath}"); + Environment.ExitCode = 2; + return; + } + + var bspFiles = Directory.EnumerateFiles(dirPath) + .Where(f => string.Equals(Path.GetExtension(f), ".bsp", StringComparison.OrdinalIgnoreCase)) + .Select(f => Path.GetFileNameWithoutExtension(f)) + .ToList(); + + Console.WriteLine($"Found {bspFiles.Count} .bsp file(s) in {dirPath}"); + + if (bspFiles.Count == 0) + { + File.WriteAllText(outputPath, string.Empty, Encoding.UTF8); + Console.WriteLine($"Wrote empty file to {outputPath}"); + Environment.ExitCode = 0; + return; + } + + var rnd = Random.Shared; + for (int i = bspFiles.Count - 1; i > 0; i--) + { + int j = rnd.Next(i + 1); + var tmp = bspFiles[i]; + bspFiles[i] = bspFiles[j]; + bspFiles[j] = tmp; + } + + var parent = Path.GetDirectoryName(outputPath); + if (!string.IsNullOrEmpty(parent) && !Directory.Exists(parent)) + Directory.CreateDirectory(parent); + + File.WriteAllLines(outputPath, bspFiles, Encoding.UTF8); + Console.WriteLine($"Wrote {bspFiles.Count} entries to {outputPath}"); + Environment.ExitCode = 0; +} +catch (Exception ex) +{ + Console.Error.WriteLine($"Error: {ex.Message}"); + Environment.ExitCode = 99; +} + diff --git a/MapShuffler/README.md b/MapShuffler/README.md new file mode 100644 index 0000000..9bdda11 --- /dev/null +++ b/MapShuffler/README.md @@ -0,0 +1,52 @@ +# MapShuffler + +Small console utility to enumerate `.bsp` map files from a directory, shuffle their order, and write the shuffled list to a text file. + +## Prerequisites + +- .NET 10 SDK (the project targets `net10.0`). + +## Build + +From the repository root run: + +```bash +dotnet build MapShuffler/MapShuffler.csproj +``` + +## Usage + +Syntax: + +```bash +dotnet run --project MapShuffler -- +``` + +- ``: path to the output text file that will be (over)written. +- ``: directory containing `.bsp` files (top-level only). + +Example: + +```bash +dotnet run --project MapShuffler -- shuffled_maps.txt /home/user/maps +``` + +Behavior notes: + +- The program searches the provided directory (non-recursively) for files with the `.bsp` extension (case-insensitive). +- Output contains one entry per line: the map filename with the `.bsp` extension removed. +- The list is shuffled using a Fisher–Yates shuffle and written in UTF-8. +- If there are no `.bsp` files found the program will write an empty file at the specified output path. +- The program will create the parent directory for the output file if it does not exist. +- Existing output files are overwritten. + +Exit codes: + +- `0` — success +- `1` — incorrect arguments +- `2` — input directory not found +- `99` — unexpected error + +See the implementation in [MapShuffler/Program.cs](MapShuffler/Program.cs#L1). + +If you'd like different behavior (recursive search, full paths in output, interactive prompts, or one-file-per-map), tell me and I can update the tool.