实现原理:修改游戏文件
注入机器人
使用 MonoCecil,修改 Unity3D 生成的 Assembly 文件,在内部注入我们自己编写的Unity3D模块!
实现细节:修改游戏文件
其实主要是 MonoCecil 的使用
通过文件名、类名、方法名,取出方法的定义
static MethodDefinition fetch_method(string file, string type, string method)
{
// find hook method
try
{
AssemblyDefinition ad = AssemblyDefinition.ReadAssembly(file);
TypeDefinition td = null;
foreach (TypeDefinition t in ad.MainModule.Types)
{
if (t.Name == type)
{
td = t;
break;
}
}
if (td == null) return null;
MethodDefinition md = null;
foreach (MethodDefinition t in td.Methods)
{
if (t.Name == method)
{
md = t;
break;
}
}
return md;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return null;
}
}
通过文件名和方法定义实现注入
static AssemblyDefinition inject_method(string file, MethodDefinition method, MethodDefinition method_tobe_inject)
{
try
{
AssemblyDefinition ad = AssemblyDefinition.ReadAssembly(file);
ILProcessor ilp = method.Body.GetILProcessor();
Instruction ins_first = ilp.Body.Instructions[0];
Instruction ins = ilp.Create(OpCodes.Call, ad.MainModule.Import(method_tobe_inject.Resolve()));
ilp.InsertBefore(ins_first, ins);
return ad;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return null;
}
}
注入完了之后,利用
AssemblyDefinition.Write();
方法,把修改后的Assembly再写到一个文件里。
实现原理一:游戏状态获取
单例类
HearthStone有几个重要的类,如SceneMgr、MulliganManager、GameState、InputManager等等。
单例,直接调用Get()方法就可以了!
状态的识别
通常来说,可以使用SceneMgr取得当前的场景情况,可以使用GameState获得当前对战的游戏状态。两者一结合,基本就是整个游戏的状态的了。
通过状态就可以知道哪些类当前是有实例的,哪些是没有的。
实现原理二:场上卡牌的数据
GameState可以获得当前的两个玩家Player,而Player可以活动当前的卡牌,如Player.GetHandZone().GetCards()取得手上的卡牌。
实现原理三:机器人自动执行
首先,通过分析场上的数据就可以算出该怎么走牌,这是AI的部分,我只写了个有啥走啥的简单AI。
我们代码是使用组建的方式注册在游戏里的,会被定时调用,调用的时候,进行操作(如走牌)然后及时返回即可。
实现原理四:走牌和攻击
走牌和攻击的实现,虽然我能够完全模仿客户端逻辑,但是总觉得不太靠谱,因为客户端更新,我可能没发现,就会出错。因此我走牌和攻击的实现是直接调用了InputManager。
InputManager包含了一些方法,用于处理鼠标和键盘事件,我直接调用这些函数。
当然这么做的缺陷也是很明显的:InputManager的能力范围有限,并且我不能组织InputManager不去处理一些事件。比如当鼠标滑出窗口时,InputManager默认会把牌放回手里。