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");
}
}