--This functions saves our settings, or sets up for a load.

rawset(_G, "SAsaveorload", function(p, dosave, doload, silent)
if not (p.valid and p.yusonictable)
return false
end
if (doload and p.yusonictable.hasloaded)
return false
end

local yu = p.yusonictable
local s1 = nil --S1 means Save 1. Leftover idea from Samus.

	if dosave
		--First, setup a table, and what to default to. We save in numbers to keep the script small in size.
		s1 = {
		
			customtrail = 0, --Customtrail color. Number value. Check for maxcolors first! DEFAULT: NIL
			
			customtrailshiny = 0, --Additive blending. True or false DEFAULT: FALSE
			customtrailtaper = 0, --Trail tapering. True or false  DEFAULT: FALSE
			ballspinonly = 1, --SA2 sflair. True or false. DEFAULT: TRUE
			noaura = 0, --SA1 flair. True or false. If BallspinOnly but not NoAura, you have Hybrid flair. DEFAULT: FALSE
			yuvoice = 1, --Sonic Voice. True or False. DEFAULT: TRUE
			yumusic = 1, --SA-Sonic Music. True or False. DEFAULT: TRUE
			
			spindashcontrol = 2, --Spin Smoothing. From 2 to 6. DEFAULT: 2
			
			spinguideline = 1, --Spindash Aiming Guide. True or False. DEFAULT: TRUE
			noflashes = 0, --Flash Effects. True or False. DEFAULT: FALSE
			noquakes  =  0, --Quake Effects. True or False. DEFAULT: FALSE
			bhz = 0, --Has beaten Black Hole Zone. True or False. DEFAULT: FALSE
			hasbeennotified = 1, --Notified about SAmenu. If we're saving, that means you're already in the menu, so default to true.
			spcolor = 1, --1 or 0 is SA2, 2 is SADX, 3 is Advance.
		}
			--Condense this a bit awkwardly to save space.
			if yu.customtrail s1.customtrail = R_GetNameByColor(yu.customtrail) end
			if yu.customtrailshiny s1.customtrailshiny = 1 end
			if yu.customtrailtaper s1.customtrailtaper = 1 end
			if not yu.ballspinonly s1.ballspinonly = 0 end
			if yu.noaura s1.noaura = 1 end
			if not yu.yuvoice s1.yuvoice = 0 end
			if not yu.yumusic s1.yumusic = 0 end
			if yu.spindashcontrol s1.spindashcontrol = yu.spindashcontrol end
			if not yu.spinguideline s1.spinguideline = 0 end
			if yu.noflashes s1.noflashes = 1 end
			if yu.noquakes s1.snoquakes = 1 end
			if yu.bhz
				if yu.spawnmemories
				s1.bhz = 2
				else
				s1.bhz = 1
				end
			end
			if yu.spcolor
				if yu.spcolor == 2
					s1.spcolor = 2
				elseif yu.spcolor == 3
					s1.spcolor = 3
				end
			end
	end
	
--Are we loading? Or saving?

	if doload --We're loading!
		if yu.hasloaded --But only ever load ONE time per game!
		return false
		end
				
		local file = io.openlocal("client/AdventureSonic/SAsettingsv1-3.dat")
			if file 
			local code = file:read("*a")
				if code ~= nil
				and (string.find(code, "SAyuload "))
				and not (string.find(code, ";"))
					COM_BufInsertText(p, tostring(code))
				end		
				file:close()
			end				
			return true
					
	elseif dosave and s1 --We're saving!
		if io and (p == consoleplayer)
			local file = io.openlocal("client/AdventureSonic/SAsettingsv1-3.dat", "w+")
						
				file:write("SAyuload "+s1.customtrail+" "+s1.customtrailshiny+" "+s1.customtrailtaper+" "
				+s1.ballspinonly+" "+s1.noaura+" "+s1.yuvoice+" "+s1.yumusic+" "
				+s1.spindashcontrol+" "+s1.spinguideline+" "+s1.noflashes+" "+s1.snoquakes+" "
				+s1.bhz+" "+s1.hasbeennotified+" "+s1.spcolor)

			file:close()
			if not silent
			CONS_Printf(p, "\x83"+"Updated saved settings for Adventure Sonic!")
			end
			yu.hasloaded = true
			return true
		end
	end
	return false
end)

--Why do we need a command to load stuff for us? Because otherwise it's clientside and won't load in netgames. Dumb, I know.

COM_AddCommand("SAyuload", function(p, a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14)
if a13 == nil
//CONS_Printf(p, "\x85"+"Error: invalid command input. Don't use this command manually!")
	return false
end
if not (p.valid and p.yusonictable)
or (gamestate ~= GS_LEVEL)
	return false
end
if p.yusonictable.hasloaded
//	CONS_Printf(p, "\x85"+"Can only load once.")
	return false
end
--All of you! Numbers, now!!
a2 = tonumber(a2) a3 = tonumber(a3) a4 = tonumber(a4) a5 = tonumber(a5)
a6 = tonumber(a6) a7 = tonumber(a7) a8 = tonumber(a8) a9 = tonumber(a9) a10 = tonumber(a10)
a11 = tonumber(a11) a12 = tonumber(a12) a13 = tonumber(a13) a14 = tonumber(a14)
local yu = p.yusonictable

			--Custom trails first.
 			if a1		
				--Skincolor conversion.			
				if (R_GetColorByName(a1))
					a1 = R_GetColorByName(a1)
				elseif (R_GetSuperColorByName(a1))
					a1 = R_GetSuperColorByName(a1)
				else
					a1 = tonumber(a1)
				end	
				if a1 --Sanity check.
					--Get our converted color!
					if not (a1 > (#skincolors - 1))
						yu.customtrail = a1
							if a2
							yu.customtrailshiny = true
							end
							if a3
							yu.customtrailtaper = true
							end
					else
						CONS_Printf(p, "\x85"+"Failed to load custom spintrail. Missing skincolor mods can occasionally mess this up. Defaulting to\x83 AUTO\x85 to prevent crash.")
					end
				end
			end
			
			if a4
				yu.ballspinonly = true
			else
				yu.ballspinonly = nil
			end			
			if a5
				yu.noaura = true
			else
				yu.noaura = nil
			end
			if not a6
				yu.yuvoice = false
			end
			if not a7
				yu.yumusic = false
			end
			if a8
				if (a8 < 2) or (a8 > 6) --Good to have failsafes.
					yu.spindashcontrol = 2
				else
					yu.spindashcontrol = a8
				end
			end
			if not a9
				yu.spinguideline = false
			end
			if a10
				yu.noflashes = true
			end
			if a11
				yu.noquakes = true
			end
			if a12
				if a12 == 2
					yu.bhz = 2
				else
					yu.bhz = 1
				end
				if not netgame
				and not multiplayer
					if a12 == 2
						yu.spawnmemories = true
						yu.bhz = 1
					else
						yu.hyperpower = true
						yu.bhz = 1
					end
				end
			end
			if a13
				yu.hasbeennotified = true
			end
			
			yu.spcolor = 1 --Backup and default.
			if a14
				if a14 == 2
					yu.spcolor = 2
					if not netgame and (p.mo and p.mo.valid)
						p.skincolor = SKINCOLOR_SADXSONIC
						p.mo.color = SKINCOLOR_SADXSONIC
					end
				elseif a14 == 3
					yu.spcolor = 3
					if not netgame and (p.mo and p.mo.valid)
						p.skincolor = SKINCOLOR_ADVANCESONIC
						p.mo.color = SKINCOLOR_ADVANCESONIC	
					end
				end
			end
			
			yu.hasloaded = true
			CONS_Printf(p, "\x8b"+"Loaded Adventure Sonic settings!")
end)