-
Notifications
You must be signed in to change notification settings - Fork 0
/
update
116 lines (111 loc) · 5.08 KB
/
update
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
-- Viewer installer/updater
-- Connects to GitHub to retrieve the latest version available
-- Clear log files
if fs.exists("/logs/viewerUpdater.log") then
fs.delete ("/logs/viewerUpdater.log")
end
--Check filesystem dependencies
if not fs.exists("/logs") then
fs.makeDir("/logs")
end
if not fs.exists("/temp") then
fs.makeDir("/temp")
end
-- Debug tool
local function logfile(input)
local file = fs.open("/logs/viewerUpdater.log", "a")
file.write(input.."\n")
file.close()
end
-- Installer code
local files = {
"main",
"gui",
"update",
"settings",
"alias",
}
local args = {...}
logfile("Updater started, checking for updates for "..#files.." files")
if not http then
logfile("HTTP API not enabled! Please enable the HTTP API in the ComputerCraft config file, or ask your server administrator to do so.")
print("HTTP API not enabled! Please enable the HTTP API in\nthe ComputerCraft config file, or ask your server/nadministrator to do so.")
return
else
logfile("Checking for settings.")
if not fs.exists("settings") then
logfile("Settings not found!")
local file = fs.open("settings", "w")
local site = http.get("https://raw.githubusercontent.com/thatcraniumguy/Viewer/master/settings")
local content = site.readAll()
if not content then
logfile("No response from GitHub. Please check the ComputerCraft config and ensure that the GitHub url has been whitelisted.")
return
else
file.write(content)
file.close()
logfile("Settings retrieved from GitHub successfully")
end
end
logfile("Loading settings into API")
os.loadAPI("settings")
if args[1] == nil then
for i,filename in pairs(files) do
sleep(0)
if filename == "settings" then
-- Load current settings, download update, then try to merge them somehow
end
logfile("Checking for updates for '"..filename.."':")
local site = http.get("https://raw.githubusercontent.com/thatcraniumguy/Viewer/master/"..filename)
local content = site.readAll()
if not content then
logfile(" No response from GitHub. Please check the ComputerCraft config and ensure that the GitHub url has been whitelisted.")
print("No response from GitHub. Please check the\nComputerCraft config and ensure that the GitHub url\nhas been whitelisted.")
return
else
logfile(" Retrieved file from https://raw.githubusercontent.com/thatcraniumguy/Viewer/master/"..filename)
local tempFile = fs.open("/temp/new_settings", "w")
local tempSite = http.get("https://raw.githubusercontent.com/thatcraniumguy/Viewer/master/settings").readAll()
tempFile.write(tempSite)
tempFile.close()
os.loadAPI("/temp/new_settings")
print("Current version for '"..filename.."': "..settings[filename].version.."\n Updated version: "..new_settings[filename].version)
if not fs.exists(filename) then
logfile(" No file found for '"..filename.."'!\n Downloading file now...")
local file = fs.open(filename, "w")
file.write(content)
file.close()
logfile(" Downloaded!")
elseif new_settings[filename].version > settings[filename].version then
logfile(" Newer version found for '"..filename.."'.\n Current version: "..settings[filename].version.."\n New version: "..new_settings[filename].version)
local file = fs.open(filename, "w")
file.write(content)
file.close()
print(" Updated!")
logfile(" Updated!")
else
print(" Skipping...")
logfile(" No new version found for '"..filename.."', skipping.")
end
end
end
elseif args[1] == "--force" or args[1] == "-f" then
logfile("Forcing update of all files")
for i, filename in pairs(files) do
local site = http.get("https://raw.githubusercontent.com/thatcraniumguy/Viewer/master/"..filename)
local content = site.readAll()
if not content then
logfile(" No response from GitHub. Please check the ComputerCraft config and ensure that the GitHub url has been whitelisted.")
print("No response from GitHub. Please check the\nComputerCraft config and ensure that the GitHub url\nhas been whitelisted.")
return
else
logfile(" Retrieved file from https://raw.githubusercontent.com/thatcraniumguy/Viewer/master/"..filename)
local file = fs.open(filename, "w")
file.write(content)
file.close()
logfile(" Downloaded!")
end
end
end
end
logfile("Updater completed checking for updates")