-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
122 lines (94 loc) · 3.47 KB
/
init.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
local arguments_raw = { ... }
local arguments = ""
for index, value in ipairs(arguments_raw) do
arguments = arguments .. value .. " "
end
arguments = arguments:sub(1, -2)
local index = 1
local exit = false
local namedTokens = {}
local indexTokens = {}
while exit ~= true do
if string.sub(arguments, index, index) == "-" then
index = index + 1
if string.sub(arguments, index, index) == "-" then
index = index + 1
local key = ""
while string.sub(arguments, index, index) ~= " " and string.sub(arguments, index, index) ~= "" do
key = key .. string.sub(arguments, index, index)
index = index + 1
end
index = index + 1
local value = ""
while string.sub(arguments, index, index) ~= " " and string.sub(arguments, index, index) ~= "" do
value = value .. string.sub(arguments, index, index)
index = index + 1
end
namedTokens[key] = value
else
while string.sub(arguments, index, index) ~= " " and string.sub(arguments, index, index) ~= "" do
table.insert(indexTokens, string.sub(arguments, index, index))
index = index + 1
end
end
end
if string.sub(arguments, index, index) == "" then
exit = true
end
index = index + 1
end
if namedTokens["host"] == nil then
namedTokens["host"] = "localhost"
end
if namedTokens["port"] == nil then
namedTokens["port"] = 3434
end
local endpoint = "http"
if indexTokens["s"] then
endpoint = endpoint .. "s"
end
endpoint = endpoint .. "://" .. namedTokens["host"] .. ":" .. namedTokens["port"] .. "/"
local response, errorReason, errorResponse = http.get(endpoint .. "turtlejs-client", { HOST = _HOST, CC_DEFAULT_SETTINGS = _CC_DEFAULT_SETTINGS, VERSION = os.version(), COMPUTER_ID = os.getComputerID(), COMPUTER_LABEL = os.getComputerLabel() }, true)
if response == nil then
error(errorReason)
end
fs.delete("/turtlejs/prog/turtlejs-client/")
local primaryFileNameLength0 = response.read()
local primaryFileNameLength1 = response.read()
if primaryFileNameLength0 == nil or primaryFileNameLength1 == nil then
error("Failed to parse: ended on name length")
end
local primaryFileNameLength = primaryFileNameLength0 + (primaryFileNameLength1 * 256)
local primaryFileName = response.read(primaryFileNameLength)
local continue = true
while continue do
local nameLength0 = response.read()
local nameLength1 = response.read()
if nameLength0 == nil or nameLength1 == nil then
error("Failed to parse: ended on name length")
end
local nameLength = nameLength0 + (nameLength1 * 256)
local name = response.read(nameLength)
local file = fs.open("/turtlejs/prog/turtlejs-client/" .. name, "wb")
local fileLength0 = response.read()
local fileLength1 = response.read()
local fileLength2 = response.read()
local fileLength3 = response.read()
if fileLength0 == nil or fileLength1 == nil or fileLength2 == nil or fileLength3 == nil then
error("Failed to parse: ended on file length")
end
local fileLength = fileLength0 + (fileLength1 * 256) + (fileLength2 * 65536) + (fileLength3 * 16777216)
local i = 0
while i < fileLength do
i = i + 1
file.write(response.read())
end
local hasEnded = response.read()
if hasEnded == 1 then
continue = false
end
end
local r = require("cc.require")
local env = setmetatable({ arguments = { namedTokens = namedTokens, indexTokens = indexTokens } }, { __index = _ENV })
env.require, env.package = r.make(env, "/turtlejs/prog/turtlejs-client/")
env.require(primaryFileName)