アップデート用 as


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package desktop.update
  2. {
  3. import desktop.download.FileDownloader;
  4. import desktop.events.DesktopEvent;
  5.  
  6. import flash.desktop.NativeApplication;
  7. import flash.desktop.Updater;
  8. import flash.events.Event;
  9. import flash.events.EventDispatcher;
  10. import flash.events.HTTPStatusEvent;
  11. import flash.events.IEventDispatcher;
  12. import flash.events.IOErrorEvent;
  13. import flash.net.URLLoader;
  14. import flash.net.URLRequest;
  15. import flash.net.URLRequestHeader;
  16.  
  17. [Event(name="checkStart", type="desktop.events.DesktopEvent")]
  18. [Event(name="checkComplete",type="desktop.events.DesktopEvent")]
  19. [Event(name="verIOError", type="desktop.events.DesktopEvent")]
  20. [Event(name="updateStart", type="desktop.events.DesktopEvent")]
  21. [Event(name="progress", type="desktop.events.DesktopEvent")]
  22. [Event(name="downloadError",type="desktop.events.DesktopEvent")]
  23.  
  24. public class UpdateManager extends EventDispatcher
  25. {
  26. private var fileDownloader:FileDownloader = new FileDownloader();
  27. private var versionLoader:URLLoader = new URLLoader();
  28. private var updXmlMng:UpdateXmlManager = UpdateXmlManager.getManager();
  29.  
  30. private var httpStatus:int = 0;
  31. private var updateVersion:String;
  32. public var totalBytes:int = 0;
  33. public var loadedBytes:int = 0;
  34. public var currentVersion:String;
  35. public var versionFileURL:String;
  36.  
  37. public function UpdateManager(target:IEventDispatcher=null)
  38. {
  39. super(target);
  40. }
  41.  
  42. //==============================================
  43. // �������� � �����
  44.  
  45. public function autoUpdateStandby():void{
  46. this.versionFileURL = updXmlMng.getUpdateXmlPath();
  47. this.addEventListener(DesktopEvent.CHECK_COMPLETE,updateEnd,false,0,true);
  48. this.addEventListener(DesktopEvent.VER_IO_ERROR,updateEnd,false,0,true);
  49. this.addEventListener(DesktopEvent.DOWNLOAD_ERROR,updateEnd,false,0,true);
  50. // this.checkVersion();
  51. }
  52.  
  53. private function updateEnd(event:DesktopEvent):void{
  54. this.dispatchEvent(new DesktopEvent(DesktopEvent.UPDATE_END));
  55. }
  56.  
  57. //==============================================
  58.  
  59. public function checkVersion():void {
  60. onInit();
  61. }
  62.  
  63. private function onInit():void {
  64. setApplicatioNameAndVersion();
  65.  
  66. fileDownloader.addEventListener(DesktopEvent.PROGRESS,onDownloadProgress,false,0,true);
  67. fileDownloader.addEventListener(DesktopEvent.IO_ERROR,onDownloadError,false,0,true);
  68. fileDownloader.addEventListener(DesktopEvent.SAVE_COMPLETE,onDownloadComplete,false,0,true);
  69.  
  70. onUpdate();
  71. }
  72.  
  73. private function onUpdate():void {
  74. var req:URLRequest = new URLRequest(versionFileURL);
  75. var header:URLRequestHeader = new URLRequestHeader("pragma","no-cache");
  76. req.requestHeaders.push(header);
  77. versionLoader.addEventListener(IOErrorEvent.IO_ERROR,onCheckError,false,0,true);
  78. versionLoader.addEventListener(Event.COMPLETE,versionLoadComplete,false,0,true);
  79. versionLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS,httpStatusHandler,false,0,true);
  80. versionLoader.load(req);
  81. }
  82.  
  83. private function versionLoadComplete(event:Event):void {
  84. if (httpStatus!=200) return;
  85. try {
  86. updateVersion = XML(event.currentTarget.data).version;
  87. } catch (err:Error) {
  88. onDownloadError();
  89. return;
  90. }
  91.  
  92. var tmpStr:String = updateVersion.replace("Version ","");
  93. var tmpStrArray:Array = tmpStr.split(".");
  94. var updateVersionNum:int = getVersionNum(tmpStrArray);
  95. currentVersion = currentVersion.replace("Version ","");
  96. var currentVersionArray:Array = currentVersion.split(".");
  97. var currentVersionNum:int = getVersionNum(currentVersionArray);
  98.  
  99. // trace(currentVersionNum,updateVersionNum);
  100.  
  101. if (updateVersionNum>currentVersionNum) {
  102. this.dispatchEvent(new DesktopEvent(DesktopEvent.UPDATE_START));
  103. fileDownloader.download(XML(event.currentTarget.data).url,FileDownloader.TEMP_FILE);
  104. } else {
  105. this.dispatchEvent(new DesktopEvent(DesktopEvent.CHECK_COMPLETE));
  106. }
  107. }
  108.  
  109. private function getVersionNum(arg:Array):int {
  110. var i:int;
  111. for (i=0;i<arg.length;i++) {
  112. if (arg[i].length==1) arg[i] = "00" + arg[i];
  113. else if (arg[i].length==2) arg[i] = "0" + arg[i];
  114. }
  115. return int(arg.join().replace(/,/g,""));
  116. }
  117.  
  118. private function httpStatusHandler(event:HTTPStatusEvent):void {
  119. httpStatus = event.status;
  120. if (event.status!=200) this.dispatchEvent(new DesktopEvent(DesktopEvent.VER_IO_ERROR));
  121. }
  122.  
  123. private function onCheckError(event:IOErrorEvent):void {
  124. this.dispatchEvent(new DesktopEvent(DesktopEvent.VER_IO_ERROR));
  125. }
  126.  
  127. //==============================================
  128.  
  129. private function onDownloadProgress(event:DesktopEvent):void {
  130. totalBytes = event.currentTarget.bytesTotal;
  131. loadedBytes = event.currentTarget.bytesLoaded;
  132. // trace(loadedBytes + "/" + totalBytes);
  133. this.dispatchEvent(new DesktopEvent(DesktopEvent.PROGRESS));
  134. }
  135.  
  136. private function onDownloadError(event:DesktopEvent=null):void {
  137. this.dispatchEvent(new DesktopEvent(DesktopEvent.DOWNLOAD_ERROR));
  138. }
  139.  
  140. private function onDownloadComplete(event:DesktopEvent):void {
  141. var updater:Updater = new Updater();
  142. try {
  143. // trace(updateVersion);
  144. updater.update(fileDownloader.file,updateVersion);
  145. }
  146. catch (e:Error) {
  147. // trace(e.message);
  148. this.dispatchEvent(new DesktopEvent(DesktopEvent.CHECK_COMPLETE));
  149. }
  150. }
  151.  
  152. private function setApplicatioNameAndVersion():void {
  153. var appXML:XML = NativeApplication.nativeApplication.applicationDescriptor;
  154. var ns:Namespace = appXML.namespace();
  155. currentVersion = appXML.ns::version;
  156. }
  157. }
  158. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.