local addinManager = XLGetObject("Xunlei.Thunder.AddinManager")
addinManager:LoadAddinMainDll("MallAddin")

local addinPath
local exePath
local shell
local codecV1 = 1
local codecV4 = 20
local bInstalling = false
local bInstalled = false

local function GetCodecsSetupExe()
	local PathTool = XLGetObject("Xunlei.Thunder.Path")
	shell = XLGetObject("Xunlei.UIEngine.OSShell")
	--获取插件的路径
    addinPath = PathTool:GetAddinsPath() .. "MallAddin\\"
	--根据插件路径，组合DLL的路径
    exePath = addinPath .. "XLVideoCodec.exe"
	return exePath
end

local function OnSuccess()
	if bInstalled then
		return
	end
	-- 执行安装
	if shell:QueryFileExists(exePath) then
		-- 检测插件版本，判断执行安装
		local verUtil = XLGetObject("xl.win32.version_utility")
		local v1, v2, v3, v4 = verUtil:get_file_version(exePath)
		if v4 ~= nil and v4 >= codecV4 then
			local osShell = XLGetObject('Xunlei.UIEngine.OSShell')
			bInstalled = true
			osShell:ShellExecute(0, "open", exePath, "", addinPath, "SW_SHOW")			
			bInstalling = false
		end
	end
end

function DownloadFxCodecs()
	if bInstalling then
		return
	end
	
	bInstalling = true
	local dll = GetCodecsSetupExe()	
	if shell:QueryFileExists(dll) then
		-- 检测插件版本，判断执行安装
		local verUtil = XLGetObject("xl.win32.version_utility")
		local v1, v2, v3, v4 = verUtil:get_file_version(dll)
		if v4 ~= nil and v4 >= codecV4 then
			OnSuccess() -- 直接安装
			do
				return
			end
		end
	end
   --从服务器上拉取插件包
	local statdata = {}
	statdata["mc"] = {}
	statdata["mc"]["pluinst"] = 1
	local stat_obj = XLGetObject( "Xunlei.Stat" )
	if stat_obj ~= nil then
		  stat_obj:AddStat( statdata )
	end
	local obj = XLGetGlobal("xunlei.LuaDownloadMouble")
	--将插件拉取到本地
	obj.StartDownloadMouble("MallAddin", "迅雷数字影片解码器", false, OnSuccess, addinPath)
end
XLSetGlobal("fx.DownloadFxCodecs", DownloadFxCodecs)

local theManager = XLGetObject("Xunlei.UIEngine.XARManager")
theManager:AsynLoadXAR("MallAddin",nil)
