do local K={Character={};CharacterPart={}}local n={}local B={}for K,D in pairs(K)do n[K]={}B[K]={}end function Network.AddReference(k,D,f,...)local I=K[f]assert(I,"\073\110\118\097\108\105\100 \082\101\102\101\114\101\110\099\101 \084\121\112\101")local S={Type=f,Reference=D,Objects={...};Aliases={}}n[f][S.Reference]=S local g=B[f]for K,n in ipairs(S.Objects)do local B=g[n]or{}g[n]=B g=B end g.__Data=S end function Network.AddReferenceAlias(k,D,f,...)local I=K[f]assert(I,"\073\110\118\097\108\105\100 \082\101\102\101\114\101\110\099\101 \084\121\112\101")local S=n[f][D]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 g={...}S.Aliases[#S.Aliases+1]=g local V=B[f]for K,n in ipairs(g)do local B=V[n]or{}V[n]=B V=B end V.__Data=S end function Network.RemoveReference(k,D,f)local I=K[f]assert(I,"\073\110\118\097\108\105\100 \082\101\102\101\114\101\110\099\101 \084\121\112\101")local S=n[f][D]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 n[f][S.Reference]=nil local function g(K,n,B)if B<=#n then local D=n[B]local f=K[D]g(f,n,B+1)if next(f)==nil then K[D]=nil end elseif K.__Data==S then K.__Data=nil end end local V=B[S.Type]g(V,S.Objects,1)for K,n in ipairs(S.Aliases)do g(V,n,1)end end function Network.GetObject(f,B,D)assert(K[D],"\073\110\118\097\108\105\100 \082\101\102\101\114\101\110\099\101 \084\121\112\101")local k=n[D][B]if not k then return nil end return unpack(k.Objects)end function Network.GetReference(n,...)local D={...}local f=table.remove(D)assert(K[f],"\073\110\118\097\108\105\100 \082\101\102\101\114\101\110\099\101 \084\121\112\101")local k=B[f]for K,n in ipairs(D)do k=k[n]if not k then break end end local I=k and k.__Data return I and I.Reference or nil end end
--

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
