-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStargateControl.lua
126 lines (111 loc) · 3.45 KB
/
StargateControl.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
local component = require("component")
local shell = require("shell")
local fs = require("filesystem")
if not component.isAvailable("gpu") then
io.stderr:write("GPU not found\n")
return
end
if not component.isAvailable("stargate") then
io.stderr:write("Stargate interface not found\n")
return
end
if not component.isAvailable("internet") then
io.stderr:write("Internet card not found\n")
return
end
-- table deserialiazition function, part of Serpent library by Paul Kulchenko
-- see https://github.com/pkulchenko/serpent
local function deserialize(data, opts)
local env = (opts and opts.safe == false) and G
or setmetatable({}, {
__index = function(t,k) return t end,
__call = function(t,...) error("cannot call functions") end
})
local f, res = (loadstring or load)('return '..data, nil, nil, env)
if not f then f, res = (loadstring or load)(data, nil, nil, env) end
if not f then return f, res end
if setfenv then setfenv(f, env) end
return pcall(f)
end
local internet = require("internet")
local url = "https://raw.githubusercontent.com/StargateMC/DiallingComputer/"
local function loadRemoteConfig()
local result, response = pcall(internet.request, url .. "master/programs.cfg")
if not result then
-- HTTP error, terminate
io.stderr:write("Failed to load remote config: " .. response .. "\n")
return nil
else
local _, cfg = deserialize(response())
return cfg
end
end
local remoteCfg = loadRemoteConfig()
if remoteCfg == nil then
return
end
function install()
local files = remoteCfg.StargateControl.files
-- create src folder
if not fs.exists(shell.resolve("./src/")) then
fs.makeDirectory(shell.resolve("./src/"))
end
for remoteFileName, localFileName in pairs(files) do
print(remoteFileName, localFileName)
local result, response = pcall(internet.request, url .. remoteFileName)
if not result then
-- HTTP error, terminate
io.stderr:write("Failed to load remote file: " .. remoteFileName .. "\n" .. response .. "\n")
return false
else
-- remove old
if fs.exists(localFileName) then
fs.remove(localFileName)
end
-- download new
local f, reason = io.open(localFileName, "w")
if not f then
io.stderr:write("Failed to open file: " .. localFileName .. "\n" .. reason .. "\n")
return
end
for chunk in response do
f:write(chunk)
end
f:close()
end
end
return true
end
-- check version and update/install
local programsFile, reason = io.open("src/programs.cfg", "r")
if not programsFile and reason ~= "file not found" then
-- unknown error
io.stderr:write("Failed to load config file: " .. reason .. "\n")
return
elseif reason == "file not found" then
-- fresh install
io.write("Missing programs.cfg, installing ...\n")
if not install() then
return
end
else
-- local config file present, deserialize
local ok, cfgLocal = deserialize(programsFile:read("*a"))
programsFile:close()
local versionLocal = cfgLocal.StargateControl.note
if versionLocal == nil or not ok then
-- programs.cfg malformed, reinstall
io.write("Malformed programs.cfg, reinstalling ...\n")
if not install() then
return
end
elseif versionLocal ~= remoteCfg.StargateControl.note then
-- update required
io.write("New version detected, updating ...\n")
if not install() then
return
end
end
end
local app = require("src/app")
app.run()