2018-06-27 23:41:12 +02:00
|
|
|
unit LuaManualModuleLoader;
|
|
|
|
|
|
|
|
|
|
{$mode delphi}
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
2019-12-18 12:07:17 +01:00
|
|
|
|
|
|
|
|
{$IFDEF windows}
|
2018-06-27 23:41:12 +02:00
|
|
|
uses
|
2018-06-30 08:50:27 +02:00
|
|
|
windows, Classes, SysUtils;
|
2018-06-27 23:41:12 +02:00
|
|
|
|
|
|
|
|
procedure initializeLuaModuleLoader;
|
2019-12-18 12:07:17 +01:00
|
|
|
{$ENDIF}
|
2018-06-27 23:41:12 +02:00
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
2019-12-18 12:07:17 +01:00
|
|
|
{$IFDEF windows}
|
2023-11-27 23:15:47 +01:00
|
|
|
uses ManualModuleLoader, lua, lauxlib, lualib, LuaClass, LuaHandler, LuaObject, luafile;
|
2018-06-27 23:41:12 +02:00
|
|
|
|
|
|
|
|
function moduleloader_createModuleLoader(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
ml: TModuleLoader;
|
|
|
|
|
filename: string;
|
2018-06-30 08:50:27 +02:00
|
|
|
executeEntryPoint: boolean;
|
|
|
|
|
paramlist: integer;
|
|
|
|
|
i: integer;
|
2020-08-19 19:58:50 +02:00
|
|
|
|
|
|
|
|
usetimeout: boolean=false;
|
|
|
|
|
timeout: integer;
|
2023-11-27 23:13:38 +01:00
|
|
|
|
|
|
|
|
paramoffset: integer;
|
|
|
|
|
|
|
|
|
|
o: TObject;
|
|
|
|
|
ms: tmemorystream absolute o;
|
2018-06-27 23:41:12 +02:00
|
|
|
begin
|
|
|
|
|
result:=0;
|
2023-11-27 23:13:38 +01:00
|
|
|
paramoffset:=0;
|
2018-06-30 08:50:27 +02:00
|
|
|
if lua_gettop(L)>=1 then
|
2018-06-27 23:41:12 +02:00
|
|
|
begin
|
2023-11-27 23:13:38 +01:00
|
|
|
if lua_isstring(L,1) then
|
|
|
|
|
begin
|
|
|
|
|
filename:=Lua_ToString(L,1);
|
|
|
|
|
try
|
|
|
|
|
ml:=TModuleLoader.create(filename);
|
|
|
|
|
ml.createSymbolListHandler;
|
|
|
|
|
except
|
|
|
|
|
on e: exception do
|
|
|
|
|
begin
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
lua_pushstring(L,e.message);
|
|
|
|
|
exit(2);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
if lua_isuserdata(L,1) then
|
|
|
|
|
begin
|
|
|
|
|
o:=lua_toceuserdata(L, 1);
|
2023-11-27 23:15:47 +01:00
|
|
|
if o is TLuafile then
|
|
|
|
|
o:=tluafile(o).stream;
|
|
|
|
|
|
2023-11-27 23:13:38 +01:00
|
|
|
if o is TMemoryStream then //(memstream, filename, executeEntrypoint, timeout)
|
2018-06-30 08:50:27 +02:00
|
|
|
begin
|
2023-11-27 23:13:38 +01:00
|
|
|
if lua_gettop(L)>=2 then
|
|
|
|
|
filename:=Lua_ToString(L,2);
|
|
|
|
|
|
|
|
|
|
ml:=TModuleLoader.create(ms, filename);
|
|
|
|
|
ml.createSymbolListHandler;
|
|
|
|
|
paramoffset:=1;
|
2018-06-30 08:50:27 +02:00
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2023-11-27 23:13:38 +01:00
|
|
|
if lua_gettop(L)>=2+paramoffset then
|
|
|
|
|
executeEntryPoint:=lua_toboolean(L,2+paramoffset)
|
2018-06-30 08:50:27 +02:00
|
|
|
else
|
|
|
|
|
executeEntryPoint:=true;
|
|
|
|
|
|
2023-11-27 23:13:38 +01:00
|
|
|
if lua_gettop(L)>=3+paramoffset then
|
2020-08-19 19:58:50 +02:00
|
|
|
begin
|
2023-11-27 23:13:38 +01:00
|
|
|
if lua_isnil(L,3+paramoffset) then
|
2020-08-19 19:58:50 +02:00
|
|
|
useTimeout:=false
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
useTimeout:=true;
|
2023-11-27 23:13:38 +01:00
|
|
|
timeout:=lua_tointeger(L,3+paramoffset);
|
2020-08-19 19:58:50 +02:00
|
|
|
end;
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
useTimeout:=false;
|
|
|
|
|
|
2018-06-30 08:50:27 +02:00
|
|
|
|
|
|
|
|
if executeEntryPoint then
|
|
|
|
|
begin
|
|
|
|
|
lua_getglobal(L,'executeCodeEx');
|
|
|
|
|
if not lua_isfunction(L,-1) then
|
|
|
|
|
begin
|
|
|
|
|
lua_pop(L,lua_gettop(L));
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
lua_pushstring(L,'executeCodeEx invalid');
|
|
|
|
|
exit(2);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lua_pushinteger(L,0); //stdcall
|
2020-08-19 19:58:50 +02:00
|
|
|
if usetimeout then
|
|
|
|
|
lua_pushinteger(L,timeout) //timeout
|
|
|
|
|
else
|
|
|
|
|
lua_pushnil(L); //timeout:infinite
|
2018-06-30 08:50:27 +02:00
|
|
|
lua_pushinteger(L,ml.EntryPoint); //address
|
|
|
|
|
|
|
|
|
|
lua_newtable(L); //hinstance
|
|
|
|
|
lua_pushstring(L, 'type');
|
|
|
|
|
lua_pushinteger(L,0); //default int type
|
|
|
|
|
lua_settable(L,-3);
|
|
|
|
|
lua_pushstring(L, 'value');
|
|
|
|
|
lua_pushinteger(L,ml.BaseAddress); //hinstance
|
|
|
|
|
lua_settable(L,-3);
|
|
|
|
|
|
|
|
|
|
lua_newtable(L); //dwReason
|
|
|
|
|
lua_pushstring(L, 'type');
|
|
|
|
|
lua_pushinteger(L,0); //default int type
|
|
|
|
|
lua_settable(L,-3);
|
|
|
|
|
lua_pushstring(L, 'value');
|
|
|
|
|
lua_pushinteger(L,DLL_PROCESS_ATTACH);
|
|
|
|
|
lua_settable(L,-3);
|
|
|
|
|
|
|
|
|
|
lua_newtable(L); //reserved
|
|
|
|
|
lua_pushstring(L, 'type');
|
|
|
|
|
lua_pushinteger(L,0);
|
|
|
|
|
lua_settable(L,-3);
|
|
|
|
|
lua_pushstring(L, 'value');
|
|
|
|
|
lua_pushinteger(L,0); //init to 0
|
|
|
|
|
lua_settable(L,-3);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lua_pcall(L,6,1,0);
|
2018-06-27 23:41:12 +02:00
|
|
|
end;
|
|
|
|
|
|
2018-06-30 08:50:27 +02:00
|
|
|
|
2018-06-27 23:41:12 +02:00
|
|
|
luaclass_newClass(L,ml);
|
|
|
|
|
result:=1;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function moduleloader_getExports(L: PLua_State): integer; cdecl;
|
|
|
|
|
var
|
|
|
|
|
ml: TModuleLoader;
|
|
|
|
|
i: integer;
|
|
|
|
|
begin
|
|
|
|
|
ml:=luaclass_getClassObject(L);
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
for i:=0 to ml.Exporttable.Count-1 do
|
|
|
|
|
begin
|
|
|
|
|
lua_pushstring(L, ml.Exporttable[i]);
|
|
|
|
|
lua_pushinteger(L, ptruint(ml.Exporttable.objects[i]));
|
|
|
|
|
lua_settable(L,-3);
|
|
|
|
|
end;
|
|
|
|
|
result:=1;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure moduleloader_addMetadata(L: PLua_state; metatable: integer; userdata: integer );
|
|
|
|
|
begin
|
|
|
|
|
object_addMetaData(L, metatable, userdata);
|
|
|
|
|
Luaclass_addPropertyToTable(L, metatable, userdata, 'Exports', moduleloader_getExports, nil);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure initializeLuaModuleLoader;
|
|
|
|
|
begin
|
|
|
|
|
lua_register(LuaVM, 'loadModule', moduleloader_createModuleLoader);
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
luaclass_register(TModuleLoader, moduleloader_addMetadata);
|
2019-12-18 12:07:17 +01:00
|
|
|
{$ENDIF}
|
2018-06-27 23:41:12 +02:00
|
|
|
|
|
|
|
|
end.
|
|
|
|
|
|