Unity3D AssetStore 外部工具下载
参考https://www.jianshu.com/p/3ceb18e69d11
Unity 商店更新后还是没有很好得解决下载资源失败从头再来得问题。只好查找如何离线下载得文章,在简书上有一篇16年得文章利用反射机制调用编辑器解密下载文件得方法,但是因为unity得升级,该方法已经从AssetStoreUtils类里移除了。
解决方案
既然已经删除了DecryptFile函数,那反射肯定不行,那是不是可以从反编译得角度出发,找到该函数得实现方式,来直接解压。
资料查找
// 参考文档
https://github.com/jamesjlinden/unity-decompiled
实现
// 参考文档
//https://github.com/jamesjlinden/unity-decompiled
using UnityEditor;
using System;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
class DecryptUtility
{
[MenuItem("Utility/DecryptFile")]
static void DecryptFile()
{
var inputFile = @"D:\2223333";
var key = "11122222";
//var unityEditor = typeof(Editor).Assembly;
//var assetStoreUtils = unityEditor.GetType("UnityEditor.AssetStoreUtils");
//assetStoreUtils.Invoke("DecryptFile", inputFile, inputFile + ".unitypackage", key);
DecryptFile(inputFile, inputFile + ".unitypackage", key);
}
private static void HexStringToByteArray(string hex, byte[] array, int offset)
{
if (offset + array.Length * 2 > hex.Length)
throw new ArgumentException("Hex string too short");
for (int index = 0; index < array.Length; ++index)
{
string s = hex.Substring(index * 2 + offset, 2);
array[index] = byte.Parse(s, NumberStyles.HexNumber);
}
}
public static void DecryptFile(string inputFile, string outputFile, string keyIV)
{
byte[] array1 = new byte[32];
byte[] array2 = new byte[16];
HexStringToByteArray(keyIV, array1, 0);
HexStringToByteArray(keyIV, array2, 64);
EditorUtility.DisplayProgressBar("Decrypting", "Decrypting package", 0.0f);
FileStream fileStream1 = File.Open(inputFile, System.IO.FileMode.Open);
FileStream fileStream2 = File.Open(outputFile, System.IO.FileMode.CreateNew);
long length = fileStream1.Length;
long num = 0;
AesManaged aesManaged = new AesManaged();
aesManaged.Key = array1;
aesManaged.IV = array2;
CryptoStream cryptoStream = new CryptoStream((Stream)fileStream1, aesManaged.CreateDecryptor(aesManaged.Key, aesManaged.IV), CryptoStreamMode.Read);
try
{
byte[] numArray = new byte[40960];
int count;
while ((count = cryptoStream.Read(numArray, 0, numArray.Length)) > 0)
{
fileStream2.Write(numArray, 0, count);
num += (long)count;
if (EditorUtility.DisplayCancelableProgressBar("Decrypting", "Decrypting package", (float)num / (float)length))
throw new Exception("User cancelled decryption");
}
}
finally
{
cryptoStream.Close();
fileStream1.Close();
fileStream2.Close();
EditorUtility.ClearProgressBar();
}
}
}