Skip to content

Commit 5923695

Browse files
committed
add .lua to pastebin scripts
add load and checkRequirements function
1 parent 02b58c4 commit 5923695

File tree

1 file changed

+93
-3
lines changed

1 file changed

+93
-3
lines changed

scm.lua

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,9 @@ function scm:downloadPastebin (sourceObject, code, targetDirectory, updateObj)
303303
end
304304

305305
if sourceObject.type == "program" then
306-
shell.run("pastebin", "get", code, sourceObject.name)
306+
shell.run("pastebin", "get", code, sourceObject.name .. ".lua")
307307
else
308-
shell.run("pastebin", "get", code, targetDirectory .. sourceObject.name)
308+
shell.run("pastebin", "get", code, targetDirectory .. sourceObject.name .. ".lua")
309309
end
310310

311311
return sourceObject, true
@@ -429,7 +429,7 @@ function scm:removeScript (name, keepScriptConfig)
429429
self:saveScripts()
430430
end
431431

432-
if scriptType and fs.exists(self.config[scriptType .. "Directory"] .. name .. self.config[scriptType .. "Suffix"]) then
432+
if scriptType and fs.exists(self.config[scriptType .. "Directory"] .. name .. ".lua") then
433433
fs.delete(self.config[scriptType .. "Directory"] .. name .. self.config[scriptType .. "Suffix"])
434434
if scriptType == "library" then
435435
fs.delete(self.config[scriptType .. "Directory"] .. name .. ".lua")
@@ -554,6 +554,96 @@ function scm:updateConfig (name, value)
554554
end
555555
end
556556

557+
---@param name string
558+
function scm:checkRequirements(name)
559+
print ("Checking requirements of " .. name .. "...")
560+
local file
561+
if fs.exists("./" .. self.config["libraryDirectory"] .. name .. self.config["librarySuffix"] .. "/" .. name .. ".lua") then
562+
print("1","./" .. self.config["libraryDirectory"] .. name)
563+
file = fs.open("./" .. self.config["libraryDirectory"] .. name .. self.config["librarySuffix"] .. "/" .. name .. ".lua", "r")
564+
else
565+
print("2", "./" .. self.config["libraryDirectory"] .. name)
566+
file = fs.open("./" .. self.config["libraryDirectory"] .. name .. ".lua", "r")
567+
end
568+
569+
-- Find requirements by searching for comment --@requires name
570+
local requires = {}
571+
while true do
572+
local line = file.readLine()
573+
if not line then break end
574+
575+
local find = string.find(line, "--@requires")
576+
if find then
577+
line = string.sub(line, find + 12)
578+
local lineEnd = string.find(line, " ")
579+
580+
local scriptName = nil
581+
if lineEnd then
582+
scriptName = string.sub(line, 0, lineEnd - 1)
583+
else
584+
scriptName = string.sub(line, 0)
585+
end
586+
587+
local scriptExists = false
588+
for i = 1, #self.scripts, 1 do
589+
if self.scripts[i].name == scriptName then
590+
scriptExists = true
591+
end
592+
end
593+
if scriptExists then
594+
-- requirement already satisfied!
595+
print ("Requirement already satisfied! (" .. scriptName .. ")")
596+
else
597+
requires[#requires + 1] = scriptName
598+
end
599+
end
600+
end
601+
file.close()
602+
603+
-- Install missing requirements
604+
for i = 1, #requires do
605+
local n = requires[i]
606+
print("Trying to install " .. n .. "...")
607+
self:download(n, "library")
608+
local tmpName, tmpCode = self:splitNameCode(n)
609+
if tmpCode then n = tmpName end
610+
self:checkRequirements(n)
611+
end
612+
end
613+
614+
---@param name string
615+
---@return any
616+
function scm:load(name)
617+
local scriptExists = false
618+
for i = 1, #self.scripts, 1 do
619+
if self.scripts[i].name == name then
620+
scriptExists = true
621+
end
622+
end
623+
624+
if not scriptExists then
625+
self:download(name, "library")
626+
end
627+
628+
scriptExists = false
629+
for i = 1, #self.scripts, 1 do
630+
if self.scripts[i].name == name then
631+
scriptExists = true
632+
end
633+
end
634+
635+
if scriptExists then
636+
self:checkRequirements(name)
637+
local path = "./" .. self.config["libraryDirectory"] .. name
638+
local script = require(path)
639+
return script
640+
else
641+
-- error installing failed
642+
end
643+
644+
return nil
645+
end
646+
557647
function scm:init ()
558648
-- Create directories
559649
if not fs.exists(self.config["configDirectory"]) then

0 commit comments

Comments
 (0)