· 5 years ago · Jun 06, 2020, 01:34 PM
1local fs = require("filesystem")
2local term = require("term")
3
4local api = {
5 version = 1.1,
6 debugging = false
7}
8
9local callName = ""
10local apiOptions = {
11 bar = "hmCLEdpH",
12 button = "ivyWQqLg",
13 division = "BHwuU95U",
14 tab = "cE7vQiJN"
15}
16
17function api.setDebugging(value)
18 api.debugging = value
19end
20
21local function setCallName(value)
22 if callName == "" then
23 callName = value
24 end
25end
26
27local function getCallName()
28 return callName
29end
30
31local function resetCallName()
32 callName = ""
33end
34
35function api.handleError(prefix)
36 return function(err)
37 if prefix ~= nil then
38 prefix = prefix .. " "
39 end
40
41 print(prefix .. "Error: " .. err)
42
43 if api.debugging then
44 print(debug.traceback())
45 end
46 end
47end
48
49
50--[ API Utils ]--
51
52api.apis = {}
53
54local function apiFilePath(name, isLibrary)
55 isLibrary = isLibrary or false
56
57 if isLibrary then
58 return string.format("/lib/%s.lua", name)
59 end
60
61 return name
62end
63
64function api.apis.exists(name, isLibrary)
65 local filePath = apiFilePath(name, isLibrary)
66
67 return fs.exists(filePath)
68end
69
70function api.apis.get(code, name, isLibrary, forceGet)
71 forceGet = forceGet or false
72
73 if api.apis.exists(name, isLibrary) and not forceGet then
74 return true
75 end
76
77 local status, _ = xpcall(
78 os.execute,
79 api.handleError("Get API"),
80 string.format("pastebin -f get %s %s >> /dev/null", code, apiFilePath(name, isLibrary))
81 )
82
83 return status
84end
85
86function api.apis.load(code, name, forceGet)
87 package.loaded[name] = nil
88 local status = api.apis.get(code, name, true, forceGet)
89
90 if not status then
91 error(string.format("Failed to load %s API", name))
92 end
93
94 return require(name)
95end
96
97function api.apis.loadByName(name, forceGet)
98 if apiOptions[name] == nil then
99 error(string.format("No API with name %s found", name))
100 end
101
102 return api.apis.load(apiOptions[name], name, forceGet)
103end
104
105
106--[ Table Utils ]--
107
108api.table = {}
109
110function api.table.contains(needle, haystack, returnOffset)
111 returnOffset = returnOffset or false
112
113 for offset, value in pairs(haystack) do
114 if value == needle then
115 if retturnOffset then
116 return offset
117 end
118
119 return true
120 end
121 end
122
123 if returnOffset then
124 return nil
125 end
126
127 return false
128end
129
130function api.table.keys(tableParam)
131 local keys = {}
132
133 for key, _ in pairs(tableParam) do
134 table.insert(keys, key)
135 end
136
137 return keys
138end
139
140function api.table.values(tableParam)
141 local values = {}
142
143 for _, value in pairs(tableParam) do
144 table.insert(values, value)
145 end
146
147 return values
148end
149
150function api.table.concat(tableParam, indent)
151 indent = indent or 0
152
153 local output = ""
154
155 for key, value in pairs(tableParam) do
156 local stringValue = value
157
158 if type(value) == "table" then
159 stringValue = "\n" .. api.table.concat(value, indent + 1)
160
161 --print(stringValue)
162 --error("stop")
163 end
164
165 output = output .. string.rep("\t", indent) .. string.format("%s: %s\n", key, stringValue)
166 end
167
168 return output
169end
170
171
172--[ Color Manager Class ]--
173
174local ColorManager = {
175 gpu = nil,
176
177 backgroundColor = nil,
178 backgroundIsPalette = false,
179 foregroundColor = nil,
180 foregroundIsPalette = false
181}
182
183function ColorManager:new(gpu)
184 if gpu == nil then
185 error("ColorManager - Missing required parameter: gpu")
186 end
187
188 local colorManager = {}
189 setmetatable(colorManager, self)
190 self.__index = self
191
192 self.gpu = gpu
193
194 return colorManager
195end
196
197function ColorManager:save()
198 self.backgroundColor, self.backgroundIsPalette = self.gpu.getBackground()
199 self.foregroundColor, self.foregroundIsPalette = self.gpu.getForeground()
200end
201
202function ColorManager:restore()
203 if self.backgroundColor ~= nil then
204 self.gpu.setBackground(self.backgroundColor, self.backgroundIsPalette)
205
206 self.backgroundColor = nil
207 self.backgroundIsPalette = false
208 end
209
210 if self.foregroundColor ~= nil then
211 self.gpu.setForeground(self.foregroundColor, self.foregroundIsPalette)
212
213 self.backgroundColor = nil
214 self.backgroundIsPalette = false
215 end
216end
217
218api.ColorManager = ColorManager
219
220
221--[ Command Handlers ]--
222
223local originalPath = "/lib/event.lua"
224local unpatchedPath = "/lib/event_unpatched.lua"
225
226local validCommands = {
227 "patch",
228 "unpatch",
229 "getApi"
230}
231
232local function commandPatch(args)
233 if fs.exists(unpatchedPath) then
234 print(string.format("Unpatched file already exists at %s", unpatchedPath))
235 print("Unpatch command should be used before applying the patch")
236
237 return
238 end
239
240 -- Move the old event.lua
241 fs.rename(originalPath, unpatchedPath)
242
243 -- Download patched event.lua
244 print(string.format("Downloading patched %s", originalPath))
245
246 api.apis.get("V7k0adVW", "event", true)
247
248 print(string.format("Successfully patched %s", originalPath))
249end
250
251local function commandUnpatch(args)
252 if not fs.exists(unpatchedPath) then
253 print(string.format("Unable to locate unpatched files at %s", unpatchedPath))
254
255 return
256 end
257
258 fs.remove(originalPath)
259 fs.rename(unpatchedPath, originalPath)
260
261 print(string.format("Successfully unpatched %s", originalPath))
262end
263
264local function commandGetApi(args)
265 local apiName = args[2]
266
267 if apiOptions[apiName] == nil then
268 if apiName ~= nil then
269 print(string.format("No API found with name %s", apiName))
270 else
271 print("No API name provided")
272 end
273
274 print(string.format("Try: %s", table.concat(api.table.keys(apiOptions), ", ")))
275
276 return
277 end
278
279 print(string.format("Downloading %s API to /lib/%s.lua", apiName, apiName))
280
281 if api.apis.get(apiOptions[apiName], apiName, true) then
282 print(string.format("Successfully downloaded %s API", apiName))
283 else
284 print(string.format("Failed to download %s API. Please try again", apiName))
285 end
286end
287
288local function handleCommand(args)
289 local command = args[1]
290
291 if command == "patch" then
292 commandPatch(args)
293 elseif command == "unpatch" then
294 commandUnpatch(args)
295 elseif command == "getApi" then
296 commandGetApi(args)
297 else
298 print(string.format("Unknown command: %s", command))
299 print(string.format("Try: %s", table.concat(validCommands, ", ")))
300 end
301end
302
303--[ Functional Code ]--
304
305local args = { ... }
306
307if #args ~= 0 and args[1] ~= "bioUtil" then
308 handleCommand(args)
309end
310
311return api