Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Projects

Hexhub Lua Plugin

A community discussion post from the original code:zIRON forum.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

Hexhub Lua Plugin

Due to an earlier request, I have written a very basic lua plugin in Ziron for Hexhub.

Create a folder in the hexhub directory "scripts", place the lua52-32.dll inside the hexhub dir, compile the plugin using the latest Ziron assembler and place the dll into the Plugins directory.

/*
	HexLua :: Hexhub Lua plugin
	Original Author: Colin Stewart
	
	Changes:
		...
*/

program PE32_DLL 'Hex Lua';

// includes
#include 'win_def.zir';
#include 'lua/lua.zir';

// some definitions
#define HEXLUA_TITLE 'Hexhub Lua Plugin';
#define HEXHUB_API stdcall;
#define HEXLUA_VERSION '0.0.0.1';
#define HEXLUA_AUTHOR 'Colin Stewart';

// method pointers
function* HEXHUB_API GetFunction(char* name): pointer;
procedure* HEXHUB_API SendToAll(char* buffer; DWord size); 

// blocks
block LUA_SCRIPT {
	lua_State* L;

	LUA_SCRIPT* next;
}

// variables
pointer firstScript = null;
pointer lastScript = null;

//#
//# SCRIPT INTERMEDIATE METHODS
//#
function LUA_API luaSendToAll(lua_State* L) {
	uses edi;
	edi = lua_gettop(L);
		
	char* output;
	output = lua_tostring(L, 1);

	SendToAll(output, strlen(output));
	lua_pop(L, edi);
	
	return 0;
}


//#
//# INTERNAL FUNCTIONALITY
//#

//
// load scripts
//
procedure loadScripts() {
	uses esi edi ebx;
	_WIN32_FIND_DATAA fnd;
	
	//
	
	SetCurrentDirectory('scripts');
	
	ebx = FindFirstFileA('*.lua', @fnd);
		
	repeat {
		esi = @fnd.cFileName;
		////
		
		edi = luaL_newstate();	
		luaL_openlibs(edi);

		// register our functions
		lua_register(edi, 'SendToAll', @luaSendToAll); 
		
		eax = luaL_loadfile(edi, esi);
		if (eax != 0) {
			ecx = lua_tostring(edi, -1);
		//	print('Couldn\'t load file: ',[char*]ecx,'\n');
		} else {
			// Ask Lua to run our little script
			eax = lua_pcall(edi, 0, LUA_MULTRET, 0);
			if (eax != 0) {
				ecx = lua_tostring(edi, -1);
				// print('Failed to run script: ',[char*]ecx,'\n');
			} else {
				lua_pop(edi, 1);  // Take script return value out of the stack
			}
		}	
		
		edx = new LUA_SCRIPT;
		[edx] = edi;
		[edx+4] = nil;
		
		if (firstScript == nil) {
			firstScript = edx;
			lastScript = edx;
		} else {
			edi = lastScript;
			[edi+4] = edx;
			lastScript = edx;
		}				
		
		////
		ebx = FindNextFileA(ebx, @fnd);
	} until (ebx == 0);
	
	FindClose(ebx);
	
	SetCurrentDirectory('..');
}

//
// DLL entry function
//
entry function WINAPI DLLMain(DWord iDLL; DWord iReason; Pointer iResult) {
	if (iReason == DLL_PROCESS_ATTACH) {
		return true;
	}

	return false;
}

//#
//# HEXHUB FUNCTIONS ETC
//#

//
// called on entry to plugin
//
function HEXHUB_API InitPlugin(pointer gF) {
	RTLMain();
	GetFunction = gF;

	// get hexhub functions etc
	SendToAll = GetFunction('SendToAll');
	
	// init lua
	eax = lua_loadlib('lua52-32.dll');
	if (eax == -2) {
		MessageBoxA(0, 'Unable to load Lua DLL', 'Lua Plugin', MB_OK);
	}
		
	// load lua scripts?
	loadScripts();
	
	return HEXLUA_TITLE;
}

//
// called on unloading plugin
//
function HEXHUB_API UnloadPlugin() {
	uses esi edi;
	esi = firstScript as LUA_SCRIPT;

	while (esi <> nil) {
		lua_close(esi.L);			
		edi = esi.next;		
		delete esi;
		esi = edi;
	}

	lua_freelib();
}

procedure HEXHUB_API GetConfig(DWord parent) {
	MessageBoxA(parent, 'Not yet implemented', 'Lua Plugin Configuration', MB_OK);
}

//
// called when a chat message is sent to the hub :: returns boolean
//
function HEXHUB_API onChatMessage(DWord userId; char* lpBuffer; Int32 bufferSize; pointer userFloodInfo; pointer floodDefInfo) { //LPFLOODINFO userFloodInfo,LPDEFINFO floodDefInfo
	uses esi edi;
	esi = firstScript as LUA_SCRIPT;

	while (esi <> nil) {
		edi = esi.L;
		
		lua_getglobal(edi, 'onChatMessage');
		eax = lua_pcall(edi, 0, LUA_MULTRET, 0);
		
		//:TODO: check for return value.. if false, then exit with return value
		
		esi = esi.next;
	}

	return true;
}


exports InitPlugin, UnloadPlugin, GetConfig, onChatMessage;

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

A small update:

/*
	HexLua :: Hexhub Lua plugin
	Original Author: Colin Stewart
	
	Changes:
		...
*/

program PE32_DLL 'Hex Lua';

// includes
#include 'win_def.zir';
#include 'lua/lua.zir';

// some definitions
#define HEXLUA_TITLE 'Hexhub Lua Plugin';
#define HEXHUB_API stdcall;
#define HEXLUA_VERSION '0.0.0.2';
#define HEXLUA_AUTHOR 'Colin Stewart';

// method pointers
function* HEXHUB_API GetFunction(char* name): pointer;
procedure* HEXHUB_API SendToAll(char* buffer; DWord size); 

// blocks
block LUA_SCRIPT {
	lua_State* L;

	LUA_SCRIPT* next;
}

// variables
pointer firstScript = null;
pointer lastScript = null;

//#
//# SCRIPT INTERMEDIATE METHODS
//#
function LUA_API luaSendToAll(lua_State* L) {
	uses edi;
	edi = lua_gettop(L);
		
	char* output;
	output = lua_tostring(L, 1);

	SendToAll(output, strlen(output));
	lua_pop(L, edi);
	
	return 0;
}


//#
//# INTERNAL FUNCTIONALITY
//#

//
// load scripts
//
procedure loadScripts() {
	uses esi edi ebx;
	_WIN32_FIND_DATAA fnd;
	
	//
	
	SetCurrentDirectory('scripts');
	
	ebx = FindFirstFileA('*.lua', @fnd);
		
	repeat {
		esi = @fnd.cFileName;
		////
		
		edi = luaL_newstate();	
		luaL_openlibs(edi);

		// register our functions
		lua_register(edi, 'SendToAll', @luaSendToAll); 
		
		eax = luaL_loadfile(edi, esi); //'scripts/script.lua'
		if (eax != 0) {
			ecx = lua_tostring(edi, -1);
		//	print('Couldn\'t load file: ',[char*]ecx,'\n');
		} else {
			// Ask Lua to run our little script
			eax = lua_pcall(edi, 0, LUA_MULTRET, 0);
			if (eax != 0) {
				ecx = lua_tostring(edi, -1);
				// print('Failed to run script: ',[char*]ecx,'\n');
			} else {
				lua_pop(edi, 1);  // Take script return value out of the stack
				
				//call out onLoad method
				lua_getglobal(edi, 'onLoad');
				eax = lua_pcall(edi, 0, 0, 0);				
			}
		}	
		
		edx = new LUA_SCRIPT as LUA_SCRIPT;
		edx.L = edi;
		edx.next = nil;
		
		if (firstScript == nil) {
			firstScript = edx;
			lastScript = edx;
		} else {
			edi = lastScript as LUA_SCRIPT;
			edi.next = edx;
			lastScript = edx;
		}				
		
		////
		ebx = FindNextFileA(ebx, @fnd);
	} until (ebx == 0);
	
	FindClose(ebx);
	
	SetCurrentDirectory('..');
}


//
// call methods with no params
//
inline function getScriptContinue(;) {
	$if $argc < 1:
		$raise 'getScriptContinue requires at least 1 parameter!';
	$end;
		
	$params = $argc - 1;
		
	using(esi,edi) {
		esi = firstScript as LUA_SCRIPT;

		while (esi <> nil) {
			edi = esi.L;
			
			//get the function to call
			lua_getglobal(edi, $arg[0]);
			
			$temp = 1; //params start from
			//push arguments
			$if $params > 0:
				$repeat $params:	
					$tt = tokentype($arg[$temp]);
						
					$if $tt == TOKEN_NUMBER:		
						lua_pushunsigned(edi, $arg[$temp]);
					$else
						$if casttype($arg[$temp]) == char*:
							lua_pushstring(edi, $arg[$temp]);
						$elseif casttype($arg[$temp]) == DWord:
							lua_pushunsigned(edi, $arg[$temp]);
						$elseif casttype($arg[$temp]) == int32:
							lua_pushunsigned(edi, $arg[$temp]);	
						$else
							lua_pushstring(edi, $arg[$temp]);
						$end;
					$end;
									
					$temp = $temp + 1;
				$end;
			$end;
			
			eax = lua_pcall(edi, $params, 1, 0);
			if (eax <> 0) {
				//get return result?
				eax = lua_toboolean(edi, -1);
				if (eax == false) {
					goto cleanexit; 
				}
			}
			
			esi = esi.next;
		}
	}
	
	eax = true; //continue execution
cleanexit:	
	$return eax;
}


//
// DLL entry function
//
entry function WINAPI DLLMain(DWord iDLL; DWord iReason; Pointer iResult) {
	if (iReason == DLL_PROCESS_ATTACH) {
		return true;
	}

	return false;
}

//#
//# HEXHUB FUNCTIONS ETC
//#

//
// called on entry to plugin
//
function HEXHUB_API InitPlugin(pointer gF) {
	RTLMain();
	GetFunction = gF;

	// get hexhub functions etc
	SendToAll = GetFunction('SendToAll');
	
	// init lua
	eax = lua_loadlib('lua52-32.dll');
	if (eax == -2) {
		MessageBoxA(0, 'Unable to load Lua DLL', 'Lua Plugin', MB_OK);
	}
		
	// load lua scripts?
	loadScripts();
	
	return HEXLUA_TITLE;
}

//
// called on unloading plugin
//
function HEXHUB_API UnloadPlugin() {
	uses esi edi;
	esi = firstScript as LUA_SCRIPT;

	while (esi <> nil) {
		edi = esi.L;
		
		lua_getglobal(edi, 'onUnload');
		eax = lua_pcall(edi, 0, 0, 0);
	
		lua_close(edi);			
		edi = esi.next;		
		delete esi;
		esi = edi;
	}

	lua_freelib();
}

procedure HEXHUB_API GetConfig(DWord parent) {
	MessageBoxA(parent, 'Not yet implemented', 'Lua Plugin Configuration', MB_OK);
}


//
//
//
function HEXHUB_API onPublicMessage(DWord userId; char* lpBuffer; int32 bufferSize) {
	eax = getScriptContinue('onPublicMessage', userId, lpBuffer);

	return eax;
}

//
// called when a chat message is sent to the hub :: returns boolean
//
function HEXHUB_API onChatMessage(DWord userId; char* lpBuffer; Int32 bufferSize; pointer userFloodInfo; pointer floodDefInfo) { //LPFLOODINFO userFloodInfo,LPDEFINFO floodDefInfo
	eax = getScriptContinue('onChatMessage', userId, lpBuffer); //, userId, lpBuffer);

	return eax;
}


exports InitPlugin, UnloadPlugin, GetConfig, onChatMessage, onPublicMessage;