local function K(K,M)assert(type(K)=="\115\116\114\105\110\103")local D={}local b=""local a=false local U=false local Z=1 local A="\047"local m="\046"local k="\037"local y="\123"local J="\125"while Z<=#K do local x=K:sub(Z,Z)if(x==A or x==m and M)and(not a and not U)then table.insert(D,b)b=""elseif x==k then U=true elseif x==y and not U then if not a then a=true end elseif x==J and(a and not U)then a=false else b=b..x U=false end Z=Z+1 end if b~=""then table.insert(D,b)end return D end local function M(K)return K:gsub("\094\037\115\042\040\046-\041\037\115\042\036","\037\049")end local function D(K)local D="\044"local b=1 local a=""local U={}while b<=#K do local Z=K:sub(b,b)if Z==D or b>#K then table.insert(U,M(a))a=""else a=a..Z end b=b+1 end if a~=""then table.insert(U,M(a))end return U end local b=game:GetService("\082\117\110\083\101\114\118\105\099\101")local a=b:IsServer()local function U(M,D)local b=D.modules local U=D.variables local Z=D.homePath if type(M)=="\115\116\114\105\110\103"then M=K(M)end local A=D.relativeTo or game local m=M[1]if m and(U and m:match("\037\064\040\046-\041"))then A=assert(U[m:sub(2)],"\073\110\118\097\108\105\100 \118\097\114\105\097\098\108\101\058 "..m)table.remove(M,1)elseif b and(m:match("\094\040\091\065-\122\093\091\065-\122\048-\057\093\043\041\036")and b[m])then A=b[m]table.remove(M,1)elseif m=="~"then A=Z table.remove(M,1)elseif m==""then A=game table.remove(M,1)elseif m=="\046"then table.remove(M,1)end local k if type(A)=="\115\116\114\105\110\103"then k=game:GetService(A)else k=A end local y={k.Name}local J="\105\110\115\116\097\110\099\101"local x=true for K,b in next,M do if b=="\046\046"and J=="\105\110\115\116\097\110\099\101"then k=k.Parent table.insert(y,k.Name)elseif b=="\035\033"and J=="\105\110\115\116\097\110\099\101"then k=require(k)x=true J="\108\117\097"elseif b=="\035\063"and J=="\105\110\115\116\097\110\099\101"then k=require(k)x=false J="\108\117\097"elseif b=="\042\035\033"and(K==#M and J=="\105\110\115\116\097\110\099\101")then local K={}for M,D in pairs(k:GetChildren())do if D:IsA("\077\111\100\117\108\101\083\099\114\105\112\116")then K[D.Name]=require(D)end end return K elseif b=="\042"and K==#M then local K=typeof(k)if K=="\073\110\115\116\097\110\099\101"then return k:GetChildren()elseif K=="\116\097\098\108\101"then return unpack(k)else return k end else if typeof(k)=="\073\110\115\116\097\110\099\101"then local K=a and k:FindFirstChild(b,D.findRelative)or k:WaitForChild(b,10)if K then k=K table.insert(y,k.Name)else error(("\037\115 \105\115 \110\111\116 \097 \118\097\108\105\100 \109\101\109\098\101\114 \111\102 \037\115"):format(b,k:GetFullName()),2)end elseif typeof(k)=="\116\097\098\108\101"then local K=k[b]if K then table.insert(y,b)k=K else if x then error(("\037\115 \105\115 \110\111\116 \097 \118\097\108\105\100 \112\097\116\104 \114\101\108\097\116\105\118\101 \116\111 \037\115"):format(b,table.concat(y,"\047")),2)else return nil end end else error("\067\097\110\110\111\116 \114\101\115\111\108\118\101 \040"..(table.concat(M,"\047")..("\041 \102\114\111\109 \040"..(type(k)..("\041 "..(table.concat(y,"\047")..""))))))end end end return k,y end return{get=U,parse=K,parseList=D}
local function pathParser(str, allowDot)
	assert(type(str) == "string")
	local parts = {}
	local part = ""
	local isSub = false
	local isEscaped = false
	local index = 1
	local SPLIT_OP = "/"
	local DOT_OP = "."
	local ESCAPE_OP = "%"
	local SUB_OP_OPEN = "{"
	local SUB_OP_CLOSE = "}"

	while index <= #str do
		local char = str:sub(index, index)

		if (char == SPLIT_OP or (char == DOT_OP and allowDot)) and not isSub and not isEscaped then
			table.insert(parts, part)
			part = ""
		elseif char == ESCAPE_OP then
			isEscaped = true
		elseif char == SUB_OP_OPEN and not isEscaped then
			if not isSub then
				isSub = true
			end
		elseif char == SUB_OP_CLOSE and isSub and not isEscaped then
			isSub = false
		else
			part = part .. char
			isEscaped = false
		end

		index = index + 1
	end

	if part ~= "" then
		table.insert(parts, part)
	end

	return parts
end

local function strtrim(s)
	return (s:gsub("^%s*(.-)%s*$", "%1"))
end

local function listParser(str)
	local SPLIT_OP = ","

	local index = 1
	local part = ""
	local parts = {}
	while index <= #str do
		local char = str:sub(index, index)

		if char == SPLIT_OP or index > #str then
			table.insert(parts, strtrim(part))
			part = ""
		else
			part = part .. char
		end
		index = index + 1
	end

	if part ~= "" then
		table.insert(parts, strtrim(part))
	end

	return parts
end

local RunService = game:GetService("RunService")
local IS_SERVER = RunService:IsServer()

local function path(fullPath, opts)
	local modules = opts.modules
	local vars = opts.variables
	local homePath = opts.homePath

	if type(fullPath) == "string" then
		fullPath = pathParser(fullPath) --split(fullPath, "/")
	end

	local relativeTo = opts.relativeTo or game

	local first = fullPath[1]
	if (first and vars and first:match("%@(.-)")) then
		relativeTo = assert(vars[first:sub(2)], "Invalid variable: " .. first)
		table.remove(fullPath, 1)
	elseif modules and first:match("^([A-z][A-z0-9]+)$") and modules[first] then
		relativeTo = modules[first]
		table.remove(fullPath, 1)
	elseif (first == "~") then
		relativeTo = homePath
		table.remove(fullPath, 1)
	elseif (first == "") then -- root
		relativeTo = game
		table.remove(fullPath, 1)
	elseif (first == ".") then -- remove script relativity.
		table.remove(fullPath, 1)
	end

	local target
	if type(relativeTo) == "string" then
		target = game:GetService(relativeTo)
	else
		target = relativeTo
	end

	local stack = {target.Name}
	local mode = "instance"
	local strict = true

	for i, part in next, fullPath do
		if part == ".." and mode == "instance" then
			target = target.Parent
			table.insert(stack, target.Name)
		elseif part == "#!" and mode == "instance" then -- allow LuaPathResolve
			-- elseif part:match("{.*}") then
			-- e.g. /ReplicatedStorage/MyModule/!#/MyFunction would be like require(ReplicatedStorage.MyModule).MyFunction
			target = require(target)
			strict = true
			mode = "lua"
		elseif part == "#?" and mode == "instance" then
			target = require(target)
			strict = false
			mode = "lua"
		elseif part == "*#!" and i == #fullPath and mode == "instance" then
			local values = {}
			for _, child in pairs(target:GetChildren()) do
				if child:IsA("ModuleScript") then
					values[child.Name] = require(child)
				end
			end
			return values
		elseif part == "*" and i == #fullPath then
			-- /ReplicatedStorage/MyModule/!#/MyTable/* - returns unpacked table, or gets children of instance
			local targetType = typeof(target)
			if targetType == "Instance" then
				return target:GetChildren()
			elseif targetType == "table" then
				return unpack(target)
			else
				return target
			end
		else
			if typeof(target) == "Instance" then
				local instance = IS_SERVER and target:FindFirstChild(part, opts.findRelative) or target:WaitForChild(part, 10)
				if instance then
					target = instance
					table.insert(stack, target.Name)
				else
					error(("%s is not a valid member of %s"):format(part, target:GetFullName()), 2)
				end
			elseif typeof(target) == "table" then
				local value = target[part]
				if value then
					table.insert(stack, part)
					target = value
				else
					if strict then
						error(("%s is not a valid path relative to %s"):format(part, table.concat(stack, "/")), 2)
					else
						return nil
					end
				end
			else
				error(
					"Cannot resolve (" ..
						table.concat(fullPath, "/") .. ") from (" .. type(target) .. ") " .. table.concat(stack, "/") .. ""
				)
			end
		end
	end

	return target, stack
end

return {
	get = path,
	parse = pathParser,
	parseList = listParser
}
