Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local snipeComment = {"--[[","{{FILESNIPE VAR HANDLE}}"," ","please do not edit anything in this comment block"," ","{ ,}","]]",}
- local mv = {
- __index = function(t,k)
- local file = fs.open(k,"r")
- if not file then return nil end
- local total = {}
- line = file.readLine()
- repeat
- table.insert(total,line)
- line = file.readLine()
- until not line
- file.close()
- return total
- end;
- __newindex = function(t,k,v)
- if type(v)==nil then
- fs.delete(k)
- else
- local file = fs.open(k,"w")
- for i=1,#v do
- file.writeLine(v[i])
- end
- file.close()
- end
- end;
- }
- local function init()
- fileSnipeNew = nil
- fileSnipeNew = setmetatable({
- fileData = {},
- fileName = "nil",
- fileMode = false,
- bypass = false,
- fileIsVar = false,
- handle = function(self,bool)
- if not bool and not self.bypass and not self.fileIsVar and self.fileData[2]=="{{FILESNIPE VAR HANDLE}}" then
- self.fileIsVar = {}
- for i=1,7 do
- table.insert(self.fileIsVar,self.fileData[1])
- table.remove(self.fileData,1)
- end
- elseif not bypass and self.fileIsVar then
- for i=#self.fileIsVar,1,-1 do
- table.insert(self.fileData,1,self.fileIsVar[i])
- end
- self.fileIsVar = false
- end
- end,
- },{
- __index = function(t,k)
- if t.fileMode then t.fileData=snipeAPIFiles[t.fileName] end
- t:handle(true)
- if t.fileData[2]~="{{FILESNIPE VAR HANDLE}}" then
- for i=#snipeComment,1,-1 do
- table.insert(t.fileData,1,snipeComment[i])
- end
- end
- local var = textutils.unserialize(t.fileData[6]) or {}
- if tostring(var[k]):sub(1,8)=="function" then return loadstring("return "..var[k])() end
- return var[k]
- end;
- __newindex = function(t,k,v)
- if type(v)=="function" then
- func = setmetatable({},{
- __call = function(_,...)
- if rawequal(t,({...})[1]) then
- return v(...)
- end
- return v(t,...)
- end;
- })
- rawset(t,k,func)
- else
- if t.fileMode then t.fileData=snipeAPIFiles[t.fileName] end
- t:handle(true)
- if t.fileData[2]~="{{FILESNIPE VAR HANDLE}}" then
- for i=#snipeComment,1,-1 do
- table.insert(t.fileData,1,snipeComment[i])
- end
- snipeAPIFiles[t.fileName] = t.fileData
- end
- local var=textutils.unserialize(t.fileData[6]) or {}
- var[k] = v
- t.fileData[6] = textutils.serialize(var):gsub("\n","")
- snipeAPIFiles[t.fileName] = t.fileData
- end
- end;
- })
- fileSnipeNew.getLine = function(self,...)
- if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
- self:handle(false)
- for _,line in ipairs({...}) do
- if line<0 then line=#self.fileData+(line+1) end
- if line>#self.fileData or line<0 then return nil
- else return self.fileData[line] end
- end
- end
- fileSnipeNew.setLine = function(self,line,data)
- if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
- self:handle(false)
- if line<0 then line=#self.fileData+(line+1) end
- if line>#self.fileData then
- for i=1,line-#self.fileData do
- table.insert(self.fileData,"")
- end
- elseif line<1 then
- repeat
- table.insert(self.fileData,1,"")
- line = line+1
- until line>0
- end
- self.fileData[line] = data
- self:handle(true)
- snipeAPIFiles[self.fileName] = self.fileData
- end
- fileSnipeNew.len = function(self)
- if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
- self:handle(false)
- return #self.fileData
- end
- fileSnipeNew.getFile = function(self)
- if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
- self:handle(false)
- return self.fileData
- end
- fileSnipeNew.setFile = function(self,...)
- local data = {...}
- self:handle(false)
- if data[1]=="table" then data = data[1] end
- self.fileData = data
- self:handle(true)
- snipeAPIFiles[self.fileName] = self.fileData
- end
- fileSnipeNew.remLine = function(self,...)
- if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
- self:handle(false)
- local args = {...}
- table.sort(args,function(a,b) return a>b end)
- for _,line in ipairs(args) do
- table.remove(self.fileData,line)
- self:handle(true)
- snipeAPIFiles[self.fileName]=self.fileData
- end
- end
- fileSnipeNew.insert = function(self,line,data)
- if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
- self:handle(false)
- if type(data)=="string" then table.insert(self.fileData,line,data)
- elseif type(data)=="table" then
- for i=#data,1,-1 do
- table.insert(self.fileData,line,data[i])
- end
- end
- self:handle(true)
- snipeAPIFiles[self.fileName]=self.fileData
- end
- fileSnipeNew.find = function(self,str,whole)
- if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
- self:handle(false)
- local instances = {}
- local cut = {}
- for i=1,#self.fileData do
- local t=1
- cut = {string.find(self.fileData[i],str,t)}
- while cut[1] do
- local s = self.fileData[i]:sub(cut[2]+1,cut[2]+1)
- if not whole or s==" " or s=="?" or s=="," or s=="!" or s=="." or s=="\"" or s=="'" or s=="(" or s==")" or s=="=" or s=="" then
- table.insert(instances,{i,cut[1]})
- end
- t=cut[2]+1
- cut = {string.find(self.fileData[i],str,t)}
- end
- end
- return instances
- end
- fileSnipeNew.swap = function(self,line1,line2)
- if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
- self:handle(false)
- if line1<=#self.fileData and line2<=#self.fileData then
- local ph = self.fileData[line1]
- self.fileData[line1]=self.fileData[line2]
- self.fileData[line2]=ph
- self:handle(true)
- snipeAPIFiles[self.fileName]=self.fileData
- end
- end
- fileSnipeNew.replace = function(self,old,new,index,whole)
- if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
- self:handle(false)
- local tbl = self:find(old,whole)
- local segment = self:getLine(tbl[index][1])
- segment = segment:sub(1,tbl[index][2]-1)..new..segment:sub(tbl[index][2]+old:len(),segment:len())
- self:setLine(tbl[index][1],segment)
- end
- fileSnipeNew.replaceAll = function(self,old,new,whole)
- if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
- self:handle(false)
- local tbl = self:find(old,whole)
- for i=#tbl,1,-1 do
- local segment = self:getLine(tbl[i][1])
- segment = segment:sub(1,tbl[i][2]-1)..new..segment:sub(tbl[i][2]+old:len(),segment:len())
- self:setLine(tbl[i][1],segment)
- end
- end
- fileSnipeNew.append = function(self,...)
- if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
- self:handle(false)
- local req = {...}
- if type(req[1])=="table" then req = req[1] end
- for _,data in ipairs(req) do
- table.insert(self.fileData,data)
- end
- self:handle(true)
- snipeAPIFiles[self.fileName] = self.fileData
- end
- fileSnipeNew.combineLines = function(self,line1,line2,sep)
- if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
- self:handle(false)
- if not sep then sep=" " end
- self.fileData[line1] = self.fileData[line1]..sep..self.fileData[line2]
- table.remove(self.fileData,line2)
- self:handle(true)
- snipeAPIFiles[self.fileName] = self.fileData
- end
- fileSnipeNew.moveLine = function(self,from,to)
- if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
- self:handle(false)
- if from<0 then from=#self.fileData+(from+1) end
- if to<0 then to=#self.fileData+(to+1) end
- if to>from then
- table.insert(self.fileData,to+1,self.fileData[from])
- else
- table.insert(self.fileData,to,self.fileData[from])
- from=from+1
- end
- table.remove(self.fileData,from)
- self:handle(true)
- snipeAPIFiles[self.fileName] = self.fileData
- end
- fileSnipeNew.concatLine = function(self,line,data)
- if self.fileMode then self.fileData=snipeAPIFile[self.fileName] end
- self:handle(false)
- if line<0 then line=#self.fileData+(line+1) end
- self.fileData[line] = self.fileData[line]..data
- self:handle(true)
- snipeAPIFiles[self.fileName] = self.fileData
- end
- fileSnipeNew.sepLines = function(self,line,sep)
- if self.fileMode then self.fileData=snipeAPIFile[self.fileName] end
- self:handle(false)
- table.insert(self.fileData,line+1,self.fileData[line]:sub(sep,self.fileData[line]:len()))
- self.fileData[line] = self.fileData[line]:sub(1,sep-1)
- self:handle(true)
- snipeAPIFiles[self.fileName] = self.fileData
- end
- fileSnipeNew.rawVar = function(self,key)
- if self.fileMode then self.fileData=snipeAPIFile[self.fileName] end
- local var = textutils.unserialize(self.fileData[6])
- if not var then return nil
- else return var[key] end
- end
- end
- snipeAPIFiles = setmetatable({},mv)
- function open(sFile,mode,bypass)
- init()
- fileSnipeNew.fileName = sFile
- if type(mode)~="string" then
- bypass = mode
- mode = nil
- end
- local setData = snipeAPIFiles[sFile]
- if setData then fileSnipeNew.fileData = setData end
- if mode=="d" then fileSnipeNew.fileMode=true
- elseif mode=="s" or not mode then fileSnipeNew.fileMode=false
- else error("mode format not supported") end
- if bypass==nil then bypass = false end
- fileSnipeNew.bypass = bypass
- return fileSnipeNew
- end
Advertisement
Add Comment
Please, Sign In to add comment