在Tolua中實現Xlua IL注入的熱更方式

2021-10-01 23:06:34 字數 2771 閱讀 2610

比如你有這麼乙個充值的方法

public void getmoney(int money)  萬元", money));

}

在todo之前我們來了解下xlua熱更的原理,很簡單,大概就是在開始的時候加入了乙個hotfix委託檢測的注入,我這裡就用c#**表示了

public void getmoney(int money)  萬元", money));

}

如果這個function出了問題,比如臨時需要關閉充值入口,那麼在lua層會利用xlua.hotfix把這個luafuntion定位到hotfix_getmoney 中

let's do that ! 

注:本文實現方式與xlua有所不同

首先,加入hotfix特性

[attributeusage(attributetargets.all)]

public class hotfix : attribute

然後加入hotfix class名單設定支援

namespace m.luahotfix ;

public static string _gt(type type)}}

新增il注入選單,搜尋dll查詢hotfix的方法

public static void inject()

var readerparameters = new readerparameters ;

var assembly = assemblydefinition.readassembly(assemblypath, readerparameters);

var module = assembly.mainmodule;

//遍歷所有的型別

foreach (var type in module.types)

//遍歷所有的method,進行注入

foreach (var method in type.methods)

}assembly.write(assemblypath, new writerparameters );

debug.log("injecttool inject success!");

}

之後就是注入il**了,我們在整個方法執行前做乙個是否需要hotfix的檢測

private static void injectmethod(moduledefinition module,typedefinition type, methoddefinition method)

));module.importreference(typeof(hotfixmanager).getmethod("hotfixdetector", new ));

var insertpoint = method.body.instructions[0];

var ilprocessor = method.body.getilprocessor();

var paramcount = method.parameters.count + (method.isstatic ? 0 : 1);

//開始注入

ilprocessor.insertbefore(insertpoint, ilprocessor.create(opcodes.nop));

ilprocessor.insertbefore(insertpoint, ilprocessor.create(opcodes.ldstr, type.fullname));

ilprocessor.insertbefore(insertpoint, ilprocessor.create(opcodes.ldstr, method.name));

ilprocessor.insertbefore(insertpoint, ilprocessor.create(opcodes.ldc_i4, paramcount));

ilprocessor.insertbefore(insertpoint, ilprocessor.create(opcodes.newarr, objtype));

//將原本的引數壓入函式堆疊

for (int i = 0; i < paramcount; i++)

else

if (paramtype.isvaluetype)

ilprocessor.insertbefore(insertpoint, ilprocessor.create(opcodes.stelem_ref));

}//call hotfix 攔截原方法

ilprocessor.insertbefore(insertpoint, ilprocessor.create(opcodes.call, hotfixdetector));

ilprocessor.insertbefore(insertpoint, ilprocessor.create(opcodes.b***lse, insertpoint));

ilprocessor.insertbefore(insertpoint, ilprocessor.create(opcodes.ret));

}

然後接tolua,這個不用贅述

public static bool hotfixdetector(string classfullname, string funcname, object args)

執行原本的**是這樣的:

注入了lua**之後是

Tolua 實現分析

tolua 是乙個將 c c 的函式和物件匯出給 lua 指令碼使用的工具。使用這個工具的基本步驟 每個目標檔案都是從一系列 pkg 檔案編譯而來,主要完成下列功能 不管是 c 函式還是 c 物件的方法,都一律匯出為靜態函式。c 函式的匯出形式如下 123 4567 891011 1213 1415...

Tolua實現Update功能

第一步 在元件上,掛載 lualooper 指令碼 這是驅動tolua裡面update 第二步 找到tolua 裡面的event 指令碼 把下面 複製進來 function event add func,obj local events updatebeat createlistener func,...

使用tolua 實現C 與LUA相互呼叫

lua是一種目前很流行的高效精簡的指令碼語言。lua乙個特點是比較方便的與c通訊。然而要在指令碼中使用c 類使用基本的lua方法還是比較麻煩,純手工暴露乙個類的介面到lua工作量還是很大的,而且都是一些簡單的重複勞動。好在有tolua 這個工具,可以讓程式設計師從簡單的重複勞動解脫出來。tolua ...