Skip to content

Commit 337cdce

Browse files
committed
Improve CLI code dumping
- No longer exports sub-function code entries - Improved performance of UMT_DUMP_ALL, and parallelized it
1 parent 1e19917 commit 337cdce

1 file changed

Lines changed: 44 additions & 15 deletions

File tree

UndertaleModCli/Program.cs

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
using System.Text;
1313
using System.Threading;
1414
using System.Threading.Tasks;
15+
using Underanalyzer.Decompiler;
1516
using UndertaleModLib;
1617
using UndertaleModLib.Compiler;
18+
using UndertaleModLib.Decompiler;
1719
using UndertaleModLib.Models;
1820
using UndertaleModLib.Project;
1921
using UndertaleModLib.Scripting;
@@ -499,15 +501,30 @@ private static int Dump(DumpOptions options)
499501
// If user provided code to dump, dump code
500502
if ((options.Code?.Length > 0) && (program.Data.Code?.Count > 0))
501503
{
504+
GlobalDecompileContext globalDecompileContext = new(program.Data);
505+
IDecompileSettings decompilerSettings = program.Data.ToolInfo.DecompilerSettings;
506+
502507
// If user wanted to dump everything, do that, otherwise only dump what user provided
503-
string[] codeArray;
504508
if (options.Code.Contains(UMT_DUMP_ALL))
505-
codeArray = program.Data.Code.Select(c => c.Name.Content).ToArray();
509+
{
510+
int totalCores = Environment.ProcessorCount;
511+
int outerLimit = Math.Max(1, totalCores / 4);
512+
Parallel.ForEach(program.Data.Code, new ParallelOptions { MaxDegreeOfParallelism = outerLimit }, code =>
513+
{
514+
if (code.ParentEntry is not null)
515+
{
516+
return;
517+
}
518+
program.DumpCodeEntry(code, globalDecompileContext, decompilerSettings);
519+
});
520+
}
506521
else
507-
codeArray = options.Code;
508-
509-
foreach (string code in codeArray)
510-
program.DumpCodeEntry(code);
522+
{
523+
foreach (string code in options.Code)
524+
{
525+
program.DumpCodeEntry(code, globalDecompileContext, decompilerSettings);
526+
}
527+
}
511528
}
512529

513530
// If user wanted to dump strings, dump all of them in a text file
@@ -817,32 +834,44 @@ private void CliQuickInfo()
817834
/// <summary>
818835
/// Dumps a code entry from a data file.
819836
/// </summary>
820-
/// <param name="codeEntry">The code entry that should get dumped</param>
821-
private void DumpCodeEntry(string codeEntry)
837+
/// <param name="codeEntryName">The code entry name that should get dumped</param>
838+
/// <param name="context">Decompile context to use when dumping</param>
839+
/// <param name="settings">Settings to use for the decompiler</param>
840+
private void DumpCodeEntry(string codeEntryName, GlobalDecompileContext context, IDecompileSettings settings)
822841
{
823-
UndertaleCode code = Data.Code.ByName(codeEntry);
824-
842+
UndertaleCode code = Data.Code.ByName(codeEntryName);
825843

826844
if (code == null)
827845
{
828-
Console.Error.WriteLine($"Data file does not contain a code entry named {codeEntry}!");
846+
Console.Error.WriteLine($"Data file does not contain a code entry named {codeEntryName}!");
829847
return;
830848
}
831849

850+
DumpCodeEntry(code, context, settings);
851+
}
852+
853+
/// <summary>
854+
/// Dumps a code entry from a data file.
855+
/// </summary>
856+
/// <param name="code">The code entry that should get dumped</param>
857+
/// <param name="context">Decompile context to use when dumping</param>
858+
/// <param name="settings">Settings to use for the decompiler</param>
859+
private void DumpCodeEntry(UndertaleCode code, GlobalDecompileContext context, IDecompileSettings settings)
860+
{
832861
string directory = Path.Join(Output.FullName, "CodeEntries");
833862

834863
Directory.CreateDirectory(directory);
835864

836865
if (Verbose)
837-
Console.WriteLine($"Dumping {codeEntry}");
866+
Console.WriteLine($"Dumping {code.Name?.Content}");
838867

839-
string dest = Paths.TryJoinVerifyWithinDirectory(directory, $"{codeEntry}.gml");
868+
string dest = Paths.TryJoinVerifyWithinDirectory(directory, $"{code.Name?.Content}.gml");
840869
if (dest is null)
841870
{
842-
Console.Error.WriteLine($"Failed to export code entry with name {codeEntry}");
871+
Console.Error.WriteLine($"Failed to export code entry with name {code.Name?.Content}");
843872
return;
844873
}
845-
File.WriteAllText(dest, GetDecompiledText(code));
874+
File.WriteAllText(dest, GetDecompiledText(code, context, settings));
846875
}
847876

848877
/// <summary>

0 commit comments

Comments
 (0)