Skip to content

Instantly share code, notes, and snippets.

@naive924
Last active October 1, 2018 06:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naive924/70d9f94f580c5a84576d4e6c08a3dbc5 to your computer and use it in GitHub Desktop.
Save naive924/70d9f94f580c5a84576d4e6c08a3dbc5 to your computer and use it in GitHub Desktop.
AssetBundle 対象のフォルダついて、Asset の依存関係も含めて実ファイルのハッシュ値を計算する
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
public static class AssetDirHashSample
{
/// <summary>
/// テスト実行
/// </summary>
[MenuItem(@"Test/CalculateDirHash", false)]
private static void Test()
{
      DirectoryInfo directoryInfo = new DirectoryInfo (<AssetBundle 対象のフォルダを指定>);
Debug.Log("Dir=" + directoryInfo);
Debug.Log("CalculateDirHash=" + CalculateDirHash(directoryInfo));
}
/// <summary>
/// AssetBundle 対象のフォルダついて、Asset の依存関係も含めて実ファイルのハッシュ値を計算する
/// </summary>
/// <returns>AssetBundle 対象のフォルダの実ファイルハッシュ値</returns>
/// <param name="baseDir">AssetBundle 対象のフォルダ</param>
public static string CalculateDirHash(DirectoryInfo baseDir)
{
var dirQueue = new Queue<DirectoryInfo>();
dirQueue.Enqueue(baseDir);
string checksum = "";
while (dirQueue.Count > 0) {
var dir = dirQueue.Dequeue();
var subDirList = new List<DirectoryInfo>(dir.GetDirectories());
subDirList.Sort((d1, d2) => d1.FullName.CompareTo(d2.FullName));
foreach (var subDir in subDirList) {
dirQueue.Enqueue(subDir);
}
var fileList = new List<FileInfo>(dir.GetFiles());
fileList.Sort((f1, f2) => f1.FullName.CompareTo(f2.FullName));
foreach (var file in fileList) {
var path = GetAssetPathFromGlobalPath(file.FullName);
var mainAsset = AssetDatabase.LoadMainAssetAtPath(path);
if (mainAsset == null) {
continue;
}
checksum += path + "\n";
checksum += CalculateHash(mainAsset);
}
}
checksum = Md5Sum(checksum);
return checksum;
}
private static string CalculateHash(UnityEngine.Object asset)
{
string checksum = CalcAssetChecksum(asset);
UnityEngine.Object[] depAssets = EditorUtility.CollectDependencies(new UnityEngine.Object[] { asset });
List<UnityEngine.Object> depAssetList = new List<UnityEngine.Object>(depAssets);
depAssetList.Sort((a1, a2) => {
int ret = a1.GetType().FullName.CompareTo(a2.GetType().FullName);
if (ret != 0) {
return ret;
}
else {
return a1.name.CompareTo(a2.name);
}
});
foreach (UnityEngine.Object depAsset in depAssetList) {
Debug.Log("\tTargetAsset - " + depAsset.GetType() + ":" +depAsset.name);
checksum += depAsset.name + " : " + depAsset.GetType() + "\n";
checksum += CalcAssetChecksum(depAsset) + "\n";
}
return Md5Sum(checksum);
}
private static string CalcAssetChecksum(UnityEngine.Object asset)
{
string assetPath = AssetDatabase.GetAssetPath(asset);
string filePath = Application.dataPath.Replace("Assets", "") + assetPath;
if (string.IsNullOrEmpty(assetPath) || !System.IO.File.Exists(filePath)) {
return Md5Sum(asset.name);
}
else {
byte[] binaryData = File.ReadAllBytes(assetPath);
string metaInfo = File.ReadAllText(assetPath + ".meta");
return Md5Sum(Md5Sum(binaryData) + Md5Sum(metaInfo));
}
}
private static string GetAssetPathFromGlobalPath(string globalPath)
{
var startIndex = globalPath.IndexOf("Assets/");
if (startIndex > 0) {
return globalPath.Remove(0, startIndex);
}
return globalPath;
}
private static string Md5Sum(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
return Md5Sum(ue.GetBytes(strToEncrypt));
}
private static string Md5Sum(byte[] bytesToEncrypt)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytesToEncrypt);
string hashString = "";
for (int i = 0; i < hashBytes.Length; i++) {
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
return hashString.PadLeft(32, '0');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment