RescourseUpdate

using UnityEngine;
using System.Collections;
using System.Collections.Generic;  
using System.Text;  
using System.IO;

public class ResourcesUpdate : MonoBehaviour {

    private static ResourcesUpdate instance;
    public static ResourcesUpdate GetInstance ()
    {
        return instance;
    }

    //本地存储版本号
    public static readonly string LOCAL_VERSION_FILE = "LocalVersion.txt";
    public static readonly string SERVER_VERSION_FILE = "ServerVersion.txt";
    public static readonly string LOCAL_ALl_PREFAB = "LocalResources/ALL.assetbundle";
    public static readonly string SERVER_ALl_PREFAB = "ServerResources/ALL.assetbundle";

    public static string LOCAL_RES_PATH = "";
    public static string LOCAL_RES_URL = "";
    public static string SERVER_RES_URL = "";

    private List<string> LocalResVersion;  
    private List<string> ServerResVersion; 

    private List<string> NeedDownFiles;  
    private bool NeedUpdateLocalVersionFile = false; 

    public delegate void HandleFinishDownload(WWW www);

    public AssetBundle bundle;

    private bool isNeedUpdate = false;
    private int codeVersion;
    private int localResVersion;
    private int serverResVersion;

    void Awake(){
        LOCAL_RES_PATH = GameUtils.GetStreamingAssetsPath();
        LOCAL_RES_URL = GameUtils.GetStreamingAssetsPath();
        SERVER_RES_URL = GameUtils.GetStreamingAssetsPath();
        instance = this;
    }

    void Start () {

        LocalResVersion = new List<string>();  
        ServerResVersion = new List<string>(); 
        NeedDownFiles = new List<string> ();

        LoadLocalVersion ();

    }


    void LoadLocalVersion(){
        //加载本地version配置  
        StartCoroutine(DownLoad(LOCAL_RES_URL + LOCAL_VERSION_FILE, delegate(WWW localVersion)  
            {  
//                GLogger.LogWarning("本地文件路径名: " + LOCAL_RES_URL + LOCAL_VERSION_FILE);
                //保存本地的version  
                ParseLocalVersionFile(localVersion.text);  
                //加载服务端version配置  
                StartCoroutine(this.DownLoad(SERVER_RES_URL + SERVER_VERSION_FILE, delegate(WWW serverVersion)  
                    {  
//                        GLogger.LogWarning("服务器文件路径名: " + LOCAL_RES_URL + LOCAL_VERSION_FILE);
                        //保存服务端version  
                        ParseServerVersionFile(serverVersion.text);  
                        //计算出需要重新加载的资源  
                        CompareVersion();  
                        //加载需要更新的资源  
                        DownLoadRes();  
                    }));  

            }));  
    }

    private IEnumerator DownLoad(string url, HandleFinishDownload finishFun)  
    {  
        WWW www = new WWW(url);  
        yield return www;  
        if (finishFun != null)  
        {  
            finishFun(www);  
        }  
        www.Dispose();  
    }  

    private void ParseLocalVersionFile(string content)  
    {  
        if (content == null || content.Length == 0)  
        {  
            return;  
        }  

        string[] items = content.Split(new char[] { '\n' });  
        foreach (string item in items)  
        {   
            LocalResVersion.Add (item);
        } 

        ParseVersion.GetInstance ().LoadVersion (content);
        localResVersion = ParseVersion.GetInstance ().GetResVersionNum ();

    }  

    private void ParseServerVersionFile(string content)  
    {  
        if (content == null || content.Length == 0)  
        {  
            return;  
        }  

        string[] items = content.Split(new char[] { '\n' });  
        foreach (string item in items)  
        {   
            ServerResVersion.Add (item);
        } 

        ParseVersion.GetInstance ().LoadVersion (content);
        serverResVersion = ParseVersion.GetInstance ().GetResVersionNum ();

    } 



    private void CompareVersion()  
    {   
        if (localResVersion != serverResVersion) {
            GLogger.LogWarning ("资源版本bu相等!!!");
            isNeedUpdate = true;
        } else {
            isNeedUpdate = false;
            GLogger.LogWarning ("资源版本相等!!!");
        }

    }


    //依次加载需要更新的资源  
    private void DownLoadRes()  
    {  
        if (!isNeedUpdate)  
        {  
            StartCoroutine(Show());  
            return;  
        }  

        UpdateLocalVersionFile();  
        StartCoroutine(this.DownLoad(SERVER_RES_URL + SERVER_ALl_PREFAB, delegate(WWW w)  
            {  
                //将下载的资源替换本地就的资源  
                ReplaceLocalRes(SERVER_ALl_PREFAB, w.bytes); 
                isNeedUpdate = false;
                DownLoadRes();  
            }));  
    } 

    //显示资源  
    private IEnumerator Show()  
    {  
        WWW asset = new WWW(LOCAL_RES_URL + LOCAL_ALl_PREFAB);  
        yield return asset;
        bundle = asset.assetBundle;  
        GameObject ob =  Instantiate(bundle.Load("AddFriends")) as GameObject;  
        bundle.Unload(false);  
    }  

    //更新本地的version配置  
    private void UpdateLocalVersionFile()  
    {  
        if (isNeedUpdate)  
        {  
            StringBuilder versions = new StringBuilder();  
            foreach (var item in ServerResVersion)  
            {  
                versions.Append(item).Append("\n");  
            }  
            GLogger.LogWarning ("====================更新LocalVersion!");
            FileStream stream = new FileStream(LOCAL_RES_PATH.Replace("file://","") + LOCAL_VERSION_FILE, FileMode.Create);  
            byte[] data = Encoding.UTF8.GetBytes(versions.ToString());  
            stream.Write(data, 0, data.Length);  
            stream.Flush();  
            stream.Close();  
        }  
        //加载显示对象  
//        StartCoroutine(Show());  
    }  

    private void ReplaceLocalRes(string fileName, byte[] data)  
    {  
        string filePath = LOCAL_RES_PATH.Replace("file://","") + LOCAL_ALl_PREFAB;  
        FileStream stream = new FileStream(filePath, FileMode.Create);  
        stream.Write(data, 0, data.Length);  
        stream.Flush();  
        stream.Close();  

    }  


}





using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;

public class Version {
    public string version;
    public bool isRelease;
    public bool isRecharge;
    public string ResUrl;
//    public string updateurl;
//    public string KEY;
//    public ServerListServer serverListServer;
}
    

//public class ServerListServer { 
//    public int ROOT_SERVER_INDEX;
//    public string ROOT_HOST;
//    public int ROOT_POST;
//}

public class ParseVersion : MonoBehaviour {

    private static ParseVersion instance;
    public static ParseVersion GetInstance ()
    {
        return instance;
    }

    public Version ver = null;
//    public ServerListServer sls = null;

    string[] versionNum;
//    string str;

    void Awake(){
        instance = this;
    }


//    void Start () {
//        LoadVersion();
//        //sls = ver.serverListServer;
//        //Debug.Log(sls.ROOT_HOST);
//
//    }

    public void LoadVersion (string versionInfo) {
//        UnityEngine.TextAsset ta = Resources.Load("version") as TextAsset;
//        str = ta.text;
        ver = JsonMapper.ToObject<Version>(versionInfo);
        //JsonData jd = JsonMapper.ToObject(str);
        //JsonData jsd = jd["serverListServer"];
        //Debug.Log(jsd.Count);
    }

    public int GetCodeVersionNum(){
        int codeNum = 0;
        versionNum = ver.version.Split(new char[]{'.'});
        codeNum = int.Parse(versionNum [2]);
        return codeNum;
    }

    public int GetResVersionNum(){
        int resNum = 0;
        versionNum = ver.version.Split(new char[]{'.'});
        resNum = int.Parse(versionNum [1]);
        GLogger.LogWarning ("资源版本号:" + resNum);
        return resNum;
    }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值