Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --!nocheck
- local config = script:FindFirstChild("Configuration") or script
- local serverDirectory = config:FindFirstChild("ServerDirectory") and config.ServerDirectory.Value or "ServerLoad"
- local clientDirectory = config:FindFirstChild("ClientDirectory") and config.ClientDirectory.Value or "ClientLoad"
- local serverConfigDirectory = config:FindFirstChild("ServerConfigsDirectory") and config.ServerConfigsDirectory.Value or "ServerConfigs"
- local clientConfigDirectory = config:FindFirstChild("ClientConfigsDirectory") and config.ClientConfigsDirectory.Value or "ClientConfigs"
- local serverIdentifier = config:FindFirstChild("ServerIdentifier") and config.ServerIdentifier.Value or 0
- local clientIdentifier = config:FindFirstChild("ClientIdentifier") and config.ClientIdentifier.Value or 1
- local loadedModules = {}
- local loader = {}
- function loader.setup()
- if not game.ServerStorage:FindFirstChild(serverDirectory) then
- local folder = Instance.new("Folder")
- folder.Name = serverDirectory
- folder.Parent = game.ServerStorage
- end
- serverDirectory = game.ServerStorage[serverDirectory]
- if not game.ReplicatedStorage:FindFirstChild(clientDirectory) then
- local folder = Instance.new("Folder")
- folder.Name = clientDirectory
- folder.Parent = game.ReplicatedStorage
- end
- clientDirectory = game.ReplicatedStorage[clientDirectory]
- if not serverDirectory:FindFirstChild(serverConfigDirectory) then
- local folder = Instance.new("Folder")
- folder.Name = serverConfigDirectory
- folder.Parent = serverDirectory
- end
- serverConfigDirectory = serverDirectory[serverConfigDirectory]
- if not clientDirectory:FindFirstChild(clientConfigDirectory) then
- local folder = Instance.new("Folder")
- folder.Name = clientConfigDirectory
- folder.Parent = clientDirectory
- end
- clientConfigDirectory = clientDirectory[clientConfigDirectory]
- end
- function loader.load(path, side)
- if type(path) ~= "string" then
- error("Path must be a string, got: " .. type(path))
- end
- if side ~= serverIdentifier and side ~= clientIdentifier then
- error("Side must be 0 (server) or 1 (client), got: " .. tostring(side))
- end
- local pathParts = string.split(path, "/")
- if #pathParts < 2 then
- error("Path must be in format 'subdirectory/module', got: " .. path)
- end
- local subdirectory, moduleName = pathParts[1], pathParts[2]
- if not subdirectory or subdirectory == "" then
- error("Invalid subdirectory name in path: " .. path)
- end
- if not moduleName or moduleName == "" then
- error("Invalid module name in path: " .. path)
- end
- local findIn = (side == clientIdentifier) and clientDirectory or serverDirectory
- if not findIn:FindFirstChild(subdirectory) then
- error(string.format("Subdirectory '%s' not found in %s", subdirectory, findIn.Name))
- end
- if not findIn[subdirectory]:FindFirstChild(moduleName) then
- error(string.format("Module '%s' not found in subdirectory '%s'", moduleName, subdirectory))
- end
- local modulePath = string.format("%d/%s", side, path)
- if loadedModules[modulePath] then
- return loadedModules[modulePath]
- end
- local success, module = pcall(require, findIn[subdirectory][moduleName])
- if not success then
- error(string.format("Failed to require module '%s': %s", path, module))
- end
- loadedModules[modulePath] = module
- return module
- end
- function loader.loadConfiguration(configName, side)
- local findIn = (side == clientIdentifier) and clientConfigDirectory or serverConfigDirectory
- local configModule = findIn:FindFirstChild(configName)
- if not configModule then
- error("Configuration '" .. configName .. "' not found")
- end
- local configTable = require(configModule)
- local loadedModules = {}
- if configTable.modules then
- for _, modulePath in ipairs(configTable.modules) do
- local module = loader.load(modulePath, side)
- table.insert(loadedModules, module)
- if module.init then
- module.init()
- end
- end
- end
- return loadedModules
- end
- function loader.lazyLoad(path, side)
- local cachedModule = nil
- local isLoading = false
- return setmetatable({}, {
- __index = function(self, key)
- if cachedModule then
- return cachedModule[key]
- end
- if isLoading then
- error("Circular dependency detected in lazy load for: " .. path)
- end
- isLoading = true
- cachedModule = loader.load(path, side)
- isLoading = false
- return cachedModule[key]
- end,
- __tostring = function(self)
- return string.format("LazyModule[%s, %s]", path, cachedModule and "Loaded" or "Unloaded")
- end
- })
- end
- function loader.isLoaded(path, side)
- local modulePath = string.format("%d/%s", side, path)
- return loadedModules[modulePath] ~= nil
- end
- function loader.clearFromCache(path, side)
- local modulePath = string.format("%d/%s", side, path)
- loadedModules[modulePath] = nil
- end
- function loader.getLoadedModules()
- local modules = {}
- for path, module in pairs(loadedModules) do
- modules[path] = module
- end
- return modules
- end
- return loader
Advertisement