Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local function split(input,sep)
- local t = {}
- for str in input:gmatch("([^"..sep.."]+)") do
- table.insert(t,str)
- end
- return t
- end
- --Known issue, double square brackets are not supported
- local function popStrings(input)
- local newStr,quotes = "",{}
- local quoteType = "none" --'"',"'"
- local tempStr = ""
- local backSlash = false
- for i=1,#input do
- local char = input:sub(i,i)
- if backSlash then
- tempStr = tempStr .. char
- backSlash = false --consume backslash
- elseif char == quoteType or (char == "\n" and quoteType ~= "none") then --If char is end quote or newLine
- table.insert(quotes,quoteType .. tempStr .. quoteType)
- tempStr = ""
- newStr = newStr .. '"s' .. #quotes .. '"'
- quoteType = "none"
- elseif char == "\\" then
- tempStr = tempStr .. char
- backSlash = not backSlash
- elseif quoteType == "none" and (char == '"' or char == "'") then
- newStr = newStr .. tempStr
- tempStr = ""
- quoteType = char
- else
- tempStr = tempStr .. char
- end
- end
- newStr = newStr .. tempStr
- return newStr,quotes
- end
- --Last step, insert the quotes back into the original data
- local function insertStrings(input,quotes,num)
- if tonumber(num) and quotes[num] then
- return input:gsub('"s'..num .. '"',quotes[num])
- end
- return input:gsub('"s%d+"',function(str) return quotes[tonumber(str:match("%d+"))] end)
- end
- --REMOVE
- -- local function removeSpaceAroundChar(input,char)
- -- return input:gsub("%s*%".. char .. "%s*",char)
- -- end
- -- --Input must not contain sub-strings
- -- --Known issue, double square brackets are popped off here instead
- -- local function deFluff(input)
- -- input = input:gsub("%-%-.-\n","\n") --pops off comments (format "-- ... newline")
- -- input = input:gsub("\\[%[%]]","") --remove stuff
- -- input = input:gsub("%[%[.-%]%]","") --Remove multiline comments
- -- input = input:gsub("%s+"," ") --Remove excessive whitespace
- -- for _,v in ipairs({",","(",")","[","]","{","}","*","/","=","#",".",":"}) do
- -- input = removeSpaceAroundChar(input,v)
- -- end
- -- return input
- -- end
- -- local function getNameGen(mode)
- -- local num = 0
- -- if mode == "short" then
- -- return function() num = num + 1 return string.format("_%x",num) end
- -- else
- -- return function() num = num + 1 return string.rep("_",num) end
- -- end
- -- end
- -- local function isValidVar(str)
- -- return str:match("[_%a][_%w]*") == str
- -- end
- -- local function createBlankMap()
- -- local t = {
- -- ["toSlot"] = "toSlot",
- -- ["fromSlot"] = "fromSlot",
- -- ["compact"] = "compact",
- -- ["name"] = "name",
- -- ["count"] = "count",
- -- ["allow_repetitions"] = "allow_repetitions",
- -- ["in"] = "in"
- -- }
- -- for k,v in pairs(_G) do
- -- t[k] = k
- -- end
- -- return t
- -- end
- -- --Input should be defluffed
- -- local function createVarRemap(input,quotes,mode)
- -- local nextName = getNameGen(mode)
- -- local map = createBlankMap()
- -- for args in input:gmatch("([_%a][_,%.%w]*)=[^=]") do --Normal declarations, like `local a = ...`. Avoids boolean expressions like ==
- -- args = split(args,",")
- -- for k,v in ipairs(args) do
- -- v = v:match("[^%.]*$")
- -- if isValidVar(v) and not map[v] then
- -- map[v] = nextName()
- -- end
- -- end
- -- end
- -- for str in input:gmatch("%[.-%]=[^=]") do --`tab[key]=...` or `tab["key"]=...`
- -- str = str:sub(2,-4)
- -- if type(str) == "string" and not map[str] then --If normal number key, leave it alone
- -- if str:match('^"s%d+"$') then --If its a string "s#" key then add mappings to both the string and its replacement
- -- map[str] = nextName()
- -- local num = tonumber(str:match("%d+"))
- -- if num and type(quotes[num]) == "string" and isValidVar(quotes[num]:gsub("'\"","")) then --If a valid variable
- -- map[quotes[num]:gsub("'\"","")] = nextName()
- -- end
- -- elseif isValidVar(str) then --Otherwise, if its a valid variable name
- -- map[str] = nextName()
- -- end
- -- end
- -- end
- -- for fName,args in input:gmatch("function ([_%a][_%.%w]*[_%w]?)%((.-)%)") do --function name(var,...)
- -- fName = fName:match("[^%.]+$")
- -- if fName and not map[fName] then map[fName] = nextName() end
- -- args = split(args,",")
- -- for k,v in ipairs(args) do
- -- if isValidVar(v) and not map[v] then
- -- map[v] = nextName()
- -- end
- -- end
- -- end
- -- for str in input:gmatch("function%((.-)%)") do --args of directly called functions
- -- args = split(str,",")
- -- for k,v in ipairs(args) do
- -- if isValidVar(v) and not map[v] then
- -- map[v] = nextName()
- -- end
- -- end
- -- end
- -- for fName,args in input:gmatch("function [_%a][_%.%w]*:([_%a][_%w]*)%((.-)%)") do --The weird colon functions
- -- if not map[fName] then map[fName] = nextName() end
- -- args = split(args,",")
- -- for k,v in ipairs(args) do
- -- if isValidVar(v) and not map[v] then
- -- map[v] = nextName()
- -- end
- -- end
- -- end
- -- for args in input:gmatch("for (.-)do") do --for loop
- -- args = split(args,", =")
- -- for k,v in ipairs(args) do
- -- if isValidVar(v) and not map[v] then
- -- map[v] = nextName()
- -- end
- -- end
- -- end
- -- return map
- -- end
- -- local function replaceVars(input,quotes,mode)
- -- local map = createVarRemap(input,quotes,mode)
- -- return input:gsub("[_%a][_%w]*",function(str) return map[str] or str end)
- -- end
- -- local function obfuscate(input,mode)
- -- nostr,quotes = popStrings(input)
- -- compactstr = deFluff(nostr)
- -- newvars = replaceVars(compactstr,quotes,mode)
- -- output = insertStrings(newvars,quotes)
- -- return output
- -- end
- --REMOVE
- local reservedWords = {["if"]=true,["for"]=true,["while"]=true,["true"]=true,["false"]=true,["nil"]=true,["and"]=true,["or"]=true,["not"]=true,["local"]=true,["return"]=true,["function"]=true,["require"]=true}
- local function reFluff(nostr)
- for k,v in pairs({"if","local","while","for","return","require"}) do
- nostr = nostr:gsub("[^\n]"..v,function(nostr) return nostr:sub(1,1) .. "\n"..v end)
- end
- local str = ""
- local line = ""
- local lastWord = ""
- local tabAmt = 0
- for i=1,#nostr do
- local char = nostr:sub(i,i)
- if char:match("%a") then lastWord = lastWord .. char elseif char ~= " " then lastWord = "" end
- if lastWord == "then" or lastWord == "do" or lastWord == "repeat" or line:match("function.+%)") then
- tabAmt = tabAmt + 1
- str = str .. line .. "\n"
- line = string.rep("\t",tabAmt) or ""
- elseif lastWord == "elseif" or lastWord == "end" or lastWord == "until" then
- line = line:gsub("%a+$","",1) --remove lastword
- str = str .. line .. "\n"
- tabAmt = tabAmt - 1
- line = string.rep("\t",tabAmt) or ""
- line = line .. lastWord
- elseif lastWord == "else" and not char:match("%a") then
- line = line:gsub("%a+$","",1) --remove lastword
- str = str .. line .. "\n"
- line = string.rep("\t",math.max(tabAmt - 1,0)) or ""
- str = str .. line .. "\n"
- line = string.rep("\t",tabAmt) or ""
- line = line .. lastWord
- elseif char == "\n" then
- str = str .. line .. "\n"
- line = string.rep("\t",tabAmt) or ""
- else
- line = line .. char
- end
- -- elseif char == " " and not reservedWords[lastWord] and not line:match("for") and not line:match("if") and not line:match("while") then
- -- str = str .. line .. "\n"
- -- line = string.rep("\t",tabAmt) or ""
- -- end
- end
- str = str .. line
- str = str:gsub("\n%s+\n","\n")
- return str
- end
- local function format(str)
- nostr,quotes = popStrings(str)
- fluff = reFluff(nostr)
- output = insertStrings(fluff,quotes)
- return output
- end
- if type(args[1]) == "string" and fs.exists(args[1]) and not fs.isDir(args[1]) then
- local file = fs.open(args[1],"r")
- local data = file.readAll()
- file.close()
- local newData = format(data,args[2])
- print(newData)
- local key
- repeat
- local _,key = os.pullEvent("key")
- if key == keys.q then return end
- until key == keys.space or key == keys.enter
- -- fs.delete(args[1])
- -- local file = fs.open(args[1],"w+")
- -- file.write(newData)
- -- file.close()
- print("success")
- end
Advertisement
Add Comment
Please, Sign In to add comment