function DataStore2.Combine(o,...)for m,U in pairs({...})do combinedDataStoreInfo[U]=o end end function DataStore2.ClearCache()DataStoreCache={}end function DataStore2.SaveAll(o)if DataStoreCache[o]then for o,m in pairs(DataStoreCache[o])do if m.combinedStore==nil then m:Save()end end end end function DataStore2.PatchGlobalSettings(o)for o,m in pairs(o)do assert(Settings[o]~=nil,"\078\111 \115\117\099\104 \107\101\121 \101\120\105\115\116\115\058 "..o)Settings[o]=m end end function DataStore2.__call(o,m,U)assert(typeof(m)=="\115\116\114\105\110\103"and typeof(U)=="\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(m),typeof(U)))if DataStoreCache[U]and DataStoreCache[U][m]then return DataStoreCache[U][m]elseif combinedDataStoreInfo[m]then local o=DataStore2(combinedDataStoreInfo[m],U)o:BeforeSave(function(o)for m in pairs(o)do if combinedDataStoreInfo[m]then local y=DataStore2(m,U)local V=y:Get(nil,true)if V~=nil then if y.combinedBeforeSave then V=y.combinedBeforeSave(clone(V))end o[m]=V end end end return o end)local y=setmetatable({combinedName=m,combinedStore=o},{__index=function(m,U)return CombinedDataStore[U]or o[U]end})if not DataStoreCache[U]then DataStoreCache[U]={}end DataStoreCache[U][m]=y return y end local y={}y.Name=m y.UserId=U.UserId y.callbacks={}y.beforeInitialGet={}y.afterSave={}y.bindToClose={}y.savingMethod=SavingMethods[Settings.SavingMethod].new(y)setmetatable(y,DataStoreMetatable)local V,G=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116"),false game:BindToClose(function()local o=y:Get(nil,true)for m,y in pairs(y.bindToClose)do y(U,o)end end)local R R=U.AncestryChanged:Connect(function()if U:IsDescendantOf(game)then return end R:Disconnect();(((y:SaveAsync()):andThen(function()print("\112\108\097\121\101\114 \108\101\102\116\044 \115\097\118\101\100 "..m)end)):catch(function(o)warn("\101\114\114\111\114 \119\104\101\110 \112\108\097\121\101\114 \108\101\102\116\033 "..o)end)):finally(function()V:Fire()G=true end)delay(40,function()DataStoreCache[U]=nil end)end)if not DataStoreCache[U]then DataStoreCache[U]={}end DataStoreCache[U][m]=y return y end DataStore2.Constants=Constants return setmetatable(DataStore2,DataStore2)
--[[**
	<description>
	Run this once to combine all keys provided into one "main key".
	Internally, this means that data will be stored in a table with the key mainKey.
	This is used to get around the 2-DataStore2 reliability caveat.
	</description>

	<parameter name = "mainKey">
	The key that will be used to house the table.
	</parameter>

	<parameter name = "...">
	All the keys to combine under one table.
	</parameter>
**--]]
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)
