-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathHarmonyException.cs
73 lines (63 loc) · 2.86 KB
/
HarmonyException.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace HarmonyLib
{
/// <summary>Under Mono, HarmonyException wraps IL compile errors with detailed information about the failure</summary>
///
[Serializable]
public class HarmonyException : Exception
{
Dictionary<int, CodeInstruction> instructions;
int errorOffset;
internal HarmonyException() { }
internal HarmonyException(string message) : base(message) { }
internal HarmonyException(string message, Exception innerException) : base(message, innerException) { }
/// <summary>Default serialization constructor (not implemented)</summary>
/// <param name="serializationInfo">The info</param>
/// <param name="streamingContext">The context</param>
///
protected HarmonyException(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext streamingContext) => throw new NotImplementedException();
internal HarmonyException(Exception innerException, Dictionary<int, CodeInstruction> instructions, int errorOffset) : base("IL Compile Error", innerException)
{
this.instructions = instructions;
this.errorOffset = errorOffset;
}
internal static Exception Create(Exception ex, Dictionary<int, CodeInstruction> finalInstructions)
{
var match = Regex.Match(ex.Message.TrimEnd(), "Reason: Invalid IL code in.+: IL_(\\d{4}): (.+)$");
if (match.Success is false) return ex;
var offset = int.Parse(match.Groups[1].Value, System.Globalization.NumberStyles.HexNumber);
_ = Regex.Replace(match.Groups[2].Value, " {2,}", " ");
if (ex is HarmonyException hEx)
{
hEx.instructions = finalInstructions;
hEx.errorOffset = offset;
return hEx;
}
return new HarmonyException(ex, finalInstructions, offset);
}
/// <summary>Get a list of IL instructions in pairs of offset+code</summary>
/// <returns>A list of key/value pairs which represent an offset and the code at that offset</returns>
///
public List<KeyValuePair<int, CodeInstruction>> GetInstructionsWithOffsets() => [.. instructions.OrderBy(ins => ins.Key)];
/// <summary>Get a list of IL instructions without offsets</summary>
/// <returns>A list of <see cref="CodeInstruction"/></returns>
///
public List<CodeInstruction> GetInstructions() => instructions.OrderBy(ins => ins.Key).Select(ins => ins.Value).ToList();
/// <summary>Get the error offset of the errornous IL instruction</summary>
/// <returns>The offset</returns>
///
public int GetErrorOffset() => errorOffset;
/// <summary>Get the index of the errornous IL instruction</summary>
/// <returns>The index into the list of instructions or -1 if not found</returns>
///
public int GetErrorIndex()
{
if (instructions.TryGetValue(errorOffset, out var instruction))
return GetInstructions().IndexOf(instruction);
return -1;
}
}
}