-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtlib.lua
More file actions
109 lines (93 loc) · 2.31 KB
/
tlib.lua
File metadata and controls
109 lines (93 loc) · 2.31 KB
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
--Tristan Swanson
--General purpose CC Tweaked API
--Last updated: 10/29/2021
-- Internal
local libTable = {}
-- Installed?
old_v = false
if not (fs.exists("lib")) then
fs.makeDir("lib")
print("Installing...")
old_v = true
end
if not (fs.exists("lib/manifest.json")) then
print("Installing...")
old_v = true
else
--load er up
local mfile = fs.open("lib/manifest.json", "r")
libTable = textutils.unserializeJSON(mfile.readAll())
mfile.close()
end
function update()
term.clear()
term.setCursorPos(0, 0)
print("Updating tlib")
-- Main File
local download = {name = "tlib.lua", url = "https://raw.githubusercontent.com/Tridius1/cc_tlib/main/tlib.lua"}
local request = http.get(download.url)
local data = request.readAll()
if fs.exists(download.name) then
fs.delete(download.name)
end
local file = fs.open(download.name, "w")
file.write(data)
file.close()
-- Manifest
if not (fs.exists("lib")) then
fs.makeDir("lib")
end
download = {name = "manifest.json", url = "https://raw.githubusercontent.com/Tridius1/cc_tlib/main/lib/manifest.json"}
request, e = http.get(download.url)
if (request == nil) then
print("http get failed: manifest")
error(e)
end
manifestJSON = request.readAll()
libTable = textutils.unserializeJSON(manifestJSON)
mfile = fs.open("lib/manifest.json", "w")
mfile.write(manifestJSON)
mfile.close()
-- library files
for k,v in pairs(libTable) do
print("Downloading "..k.." module to "..v)
download = {name = v, url = "https://raw.githubusercontent.com/Tridius1/cc_tlib/main/lib/"..v}
request, e = http.get(download.url)
if (request == nil) then
print("http get failed: "..k)
error(e)
end
data = request.readAll()
if fs.exists("lib/" .. download.name) then
fs.delete("lib/" .. download.name)
end
file = fs.open("lib/" .. download.name, "w")
file.write(data)
file.close()
end
print("Done")
end
-- Load up all modules
local function loadModules(lib)
modules = {}
for k,v in pairs(lib) do
for n,f in pairs(require("lib."..k)) do
modules[n] = f
end
end
return modules
end
-- ### MAIN ###
if (arg[1] == "u" or arg[1] == "update") then
update()
elseif (old_v) then
update()
end
local success, modules = pcall(loadModules, libTable)
if success then
return modules
else
print("Error loading tlib libraries, update required")
print(modules)
return
end