02生成AB包对比文件

利用文件名、文件大小、文件md5码生成对比文件,用来记录文件信息,之后再下载文件时就可以根据这些数据判断是否需要更新文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using System.Security.Cryptography;

public class CreateABCompare
{
    [MenuItem("AB包工具/生成对比文件")]
    public static void GenerateCompareInfoFileTest()
    {
        GenerateCompareInfoFile("Assets/ArtRes/AB/PC", "Assets/ArtRes/AB/PC/ABCompareInfo.txt");
    }

    public static void GenerateCompareInfoFile(string directoryPath, string savepath)
    {
        DirectoryInfo directionInfo = Directory.CreateDirectory(directoryPath);
        FileInfo[] fileInfos = directionInfo.GetFiles();
        GenerateCompareInfoFile(fileInfos, savepath);
    }

    public static void GenerateCompareInfoFile(FileInfo[] fileInfos,string savepath)
    {
        StringBuilder stringBuilder = new StringBuilder();  
        foreach (FileInfo item in fileInfos)
        {
            if (item.Extension != "") continue;
            stringBuilder.Append($"{item.Name} {item.Length} {GetMD5(item.FullName)}");
            //分隔符
            stringBuilder.Append("|\r\n");
        }
        string compareInfo = stringBuilder.ToString().Substring(0, stringBuilder.Length - 3);
        File.WriteAllText(savepath, compareInfo);
        AssetDatabase.Refresh();
    }
    public static string GetMD5(string filepath)
    {
        MD5 mD5 = new MD5CryptoServiceProvider();
        StringBuilder mD5_string = new StringBuilder();
        using (FileStream fs = File.OpenRead(filepath))
        {
            byte[] mD5_bytes = mD5.ComputeHash(fs);
            fs.Close();
            //转换为16进制
            foreach (byte item in mD5_bytes)
            {
                mD5_string.Append(item.ToString("X2"));
            }
        }
        return mD5_string.ToString();
    }
}