替同事做了个洛奇英雄传自动染色程序,关于屏幕取色的.因为里面他对颜色的要求比较复杂,改动也比较大,于是我让他把逻辑写在 lua 脚本里面.
- function lua_CheckColor(r,g,b:Integer):Boolean;
- var
- Lua : TLua;
- begin
- Lua := TLua.Create;
- luaopen_debug(LuaInstance);
- luaopen_math(LuaInstance);
- luaopen_os(LuaInstance);
- luaopen_string(LuaInstance);
- luaopen_table(LuaInstance);
- Lua.DoFile('lua_GetColor.lua');
- lua_getglobal(Lua.LuaInstance,'nogi_GetColor');
- lua_pushnumber(Lua.LuaInstance, r);
- lua_pushnumber(Lua.LuaInstance, g);
- lua_pushnumber(Lua.LuaInstance, b);
-
-
- lua_pcall(Lua.LuaInstance, 3, 1,0) ;
-
-
- Result := (lua_toInteger(Lua.LuaInstance,-1) = 1);
- Lua.Free;
- end;
LUA 里面的内容是这样的
- function nogi_GetColor(nR,nG,nB)
- if nR <= 25 and nG <= 25 and nB <= 25 then -- 取出25以下黑色
- return 1;
- end;
-
- return 0;
- end
附上我这里带的 LUA.PAS 和 LUALIB.PAS
lua.pas
-
- unit Lua;
-
- interface
-
- uses
- Classes,
- LuaLib;
-
- type
- TLuaState = Lua_State;
-
- TLua = class(TObject)
- private
- fAutoRegister: Boolean;
- CallbackList: TList;
- public
- LuaInstance: TLuaState;
- constructor Create(AutoRegister: Boolean = True); overload; virtual;
- destructor Destroy; override;
- function DoFile(Filename: String): Integer; virtual;
- procedure RegisterFunction(FuncName: AnsiString; MethodName: AnsiString = ''; Obj: TObject = NIL); virtual;
- procedure AutoRegisterFunctions(Obj: TObject);
- procedure UnregisterFunctions(Obj: TObject);
- end;
-
- implementation
-
- type
- TProc = function(L: TLuaState): Integer of object;
-
- TCallback = class
- Routine: TMethod;
- Exec: TProc;
- end;
-
- function LuaCallBack(L: Lua_State): Integer; cdecl;
- var
- CallBack: TCallBack;
- begin
-
- CallBack := lua_topointer(L, lua_upvalueindex(1));
-
-
- if (assigned(CallBack) and assigned(CallBack.Exec)) then
- Result := CallBack.Exec(L)
- else
- Result := 0;
- end;
-
-
- constructor TLua.Create(AutoRegister: Boolean = True);
- begin
- inherited Create;
-
- if (not LuaLibLoaded) then
- LoadLuaLib;
-
-
- LuaInstance := Lua_Open();
- luaopen_base(LuaInstance);
-
- fAutoRegister := AutoRegister;
-
-
- CallBackList := TList.Create;
-
-
- if (AutoRegister) then
- AutoRegisterFunctions(self);
- end;
-
- destructor TLua.Destroy;
- begin
-
- if (fAutoRegister) then
- UnregisterFunctions(Self);
-
-
- CallBackList.Free;
-
-
- Lua_Close(LuaInstance);
- inherited;
- end;
-
- function TLua.DoFile(Filename: String): Integer;
- begin
- Result := lual_dofile(LuaInstance, PAnsiChar(AnsiString(Filename)));
- end;
-
- procedure TLua.RegisterFunction(FuncName: AnsiString; MethodName: AnsiString = ''; Obj: TObject = NIL);
- var
- CallBack: TCallBack;
- begin
-
- if (MethodName = '') then
- MethodName := FuncName;
-
-
- if (Obj = NIL) then
- Obj := Self;
-
-
- CallBack := TCallBack.Create;
- CallBack.Routine.Data := Obj;
- CallBack.Routine.Code := Obj.MethodAddress(String(MethodName));
- CallBack.Exec := TProc(CallBack.Routine);
- CallbackList.Add(CallBack);
-
-
- lua_pushstring(LuaInstance, PAnsiChar(FuncName));
-
-
- lua_pushlightuserdata(LuaInstance, CallBack);
-
-
- lua_pushcclosure(LuaInstance, LuaCallBack, 1);
- lua_settable(LuaInstance, LUA_GLOBALSINDEX);
- end;
-
- procedure TLua.UnregisterFunctions(Obj: TObject);
- var
- I: Integer;
- CallBack: TCallBack;
- begin
-
- for I := CallBackList.Count downto 1 do
- begin
- CallBack := CallBackList[I-1];
- if (assigned(CallBack)) and (CallBack.Routine.Data = Obj) then
- begin
- CallBack.Free;
- CallBackList.Items[I-1] := NIL;
- CallBackList.Delete(I-1);
- end;
- end;
- end;
-
- procedure TLua.AutoRegisterFunctions(Obj: TObject);
- 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
-
- MethodTable := PAnsiChar(Pointer(PAnsiChar(Obj.ClassType) + vmtMethodTable)^);
-
- if (MethodTable <> Nil) then
- begin
-
- Move(MethodTable^, wCount, 2);
-
-
-
- MethodRec := PMethodRec(MethodTable + 2);
-
-
- for nMethod := 0 to wCount - 1 do
- begin
-
- RegisterFunction(MethodRec.sName, MethodRec.sName, Obj);
-
- MethodRec := PMethodRec(PAnsiChar(MethodRec) + MethodRec.wSize);
- end;
- end;
- end;
-
-
- end.
lualib.pas
- unit LuaLib;
-
- interface
-
- const
- LUA_VERSION = 'Lua 5.1';
- LUA_RELEASE = 'Lua 5.1.2';
- LUA_COPYRIGHT = 'Copyright (C) 1994-2004 Tecgraf, PUC-Rio';
- LUA_AUTHORS = 'R. Ierusalimschy, L. H. de Figueiredo & W. Celes';
-
- LUA_PASCAL_51_AUTHOR = 'Marco Antonio Abreu';
- LUA_PASCAL_51_COPYRIGHT = 'Copyright (C) 2007 Marco Antonio Abreu';
-
-
- LUA_SIGNATURE = #27'Lua';
-
-
- LUA_MULTRET = -1;
-
-
- LUA_REGISTRYINDEX = -10000;
- LUA_ENVIRONINDEX = -10001;
- LUA_GLOBALSINDEX = -10002;
-
-
- LUA_TRD_YIELD = 1;
- LUA_ERRRUN = 2;
- LUA_ERRSYNTAX = 3;
- LUA_ERRMEM = 4;
- LUA_ERRERR = 5;
-
-
- LUA_ERRFILE = LUA_ERRERR + 1;
-
-
- LUA_TNONE = -1;
- LUA_TNIL = 0;
- LUA_TBOOLEAN = 1;
- LUA_TLIGHTUSERDATA = 2;
- LUA_TNUMBER = 3;
- LUA_TSTRING = 4;
- LUA_TTABLE = 5;
- LUA_TFUNCTION = 6;
- LUA_TUSERDATA = 7;
- LUA_TTHREAD = 8;
-
-
- LUA_MINSTACK = 20;
-
-
- LUA_GCSTOP = 0;
- LUA_GCRESTART = 1;
- LUA_GCCOLLECT = 2;
- LUA_GCCOUNT = 3;
- LUA_GCCOUNTB = 4;
- LUA_GCSTEP = 5;
- LUA_GCSETPAUSE = 6;
- LUA_GCSETSTEPMUL = 7;
-
-
-
-
- LUA_HOOKCALL = 0;
- LUA_HOOKRET = 1;
- LUA_HOOKLINE = 2;
- LUA_HOOKCOUNT = 3;
- LUA_HOOKTAILRET = 4;
-
-
- LUA_MASKCALL = (1 shl LUA_HOOKCALL);
- LUA_MASKRET = (1 shl LUA_HOOKRET);
- LUA_MASKLINE = (1 shl LUA_HOOKLINE);
- LUA_MASKCOUNT = (1 shl LUA_HOOKCOUNT);
-
-
-
-
- LUA_NUMBER_SCAN = '%lf';
- LUA_NUMBER_FMT = '%.14g';
- LUAI_MAXNUMBER2STR = 32;
-
-
- LUA_NOREF = -2;
- LUA_REFNIL = -1;
-
- LUA_IDSIZE = 60;
-
-
- LUA_COLIBNAME = 'coroutine';
- LUA_TABLIBNAME = 'table';
- LUA_IOLIBNAME = 'io';
- LUA_OSLIBNAME = 'os';
- LUA_STRLIBNAME = 'string';
- LUA_MATHLIBNAME = 'math';
- LUA_DBLIBNAME = 'debug';
- LUA_LOADLIBNAME = 'package';
-
-
-
- BUFSIZ = 512;
- LUAL_BUFFERSIZE = BUFSIZ;
-
- type
- lua_State = type Pointer;
-
- lua_CFunction = function(L: lua_State): Integer; cdecl;
-
-
- lua_Reader = function(L: lua_State; data: Pointer; var size: Cardinal): PAnsiChar; cdecl;
- lua_Writer = function(L: lua_State; p: Pointer; sz: Cardinal; ud: Pointer): Integer; cdecl;
-
-
- lua_Alloc = function(ud, ptr: Pointer; osize, nsize: Cardinal): Pointer; cdecl;
-
-
- lua_Number = type double;
-
- lua_Integer = type integer;
-
- lua_Debug = packed record
- event: Integer;
- name: PAnsiChar;
- namewhat: PAnsiChar;
- what: PAnsiChar;
- source: PAnsiChar;
- currentline: Integer;
- nups: Integer;
- linedefined: Integer;
- lastlinedefine: Integer;
- short_src: array[0..LUA_IDSIZE - 1] of AnsiChar;
-
- i_ci: Integer;
- end;
-
-
- lua_Hook = procedure(L: lua_State; var ar: lua_Debug); cdecl;
-
-
- PluaL_reg = ^luaL_reg;
- luaL_reg = packed record
- name: PAnsiChar;
- func: lua_CFunction;
- end;
-
-
-
- luaL_Buffer = packed record
- p: PAnsiChar;
- lvl: Integer;
- L: lua_State;
- buffer: array[0..LUAL_BUFFERSIZE - 1] of AnsiChar;
- end;
-
- var
-
- lua_newstate: function(f: lua_Alloc; ud: Pointer): lua_State; cdecl;
- lua_close: procedure(L: lua_State); cdecl;
- lua_newthread: function(L: lua_State): lua_State; cdecl;
- lua_atpanic: function(L: lua_State; panicf: lua_CFunction): lua_CFunction; cdecl;
-
-
- lua_gettop: function(L: lua_State): Integer; cdecl;
- lua_settop: procedure(L: lua_State; idx: Integer); cdecl;
- lua_pushvalue: procedure(L: lua_State; idx: Integer); cdecl;
- lua_remove: procedure(L: lua_State; idx: Integer); cdecl;
- lua_insert: procedure(L: lua_State; idx: Integer); cdecl;
- lua_replace: procedure(L: lua_State; idx: Integer); cdecl;
- lua_checkstack: function(L: lua_State; extra: Integer): LongBool; cdecl;
- lua_xmove: procedure(from, dest: lua_State; n: Integer); cdecl;
-
-
-
- lua_isnumber: function(L: lua_State; idx: Integer): LongBool; cdecl;
- lua_isstring: function(L: lua_State; idx: Integer): LongBool; cdecl;
- lua_iscfunction: function(L: lua_State; idx: Integer): LongBool; cdecl;
- lua_isuserdata: function(L: lua_State; idx: Integer): LongBool; cdecl;
- lua_type: function(L: lua_State; idx: Integer): Integer; cdecl;
- lua_typename: function(L: lua_State; tp: Integer): PAnsiChar; cdecl;
-
- lua_equal: function(L: lua_State; idx1, idx2: Integer): LongBool; cdecl;
- lua_rawequal: function(L: lua_State; idx1, idx2: Integer): LongBool; cdecl;
- lua_lessthan: function(L: lua_State; idx1, idx2: Integer): LongBool; cdecl;
-
- lua_tonumber: function(L: lua_State; idx: Integer): lua_Number; cdecl;
- lua_tointeger: function(L: lua_State; idx: Integer): lua_Integer; cdecl;
- lua_toboolean: function(L: lua_State; idx: Integer): LongBool; cdecl;
- lua_tolstring: function(L: lua_State; idx: Integer; var len: Cardinal): PAnsiChar; cdecl;
- lua_objlen: function(L: lua_State; idx: Integer): Cardinal; cdecl;
- lua_tocfunction: function(L: lua_State; idx: Integer): lua_CFunction; cdecl;
- lua_touserdata: function(L: lua_State; idx: Integer): Pointer; cdecl;
- lua_tothread: function(L: lua_State; idx: Integer): lua_State; cdecl;
- lua_topointer: function(L: lua_State; idx: Integer): Pointer; cdecl;
-
-
- lua_pushnil: procedure(L: lua_State); cdecl;
- lua_pushnumber: procedure(L: lua_State; n: lua_Number); cdecl;
- lua_pushinteger: procedure(L: lua_State; n: lua_Integer); cdecl;
- lua_pushlstring: procedure(L: lua_State; s: PAnsiChar; len: Cardinal); cdecl;
- lua_pushstring: procedure(L: lua_State; s: PAnsiChar); cdecl;
- lua_pushvfstring: function(L: lua_State; fmt, argp: PAnsiChar): PAnsiChar; cdecl;
-
- lua_pushfstring: function(L: lua_State; fmt: PAnsiChar; args: array of const): PAnsiChar; cdecl;
- lua_pushcclosure: procedure(L: lua_State; fn: lua_CFunction; n: Integer); cdecl;
- lua_pushboolean: procedure(L: lua_State; b: LongBool); cdecl;
- lua_pushlightuserdata: procedure(L: lua_State; p: Pointer); cdecl;
- lua_pushthread: function(L: lua_State): Integer; cdecl;
-
-
- lua_gettable: procedure(L: lua_State; idx: Integer); cdecl;
- lua_getfield: procedure(L: lua_State; idx: Integer; k: PAnsiChar); cdecl;
- lua_rawget: procedure(L: lua_State; idx: Integer); cdecl;
- lua_rawgeti: procedure(L: lua_State; idx, n: Integer); cdecl;
- lua_createtable: procedure(L: lua_State; narr, nrec: Integer); cdecl;
- lua_newuserdata: function(L: lua_State; size: Cardinal): Pointer; cdecl;
- lua_getmetatable: function(L: lua_State; idx: Integer): LongBool; cdecl;
- lua_getfenv: procedure(L: lua_State; idx: Integer); cdecl;
-
-
- lua_settable: procedure(L: lua_State; idx: Integer); cdecl;
- lua_setfield: procedure(L: lua_State; idx: Integer; k: PAnsiChar ); cdecl;
- lua_rawset: procedure(L: lua_State; idx: Integer); cdecl;
- lua_rawseti: procedure(L: lua_State; idx, n: Integer); cdecl;
- lua_setmetatable: function(L: lua_State; idx: Integer): LongBool; cdecl;
- lua_setfenv: function(L: lua_State; idx: Integer): LongBool; cdecl;
-
-
- lua_call: procedure(L: lua_State; nargs, nresults: Integer); cdecl;
- lua_pcall: function(L: lua_State; nargs, nresults, errfunc: Integer): Integer; cdecl;
- lua_cpcall: function(L: lua_State; func: lua_CFunction; ud: Pointer): Integer; cdecl;
- lua_load: function(L: lua_State; reader: lua_Reader; data: Pointer; chunkname: PAnsiChar): Integer; cdecl;
- lua_dump: function(L: lua_State; writer: lua_Writer; data: Pointer): Integer; cdecl;
-
-
- lua_yield: function(L: lua_State; nresults: Integer): Integer; cdecl;
- lua_resume: function(L: lua_State; narg: Integer): Integer; cdecl;
- lua_status: function(L: lua_State): Integer; cdecl;
-
-
- lua_gc: function(L: lua_State; what, data: Integer): Integer; cdecl;
-
-
-
- lua_error: function(L: lua_State): Integer; cdecl;
- lua_next: function(L: lua_State; idx: Integer): Integer; cdecl;
- lua_concat: procedure(L: lua_State; n: Integer); cdecl;
-
- lua_getallocf: function(L: lua_State; ud: Pointer): lua_Alloc; cdecl;
- lua_setallocf: procedure(L: lua_State; f: lua_Alloc; ud: Pointer); cdecl;
-
-
-
- lua_getstack: function(L: lua_State; level: Integer; var ar: lua_Debug): Integer; cdecl;
- lua_getinfo: function(L: lua_State; what: PAnsiChar; var ar: lua_Debug): Integer; cdecl;
- lua_getlocal: function(L: lua_State; var ar: lua_Debug; n: Integer): PAnsiChar; cdecl;
- lua_setlocal: function(L: lua_State; var ar: lua_Debug; n: Integer): PAnsiChar; cdecl;
- lua_getupvalue: function(L: lua_State; funcindex, n: Integer): PAnsiChar; cdecl;
- lua_setupvalue: function(L: lua_State; funcindex, n: Integer): PAnsiChar; cdecl;
-
- lua_sethook: function(L: lua_State; func: lua_Hook; mask, count: Integer): Integer; cdecl;
- lua_gethook: function(L: lua_State): lua_Hook; cdecl;
- lua_gethookmask: function(L: lua_State): Integer; cdecl;
- lua_gethookcount: function(L: lua_State): Integer; cdecl;
-
-
- luaopen_base: function(L: lua_State): Integer; cdecl;
- luaopen_debug: function(L: lua_State): Integer; cdecl;
- luaopen_io: function(L: lua_State): Integer; cdecl;
- luaopen_math: function(L: lua_State): Integer; cdecl;
- luaopen_os: function(L: lua_State): Integer; cdecl;
- luaopen_package: function(L: lua_State): Integer; cdecl;
- luaopen_string: function(L: lua_State): Integer; cdecl;
- luaopen_table: function(L: lua_State): Integer; cdecl;
-
- luaL_openlibs: procedure(L: lua_State); cdecl;
-
- luaL_register: procedure(L: lua_State; libname: PAnsiChar; lr: PluaL_reg); cdecl;
- luaL_getmetafield: function(L: lua_State; obj: Integer; e: PAnsiChar): Integer; cdecl;
- luaL_callmeta: function(L: lua_State; obj: Integer; e: PAnsiChar): Integer; cdecl;
- luaL_typerror: function(L: lua_State; narg: Integer; tname: PAnsiChar): Integer; cdecl;
- luaL_argerror: function(L: lua_State; narg: Integer; extramsg: PAnsiChar): Integer; cdecl;
- luaL_checklstring: function(L: lua_State; narg: Integer; var len: Cardinal): PAnsiChar; cdecl;
- luaL_optlstring: function(L: lua_State; narg: Integer; d: PAnsiChar; var len: Cardinal): PAnsiChar; cdecl;
- luaL_checknumber: function(L: lua_State; narg: Integer): lua_Number; cdecl;
- luaL_optnumber: function(L: lua_State; narg: Integer; d: lua_Number): lua_Number; cdecl;
-
- luaL_checkinteger: function(L: lua_State; narg: Integer): lua_Integer; cdecl;
- luaL_optinteger: function(L: lua_State; narg: Integer; d: lua_Integer): lua_Integer; cdecl;
-
- luaL_checkstack: procedure(L: lua_State; sz: Integer; msg: PAnsiChar); cdecl;
- luaL_checktype: procedure(L: lua_State; narg, t: Integer); cdecl;
- luaL_checkany: procedure(L: lua_State; narg: Integer); cdecl;
-
- luaL_newmetatable: function(L: lua_State; tname: PAnsiChar): Integer; cdecl;
- luaL_checkudata: function(L: lua_State; narg: Integer; tname: PAnsiChar): Pointer; cdecl;
-
- luaL_checkoption: function(L: lua_State; narg: Integer; def: PAnsiChar; lst: array of PAnsiChar): Integer; cdecl;
-
- luaL_where: procedure(L: lua_State; lvl: Integer); cdecl;
- luaL_error: function(L: lua_State; fmt: PAnsiChar; args: array of const): Integer; cdecl;
-
- luaL_ref: function(L: lua_State; t: Integer): Integer; cdecl;
- luaL_unref: procedure(L: lua_State; t, ref: Integer); cdecl;
-
- {$ifdef LUA_COMPAT_GETN}
- luaL_getn: function(L: lua_State; t: Integer): Integer; cdecl;
- luaL_setn: procedure(L: lua_State; t, n: Integer); cdecl;
- {$endif}
-
- luaL_loadfile: function(L: lua_State; filename: PAnsiChar): Integer; cdecl;
- luaL_loadbuffer: function(L: lua_State; buff: PAnsiChar; sz: Cardinal; name: PAnsiChar): Integer; cdecl;
- luaL_loadstring: function(L: lua_State; s: PAnsiChar): Integer; cdecl;
-
- luaL_newstate: function(): lua_State; cdecl;
- luaL_gsub: function(L: lua_State; s, p, r: PAnsiChar): PAnsiChar; cdecl;
- luaL_findtable: function(L: lua_State; idx: Integer; fname: PAnsiChar; szhint: Integer): PAnsiChar; cdecl;
-
- luaL_buffinit: procedure(L: lua_State; var B: luaL_Buffer); cdecl;
- luaL_prepbuffer: function(var B: luaL_Buffer): PAnsiChar; cdecl;
- luaL_addlstring: procedure(var B: luaL_Buffer; s: PAnsiChar; l: Cardinal); cdecl;
- luaL_addstring: procedure(var B: luaL_Buffer; s: PAnsiChar); cdecl;
- luaL_addvalue: procedure(var B: luaL_Buffer); cdecl;
- luaL_pushresult: procedure(var B: luaL_Buffer); cdecl;
-
-
-
- {$ifndef LUA_COMPAT_GETN}
- function luaL_getn(L: lua_State; t: Integer): Integer;
- procedure luaL_setn(L: lua_State; t, n: Integer);
|
请发表评论