using AX.NetworkSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Xml;
using UnityEngine;
public class CreateMD5Tool
{
private static CreateMD5Tool instance;
public static CreateMD5Tool Instance
{
get
{
if (instance == null)
{
instance = new CreateMD5Tool();
}
return instance;
}
}
///
/// 创建配置文件
///
/// 需要生成MD5文件的路径
/// 生成MD5的配置文件
///
public void CreateManifestXml(string directoryPath, string fileVersionPath, Dictionary data)
{
setFileXML(directoryPath, fileVersionPath, data);
}
public void CreateManifestXml(string directoryPath, string fileVersionPath)
{
var data = new Dictionary();
setFileXML(directoryPath, fileVersionPath, data);
}
private void setFileXML(string directoryPath, string fileVersionPath, Dictionary data)
{
if (Directory.Exists(directoryPath))
{
string[] strArray = Directory.GetFiles(directoryPath, "*", SearchOption.AllDirectories);
for (int i = 0; i < strArray.Length; i++)
{
if (!strArray[i].EndsWith(".meta") && !strArray[i].EndsWith(".manifest"))
{
string key = strArray[i].Replace(directoryPath, string.Empty);
//排除掉地图文件,地图资源单独更新,因此在进行版本更新时不需要更新地图资源,所以在生成资源配置表时剔除掉地图资源
if (!key.Contains(@"\AssetBundles\assets\mapfiles"))
{
string str = this.getFileHash(strArray[i]);
data.Add(key, str);
}
}
}
SaveVersionFile(data, fileVersionPath);
}
}
///
/// 测试用:将文件夹内的所有文件(除xml外)上传到服务器
///
///
public void UploadDirectoryFiles(string directoryPath,string id)
{
if (Directory.Exists(directoryPath))
{
string[] strArray = Directory.GetFiles(directoryPath, "*", SearchOption.AllDirectories);
for(int i = 0; i < strArray.Length; i++)
{
if(!strArray[i].EndsWith(".xml") && !strArray[i].EndsWith(".meta") && !strArray[i].EndsWith(".manifest"))
{
string fileName = strArray[i].Replace(directoryPath, id + "/");
NetworkManager.Default.UploadFileAsync(strArray[i], fileName, null, null, OnUploadCompelete);
}
}
}
}
private void OnUploadCompelete(string fullName, string fileName)
{
Debug.Log("已将" + fullName + "上传至服务器,文件名:" + fileName);
}
//获取文件的MD5
private string getFileHash(string filePath)
{
try
{
FileStream stream = new FileStream(filePath, FileMode.Open);
int length = (int)stream.Length;
byte[] buffer = new byte[length];
stream.Read(buffer, 0, length);
stream.Close();
byte[] buffer2 = new MD5CryptoServiceProvider().ComputeHash(buffer);
string str = string.Empty;
foreach (byte num2 in buffer2)
{
str = str + Convert.ToString(num2, 0x10);
}
return str;
}
catch (FileNotFoundException exception)
{
Console.WriteLine(exception.Message);
return string.Empty;
}
}
//保存资源版本配置文件
private void SaveVersionFile(Dictionary data,string fileVersionPath)
{
if (File.Exists(fileVersionPath))
{
File.Delete(fileVersionPath);
}
XmlDocument document = new XmlDocument();
XmlDeclaration xmldecl = document.CreateXmlDeclaration("1.0", "UTF-8", "yes");
document.AppendChild(xmldecl);
XmlElement newChild = document.CreateElement("Files");
document.AppendChild(newChild);
foreach (KeyValuePair pair in data)
{
XmlElement element2 = document.CreateElement("File");
newChild.AppendChild(element2);
element2.SetAttribute("FileName", pair.Key);
element2.SetAttribute("Hash", pair.Value);
}
document.Save(fileVersionPath);
document = null;
}
//获取xml文件版本信息
private Dictionary getMainFileVersion(string path)
{
Dictionary fileHashID = new Dictionary();
XmlDocument document = new XmlDocument();
document.Load(path);
XmlNodeList nodeList = document.SelectSingleNode("Files").ChildNodes;
foreach (XmlElement fileNode in nodeList)
{
var fileName = fileNode.GetAttribute("FileName");
var fileHash = fileNode.GetAttribute("Hash");
fileHashID.Add(fileName, fileHash);
}
document = null;
return fileHashID;
}
public SaveData CheckUpdate(string preFileXml,string curFileXml)
{
SaveData data = new SaveData();
//获取本地的HashID
Dictionary preHashID = getMainFileVersion(preFileXml);
//获取最新的HashID
Dictionary curHashID = getMainFileVersion(curFileXml);
foreach(string fileName in curHashID.Keys)
{
//1、先判断本地资源列表是否存在,若不存在则属于新资源
if (!preHashID.ContainsKey(fileName))
{
data.updateList.Add(fileName);
}
//2、再判断HashID是否相等,若不等则属于新资源
else if (preHashID[fileName] != curHashID[fileName])
{
data.updateList.Add(fileName);
}
}
foreach(string fileName in preHashID.Keys)
{
//若不存在则属于应删除的资源
if (!curHashID.ContainsKey(fileName))
{
data.delList.Add(fileName);
}
}
return data;
}
///
/// 文件移动
///
///
///
public void FileMove(string sourceFile, string targetFile)
{
if (File.Exists(targetFile))
{
File.Delete(targetFile);
}
else
{
string directoryName = Path.GetDirectoryName(targetFile);
if (!Directory.Exists(directoryName) && directoryName != "")
{
Directory.CreateDirectory(directoryName);
}
}
try
{
File.Move(sourceFile, targetFile);
}
catch (Exception exception)
{
Debug.LogError(exception.Message);
}
}
}
public class SaveData
{
public List updateList;
public List delList;
public SaveData()
{
updateList = new List();
delList = new List();
}
}