Created
August 22, 2020 11:02
-
-
Save farsil/bdf13dfcd2cdeaccd3d0c99d3612de00 to your computer and use it in GitHub Desktop.
PackTool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
namespace PackTool | |
{ | |
internal static class Program | |
{ | |
private static readonly Regex MusicFileRegex = new Regex("#MUSIC:\\s*(.*?)\\s*;"); | |
internal static void Main(string[] args) | |
{ | |
if (args.Length < 1) | |
{ | |
Console.WriteLine($"Usage: {AppDomain.CurrentDomain.FriendlyName} <path>"); | |
Console.WriteLine(); | |
Console.WriteLine("You can drag and drop a folder from windows explorer to specify path."); | |
Environment.Exit(1); | |
} | |
foreach (var smFile in Directory.EnumerateFiles(args[0], "*.sm", SearchOption.AllDirectories)) | |
{ | |
var smFileInfo = new FileInfo(smFile); | |
Debug.Assert(smFileInfo.Directory != null); | |
Console.WriteLine($"Processing file '{smFileInfo.Name}' in folder '{smFileInfo.Directory.Name}'"); | |
IList<string> lines = File.ReadLines(smFile, Encoding.UTF8).ToList(); | |
var bannerFound = false; | |
var backgroundFound = false; | |
string musicFile = null; | |
for (var i = 0; i < lines.Count; i++) | |
{ | |
if (lines[i].StartsWith("#BANNER")) | |
{ | |
Console.WriteLine("\t-> Replacing #BANNER tag"); | |
lines[i] = "#BANNER:../bn.png;"; | |
bannerFound = true; | |
} | |
else if (lines[i].StartsWith("#BACKGROUND")) | |
{ | |
Console.WriteLine("\t-> Replacing #BACKGROUND tag"); | |
lines[i] = "#BACKGROUND:../bg.png;"; | |
backgroundFound = true; | |
} | |
else if (lines[i].StartsWith("#CDTITLE")) | |
{ | |
Console.WriteLine("\t-> Emptying #CDTITLE tag"); | |
lines[i] = "#CDTITLE:;"; | |
} | |
else if (lines[i].StartsWith("#MUSIC")) | |
{ | |
musicFile = MusicFileRegex.Match(lines[i]).Groups[1].Value; | |
} | |
} | |
if (!bannerFound) | |
{ | |
Console.WriteLine("\t-> Adding #BANNER tag"); | |
lines.Insert(0, "#BANNER:../bn.png;"); | |
} | |
if (!backgroundFound) | |
{ | |
Console.WriteLine("\t-> Adding #BACKGROUND tag"); | |
lines.Insert(0, "#BACKGROUND:../bg.png;"); | |
} | |
if (musicFile == null) | |
{ | |
Console.WriteLine($"\t/!\\ Unable to locate #MUSIC tag in file '{smFileInfo.Name}', skipping!"); | |
continue; | |
} | |
File.WriteAllLines(smFile, lines, Encoding.UTF8); | |
foreach (var fileInfo in smFileInfo.Directory.EnumerateFiles()) | |
{ | |
if (fileInfo.FullName != smFile && fileInfo.Name != musicFile) | |
{ | |
Console.WriteLine($"Removing file '{fileInfo.Name}' in folder '{smFileInfo.Directory.Name}'"); | |
fileInfo.Delete(); | |
} | |
} | |
} | |
Console.WriteLine(); | |
Console.WriteLine("Done!"); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment