m1nvf

Module Loader

Jan 13th, 2026 (edited)
236
0
Never
9
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.94 KB | Source Code | 0 0
  1. --!nocheck
  2. local config = script:FindFirstChild("Configuration") or script
  3. local serverDirectory = config:FindFirstChild("ServerDirectory") and config.ServerDirectory.Value or "ServerLoad"
  4. local clientDirectory = config:FindFirstChild("ClientDirectory") and config.ClientDirectory.Value or "ClientLoad"
  5. local serverConfigDirectory = config:FindFirstChild("ServerConfigsDirectory") and config.ServerConfigsDirectory.Value or "ServerConfigs"
  6. local clientConfigDirectory = config:FindFirstChild("ClientConfigsDirectory") and config.ClientConfigsDirectory.Value or "ClientConfigs"
  7. local serverIdentifier = config:FindFirstChild("ServerIdentifier") and config.ServerIdentifier.Value or 0
  8. local clientIdentifier = config:FindFirstChild("ClientIdentifier") and config.ClientIdentifier.Value or 1
  9.  
  10. local loadedModules = {}
  11.  
  12. local loader = {}
  13.  
  14. function loader.setup()
  15.     if not game.ServerStorage:FindFirstChild(serverDirectory) then
  16.         local folder = Instance.new("Folder")
  17.         folder.Name = serverDirectory
  18.         folder.Parent = game.ServerStorage
  19.     end
  20.     serverDirectory = game.ServerStorage[serverDirectory]
  21.  
  22.     if not game.ReplicatedStorage:FindFirstChild(clientDirectory) then
  23.         local folder = Instance.new("Folder")
  24.         folder.Name = clientDirectory
  25.         folder.Parent = game.ReplicatedStorage
  26.     end
  27.     clientDirectory = game.ReplicatedStorage[clientDirectory]
  28.  
  29.     if not serverDirectory:FindFirstChild(serverConfigDirectory) then
  30.         local folder = Instance.new("Folder")
  31.         folder.Name = serverConfigDirectory
  32.         folder.Parent = serverDirectory
  33.     end
  34.     serverConfigDirectory = serverDirectory[serverConfigDirectory]
  35.  
  36.     if not clientDirectory:FindFirstChild(clientConfigDirectory) then
  37.         local folder = Instance.new("Folder")
  38.         folder.Name = clientConfigDirectory
  39.         folder.Parent = clientDirectory
  40.     end
  41.     clientConfigDirectory = clientDirectory[clientConfigDirectory]
  42. end
  43.  
  44. function loader.load(path, side)
  45.     if type(path) ~= "string" then
  46.         error("Path must be a string, got: " .. type(path))
  47.     end
  48.     if side ~= serverIdentifier and side ~= clientIdentifier then
  49.         error("Side must be 0 (server) or 1 (client), got: " .. tostring(side))
  50.     end
  51.  
  52.     local pathParts = string.split(path, "/")
  53.     if #pathParts < 2 then
  54.         error("Path must be in format 'subdirectory/module', got: " .. path)
  55.     end
  56.  
  57.     local subdirectory, moduleName = pathParts[1], pathParts[2]
  58.  
  59.     if not subdirectory or subdirectory == "" then
  60.         error("Invalid subdirectory name in path: " .. path)
  61.     end
  62.     if not moduleName or moduleName == "" then
  63.         error("Invalid module name in path: " .. path)
  64.     end
  65.  
  66.     local findIn = (side == clientIdentifier) and clientDirectory or serverDirectory
  67.  
  68.     if not findIn:FindFirstChild(subdirectory) then
  69.         error(string.format("Subdirectory '%s' not found in %s", subdirectory, findIn.Name))
  70.     end
  71.  
  72.     if not findIn[subdirectory]:FindFirstChild(moduleName) then
  73.         error(string.format("Module '%s' not found in subdirectory '%s'", moduleName, subdirectory))
  74.     end
  75.  
  76.     local modulePath = string.format("%d/%s", side, path)
  77.     if loadedModules[modulePath] then
  78.         return loadedModules[modulePath]
  79.     end
  80.  
  81.     local success, module = pcall(require, findIn[subdirectory][moduleName])
  82.     if not success then
  83.         error(string.format("Failed to require module '%s': %s", path, module))
  84.     end
  85.  
  86.     loadedModules[modulePath] = module
  87.     return module
  88. end
  89.  
  90. function loader.loadConfiguration(configName, side)
  91.     local findIn = (side == clientIdentifier) and clientConfigDirectory or serverConfigDirectory
  92.  
  93.     local configModule = findIn:FindFirstChild(configName)
  94.     if not configModule then
  95.         error("Configuration '" .. configName .. "' not found")
  96.     end
  97.  
  98.     local configTable = require(configModule)
  99.     local loadedModules = {}
  100.  
  101.     if configTable.modules then
  102.         for _, modulePath in ipairs(configTable.modules) do
  103.             local module = loader.load(modulePath, side)
  104.             table.insert(loadedModules, module)
  105.  
  106.             if module.init then
  107.                 module.init()
  108.             end
  109.         end
  110.     end
  111.  
  112.     return loadedModules
  113. end
  114.  
  115. function loader.lazyLoad(path, side)
  116.     local cachedModule = nil
  117.     local isLoading = false
  118.  
  119.     return setmetatable({}, {
  120.         __index = function(self, key)
  121.             if cachedModule then
  122.                 return cachedModule[key]
  123.             end
  124.  
  125.             if isLoading then
  126.                 error("Circular dependency detected in lazy load for: " .. path)
  127.             end
  128.  
  129.             isLoading = true
  130.             cachedModule = loader.load(path, side)
  131.             isLoading = false
  132.  
  133.             return cachedModule[key]
  134.         end,
  135.  
  136.         __tostring = function(self)
  137.             return string.format("LazyModule[%s, %s]", path, cachedModule and "Loaded" or "Unloaded")
  138.         end
  139.     })
  140. end
  141.  
  142. function loader.isLoaded(path, side)
  143.     local modulePath = string.format("%d/%s", side, path)
  144.     return loadedModules[modulePath] ~= nil
  145. end
  146.  
  147. function loader.clearFromCache(path, side)
  148.     local modulePath = string.format("%d/%s", side, path)
  149.     loadedModules[modulePath] = nil
  150. end
  151.  
  152. function loader.getLoadedModules()
  153.     local modules = {}
  154.     for path, module in pairs(loadedModules) do
  155.         modules[path] = module
  156.     end
  157.     return modules
  158. end
  159.  
  160. return loader
  161.  
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment