|
[分享]LUA协程和多任务笔记
外挂在游戏中有许多数据要读取 如果一个个读取很麻烦, 如果绑定为LUA对象就好用多了 下面是以Delhpi TList对象的绑定为例( Delphi 7) unit unitDelphi; { Tlist类 脚本调用 mir = require('luamir') list = mir.tlist(TList的指针) count = list.count for i = 1, count do print( list[i]) end find = list.indexof(list, AItem) --或使用:操作符 更直观 find = list:indexof(AItem) } interface uses SysUtils,StrUtils, Classes, lualib,luapackage; type TLuaDelphi = class(TLuaPackage) private function TList__index: integer;cdecl; function TList__indexof: integer;cdecl; published function TList: integer;cdecl; end; var List:TList; i:integer; function open_Delphi(L:lua_State): Integer; cdecl; implementation function open_Delphi(L:lua_State): Integer; cdecl; begin RegisterFunctions(L, TLuaDelphi); //用于测试TList if List = nil then List := TList.create; lua_pushinteger(L, integer(List)); lua_setfield(L, -2, 'testlist'); for i:=1 to 10 do begin List.Add(pointer(i*100)); end; // //Delphi TList类 luaL_newmetatable(L, 'Delphi.TList'); lua_pushcclosure(L, @TLuaDelphi.TList__index, 0); lua_setfield(L, -2, '__index'); end; { TLuaDelphi } function TLuaDelphi.TList: integer; var str:string; p : integer; begin p := lua_tointeger(self,1); lua_pushlightuserdata(self, pointer(p)); luaL_getmetatable(self, 'Delphi.TList'); lua_setmetatable(self, -2); result:=1; end; function TLuaDelphi.TList__index: integer; //堆栈List,Index var List: Classes.TList; Key : pAnsiChar; Index : integer; begin result := 1; try List := Classes.TList( lua_touserdata(self, 1)); Key := lua_tostring(self, 2); Index := lua_tointeger(self, 2); if (index>0) and (index<=List.Count) then begin lua_pushinteger(self, integer(List.Items[Index-1])); end else if AnsiStrIComp(Key, 'count')=0 then begin lua_pushinteger(self, List.Count); end else if AnsiStrIComp(Key, 'indexof')=0 then begin lua_pushcclosure(self, pointer(@TLuaDelphi.TList__indexof), 0); end else begin result := 0; end; except lua_pushstring(self, 'Trace List,Index,Key'); lua_pushinteger(self, integer(List)); lua_pushinteger(self, Index); lua_pushstring(self, Key); error(); result :=0 ; end end; //脚本调用 list:indexof(item) function TLuaDelphi.TList__indexof: integer; var List: Classes.TList; Item: Integer; begin try List := Classes.TList( lua_touserdata(self, 1)); Item := lua_tointeger( self, 2); lua_pushinteger(self, List.IndexOf(pointer(Item))+1); result:=1; except lua_pushstring(self, 'Trace List,Item'); lua_pushinteger(self, integer(List)); lua_pushinteger(self, Item); error(); result := 0; end; end; end. |
|
[分享]LUA协程和多任务笔记
因为原来用的头文件是5.1版的 开始没找到5.3版的, 准备自已编个 前天在坐长途车时, 找到一个 Dennis D. Spreen写好的一个例子 里面是用5.3.0版的, 正好可以用 原文是Delphi XE版的, 稍微改下就能用了 原文有2个文件VerySimple.Lua.Lib.pas, VerySimple.Lua.pas 我只用其中的一个VerySimple.Lua.Lib.pas 都放在压缩包里 |
|
[分享]LUA协程和多任务笔记
Delphi7 编写LUA 扩展库 因为Delphi7编写东西比较快, 所以做了下试验 C++虽也很好, 不过还不熟, 要边百度边写, 很慢, 网上找来的lua头文件, 是5.1的, 不适合5.3, 先用着 有空再改一个出来 目的: 生成一个mylib.dll的库,包含2个函数xx,yy 先把mylib.dll拷到lua目录, 使用方法: mylib=require('mylib') print( mylib.xx() ) -->输出 mylib.xx print( mylib.yy() ) -->输出 1818 实现方法: 新建DLL工程, 保存为mylib 添加单元,添加以下内容,编译即可 unit lualib_test; interface uses SysUtils, Classes, lua, luapas; //lua头文件 type TMyLib = class published //所有库函数定义在这里,才能注册到库 function xx: integer;cdecl; function yy: integer;cdecl; end; procedure RegisterFunctions(L:plua_State; Cls: TClass);//注册库函数 implementation function luaopen_mypas(L:plua_State): Integer; cdecl; begin luapas.lua_newtable(L); RegisterFunctions(L, TMyLib); result := 1; end; exports luaopen_mypas; //导出函数 //注册类的published方法到库, 假设table在栈顶 procedure RegisterFunctions(L:plua_State; Cls: TClass); type PPointer = ^Pointer; PMethodRec = ^TMethodRec; TMethodRec = packed record wSize: Word; pCode: Pointer; sName: ShortString; end; var MethodTable: PAnsiChar; MethodRec: PMethodRec; wCount: Word; nMethod: Integer; begin // Get a pointer to the class's published method table MethodTable := PAnsiChar(Pointer(PAnsiChar(Cls) + vmtMethodTable)^); if (MethodTable <> Nil) then begin // Get the count of the methods in the table Move(MethodTable^, wCount, 2); // Position the MethodRec pointer at the first method in the table // (skip over the 2-byte method count) MethodRec := PMethodRec(MethodTable + 2); // Iterate through all the published methods of this class for nMethod := 0 to wCount - 1 do begin // Add the method name to the lua functions // 名称:MethodRec.sName 地址:MethodRec.pcode lua_pushcclosure(L, MethodRec.pcode, 0); //地址 lua_setfield(L, -2, PAnsiChar(AnsiString(MethodRec.sName))); //名称 // Skip to the next method MethodRec := PMethodRec(PAnsiChar(MethodRec) + MethodRec.wSize); end; end; end; { mylib } function Tmylib.xx: integer; begin lua_pushstring(self, 'mylib.xx'); result:=1; end; function Tmylib.yy: integer; begin lua_pushinteger(self, 1818); result:=1; end; end. 如果要添加新的函数 只要在TMyLib类中添加新的函数,并编好实现代码即可, 函数放在published部分可以自动注册 放在其它部分要手动注册, 可以用以下代码 //table在栈顶 lua_pushcclosure(L, 函数地址, 0); lua_setfield(L, -2, 函数名称); |
|
[分享]LUA协程和多任务笔记
lua的C API 就是操作LUA栈, 很难计算, 不过编好了, 使用起来却时很方便的 我也算不来, 先写好实现的语句, 不管堆栈 运行时记下每处的堆栈变化, 再调整语句, 最后注释掉记录语句 这样比较省力, 也不会出错,如 s += " " + IntToStr(lua_gettop(L)); 某些操作 s += " " + IntToStr(lua_gettop(L)); 某些操作 s += " " + IntToStr(lua_gettop(L)); 某些操作 s += " " + IntToStr(lua_gettop(L)); 某些操作 print(s.c_str()); //打印出堆栈变化情况 在C中调用lua的print如下 static void print(char *msg) { lua_getglobal(::L, "print"); lua_pushstring(::L, msg); lua_call(::L, 1, 0);//1个参数,0个结果 } |
|
[分享]LUA协程和多任务笔记
0B0FC5C8 发 3013=自跑 12=X 17=Y 1=方向 0 0 nil 0B0FCC18 发 3010=自转 12=X 17=Y 3=方向 0 0 nil 0B0FD270 发 3013=自跑 12=X 19=Y 4=方向 0 0 nil 0B0FD88C 发 3013=自跑 10=X 21=Y 5=方向 0 0 nil 0B0FDEA8 发 3011=自走 9=X 22=Y 5=方向 0 0 nil 0B0FE4F4 发 3011=自走 9=X 23=Y 4=方向 0 0 nil 0B0FEB44 发 3011=自走 8=X 22=Y 7=方向 0 0 nil 0B0F06B8 发 1033 0 0 0 0 0 nil 0B0F0CF0 发 3011=自走 380=X 301=Y 5=方向 0 0 nil 0B0F1374 发 3013=自跑 382=X 303=Y 3=方向 0 0 nil 0B0F19F8 发 3013=自跑 382=X 305=Y 4=方向 0 0 nil 0B0F2048 发 3011=自走 381=X 305=Y 6=方向 0 0 nil 0B0F2698 发 3013=自跑 381=X 307=Y 4=方向 0 0 nil 0B0C9878 发 3011=自走 381=X 308=Y 4=方向 0 0 nil 0B0E37D4 发 3013=自跑 383=X 310=Y 3=方向 0 0 nil 0B0F7F94 发 3013=自跑 385=X 308=Y 1=方向 0 0 nil 0B0EE3FC 发 3013=自跑 385=X 306=Y 0=方向 0 0 nil 0B0CF590 发 3013=自跑 383=X 304=Y 7=方向 0 0 nil 0B0CFBE8 发 1010 116604992 0 0 0 0 nil 0B0CB988 发 1011=NPC命令 116604992=NPC 0 0 0 8 @goback 0B0C80C0 发 1033 0 0 0 0 0 nil 0B0C86F8 发 1010 117189504 0 0 0 0 nil 0B0D8540 发 1011=NPC命令 117189504=NPC 0 0 0 8 @gostep 0B0D40D8 发 1033 0 0 0 0 0 nil 0B0D46F4 发 3013=自跑 331=X 333=Y 7=方向 0 0 nil 0B0D4D78 发 3013=自跑 329=X 331=Y 7=方向 0 0 nil 0B0D53E4 发 1010 117189504 0 0 0 0 nil 0B0D5A00 发 1011=NPC命令 117189504=NPC 0 0 0 9 @godrink 0B0D607C 发 1033 0 0 0 0 0 nil 0B0DE05C 发 1010 116604992 0 0 0 0 nil 0B0DE678 发 1011=NPC命令 116604992=NPC 0 0 0 9 |
|
[分享]LUA协程和多任务笔记
实际使用效果 0B0E2AE4 收 1107 681879936 16 0 0 0 nil 0B0E3860 收 17=人? 598938656=角色 13=X 16 15=Y 8 0B0E3EA0 收 638=人?开盾 598938656=角色 13=X 4100 15=Y 8 ? 0B0E44E0 收 17=人? 610322560=角色 10=X 29 18=Y 8 0B0E4B20 收 657=人? 610322560=角色 0 0 24 0 nil 0B0E5028 收 638=人?开盾 610322560=角色 10=X 7428 18=Y 8 0B0D907C 收 10=转 427863328=角色 15=X 0=方向 16=Y 8 0B0D1188 收 11=走 569500128=角色 13=X 3=方向 18=Y 8 0B0CE928 收 13=跑 536616704=角色 10=X 6=方向 20=Y 8 0B0D9E7C 收 13=跑 536616704=角色 8=X 5=方向 22=Y 8 0B0DA53C 收 30=人没 536616704=角色 0 0 0 0 nil 0B0DAB88 收 30=人没 442646400=角色 0 0 0 0 nil 0B0DB1E4 收 17=人? 440465568=角色 8=X 16 21=Y 8 0B0DB8E8 收 17=人? 598938656=角色 13=X 16 15=Y 8 0B0DBF88 收 638=人?开盾 598938656=角色 13=X 4100 15=Y 8 ? 0B0DC600 收 10=转 681895344=角色 14=X 4=方向 14=Y 17 0B0DCD08 收 42=称号 681895344=角色 255 0 0 5 入入 0B0DD3B0 收 10=转 681895344=角色 14=X 4=方向 14=Y 17 0B0DD9F0 收 2873=人? 681895344=角色 14=X 4=方向 14=Y 0 nil |
|
[分享]LUA协程和多任务笔记
--C中实现协程 --以脚务器消息接收为例 --编写一个消息处理的函数Lua(伪代码) function Recv(Recog, Ident, Param, Tag, Series, buf, len) --if 定义[Ident] and 定义[Ident].过滤==1 then return end; print('收',翻译(Ident,Recog,Param,Series,Tag,len,buf)) mylib.delay(5000) --print('协程结束') 回收协程() end --编写一个消息转发Lua的C函数 相当于Lua下列语句,挂到消息接收上 static _stdcall void SocketRecvHook(int buf, int len) local co=coroutine.create(recv) ...转化内存数据到变量 coroutine.resume(co, Recog, Ident, Param, Tag, Series, buf, len) 库名.copool=co //必须, 不然协程被当垃圾回收出错, 也可以留在栈里 --如此每收到1个消息就开启1个协程, 所以协程数量增长很快, --如果协程运行结束, 就要进行回收, 即可在C中编写,也可在LUA中 function 回收协程() local 总数=0 local 回收=0 for i in pairs(mir.copool) do 总数 = 总数+1 if coroutine.status(mir.copool[i])=='dead' then mir.copool[i]=nil --垃圾回收器会自动回收该协程 回收=回收+1 end end --if 回收>0 then print('协程总数,回收,剩余=',总数,回收,总数-回收) end end --C代码如下 static _stdcall void SocketRecvHook(int buf, int len) { //调用原来的程序 OrgSocketRecvDecode(buf, len); //调用LUA程序 PTDefaultMessage pm = (PTDefaultMessage)(buf+4); //co=coroutine.create(recv) lua_getglobal(L, "coroutine"); //--->+1 lua_getfield(L, -1, "create"); //调用函数coroutine.create//--->+2 //获得self.recv lua_pushlightuserdata(L, (void*)&Key); //压入地址 //--->+3 lua_gettable(L, LUA_REGISTRYINDEX);//table在栈顶 //--->+4 lua_getfield(L, -1, "recv"); //self.Recv //--->+5 lua_remove(L, -2);//移除table //--->+4 if (lua_isfunction(L, -1)) { lua_call(L, 1, 1);//1个参数,1个结果->co //--->+2 //库名.copool=co savepool(IntToStr(GetTickCount())+"_R_"+IntToStr(pm->Ident) ); //coroutine.resume(co,参数) lua_getglobal(L, "coroutine"); lua_getfield(L, -1, "resume"); //调用函数coroutine.resume lua_pushvalue(L, -3);//复制co lua_pushinteger(L, pm->Recog);//1 lua_pushinteger(L, pm->Ident);//2 lua_pushinteger(L, pm->Param);//3 lua_pushinteger(L, pm->Tag);//4 lua_pushinteger(L, pm->Series);//5 lua_pushinteger(L, buf + 16);//6 lua_pushinteger(L, len - 16);//7 lua_call(L, 1+7, 0);//1+7个参数,0个结果->co lua_pop(L, 3); } else { lua_pop(L, 3); } } |
|
[分享]C++Build共享内存段测试成功
平时不大看书,书买来时粗略看下,有兴趣的部分, 会实践一下。 实体书和电子书,收了较多 C,Delphi,C++,VB,汇编,IDA,LUA,VC, ,数据库,破解,驱动,逆向,外挂,SQL VBA,ASP,PHP,Pascal,BC,windows API, 单片机,反正杂七杂八,基至有Fortran 在项目需要时才会去找来看 |
|
[求助]我现在站在十字路口,大神们请指点!
感觉要学习的东西太多, 干脆不学 从做开始, 先有个想法, 如何实现, 再上网找, 不懂再找, 再翻书 虽然很慢, 也能做点东西出来 如果等学好了(也不可能学好), 再做, 也许太长时间了 我是用我玩的游戏当试验, 做一个外挂, 慢慢加入功能 觉得LUA脚本比较好, 给外挂添加了脚本功能, 再给LUA添加一些DLL模块, 外挂给身边朋友用用, 不准外传,从没想过要靠这个赚钱 OD,IDA,delphi,C++builder,VC6,VB5都有接触不精 编代码一直记不住语法, VB,C,delphi,lua的语法有差别 经常搞混, 函数更记不住, 都是边查边写 |
|
|
|
[分享]IDA Pro权威指南(第二版)高清印前PDF
下载收藏,谢谢 |
|
小经验: 如果修改了Win7 64位系统内核, 一定要关掉PatchGuard,不然系统经常蓝屏
完全按原文的方法, 电脑重启, 从新的入口(默认项)启动. 从昨天16点到现在, 29小时了, 游戏开了20-30个号做任务测试都挂正常 |
操作理由
RANk
{{ user_info.golds == '' ? 0 : user_info.golds }}
雪币
{{ experience }}
课程经验
{{ score }}
学习收益
{{study_duration_fmt}}
学习时长
基本信息
荣誉称号:
{{ honorary_title }}
能力排名:
No.{{ rank_num }}
等 级:
LV{{ rank_lv-100 }}
活跃值:
在线值:
浏览人数:{{ visits }}
最近活跃:{{ last_active_time }}
注册时间:{{ user_info.create_date_jsonfmt }}
勋章
兑换勋章
证书
证书查询 >
能力值