From fe647ef85155397dcad206b1b8f83977e85299f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C3=ABl=20Kooi?= <48814281+RA-Kooi@users.noreply.github.com> Date: Tue, 13 Sep 2022 09:44:15 +0200 Subject: [PATCH] Add dump command --- DwarfOne2C/CWriter/CWriter.cs | 8 +++ DwarfOne2C/Program.cs | 117 ++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/DwarfOne2C/CWriter/CWriter.cs b/DwarfOne2C/CWriter/CWriter.cs index 5c46a09..d73002e 100644 --- a/DwarfOne2C/CWriter/CWriter.cs +++ b/DwarfOne2C/CWriter/CWriter.cs @@ -130,6 +130,14 @@ public void GenerateCode( } } + public void InsertFileDelimiter() + { + code.Add( + "// ------------------------------------------------------------" + + "--------------------"); + code.Add(""); + } + public void WriteCode() { string allDirs = Path.GetDirectoryName(outputPath); diff --git a/DwarfOne2C/Program.cs b/DwarfOne2C/Program.cs index 7118171..e5d4cf5 100644 --- a/DwarfOne2C/Program.cs +++ b/DwarfOne2C/Program.cs @@ -1,5 +1,9 @@ +using System; +using System.Collections.Generic; using System.CommandLine; using System.IO; +using System.Linq; +using System.Text; #nullable enable @@ -28,7 +32,50 @@ static int Main(string[] args) }, dumpFile); + Command dumpCommand = new( + "dump", + "Dump one or more files from the symbol dump."); + + Argument listFile = new( + "LISTFILE", + "A text file with a newline separated list of the files to dump."); + + listFile.Arity = ArgumentArity.ExactlyOne; + + Argument stripList = new( + "STRIPLIST", + "A text file with a newline separated list of the paths to " + + "strip.\nEach path only has to appear once. A strip path is part" + + "of the path you want removed from the file name you want to " + + "dump.\nE.g. \"C:\\GameCube\\killer7eu\" causes " + + "\"C:\\GameCube\\killer7eu\\Src\\Wat\\main.cpp\" to become " + + "\\Src\\Wat\\main.cpp"); + + stripList.Arity = ArgumentArity.ExactlyOne; + + Option outOption = new( + aliases: new []{"--out", "-o"}, + description: "Ouput directory", + getDefaultValue: () => + { + return new DirectoryInfo(Directory.GetCurrentDirectory()); + }); + + dumpCommand.AddArgument(dumpFile); + dumpCommand.AddArgument(listFile); + dumpCommand.AddArgument(stripList); + + dumpCommand.AddOption(outOption); + + dumpCommand.SetHandler( + (dumpFile, listFile, stripFile, outOption) => + { + DumpFiles(dumpFile!, listFile!, stripFile!, outOption); + }, + dumpFile, listFile, stripList, outOption); + rootCommand.AddCommand(listCommand); + rootCommand.AddCommand(dumpCommand); return rootCommand.Invoke(args); } @@ -38,5 +85,75 @@ static void ListFiles(FileInfo file) DumpParser dumpParser = new(file.FullName); dumpParser.ListCompilationUnits(); } + + static void DumpFiles( + FileInfo dumpFile, + FileInfo listFile, + FileInfo stripFile, + DirectoryInfo outputDir) + { + try + { + if(!outputDir.Exists) + outputDir.Create(); + + string[] fileList = File.ReadAllLines( + listFile.FullName, + Encoding.UTF8); + + string[] stripList = File.ReadAllLines( + stripFile.FullName, + Encoding.UTF8); + + DumpParser parser = new(dumpFile.FullName); + + HashSet units = parser.Parse(); + + Dictionary> sharedNames = new(); + + foreach(CompilationUnit unit in units) + { + sharedNames.TryAdd(unit.name, new()); + List sharers = sharedNames[unit.name]; + + sharers.AddRange(units.Where(c => c.name == unit.name)); + } + + sharedNames + .Where(n => fileList.Where(f => f == n.Key).Any()) + .All( + unit => + { + IEnumerable stripper = stripList + .Where(s => unit.Key.Contains(s)); + + if(!stripper.Any()) + return true; + + CWriter writer = new(outputDir.FullName, stripper.First()); + + bool insertDelimiter = unit.Value.Count > 1; + unit.Value.ForEach( + (cu) => + { + writer.GenerateCode( + cu, + parser.allTags, + parser.IDToIndex); + + if(insertDelimiter) + writer.InsertFileDelimiter(); + }); + + writer.WriteCode(); + + return true; + }); + } + catch(Exception e) + { + Console.Error.WriteLine($"Error: {e.ToString()}"); + } + } } }