cheat central
cheat central

Reputation: 31

how would I make this script less laggy? lua

So I am working on a obfuscator but when I add the string decoding algorithm it is really laggy because of this script:

local addEnc = ""
local shouldAdd = true
newScript:gsub(".", function(c)
    if c == ")" then
        if shouldAdd == true then
            addEnc = addEnc .. ")" .. MyAlgorithm
            shouldAdd = false
        else
            addEnc = addEnc .. c
        end
    else
        addEnc = addEnc .. c
    end
end)

It works fine with smaller script but when you obfuscate really large ones it takes ages. So basically that script adds the decode algorithm after the first ) Is there any way to make this but not as laggy?

Upvotes: 0

Views: 99

Answers (2)

Kelvin Schoofs
Kelvin Schoofs

Reputation: 8718

You're going through the whole string, just to concat something after the first occurrence of ). You could use newScript:find('%)') to find the location of the first ), then simply concat the parts:

local index = newScript:find('%)')
return newScript:sub(1, index) .. MyAlgorithm .. newScript:sub(index + 1)

The % in %) is because is a special character in Lua string patterns. Alternatively, you can use :find(')', 1, true) to disable pattern matching and use a strict find.

Upvotes: 2

Nifim
Nifim

Reputation: 5021

You are using gsub for its standard purpose but in a very nonstandard way.

Rather than the function you can simply do:

newScript:gsub('%)', ')' .. MyAlgorithm, 1)

The 1 here prevents gsub from continuing to replace ) with ")" .. MyAlgorithm


Example

newScript = "((hello)!)"
MyAlgorithm = "world"

print(newScript:gsub('%)', ')' .. MyAlgorithm, 1))

Output

((hello)world!)

Upvotes: 2

Related Questions