HTOTM: FUSION
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
Lua really needs to be updated with all these, so I don't have to resort to these dodgy ways :P

This, which uses your OpenFile, can be useful for debugging:

local function OpenFile(filename)
    local oldinput = io.input()
    io.input(filename)
    local file = io.input()
    io.input(oldinput)
    return file
end

add_hook("key_down","stderrCheck",
    function(key)
        if(key == string.byte("i")) then
            stderr = OpenFile("stderr.txt")
            line = stderr:read("*l")
            while line ~= nil do
                echo(line)
                line = stderr:read("*l")
            end
            stderr:close()
        end
    end
)
Last edited by Blam; Jun 6, 2009 at 08:48 PM.
:D
Originally Posted by Blam View Post
Lua really needs to be updated with all these, so I don't have to resort to these dodgy ways :P

This, which uses your OpenFile, can be useful for debugging:

-code snipped-

Do you put this as an own script then I guess?
Sigma | Gata
Lost pet looking for home
blam, this code is better for debugging
local result,errorcode = loadfile("somefile.lua")

if result == nil then
	echo("Error loading script:")
	echo(errorcode)
	return
end

result,errorcode = pcall(result)

if errorcode ~= nil then
	echo("Error loading script:")
	echo(errorcode)
end
[SIGPIC][/SIGPIC]
orgs: rukeepers, Promo Team
Yeah but mines more for easily finding running errors, such as those in hooks.
:D
blam i started writing scripts about a month ago and i havent advanced beond the level of note pad so can you give me instruction as to how i use this?
U.S ARMY|My MOS?: 18B.: Special Operations Weapons Sergeant|***Special Forces***|ARMY Strong.