View difference between Paste ID: em32tPEV and JvAAnvAP
SHOW: | | - or go back to the newest paste.
1
local snipeComment = {"--[[","{{FILESNIPE VAR HANDLE}}"," ","please do not edit anything in this comment block"," ","{ ,}","]]",}
2
local mv = {
3
  __index = function(t,k)
4
    local file = fs.open(k,"r")
5
    if not file then return nil end
6
    local total = {}
7
    line = file.readLine()
8
    repeat
9
      table.insert(total,line)
10
      line = file.readLine()
11
    until not line
12
    file.close()
13
    return total
14
  end;
15
  __newindex = function(t,k,v)
16
    if type(v)==nil then
17
      fs.delete(k)
18
    else
19
      local file = fs.open(k,"w")
20
      for i=1,#v do
21
        file.writeLine(v[i])
22
      end
23
      file.close()
24
    end
25
  end;
26
}
27
local function init()
28
  fileSnipeNew = nil
29
  fileSnipeNew = setmetatable({
30
    fileData = {},
31
    fileName = "nil",
32
    fileMode = false,
33
    bypass = false,
34
    fileIsVar = false,
35
    handle = function(self,bool)
36
      if not bool and not self.bypass and not self.fileIsVar and self.fileData[2]=="{{FILESNIPE VAR HANDLE}}" then
37
        self.fileIsVar = {}
38
        for i=1,7 do
39
         table.insert(self.fileIsVar,self.fileData[1])
40
         table.remove(self.fileData,1)
41
        end
42
      elseif not bypass and self.fileIsVar then
43
        for i=#self.fileIsVar,1,-1 do
44
          table.insert(self.fileData,1,self.fileIsVar[i])
45
        end
46
        self.fileIsVar = false
47
      end
48
    end,
49
    },{
50
    __index = function(t,k)  
51
      if t.fileMode then t.fileData=snipeAPIFiles[t.fileName] end      
52
      t:handle(true)
53
      if t.fileData[2]~="{{FILESNIPE VAR HANDLE}}" then
54
        for i=#snipeComment,1,-1 do
55
          table.insert(t.fileData,1,snipeComment[i])
56
        end
57
      end
58
      local var = textutils.unserialize(t.fileData[6]) or {}
59
      if tostring(var[k]):sub(1,8)=="function" then return loadstring("return "..var[k])() end
60
      return var[k]
61
    end;
62
    __newindex = function(t,k,v)
63
      if type(v)=="function" then
64
        func = setmetatable({},{
65
          __call = function(_,...)
66
            if rawequal(t,({...})[1]) then
67
              return v(...)
68
            end
69
            return v(t,...)
70
          end;
71
        })
72
        rawset(t,k,func)
73
      else
74
        if t.fileMode then t.fileData=snipeAPIFiles[t.fileName] end
75
        t:handle(true)
76
        if t.fileData[2]~="{{FILESNIPE VAR HANDLE}}" then
77
          for i=#snipeComment,1,-1 do
78
            table.insert(t.fileData,1,snipeComment[i])
79
          end
80
          snipeAPIFiles[t.fileName] = t.fileData
81
        end
82
        local var=textutils.unserialize(t.fileData[6]) or {}
83
        var[k] = v
84
        t.fileData[6] = textutils.serialize(var):gsub("\n","")
85
        snipeAPIFiles[t.fileName] = t.fileData
86
      end
87
    end;
88
  })
89
  fileSnipeNew.getLine = function(self,...)
90
    if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
91
    self:handle(false)
92
    for _,line in ipairs({...}) do
93
      if line<0 then line=#self.fileData+(line+1) end
94
      if line>#self.fileData or line<0 then return nil
95
      else return self.fileData[line] end
96
    end
97
  end
98
  fileSnipeNew.setLine = function(self,line,data)
99
    if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
100
    self:handle(false)
101
    if line<0 then line=#self.fileData+(line+1) end
102
    if line>#self.fileData then
103
      for i=1,line-#self.fileData do
104
        table.insert(self.fileData,"")
105
      end
106
    elseif line<1 then
107
      repeat
108
        table.insert(self.fileData,1,"")
109
        line = line+1
110
      until line>0
111
    end
112
    self.fileData[line] = data
113
    self:handle(true)
114
    snipeAPIFiles[self.fileName] = self.fileData
115
  end
116
  fileSnipeNew.len = function(self)
117
    if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
118
    self:handle(false)
119
    return #self.fileData
120
  end
121
  fileSnipeNew.getFile = function(self)
122
    if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
123
    self:handle(false)
124
    return self.fileData
125
  end
126
  fileSnipeNew.setFile = function(self,...)
127
    local data = {...}
128
    self:handle(false)
129
    if data[1]=="table" then data = data[1] end
130
    self.fileData = data
131
    self:handle(true)
132
    snipeAPIFiles[self.fileName] = self.fileData
133
  end
134
  fileSnipeNew.remLine = function(self,...)
135
    if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
136
    self:handle(false)
137-
    for _,line in ipairs(table.sort({...},function(a,b) return a>b end)) do
137+
    local args = table.sort({...},function(a,b) return a>b end)
138
    for _,line in ipairs(args) do
139
      table.remove(self.fileData,line)
140
      self:handle(true)
141
      snipeAPIFiles[self.fileName]=self.fileData
142
    end
143
  end
144
  fileSnipeNew.insert = function(self,line,data)
145
    if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
146
    self:handle(false)
147
    if type(data)=="string" then table.insert(self.fileData,line,data)
148
    elseif type(data)=="table" then
149
      for i=#data,1,-1 do
150
        table.insert(self.fileData,line,data[i])
151
      end
152
    end
153
    self:handle(true)
154
    snipeAPIFiles[self.fileName]=self.fileData
155
  end
156
  fileSnipeNew.find = function(self,str,whole)
157
    if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
158
    self:handle(false)
159
    local instances = {}
160
    local cut = {}
161
    for i=1,#self.fileData do
162
      local t=1
163
      cut = {string.find(self.fileData[i],str,t)}
164
      while cut[1] do
165
        local s = self.fileData[i]:sub(cut[2]+1,cut[2]+1)
166
        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
167
          table.insert(instances,{i,cut[1]})
168
        end
169
        t=cut[2]+1
170
        cut = {string.find(self.fileData[i],str,t)}
171
      end
172
    end
173
    return instances
174
  end
175
  fileSnipeNew.swap = function(self,line1,line2)
176
    if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
177
    self:handle(false)
178
    if line1<=#self.fileData and line2<=#self.fileData then
179
      local ph = self.fileData[line1]
180
      self.fileData[line1]=self.fileData[line2]
181
      self.fileData[line2]=ph
182
      self:handle(true)
183
      snipeAPIFiles[self.fileName]=self.fileData
184
    end
185
  end
186
  fileSnipeNew.replace = function(self,old,new,index,whole)
187
    if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
188
    self:handle(false)
189
    local tbl = self:find(old,whole)
190
    local segment = self:getLine(tbl[index][1])
191
    segment = segment:sub(1,tbl[index][2]-1)..new..segment:sub(tbl[index][2]+old:len(),segment:len())
192
    self:setLine(tbl[index][1],segment)
193
  end
194
  fileSnipeNew.replaceAll = function(self,old,new,whole)
195
    if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
196
    self:handle(false)
197
    local tbl = self:find(old,whole)
198
    for i=#tbl,1,-1 do
199
      local segment = self:getLine(tbl[i][1])
200
      segment = segment:sub(1,tbl[i][2]-1)..new..segment:sub(tbl[i][2]+old:len(),segment:len())
201
      self:setLine(tbl[i][1],segment)
202
    end
203
  end
204
  fileSnipeNew.append = function(self,...)
205
    if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
206
    self:handle(false)
207
    local req = {...}
208
    if type(req[1])=="table" then req = req[1] end
209
    for _,data in ipairs(req) do
210
      table.insert(self.fileData,data)
211
    end
212
    self:handle(true)
213
    snipeAPIFiles[self.fileName] = self.fileData
214
  end
215
  fileSnipeNew.combineLines = function(self,line1,line2,sep)
216
    if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
217
    self:handle(false)
218
    if not sep then sep=" " end
219
    self.fileData[line1] = self.fileData[line1]..sep..self.fileData[line2]
220
    table.remove(self.fileData,line2)
221
    self:handle(true)
222
    snipeAPIFiles[self.fileName] = self.fileData
223
  end
224
  fileSnipeNew.moveLine = function(self,from,to)
225
    if self.fileMode then self.fileData=snipeAPIFiles[self.fileName] end
226
    self:handle(false)
227
    if from<0 then from=#self.fileData+(from+1) end
228
    if to<0 then to=#self.fileData+(to+1) end
229
    if to>from then 
230
      table.insert(self.fileData,to+1,self.fileData[from])
231
    else 
232
      table.insert(self.fileData,to,self.fileData[from])
233
      from=from+1 
234
    end
235
    table.remove(self.fileData,from)
236
    self:handle(true)
237
    snipeAPIFiles[self.fileName] = self.fileData
238
  end
239
  fileSnipeNew.concatLine = function(self,line,data)
240
    if self.fileMode then self.fileData=snipeAPIFile[self.fileName] end
241
    self:handle(false)
242
    if line<0 then line=#self.fileData+(line+1) end
243
    self.fileData[line] = self.fileData[line]..data
244
    self:handle(true)
245
    snipeAPIFiles[self.fileName] = self.fileData
246
  end
247
  fileSnipeNew.sepLines = function(self,line,sep)
248
    if self.fileMode then self.fileData=snipeAPIFile[self.fileName] end
249
    self:handle(false)
250
    table.insert(self.fileData,line+1,self.fileData[line]:sub(sep,self.fileData[line]:len()))
251
    self.fileData[line] = self.fileData[line]:sub(1,sep-1)
252
    self:handle(true)
253
    snipeAPIFiles[self.fileName] = self.fileData
254
  end
255
  fileSnipeNew.rawVar = function(self,key)
256
    if self.fileMode then self.fileData=snipeAPIFile[self.fileName] end
257
    local var = textutils.unserialize(self.fileData[6])
258
    if not var then return nil
259
    else return var[key] end
260
  end
261
end
262
snipeAPIFiles = setmetatable({},mv)
263
function open(sFile,mode,bypass)
264
  init()
265
  fileSnipeNew.fileName = sFile
266
  if type(mode)~="string" then
267
    bypass = mode
268
    mode = nil
269
  end
270
  local setData = snipeAPIFiles[sFile]
271
  if setData then fileSnipeNew.fileData = setData end
272
  if mode=="d" then fileSnipeNew.fileMode=true
273
  elseif mode=="s" or not mode then fileSnipeNew.fileMode=false
274
  else error("mode format not supported") end
275
  if bypass==nil then bypass = false end
276
  fileSnipeNew.bypass = bypass
277
  return fileSnipeNew
278
end