Clientside Addon Loader

Most people have noticed that in GMod 13, clientside addons, such as: wallhacks, ESP’s, etc… need to be loaded manually. As you can imagine, this is a giant pain in the ass. So I have created a script to make that process simple :D.

I know it could probably be done in better ways, but I’m not really amazing when it comes to Lua. And hey, at least it works :D.

NOTE: It’ll only work if the server has sv_allowcslua set to 1. (of course).

Enjoy!

Code:



--[[
	GMod 13 Clientside Addon Loader.
	Why? In GMod 13, clientside addons
	need to be manually loaded. This makes 
	that process one helluva lot easier.

	Author: Adult
]]--

-- Recursive strings. Just incase.
local recs = {}


local function openScript( filename )
	-- Don't load non-lua files.
	if string.sub( filename, -4 ) ~= ".lua" then return end

	print("Loading file: " .. filename )
	include( filename )
end

local function expandDir( files, dir, rec )
	local dirfiles, subdirs = file.Find( dir , "GAME" )

	if #subdirs ~= 0 then
		for i=1,#subdirs do
			recs[rec] = recs[rec] .. "/" .. subdirs*
			files = expandDir( files, recs[rec] .. "/*", rec )
		end
	end

	table.Add( files, dirfiles )

	return files
end

local function openAllFromDir( addon )
	-- Fancy shit
	MsgC( Color(0,255,0), "==================================
" )
	MsgC( Color(0,255,0), "===== Loading Client Addon =======
" )
	MsgC( Color(0,255,0), "==================================
" )

	-- Make it a directory
	addon = "addons/" .. addon

	-- Error checking
	if not file.Exists( addon, "GAME" ) then
		MsgC( Color(255,0,0), addon .. " not found!" )
		return 
	end

	-- Defeats the purpose if it's just loading a single file.
	if not file.IsDir( addon, "GAME" ) then
		MsgC( Color(255,0,0), addon .. " is not a directory! Use lua_openscript_cl." )
		return 
	end

	-- Tell you what's going on pls.
	MsgC( Color(0,255,0), "Loading: " .. addon .. "
" )


	-- We use this to keep expanding directories.
	-- This checks if there's more than one folder
	-- in the addonname/lua/ directory, resulting
	-- in the need for more recursive variables.
	-- -----------------------------------------
	-- WARNING: From here down is really messy.
	-- -----------------------------------------
	local _, luadirs = file.Find( addon .. "/lua/*", "GAME" )

	if #luadirs < 1 then
		recs[1] = addon .. "/lua"
	else
		for i=1,#luadirs do
			recs* = addon .. "/lua/" ..  luadirs*
		end
	end

	-- Make sure it opens the DIRECTORY. Else it would create a stack overflow.
	local opendir = addon .. "/lua/*"

	local files, dirs = file.Find( opendir, "GAME" )

	for i=1,#dirs do
		local dir = dirs*
		
		files = expandDir( files, addon .. "/lua/" .. dir .. "/*", 1 )
	end

	-- Nests ;-;
	for i=1,#files do
		for j=1,#recs do
			-- +6 because you add two forward slashes on the way, then "lua/". 
			-- Total of 6 chars.
			local rec = recs[j]
			
			local lua = string.sub( rec, string.len( addon )+6, string.len( rec ) )
			openScript( lua .. "/" ..  files* )
		end
	end

	table.Empty( recs )
end

local function autoComplete()
	local _, dirs = file.Find( "addons/*", "GAME" )
	for i=1,#dirs do
		dirs* = "loadaddon " .. dirs*
	end
	
	return dirs
end
concommand.Add( "loadaddon", function( _, _, args)
	openAllFromDir( args[1] )
end, 
autoComplete )

--[[
	Menu shit. 
	Code isn't pretty, but then again
	fuck you.

	I don't know VGUI that well. I just made it work.
]]-- 

local load_dir = ""

local function displayMenu()
	local _, dirs = file.Find( "addons/*", "GAME" )

	-- Do all visual stuff first, then internal crap.

	-- Main panel
	local Home = vgui.Create( "DFrame" )
	Home:SetSize( 500, 500 )
	Home:Center()
	Home:MakePopup()
	Home:SetVisible( true )
	Home:SetTitle( "Addon Loader" )

	-- List of addons
	local List = vgui.Create( "DListView", Home )
	List:AddColumn( "Addon Name" )
	List:SetMultiSelect( false )
	List:SetSize( 450, 300 )
	List:SetPos( 25, 50 )

	for i=1,#dirs do
		local dir = dirs*
		List:AddLine( dir )
	end

	-- Button to load addons
	local Button = vgui.Create( "DButton", Home )
	Button:SetSize( 100, 100 )
	Button:SetPos( 200, 375 )
	Button:SetText( "Load addon" )

	-- Text telling what addon is selected
	local Text = vgui.Create( "DLabel", Home )
	Text:SetPos( 10, 415 )
	Text:SetText( "Current Addon: " )
	Text:SizeToContents()

	-- Give me credit :>
	local Credits = vgui.Create( "DLabel", Home )
	Credits:SetPos( 420, 475 )
	Credits:SetText( "Made by: Adult" ) 
	Credits:SizeToContents()


	-- When y	ou click the line, tell you.
	List.OnClickLine = function( _, line )
		load_dir = line:GetValue(1) 
		Text:SetText( "Current Addon: " .. load_dir )
		Text:SizeToContents()
	end

	-- When you click the button, do it.
	Button.DoClick = function()
		Button:SetText( "Check console!" )
		RunConsoleCommand( "loadaddon", load_dir )
	end
end

concommand.Add( "loadaddon_menu", displayMenu )


Instructions:
1. Save script to garrysmod/lua as cl_addon.lua
2. Bind a key to “lua_openscript_cl cl_addon.lua”
3. Optional: Bind a key to “loadaddon <your addon folder name>”
4. Optional: Bind a key to “loadaddon_menu”. This opens a menu that lists all addons for you.
[/list]

Pix:

I know the derma sucks, I really couldn’t get it to look any better :<