Skip to content

Instantly share code, notes, and snippets.

@butihuzi
Last active August 29, 2015 14:05
Show Gist options
  • Save butihuzi/b79d8f33e79f3398d416 to your computer and use it in GitHub Desktop.
Save butihuzi/b79d8f33e79f3398d416 to your computer and use it in GitHub Desktop.
Flash cookie 封装类
package ez.utils
{
import flash.events.NetStatusEvent;
import flash.net.SharedObject;
import flash.net.SharedObjectFlushStatus;
import flash.utils.ByteArray;
public class CookieUtil extends Object
{
static private const MAX_SAVE:int = 1073741824 ;
static private const DFT_SAVE:int = 90000 ; // 90KB
static private const soCfg:SharedObject = SharedObject.getLocal("ez_cfg") ;
/**保证进了游戏才开启flush*/
static public var isFlushing:Boolean = true ;
/**是否缓存所有文件资源,与游戏中的设置无关*/
static public var isUseCookieAssets:Boolean = false ;
/**是否缓存游戏中的设置*/
static public var isUseCookieConfig:Boolean = true ;
/**如果连续1000ms没有新的写入动作则执行写入。优化方向主要考虑:场景加载过程的最佳写入时机*/
//static private var _flushTime:int = 5000 ;
//static private var _flushTimer:int = uint.MAX_VALUE ;
static private var _flushCache:Vector.<SharedObject> = new Vector.<SharedObject> ;
/**
* 将KV键值对写入文件fileName中
* @param fileName 文件名
*/
static public function setAssets(fileName:String, ...kv):void
{
if (!isUseCookieAssets || kv == null || kv.length == 0)
return ;
var so:SharedObject = getSO(fileName) ;
if (so == null)
return ;
for (var i:int = 0; i < kv.length; i+=2)
{
if (i+1 < kv.length)
so.data[kv[i]] = kv[i+1] ;
}
flushCookie(so, MAX_SAVE) ;
}
/**
* 获取Cookie,如果是ByteArray,会尝试解压缩。
* @param fileName 文件名
* @param key 键
*/
static public function getAsset(fileName:String, key:String):*
{
if (!isUseCookieAssets || key == null || key.length == 0)
return null ;
var so:SharedObject = getSO(fileName) ;
if (so == null)
return null ;
var v:* = so.data[key];
if (v is ByteArray)
{
try { v.uncompress() ;}
catch(error:Error) {}
}
return v ;
}
/**
* 将KV键值对存在设置里。
*/
static public function setConfig(...kv):void
{
if (!isUseCookieConfig || soCfg == null || kv == null || kv.length == 0)
return ;
for (var i:int = 0; i < kv.length; i+=2)
{
if (i+1 < kv.length)
soCfg.data[kv[i]] = kv[i+1] ;
}
flushCookie(soCfg, DFT_SAVE) ;
}
static public function getConfig(key:String):*
{
if (!isUseCookieConfig || soCfg == null || key == null || key.length == 0)
return null ;
var v:* = soCfg.data[key];
return v ;
}
static private function flushCookie(so:SharedObject, size:int = DFT_SAVE):void
{
if (isFlushing)
return ;
try
{
if (so.flush(size) == SharedObjectFlushStatus.PENDING)
{
isFlushing = true;
so.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
}
else
isFlushing = false;
}
catch(e:Error)
{
if (size == MAX_SAVE) isUseCookieAssets = false;
if (size == DFT_SAVE) isUseCookieConfig = false;
trace("Cookie flush error:" + e.message);
}
function onNetStatus(soEvent:NetStatusEvent):void
{
so.removeEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
if (soEvent.info.code == "SharedObject.Flush.Success")
flushCookie(so) ;
else
setConfig("agree", false) ;
}
}
static public function clear(fileName:String, key:String = ''):void
{
var so:SharedObject = getSO(fileName) ;
if (so == null)
return ;
if (key && key.length > 0)
so.data[key] = null;
else
so.clear();
}
/**
* 获取cookie lang
*/
static public function get lang():String
{
return getConfig("lang") ;
}
/**
* 设置cookie lang
*/
static public function set lang(value:String):void
{
setConfig("lang", value) ;
}
static public function tryToGetUserAgree():void
{
isUseCookieAssets = true ;
isUseCookieConfig = true ;
if (soCfg)
{
isFlushing = false ;
soCfg.data['agree'] = true ;
flushCookie(soCfg, MAX_SAVE) ;
}
}
static private function getSO(fileName:String, localPath:String=null, secure:Boolean=false):SharedObject
{
if (!isUseCookieConfig && !isUseCookieAssets)
return null ;
var so:SharedObject = null ;
try
{
//去掉协议头、替换特殊字符、替换..
fileName = fileName.replace(/\w*:\/\//i, "") ;
fileName = fileName.replace(/(~|%|&|\\|;|:|"|'|,|<|>|\?|#|,|\s)/g, "_") ;
fileName = fileName.replace("..", ".") ;
so = SharedObject.getLocal(fileName, localPath, secure) ;
}
catch(error:Error) { trace("Cookie getSO error:", fileName, localPath) }
return so ;
}
static public function get isNeedShowCookieTip():Boolean
{
return isUseCookieAssets && isUseCookieConfig && !getConfig("agree") ;
}
static private function addFlushCache(so:SharedObject):void
{
var len:int = _flushCache.length ;
for (var i:int = 0; i < len; i++)
{
if (_flushCache[i] == so)
return ;
}
_flushCache[len] = so ;
}
static public function test():void
{
getSO("http://a.b.com/d/file.xml") ;
getSO("../d/file.xml") ;
getSO("./d/file.xml") ;
getSO("/d/file.xml") ;
getSO("/d/file.xml") ;
getSO("~~a.xml") ;
getSO("%&\;:'<#?a.xml") ;
getSO("%&\;:'<#? a.xml") ;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment