local O=require(script.Parent.getEnv)local z=require(script.Parent.generateScriptHeader)local b={}local function i(O)for O in pairs(O)do if O~=1 and O~=2 then return false end end return O[2]and true or false end local function X(O)local z=b[O]local X=z[1]local y=z[2]assert(X,"\082\101\113\117\101\115\116\101\100 \109\111\100\117\108\101 \101\120\112\101\114\105\101\110\099\101\100 \097\110 \101\114\114\111\114 \119\104\105\108\101 \108\111\097\100\105\110\103 \077\079\068\085\076\069\058 "..(O:GetFullName()..(" - \082\069\083\085\076\084\058 "..tostring(y))))assert(i(z),"\077\111\100\117\108\101 \099\111\100\101 \100\105\100 \110\111\116 \114\101\116\117\114\110 \101\120\097\099\116\108\121 \111\110\101 \118\097\108\117\101")return y end local y={}function y.require(i)assert(i:IsA("\077\111\100\117\108\101\083\099\114\105\112\116"),"\065\116\116\101\109\112\116\101\100 \116\111 \099\097\108\108 \114\101\113\117\105\114\101 \119\105\116\104 \105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116\040\115\041\046")if b[i]then return X(i)end local q=loadstring(z(i)..i.Source)local M=O(i)M.require=y.require setfenv(q,M)local h={pcall(q)}b[i]=h return X(i)end function y.clear()b={}end return y
local getEnv = require(script.Parent.getEnv)
local generateScriptHeader = require(script.Parent.generateScriptHeader)

local cache = {}

local function validateValues(returnValues)
    for key in pairs(returnValues) do
        if key ~= 1 and key ~= 2 then
            return false
        end
    end

    return returnValues[2] and true or false
end

local function accessModule(moduleScript)
    local returnValues = cache[moduleScript]

    local success = returnValues[1]
    local result = returnValues[2]

    assert(
        success,
        "Requested module experienced an error while loading MODULE: " ..
            moduleScript:GetFullName() .. " - RESULT: " .. tostring(result)
    )
    assert(validateValues(returnValues), "Module code did not return exactly one value")

    return result
end

local ModuleCache = {}

function ModuleCache.require(moduleScript)
    assert(moduleScript:IsA("ModuleScript"), "Attempted to call require with invalid argument(s).")

    if cache[moduleScript] then
        return accessModule(moduleScript)
    end

    local moduleFn = loadstring(generateScriptHeader(moduleScript) .. moduleScript.Source)
    local env = getEnv(moduleScript)
    env.require = ModuleCache.require

    setfenv(moduleFn, env)

    local returnValues = {pcall(moduleFn)}

    cache[moduleScript] = returnValues

    return accessModule(moduleScript)
end

function ModuleCache.clear()
    cache = {}
end

return ModuleCache
