Advertisement
Tatantyler

Patch Program

Sep 6th, 2013
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.92 KB | None | 0 0
  1. -- Patch
  2. -- By KillaVanilla
  3.  
  4. local args = {...}
  5.  
  6. if #args < 2 then
  7.     print("patch - change files according to a patchfile")
  8.     print(string.rep("=", term.getSize()))
  9.     print("USAGE: <old file> <patchfile>")
  10.     return
  11. end
  12.  
  13. assert(fs.exists(args[1]), args[1].." does not exist!")
  14.  
  15. -- read lines:
  16. local f = fs.open(args[1], "r")
  17. local lines = {}
  18. local last_pause = os.clock()
  19. while true do
  20.     local line = f.readLine()
  21.     if line then
  22.         table.insert(lines, line)
  23.     else
  24.         break
  25.     end
  26.     if (os.clock() - last_pause) >= 2.8 then
  27.         os.queueEvent("")
  28.         os.pullEvent("")
  29.         last_pause = os.clock()
  30.     end
  31. end
  32. f.close()
  33.  
  34. local f = fs.open(args[2], "r")
  35. local patch_lines = {}
  36. local last_pause = os.clock()
  37. while true do
  38.     local line = f.readLine()
  39.     if line then
  40.         table.insert(patch_lines, line)
  41.     else
  42.         break
  43.     end
  44.     if (os.clock() - last_pause) >= 2.8 then
  45.         os.queueEvent("")
  46.         os.pullEvent("")
  47.         last_pause = os.clock()
  48.     end
  49. end
  50. f.close()
  51. local line_offset = 0
  52.  
  53. for i=1, #patch_lines do
  54.     local line_new, type, text = string.match(patch_lines[i], "(%d+)([%>%<])([^%>%<]*)")
  55.     line_new = tonumber(line_new)
  56.     --print(type)
  57.     if type == ">" then
  58.         --print("ADD LINE "..line_new..": \""..text.."\"")
  59.         table.insert(lines, line_new, text)
  60.     elseif type == "<" then
  61.         local start_search = line_new-5
  62.         local pos = line_new
  63.         if start_search < 1 then
  64.             start_search = 1
  65.         end
  66.         for j=start_search, line_new+5 do
  67.             if lines[j] == text then
  68.                 pos = j
  69.                 break
  70.             end
  71.         end
  72.         --print("REMOVE LINE "..pos.."("..line_new..")"..": \""..lines[pos].."\"")
  73.         table.remove(lines, pos)
  74.     end
  75. end
  76.  
  77. local f = fs.open(args[1]..".out", "w")
  78. for i=1, #lines do
  79.     f.writeLine(lines[i])
  80. end
  81. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement