Repository navigation
Expand file tree
/
Copy pathNpgsqlCommandRuntimePatcher.cs
More file actions
122 lines (102 loc) · 3.14 KB
/
Copy pathNpgsqlCommandRuntimePatcher.cs
File metadata and controls
122 lines (102 loc) · 3.14 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Linq;
using System.Reflection;
using HarmonyLib;
namespace PostgresRuntimePatcher;
internal sealed class NpgsqlCommandRuntimePatcher : IDisposable
{
private const string HarmonyId = "com.cenizalunar.tshock.postgres-runtime-patcher";
private static readonly string[] TargetMethodNames =
[
"ExecuteNonQuery",
"ExecuteScalar",
"ExecuteNonQueryAsync",
"ExecuteScalarAsync",
"ExecuteDbDataReader",
"ExecuteDbDataReaderAsync"
];
private readonly Harmony harmony = new(HarmonyId);
private bool applied;
public int Apply()
{
if (this.applied)
{
return 0;
}
Type? commandType = GetNpgsqlCommandType();
if (commandType is null)
{
return 0;
}
var prefixMethod = typeof(NpgsqlCommandRuntimePatcher).GetMethod(nameof(RewriteCommandTextPrefix), BindingFlags.NonPublic | BindingFlags.Static);
if (prefixMethod is null)
{
return 0;
}
var prefix = new HarmonyMethod(prefixMethod);
int patched = 0;
foreach (var method in commandType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly).Where(IsPatchableCommandMethod).Distinct())
{
try
{
this.harmony.Patch(method, prefix: prefix);
patched++;
}
catch
{
}
}
this.applied = patched > 0;
return patched;
}
public void Dispose()
{
if (!this.applied)
{
return;
}
this.harmony.UnpatchAll(HarmonyId);
this.applied = false;
}
private static Type? GetNpgsqlCommandType()
{
var assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => string.Equals(x.GetName().Name, "Npgsql", StringComparison.OrdinalIgnoreCase));
if (assembly is null)
{
try
{
assembly = Assembly.Load("Npgsql");
}
catch
{
return null;
}
}
return assembly.GetType("Npgsql.NpgsqlCommand", false);
}
private static bool IsPatchableCommandMethod(MethodInfo method)
{
if (method.IsAbstract || method.ContainsGenericParameters)
{
return false;
}
return TargetMethodNames.Contains(method.Name, StringComparer.Ordinal);
}
private static void RewriteCommandTextPrefix(object __instance)
{
var property = __instance.GetType().GetProperty("CommandText", BindingFlags.Public | BindingFlags.Instance);
if (property is null || !property.CanRead || !property.CanWrite)
{
return;
}
if (property.GetValue(__instance) is not string sql || string.IsNullOrWhiteSpace(sql))
{
return;
}
string rewritten = PostgresSqlRewriter.Rewrite(sql);
if (!string.Equals(sql, rewritten, StringComparison.Ordinal))
{
property.SetValue(__instance, rewritten);
}
}
}