/ Published in: ActionScript 3
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package desktop.update { import desktop.download.FileDownloader; import desktop.events.DesktopEvent; import flash.desktop.NativeApplication; import flash.desktop.Updater; import flash.events.Event; import flash.events.EventDispatcher; import flash.events.HTTPStatusEvent; import flash.events.IEventDispatcher; import flash.events.IOErrorEvent; import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.URLRequestHeader; [Event(name="checkStart", type="desktop.events.DesktopEvent")] [Event(name="checkComplete",type="desktop.events.DesktopEvent")] [Event(name="verIOError", type="desktop.events.DesktopEvent")] [Event(name="updateStart", type="desktop.events.DesktopEvent")] [Event(name="progress", type="desktop.events.DesktopEvent")] [Event(name="downloadError",type="desktop.events.DesktopEvent")] public class UpdateManager extends EventDispatcher { private var fileDownloader:FileDownloader = new FileDownloader(); private var versionLoader:URLLoader = new URLLoader(); private var updXmlMng:UpdateXmlManager = UpdateXmlManager.getManager(); private var httpStatus:int = 0; private var updateVersion:String; public var totalBytes:int = 0; public var loadedBytes:int = 0; public var currentVersion:String; public var versionFileURL:String; public function UpdateManager(target:IEventDispatcher=null) { super(target); } //============================================== // �������� � ����� public function autoUpdateStandby():void{ this.versionFileURL = updXmlMng.getUpdateXmlPath(); this.addEventListener(DesktopEvent.CHECK_COMPLETE,updateEnd,false,0,true); this.addEventListener(DesktopEvent.VER_IO_ERROR,updateEnd,false,0,true); this.addEventListener(DesktopEvent.DOWNLOAD_ERROR,updateEnd,false,0,true); // this.checkVersion(); } private function updateEnd(event:DesktopEvent):void{ this.dispatchEvent(new DesktopEvent(DesktopEvent.UPDATE_END)); } //============================================== public function checkVersion():void { onInit(); } private function onInit():void { setApplicatioNameAndVersion(); fileDownloader.addEventListener(DesktopEvent.PROGRESS,onDownloadProgress,false,0,true); fileDownloader.addEventListener(DesktopEvent.IO_ERROR,onDownloadError,false,0,true); fileDownloader.addEventListener(DesktopEvent.SAVE_COMPLETE,onDownloadComplete,false,0,true); onUpdate(); } private function onUpdate():void { var req:URLRequest = new URLRequest(versionFileURL); var header:URLRequestHeader = new URLRequestHeader("pragma","no-cache"); req.requestHeaders.push(header); versionLoader.addEventListener(IOErrorEvent.IO_ERROR,onCheckError,false,0,true); versionLoader.addEventListener(Event.COMPLETE,versionLoadComplete,false,0,true); versionLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS,httpStatusHandler,false,0,true); versionLoader.load(req); } private function versionLoadComplete(event:Event):void { if (httpStatus!=200) return; try { updateVersion = XML(event.currentTarget.data).version; } catch (err:Error) { onDownloadError(); return; } var tmpStr:String = updateVersion.replace("Version ",""); var tmpStrArray:Array = tmpStr.split("."); var updateVersionNum:int = getVersionNum(tmpStrArray); currentVersion = currentVersion.replace("Version ",""); var currentVersionArray:Array = currentVersion.split("."); var currentVersionNum:int = getVersionNum(currentVersionArray); // trace(currentVersionNum,updateVersionNum); if (updateVersionNum>currentVersionNum) { this.dispatchEvent(new DesktopEvent(DesktopEvent.UPDATE_START)); fileDownloader.download(XML(event.currentTarget.data).url,FileDownloader.TEMP_FILE); } else { this.dispatchEvent(new DesktopEvent(DesktopEvent.CHECK_COMPLETE)); } } private function getVersionNum(arg:Array):int { var i:int; for (i=0;i<arg.length;i++) { if (arg[i].length==1) arg[i] = "00" + arg[i]; else if (arg[i].length==2) arg[i] = "0" + arg[i]; } return int(arg.join().replace(/,/g,"")); } private function httpStatusHandler(event:HTTPStatusEvent):void { httpStatus = event.status; if (event.status!=200) this.dispatchEvent(new DesktopEvent(DesktopEvent.VER_IO_ERROR)); } private function onCheckError(event:IOErrorEvent):void { this.dispatchEvent(new DesktopEvent(DesktopEvent.VER_IO_ERROR)); } //============================================== private function onDownloadProgress(event:DesktopEvent):void { totalBytes = event.currentTarget.bytesTotal; loadedBytes = event.currentTarget.bytesLoaded; // trace(loadedBytes + "/" + totalBytes); this.dispatchEvent(new DesktopEvent(DesktopEvent.PROGRESS)); } private function onDownloadError(event:DesktopEvent=null):void { this.dispatchEvent(new DesktopEvent(DesktopEvent.DOWNLOAD_ERROR)); } private function onDownloadComplete(event:DesktopEvent):void { var updater:Updater = new Updater(); try { // trace(updateVersion); updater.update(fileDownloader.file,updateVersion); } catch (e:Error) { // trace(e.message); this.dispatchEvent(new DesktopEvent(DesktopEvent.CHECK_COMPLETE)); } } private function setApplicatioNameAndVersion():void { var appXML:XML = NativeApplication.nativeApplication.applicationDescriptor; var ns:Namespace = appXML.namespace(); currentVersion = appXML.ns::version; } } }