do local W={Character={};CharacterPart={}}local k={}local w={}for W,m in pairs(W)do k[W]={}w[W]={}end function Network.AddReference(B,m,z,...)local O=W[z]assert(O,"\073\110\118\097\108\105\100 \082\101\102\101\114\101\110\099\101 \084\121\112\101")local S={Type=z,Reference=m,Objects={...};Aliases={}}k[z][S.Reference]=S local o=w[z]for W,k in ipairs(S.Objects)do local w=o[k]or{}o[k]=w o=w end o.__Data=S end function Network.AddReferenceAlias(B,m,z,...)local O=W[z]assert(O,"\073\110\118\097\108\105\100 \082\101\102\101\114\101\110\099\101 \084\121\112\101")local S=k[z][m]if not S then warn("\084\114\105\101\100 \116\111 \097\100\100 \097\110 \097\108\105\097\115 \116\111 \097 \110\111\110-\101\120\105\115\116\105\110\103 \114\101\102\101\114\101\110\099\101")return end local o={...}S.Aliases[#S.Aliases+1]=o local f=w[z]for W,k in ipairs(o)do local w=f[k]or{}f[k]=w f=w end f.__Data=S end function Network.RemoveReference(B,m,z)local O=W[z]assert(O,"\073\110\118\097\108\105\100 \082\101\102\101\114\101\110\099\101 \084\121\112\101")local S=k[z][m]if not S then warn("\084\114\105\101\100 \116\111 \114\101\109\111\118\101 \097 \110\111\110-\101\120\105\115\116\105\110\103 \114\101\102\101\114\101\110\099\101")return end k[z][S.Reference]=nil local function o(W,k,w)if w<=#k then local m=k[w]local z=W[m]o(z,k,w+1)if next(z)==nil then W[m]=nil end elseif W.__Data==S then W.__Data=nil end end local f=w[S.Type]o(f,S.Objects,1)for W,k in ipairs(S.Aliases)do o(f,k,1)end end function Network.GetObject(z,w,m)assert(W[m],"\073\110\118\097\108\105\100 \082\101\102\101\114\101\110\099\101 \084\121\112\101")local B=k[m][w]if not B then return nil end return unpack(B.Objects)end function Network.GetReference(k,...)local m={...}local z=table.remove(m)assert(W[z],"\073\110\118\097\108\105\100 \082\101\102\101\114\101\110\099\101 \084\121\112\101")local B=w[z]for W,k in ipairs(m)do B=B[k]if not B then break end end local O=B and B.__Data return O and O.Reference or nil end end
--[[ Reference extension ]]
--

do
	local ReferenceTypes = {
		Character = {},
		CharacterPart = {},
	}

	local References = {}
	local Objects = {}

	for i, v in pairs(ReferenceTypes) do
		References[i] = {}
		Objects[i] = {}
	end

	function Network:AddReference(key, refType, ...)
		local refInfo = ReferenceTypes[refType]
		assert(refInfo, "Invalid Reference Type")

		local refData = {
			Type = refType,
			Reference = key,
			Objects = { ... },
			Aliases = {},
		}

		References[refType][refData.Reference] = refData

		local last = Objects[refType]
		for _, obj in ipairs(refData.Objects) do
			local list = last[obj] or {}
			last[obj] = list
			last = list
		end

		last.__Data = refData
	end

	function Network:AddReferenceAlias(key, refType, ...)
		local refInfo = ReferenceTypes[refType]
		assert(refInfo, "Invalid Reference Type")

		local refData = References[refType][key]
		if not refData then
			warn("Tried to add an alias to a non-existing reference")
			return
		end

		local objects = { ... }
		refData.Aliases[#refData.Aliases + 1] = objects

		local last = Objects[refType]
		for _, obj in ipairs(objects) do
			local list = last[obj] or {}
			last[obj] = list
			last = list
		end

		last.__Data = refData
	end

	function Network:RemoveReference(key, refType)
		local refInfo = ReferenceTypes[refType]
		assert(refInfo, "Invalid Reference Type")

		local refData = References[refType][key]
		if not refData then
			warn("Tried to remove a non-existing reference")
			return
		end

		References[refType][refData.Reference] = nil

		local function rem(parent, objects, index)
			if index <= #objects then
				local key = objects[index]
				local child = parent[key]

				rem(child, objects, index + 1)

				if next(child) == nil then
					parent[key] = nil
				end
			elseif parent.__Data == refData then
				parent.__Data = nil
			end
		end

		local objects = Objects[refData.Type]
		rem(objects, refData.Objects, 1)

		for i, alias in ipairs(refData.Aliases) do
			rem(objects, alias, 1)
		end
	end

	function Network:GetObject(ref, refType)
		assert(ReferenceTypes[refType], "Invalid Reference Type")

		local refData = References[refType][ref]
		if not refData then
			return nil
		end

		return unpack(refData.Objects)
	end

	function Network:GetReference(...)
		local objects = { ... }

		local refType = table.remove(objects)
		assert(ReferenceTypes[refType], "Invalid Reference Type")

		local last = Objects[refType]
		for i, v in ipairs(objects) do
			last = last[v]

			if not last then
				break
			end
		end

		local refData = last and last.__Data
		return refData and refData.Reference or nil
	end
end
