g_VR = g_VR or {}

local convars, convarValues = vrmod.GetConvars()

vrmod.AddCallbackedConvar("vrmod_configversion", nil, "5")

if convars.vrmod_configversion:GetString() ~= convars.vrmod_configversion:GetDefault() then
	timer.Simple(1,function()
		for k,v in pairs(convars) do
			pcall(function() v:Revert() end) --reverting certain convars makes error
		end
	end)
end



if CLIENT then

	g_VR.scale = 0
	g_VR.origin = Vector(0,0,0)
	g_VR.originAngle = Angle(0,0,0)
	g_VR.viewModel = nil --this will point to either the viewmodel, worldmodel or nil
	g_VR.viewModelMuzzle = nil
	g_VR.viewModelPos = Vector(0,0,0)
	g_VR.viewModelAng = Angle(0,0,0)
	g_VR.usingWorldModels = false
	g_VR.active = false
	g_VR.threePoints = false --hmd + 2 controllers
	g_VR.sixPoints = false --hmd + 2 controllers + 3 trackers
	g_VR.tracking = {}
	g_VR.input = {}
	g_VR.changedInputs = {}
	g_VR.errorText = ""
	
	--todo move some of these to the files where they belong
	vrmod.AddCallbackedConvar("vrmod_althead", nil, "0")
	vrmod.AddCallbackedConvar("vrmod_autostart", nil, "0")
	vrmod.AddCallbackedConvar("vrmod_scale", nil, "38.7")
	vrmod.AddCallbackedConvar("vrmod_heightmenu", nil, "1")
	vrmod.AddCallbackedConvar("vrmod_floatinghands", nil, "0")
	vrmod.AddCallbackedConvar("vrmod_desktopview", nil, "3")
	vrmod.AddCallbackedConvar("vrmod_useworldmodels", nil, "0")
	vrmod.AddCallbackedConvar("vrmod_laserpointer", nil, "0")
	vrmod.AddCallbackedConvar("vrmod_znear", nil, "1.0")
	vrmod.AddCallbackedConvar("vrmod_characterEyeHeight", nil, "66.8", nil, "", nil, nil, tonumber)--cvarName, valueName, defaultValue, flags, helptext, min, NAME_1, conversionFunc, callbackFunc
	vrmod.AddCallbackedConvar("vrmod_characterHeadToHmdDist", nil, "6.3", nil, "", nil, nil, tonumber)--cvarName, valueName, defaultValue, flags, helptext, min, NAME_1, conversionFunc, callbackFunc
	vrmod.AddCallbackedConvar("vrmod_oldcharacteryaw", nil, "0")
	vrmod.AddCallbackedConvar("vrmod_controlleroffset_x", nil, "-15")
	vrmod.AddCallbackedConvar("vrmod_controlleroffset_y", nil, "-1")
	vrmod.AddCallbackedConvar("vrmod_controlleroffset_z", nil, "5")
	vrmod.AddCallbackedConvar("vrmod_controlleroffset_pitch", nil, "50")
	vrmod.AddCallbackedConvar("vrmod_controlleroffset_yaw", nil, "0")
	vrmod.AddCallbackedConvar("vrmod_controlleroffset_roll", nil, "0")
	vrmod.AddCallbackedConvar("vrmod_postprocess", nil, "0", nil, nil, nil, nil, tobool, function(val)
		if g_VR.view then
			g_VR.view.dopostprocess = val
		end
	end)
	
	hook.Add("VRMod_Menu","vrmod_options",function(frame)
		local form = frame.Setting