Removed some duplicated code.

This commit is contained in:
Westin Miller
2017-11-01 01:03:10 -07:00
parent 98aae10126
commit 6814a833be
3 changed files with 6 additions and 48 deletions

View File

@@ -24,7 +24,6 @@ namespace Torch.Managers.PatchManager
{ {
var context = new MethodContext(method); var context = new MethodContext(method);
context.Read(); context.Read();
context.CheckIntegrity();
return context.Instructions; return context.Instructions;
} }

View File

@@ -100,47 +100,7 @@ namespace Torch.Managers.PatchManager.Transpile
} }
} }
[Conditional("DEBUG")]
public void CheckIntegrity()
{
var entryStackCount = new Dictionary<MsilLabel, Dictionary<MsilInstruction, int>>();
var currentStackSize = 0;
foreach (MsilInstruction insn in _instructions)
{
// I don't want to deal with this, so I won't
if (insn.OpCode == OpCodes.Br || insn.OpCode == OpCodes.Br_S || insn.OpCode == OpCodes.Jmp ||
insn.OpCode == OpCodes.Leave || insn.OpCode == OpCodes.Leave_S)
break;
foreach (MsilLabel label in insn.Labels)
if (entryStackCount.TryGetValue(label, out Dictionary<MsilInstruction, int> dict))
dict.Add(insn, currentStackSize);
else
(entryStackCount[label] = new Dictionary<MsilInstruction, int>()).Add(insn, currentStackSize);
currentStackSize += insn.StackChange();
if (insn.Operand is MsilOperandBrTarget br)
if (entryStackCount.TryGetValue(br.Target, out Dictionary<MsilInstruction, int> dict))
dict.Add(insn, currentStackSize);
else
(entryStackCount[br.Target] = new Dictionary<MsilInstruction, int>()).Add(insn, currentStackSize);
}
foreach (KeyValuePair<MsilLabel, Dictionary<MsilInstruction, int>> label in entryStackCount)
{
if (label.Value.Values.Aggregate(new HashSet<int>(), (a, b) =>
{
a.Add(b);
return a;
}).Count > 1)
{
_log.Warn($"Label {label.Key} has multiple entry stack counts");
foreach (KeyValuePair<MsilInstruction, int> kv in label.Value)
_log.Warn($"{kv.Key.Offset:X4} {kv.Key} => {kv.Value}");
}
}
}
public string ToHumanMsil() public string ToHumanMsil()
{ {
return string.Join("\n", _instructions.Select(x => $"IL_{x.Offset:X4}: {x.StackChange():+0;-#} {x}")); return string.Join("\n", _instructions.Select(x => $"IL_{x.Offset:X4}: {x.StackChange():+0;-#} {x}"));

View File

@@ -19,9 +19,7 @@ namespace Torch.Managers.PatchManager.Transpile
{ {
var context = new MethodContext(baseMethod); var context = new MethodContext(baseMethod);
context.Read(); context.Read();
context.CheckIntegrity(); // IntegrityAnalysis(LogLevel.Trace, context.Instructions);
// _log.Trace("Input Method:");
// _log.Trace(context.ToHumanMsil);
var methodContent = (IEnumerable<MsilInstruction>)context.Instructions; var methodContent = (IEnumerable<MsilInstruction>)context.Instructions;
foreach (MethodInfo transpiler in transpilers) foreach (MethodInfo transpiler in transpilers)
@@ -47,7 +45,7 @@ namespace Torch.Managers.PatchManager.Transpile
if (logMsil) if (logMsil)
{ {
var list = methodContent.ToList(); var list = methodContent.ToList();
IntegrityAnalysis(list); IntegrityAnalysis(LogLevel.Info, list);
foreach (var k in list) foreach (var k in list)
k.Emit(output); k.Emit(output);
} }
@@ -61,8 +59,9 @@ namespace Torch.Managers.PatchManager.Transpile
/// <summary> /// <summary>
/// Analyzes the integrity of a set of instructions. /// Analyzes the integrity of a set of instructions.
/// </summary> /// </summary>
/// <param name="level">default logging level</param>
/// <param name="instructions">instructions</param> /// <param name="instructions">instructions</param>
private static void IntegrityAnalysis(List<MsilInstruction> instructions) private static void IntegrityAnalysis(LogLevel level, IReadOnlyList<MsilInstruction> instructions)
{ {
var targets = new Dictionary<MsilLabel, int>(); var targets = new Dictionary<MsilLabel, int>();
for (var i = 0; i < instructions.Count; i++) for (var i = 0; i < instructions.Count; i++)
@@ -143,7 +142,7 @@ namespace Torch.Managers.PatchManager.Transpile
if (line.StartsWith("WARN", StringComparison.OrdinalIgnoreCase)) if (line.StartsWith("WARN", StringComparison.OrdinalIgnoreCase))
_log.Warn(line.Substring(4).Trim()); _log.Warn(line.Substring(4).Trim());
else else
_log.Info(line.Trim()); _log.Log(level, line.Trim());
} }
} }