-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
132 lines (123 loc) · 4.06 KB
/
init.lua
File metadata and controls
132 lines (123 loc) · 4.06 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
local completion = require "cc.completion"
local _REPO = "https://raw.githubusercontent.com/Lorenyx/Lorell/main/"
-- Values to save to 'lorell.ini'
local config = {
rednet = {
protocol = "LORELL",
timeout = "5",
},
lorell = {
purpose = "dev", -- Change to "server", or "client"
update = ".UPDATE_LORELL",
token = ".TOKEN_LORELL",
wallet = "TESTUSER",
},
}
-- init functions
function init(purpose)
init_lib()
init_config(purpose)
init_startup()
if "client" == purpose then
init_token()
init_script({"client.lua"})
elseif "server" == purpose then
init_script({"database.lua", "server.lua"})
elseif "dev" == purpose then
init_script({"client.lua", "database.lua", "server.lua"})
local random = math.random
ccemux.attach("back", "wireless_modem", {
-- The range of this modem
range = 64,
-- Whether this is an ender modem
interdimensional = false,
-- The current world's name. Sending messages between worlds requires an interdimensional modem
world = "main",
-- The position of this wireless modem within the world
posX = random(32), posY = random(32), posZ = random(32),
})
else
printError("Unknown purpose.")
return
end
-- remove file at completion
fs.delete("init.lua")
write("Rebooting in ")
textutils.slowPrint("3... 2... 1...", 4)
os.reboot()
end -- function init
function init_lib()
local _inifile_url = "https://github.com/Lorenyx/inifile/raw/main/inifile.lua"
local _ecc_url = "https://pastebin.com/raw/ZGJGBJdg"
local _log4cc = "https://gitlab.com/-/snippets/2249573/raw/main/get_log4cc.lua"
-- Check for lib.inifile
download_file(_inifile_url, "/lib/inifile.lua")
download_file(_ecc_url, "/lib/ecc.lua")
shell.run("wget", "run", _log4cc)
end -- function init_lib
function init_config(purpose)
local _ini = "lorell.ini"
config.lorell.purpose = purpose
if not fs.exists(_ini) then
-- Save inifile with purpose
inifile = require "lib.inifile"
inifile.save(_ini, config)
end -- if not fs.exists
end -- function init_config
function init_startup()
init_script("startup.lua")
end -- function init_startup
function init_token()
local _token = ".TOKEN_LORELL"
function _generate()
-- generate uuid
-- DO NOT SHARE SEED
local _seed = os.epoch()
-- local _seed = os.time() -- FOR TESTING ONLY
math.randomseed(_seed)
--> Available letters for encoding
local A = "abcdefghijklmnopqrstuvwxyz"
A = A..A:upper().."1234567890"
assert(#A == (26+26+10), "Alphabet is incorrect length!")
return string.gsub(string.rep('-', 32), '-', function()
local k = math.random(#A)
return A:sub(k,k)
end) -- function()
end -- function get_token
-- Check for token
if not fs.exists(config.lorell.token) then
local token, length = _generate()
assert(length == 32, "Key is incorrect length!")
assert(io.open(config.lorell.token, "w"))
:write(token)
:close()
end -- if not fs.exists
end -- function init_token
function init_script(script)
-- Check for client
function get_script(file)
if not fs.exists(file) then
print("Downloading "..file)
download_file(_REPO..file, file)
end -- if not exists
end -- function get_script
if type(script) == "table" then
for i, s in ipairs(script) do
get_script(s)
end -- for i, s in ipairs
elseif type(script) == "string" then
get_script(script)
end -- if type(script) ==
end -- function init_client
-- Util functions --
function download_file(url, file)
shell.run("wget", url, file)
end -- function get_script
-- Main loop
while true do
write("Purpose? ")
local input = read(nil, nil, function(text)
return completion.choice(text, {"server", "client"})
end)
init(input)
end