记录使用 Lib.Harmony 时注入过程中遇到的一个坑

1. 从这里开始

在一个无聊的下午,我偶然看到某个 Terraria 群里在谈论着一个叫 TerrariaHelper(自动钓鱼) 的软件,顿时有了兴致。我之前粗略研究过 FruitY(某个直接修改 Terraria 程序集代码而做出来的魔改端(作弊端),功能丰富) 的实现代码,但是今天看到的是一个独立的 Helper 程序,没有修改 Terraria 的源文件,也包含了自瞄,自动回血等等功能,不禁让人好奇这些功能是如何实现的。

我一开始猜想这可能类似于 CE(Cheat Engine) 那样,从内存的层面上去修改一些东西(感觉很复杂,没有研究过)。直到我偷偷地把它拖进了 dnSpy(一个反编译软件),短暂地分析代码后,得出的结果让我大吃一惊——居然使用了 Lib.Harmony(我接触过 Lib.Harmony 但是我只知道它是用来制作 Unity 游戏的 Mod 的),然后我才意识到 Lib.Harmony 功能强大,不仅仅是做 Unity 游戏 Mod 的工具。

2. 阅读 Lib.Harmony 的文档

因为 Lib.Harmony 没有多少中文资料(宵夜97制作过一系列教程),我们还是从看它的官网文档开始!

Harmony 2

Introduction

Harmony gives you an elegant(优雅的) and high level way to alter functionality(实用地更改) in applications written in C#. It does this at runtime by monkey patching methods unlike other solutions that change the content of dll files.

It supports Mono and .NET environments on Windows, Unix and macOS except when Unity uses the stripped down NetStandard profile (.NET 4.x profile works fine). Harmony is used in mainstream Unity games and many other applications.(不仅仅用于 Unity 游戏,还有其他的应用程序)

Designed to be used by multiple users (usually called Mods) that would otherwise override each others hooks, it was originally created for the game RimWorld and its large modding community by Andreas Pardeike.

Enjoy!
/Andreas Pardeike

Introduction

Harmony - a library for patching, replacing and decorating .NET methods during runtime.

上面的介绍提到了 Lib.Harmony 是一个跨平台的适用于 Mono 和 .NET 运行时环境的在程序运行时对方法进行 修补(patch),替换(replace) 和 装饰(decorate) 的类库。同时,尽管 Lib.Harmony 最初是为 RimWorld 而创造 且现在大多用于 Unity 游戏的 Mod 创作,但是它仍然适用于其他的符合要求的 .NET 应用程序。

Bootstrapping and Injection

Harmony does not provide you with a way to run your own code within an application that is not designed to execute foreign code. You need a way to inject at least the few lines that start the Harmony patching and this is usually done with a loader. Here are some common examples of l

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值