HarmonyLib Patch


using System;
using System.IO;
using System.Linq.Expressions;
using System.Net;
using System.Reflection;
using HarmonyLib;

public static class Pacth
{
	public static void MyPacth()
	{	foreach (string text in Directory.GetFiles("C:\\Users\\Administrator\\Desktop\\t5\\bin\\Debug\\net5.0\\ttt", "*.dll"))
			{
				AppDomain.CurrentDomain.Load(File.ReadAllBytes(text));
			}
		foreach (MethodInfo methodInfo in AccessTools.GetDeclaredMethods(Type.GetType("Netch.Utils.WebUtil")))
		{
		     var harmony = new Harmony(methodInfo.MetadataToken.ToString());
                var mPrefix = SymbolExtensions.GetMethodInfo(() => Prefix(null));
                harmony.Patch(methodInfo, new HarmonyMethod(mPrefix), new HarmonyMethod(Postfix));
		}
	}

	private static void Postfix2(ref object __result)
    {
		if (__result is string){
			System.IO.File.AppendAllText("vvv2.log","Postfix:"+(string)__result+"\r\n");
		}
    }
    private static void Postfix(ref object __instance, ref object __result)
    {
        if (__result.GetType()==)
		System.IO.File.AppendAllText("vvv2.log","Postfix result:"+__result.GetType().FullName+"\r\n");
    }
    
	private static bool Prefix(object[] __args)
	{
		if (__args.Length > 2)
		{
			if (__args[0] is HttpWebRequest)
			{
				File.AppendAllText("vvv2.log", "Prefix:"+((HttpWebRequest)__args[0]).RequestUri.ToString()+"\r\n");
			}
			if (__args[1] is string)
			{
				File.AppendAllText("vvv2.log","Prefix:"+ (string)__args[1]+"\r\n");
			}
		}
		return true;
	}
}
[HarmonyPatch(typeof(YourClassName))] // 替换 "YourClassName" 为包含 DownloadStringAsyncPost 方法的类名
[HarmonyPatch("DownloadStringAsyncPost")]
public class DownloadStringAsyncPostPatch
{
    [HarmonyPostfix] // 使用 HarmonyPostfix 注解来在方法执行后执行你的代码
    public static void Postfix(string __result) // 使用 Postfix 方法来接收返回值
    {
        Console.WriteLine("返回数据:" + __result);
    }

    [HarmonyPostfix] // 使用 HarmonyPostfix 注解来在方法执行后执行你的代码
    public static void Postfix(HttpWebRequest req, string json, string encoding) // 使用 Postfix 方法来接收参数
    {
        Console.WriteLine("请求参数 req:" + req);
        Console.WriteLine("请求参数 json:" + json);
        Console.WriteLine("请求参数 encoding:" + encoding);
    }
}

posted @ 2023-06-07 10:21  月渊  阅读(220)  评论(0编辑  收藏  举报