0
0
mirror of https://github.com/sp-tarkov/assembly-tool.git synced 2025-02-13 04:30:45 -05:00

Cleanup and refactor logging

This commit is contained in:
Cj 2024-12-31 13:18:11 -05:00
parent 6fd351e2ba
commit d040cb1e47
2 changed files with 111 additions and 78 deletions

View File

@ -110,7 +110,9 @@ public class ReCodeItRemapper
// Don't go any further during a validation // Don't go any further during a validation
if (validate) if (validate)
{ {
DisplayEndBanner(validate: true); new Statistics(_remaps, Stopwatch, OutPath)
.DisplayStatistics(true);
return; return;
} }
@ -223,6 +225,7 @@ public class ReCodeItRemapper
} }
types = GenericTypeFilters.FilterAbstract(types, mapping.SearchParams); types = GenericTypeFilters.FilterAbstract(types, mapping.SearchParams);
if (types.Count() == 1) return true;
if (!types.Any()) if (!types.Any())
{ {
@ -232,6 +235,7 @@ public class ReCodeItRemapper
} }
types = GenericTypeFilters.FilterSealed(types, mapping.SearchParams); types = GenericTypeFilters.FilterSealed(types, mapping.SearchParams);
if (types.Count() == 1) return true;
if (!types.Any()) if (!types.Any())
{ {
@ -241,6 +245,7 @@ public class ReCodeItRemapper
} }
types = GenericTypeFilters.FilterInterface(types, mapping.SearchParams); types = GenericTypeFilters.FilterInterface(types, mapping.SearchParams);
if (types.Count() == 1) return true;
if (!types.Any()) if (!types.Any())
{ {
@ -250,6 +255,7 @@ public class ReCodeItRemapper
} }
types = GenericTypeFilters.FilterStruct(types, mapping.SearchParams); types = GenericTypeFilters.FilterStruct(types, mapping.SearchParams);
if (types.Count() == 1) return true;
if (!types.Any()) if (!types.Any())
{ {
@ -259,6 +265,7 @@ public class ReCodeItRemapper
} }
types = GenericTypeFilters.FilterEnum(types, mapping.SearchParams); types = GenericTypeFilters.FilterEnum(types, mapping.SearchParams);
if (types.Count() == 1) return true;
if (!types.Any()) if (!types.Any())
{ {
@ -268,6 +275,7 @@ public class ReCodeItRemapper
} }
types = GenericTypeFilters.FilterAttributes(types, mapping.SearchParams); types = GenericTypeFilters.FilterAttributes(types, mapping.SearchParams);
if (types.Count() == 1) return true;
if (!types.Any()) if (!types.Any())
{ {
@ -277,6 +285,7 @@ public class ReCodeItRemapper
} }
types = GenericTypeFilters.FilterDerived(types, mapping.SearchParams); types = GenericTypeFilters.FilterDerived(types, mapping.SearchParams);
if (types.Count() == 1) return true;
if (!types.Any()) if (!types.Any())
{ {
@ -607,13 +616,14 @@ public class ReCodeItRemapper
throw; throw;
} }
DisplayEndBanner(hollowedPath);
if (DataProvider.Settings?.Remapper?.MappingPath != string.Empty) if (DataProvider.Settings?.Remapper?.MappingPath != string.Empty)
{ {
DataProvider.UpdateMapping(DataProvider.Settings!.Remapper!.MappingPath.Replace("mappings.", "mappings-new."), _remaps); DataProvider.UpdateMapping(DataProvider.Settings!.Remapper!.MappingPath.Replace("mappings.", "mappings-new."), _remaps);
} }
new Statistics(_remaps, Stopwatch, OutPath, hollowedPath)
.DisplayStatistics();
Stopwatch.Reset(); Stopwatch.Reset();
Module = null; Module = null;
@ -638,79 +648,6 @@ public class ReCodeItRemapper
} }
} }
private void DisplayEndBanner(string hollowedPath = "", bool validate = false)
{
var failures = 0;
var changes = 0;
Logger.Log("-----------------------------------------------", ConsoleColor.Green);
Logger.Log("-----------------------------------------------", ConsoleColor.Green);
foreach (var remap in _remaps)
{
if (remap.Succeeded is false) { continue; }
var original = remap.OriginalTypeName;
var proposed = remap.NewTypeName;
Logger.Log($"Renamed {original} to {proposed}", ConsoleColor.Green);
DisplayAlternativeMatches(remap);
}
foreach (var remap in _remaps)
{
if (remap.Succeeded is false && remap.NoMatchReasons.Contains(ENoMatchReason.AmbiguousWithPreviousMatch))
{
Logger.Log("----------------------------------------------------------------------", ConsoleColor.Red);
Logger.Log("Ambiguous match with a previous match during matching. Skipping remap.", ConsoleColor.Red);
Logger.Log($"New Type Name: {remap.NewTypeName}", ConsoleColor.Red);
Logger.Log($"{remap.AmbiguousTypeMatch} already assigned to a previous match.", ConsoleColor.Red);
Logger.Log("----------------------------------------------------------------------", ConsoleColor.Red);
}
else if (remap.Succeeded is false)
{
Logger.Log("-----------------------------------------------", ConsoleColor.Red);
Logger.Log($"Renaming {remap.NewTypeName} failed with reason(s)", ConsoleColor.Red);
foreach (var reason in remap.NoMatchReasons)
{
Logger.Log($"Reason: {reason}", ConsoleColor.Red);
}
Logger.Log("-----------------------------------------------", ConsoleColor.Red);
failures++;
continue;
}
changes++;
}
Logger.Log("-----------------------------------------------", ConsoleColor.Green);
Logger.Log("-----------------------------------------------", ConsoleColor.Green);
Logger.Log($"Result renamed {changes} Types. Failed to rename {failures} Types", ConsoleColor.Green);
if (!validate)
{
Logger.Log($"Assembly written to `{OutPath}`", ConsoleColor.Green);
Logger.Log($"Hollowed written to `{hollowedPath}`", ConsoleColor.Green);
Logger.Log($"Remap took {Stopwatch.Elapsed.TotalSeconds:F1} seconds", ConsoleColor.Green);
}
}
private void DisplayAlternativeMatches(RemapModel remap)
{
if (remap.TypeCandidates.Count() > 1)
{
Logger.Log($"Warning! There were {remap.TypeCandidates.Count()} possible matches for {remap.NewTypeName}. Consider adding more search parameters, Only showing the first 5.", ConsoleColor.Yellow);
foreach (var type in remap.TypeCandidates.Skip(1).Take(5))
{
Logger.Log($"{type.Name}", ConsoleColor.Yellow);
}
}
}
/// <summary> /// <summary>
/// This is used to log that all types for a given remap were filtered out. /// This is used to log that all types for a given remap were filtered out.
/// </summary> /// </summary>

View File

@ -0,0 +1,96 @@
using System.Diagnostics;
using ReCodeIt.Enums;
using ReCodeIt.Models;
using ReCodeIt.Utils;
namespace ReCodeItLib.Remapper;
public class Statistics(
List<RemapModel> remapModels,
Stopwatch stopwatch,
string outPath,
string hollowedPath = "")
{
public void DisplayStatistics(bool validate = false)
{
DisplayAlternativeMatches();
DisplayFailuresAndChanges();
if (!validate)
{
DisplayWriteAssembly();
}
}
private void DisplayAlternativeMatches()
{
foreach (var remap in remapModels)
{
if (remap.Succeeded is false) { continue; }
if (remap.TypeCandidates.Count > 1)
{
DisplayAlternativeMatches(remap);
}
}
}
private void DisplayAlternativeMatches(RemapModel remap)
{
Logger.Log($"Warning! There were {remap.TypeCandidates.Count()} possible matches for {remap.NewTypeName}. Consider adding more search parameters, Only showing the first 5.", ConsoleColor.Yellow);
foreach (var type in remap.TypeCandidates.Skip(1).Take(5))
{
Logger.Log($"{type.Name}", ConsoleColor.Yellow);
}
}
private void DisplayFailuresAndChanges()
{
var failures = 0;
var changes = 0;
foreach (var remap in remapModels)
{
if (remap.Succeeded is false && remap.NoMatchReasons.Contains(ENoMatchReason.AmbiguousWithPreviousMatch))
{
Logger.Log("----------------------------------------------------------------------", ConsoleColor.Red);
Logger.Log("Ambiguous match with a previous match during matching. Skipping remap.", ConsoleColor.Red);
Logger.Log($"New Type Name: {remap.NewTypeName}", ConsoleColor.Red);
Logger.Log($"{remap.AmbiguousTypeMatch} already assigned to a previous match.", ConsoleColor.Red);
Logger.Log("----------------------------------------------------------------------", ConsoleColor.Red);
}
else if (remap.Succeeded is false)
{
Logger.Log("-----------------------------------------------", ConsoleColor.Red);
Logger.Log($"Renaming {remap.NewTypeName} failed with reason(s)", ConsoleColor.Red);
foreach (var reason in remap.NoMatchReasons)
{
Logger.Log($"Reason: {reason}", ConsoleColor.Red);
}
Logger.Log("-----------------------------------------------", ConsoleColor.Red);
failures++;
continue;
}
changes++;
}
var renamedColor = changes > 0 ? ConsoleColor.Green : ConsoleColor.Yellow;
Logger.Log($"Renamed {changes} types", renamedColor);
var failColor = failures > 0 ? ConsoleColor.Red : ConsoleColor.Green;
Logger.Log($"Failed to rename {failures} types", failColor);
}
private void DisplayWriteAssembly()
{
Logger.Log($"Assembly written to `{outPath}`", ConsoleColor.Green);
Logger.Log($"Hollowed written to `{hollowedPath}`", ConsoleColor.Green);
Logger.Log($"Remap took {stopwatch.Elapsed.TotalSeconds:F1} seconds", ConsoleColor.Green);
}
}