Skip to content

Instantly share code, notes, and snippets.

@GMMan
Created June 30, 2024 21:20
Show Gist options
  • Save GMMan/7294c141576cd056ed2369e34a5c59e7 to your computer and use it in GitHub Desktop.
Save GMMan/7294c141576cd056ed2369e34a5c59e7 to your computer and use it in GitHub Desktop.
TI MCE patch source splitter
// See https://aka.ms/new-console-template for more information
using System.Text.RegularExpressions;
string inFile = @"your_patch_file.c";
string outDir = @"your_output_dir";
Regex patternRegex = new Regex(@"; (.+?):\s+(\d+) (.*)");
using StreamReader sr = File.OpenText(inFile);
Directory.CreateDirectory(outDir);
bool foundStart = false;
Dictionary<string, FileContext> sourceFiles = new();
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (!foundStart)
{
if (line == "/* -------- ASSEMBLY CODE ----------------------------------------------")
{
foundStart = true;
}
}
else
{
if (line == "*/") break;
Match match = patternRegex.Match(line);
if (!match.Success) throw new InvalidDataException("Can't match source line.");
string fileName = match.Groups[1].Value;
string lineNum = match.Groups[2].Value;
string lineContent = match.Groups[3].Value;
int lineNumInt = int.Parse(lineNum);
if (!sourceFiles.TryGetValue(fileName, out var fileCtx))
{
fileCtx = new(0, File.CreateText(Path.Combine(outDir, fileName)));
sourceFiles.Add(fileName, fileCtx);
}
if (lineNumInt - 1 != fileCtx.LineNum) throw new InvalidDataException($"{fileName}:{lineNumInt}: was expecting line {fileCtx.LineNum + 1}!");
fileCtx.Writer.WriteLine(lineContent);
fileCtx.LineNum = lineNumInt;
}
}
foreach (var ctx in sourceFiles.Values)
{
ctx.Writer.Flush();
ctx.Writer.Close();
}
class FileContext
{
public int LineNum { get; set; }
public StreamWriter Writer { get; set; }
public FileContext(int lineNum, StreamWriter writer)
{
LineNum = lineNum;
Writer = writer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment