-
Notifications
You must be signed in to change notification settings - Fork 1
/
c4.lua
94 lines (83 loc) · 2.33 KB
/
c4.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
args = {...}
CATALOG_URL = "https://raw.githubusercontent.com/brooswit/c4/main/catalog.cc"
-- Computer Craft Code Catalog
local function addCacheBusterToURL(url)
local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
local cacheBustedURL = url .. "?cb=" .. cacheBuster
return cacheBustedURL
end
local function fetchContentAtURL(url)
local request = http.get(addCacheBusterToURL(url))
if (request == nil) then
print('cannot fetch content at URL. Unable request ' .. url)
return nil
end
local content = request.readAll()
return content
end
local function fetchCatalog()
local content = fetchContentAtURL(CATALOG_URL)
if content == nil then
print('cannot fetch catalog')
return nil
end
local catalog = textutils.unserialize(content)
if catalog == nil then
print('cannot fetch catalog. Unable unserialize content')
print('--------')
print(content)
print('--------')
return nil
end
return catalog
end
local function getURLFromCatalog(name)
local catalog = fetchCatalog()
if catalog == nil then
print('cannot URL from catalog. catalog is nil')
return nil
end
return catalog[name]
end
local function fetchContentFromCatalog(name)
url = getURLFromCatalog(name)
if url == nil then
print('cannot fetch content from catalog. URL is nil')
return nil
end
return fetchContentAtURL(url)
end
local function loadAPIFromCatalog(name)
local content = fetchContentFromCatalog(name)
if content == nil then
print('cannot load API from catalog. content is nil')
return nil
end
local file = fs.open(name, "w")
if file == nil then
print('cannot load API from catalog. Unable to open file ' .. name)
return nil
end
file.write(content)
file.close()
os.loadAPI(name)
end
function loadAPI(name)
print("loading " .. name .. "...")
loadAPIFromCatalog(name)
print("... done loading " .. name .. "!")
end
local function updateC4()
local content = fetchContentAtURL("https://raw.githubusercontent.com/brooswit/c4/main/c4.lua")
local file = fs.open("c4", "w")
if file == nil then
return nil
end
file.write(content)
file.close()
end
updateC4()
name = args[1]
if name ~= nil then
loadAPI(name)
end