unity 自动将文件上传_unity3d 自己主动文件更新系统

该博客介绍了如何在Unity中实现一个自动更新文件的系统。通过加载服务器和本地的文件CRC校验值进行对比,找出需要更新的文件并下载,最后保存最新的文件MD5值表,确保文件同步。
摘要由CSDN通过智能技术生成

public static IEnumerator  UpdateDataFromServer(UpdateProgress up)

{

string server_datapath = Engine.Instance.server_datapath;

string local_datapath = Engine.Instance.local_datapath;

byte[] server_crc_data  = null;

Dictionary filehash_server=new Dictionary();

Dictionary  filehash_local=new Dictionary();

List needUpdateFile = new List();

Debug.Log("Load Server FileHash");

using(WWW www = new WWW(server_datapath+"crc.txt"))

{

yield return www;

if (!String.IsNullOrEmpty(www.error))

{

Debug.Log("Load Filehash Failed");

up(1.0f);

yield break;

}

JSDocument.JSNode node = new JSDocument.JSNode("filehash",www.text);

Document.SNode[] data = node.getChildren("filehash");

for(int i=0;i

{

filehash_server[data[i].get("name","")] = data[i].get("crc",(long)0);

}

server_crc_data = www.bytes;

}

Debug.Log("Load Local FileHash");

//从本地载入文件MD5表。可能没有

try

{

JSDocument.JSNode node = new JSDocument.JSNode("filehash",System.IO.File.ReadAllText(local_datapath+"crc.txt"));

Document.SNode[] data = node.getChildren("filehash");

for(int i=0;i

{

filehash_local[data[i].get("name","")] = data[i].get("crc",(long)0);

}

}

catch(Exception e)

{

Debug.Log(e.Message);

}

Debug.Log("Check FileHash");

//计算须要更新的文件

foreach(KeyValuePair data in filehash_server)

{

//更新须要的文件

if(!filehash_local.ContainsKey(data.Key) || filehash_local[data.Key] != data.Value)

{

needUpdateFile.Add(data.Key);

}

}

Debug.Log("Update File");

//下载并存储

for(int i=0;i

{

using(WWW www = new WWW(server_datapath+needUpdateFile[i]))

{

yield return www;

byte[] bytes = null;

if (!String.IsNullOrEmpty(www.error))

{

Debug.Log(www.error);

yield break;

}

else

{

bytes = www.bytes;

}

up((float)i/needUpdateFile.Count);

string path = local_datapath+ needUpdateFile[i];

Debug.Log(path);

FileStream fs = new FileStream(path,FileMode.Create);

fs.Write(bytes,0,bytes.Length);

fs.Flush();

fs.Close();

//              //保存

//              BinaryWriter writer;

//              FileInfo t =new FileInfo(local_datapath+ needUpdateFile[i]);

//                if(!t.Exists)

//              {

//                  writer = new BinaryWriter(t.Open(FileMode.OpenOrCreate));

//              }

//              else

//              {

//                  t.Delete();

//                  writer = new BinaryWriter(t.Open(FileMode.Create));

//              }

//              writer.Write(bytes);

//              writer.Close();

}

}

Debug.Log("Save FileHash");

if(needUpdateFile.Count>0)

{

//保存最新的文件MD5值表

//          FileStream fs = new FileStream(local_datapath+"crc.txt",FileMode.Create);

//          fs.Write(server_crc_data,0,server_crc_data.Length);

//          fs.Flush();

//            fs.Close();

BinaryWriter writer;

FileInfo t =new FileInfo(local_datapath+"crc.txt");

if(!t.Exists)

{

writer = new BinaryWriter(t.Open(FileMode.Create));

}

else

{

t.Delete();

writer = new BinaryWriter(t.Open(FileMode.Create));

}

writer.Write(server_crc_data);

writer.Close();

Debug.Log(local_datapath+"crc.txt");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值