function ServiceBag._addServiceType(E,c)if not E._serviceTypesToInitializeSet then error(("\065\108\114\101\097\100\121 \102\105\110\105\115\104\101\100 \105\110\105\116\105\097\108\105\122\105\110\103\044 \099\097\110\110\111\116 \097\100\100 \037\113"):format(E:_getServiceName(c)))return end if E._services[c]then return end local w=setmetatable({},{__index=c})E._services[c]=w E:_ensureInitialization(c)end function ServiceBag._ensureInitialization(E,c)if E._initializedServiceTypeSet[c]then return end if E._initializing then E._serviceTypesToInitializeSet[c]=nil E._initializedServiceTypeSet[c]=true E:_initService(c)elseif E._serviceTypesToInitializeSet then E._serviceTypesToInitializeSet[c]=true else local w=E:_getServiceName(c)error(string.format("\067\097\110\110\111\116 \105\110\105\116\105\097\108\105\122\101 \115\101\114\118\105\099\101 \037\113 \112\097\115\116 \105\110\105\116\105\097\108\105\122\105\110\103 \112\104\097\115\101",w))end end function ServiceBag._initService(E,c)local w=assert(E._services[c],"\078\111 \115\101\114\118\105\099\101")local Q=E:_getServiceName(c)if w.Init then local c task.spawn(function()debug.setmemorycategory(Q)c=coroutine.running()w:Init(E)end)local g=coroutine.status(c)=="\100\101\097\100"if not g then error(("\073\110\105\116\105\097\108\105\122\105\110\103 \115\101\114\118\105\099\101 \037\113 \121\105\101\108\100\101\100"):format(Q))end end table.insert(E._serviceTypesToStart,c)end

function ServiceBag:_addServiceType(serviceType)
	if not self._serviceTypesToInitializeSet then
		error(("Already finished initializing, cannot add %q"):format(self:_getServiceName(serviceType)))
		return
	end

	-- Already added
	if self._services[serviceType] then
		return
	end

	-- Construct a new version of this service so we're isolated
	local service = setmetatable({}, { __index = serviceType })
	self._services[serviceType] = service

	self:_ensureInitialization(serviceType)
end

function ServiceBag:_ensureInitialization(serviceType)
	if self._initializedServiceTypeSet[serviceType] then
		return
	end

	if self._initializing then
		self._serviceTypesToInitializeSet[serviceType] = nil
		self._initializedServiceTypeSet[serviceType] = true
		self:_initService(serviceType)
	elseif self._serviceTypesToInitializeSet then
		self._serviceTypesToInitializeSet[serviceType] = true
	else
		local serviceName = self:_getServiceName(serviceType)
		error(string.format("Cannot initialize service %q past initializing phase", serviceName))
	end
end

function ServiceBag:_initService(serviceType)
	local service = assert(self._services[serviceType], "No service")
	local serviceName = self:_getServiceName(serviceType)

	if service.Init then
		local current
		task.spawn(function()
			debug.setmemorycategory(serviceName)
			current = coroutine.running()
			service:Init(self)
		end)

		local isDead = coroutine.status(current) == "dead"
		if not isDead then
			error(("Initializing service %q yielded"):format(serviceName))
		end
	end

	table.insert(self._serviceTypesToStart, serviceType)
end
