function DataStore2.Combine(B,...)for f,e in pairs({...})do combinedDataStoreInfo[e]=B end end function DataStore2.ClearCache()DataStoreCache={}end function DataStore2.SaveAll(B)if DataStoreCache[B]then for B,f in pairs(DataStoreCache[B])do if f.combinedStore==nil then f:Save()end end end end function DataStore2.PatchGlobalSettings(B)for B,f in pairs(B)do assert(Settings[B]~=nil,"\078\111 \115\117\099\104 \107\101\121 \101\120\105\115\116\115\058 "..B)Settings[B]=f end end function DataStore2.__call(B,f,e)assert(typeof(f)=="\115\116\114\105\110\103"and typeof(e)=="\073\110\115\116\097\110\099\101",("\068\097\116\097\083\116\111\114\101\050\040\041 \065\080\073 \099\097\108\108 \101\120\112\101\099\116\101\100 \123\115\116\114\105\110\103 \100\097\116\097\083\116\111\114\101\078\097\109\101\044 \073\110\115\116\097\110\099\101 \112\108\097\121\101\114\125\044 \103\111\116 \123\037\115\044 \037\115\125"):format(typeof(f),typeof(e)))if DataStoreCache[e]and DataStoreCache[e][f]then return DataStoreCache[e][f]elseif combinedDataStoreInfo[f]then local B=DataStore2(combinedDataStoreInfo[f],e)B:BeforeSave(function(B)for f in pairs(B)do if combinedDataStoreInfo[f]then local L=DataStore2(f,e)local N=L:Get(nil,true)if N~=nil then if L.combinedBeforeSave then N=L.combinedBeforeSave(clone(N))end B[f]=N end end end return B end)local L=setmetatable({combinedName=f;combinedStore=B},{__index=function(f,e)return CombinedDataStore[e]or B[e]end})if not DataStoreCache[e]then DataStoreCache[e]={}end DataStoreCache[e][f]=L return L end local L={}L.Name=f L.UserId=e.UserId L.callbacks={}L.beforeInitialGet={}L.afterSave={}L.bindToClose={}L.savingMethod=SavingMethods[Settings.SavingMethod].new(L)setmetatable(L,DataStoreMetatable)local N,m=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116"),false game:BindToClose(function()if not m then spawn(function()e.Parent=nil end)N.Event:wait()end local B=L:Get(nil,true)for f,L in pairs(L.bindToClose)do L(e,B)end end)local z z=e.AncestryChanged:Connect(function()if e:IsDescendantOf(game)then return end z:Disconnect();(((L:SaveAsync()):andThen(function()print("\112\108\097\121\101\114 \108\101\102\116\044 \115\097\118\101\100 "..f)end)):catch(function(B)warn("\101\114\114\111\114 \119\104\101\110 \112\108\097\121\101\114 \108\101\102\116\033 "..B)end)):finally(function()N:Fire()m=true end)delay(40,function()DataStoreCache[e]=nil end)end)if not DataStoreCache[e]then DataStoreCache[e]={}end DataStoreCache[e][f]=L return L end DataStore2.Constants=Constants return setmetatable(DataStore2,DataStore2)

function DataStore2.Combine(mainKey, ...)
	for _, name in pairs({...}) do
		combinedDataStoreInfo[name] = mainKey
	end
end

function DataStore2.ClearCache()
	DataStoreCache = {}
end

function DataStore2.SaveAll(player)
	if DataStoreCache[player] then
		for _, dataStore in pairs(DataStoreCache[player]) do
			if dataStore.combinedStore == nil then
				dataStore:Save()
			end
		end
	end
end

function DataStore2.PatchGlobalSettings(patch)
	for key, value in pairs(patch) do
		assert(Settings[key] ~= nil, "No such key exists: " .. key)
		-- TODO: Implement type checking with this when osyris' t is in
		Settings[key] = value
	end
end

function DataStore2.__call(_, dataStoreName, player)
	assert(
		typeof(dataStoreName) == "string" and typeof(player) == "Instance",
		("DataStore2() API call expected {string dataStoreName, Instance player}, got {%s, %s}")
		:format(
			typeof(dataStoreName),
			typeof(player)
		)
	)

	if DataStoreCache[player] and DataStoreCache[player][dataStoreName] then
		return DataStoreCache[player][dataStoreName]
	elseif combinedDataStoreInfo[dataStoreName] then
		local dataStore = DataStore2(combinedDataStoreInfo[dataStoreName], player)

		dataStore:BeforeSave(function(combinedData)
			for key in pairs(combinedData) do
				if combinedDataStoreInfo[key] then
					local combinedStore = DataStore2(key, player)
					local value = combinedStore:Get(nil, true)
					if value ~= nil then
						if combinedStore.combinedBeforeSave then
							value = combinedStore.combinedBeforeSave(clone(value))
						end
						combinedData[key] = value
					end
				end
			end

			return combinedData
		end)

		local combinedStore = setmetatable({
			combinedName = dataStoreName,
			combinedStore = dataStore,
		}, {
			__index = function(_, key)
				return CombinedDataStore[key] or dataStore[key]
			end
		})

		if not DataStoreCache[player] then
			DataStoreCache[player] = {}
		end

		DataStoreCache[player][dataStoreName] = combinedStore
		return combinedStore
	end

	local dataStore = {}

	dataStore.Name = dataStoreName
	dataStore.UserId = player.UserId

	dataStore.callbacks = {}
	dataStore.beforeInitialGet = {}
	dataStore.afterSave = {}
	dataStore.bindToClose = {}
	dataStore.savingMethod = SavingMethods[Settings.SavingMethod].new(dataStore)

	setmetatable(dataStore, DataStoreMetatable)

	local event, fired = Instance.new("BindableEvent"), false

	game:BindToClose(function()
		if not fired then
			spawn(function()
				player.Parent = nil -- Forces AncestryChanged to fire and save the data
			end)

			event.Event:wait()
		end

		local value = dataStore:Get(nil, true)

		for _, bindToClose in pairs(dataStore.bindToClose) do
			bindToClose(player, value)
		end
	end)

	local playerLeavingConnection
	playerLeavingConnection = player.AncestryChanged:Connect(function()
		if player:IsDescendantOf(game) then return end
		playerLeavingConnection:Disconnect()
		dataStore:SaveAsync():andThen(function()
			print("player left, saved " .. dataStoreName)
		end):catch(function(error)
			-- TODO: Something more elegant
			warn("error when player left! " .. error)
		end):finally(function()
			event:Fire()
			fired = true
		end)

		delay(40, function() --Give a long delay for people who haven't figured out the cache :^(
			DataStoreCache[player] = nil
		end)
	end)

	if not DataStoreCache[player] then
		DataStoreCache[player] = {}
	end

	DataStoreCache[player][dataStoreName] = dataStore

	return dataStore
end

DataStore2.Constants = Constants

return setmetatable(DataStore2, DataStore2)
