• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua io.file Read and Write

HeberPcL

[PowerOT.com.br]
Joined
Aug 21, 2007
Messages
1,292
Reaction score
51
Location
Brazil
GitHub
heberpcl
Hey community,
I need to make a script to open the config.lua file, look for protectionLevel and change what is configuration.

If the setting is protectionLevel = 50 it changes for protectionLevel = 8.

I found the following code, but I don't know how to apply in TFS;
PHP:
local f = io.open('filename','r')
f:seek('end',-1024)
local text = f:read('*a')
local after = string.match(text,".*foo(.*)")
f:close()

Does anyone know how to do?
 
First, I don't know nothing related to OTs, but I do know some Lua to help.

The following script replaces "protectionLevel=SOME_NUMBER", for "protectionLevel=123321"

PHP:
local filename = 'huehuehuehuehue.br'

function writeit(value, var, num)
   local f = assert(io.open(filename, "w"))
   f:write((value:gsub("(%s*" .. var .. ")%s*=%s*%d+", function(left)
     return left .. " = " .. num
   end)))
   f:flush()
   f:close()
end

function readit()
   local f = assert(io.open(filename, "r"))
   local value = f:read("*all")
   f:close()
   return value
end

writeit(readit(), "protectionLevel", 123321)

Tested and worked fine for me, but use at your own risk :)
 
@Blequi, tranks!

I will try to use.

If you change the value, you need also to reload the value or it may req restart.

Can use doReloadInfo(id[, cid]) to reload.

First, I don't know nothing related to OTs, but I do know some Lua to help.

The following script replaces "protectionLevel=SOME_NUMBER", for "protectionLevel=123321"

PHP:
local filename = 'huehuehuehuehue.br'

function writeit(value, var, num)
   local f = assert(io.open(filename, "w"))
   f:write((value:gsub("(%s*" .. var .. ")%s*=%s*%d+", function(left)
     return left .. " = " .. num
   end)))
   f:flush()
   f:close()
end

function readit()
   local f = assert(io.open(filename, "r"))
   local value = f:read("*all")
   f:close()
   return value
end

writeit(readit(), "protectionLevel", 123321)

Tested and worked fine for me, but use at your own risk :)

I tested, the script read the file but not change.
 
Last edited by a moderator:
@Blequi, tranks!

I will try to use.



Can use doReloadInfo(id[, cid]) to reload.



I tested, the script read the file but not change.

Following the way I described to you, it worked smooth for me.

There are some points you will have to check:
  • If you try nested table values like: a.b.c = 12312. It may not work properly.
  • If you try double values like: something = 111.3232323. It will replace just work partially the first part (111)
  • If you try something that is not an integer ([..., -3, -2, -1, 0, 1, 2, 3, ...]), it will not work.
  • If you put comments in between (as follows), it will not work:
    • something --[[ my fancy comment ]] = 123123123
    • something = --[[ my fancy comment ]] 123123123
  • The way "to use my script" is exactly like I said earlier: something = SOME_NUMBER (integer)
If you still face troubles, say me exactly how is it written on your file or give more detailed explanation.
 

Similar threads

Back
Top