Skip to content

Commit

Permalink
Add dump command
Browse files Browse the repository at this point in the history
  • Loading branch information
RA-Kooi committed Sep 13, 2022
1 parent a58377a commit fe647ef
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
8 changes: 8 additions & 0 deletions DwarfOne2C/CWriter/CWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ public void GenerateCode(
}
}

public void InsertFileDelimiter()
{
code.Add(
"// ------------------------------------------------------------" +
"--------------------");
code.Add("");
}

public void WriteCode()
{
string allDirs = Path.GetDirectoryName(outputPath);
Expand Down
117 changes: 117 additions & 0 deletions DwarfOne2C/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.IO;
using System.Linq;
using System.Text;

#nullable enable

Expand Down Expand Up @@ -28,7 +32,50 @@ static int Main(string[] args)
},
dumpFile);

Command dumpCommand = new(
"dump",
"Dump one or more files from the symbol dump.");

Argument<FileInfo?> listFile = new(
"LISTFILE",
"A text file with a newline separated list of the files to dump.");

listFile.Arity = ArgumentArity.ExactlyOne;

Argument<FileInfo?> 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 " +
"<output directory>\\Src\\Wat\\main.cpp");

stripList.Arity = ArgumentArity.ExactlyOne;

Option<DirectoryInfo> 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);
}
Expand All @@ -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<CompilationUnit> units = parser.Parse();

Dictionary<string, List<CompilationUnit>> sharedNames = new();

foreach(CompilationUnit unit in units)
{
sharedNames.TryAdd(unit.name, new());
List<CompilationUnit> 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<string> 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()}");
}
}
}
}

0 comments on commit fe647ef

Please sign in to comment.