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<string,long> filehash_server=new dictionary<string, long> (); Dictionary<string,long> filehash_local=new dictionary<string, long> (); list<string> needupdatefile = new list<string> (); 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<data. length;i++) { Filehash_server[data[i].get ("name", "")] = Data[i].get ("CRC", (long) 0); } Server_crc_data = www.bytes; } Debug.Log ("Load Local Filehash"); Loading a file from the local MD5 table may not 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<data. length;i++) { Filehash_local[data[i].get ("name", "")] = Data[i].get ("CRC", (long) 0); } } catch (Exception e) { Debug.Log (E.message); } Debug.Log ("Check filehash"); Calculate files that need to be updated foreach (keyvaluepair<string,long> data in Filehash_server) { Update the required files if (!filehash_local. ContainsKey (data. Key) | | Filehash_local[data. Key]! = data. Value) { Needupdatefile.add (data. Key); } } Debug.Log ("Update File"); Download and store for (int i=0;i<needupdatefile.count;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 (); Save 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) { Save the most recent file MD5 value table 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"); } } |