All pastes #929626 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-03-05 22:13 UTC
rendered paste body
local sound = { }

function sound:init( )
	self.Assembly = { }

	if not file.Exists( "soundbrowsercache.txt" ) then
		print( "building browse cache ( started at: " .. self:gettime( ) .. ")." )

		self:build( "../sound" )
		file.Write( "soundbrowsercache.txt", util.TableToKeyValues( self.Assembly ) )

		print( "finished building browse cache ( finished at: " .. self:gettime( ) .. ")." )
		print( "a total of " .. #self.Assembly .. " sounds were found." )
	else
		self.Assembly = util.KeyValuesToTable( file.Read( "soundbrowsercache.txt" ) )
	end
end

function sound:gettime( )
	local date, tag

	tag = "am"

	date = os.date( "*t" )

	if date.hour > 11 then
		tag = "pm"
	end

	if date.hour > 12 then 
		date.hour = date.hour - 12 
	end
	
	return date.hour .. ":" .. date.min .. ":" .. ( string.len( date.sec ) == 2 and date.sec or "0" .. date.sec ) .. " " .. string.upper( tag )
end

function sound:build( folder )
	local home, k, v

	home = folder .. "/"

	for k, v in pairs( file.Find( home .. "*" ) ) do
		if file.IsDir( home .. v ) then
			self:build( home .. v )
		else
			table.insert( self.Assembly, { name = v, path = string.sub( home .. v, 10 ) } )
		end
	end
end

function sound:search( _, _, args )
	local search, k, v

	if not args 		then return end
	if not args[ 1 ] 	then return end
	if args[ 1 ] == "" 	then return end

	search = table.concat( args, " " )

	for k, v in pairs( self.Assembly ) do
		if string.find( v.path, search ) then
			print( v.path )
		end
	end
end

function sound:rebuild( )
	self.Assembly = { }

	print( "building browse cache ( started at: " .. self:gettime( ) .. ")." )

	self:build( "../sound" )
	file.Write( "soundbrowsercache.txt", util.TableToKeyValues( self.Assembly ) )

	print( "finished building browse cache ( finished at: " .. self:gettime( ) .. ")." )
	print( "a total of " .. #self.Assembly .. " sounds were found." )
end

sound:init( )

concommand.Add( "/browse", function( pl, cmd, args ) sound:search( '', '', args ) end )
concommand.Add( "/rebuild", function( pl, cmd, args ) sound:rebuild( '', '', '' ) end )