function AccessoryUpdate(idx)
	local as = gSportsRNA
	local accessory = as:GetTable("wvAccessory", idx)
	
	local state = as:GetTable("wvState") -- to get the lighting and weather
	db.accessory[idx].stadiumLightID = as:GetInt(state, "wvAttribStadLightID")
	db.accessory[idx].stadiumID = as:GetInt(state, "wvAttribStadID")
	db.accessory[idx].envLighting = as:GetInt(state, "wvAttribEnvLighting")
	
	--RM
	local player = as:GetTable("wvPlayer", math.floor(idx/10))
	db.accessory[idx].playerid = as:GetInt(player, "playerassetid")
	db.accessory[idx].teamid = as:GetInt(player, "teamid") 
	
	--GET SLEEVE LENGTH
	local weather = as:GetInt(state, "wvAttribStadWeather" )
	local seasonal = as:GetInt(player, "seasonaljersey")
	local winterAcc = getWinterAccessoriesWeather(weather)
	local jerseySleeveLength = as:GetInt(player, "jerseySleeveLength")
	seasonal = getWinterAccessories(db.accessory[idx].playerid,seasonal)
	jerseySleeveLength = getSleeveLength(db.accessory[idx].playerid,jerseySleeveLength)
	if (winterAcc) then
		jerseySleeveLength = seasonal
	end

    -- Calculation below is to minimize the amount of textures loaded for accessories. Forces the sharing of 1 colormap for left/right meshes.
    -- This is assuming all accessories that require left and right variations have model = even# for left, odd# for right
    -- AND
    -- all single-variation accessories have even# for model
    -- Ideal solution: retrieve modelvar from database so that we don't have to make assumptions here
    local modelNum = as:GetInt(accessory, "accessoryModel")
	
	local modelNum2 = modelNum
	
	--ACCESSORY MODEL
	modelNum = getAccessoryModel(db.accessory[idx].playerid,modelNum)
	
	--DISABLE
	if (acc) then 
	modelNum = -1
	end
	
	--IF LONG SLEEVES
	if (jerseySleeveLength > 0) then
	if (modelNum == 8) then
		modelNum = -1
	end
	if (modelNum == 9) then
		modelNum = -1
	end
	if ((modelNum > 23) and (modelNum < 28)) then
		modelNum = -1
	end
	end
	
	
	db.accessory[idx].model = modelNum - math.abs(modelNum % 2)
	db.accessory[idx].modelvar = math.abs(modelNum % 2)
	db.accessory[idx].color = as:GetInt(accessory, "accessoryColor")
	db.accessory[idx].material = "accessory"
	db.accessory[idx].accessoryname = "accessory"
	db.accessory[idx].coloridx = 0
	db.accessory[idx].isgkglove = 0
	
	--ACCESSORY COLOUR
	db.accessory[idx].color = getAccessoryColour(db.accessory[idx].playerid,modelNum2,db.accessory[idx].color)
	
	-- special case for gkgloves
	if ( db.accessory[idx].model == 18 ) then
		db.accessory[idx].model = db.accessory[idx].color
		db.accessory[idx].accessoryname = "gkglove"
		db.accessory[idx].material = "gkglove"
		db.accessory[idx].isgkglove = 1
	elseif (db.accessory[idx].model > 27 and db.accessory[idx].model < 32) then
		-- referee cards are 28 and 30, just use diffuse.
		db.accessory[idx].material = "accessories_matte"
	-- elseif (db.accessory[idx].model > 4) then
		-- db.accessory[idx].coloridx = db.accessory[idx].color
		-- db.accessory[idx].color = 0  -- use 0 for runtime recolouring
		
	else
		-- referee watches, referee earpieces
		db.accessory[idx].coloridx = db.accessory[idx].color
		db.accessory[idx].color = 0  -- use 0 for runtime recolouring
		db.accessory[idx].material = "accessories_matte"
	end
	
		
	
end

---------------------------------------------------------------------------------------------------
-- Get accessory color to dispatch to shader 
function GetAccessoryColorARGB(idx)
	-- gamma'd list r*r g*g b*b
	accessoryColorList = { 0x00ffffff, 0x000a0a0a, 0x00000072, 0x00720000, 
						   0x00cece00, 0x00000900, 0x009b0900, 0x00090026, 
						   0x00060000, 0x00e112e1, 0x00130001, 0x000038e1, 
						   0x0000001f, 0x003f3f3f }

	local clr = accessoryColorList[db.accessory[idx].coloridx + 1] 
	
	if (clr == nil) then
		clr = 0x00ffffff
	end
	return clr
end

---------------------------------------------------------------------------------------------------
function GetAccessoryMesh(idx)
	if (db.accessory[idx].isgkglove == 1) then
		return "data/sceneassets/${db.accessory[?].accessoryname}/${db.accessory[?].accessoryname}_${db.accessory[?].color}.rx3"
	else
		return "data/sceneassets/${db.accessory[?].accessoryname}/${db.accessory[?].accessoryname}_${db.accessory[?].model}_${db.accessory[?].modelvar}.rx3"
	end
end

---------------------------------------------------------------------------------------------------
function GetAccessoryTexture(idx)
	if (db.accessory[idx].isgkglove == 1) then
		return "data/sceneassets/gkglove/playergkglove_${db.accessory[?].playerid}_0_textures.rx3;data/sceneassets/${db.accessory[?].accessoryname}/${db.accessory[?].accessoryname}_${db.accessory[?].model}_textures.rx3"
	else
		return "${GetRMAccessory(?)}data/sceneassets/${db.accessory[?].accessoryname}/${db.accessory[?].accessoryname}_${db.accessory[?].model}_${db.accessory[?].color}_textures.rx3"
	end
end

---------------------------------------------------------------------------------------------------
function AccessoryAssetBind(accessory)
	local gr = gRenderables
   	local lod = 0
	
	gr:AddCallback(accessory, lod, "AccessoryUpdate(?)")
	gr:AddAsset(accessory, lod, "shader", "data/fifarna/shader.big")
	gr:AddAsset(accessory, lod, "accessorymesh", "${GetAccessoryMesh(?)}")
	gr:AddAsset(accessory, lod, "accessorytex", "${GetAccessoryTexture(?)}" )
	gr:AddAsset(accessory, lod, "charcmn", "data/sceneassets/charactercmn/charactercmn_${db.accessory[?].envLighting}.rx3")	
	
	local part = "accessorymesh"
	gr:CreateMaterialFromAttribulator(accessory, lod, part, "accessory", "${db.accessory[?].material}" )
	gr:SetTexture(accessory, lod, part, "textures", "diffuseTexture", "accessorytex", "${db.accessory[?].accessoryname}_cm")
	gr:SetTexture(accessory, lod, part, "textures", "normalMap", "accessorytex", "${db.accessory[?].accessoryname}_nm")
	gr:SetTexture(accessory, lod, part, "textures", "coeffMap", "accessorytex", "${db.accessory[?].accessoryname}_coeff")
	gr:SetTexture(accessory, lod, part, "textures", "envDiffuseTexture", "charcmn", "envd_")
	gr:SetTexture(accessory, lod, part, "textures", "envSpecTexture", "charcmn", "envs_")
	gr:SetConstantARGB(accessory, lod, part, "global", "MaterialColour", "${GetAccessoryColorARGB(?)}")
	gr:SetTextureFromRuntime(accessory, lod, part, "textures", "coverageMap", "covmap_${db.accessory[?].stadiumID}_${db.accessory[?].stadiumLightID}")
	
	return accessory
end





acc = false

function removeAccessories()
acc = true
end



accessoryModel = {}
accessoryColour = {}

function swapAccessory(player,old,new,colour)

if (accessoryModel[player] == nil) then
accessoryModel[player] = {}
end

if (accessoryColour[player] == nil) then
accessoryColour[player] = {}
end

accessoryModel[player][old] = new
accessoryColour[player][old] = colour
end

function getAccessoryModel(player,old)

if (accessoryModel[player] ~= nil) then
if (accessoryModel[player][old] ~= nil) then
return accessoryModel[player][old]
end
end

return old
end

function getAccessoryColour(player,old,colour)

if (accessoryColour[player] ~= nil) then
if (accessoryColour[player][old] ~= nil) then
return accessoryColour[player][old]
end
end

return colour
end


function GetRMAccessory(idx)
	local pct = ""
	pct = pct.."data/sceneassets/"..db.accessory[idx].accessoryname.."/specific"..db.accessory[idx].accessoryname.."_"..db.accessory[idx].playerid.."_"..db.accessory[idx].teamid.."_"..(db.accessory[idx].model+db.accessory[idx].modelvar).."_textures.rx3;"
	pct = pct.."data/sceneassets/"..db.accessory[idx].accessoryname.."/specific"..db.accessory[idx].accessoryname.."_0_"..db.accessory[idx].teamid.."_"..(db.accessory[idx].model+db.accessory[idx].modelvar).."_textures.rx3;"
	pct = pct.."data/sceneassets/"..db.accessory[idx].accessoryname.."/specific"..db.accessory[idx].accessoryname.."_"..db.accessory[idx].playerid.."_0_"..(db.accessory[idx].model+db.accessory[idx].modelvar).."_textures.rx3;"
	pct = pct.."data/sceneassets/"..db.accessory[idx].accessoryname.."/generic"..db.accessory[idx].accessoryname.."_"..db.accessory[idx].model.."_0_"..db.accessory[idx].coloridx.."_textures.rx3;"
	return pct
end

--Revolution Mod 14 1.0
--Edited by scouser09