using Celeste;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using static System.Net.WebRequestMethods;namespace ClassLibrary1
{public class Class1{public static int EntryPoint(string arg){//加载hook libSystem.IO.File.AppendAllText("E:\\Celeste\\log.txt", "----------------start log----------------\r\n");var harmony = new Harmony("com.example.patch");harmony.PatchAll();return 0;}//hook 发送数据的类和方法[HarmonyPatch(typeof(Steamworks.InteropHelp))] //类[HarmonyPatch("TestIfAvailableClient")] //方法,防止写错尽量用nameof()class Patch01{//Prefix返回一个bool,如果false,则不执行后续Prefix,不执行原始方法[HarmonyPrefix]static bool Prefix(){System.IO.File.AppendAllText("E:\\Celeste\\log.txt", "TestIfAvailableClient\r\n");return false;}Postfix永远执行//[HarmonyPostfix]//static void Postfix(ref int __result) //__result表示Hook方法的返回值//{// __result = 0;//}}//hook 发送数据的类和方法[HarmonyPatch(typeof(Steamworks.SteamUserStats))] //类[HarmonyPatch("RequestCurrentStats")] //方法,防止写错尽量用nameof()class Patch02{//Prefix返回一个bool,如果false,则不执行后续Prefix,不执行原始方法[HarmonyPrefix]static bool Prefix(){//Achievements.Register(Achievement.PICO8);System.IO.File.AppendAllText("E:\\Celeste\\log.txt", "RequestCurrentStats\r\n");return false;}//Postfix永远执行[HarmonyPostfix]static void Postfix(ref bool __result) //__result表示Hook方法的返回值{__result = false;}}//hook 发送数据的类和方法[HarmonyPatch(typeof(Steamworks.SteamUserStats))] //类[HarmonyPatch("RequestGlobalStats")] //方法,防止写错尽量用nameof()class Patch03{//Prefix返回一个bool,如果false,则不执行后续Prefix,不执行原始方法[HarmonyPrefix]static bool Prefix(){System.IO.File.AppendAllText("E:\\Celeste\\log.txt", "RequestGlobalStats\r\n");return false;}}//hook 发送数据的类和方法[HarmonyPatch(typeof(Steamworks.SteamUserStats))] //类[HarmonyPatch("GetAchievement")] //方法,防止写错尽量用nameof()class Patch04{//Prefix返回一个bool,如果false,则不执行后续Prefix,不执行原始方法[HarmonyPrefix]static bool Prefix(){System.IO.File.AppendAllText("E:\\Celeste\\log.txt", "GetAchievement\r\n");return false;}//Postfix永远执行[HarmonyPostfix]static void Postfix(ref bool __result) //__result表示Hook方法的返回值{__result = false;}}//hook 发送数据的类和方法[HarmonyPatch(typeof(Steamworks.SteamUserStats))] //类[HarmonyPatch("SetAchievement")] //方法,防止写错尽量用nameof()class Patch05{//Prefix返回一个bool,如果false,则不执行后续Prefix,不执行原始方法[HarmonyPrefix]static bool Prefix(){System.IO.File.AppendAllText("E:\\Celeste\\log.txt", "SetAchievement\r\n");return false;}}//不知道为什么这个不能进行hookhook 发送数据的类和方法//[HarmonyPatch(typeof(Celeste.Achievements))] //类//[HarmonyPatch("Has")] //方法,防止写错尽量用nameof()//class Patch06//{// //Prefix返回一个bool,如果false,则不执行后续Prefix,不执行原始方法// [HarmonyPrefix]// static bool Prefix()// {// System.IO.File.AppendAllText("E:\\Celeste\\log.txt", "Has\r\n");// return false;// }// //Postfix永远执行// [HarmonyPostfix]// static void Postfix(ref bool __result) //__result表示Hook方法的返回值// {// __result = true;// }//}}
}
注入代码如下
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <mscoree.h>
#include <metahost.h>
#include <assert.h>#define BUFFER_SIZE 500
typedef long (__stdcall *CLRCreateInstance_Des)(REFCLSID clsid, REFIID riid, /*iid_is(riid)*/ LPVOID* ppInterface);static CLRCreateInstance_Des CLRCreateInstance_Fun;void StartTheDotNetRuntime(LPCWSTR runtimeVersion, LPCWSTR dllPath, LPCWSTR startClass, LPCWSTR startMethod, LPCWSTR startArgument)
{ICLRMetaHost* pMetaHost = NULL;ICLRMetaHostPolicy* pMetaHostPolicy = NULL;ICLRDebugging* pCLRDebugging = NULL;CLRCreateInstance_Fun(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost);CLRCreateInstance_Fun(CLSID_CLRMetaHostPolicy, IID_ICLRMetaHostPolicy, (LPVOID*)&pMetaHostPolicy);CLRCreateInstance_Fun(CLSID_CLRDebugging, IID_ICLRDebugging, (LPVOID*)&pCLRDebugging);DWORD dwVersion = 0;DWORD dwImageVersion = 0;ICLRRuntimeInfo* pRuntimeInfo;HRESULT result;result = pMetaHost->GetRuntime(runtimeVersion, IID_ICLRRuntimeInfo, (LPVOID*)&pRuntimeInfo);assert(SUCCEEDED(result));ICLRRuntimeHost* pRuntimeHost = NULL;result = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*)&pRuntimeHost);assert(SUCCEEDED(result));result = pRuntimeHost->Start();assert(SUCCEEDED(result));DWORD dwRetCode = 0;result = pRuntimeHost->ExecuteInDefaultAppDomain(dllPath, startClass, startMethod, startArgument, &dwRetCode);assert(SUCCEEDED(result));pRuntimeHost->Stop();pRuntimeHost->Release();pRuntimeInfo->Release();pCLRDebugging->Release();pMetaHostPolicy->Release();pMetaHost->Release();
}extern "C" void Loader()
{HMODULE hValve = LoadLibrary(L"mscoree.dll");if (hValve!=NULL) {CLRCreateInstance_Fun = (CLRCreateInstance_Des)GetProcAddress((HMODULE)hValve, "CLRCreateInstance");if (CLRCreateInstance_Fun != nullptr) {StartTheDotNetRuntime(L"v4.0.30319", L"ClassLibrary1.dll", L"ClassLibrary1.Class1", L"EntryPoint", L"");}}}