local n=_G.HDAdminMain local T={"\067\111\110\116\114\105\098\117\116\111\114\115","\080\114\101\102\105\120\101\115";"\082\097\110\107";"\065\108\105\097\115\101\115","\084\097\103\115";"\068\101\115\099\114\105\112\116\105\111\110","\065\114\103\115","\076\111\111\112\097\098\108\101"}function module.Setup(T)local Y={n.lighting,n.rs;n.ss;n.starterPack}for T,Y in pairs(Y)do for T,Y in pairs(Y:GetDescendants())do if Y:IsA("\084\111\111\108")and Y.Parent.Name~="\066\117\105\108\100\105\110\103\084\111\111\108\115"then table.insert(n.listOfTools,Y)end end end for T,Y in pairs(n.server.Morphs:GetChildren())do n.morphNames[tostring(Y.Name)]=true end for T,Y in pairs(n.listOfTools)do n.toolNames[tostring(Y.Name)]=true end local F={}for n,T in pairs(n.settings.RankRequiredToViewRankType)do n=string.lower(n)F[n]=T end n.settings.RankRequiredToViewRankType=F local v={}for T,Y in pairs(n.settings.RankRequiredToViewRank)do T=tonumber(T)or(n:GetModule("\099\102")):GetRankId(T)v[T]=Y end n.settings.RankRequiredToViewRank=v for T,Y in pairs(n.settings)do if T=="\066\097\116\099\104\075\101\121"then n.settings.BatchKey=tostring(Y)elseif T=="\067\104\097\116\067\111\108\111\114\115"then local T={}for Y,F in pairs(Y)do local v=Y local G=F if not tonumber(Y)then v=(n:GetModule("\099\102")):GetRankId(Y)end if type(F)~="\117\115\101\114\100\097\116\097"then G=(n:GetModule("\099\102")):GetColorFromString(F)end T[v]=G end n.settings.ChatColors=T elseif T=="\067\111\114\101\078\111\116\105\099\101\115"then for T,Y in pairs(Y)do(n:GetModule("\067\111\114\101\078\111\116\105\099\101\115"))[T]=Y end elseif T=="\073\103\110\111\114\101\083\099\097\108\101\076\105\109\105\116"or T=="\086\105\101\119\066\097\110\108\097\110\100"or T=="\073\103\110\111\114\101\071\101\097\114\066\108\097\099\107\108\105\115\116"or T=="\073\103\110\111\114\101\067\111\109\109\097\110\100\076\105\109\105\116\080\101\114\077\105\110\117\116\101"then if tonumber(Y)==nil then n.settings[T]=(n:GetModule("\099\102")):GetRankId(Y)end elseif T=="\082\097\110\107\082\101\113\117\105\114\101\100\084\111\086\105\101\119\080\097\103\101"or T=="\082\097\110\107\082\101\113\117\105\114\101\100\084\111\086\105\101\119\082\097\110\107"or T=="\082\097\110\107\082\101\113\117\105\114\101\100\084\111\086\105\101\119\082\097\110\107\084\121\112\101"then for Y,F in pairs(Y)do if tonumber(F)==nil then n.settings[T][Y]=(n:GetModule("\099\102")):GetRankId(F)end end elseif T=="\066\097\110\110\101\100"then for T,Y in pairs(Y)do(coroutine.wrap(function()local T=tonumber(Y)if not T then T=(n:GetModule("\099\102")):GetUserId(Y)end T=tonumber(T)or(n:GetModule("\099\102")):GetUserId(T)local F={UserId=T;BanTime="";Reason="",Server="\065\108\108";SettingsBan=true,BannedBy=n.ownerId}table.insert(n.settingsBanRecords,F)n.banned[tostring(T)]=true end))()end elseif T=="\086\073\080\083\101\114\118\101\114\067\111\109\109\097\110\100\066\108\097\099\107\108\105\115\116"then n.settings[T]=type(Y)=="\116\097\098\108\101"and Y or{}for T,Y in pairs(Y)do n.blacklistedVipServerCommands[string.lower(Y)]=true end elseif T=="\067\111\109\109\097\110\100\076\105\109\105\116\115"then n.settings[T]=type(Y)=="\116\097\098\108\101"and Y or{}for Y,F in pairs(Y)do local v=string.lower(Y)n.settings[T][v]=F if v~=Y then n.settings[T][Y]=nil end end elseif T=="\078\111\116\105\099\101\083\111\117\110\100\073\100"or T=="\069\114\114\111\114\083\111\117\110\100\073\100"then if Y==261082034 then n.settings[T]=2865227271 n.settings.NoticeVolume=.1 elseif Y==138090596 then n.settings[T]=2865228021 n.settings.ErrorVolume=.1 end elseif T=="\082\097\110\107\115"then for T,Y in pairs(Y)do local F=Y[1]local v=Y[2]local G=Y[3]if F>5 then F=5 elseif F<0 then F=0 end if G==nil or F==5 or F==0 then G={}end for T,Y in pairs(G)do if tonumber(Y)then Y=(n:GetModule("\099\102")):GetName(Y)end n.permissions.specificUsers[Y]=F end end elseif T=="\071\097\109\101\112\097\115\115\101\115"then for n,T in pairs(n.products)do Y[T]=n end for T,Y in pairs(Y)do T=tonumber(T)if T and T>0 then local F,v=pcall(function()return(n:GetModule("\099\102")):GetProductInfo(T,Enum.InfoType.GamePass)end)if not F or not v.Name then warn("\072\068 \065\100\109\105\110 \124 \089\111\117 \101\110\116\101\114\101\100 \097\110 \105\110\118\097\108\105\100 \071\097\109\101\112\097\115\115\073\100\058 "..tostring(T))else if T and T>0 then if tonumber(Y)==nil then Y=(n:GetModule("\099\102")):GetRankId(Y)end if T then v.Rank=Y n.permissions.gamepasses[tostring(T)]=v end end end end end elseif T=="\065\115\115\101\116\115"then for T,Y in pairs(Y)do T=tonumber(T)if T and T>0 then local F,v=pcall(function()return(n:GetModule("\099\102")):GetProductInfo(T,Enum.InfoType.Asset)end)if not F or not v.Name then warn("\072\068 \065\100\109\105\110 \124 \089\111\117 \101\110\116\101\114\101\100 \097\110 \105\110\118\097\108\105\100 \065\115\115\101\116\073\100\058 "..tostring(T))else if tonumber(Y)==nil then Y=(n:GetModule("\099\102")):GetRankId(Y)end if T then v.Rank=Y n.permissions.assets[tostring(T)]=v end end end end elseif T=="\071\114\111\117\112\115"then Y[n.hdAdminGroup.Id]={}for T,Y in pairs(Y)do T=tonumber(T)if T and T>0 then local F,v=pcall(function()return n.groupService:GetGroupInfoAsync(T)end)if not F then warn("\072\068 \065\100\109\105\110 \124 \089\111\117 \101\110\116\101\114\101\100 \097\110 \105\110\118\097\108\105\100 \071\114\111\117\112\073\100\058 "..tostring(T))else local F=v.Roles if F then local T={}for n,Y in pairs(F)do T[Y.Name]={}T[Y.Name].GroupRank=Y.Rank end v.Roles=T for Y,F in pairs(Y)do if tonumber(Y)then for n,T in pairs(T)do if T.GroupRank==Y then Y=n end end end if tonumber(F)==nil then F=(n:GetModule("\099\102")):GetRankId(F)end if v.Roles[Y]then v.Roles[Y].Rank=F else end end end if T==n.hdAdminGroup.Id then n.hdAdminGroup.Info=v else n.permissions.groups[tostring(T)]=v end end end end elseif T=="\070\114\105\101\110\100\115"or T=="\070\114\101\101\065\100\109\105\110"or T=="\086\105\112\083\101\114\118\101\114\079\119\110\101\114"or T=="\086\105\112\083\101\114\118\101\114\080\108\097\121\101\114\115"then local F=Y if tonumber(F)==nil then F=(n:GetModule("\099\102")):GetRankId(F)end local v=string.lower(string.sub(T,1,1))local G=string.gsub(T,"\037\097",v,1)n.permissions[G]=F end end end return module

local main = _G.HDAdminMain
local commandInfoForClient = {"Contributors", "Prefixes", "Rank", "Aliases", "Tags", "Description", "Args", "Loopable"}



function module:Setup()
	
	--Grab all tools
	local locationsToScan = {main.lighting, main.rs, main.ss, main.starterPack}
	for _, l in pairs(locationsToScan) do
		for a,b in pairs(l:GetDescendants()) do
			if b:IsA("Tool") and b.Parent.Name ~= "BuildingTools" then
				table.insert(main.listOfTools, b)
			end
		end
	end
					
	--Setup morphNames and toolNames
	for _, morph in pairs(main.server.Morphs:GetChildren()) do
		main.morphNames[tostring(morph.Name)] = true
	end
	for _, tool in pairs(main.listOfTools) do
		main.toolNames[tostring(tool.Name)] = true
	end
	
	--
	local newRankRequiredToViewRankType = {}
	for subSettingName, rankId in pairs(main.settings.RankRequiredToViewRankType) do
		subSettingName = string.lower(subSettingName)
		newRankRequiredToViewRankType[subSettingName] = rankId
	end
	main.settings.RankRequiredToViewRankType = newRankRequiredToViewRankType
	--
	local newRankRequiredToViewRank = {}
	for subSettingName, rankId in pairs(main.settings.RankRequiredToViewRank) do
		subSettingName = tonumber(subSettingName) or main:GetModule("cf"):GetRankId(subSettingName)
		newRankRequiredToViewRank[subSettingName] = rankId
	end
	main.settings.RankRequiredToViewRank = newRankRequiredToViewRank
	--
	for settingName, details in pairs(main.settings) do
		if settingName == "BatchKey" then
			main.settings.BatchKey = tostring(details)
			
		elseif settingName == "ChatColors" then
			local newSetting = {}
			for rank, color in pairs(details) do
				local chatRankId = rank
				local chatColor3 = color
				if not tonumber(rank) then
					chatRankId = main:GetModule("cf"):GetRankId(rank)
				end
				if type(color) ~= "userdata" then
					chatColor3 = main:GetModule("cf"):GetColorFromString(color)
				end
				newSetting[chatRankId] = chatColor3
			end
			main.settings.ChatColors = newSetting
			
		elseif settingName == "CoreNotices" then
			for noticeName, notice in pairs(details) do
				main:GetModule("CoreNotices")[noticeName] = notice
			end
			
		elseif settingName == "IgnoreScaleLimit" or settingName == "ViewBanland" or settingName == "IgnoreGearBlacklist" or settingName == "IgnoreCommandLimitPerMinute" then
			if tonumber(details) == nil then
				main.settings[settingName] = main:GetModule("cf"):GetRankId(details)
			end
		
		elseif settingName == "RankRequiredToViewPage" or settingName == "RankRequiredToViewRank" or settingName == "RankRequiredToViewRankType" then
			for subSettingName, rankId in pairs(details) do
				if tonumber(rankId) == nil then
					main.settings[settingName][subSettingName] = main:GetModule("cf"):GetRankId(rankId)
				end
			end
		
		elseif settingName == "Banned" then
			for i,userIdSet in pairs(details) do
				coroutine.wrap(function()
					local userId = tonumber(userIdSet)
					if not userId then
						userId = main:GetModule("cf"):GetUserId(userIdSet)
					end
					userId = tonumber(userId) or main:GetModule("cf"):GetUserId(userId)
					local record = {
						UserId = userId;
						BanTime = "";
						Reason = "";
						Server = "All";
						SettingsBan = true;
						BannedBy = main.ownerId;
					}
					table.insert(main.settingsBanRecords, record)
					main.banned[tostring(userId)] = true
				end)()
			end
			
		elseif settingName == "VIPServerCommandBlacklist" then
			main.settings[settingName] = (type(details) == "table" and details) or {}
			for i,v in pairs(details) do
				main.blacklistedVipServerCommands[string.lower(v)] = true
			end
			
		elseif settingName == "CommandLimits" then
			main.settings[settingName] = (type(details) == "table" and details) or {}
			for commandName, limitDetails in pairs(details) do
				local lowercaseName = string.lower(commandName)
				main.settings[settingName][lowercaseName] = limitDetails
				if lowercaseName ~= commandName then
					main.settings[settingName][commandName] = nil
				end
			end
		
		elseif settingName == "NoticeSoundId" or settingName == "ErrorSoundId" then
			if details == 261082034 then -- Notice
				main.settings[settingName] = 2865227271
				main.settings.NoticeVolume = 0.1
			elseif details == 138090596 then -- Error
				main.settings[settingName] = 2865228021
				main.settings.ErrorVolume = 0.1
			end
			
		elseif settingName == "Ranks" then
			for _, rankDetails in pairs(details) do
				local rankId = rankDetails[1]
				local rankName = rankDetails[2]
				local specificUsers = rankDetails[3]
				if rankId > 5 then
					rankId = 5
				elseif rankId < 0 then
					rankId = 0
				end
				if specificUsers == nil or rankId == 5 or rankId == 0 then
					specificUsers = {}
				end
				for i,plrName in pairs(specificUsers) do
					if tonumber(plrName) then
						plrName = main:GetModule("cf"):GetName(plrName)
					end
					main.permissions.specificUsers[plrName] = rankId
				end
			end
				
		elseif settingName == "Gamepasses" then
			for productName, productId in pairs(main.products) do
				details[productId] = productName
			end
			for gamepassId, rankId in pairs(details) do
				gamepassId = tonumber(gamepassId)
				if gamepassId and gamepassId > 0 then
					local success, productInfo = pcall(function() return(main:GetModule("cf"):GetProductInfo(gamepassId, Enum.InfoType.GamePass)) end)
					if not success or not productInfo.Name then
						warn("HD Admin | You entered an invalid GamepassId: ".. tostring(gamepassId))
					else
						if gamepassId and gamepassId > 0 then
							if tonumber(rankId) == nil then
								rankId = main:GetModule("cf"):GetRankId(rankId)
							end
							if gamepassId then
								productInfo.Rank = rankId
								main.permissions.gamepasses[tostring(gamepassId)] = productInfo
							end
						end
					end
				end
			end
		
		elseif settingName == "Assets" then
			for assetId, rankId in pairs(details) do
				assetId = tonumber(assetId)
				if assetId and assetId > 0 then
					local success, productInfo = pcall(function() return(main:GetModule("cf"):GetProductInfo(assetId, Enum.InfoType.Asset)) end)
					if not success or not productInfo.Name then
						warn("HD Admin | You entered an invalid AssetId: ".. tostring(assetId))
					else
						if tonumber(rankId) == nil then
							rankId = main:GetModule("cf"):GetRankId(rankId)
						end
						if assetId then
							productInfo.Rank = rankId
							main.permissions.assets[tostring(assetId)] = productInfo
						end
					end
				end
			end
			
		elseif settingName == "Groups" then
			details[main.hdAdminGroup.Id] = {}
			for groupId, groupDetails in pairs(details) do
				groupId = tonumber(groupId)
				if groupId and groupId > 0 then
					local success, groupInfo = pcall(function() return(main.groupService:GetGroupInfoAsync(groupId)) end)
					if not success then
						warn("HD Admin | You entered an invalid GroupId: ".. tostring(groupId))
					else
						local roles = groupInfo.Roles
						if roles then
							local newRoles = {}
							for _, roleDetails in pairs(roles) do
								newRoles[roleDetails.Name] = {}
								newRoles[roleDetails.Name].GroupRank = roleDetails.Rank
							end
							groupInfo.Roles = newRoles
							for roleName, rankId in pairs(groupDetails) do
								if tonumber(roleName) then
									for rN, roleDetails in pairs(newRoles) do
										if roleDetails.GroupRank == roleName then
											roleName = rN
										end
									end
								end
								if tonumber(rankId) == nil then
									rankId = main:GetModule("cf"):GetRankId(rankId)
								end
								if groupInfo.Roles[roleName] then
									groupInfo.Roles[roleName].Rank = rankId
								else
									--warn("HD Admin: Entered incorrect role Name/ID. That role does not exists.")
								end
							end
						end
						if groupId == main.hdAdminGroup.Id then
							main.hdAdminGroup.Info = groupInfo
						else
							main.permissions.groups[tostring(groupId)] = groupInfo
						end
					end
				end
			end
			
		elseif settingName == "Friends" or settingName == "FreeAdmin" or settingName == "VipServerOwner" or settingName == "VipServerPlayers" then
			local rankId = details
			if tonumber(rankId) == nil then
				rankId = main:GetModule("cf"):GetRankId(rankId)
			end
			local lowerCase = string.lower(string.sub(settingName,1,1))
			local variableName = string.gsub(settingName,"%a",lowerCase, 1)
			main.permissions[variableName] = rankId
				
		end
	end
end




return module
