⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autoupdate.as

📁 flex 实现的一个showcase 喜欢flex的朋友可以
💻 AS
字号:
package com.finetune.apollo.autoupdate
{
	import flash.desktop.Updater;
	
	public class AutoUpdate
	{
		
		
		import flash.events.*;
		import flash.net.*;
		import flash.utils.*;
		import flash.filesystem.*;
		import flash.errors.EOFError;
		import flash.filesystem.File;
		import com.finetune.apollo.autoupdate.events.*;
		import flash.system.*;
	
	
		// CONSISTANT EVENT VARIABLES;
		static public const UPDATED_FILE_READ_COMPLETE:String="updatedFileReadComplete";
		static public const UPDATED_FILE_FOUND:String="updatedFileFound";
		static public const UPDATED_FILE_NOT_FOUND:String="updatedFileNotFound";
		static public const UPDATED_FILE_COULD_NOT_LOAD:String="updatedFileCouldNotLoad";
		static public const UPDATED_FILE_COULD_NOT_OPEN:String="updatedFileCouldNotOpen";
		static public const UPDATED_FILE_READ_PROGRESS:String="updatedFileReadProgress";
		static public const UPDATED_FILE_SECURITY_ERROR:String="updatedFileSecurityError";
		static public const UPDATED_FILE_COULD_NOT_READ:String="updatedFileCouldNotRead";
		// static public const LOCAL_FILE_WRITING_PROGRESS:String="localFileWritingProgress";
		static public const LOCAL_FILE_WRITING_BEGIN:String="localFileWritingBegin";
		static public const LOCAL_FILE_WRITING_COMPLETE:String="localFileWritingComplete";
		static public const INITIATING_UPDATE:String="initiatingUpdate";
		

		// LOCAL VARIABLES
		private var urlString:String;
		private var urlReq:URLRequest = new URLRequest(urlString);
		private var urlStream:URLStream = new URLStream();
		private var fileData:ByteArray = new ByteArray();
		private var downloadToFile:File;
		private var updatedAirFilePath:String;
		

		[Bindable]
		private var version:String = "";
		
		private var testFile:File;
		
		// START IT OFF ////////////////////////////////////////////////////////////////
		// ARGUMENTS
		//   updatePath - path to updated air ... ex. http://www.preview.teknision.com/public/teknision/personal/mike/AutoUpdate.air
		//   downloadPath - path to download location for air ... ex. 
		
		private static var singleton:AutoUpdate

		public static function get() : AutoUpdate{
			if ( singleton == null )
				singleton = new AutoUpdate( arguments.callee );
			return singleton;
		}
		
		//NOTE: AS3 does not allow for private or protected constructors
		public function AutoUpdate(caller:Function=null){ 
			
			if(caller != AutoUpdate.get){
				throw new Error ("Singleton is a singleton class, use getInstance() instead");
			}
			
			if (AutoUpdate.singleton != null ){
				throw new Error( "Only one Singleton instance should be instantiated" ); 
			}

		}


		
		public function run(updatePath:String,downloadFile:File,version_number:String):void{

			updatedAirFilePath = updatePath;
			downloadToFile = downloadFile;
			version=version_number;

			urlReq = new URLRequest(updatedAirFilePath);
			urlStream.addEventListener(Event.COMPLETE, handleLoadComplete);
			urlStream.addEventListener(HTTPStatusEvent.HTTP_STATUS, handleHTTPStatus);
			urlStream.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
			urlStream.addEventListener(Event.OPEN, handleOpenError);
			urlStream.addEventListener(ProgressEvent.PROGRESS, handleProgress);
			urlStream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurityError);
			urlStream.load(urlReq);
			
		}

		private function handleLoadComplete(event:Event):void {
			trace("DOWNLOAD OF UPDATED FILE COMPLETE");
			this.dispatchEvent(new Event(UPDATED_FILE_READ_COMPLETE));
			urlStream.addEventListener("EOFError", handleEOFError);
			urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
			writeAirFile();
		}
		 

		private function writeAirFile():void {

			var fileStream:FileStream = new FileStream();
			fileStream.addEventListener(Event.CLOSE, fileClosed);
			// fileStream.addEventListener(ProgressEvent.PROGRESS, handleFileWritingProgress);
			this.dispatchEvent(new Event(LOCAL_FILE_WRITING_BEGIN));
			fileStream.openAsync(downloadToFile, FileMode.WRITE);
			fileStream.writeBytes(fileData, 0, fileData.length);
			fileStream.close();
			trace("Temp File Written Complete");
		 }
		 
		private function fileClosed(event:Event):void {
			this.dispatchEvent(new Event(LOCAL_FILE_WRITING_COMPLETE));
		    var updater:Updater = new Updater();
			var airFile:File = downloadToFile;
			updater.update(airFile,version);
			this.dispatchEvent(new Event(INITIATING_UPDATE));
		 }
		 
		/////////////////////////////////////////////////////////////////////////////// 
		// EVENT HANDLERS /////////////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////
		
		private function handleHTTPStatus(event:HTTPStatusEvent):void{
			if(event.status == 200){
				this.dispatchEvent(new Event(UPDATED_FILE_FOUND));
			}else if(event.status == 404){
				this.dispatchEvent(new Event(UPDATED_FILE_NOT_FOUND));
			}
		}

		private function handleIOError(event:IOErrorEvent):void{
			this.dispatchEvent(new Event(UPDATED_FILE_COULD_NOT_LOAD));
		}

		private function handleOpenError(event:Event):void{
			this.dispatchEvent(new Event(UPDATED_FILE_COULD_NOT_OPEN));
		}
					 
		private function handleEOFError():void{
			this.dispatchEvent(new Event(UPDATED_FILE_COULD_NOT_READ));
		}
		
		private function handleProgress(event:ProgressEvent):void{
			
			var myProgress:UpdateFileDownloadProgress = new UpdateFileDownloadProgress(event,UPDATED_FILE_READ_PROGRESS);
			trace("PROGRESS : "+myProgress.percent);
			this.dispatchEvent(myProgress);
		}
		
		private function handleSecurityError(event:SecurityErrorEvent):void{
			var mySecurityErrorEvent:UpdateFileSecurityError = new UpdateFileSecurityError(event.text,UPDATED_FILE_SECURITY_ERROR);
			this.dispatchEvent(mySecurityErrorEvent);
		}
		
		/* private function handleFileWritingProgress(event:ProgressEvent):void{
			var myProgress:LocalFileWritingProgress = new LocalFileWritingProgress(event,LOCAL_FILE_WRITING_PROGRESS);
			trace("PROGRESS 2 : "+myProgress.percent);
			this.dispatchEvent(myProgress);
		}*/
	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -