Skip to content

Commit e9cce76

Browse files
authored
Merge pull request #15 from mc-cc-scripts/Issue-9/Allow-offline-Scripts-with-the-Manager
Added local File-Check
2 parents 6fae5ec + b9bfd6e commit e9cce76

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

scm.lua

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -745,15 +745,21 @@ function scm:updateConfig (name, value)
745745
end
746746

747747
---@param name string
748-
function scm:checkRequirements(name)
749-
scm:log("Checking requirements of " .. name .. "...")
748+
---@param localPath string
749+
function scm:checkRequirements(name, localPath)
750+
scm:log("Checking requirements of " .. (localPath or name) .. "...")
750751
local file
751-
if fs.exists("./" .. self.config["libraryDirectory"] .. name .. self.config["librarySuffix"] .. "/" .. name .. ".lua") then
752+
if localPath then
753+
file = fs.open(localPath, "r")
754+
if not file then
755+
file = fs.open('./'..localPath .. ".lua", "r")
756+
end
757+
elseif fs.exists("./" .. self.config["libraryDirectory"] .. name .. self.config["librarySuffix"] .. "/" .. name .. ".lua") then
752758
file = fs.open("./" .. self.config["libraryDirectory"] .. name .. self.config["librarySuffix"] .. "/" .. name .. ".lua", "r")
753759
else
754760
file = fs.open("./" .. self.config["libraryDirectory"] .. name .. ".lua", "r")
755761
end
756-
762+
if not file then scm:log('File ' .. name .. ' not found') end
757763
-- Find requirements by searching for comment --@requires name
758764
local requires = {}
759765
while true do
@@ -799,13 +805,46 @@ function scm:checkRequirements(name)
799805
self:download(n, "library")
800806
end
801807
else
802-
scm:log(n .. " already exists.")
808+
scm:log(n .. " already exists.")
803809
end
804810

805811
self:checkRequirements(n)
806812
end
807813
end
808814

815+
--- used when no script with the name was found online
816+
--- searches locally for the script
817+
---@param name string
818+
---@return any | nil
819+
local function fallbackRequire(name)
820+
scm:log(name .. " not found online, try to find locally")
821+
--- if script does not exist
822+
local possiblePath = {
823+
name,
824+
scm.config["libraryDirectory"] .. name,
825+
scm.config["libraryDirectory"] .. name .. "/" .. name,
826+
scm.config["libraryDirectory"] .. name .. "/" .. "init.lua"
827+
}
828+
local script
829+
local success
830+
---TryFunction for Require
831+
---@param path string
832+
---@return any
833+
local function tryRequire(path)
834+
return require(path)
835+
end
836+
837+
for _, path in pairs(possiblePath) do
838+
success, script = pcall(tryRequire, path)
839+
if success then
840+
scm:checkRequirements(name, path)
841+
return script
842+
end
843+
end
844+
scm:log("Could not load " .. name)
845+
return nil
846+
end
847+
809848
---@param name string
810849
---@return any
811850
function scm:load(name)
@@ -835,9 +874,8 @@ function scm:load(name)
835874
scm:log("Done")
836875
return script
837876
end
838-
839-
scm:log("Done")
840-
return nil
877+
878+
return fallbackRequire(name)
841879
end
842880

843881
function scm:init ()

0 commit comments

Comments
 (0)