--╔════════════════════════════╗═════════════════════════════════════════════════════════════════════════════════#
--║     SETTINGS & SAVING      ║    Here are the functions related to saving, loading, and settings.
--╚════════════════════════════╝═════════════════════════════════════════════════════════════════════════════════#
local RAIL = GS_RAILS if RAIL.blockload return end
local FILENAME = "client/GS-GrindRails/GSgrindsettings-v0.3" --Adding .dat manually later.

RAIL.SaveLoad = function(p, dosave, doload, SILENT, SKIN)
	if not (p and p.valid and type(p.GSRO)=="table") --Golden Shine's Rail Options
		return
	end
	if ((isdedicatedserver) and (p==server))
		p.GSRO.hasloaded = true return --
	end
	local SKINFILE = FILENAME
	
	if doload --We're loading!

		if splitscreen and p==players[1] p.GSRO.hasloaded = true end --You don't get a save!
		if p.GSRO.hasloaded==true return end --Already loaded...
		
		if io
			if (SKIN) and type(SKIN)=="string"
				SKINFILE = $+"["+SKIN+"]"
			elseif p.mo and p.mo.skin and type(p.mo.skin)=="string"
				SKINFILE = $+"["+p.mo.skin+"]"
			end
			SKINFILE = $+".dat"
			local file = io.openlocal(SKINFILE)
			if file==nil --No SKIN-specific file, so load a generic one.
				file = io.openlocal((FILENAME+".dat"))
				if file!=nil
					CONS_Printf(p, "Loading global GS Grind Rails save file...!")
				end
			else --Found a player specific one!
				CONS_Printf(p, "\x83"+"Loading specific GS Grind Rails file for character:\x82 "+p.mo.skin)
			end
			if file!=nil --Found any at all? then load!
				local code = file:read("*a")
				if code ~= nil and (string.find(code, "GSRLoad ")) and not (string.find(code, ";"))
					COM_BufInsertText(p, " "+(tostring(code)))
				end
				file:close()
			end
		end
		return true --
		
	elseif dosave and (p==consoleplayer) and io --We're saving!

		if (SKIN) and type(SKIN)=="string"
			SKINFILE = $+"["+SKIN+"]"
		end
		SKINFILE = $+".dat"
		
		local RO,s1 = p.GSRO,{}

		--Camera stuff.
		s1[1] = (RO.cam_vanilla)	 		and 1 or 0
		s1[2] = (RO.cam_shift==nil) 		and 10 or RO.cam_shift
		s1[3] = (RO.cam_dist==nil)	 		and 192 or RO.cam_dist 
		s1[4] = (RO.cam_height==nil) 		and 40 or RO.cam_height
		s1[5] = (RO.cam_turnspeed==nil)		and 100 or RO.cam_turnspeed
		s1[6] = (RO.cam_autoturnspeed==nil)	and 75 or RO.cam_autoturnspeed
		s1[7] = (RO.cam_centerspeed==nil)	and 150 or RO.cam_centerspeed
		
		--Gameplay stuff	
		s1[8] = (RO.devmode)				and max(1, min(3, RO.devmode)) or 0
		s1[9] = (RO.backupgrindstate) 		and RO.backupgrindstate or SPR2_ROLL 
		s1[10] = (RO.duncegrindstate) 		and RO.duncegrindstate or SPR2_EDGE
		s1[11]=(type(RO.SideHold)=="number")and RO.SideHold or 0
		s1[12] = (RO.HUDaid) 				and RO.HUDaid or 0
	
		--First time or not?	
		s1[13] = (RO.NewbieGrinder==0) 		and 1 or 0
		
		local file,WRITE = io.openlocal(SKINFILE, "w+"), "GSRLoad "
		for i = 1,#s1 WRITE = $+s1[i]+" " end
		file:write(WRITE) file:close()
		if SILENT != 7
			if (SKIN) and type(SKIN)=="string"
				CONS_Printf(p, "\x8b"+"Updated saved settings for\x82"+" GS Grind Rails"+"\x80 ["+"\x88"+SKIN+"\x80"+"]")
			else
				CONS_Printf(p, "\x8b"+"Updated global saved settings for\x82"+" GS Grind Rails")
			end
		end
		RO.hasloaded = true return true --
	end
end

--Why'd I make a command to load stuff? Because otherwise the loaded info becomes clientside and desyncs in netgames!
--No, I DON'T know why we have to do it this way either!
COM_AddCommand("GSRLoad", function(p, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11,s12, s13)
	if (gamestate ~= GS_LEVEL) or not (p.valid) return end
	
	if p==server and (isdedicatedserver)
	or splitscreen==true and p==players[1]
		if p.GSRO p.GSRO.hasloaded = true end
		return --
	end
	local RO = p.GSRO if not (RO) or (RO.hasloaded) return end
	
	--All of you! Numbers, NOW!
	s1,s2,s3,s4,s5 = tonumber($), tonumber($), tonumber($), tonumber($), tonumber($)
	s6,s7,s8,s9,s10 = tonumber($), tonumber($), tonumber($), tonumber($), tonumber($)
	s11,s12,s13 = tonumber($), tonumber($), tonumber($)
	
	--Load the camera settings.
	RO.cam_vanilla = (s1==1) and true or false
	if type(s2)=="number"
		RO.cam_shift = s2
	end
	if type(s3)=="number"
		RO.cam_dist = max(10, s3)
	end
	if type(s4)=="number"
		RO.cam_height = max(1, s4)
	end
	if type(s5)=="number"
		RO.cam_turnspeed = max(1, s5)
	end	
	if type(s6)=="number"
		RO.cam_autoturnspeed = max(0, s6)
	end
	if type(s7)=="number"
		RO.cam_centerspeed = max(1, s7)
	end	
	
	--Now load the gameplay stuff.
	if s8 and type(s8)=="number" and (s8 < 4)
		RO.devmode = max(1, s8)
	end
	local SKIN = (p.mo) and p.mo.skin or p.skin
	if SKIN!=nil
		local MAXSPRITES2 = skins[SKIN].sprites
		if MAXSPRITES2
			MAXSPRITES2 = #MAXSPRITES2
			if MAXSPRITES2
				if type(s9)=="number" and (s9 < MAXSPRITES2)
					RO.backupgrindstate = s9
				end
				if type(s10)=="number" and (s10 < MAXSPRITES2)
					RO.duncegrindstate = s10
				end				
			end
		end
	end
	
	--Now load the gameplay stuff.
	if s11 and type(s11)=="number" and (s11 < 5)
		RO.SideHold = max(0, s11)
	end
	
	--Tutorial setting, called HUDaid in the code.
	if s12==0
		RO.HUDaid,RO.NewbieGrinder = 0,0
	elseif not (s12) or s12==1 or s12==true
		RO.HUDaid = 1
	elseif s12==2
		RO.HUDaid,RO.NewbieGrinder = 2,0
	end
	if (modeattacking) or (marathonmode)
		RO.NewbieGrinder = 0 --Not here...!
	end
	
	--Add a console message that we loaded stuff.
	if (RO.hasloaded!=true)
		CONS_Printf(p, "\x8b"+"Loaded global"+"\x88 GS GrindRail\x8b settings!")		
		RO.hasloaded = true
	end return true --
end)