cpp: refactor Lua plugin handling, support discover function