-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathupdater.lua
96 lines (77 loc) · 2.78 KB
/
updater.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
local version = "1.0"
--Github: https://github.com/Kasra-G/ReactorController/#readme
-- ["filename"] = "pastebinCode"
local filesToUpdate = {
["/reactorController.lua"] = "b17hfTqe",
["/usr/apis/touchpoint.lua"] = "nx9pkLbJ",
["/update_reactor.lua"] = "w6vVtrLb",
}
local function getPastebinFileContents(filename, pastebinCode)
local tempFilename = "/temp" .. filename
-- Avoid calling pastebin API directly to be more robust towards any future API changes
shell.run("pastebin", "get", pastebinCode, tempFilename)
local tempFile = fs.open(tempFilename, "r")
if not tempFile then
return nil
end
local fileContents = tempFile.readAll()
tempFile.close()
fs.delete(tempFilename)
return fileContents
end
-- Requires HTTP to be enabled
local function getVersion(fileContents)
if not fileContents then
return nil
end
local _, numberChars = fileContents:lower():find('local version = "')
if not numberChars then
return nil
end
local fileVersion = ""
local char = ""
while char ~= '"' do
numberChars = numberChars + 1
char = fileContents:sub(numberChars,numberChars)
fileVersion = fileVersion .. char
end
fileVersion = fileVersion:sub(1,#fileVersion-1) -- Remove quotes around the version number
return fileVersion
end
local function updateFile(filename, pastebinCode)
if fs.isDir(filename) then
print("[Error] " .. filename .. " is a directory")
return
end
local pastebinContents = getPastebinFileContents(filename, pastebinCode)
if not pastebinContents then
print("[Error] " .. filename .. " has an invalid link")
end
local pastebinVersion = getVersion(pastebinContents)
if not pastebinVersion then
print("[Error] the pastebin code for " .. filename .. " does not have a version variable")
return
end
local localVersion = nil
if fs.exists(filename) then
local localFile = fs.open(filename,"r")
localVersion = getVersion(localFile.readAll())
localFile.close()
end
if localVersion ~= pastebinVersion then
local localFile = fs.open(filename,"w")
localFile.write(pastebinContents)
localFile.close()
print("[Success] " .. filename .. " has been updated to version " .. pastebinVersion)
elseif pastebinVersion == localVersion then
print("[Success] No update required: " .. filename .. " is already the latest version")
end
end
local function main()
fs.makeDir("/usr")
fs.makeDir("/usr/apis")
for filename, pastebinCode in pairs(filesToUpdate) do
updateFile(filename, pastebinCode)
end
end
main()