Skip to content

Commit

Permalink
启动时候自动更新逻辑swf 暂定版
Browse files Browse the repository at this point in the history
  • Loading branch information
hormanlu committed Sep 5, 2013
1 parent 874982d commit 34b3daf
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 19 deletions.
30 changes: 12 additions & 18 deletions TeamX_Moblie/src/TeamX_Launcher.as
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package
{
import com.core.Common.Singleton;
import com.core.Utils.File.OpenFile;

import flash.display.Loader;
import flash.display.Sprite;
import flash.display.StageAlign;
Expand All @@ -8,7 +11,9 @@ package
import flash.filesystem.File;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.Capabilities;
import flash.system.LoaderContext;
import flash.utils.ByteArray;

// [SWF(width="800", height="600",frameRate="60", backgroundColor="#cccccc")]
[SWF(width="800", height="600",frameRate="60", backgroundColor="#0")]
Expand All @@ -26,31 +31,20 @@ package
this.stage.align = StageAlign.TOP_LEFT;
}
this.mouseEnabled = this.mouseChildren = false;
this.addEventListener(Event.ADDED_TO_STAGE, logo);
this.addEventListener(Event.ADDED_TO_STAGE, start);
}

/***logo显示 */
private function logo(event:Event):void
private function start(event:Event):void
{
loaderLogicSWF();
}

/***加载逻辑SWF */
private function loaderLogicSWF():void
{
var path:String = File.applicationDirectory.resolvePath("asset").url + "/TeamX_Moblie.swf";
var request:URLRequest = new URLRequest(path);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(flash.events.Event.COMPLETE , onComplete);
loader.load(request,context);

function onComplete(e:Event):void
// 如果是调试版本 先复制逻辑swf 正式打包版本会直接讲swf放进包里
Singleton.update.UpDate(onUpDate);
function onUpDate(cls:Class):void
{
clsGame = context.applicationDomain.getDefinition("TeamX_Moblie") as Class;
clsGame = cls;
init();
}

}

/***加载逻辑SWF */
Expand Down
Binary file removed TeamX_Moblie/src/asset/TeamX_Moblie.swf
Binary file not shown.
21 changes: 20 additions & 1 deletion XLib/src/com/core/Common/Singleton.as
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ package com.core.Common
/***网络管理器 */
private static var _socket:SocketManager;

/***自动更新管理器 */
private static var _update:UpDateManager;

public function Singleton()
{

Expand Down Expand Up @@ -137,7 +140,7 @@ package com.core.Common
return _enterFrame;
}

/***网络协议 对象 */
/***网络协议 对象 */
public static function get socket():SocketManager
{
if(_socket == null)
Expand All @@ -147,6 +150,16 @@ package com.core.Common
return _socket;
}

/***自动更新管理器 */
public static function get update():UpDateManager
{
if(_update == null)
{
_update = new UpDateManager;
}
return _update;
}

/***销毁 */
public static function dispose():void
{
Expand Down Expand Up @@ -205,6 +218,12 @@ package com.core.Common
_socket = null;
}

if(_update != null)
{
_update.dispose();
_update = null;
}

ScreenManager.dispose();
}

Expand Down
94 changes: 94 additions & 0 deletions XLib/src/com/core/Common/UpDateManager.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.core.Common
{
import com.core.Utils.File.OpenFile;

import flash.display.Loader;
import flash.events.Event;
import flash.filesystem.File;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.utils.ByteArray;

public class UpDateManager implements IDispose
{
private var completeFun:Function;
public function UpDateManager()
{

}


/**执行更新*/
public function UpDate(onComplete:Function):void
{
completeFun = onComplete;
// 如果是debug 则每次都覆盖更新逻辑swf
// 后面还会添加版本比对
if(Singleton.platform.Debug)
{
updateLogicSWF(onSWFSaveComplete);
}
else
{
loaderLogicSWF();
}

function onSWFSaveComplete(e:Event):void
{
loaderLogicSWF();
}

function onSWFLoaderComplete(e:Event):void
{
loaderLogicSWF();
}


}

/***加载逻辑SWF */
private function loaderLogicSWF():void
{
var path:String = File.applicationDirectory.resolvePath("asset").url + "/TeamX_Moblie.swf";
var request:URLRequest = new URLRequest(path);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(flash.events.Event.COMPLETE , onComplete);
loader.load(request,context);

function onComplete(e:Event):void
{
if(completeFun != null)
{
completeFun(context.applicationDomain.getDefinition("TeamX_Moblie"));
}
}
}


/***更新逻辑SWF */
private function updateLogicSWF(onFileOpenedToSave:Function):void
{
var path:String = File.applicationDirectory.resolvePath("asset").nativePath + "/TeamX_Moblie.swf";
var f:File = new File(path);
if(f.exists)
{
trace("逻辑swf已经存在!");
f.deleteFile();
}
var name:String = File.applicationDirectory.url + "TeamX_Moblie.swf";
f = new File(name);
var ba:ByteArray = OpenFile.open(f);
OpenFile.writeAsync(ba,path,onFileOpenedToSave);
}

public function dispose():void
{
completeFun = null;

}

}
}
48 changes: 48 additions & 0 deletions XLib/src/com/core/Utils/File/OpenFile.as
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,54 @@ package com.core.Utils.File
}


/**
* 异步方式写文件
* @param data
* @param path
* @return
*
*/
public static function writeAsync(data:*, path:String,onFileOpenedToSave:Function = null):File
{
var stream:FileStream;
var byte:ByteArray
if (data is ByteArray)
{
byte=data as ByteArray;
}
else if (data is String)
{
byte=new ByteArray();
byte.writeUTFBytes(data);
}
else
{
byte=new ByteArray();
byte.writeObject(data);
}

byte.position=0;

try
{
var _file:File=new File(path);
stream=new FileStream();
if(onFileOpenedToSave != null)
{
stream.addEventListener(Event.CLOSE, onFileOpenedToSave);
}
stream.openAsync(_file, FileMode.WRITE);
stream.writeBytes(byte);
stream.close();
return _file;
}
catch (e:Error)
{
trace("写入文件错误-------------------->>>" + e.message);
}
return null;
}

}
}
import flash.events.Event;
Expand Down

0 comments on commit 34b3daf

Please sign in to comment.